Skip to content

Commit 7058bff

Browse files
authored
Merge pull request #1965 from tursodatabase/stmt-interrupt-rust
libsql: Add Statement::interrupt()
2 parents 867c3f1 + 2a8507d commit 7058bff

File tree

11 files changed

+55
-6
lines changed

11 files changed

+55
-6
lines changed

libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92309,10 +92309,13 @@ SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
9230992309
if( vdbeSafetyNotNull(v) ){
9231092310
return SQLITE_MISUSE_BKPT;
9231192311
}
92312+
db = v->db;
9231292313
if( v->isInterrupted ){
92313-
return SQLITE_INTERRUPT;
92314+
rc = SQLITE_INTERRUPT;
92315+
v->rc = rc;
92316+
db->errCode = rc;
92317+
return rc;
9231492318
}
92315-
db = v->db;
9231692319
sqlite3_mutex_enter(db->mutex);
9231792320
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
9231892321
&& cnt++ < SQLITE_MAX_SCHEMA_RETRY ){

libsql-ffi/bundled/bindings/session_bindgen.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,9 @@ extern "C" {
15551555
extern "C" {
15561556
pub fn sqlite3_reset(pStmt: *mut sqlite3_stmt) -> ::std::os::raw::c_int;
15571557
}
1558+
extern "C" {
1559+
pub fn libsql_stmt_interrupt(stmt: *mut sqlite3_stmt);
1560+
}
15581561
extern "C" {
15591562
pub fn sqlite3_create_function(
15601563
db: *mut sqlite3,

libsql-ffi/bundled/src/sqlite3.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92309,10 +92309,13 @@ SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
9230992309
if( vdbeSafetyNotNull(v) ){
9231092310
return SQLITE_MISUSE_BKPT;
9231192311
}
92312+
db = v->db;
9231292313
if( v->isInterrupted ){
92313-
return SQLITE_INTERRUPT;
92314+
rc = SQLITE_INTERRUPT;
92315+
v->rc = rc;
92316+
db->errCode = rc;
92317+
return rc;
9231492318
}
92315-
db = v->db;
9231692319
sqlite3_mutex_enter(db->mutex);
9231792320
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
9231892321
&& cnt++ < SQLITE_MAX_SCHEMA_RETRY ){

libsql-sqlite3/src/vdbeapi.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,10 +914,13 @@ int sqlite3_step(sqlite3_stmt *pStmt){
914914
if( vdbeSafetyNotNull(v) ){
915915
return SQLITE_MISUSE_BKPT;
916916
}
917+
db = v->db;
917918
if( v->isInterrupted ){
918-
return SQLITE_INTERRUPT;
919+
rc = SQLITE_INTERRUPT;
920+
v->rc = rc;
921+
db->errCode = rc;
922+
return rc;
919923
}
920-
db = v->db;
921924
sqlite3_mutex_enter(db->mutex);
922925
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
923926
&& cnt++ < SQLITE_MAX_SCHEMA_RETRY ){

libsql-sys/src/statement.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ impl Statement {
8282
unsafe { crate::ffi::sqlite3_step(self.raw_stmt) }
8383
}
8484

85+
pub fn interrupt(&self) {
86+
unsafe { crate::ffi::libsql_stmt_interrupt(self.raw_stmt) }
87+
}
88+
8589
pub fn reset(&self) -> std::ffi::c_int {
8690
unsafe { crate::ffi::sqlite3_reset(self.raw_stmt) }
8791
}

libsql/src/hrana/hyper.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ impl crate::statement::Stmt for crate::hrana::Statement<HttpSender> {
205205
self.run(params).await
206206
}
207207

208+
fn interrupt(&mut self) -> crate::Result<()> {
209+
Err(crate::Error::Misuse(
210+
"interrupt is not supported for remote connections".to_string(),
211+
))
212+
}
213+
208214
fn reset(&mut self) {}
209215

210216
fn parameter_count(&self) -> usize {

libsql/src/local/impls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ impl Stmt for LibsqlStmt {
120120
stmt.run(&params)
121121
}
122122

123+
fn interrupt(&mut self) -> Result<()> {
124+
self.0.interrupt()
125+
}
126+
123127
fn reset(&mut self) {
124128
self.0.reset();
125129
}

libsql/src/local/statement.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ impl Statement {
134134
}
135135
}
136136

137+
/// Interrupt the statement.
138+
pub fn interrupt(&self) -> Result<()> {
139+
self.inner.interrupt();
140+
Ok(())
141+
}
142+
137143
/// Reset the prepared statement to initial state for reuse.
138144
pub fn reset(&self) {
139145
self.inner.reset();

libsql/src/replication/connection.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,12 @@ impl Stmt for RemoteStatement {
732732
Ok(())
733733
}
734734

735+
fn interrupt(&mut self) -> Result<()> {
736+
Err(Error::Misuse(
737+
"interrupt is not supported for remote connections".to_string(),
738+
))
739+
}
740+
735741
fn reset(&mut self) {}
736742

737743
fn parameter_count(&self) -> usize {

libsql/src/statement.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub(crate) trait Stmt {
1414

1515
async fn run(&mut self, params: &Params) -> Result<()>;
1616

17+
fn interrupt(&mut self) -> Result<()>;
18+
1719
fn reset(&mut self);
1820

1921
fn parameter_count(&self) -> usize;
@@ -60,6 +62,11 @@ impl Statement {
6062
Ok(())
6163
}
6264

65+
/// Interrupt the statement.
66+
pub fn interrupt(&mut self) -> Result<()> {
67+
self.inner.interrupt()
68+
}
69+
6370
/// Execute a query that returns the first [`Row`].
6471
///
6572
/// # Errors

0 commit comments

Comments
 (0)