fix exec to run over multiple statements in the string

This commit is contained in:
Nikita Sivukhin
2025-09-25 12:03:52 +04:00
parent ddfa77997d
commit a938bdcf09
9 changed files with 169 additions and 57 deletions

View File

@@ -11,6 +11,14 @@ test('in-memory db', () => {
expect(rows).toEqual([{ x: 1 }, { x: 3 }]);
})
test('exec multiple statements', async () => {
const db = new Database(":memory:");
db.exec("CREATE TABLE t(x); INSERT INTO t VALUES (1); INSERT INTO t VALUES (2)");
const stmt = db.prepare("SELECT * FROM t");
const rows = stmt.all();
expect(rows).toEqual([{ x: 1 }, { x: 2 }]);
})
test('readonly-db', () => {
const path = `test-${(Math.random() * 10000) | 0}.db`;
try {

View File

@@ -1,5 +1,10 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare class BatchExecutor {
stepSync(): number
reset(): void
}
/** A database connection. */
export declare class Database {
/**
@@ -39,6 +44,7 @@ export declare class Database {
* A `Statement` instance.
*/
prepare(sql: string): Statement
executor(sql: string): BatchExecutor
/**
* Returns the rowid of the last row inserted.
*

View File

@@ -508,6 +508,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}
const { Database, Statement } = nativeBinding
const { BatchExecutor, Database, Statement } = nativeBinding
export { BatchExecutor }
export { Database }
export { Statement }

View File

@@ -31,6 +31,14 @@ test('in-memory-db-async', async () => {
expect(rows).toEqual([{ x: 1 }, { x: 3 }]);
})
test('exec multiple statements', async () => {
const db = await connect(":memory:");
await db.exec("CREATE TABLE t(x); INSERT INTO t VALUES (1); INSERT INTO t VALUES (2)");
const stmt = db.prepare("SELECT * FROM t");
const rows = await stmt.all();
expect(rows).toEqual([{ x: 1 }, { x: 2 }]);
})
test('readonly-db', async () => {
const path = `test-${(Math.random() * 10000) | 0}.db`;
try {