@livestore/wa-sqlite 1.0.1-dev.2 → 1.0.1-dev.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/sqlite-api.js +99 -24
- package/src/types/index.d.ts +86 -8
package/package.json
CHANGED
package/src/sqlite-api.js
CHANGED
|
@@ -249,6 +249,54 @@ export function Factory(Module) {
|
|
|
249
249
|
};
|
|
250
250
|
})();
|
|
251
251
|
|
|
252
|
+
const SQLITE_SERIALIZE_NOCOPY = 0x0_01
|
|
253
|
+
|
|
254
|
+
sqlite3.serialize = (function() {
|
|
255
|
+
const fname = 'sqlite3_serialize';
|
|
256
|
+
const f = Module.cwrap(fname, ...decl('nsnn:n'));
|
|
257
|
+
return function(db, schema) {
|
|
258
|
+
verifyDatabase(db);
|
|
259
|
+
const piSize = tmpPtr[0];
|
|
260
|
+
let address = f(db, schema, piSize, 0);
|
|
261
|
+
if (address === 0) {
|
|
262
|
+
address = f(db, schema, piSize, SQLITE_SERIALIZE_NOCOPY);
|
|
263
|
+
}
|
|
264
|
+
const size = Module.getValue(piSize, '*');
|
|
265
|
+
const result = Module.HEAPU8.subarray(address, address + size);
|
|
266
|
+
return result;
|
|
267
|
+
};
|
|
268
|
+
})();
|
|
269
|
+
|
|
270
|
+
// sqlite3.serialize = (function() {
|
|
271
|
+
// const fname = 'sqlite3_serialize';
|
|
272
|
+
// const f = Module.cwrap(fname, ...decl('nsnn:n'));
|
|
273
|
+
// return function(db, schema, flags) {
|
|
274
|
+
// verifyDatabase(db);
|
|
275
|
+
// const piSize = tmpPtr[0];
|
|
276
|
+
// const address = f(db, schema, piSize, flags);
|
|
277
|
+
// const size = Module.getValue(piSize, '*');
|
|
278
|
+
// const result = Module.HEAPU8.subarray(address, address + size);
|
|
279
|
+
// return result;
|
|
280
|
+
// };
|
|
281
|
+
// })();
|
|
282
|
+
|
|
283
|
+
sqlite3.backup = (function() {
|
|
284
|
+
const fInit = Module.cwrap('sqlite3_backup_init', ...decl('nsns:n'));
|
|
285
|
+
const fStep = Module.cwrap('sqlite3_backup_step', ...decl('nn:n'));
|
|
286
|
+
const fFinish = Module.cwrap('sqlite3_backup_finish', ...decl('n:n'));
|
|
287
|
+
return function(dest, destName, source, sourceName) {
|
|
288
|
+
verifyDatabase(dest);
|
|
289
|
+
verifyDatabase(source);
|
|
290
|
+
const backup = fInit(dest, destName, source, sourceName);
|
|
291
|
+
if (backup === 0) {
|
|
292
|
+
const errMsg = Module.ccall('sqlite3_errmsg', 'string', ['number'], [dest]);
|
|
293
|
+
throw new SQLiteError(`backup failed: ${errMsg}`, SQLite.SQLITE_ERROR);
|
|
294
|
+
}
|
|
295
|
+
fStep(backup, -1);
|
|
296
|
+
return fFinish(backup);
|
|
297
|
+
};
|
|
298
|
+
})();
|
|
299
|
+
|
|
252
300
|
sqlite3.clear_bindings = (function() {
|
|
253
301
|
const fname = 'sqlite3_clear_bindings';
|
|
254
302
|
const f = Module.cwrap(fname, ...decl('n:n'));
|
|
@@ -445,7 +493,8 @@ export function Factory(Module) {
|
|
|
445
493
|
// return SQLite.SQLITE_OK;
|
|
446
494
|
// };
|
|
447
495
|
sqlite3.exec = function(db, sql, callback) {
|
|
448
|
-
|
|
496
|
+
const stmts = sqlite3.statements(db, sql);
|
|
497
|
+
for (const stmt of stmts) {
|
|
449
498
|
let columns;
|
|
450
499
|
while (sqlite3.step(stmt) === SQLite.SQLITE_ROW) {
|
|
451
500
|
if (callback) {
|
|
@@ -455,6 +504,9 @@ export function Factory(Module) {
|
|
|
455
504
|
}
|
|
456
505
|
}
|
|
457
506
|
}
|
|
507
|
+
for (const stmt of stmts) {
|
|
508
|
+
sqlite3.finalize(stmt);
|
|
509
|
+
}
|
|
458
510
|
return SQLite.SQLITE_OK;
|
|
459
511
|
};
|
|
460
512
|
|
|
@@ -510,6 +562,28 @@ export function Factory(Module) {
|
|
|
510
562
|
})();
|
|
511
563
|
|
|
512
564
|
sqlite3.open_v2 = (function() {
|
|
565
|
+
const fname = 'sqlite3_open_v2';
|
|
566
|
+
const f = Module.cwrap(fname, ...decl('snnn:n'), { async });
|
|
567
|
+
return async function(zFilename, flags, zVfs) {
|
|
568
|
+
flags = flags || SQLite.SQLITE_OPEN_CREATE | SQLite.SQLITE_OPEN_READWRITE;
|
|
569
|
+
zVfs = createUTF8(zVfs);
|
|
570
|
+
try {
|
|
571
|
+
// Allow retry operations.
|
|
572
|
+
const rc = await retry(() => f(zFilename, tmpPtr[0], flags, zVfs));
|
|
573
|
+
|
|
574
|
+
const db = Module.getValue(tmpPtr[0], '*');
|
|
575
|
+
databases.add(db);
|
|
576
|
+
|
|
577
|
+
Module.ccall('RegisterExtensionFunctions', 'void', ['number'], [db]);
|
|
578
|
+
check(fname, rc);
|
|
579
|
+
return db;
|
|
580
|
+
} finally {
|
|
581
|
+
Module._sqlite3_free(zVfs);
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
})();
|
|
585
|
+
|
|
586
|
+
sqlite3.open_v2Sync = (function() {
|
|
513
587
|
const fname = 'sqlite3_open_v2';
|
|
514
588
|
const f = Module.cwrap(fname, ...decl('snnn:n'), { async });
|
|
515
589
|
return function(zFilename, flags, zVfs) {
|
|
@@ -532,6 +606,7 @@ export function Factory(Module) {
|
|
|
532
606
|
};
|
|
533
607
|
})();
|
|
534
608
|
|
|
609
|
+
|
|
535
610
|
sqlite3.progress_handler = function(db, nProgressOps, handler, userData) {
|
|
536
611
|
verifyDatabase(db);
|
|
537
612
|
Module.progress_handler(db, nProgressOps, handler, userData);
|
|
@@ -689,10 +764,10 @@ export function Factory(Module) {
|
|
|
689
764
|
// { async: true });
|
|
690
765
|
{ async: false });
|
|
691
766
|
|
|
692
|
-
const
|
|
767
|
+
const stmts = [];
|
|
693
768
|
|
|
694
769
|
const onFinally = [];
|
|
695
|
-
try {
|
|
770
|
+
// try {
|
|
696
771
|
// Encode SQL string to UTF-8.
|
|
697
772
|
const utf8 = new TextEncoder().encode(sql);
|
|
698
773
|
|
|
@@ -754,16 +829,16 @@ export function Factory(Module) {
|
|
|
754
829
|
if (stmt) {
|
|
755
830
|
mapStmtToDB.set(stmt, db);
|
|
756
831
|
// yield stmt;
|
|
757
|
-
|
|
832
|
+
stmts.push(stmt);
|
|
758
833
|
}
|
|
759
834
|
} while (stmt);
|
|
760
|
-
} finally {
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
}
|
|
835
|
+
// } finally {
|
|
836
|
+
// while (onFinally.length) {
|
|
837
|
+
// onFinally.pop()();
|
|
838
|
+
// }
|
|
839
|
+
// }
|
|
765
840
|
|
|
766
|
-
return
|
|
841
|
+
return stmts;
|
|
767
842
|
};
|
|
768
843
|
|
|
769
844
|
sqlite3.step = (function() {
|
|
@@ -884,22 +959,22 @@ export function Factory(Module) {
|
|
|
884
959
|
// This function is used to automatically retry failed calls that
|
|
885
960
|
// have pending retry operations that should allow the retry to
|
|
886
961
|
// succeed.
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
962
|
+
async function retry(f) {
|
|
963
|
+
let rc;
|
|
964
|
+
do {
|
|
965
|
+
// Wait for all pending retry operations to complete. This is
|
|
966
|
+
// normally empty on the first loop iteration.
|
|
967
|
+
if (Module.retryOps.length) {
|
|
968
|
+
await Promise.all(Module.retryOps);
|
|
969
|
+
Module.retryOps = [];
|
|
970
|
+
}
|
|
896
971
|
|
|
897
|
-
|
|
972
|
+
rc = await f();
|
|
898
973
|
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
974
|
+
// Retry on failure with new pending retry operations.
|
|
975
|
+
} while (rc && Module.retryOps.length);
|
|
976
|
+
return rc;
|
|
977
|
+
}
|
|
903
978
|
|
|
904
979
|
return sqlite3;
|
|
905
980
|
}
|
package/src/types/index.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ type SQLiteCompatibleType = number|string|Uint8Array|Array<number>|bigint|null;
|
|
|
33
33
|
* @see https://sqlite.org/vfs.html
|
|
34
34
|
* @see https://sqlite.org/c3ref/io_methods.html
|
|
35
35
|
*/
|
|
36
|
-
|
|
36
|
+
interface SQLiteVFS {
|
|
37
37
|
/** Maximum length of a file path in UTF-8 bytes (default 64) */
|
|
38
38
|
mxPathName?: number;
|
|
39
39
|
|
|
@@ -117,11 +117,11 @@ declare interface SQLiteVFS {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
|
-
* Options object argument for {@link SQLiteAPI
|
|
120
|
+
* Options object argument for {@link SQLiteAPI['statements']}
|
|
121
121
|
*/
|
|
122
122
|
declare interface SQLitePrepareOptions {
|
|
123
123
|
/**
|
|
124
|
-
* Statement handles prepared and yielded by {@link SQLiteAPI
|
|
124
|
+
* Statement handles prepared and yielded by {@link SQLiteAPI['statements']}
|
|
125
125
|
* are normally valid only within the scope of an iteration.
|
|
126
126
|
* Set `unscoped` to `true` to give iterated statements an arbitrary
|
|
127
127
|
* lifetime.
|
|
@@ -178,7 +178,7 @@ declare interface SQLitePrepareOptions {
|
|
|
178
178
|
*
|
|
179
179
|
* @see https://sqlite.org/c3ref/funclist.html
|
|
180
180
|
*/
|
|
181
|
-
|
|
181
|
+
interface SQLiteAPI {
|
|
182
182
|
/**
|
|
183
183
|
* Bind a collection of values to a statement
|
|
184
184
|
*
|
|
@@ -341,7 +341,6 @@ declare interface SQLiteAPI {
|
|
|
341
341
|
* unsigned mFlags Zero or more SQLITE_DESERIALIZE_* flags
|
|
342
342
|
* );
|
|
343
343
|
*/
|
|
344
|
-
|
|
345
344
|
deserialize(
|
|
346
345
|
db: number,
|
|
347
346
|
zSchema: string,
|
|
@@ -351,6 +350,51 @@ declare interface SQLiteAPI {
|
|
|
351
350
|
mFlags: number
|
|
352
351
|
): number;
|
|
353
352
|
|
|
353
|
+
/**
|
|
354
|
+
* unsigned char *sqlite3_serialize(
|
|
355
|
+
* sqlite3 *db, The database connection
|
|
356
|
+
* const char *zSchema, Which DB to serialize. ex: "main", "temp", ...
|
|
357
|
+
* sqlite3_int64 *piSize, Write size of the DB here, if not NULL
|
|
358
|
+
* unsigned int mFlags Zero or more SQLITE_SERIALIZE_* flags
|
|
359
|
+
* );
|
|
360
|
+
*/
|
|
361
|
+
serialize(
|
|
362
|
+
db: number,
|
|
363
|
+
zSchema: string,
|
|
364
|
+
// mFlags: number
|
|
365
|
+
): Uint8Array;
|
|
366
|
+
|
|
367
|
+
// sqlite3_backup *sqlite3_backup_init(
|
|
368
|
+
// sqlite3 *pDest, /* Destination database handle */
|
|
369
|
+
// const char *zDestName, /* Destination database name */
|
|
370
|
+
// sqlite3 *pSource, /* Source database handle */
|
|
371
|
+
// const char *zSourceName /* Source database name */
|
|
372
|
+
// );
|
|
373
|
+
// int sqlite3_backup_step(sqlite3_backup *p, int nPage);
|
|
374
|
+
// int sqlite3_backup_finish(sqlite3_backup *p);
|
|
375
|
+
// int sqlite3_backup_remaining(sqlite3_backup *p);
|
|
376
|
+
// int sqlite3_backup_pagecount(sqlite3_backup *p);
|
|
377
|
+
|
|
378
|
+
backup_init(
|
|
379
|
+
pDest: number,
|
|
380
|
+
zDestName: string,
|
|
381
|
+
pSource: number,
|
|
382
|
+
zSourceName: string
|
|
383
|
+
): number;
|
|
384
|
+
|
|
385
|
+
backup_step(p: number, nPage: number): number;
|
|
386
|
+
backup_finish(p: number): number;
|
|
387
|
+
backup_remaining(p: number): number;
|
|
388
|
+
backup_pagecount(p: number): number;
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Combines the `backup_init`, `backup_step`, and `backup_finish` functions.
|
|
392
|
+
* destName/sourceName is usually "main" or "temp".
|
|
393
|
+
*
|
|
394
|
+
* @returns `SQLITE_OK` (throws exception on error)
|
|
395
|
+
*/
|
|
396
|
+
backup(pDest: number, zDestName: string, pSource: number, zSourceName: string): number;
|
|
397
|
+
|
|
354
398
|
/**
|
|
355
399
|
* Reset all bindings on a prepared statement.
|
|
356
400
|
* @see https://www.sqlite.org/c3ref/clear_bindings.html
|
|
@@ -599,6 +643,12 @@ declare interface SQLiteAPI {
|
|
|
599
643
|
zVfs?: string
|
|
600
644
|
): Promise<number>;
|
|
601
645
|
|
|
646
|
+
open_v2Sync(
|
|
647
|
+
zFilename: string,
|
|
648
|
+
iFlags?: number,
|
|
649
|
+
zVfs?: string
|
|
650
|
+
): number;
|
|
651
|
+
|
|
602
652
|
/**
|
|
603
653
|
* Specify callback to be invoked between long-running queries
|
|
604
654
|
*
|
|
@@ -617,9 +667,9 @@ declare interface SQLiteAPI {
|
|
|
617
667
|
* Reset a prepared statement object
|
|
618
668
|
* @see https://www.sqlite.org/c3ref/reset.html
|
|
619
669
|
* @param stmt prepared statement pointer
|
|
620
|
-
* @returns
|
|
670
|
+
* @returns `SQLITE_OK` (rejects on error)
|
|
621
671
|
*/
|
|
622
|
-
reset(stmt: number):
|
|
672
|
+
reset(stmt: number): number;
|
|
623
673
|
|
|
624
674
|
/**
|
|
625
675
|
* Convenience function to call `result_*` based of the type of `value`
|
|
@@ -746,7 +796,7 @@ declare interface SQLiteAPI {
|
|
|
746
796
|
* @param options
|
|
747
797
|
*/
|
|
748
798
|
// statements(db: number, sql: string, options?: SQLitePrepareOptions): AsyncIterable<number>;
|
|
749
|
-
statements(db: number, sql: string, options?: SQLitePrepareOptions):
|
|
799
|
+
statements(db: number, sql: string, options?: SQLitePrepareOptions): ReadonlyArray<number>;
|
|
750
800
|
|
|
751
801
|
/**
|
|
752
802
|
* Evaluate an SQL statement
|
|
@@ -843,6 +893,11 @@ declare interface SQLiteAPI {
|
|
|
843
893
|
vfs_register(vfs: SQLiteVFS, makeDefault?: boolean): number;
|
|
844
894
|
}
|
|
845
895
|
|
|
896
|
+
declare module '@livestore/wa-sqlite' {
|
|
897
|
+
export type SQLiteAPI_ = SQLiteAPI;
|
|
898
|
+
export type SQLiteVFS_ = SQLiteVFS;
|
|
899
|
+
}
|
|
900
|
+
|
|
846
901
|
/** @ignore */
|
|
847
902
|
declare module '@livestore/wa-sqlite/src/sqlite-constants.js' {
|
|
848
903
|
export const SQLITE_OK: 0;
|
|
@@ -1115,6 +1170,7 @@ declare module '@livestore/wa-sqlite/src/VFS.js' {
|
|
|
1115
1170
|
export * from '@livestore/wa-sqlite/src/sqlite-constants.js';
|
|
1116
1171
|
|
|
1117
1172
|
export class Base {
|
|
1173
|
+
name: string;
|
|
1118
1174
|
mxPathName: number;
|
|
1119
1175
|
/**
|
|
1120
1176
|
* @param {number} fileId
|
|
@@ -1226,6 +1282,28 @@ declare module '@livestore/wa-sqlite/src/VFS.js' {
|
|
|
1226
1282
|
}
|
|
1227
1283
|
}
|
|
1228
1284
|
|
|
1285
|
+
/** @ignore */
|
|
1286
|
+
declare module '@livestore/wa-sqlite/src/FacadeVFS.js' {
|
|
1287
|
+
export type FacadeVFS = any
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
declare module '@livestore/wa-sqlite/src/examples/AccessHandlePoolVFS.js' {
|
|
1292
|
+
import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
|
|
1293
|
+
export class AccessHandlePoolVFS extends VFS.Base {
|
|
1294
|
+
constructor(name: string, module: any)
|
|
1295
|
+
static create(name: string, module: any): Promise<AccessHandlePoolVFS>;
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
declare module '@livestore/wa-sqlite/src/examples/OPFSCoopSyncVFS.js' {
|
|
1300
|
+
import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
|
|
1301
|
+
export class OPFSCoopSyncVFS extends VFS.Base {
|
|
1302
|
+
constructor(name: string, module: any)
|
|
1303
|
+
static create(name: string, module: any): Promise<OPFSCoopSyncVFS>;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1229
1307
|
/** @ignore */
|
|
1230
1308
|
declare module '@livestore/wa-sqlite/src/examples/IndexedDbVFS.js' {
|
|
1231
1309
|
import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
|