Skip to content

Commit 4a996c8

Browse files
committed
libsql: Add support for Connection::interrupt()
Let's expose SQLite capability of interrupting long-running queries.
1 parent 19e7800 commit 4a996c8

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

libsql/src/connection.rs

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

2222
async fn transaction(&self, tx_behavior: TransactionBehavior) -> Result<Transaction>;
2323

24+
fn interrupt(&self) -> Result<()>;
25+
2426
fn is_autocommit(&self) -> bool;
2527

2628
fn changes(&self) -> u64;
@@ -185,6 +187,11 @@ impl Connection {
185187
self.conn.transaction(tx_behavior).await
186188
}
187189

190+
/// Cancel ongoing operations and return at earliest opportunity.
191+
pub fn interrupt(&self) -> Result<()> {
192+
self.conn.interrupt()
193+
}
194+
188195
/// Check weather libsql is in `autocommit` or not.
189196
pub fn is_autocommit(&self) -> bool {
190197
self.conn.is_autocommit()

libsql/src/hrana/hyper.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ impl Conn for HttpConnection<HttpSender> {
163163
})
164164
}
165165

166+
fn interrupt(&self) -> crate::Result<()> {
167+
// Interrupt is a no-op for remote connections.
168+
Ok(())
169+
}
170+
166171
fn is_autocommit(&self) -> bool {
167172
self.is_autocommit()
168173
}
@@ -343,6 +348,11 @@ impl Conn for HranaStream<HttpSender> {
343348
todo!("sounds like nested transactions innit?")
344349
}
345350

351+
fn interrupt(&self) -> crate::Result<()> {
352+
// Interrupt is a no-op for remote connections.
353+
Ok(())
354+
}
355+
346356
fn is_autocommit(&self) -> bool {
347357
false // for streams this method is callable only when we're within explicit transaction
348358
}

libsql/src/local/connection.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ impl Connection {
355355
Transaction::begin(self.clone(), tx_behavior)
356356
}
357357

358+
pub fn interrupt(&self) -> Result<()> {
359+
unsafe { ffi::sqlite3_interrupt(self.raw) };
360+
Ok(())
361+
}
362+
358363
pub fn is_autocommit(&self) -> bool {
359364
unsafe { ffi::sqlite3_get_autocommit(self.raw) != 0 }
360365
}

libsql/src/local/impls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ impl Conn for LibsqlConnection {
5454
})
5555
}
5656

57+
fn interrupt(&self) -> Result<()> {
58+
self.conn.interrupt()
59+
}
60+
5761
fn is_autocommit(&self) -> bool {
5862
self.conn.is_autocommit()
5963
}

libsql/src/replication/connection.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ impl Conn for RemoteConnection {
503503
})
504504
}
505505

506+
fn interrupt(&self) -> Result<()> {
507+
// Interrupt is a no-op for remote connections.
508+
Ok(())
509+
}
510+
506511
fn is_autocommit(&self) -> bool {
507512
self.is_state_init()
508513
}

0 commit comments

Comments
 (0)