UNPKG

9.29 kBTypeScriptView Raw
1// W3C spec: http://www.w3.org/TR/webdatabase/#database
2// Spec revision: 2010-11-18
3// NOTE: the W3C websql spec has been deprecated
4
5// uncomment to integrate with Window global object
6interface Window extends WindowDatabase {}
7interface WorkerUtils extends WorkerUtilsDatabase {}
8
9// util interfaces
10interface DOMString extends String {}
11interface ObjectArray extends Array<any> {}
12
13// [Supplemental, NoInterfaceObject]
14interface WindowDatabase {
15 openDatabase(
16 name: DOMString,
17 version: DOMString,
18 displayName: DOMString,
19 estimatedSize: number,
20 creationCallback?: DatabaseCallback,
21 ): Database;
22}
23
24// [Supplemental, NoInterfaceObject]
25interface WorkerUtilsDatabase {
26 openDatabase(
27 name: DOMString,
28 version: DOMString,
29 displayName: DOMString,
30 estimatedSize: number,
31 creationCallback?: DatabaseCallback,
32 ): Database;
33
34 openDatabaseSync(
35 name: DOMString,
36 version: DOMString,
37 displayName: DOMString,
38 estimatedSize: number,
39 creationCallback?: DatabaseCallback,
40 ): DatabaseSync;
41}
42
43// [Callback = FunctionOnly, NoInterfaceObject]
44interface DatabaseCallback {
45 /*handleEvent*/ (database: Database): void;
46}
47
48/** 4.3 Asynchronous database API - The transaction() and readTransaction() methods takes
49 * one to three arguments. When called, these methods must immediately return and then
50 * asynchronously run the transaction steps with the transaction callback being the
51 * first argument, the error callback being the second argument, if any, the success
52 * callback being the third argument, if any, and with no preflight operation or
53 * postflight operation
54 */
55interface Database {
56 /*readonly/const*/ version: DOMString;
57
58 transaction(
59 callback: SQLTransactionCallback,
60 errorCallback?: SQLTransactionErrorCallback,
61 successCallback?: SQLVoidCallback,
62 ): void;
63
64 readTransaction(
65 callback: SQLTransactionCallback,
66 errorCallback?: SQLTransactionErrorCallback,
67 successCallback?: SQLVoidCallback,
68 ): void;
69
70 /** The changeVersion() method allows scripts to atomically verify the version number and change
71 * it at the same time as doing a schema update. When the method is invoked, it must immediately
72 * return, and then asynchronously run the transaction steps with the transaction callback being
73 * the third argument, the error callback being the fourth argument, the success callback being
74 * the fifth argument
75 */
76 changeVersion(
77 oldVersion: DOMString,
78 newVersion: DOMString,
79 callback?: SQLTransactionCallback,
80 errorCallback?: SQLTransactionErrorCallback,
81 successCallback?: SQLVoidCallback,
82 ): void;
83}
84
85// [Callback = FunctionOnly, NoInterfaceObject]
86interface SQLVoidCallback {
87 /*handleEvent*/ (): void;
88}
89
90// [Callback = FunctionOnly, NoInterfaceObject]
91interface SQLTransactionCallback {
92 /*handleEvent*/ (transaction: SQLTransaction): void;
93}
94
95// [Callback = FunctionOnly, NoInterfaceObject]
96interface SQLTransactionErrorCallback {
97 /*handleEvent*/ (error: SQLError): void;
98}
99
100/** 4.3.1 Executing SQL statements
101 */
102interface SQLTransaction {
103 executeSql(
104 sqlStatement: DOMString,
105 arguments?: ObjectArray,
106 callback?: SQLStatementCallback,
107 errorCallback?: SQLStatementErrorCallback,
108 ): void;
109}
110
111// [Callback = FunctionOnly, NoInterfaceObject]
112interface SQLStatementCallback {
113 /*handleEvent*/ (transaction: SQLTransaction, resultSet: SQLResultSet): void;
114}
115
116// [Callback = FunctionOnly, NoInterfaceObject]
117interface SQLStatementErrorCallback {
118 /*handleEvent*/ (transaction: SQLTransaction, error: SQLError): boolean;
119}
120
121/** 4.4 Synchronous database API
122 */
123interface DatabaseSync {
124 /*readonly/const*/ version: DOMString;
125
126 transaction(callback: SQLTransactionSyncCallback): void;
127
128 readTransaction(callback: SQLTransactionSyncCallback): void;
129
130 changeVersion(oldVersion: DOMString, newVersion: DOMString, callback: SQLTransactionSyncCallback): void;
131}
132
133// [Callback = FunctionOnly, NoInterfaceObject]
134interface SQLTransactionSyncCallback {
135 /*handleEvent*/ (transaction: SQLTransactionSync): void;
136}
137
138/** 4.4.1 Executing SQL statements
139 */
140interface SQLTransactionSync {
141 executeSql(sqlStatement: DOMString, arguments?: ObjectArray): SQLResultSet;
142}
143
144/** 4.5 Database query results
145 * The insertId attribute must return the row ID of the row that the SQLResultSet
146 * object's SQL statement inserted into the database, if the statement inserted a row.
147 * If the statement inserted multiple rows, the ID of the last row must be the one returned.
148 * If the statement did not insert a row, then the attribute must instead raise an INVALID_ACCESS_ERR exception.
149 *
150 * The rowsAffected attribute must return the number of rows that were changed by the SQL statement.
151 * If the statement did not affected any rows, then the attribute must return zero.
152 * For "SELECT" statements, this returns zero (querying the database doesn't affect any rows).
153 *
154 * The rows attribute must return a SQLResultSetRowList representing the rows returned,
155 * in the order returned by the database. The same object must be returned each time.
156 * If no rows were returned, then the object will be empty (its length will be zero)
157 */
158interface SQLResultSet {
159 insertId: number;
160 rowsAffected: number;
161 rows: SQLResultSetRowList;
162}
163
164/** SQLResultSetRowList objects have a length attribute that must return the number of
165 * rows it represents (the number of rows returned by the database). This is the length.
166 * Fetching the length might be expensive, and authors are thus encouraged to avoid using
167 * it (or enumerating over the object, which implicitly uses it) where possible.
168 * The object's supported property indices are the numbers in the range zero to length-1,
169 * unless the length is zero, in which case there are no supported property indices.
170 * The item(index) attribute must return the row with the given index index.
171 * If there is no such row, then the method must return null.
172 *
173 * Each row must be represented by a native ordered dictionary data type. In the
174 * JavaScript binding, this must be Object. Each row object must have one property
175 * (or dictionary entry) per column, with those properties enumerating in the order
176 * that these columns were returned by the database. Each property must have the
177 * name of the column and the value of the cell, as they were returned by the database
178 */
179interface SQLResultSetRowList {
180 length: number;
181 item(index: number): any;
182}
183
184/** 4.6 Errors and exceptions - asynchronous database API error
185 */
186declare class SQLError {
187 static UNKNOWN_ERR: number; // = 0;
188 static DATABASE_ERR: number; // = 1;
189 static VERSION_ERR: number; // = 2;
190 static TOO_LARGE_ERR: number; // = 3;
191 static QUOTA_ERR: number; // = 4;
192 static SYNTAX_ERR: number; // = 5;
193 static CONSTRAINT_ERR: number; // = 6;
194 static TIMEOUT_ERR: number; // = 7;
195
196 code: number;
197 message: DOMString;
198}
199
200// synchronous database API error
201declare class SQLException {
202 /** Code 0 - The transaction failed for reasons unrelated to the database itself
203 * and not covered by any other error code.
204 */
205 static UNKNOWN_ERR: number; // = 0;
206 /** Code 1 - The statement failed for database reasons not covered by any other error code. */
207 static DATABASE_ERR: number; // = 1;
208 /** Code 2 - The operation failed because the actual database version was not what it should be.
209 * For example, a statement found that the actual database version no longer matched the
210 * expected version of the Database or DatabaseSync object, or the Database.changeVersion()
211 * or DatabaseSync.changeVersion() methods were passed a version that doesn't match the actual database version.
212 */
213 static VERSION_ERR: number; // = 2;
214 /** Code 3 - The statement failed because the data returned from the database was too large.
215 * The SQL "LIMIT" modifier might be useful to reduce the size of the result set.
216 */
217 static TOO_LARGE_ERR: number; // = 3;
218 /** Code 4 - The statement failed because there was not enough remaining storage space,
219 * or the storage quota was reached and the user declined to give more space to the database.
220 */
221 static QUOTA_ERR: number; // = 4;
222 /** Code 5 - The statement failed because of a syntax error, or the number of arguments did
223 * not match the number of ? placeholders in the statement, or the statement tried to use a
224 * statement that is not allowed, such as BEGIN, COMMIT, or ROLLBACK, or the statement tried
225 * to use a verb that could modify the database but the transaction was read-only. */
226 static SYNTAX_ERR: number; // = 5;
227 /** Code 6 - An INSERT, UPDATE, or REPLACE statement failed due to a constraint failure.
228 * For example, because a row was being inserted and the value given for the primary
229 * key column duplicated the value of an existing row. */
230 static CONSTRAINT_ERR: number; // = 6;
231 /** Code 7 - A lock for the transaction could not be obtained in a reasonable time. */
232 static TIMEOUT_ERR: number; // = 7;
233
234 code: number;
235 message: DOMString;
236}