diff --git a/.gitignore b/.gitignore index 84e7fb6..e644b42 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ build/ # C benchmark binary bench/bench deno.lock +bun.lockb diff --git a/bench/bench_bun.js b/bench/bench_bun.js index 0bcc583..96e1f16 100644 --- a/bench/bench_bun.js +++ b/bench/bench_bun.js @@ -27,5 +27,4 @@ function bench(query) { } const query = createQuery(sql); -console.log(query.get()); bench(() => query.get()); diff --git a/bench/bench_deno.js b/bench/bench_deno.js index 3d63825..506732d 100644 --- a/bench/bench_deno.js +++ b/bench/bench_deno.js @@ -22,5 +22,4 @@ function bench(query) { } const query = db.prepare(sql); -console.log(query.get()); bench(() => query.get()); diff --git a/bench/bench_deno_ffi.js b/bench/bench_deno_ffi.js index fccde30..a667109 100644 --- a/bench/bench_deno_ffi.js +++ b/bench/bench_deno_ffi.js @@ -23,14 +23,14 @@ unwrap( pHandle, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_PRIVATECACHE | SQLITE3_OPEN_CREATE | SQLITE3_OPEN_MEMORY, - 0, + null, ), ); -const db = pHandle[0] + 2 ** 32 * pHandle[1]; +const db = Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]); function exec(sql) { const _pErr = new Uint32Array(2); - unwrap(sqlite3_exec(db, toCString(sql), 0, 0, _pErr)); + unwrap(sqlite3_exec(db, toCString(sql), null, null, _pErr)); } exec("PRAGMA auto_vacuum = none"); @@ -60,10 +60,10 @@ function prepareStatement() { toCString(sql), sql.length, pHandle, - 0, + null, ), ); - return pHandle[0] + 2 ** 32 * pHandle[1]; + return Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]); } const prepared = prepareStatement(); diff --git a/src/statement.ts b/src/statement.ts index 82dd4f0..8d438e7 100644 --- a/src/statement.ts +++ b/src/statement.ts @@ -81,13 +81,7 @@ function getColumn(handle: Deno.PointerValue, i: number, int64: boolean): any { } case SQLITE_INTEGER: { - const v = sqlite3_column_int64(handle, i); - const numv = Number(v); - if (Number.isSafeInteger(numv)) { - return numv; - } else { - return v; - } + return sqlite3_column_int64(handle, i); } case SQLITE_FLOAT: { @@ -364,34 +358,46 @@ export class Statement { } #runNoArgs(): number { + const handle = this.#handle; this.#begin(); - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - const status = step(this.#handle); + let status; + if (this.callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } if (status !== SQLITE3_ROW && status !== SQLITE3_DONE) { unwrap(status, this.db.unsafeHandle); } - sqlite3_reset(this.#handle); + sqlite3_reset(handle); return sqlite3_changes(this.db.unsafeHandle); } #runWithArgs(...params: RestBindParameters): number { + const handle = this.#handle; this.#begin(); this.#bindAll(params); - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - const status = step(this.#handle); + let status; + if (this.callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } if (!this.#hasNoArgs && !this.#bound && params.length) { this.#bindRefs.clear(); } if (status !== SQLITE3_ROW && status !== SQLITE3_DONE) { unwrap(status, this.db.unsafeHandle); } - sqlite3_reset(this.#handle); + sqlite3_reset(handle); return sqlite3_changes(this.db.unsafeHandle); } #valuesNoArgs>(): T[] { + const handle = this.#handle; + const callback = this.callback; this.#begin(); - const columnCount = sqlite3_column_count(this.#handle); + const columnCount = sqlite3_column_count(handle); const result: T[] = []; const getRowArray = new Function( "getColumn", @@ -406,25 +412,35 @@ export class Statement { }; `, )(getColumn); - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - let status = step(this.#handle); + let status; + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } while (status === SQLITE3_ROW) { - result.push(getRowArray(this.#handle)); - status = step(this.#handle); + result.push(getRowArray(handle)); + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } } if (status !== SQLITE3_DONE) { unwrap(status, this.db.unsafeHandle); } - sqlite3_reset(this.#handle); + sqlite3_reset(handle); return result as T[]; } #valuesWithArgs>( ...params: RestBindParameters ): T[] { + const handle = this.#handle; + const callback = this.callback; this.#begin(); this.#bindAll(params); - const columnCount = sqlite3_column_count(this.#handle); + const columnCount = sqlite3_column_count(handle); const result: T[] = []; const getRowArray = new Function( "getColumn", @@ -439,11 +455,19 @@ export class Statement { }; `, )(getColumn); - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - let status = step(this.#handle); + let status; + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } while (status === SQLITE3_ROW) { - result.push(getRowArray(this.#handle)); - status = step(this.#handle); + result.push(getRowArray(handle)); + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } } if (!this.#hasNoArgs && !this.#bound && params.length) { this.#bindRefs.clear(); @@ -451,7 +475,7 @@ export class Statement { if (status !== SQLITE3_DONE) { unwrap(status, this.db.unsafeHandle); } - sqlite3_reset(this.#handle); + sqlite3_reset(handle); return result as T[]; } @@ -480,35 +504,54 @@ export class Statement { } #allNoArgs>(): T[] { + const handle = this.#handle; + const callback = this.callback; this.#begin(); const getRowObject = this.getRowObject(); - const result: T[] = []; - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - let status = step(this.#handle); + let status; + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } while (status === SQLITE3_ROW) { - result.push(getRowObject(this.#handle)); - status = step(this.#handle); + result.push(getRowObject(handle)); + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } } if (status !== SQLITE3_DONE) { unwrap(status, this.db.unsafeHandle); } - sqlite3_reset(this.#handle); + sqlite3_reset(handle); return result as T[]; } #allWithArgs>( ...params: RestBindParameters ): T[] { + const handle = this.#handle; + const callback = this.callback; this.#begin(); this.#bindAll(params); const getRowObject = this.getRowObject(); const result: T[] = []; - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - let status = step(this.#handle); + let status; + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } while (status === SQLITE3_ROW) { - result.push(getRowObject(this.#handle)); - status = step(this.#handle); + result.push(getRowObject(handle)); + if (callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } } if (!this.#hasNoArgs && !this.#bound && params.length) { this.#bindRefs.clear(); @@ -516,7 +559,7 @@ export class Statement { if (status !== SQLITE3_DONE) { unwrap(status, this.db.unsafeHandle); } - sqlite3_reset(this.#handle); + sqlite3_reset(handle); return result as T[]; } @@ -536,8 +579,12 @@ export class Statement { } } - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - const status = step(handle); + let status; + if (this.callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } if (!this.#hasNoArgs && !this.#bound && params.length) { this.#bindRefs.clear(); @@ -562,8 +609,12 @@ export class Statement { const cc = sqlite3_column_count(handle); const arr = new Array(cc); sqlite3_reset(handle); - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - const status = step(handle); + let status; + if (this.callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } if (status === SQLITE3_ROW) { for (let i = 0; i < cc; i++) { arr[i] = getColumn(handle, i, int64); @@ -615,8 +666,12 @@ export class Statement { } } - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - const status = step(handle); + let status; + if (this.callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } if (!this.#hasNoArgs && !this.#bound && params.length) { this.#bindRefs.clear(); @@ -638,17 +693,20 @@ export class Statement { #getNoArgs>(): T | undefined { const handle = this.#handle; const int64 = this.db.int64; - const columnNames = this.columnNames(); const row: Record = this.#rowObject; sqlite3_reset(handle); - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - const status = step(handle); + let status; + if (this.callback) { + status = sqlite3_step_cb(handle); + } else { + status = sqlite3_step(handle); + } if (status === SQLITE3_ROW) { for (let i = 0; i < columnNames?.length; i++) { row[columnNames[i]] = getColumn(handle, i, int64); } - sqlite3_reset(this.#handle); + sqlite3_reset(handle); return row as T; } else if (status === SQLITE3_DONE) { return; @@ -675,11 +733,19 @@ export class Statement { *[Symbol.iterator](): IterableIterator { this.#begin(); const getRowObject = this.getRowObject(); - const step = this.callback ? sqlite3_step_cb : sqlite3_step; - let status = step(this.#handle); + let status; + if (this.callback) { + status = sqlite3_step_cb(this.#handle); + } else { + status = sqlite3_step(this.#handle); + } while (status === SQLITE3_ROW) { yield getRowObject(this.#handle); - status = step(this.#handle); + if (this.callback) { + status = sqlite3_step_cb(this.#handle); + } else { + status = sqlite3_step(this.#handle); + } } if (status !== SQLITE3_DONE) { unwrap(status, this.db.unsafeHandle);