godot-sqlite-kysely
Version:
Kysely dialect for godot-sqlite. Adds SQLite support to Godot/GodotJS.
50 lines (49 loc) • 1.98 kB
JavaScript
import { GArray } from 'godot.lib.api';
import { getResultRows } from './utils';
export class GodotSQLiteKyselySyncConnection {
#sqlite;
#nestedTransactionIndex = 0;
constructor(sqlite) {
this.#sqlite = sqlite;
}
async executeQuery(compiledQuery) {
const sqlite = this.#sqlite;
if (!sqlite.query_with_bindings(compiledQuery.sql, GArray.create(compiledQuery.parameters))) {
throw new Error(sqlite.error_message);
}
return {
insertId: compiledQuery.query.kind === 'InsertQueryNode' ? BigInt(sqlite.last_insert_rowid) : undefined,
rows: getResultRows(sqlite),
};
}
streamQuery(_compiledQuery, _chunkSize) {
throw new Error('Streaming is not supported with SQLite3');
}
async close() {
this.#sqlite.close_db();
}
async beginTransaction() {
const savepointName = `sp${this.#nestedTransactionIndex++}`;
if (!this.#sqlite.query(`savepoint ${savepointName}`)) {
throw new Error(`beginTransaction (${savepointName}) failed: ${this.#sqlite.error_message}`);
}
}
async commitTransaction() {
if (this.#nestedTransactionIndex <= 0) {
throw new Error('No transactions in progress');
}
const savepointName = `sp${--this.#nestedTransactionIndex}`;
if (!this.#sqlite.query(`release savepoint ${savepointName}`)) {
throw new Error(`commitTransaction (${savepointName}) failed: ${this.#sqlite.error_message}`);
}
}
async rollbackTransaction() {
if (this.#nestedTransactionIndex <= 0) {
throw new Error('No transactions in progress');
}
const savepointName = `sp${--this.#nestedTransactionIndex}`;
if (!this.#sqlite.query(`rollback to savepoint ${savepointName}`)) {
throw new Error(`commitTransaction (${savepointName}) failed: ${this.#sqlite.error_message}`);
}
}
}