forked from sql-js/sql.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.d.ts
301 lines (269 loc) · 11.8 KB
/
types.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Type definitions for sql.js 1.4
// Project: https://github.com/sql-js/sql.js
// Definitions by: Florian Imdahl <https://github.com/ffflorian>
// Yehyoung Kang <https://github.com/pastelmind>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="node" />
/// <reference types="emscripten" />
type SqlValue = number | string | Uint8Array | null;
type BindValue = SqlValue | boolean;
type ParamsObject = Record<string, BindValue>;
type ParamsCallback = (obj: ParamsObject) => void;
type SqlJsConfig = Partial<EmscriptenModule>;
type BindParams = BindValue[] | ParamsObject | null;
interface QueryExecResult {
columns: string[];
values: SqlValue[][];
}
interface StatementIteratorResult {
/** `true` if there are no more available statements */
done: boolean;
/** the next available Statement (as returned by `Database.prepare`) */
value: Statement;
}
interface SqlJsStatic {
Database: typeof Database;
Statement: typeof Statement;
}
interface InitSqlJsStatic {
(config?: SqlJsConfig): Promise<SqlJsStatic>;
readonly default: this;
}
declare class Database {
/**
* Represents an SQLite database
* @see [https://sql.js.org/documentation/Database.html#Database](https://sql.js.org/documentation/Database.html#Database)
*
* @param data An array of bytes representing an SQLite database file
*/
constructor(data?: ArrayLike<number> | Buffer | null);
/**
* Close the database, and all associated prepared statements. The
* memory associated to the database and all associated statements will
* be freed.
*
* **Warning**: A statement belonging to a database that has been closed
* cannot be used anymore.
*
* Databases must be closed when you're finished with them, or the
* memory consumption will grow forever
* @see [https://sql.js.org/documentation/Database.html#["close"]](https://sql.js.org/documentation/Database.html#%5B%22close%22%5D)
*/
close(): void;
/**
* Register a custom function with SQLite
* @see [https://sql.js.org/documentation/Database.html#["create_function"]](https://sql.js.org/documentation/Database.html#%5B%22create_function%22%5D)
*
* @param name the name of the function as referenced in SQL statements.
* @param func the actual function to be executed.
*/
create_function(name: string, func: (...args: any[]) => any): Database;
/**
* Execute an sql statement, and call a callback for each row of result.
*
* Currently this method is synchronous, it will not return until the
* callback has been called on every row of the result. But this might
* change.
* @see [https://sql.js.org/documentation/Database.html#["each"]](https://sql.js.org/documentation/Database.html#%5B%22each%22%5D)
*
* @param sql A string of SQL text. Can contain placeholders that will
* be bound to the parameters given as the second argument
* @param params Parameters to bind to the query
* @param callback Function to call on each row of result
* @param done A function that will be called when all rows have been
* retrieved
*/
each(
sql: string,
params: BindParams,
callback: ParamsCallback,
done: () => void
): Database;
each(sql: string, callback: ParamsCallback, done: () => void): Database;
/**
* Execute an SQL query, and returns the result.
*
* This is a wrapper against `Database.prepare`, `Statement.bind`, `Statement.step`, `Statement.get`, and `Statement.free`.
*
* The result is an array of result elements. There are as many result elements as the number of statements in your sql string (statements are separated by a semicolon)
* @see [https://sql.js.org/documentation/Database.html#["exec"]](https://sql.js.org/documentation/Database.html#%5B%22exec%22%5D)
*
* @param sql a string containing some SQL text to execute
* @param params When the SQL statement contains placeholders, you can
* pass them in here. They will be bound to the statement before it is
* executed. If you use the params argument as an array, you **cannot**
* provide an sql string that contains several statements (separated by
* `;`). This limitation does not apply to params as an object.
*/
exec(sql: string, params?: BindParams): QueryExecResult[];
/**
* Exports the contents of the database to a binary array
* @see [https://sql.js.org/documentation/Database.html#["export"]](https://sql.js.org/documentation/Database.html#%5B%22export%22%5D)
*/
export(): Uint8Array;
/**
* Returns the number of changed rows (modified, inserted or deleted) by
* the latest completed `INSERT`, `UPDATE` or `DELETE` statement on the
* database. Executing any other type of SQL statement does not modify
* the value returned by this function.
* @see [https://sql.js.org/documentation/Database.html#["getRowsModified"]](https://sql.js.org/documentation/Database.html#%5B%22getRowsModified%22%5D)
*/
getRowsModified(): number;
/**
* Analyze a result code, return null if no error occured, and throw an
* error with a descriptive message otherwise
* @see [https://sql.js.org/documentation/Database.html#["handleError"]](https://sql.js.org/documentation/Database.html#%5B%22handleError%22%5D)
*/
handleError(): null | never;
/**
* Iterate over multiple SQL statements in a SQL string. This function
* returns an iterator over Statement objects. You can use a `for..of`
* loop to execute the returned statements one by one.
* @see [https://sql.js.org/documentation/Database.html#["iterateStatements"]](https://sql.js.org/documentation/Database.html#%5B%22iterateStatements%22%5D)
*
* @param sql a string of SQL that can contain multiple statements
*/
iterateStatements(sql: string): StatementIterator;
/**
* Prepare an SQL statement
* @see [https://sql.js.org/documentation/Database.html#["prepare"]](https://sql.js.org/documentation/Database.html#%5B%22prepare%22%5D)
*
* @param sql a string of SQL, that can contain placeholders (`?`, `:VVV`, `:AAA`, `@AAA`)
* @param params values to bind to placeholders
*/
prepare(sql: string, params?: BindParams): Statement;
/**
* Execute an SQL query, ignoring the rows it returns.
* @see [https://sql.js.org/documentation/Database.html#["run"]](https://sql.js.org/documentation/Database.html#%5B%22run%22%5D)
*
* @param sql a string containing some SQL text to execute
* @param params When the SQL statement contains placeholders, you can
* pass them in here. They will be bound to the statement before it is
* executed. If you use the params argument as an array, you **cannot**
* provide an sql string that contains several statements (separated by
* `;`). This limitation does not apply to params as an object.
*/
run(sql: string, params?: BindParams): Database;
}
declare class Statement {
/**
* Bind values to the parameters, after having reseted the statement. If
* values is null, do nothing and return true.
*
* SQL statements can have parameters, named '?', '?NNN', ':VVV',
* '@VVV', '$VVV', where NNN is a number and VVV a string. This function
* binds these parameters to the given values.
*
* Warning: ':', '@', and '$' are included in the parameters names
*
* ### Value types
*
* |Javascript type|SQLite type|
* |-|-|
* |number|REAL, INTEGER|
* |boolean|INTEGER|
* |string|TEXT|
* |Array, Uint8Array|BLOB|
* |null|NULL|
* @see [https://sql.js.org/documentation/Statement.html#["bind"]](https://sql.js.org/documentation/Statement.html#%5B%22bind%22%5D)
*
* @param values The values to bind
*/
bind(values?: BindParams): boolean;
/**
* Free the memory used by the statement
* @see [https://sql.js.org/documentation/Statement.html#["free"]](https://sql.js.org/documentation/Statement.html#%5B%22free%22%5D)
*/
free(): boolean;
/**
* Free the memory allocated during parameter binding
* @see [https://sql.js.org/documentation/Statement.html#["freemem"]](https://sql.js.org/documentation/Statement.html#%5B%22freemem%22%5D)
*/
freemem(): void;
/**
* Get one row of results of a statement. If the first parameter is not
* provided, step must have been called before.
* @see [https://sql.js.org/documentation/Statement.html#["get"]](https://sql.js.org/documentation/Statement.html#%5B%22get%22%5D)
*
* @param params If set, the values will be bound to the statement
* before it is executed
*/
get(params?: BindParams): SqlValue[];
/**
* Get one row of result as a javascript object, associating column
* names with their value in the current row
* @see [https://sql.js.org/documentation/Statement.html#["getAsObject"]](https://sql.js.org/documentation/Statement.html#%5B%22getAsObject%22%5D)
*
* @param params If set, the values will be bound to the statement, and
* it will be executed
*/
getAsObject(params?: BindParams): ParamsObject;
/**
* Get the list of column names of a row of result of a statement.
* @see [https://sql.js.org/documentation/Statement.html#["getColumnNames"]](https://sql.js.org/documentation/Statement.html#%5B%22getColumnNames%22%5D)
*/
getColumnNames(): string[];
/**
* Get the SQLite's normalized version of the SQL string used in
* preparing this statement. The meaning of "normalized" is not
* well-defined: see
* [the SQLite documentation](https://sqlite.org/c3ref/expanded_sql.html).
* @see [https://sql.js.org/documentation/Statement.html#["getNormalizedSQL"]](https://sql.js.org/documentation/Statement.html#%5B%22getNormalizedSQL%22%5D)
*/
getNormalizedSQL(): string;
/**
* Get the SQL string used in preparing this statement.
* @see [https://sql.js.org/documentation/Statement.html#["getSQL"]](https://sql.js.org/documentation/Statement.html#%5B%22getSQL%22%5D)
*/
getSQL(): string;
/**
* Reset a statement, so that it's parameters can be bound to new
* values. It also clears all previous bindings, freeing the memory used
* by bound parameters.
* @see [https://sql.js.org/documentation/Statement.html#["reset"]](https://sql.js.org/documentation/Statement.html#%5B%22reset%22%5D)
*/
reset(): void;
/**
* Shorthand for bind + step + reset Bind the values, execute the
* statement, ignoring the rows it returns, and resets it
* @param values Value to bind to the statement
*/
run(values?: BindParams): void;
/**
* Execute the statement, fetching the the next line of result, that can
* be retrieved with `Statement.get`.
* @see [https://sql.js.org/documentation/Statement.html#["step"]](https://sql.js.org/documentation/Statement.html#%5B%22step%22%5D)
*/
step(): boolean;
}
/**
* An iterator over multiple SQL statements in a string, preparing and
* returning a Statement object for the next SQL statement on each
* iteration.
*
* You can't instantiate this class directly, you have to use a Database
* object in order to create a statement iterator
* @see [https://sql.js.org/documentation/StatementIterator.html#StatementIterator](https://sql.js.org/documentation/StatementIterator.html#StatementIterator)
*/
declare class StatementIterator
implements Iterator<Statement>, Iterable<Statement>
{
[Symbol.iterator](): Iterator<Statement>;
/**
* Get any un-executed portions remaining of the original SQL string
* @see [https://sql.js.org/documentation/StatementIterator.html#["getRemainingSQL"]](https://sql.js.org/documentation/StatementIterator.html#%5B%22getRemainingSQL%22%5D)
*/
getRemainingSql(): string;
/**
* Prepare the next available SQL statement
* @see [https://sql.js.org/documentation/StatementIterator.html#["next"]](https://sql.js.org/documentation/StatementIterator.html#%5B%22next%22%5D)
*/
next(): StatementIteratorResult;
}
export {
BindParams,
BindValue,
Database, InitSqlJsStatic, ParamsCallback, ParamsObject,
QueryExecResult, SqlJsConfig, SqlJsStatic, SqlValue, StatementIteratorResult
};