Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1193,11 +1193,13 @@ TCP sockets and servers built on WASI sockets.

Requires the `sqlite` feature flag. Provides a synchronous SQLite database API with an embedded SQLite engine.

- `DatabaseSync` — `prepare`, `exec`, `close`, `open`, `isOpen`, `isTransaction`, `createSession`, `applyChangeset`, `enableLoadExtension`, `function`, `aggregate`, `backup`
- `StatementSync` — `run`, `get`, `all`, `iterate`, `columns`, `setReadBigInts`, `setAllowBareNamedParameters`, `sourceSQL`, `expandedSQL`
- `DatabaseSync` — `prepare`, `exec`, `close`, `open`, `isOpen`, `isTransaction`, `createSession`, `applyChangeset`, `enableLoadExtension`, `function`, `aggregate`, `backup`, `deserialize`
- `StatementSync` — `run`, `get`, `all`, `iterate`, `columns`, `close`, `Symbol.dispose`, `stat`, `resetStats`, `setReadBigInts`, `setAllowBareNamedParameters`, `sourceSQL`, `expandedSQL`
- `Session` — `changeset`, `patchset`, `close`
- Constants: `SQLITE_CHANGESET_OMIT`, `SQLITE_CHANGESET_REPLACE`, `SQLITE_CHANGESET_ABORT`

Statements retain their prepared SQLite handles until finalized by `close()`, disposal, garbage collection, database close, or `deserialize()`. Statistics read the actual SQLite statement counters. `deserialize()` finalizes existing statements before attempting replacement, including when that attempt fails. The separate `restoreDatabaseSync()` recovery extension preserves the connection, registered functions and idle statements; it rejects active execution or cursors before changing the database.

**Not supported:** `loadExtension()` (throws — native extensions cannot be loaded in WASM).

</details>
Expand Down
92 changes: 67 additions & 25 deletions crates/wasm-rquickjs/skeleton/src/builtin/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import {
open_database, close_database, exec_sql, is_open, is_autocommit,
stmt_run, stmt_get, stmt_all, stmt_expanded_sql, stmt_columns,
prepare_statement, stmt_run, stmt_get, stmt_all, stmt_expanded_sql, stmt_columns,
stmt_iterate_init, stmt_iterate_next, stmt_iterate_return,
register_function, register_aggregate,
create_session, session_changeset, session_patchset, session_close,
apply_changeset,
get_constants,
enable_defensive, location, set_authorizer, native_backup,
serialize_database, restore_database
serialize_database, restore_database, deserialize_database, check_database_idle
} from '__wasm_rquickjs_builtin/sqlite_native';

export const constants = get_constants();
const _connIdSymbol = Symbol('connId');
const typedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
const typedArrayTag = Function.prototype.call.bind(Object.getOwnPropertyDescriptor(typedArrayPrototype, Symbol.toStringTag).get);
const typedArrayByteLength = Function.prototype.call.bind(Object.getOwnPropertyDescriptor(typedArrayPrototype, 'byteLength').get);

