Skip to content

Commit

Permalink
add .execIterator for streaming rows
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Nov 14, 2024
1 parent 5942291 commit 942438e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-impalas-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/wa-sqlite": patch
---

add .execIterator for streaming rows
14 changes: 14 additions & 0 deletions src/sqlite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
24 changes: 16 additions & 8 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<any>>

/**
* 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<SQLiteCompatibleType | null>,
columns: string[],
) => void,
): Array<Array<any>>
): Iterable<Array<any> & { readonly columns: Array<string> }>

/**
* Destroy a prepared statement object compiled by {@link statements}
Expand Down

0 comments on commit 942438e

Please sign in to comment.