Skip to content

Commit

Permalink
Merge pull request #1965 from tursodatabase/stmt-interrupt-rust
Browse files Browse the repository at this point in the history
libsql: Add Statement::interrupt()
  • Loading branch information
penberg authored Feb 24, 2025
2 parents 867c3f1 + 2a8507d commit 7058bff
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 6 deletions.
7 changes: 5 additions & 2 deletions libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -92309,10 +92309,13 @@ SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
if( vdbeSafetyNotNull(v) ){
return SQLITE_MISUSE_BKPT;
}
db = v->db;
if( v->isInterrupted ){
return SQLITE_INTERRUPT;
rc = SQLITE_INTERRUPT;
v->rc = rc;
db->errCode = rc;
return rc;
}
db = v->db;
sqlite3_mutex_enter(db->mutex);
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
&& cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
Expand Down
3 changes: 3 additions & 0 deletions libsql-ffi/bundled/bindings/session_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,9 @@ extern "C" {
extern "C" {
pub fn sqlite3_reset(pStmt: *mut sqlite3_stmt) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn libsql_stmt_interrupt(stmt: *mut sqlite3_stmt);
}
extern "C" {
pub fn sqlite3_create_function(
db: *mut sqlite3,
Expand Down
7 changes: 5 additions & 2 deletions libsql-ffi/bundled/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -92309,10 +92309,13 @@ SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
if( vdbeSafetyNotNull(v) ){
return SQLITE_MISUSE_BKPT;
}
db = v->db;
if( v->isInterrupted ){
return SQLITE_INTERRUPT;
rc = SQLITE_INTERRUPT;
v->rc = rc;
db->errCode = rc;
return rc;
}
db = v->db;
sqlite3_mutex_enter(db->mutex);
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
&& cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
Expand Down
7 changes: 5 additions & 2 deletions libsql-sqlite3/src/vdbeapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,13 @@ int sqlite3_step(sqlite3_stmt *pStmt){
if( vdbeSafetyNotNull(v) ){
return SQLITE_MISUSE_BKPT;
}
db = v->db;
if( v->isInterrupted ){
return SQLITE_INTERRUPT;
rc = SQLITE_INTERRUPT;
v->rc = rc;
db->errCode = rc;
return rc;
}
db = v->db;
sqlite3_mutex_enter(db->mutex);
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
&& cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
Expand Down
4 changes: 4 additions & 0 deletions libsql-sys/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl Statement {
unsafe { crate::ffi::sqlite3_step(self.raw_stmt) }
}

pub fn interrupt(&self) {
unsafe { crate::ffi::libsql_stmt_interrupt(self.raw_stmt) }
}

pub fn reset(&self) -> std::ffi::c_int {
unsafe { crate::ffi::sqlite3_reset(self.raw_stmt) }
}
Expand Down
6 changes: 6 additions & 0 deletions libsql/src/hrana/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ impl crate::statement::Stmt for crate::hrana::Statement<HttpSender> {
self.run(params).await
}

fn interrupt(&mut self) -> crate::Result<()> {
Err(crate::Error::Misuse(
"interrupt is not supported for remote connections".to_string(),
))
}

fn reset(&mut self) {}

fn parameter_count(&self) -> usize {
Expand Down
4 changes: 4 additions & 0 deletions libsql/src/local/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ impl Stmt for LibsqlStmt {
stmt.run(&params)
}

fn interrupt(&mut self) -> Result<()> {
self.0.interrupt()
}

fn reset(&mut self) {
self.0.reset();
}
Expand Down
6 changes: 6 additions & 0 deletions libsql/src/local/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ impl Statement {
}
}

/// Interrupt the statement.
pub fn interrupt(&self) -> Result<()> {
self.inner.interrupt();
Ok(())
}

/// Reset the prepared statement to initial state for reuse.
pub fn reset(&self) {
self.inner.reset();
Expand Down
6 changes: 6 additions & 0 deletions libsql/src/replication/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ impl Stmt for RemoteStatement {
Ok(())
}

fn interrupt(&mut self) -> Result<()> {
Err(Error::Misuse(
"interrupt is not supported for remote connections".to_string(),
))
}

fn reset(&mut self) {}

fn parameter_count(&self) -> usize {
Expand Down
7 changes: 7 additions & 0 deletions libsql/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub(crate) trait Stmt {

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

fn interrupt(&mut self) -> Result<()>;

fn reset(&mut self);

fn parameter_count(&self) -> usize;
Expand Down Expand Up @@ -60,6 +62,11 @@ impl Statement {
Ok(())
}

/// Interrupt the statement.
pub fn interrupt(&mut self) -> Result<()> {
self.inner.interrupt()
}

/// Execute a query that returns the first [`Row`].
///
/// # Errors
Expand Down
4 changes: 4 additions & 0 deletions libsql/src/sync/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl Stmt for SyncedStatement {
result
}

fn interrupt(&mut self) -> Result<()> {
self.inner.interrupt()
}

fn reset(&mut self) {
self.inner.reset()
}
Expand Down

0 comments on commit 7058bff

Please sign in to comment.