function assertBoolean(value, name) {
if (typeof value !== 'boolean') {
Expand Down Expand Up @@ -144,6 +147,36 @@ class _DatabaseSyncImpl {
return new Statement(_stmtSecret, this.#connId, sql, stmtOptions);
}

deserialize(buffer, options) {
if (this.#connId === null) throwDbNotOpen('ERR_INVALID_STATE');
check_database_idle(this.#connId);
if (typedArrayTag(buffer) !== 'Uint8Array') {
throw Object.assign(new TypeError('The "buffer" argument must be a Uint8Array.'), { code: 'ERR_INVALID_ARG_TYPE' });
}
const length = typedArrayByteLength(buffer);
if (length === 0) {
throw Object.assign(new TypeError('The "buffer" argument must not be empty.'), { code: 'ERR_INVALID_ARG_VALUE' });
}
let dbName = 'main';
if (options !== undefined) {
if (options === null || (typeof options !== 'object' && typeof options !== 'function')) {
throw Object.assign(new TypeError('The "options" argument must be an object.'), { code: 'ERR_INVALID_ARG_TYPE' });
}
const value = options.dbName;
if (value !== undefined) {
if (typeof value !== 'string') {
throw Object.assign(new TypeError('The "options.dbName" argument must be a string.'), { code: 'ERR_INVALID_ARG_TYPE' });
}
dbName = value;
}
}
if (this.#connId === null) throwDbNotOpen('ERR_INVALID_STATE');
if (typedArrayByteLength(buffer) !== length) {
throw Object.assign(new Error('The "buffer" argument was resized while reading "options"'), { code: 'ERR_INVALID_STATE' });
}
deserialize_database(this.#connId, buffer, dbName);
}

function(name, optionsOrFn, maybeFn) {
if (this.#connId === null) throwDbNotOpen('ERR_INVALID_STATE');

Expand Down Expand Up @@ -395,19 +428,16 @@ export class Session {
const _stmtSecret = Symbol('stmtSecret');

export class Statement {
#connId;
#sql;
#handle;
#options;
#lastParams = null;

constructor(secret, connId, sql, options) {
if (secret !== _stmtSecret) {
const err = new TypeError('Illegal constructor');
err.code = 'ERR_ILLEGAL_CONSTRUCTOR';
throw err;
}
this.#connId = connId;
this.#sql = sql;
this.#handle = prepare_statement(connId, sql);
this.#options = options;
}

Expand All @@ -430,55 +460,54 @@ export class Statement {
}

run(...args) {
this.#handle.check();
const params = this.#processParams(args);
this.#lastParams = params;
return stmt_run(this.#connId, this.#sql, params,
return stmt_run(this.#handle, params,
this.#options.allowBareNamedParameters,
this.#options.allowUnknownNamedParameters,
this.#options.readBigInts);
}

get(...args) {
this.#handle.check();
const params = this.#processParams(args);
this.#lastParams = params;
return stmt_get(this.#connId, this.#sql, params,
return stmt_get(this.#handle, params,
this.#options.allowBareNamedParameters,
this.#options.allowUnknownNamedParameters,
this.#options.readBigInts,
this.#options.returnArrays);
}

all(...args) {
this.#handle.check();
const params = this.#processParams(args);
this.#lastParams = params;
return stmt_all(this.#connId, this.#sql, params,
return stmt_all(this.#handle, params,
this.#options.allowBareNamedParameters,
this.#options.allowUnknownNamedParameters,
this.#options.readBigInts,
this.#options.returnArrays);
}

columns() {
return stmt_columns(this.#connId, this.#sql);
return stmt_columns(this.#handle);
}

iterate(...args) {
this.#handle.check();
const params = this.#processParams(args);
this.#lastParams = params;
const iterId = stmt_iterate_init(this.#connId, this.#sql, params,
const iterId = stmt_iterate_init(this.#handle, params,
this.#options.allowBareNamedParameters,
this.#options.allowUnknownNamedParameters);
const readBigInts = this.#options.readBigInts;
const returnArrays = this.#options.returnArrays;
const options = this.#options;

return {
[Symbol.iterator]() { return this; },
next() {
return stmt_iterate_next(iterId, readBigInts, returnArrays);
return stmt_iterate_next(iterId, options.readBigInts, options.returnArrays);
},
return() {
stmt_iterate_return(iterId);
return { value: undefined, done: true };
return { __proto__: null, value: null, done: true };
},
toArray() {
const result = [];
Expand All @@ -492,30 +521,43 @@ export class Statement {
}

get sourceSQL() {
return this.#sql;
return this.#handle.source();
}

get expandedSQL() {
if (this.#lastParams !== null && this.#lastParams !== undefined) {
return stmt_expanded_sql(this.#connId, this.#sql, this.#lastParams,
this.#options.allowBareNamedParameters);
return stmt_expanded_sql(this.#handle);
}

close() { this.#handle.close(); }
[Symbol.dispose]() { this.#handle.dispose(); }

stat(counter) {
this.#handle.check();
if (typeof counter !== 'string') {
throw Object.assign(new TypeError('The "counter" argument must be a string.'), { code: 'ERR_INVALID_ARG_TYPE' });
}
return this.#sql;
return this.#handle.stat(counter);
}

resetStats() { this.#handle.resetStats(); }

setReadBigInts(enabled) {
this.#handle.check();
assertBoolean(enabled, 'readBigInts');
this.#options.readBigInts = enabled;
}
setReturnArrays(enabled) {
this.#handle.check();
assertBoolean(enabled, 'returnArrays');
this.#options.returnArrays = enabled;
}
setAllowBareNamedParameters(enabled) {
this.#handle.check();
assertBoolean(enabled, 'allowBareNamedParameters');
this.#options.allowBareNamedParameters = enabled;
}
setAllowUnknownNamedParameters(enabled) {
this.#handle.check();
assertBoolean(enabled, 'allowUnknownNamedParameters');
this.#options.allowUnknownNamedParameters = enabled;
}
Expand Down
Loading
Loading