From 942438e926d13a64776c74dfbb9528680a06ab16 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 14 Nov 2024 18:53:35 +0100 Subject: [PATCH] add .execIterator for streaming rows --- .changeset/witty-impalas-dance.md | 5 +++++ src/sqlite-api.js | 14 ++++++++++++++ src/types/index.d.ts | 24 ++++++++++++++++-------- 3 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 .changeset/witty-impalas-dance.md diff --git a/.changeset/witty-impalas-dance.md b/.changeset/witty-impalas-dance.md new file mode 100644 index 0000000..9d6d314 --- /dev/null +++ b/.changeset/witty-impalas-dance.md @@ -0,0 +1,5 @@ +--- +"@effect/wa-sqlite": patch +--- + +add .execIterator for streaming rows diff --git a/src/sqlite-api.js b/src/sqlite-api.js index 627db98..315acb9 100644 --- a/src/sqlite-api.js +++ b/src/sqlite-api.js @@ -470,6 +470,20 @@ export function Factory(Module) { return rows } + // @ts-ignore + sqlite3.execIterator = function* (db, sql) { + for (const stmt of sqlite3.statements(db, sql)) { + let columns + while (sqlite3.step(stmt) === SQLite.SQLITE_ROW) { + columns = columns ?? sqlite3.column_names(stmt) + const row = sqlite3.row(stmt) + // @ts-ignore + row.columns = columns + yield row + } + } + } + sqlite3.finalize = (function () { const fname = "sqlite3_finalize" const f = Module.cwrap(fname, ...decl("n:n"), { async }) diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 3b8d8ee..3eb13a5 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -542,17 +542,25 @@ declare interface SQLiteAPI { * @see https://www.sqlite.org/c3ref/exec.html * @param db database pointer * @param zSQL queries - * @param callback called for each output row - * @returns Promise resolving to `SQLITE_OK` (rejects on error) + * @returns An array of rows, each containing an array of column values */ - exec( + exec(db: number, zSQL: string): Array> + + /** + * One-step query execution interface + * + * The implementation of this function uses {@link row}, which makes a + * copy of blobs and returns BigInt for integers outside the safe integer + * bounds for Number. + * @see https://www.sqlite.org/c3ref/exec.html + * @param db database pointer + * @param zSQL queries + * @returns An iterator of rows, each containing an array of column values + */ + execIterator( db: number, zSQL: string, - callback?: ( - row: Array, - columns: string[], - ) => void, - ): Array> + ): Iterable & { readonly columns: Array }> /** * Destroy a prepared statement object compiled by {@link statements}