Skip to content

Commit c4eadd1

Browse files
authored
Merge pull request #165 from tursodatabase/stmt-interrupt
Add statement interrupt API
2 parents b7a8189 + 714af5e commit c4eadd1

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
1212

1313
[dependencies]
1414
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
15-
libsql = { git = "https://github.com/tursodatabase/libsql/", rev = "016bea4f6235d39b5f1da5b3a0412af55c4dd0b2", features = ["encryption"] }
15+
libsql = { git = "https://github.com/tursodatabase/libsql/", rev = "9aa89fee3a096538336d30f3d9e9f2df8ba6677d", features = ["encryption"] }
1616
tracing = "0.1"
1717
once_cell = "1.18.0"
1818
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const {
3939
statementIsReader,
4040
statementGet,
4141
statementRun,
42+
statementInterrupt,
4243
statementRowsSync,
4344
statementColumns,
4445
statementSafeIntegers,
@@ -384,6 +385,13 @@ class Statement {
384385
}
385386
}
386387

388+
/**
389+
* Interrupts the statement.
390+
*/
391+
interrupt() {
392+
statementInterrupt.call(this.stmt);
393+
}
394+
387395
/**
388396
* Returns the columns in the result set returned by this prepared statement.
389397
*/

integration-tests/tests/async.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,21 @@ test.serial("Database.interrupt()", async (t) => {
328328
});
329329
});
330330

331+
332+
test.serial("Statement.interrupt()", async (t) => {
333+
const db = t.context.db;
334+
const stmt = await db.prepare("WITH RECURSIVE infinite_loop(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM infinite_loop) SELECT * FROM infinite_loop;");
335+
const fut = stmt.all();
336+
stmt.interrupt();
337+
await t.throwsAsync(async () => {
338+
await fut;
339+
}, {
340+
instanceOf: t.context.errorType,
341+
message: 'interrupted',
342+
code: 'SQLITE_INTERRUPT'
343+
});
344+
});
345+
331346
test.serial("Concurrent writes over same connection", async (t) => {
332347
const db = t.context.db;
333348
await db.exec(`

promise.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const {
5252
statementIsReader,
5353
statementGet,
5454
statementRun,
55+
statementInterrupt,
5556
statementRowsAsync,
5657
statementColumns,
5758
statementSafeIntegers,
@@ -386,6 +387,13 @@ class Statement {
386387
}
387388
}
388389

390+
/**
391+
* Interrupts the statement.
392+
*/
393+
interrupt() {
394+
statementInterrupt.call(this.stmt);
395+
}
396+
389397
/**
390398
* Returns the columns in the result set returned by this prepared statement.
391399
*/

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
5252
cx.export_function("statementRaw", Statement::js_raw)?;
5353
cx.export_function("statementIsReader", Statement::js_is_reader)?;
5454
cx.export_function("statementRun", Statement::js_run)?;
55+
cx.export_function("statementInterrupt", Statement::js_interrupt)?;
5556
cx.export_function("statementGet", Statement::js_get)?;
5657
cx.export_function("statementRowsSync", Statement::js_rows_sync)?;
5758
cx.export_function("statementRowsAsync", Statement::js_rows_async)?;

src/statement.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use neon::types::buffer::TypedArray;
22
use neon::types::JsPromise;
33
use neon::{prelude::*, types::JsBigInt};
4-
use tokio::time::Instant;
54
use std::cell::RefCell;
65
use std::sync::Arc;
76
use tokio::sync::Mutex;
7+
use tokio::time::Instant;
88

99
use crate::errors::throw_libsql_error;
1010
use crate::runtime;
@@ -119,6 +119,13 @@ impl Statement {
119119
Ok(info.upcast())
120120
}
121121

122+
pub fn js_interrupt(mut cx: FunctionContext) -> JsResult<JsNull> {
123+
let stmt: Handle<'_, JsBox<Statement>> = cx.this()?;
124+
let mut raw_stmt = stmt.stmt.blocking_lock();
125+
raw_stmt.interrupt();
126+
Ok(cx.null())
127+
}
128+
122129
pub fn js_get(mut cx: FunctionContext) -> JsResult<JsValue> {
123130
let stmt: Handle<'_, JsBox<Statement>> = cx.this()?;
124131
let params = cx.argument::<JsValue>(0)?;

0 commit comments

Comments
 (0)