@livestore/wa-sqlite 1.0.1-dev.10 → 1.0.1-dev.12

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livestore/wa-sqlite",
3
- "version": "1.0.1-dev.10",
3
+ "version": "1.0.1-dev.12",
4
4
  "type": "module",
5
5
  "main": "src/sqlite-api.js",
6
6
  "types": "src/types/index.d.ts",
package/src/sqlite-api.js CHANGED
@@ -252,12 +252,12 @@ export function Factory(Module) {
252
252
  sqlite3.serialize = (function() {
253
253
  const fname = 'sqlite3_serialize';
254
254
  const f = Module.cwrap(fname, ...decl('nsnn:n'));
255
- return function(db, schema, piSize, flags) {
255
+ return function(db, schema, flags) {
256
256
  verifyDatabase(db);
257
- // TODO alloc + free memory for result?
257
+ const piSize = tmpPtr[0];
258
258
  const address = f(db, schema, piSize, flags);
259
- const result = Module.HEAPU8.subarray(address, address + piSize);
260
-
259
+ const size = Module.getValue(piSize, '*');
260
+ const result = Module.HEAPU8.subarray(address, address + size);
261
261
  return result;
262
262
  };
263
263
  })();
@@ -458,7 +458,8 @@ export function Factory(Module) {
458
458
  // return SQLite.SQLITE_OK;
459
459
  // };
460
460
  sqlite3.exec = function(db, sql, callback) {
461
- for (const stmt of sqlite3.statements(db, sql)) {
461
+ const { stmts, finalize } = sqlite3.statements(db, sql);
462
+ for (const stmt of stmts) {
462
463
  let columns;
463
464
  while (sqlite3.step(stmt) === SQLite.SQLITE_ROW) {
464
465
  if (callback) {
@@ -468,6 +469,7 @@ export function Factory(Module) {
468
469
  }
469
470
  }
470
471
  }
472
+ finalize();
471
473
  return SQLite.SQLITE_OK;
472
474
  };
473
475
 
@@ -702,10 +704,10 @@ export function Factory(Module) {
702
704
  // { async: true });
703
705
  { async: false });
704
706
 
705
- const results = [];
707
+ const stmts = [];
706
708
 
707
709
  const onFinally = [];
708
- try {
710
+ // try {
709
711
  // Encode SQL string to UTF-8.
710
712
  const utf8 = new TextEncoder().encode(sql);
711
713
 
@@ -767,16 +769,22 @@ export function Factory(Module) {
767
769
  if (stmt) {
768
770
  mapStmtToDB.set(stmt, db);
769
771
  // yield stmt;
770
- results.push(stmt);
772
+ stmts.push(stmt);
771
773
  }
772
774
  } while (stmt);
773
- } finally {
775
+ // } finally {
776
+ // while (onFinally.length) {
777
+ // onFinally.pop()();
778
+ // }
779
+ // }
780
+
781
+ const finalize = () => {
774
782
  while (onFinally.length) {
775
783
  onFinally.pop()();
776
784
  }
777
785
  }
778
786
 
779
- return results;
787
+ return { stmts, finalize };
780
788
  };
781
789
 
782
790
  sqlite3.step = (function() {
@@ -117,11 +117,11 @@ interface SQLiteVFS {
117
117
  }
118
118
 
119
119
  /**
120
- * Options object argument for {@link SQLiteAPI.statements}
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.statements}
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.
@@ -361,7 +361,6 @@ interface SQLiteAPI {
361
361
  serialize(
362
362
  db: number,
363
363
  zSchema: string,
364
- piSize: number,
365
364
  mFlags: number
366
365
  ): Uint8Array;
367
366
 
@@ -760,7 +759,7 @@ interface SQLiteAPI {
760
759
  * @param options
761
760
  */
762
761
  // statements(db: number, sql: string, options?: SQLitePrepareOptions): AsyncIterable<number>;
763
- statements(db: number, sql: string, options?: SQLitePrepareOptions): ReadonlyArray<number>;
762
+ statements(db: number, sql: string, options?: SQLitePrepareOptions): { stmts: ReadonlyArray<number>; finalize: () => void; };
764
763
 
765
764
  /**
766
765
  * Evaluate an SQL statement