Skip to content

Commit 4dafcbd

Browse files
authored
Merge pull request #1888 from tursodatabase/stmt-interrupt
libsql-sqlite: Add libsql_stmt_interrupt() API
2 parents 40c272d + 4ed932d commit 4dafcbd

File tree

8 files changed

+71
-15
lines changed

8 files changed

+71
-15
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5779,6 +5779,8 @@ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
57795779
SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
57805780

57815781

5782+
SQLITE_API void libsql_stmt_interrupt(sqlite3_stmt *stmt);
5783+
57825784
/*
57835785
** CAPI3REF: Create Or Redefine SQL Functions
57845786
** KEYWORDS: {function creation routines}
@@ -24195,6 +24197,7 @@ struct Vdbe {
2419524197
int nScan; /* Entries in aScan[] */
2419624198
ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
2419724199
#endif
24200+
u8 isInterrupted; /* True if the statement has been interrupted */
2419824201
};
2419924202

2420024203
void libsql_inc_row_read(Vdbe *p, int count);
@@ -67644,9 +67647,11 @@ static int walCheckpoint(
6764467647
if (xCb) {
6764567648
rc = (xCb)(pCbData, mxSafeFrame, NULL, 0, 0, 0);
6764667649
}
67647-
i64 szDb = pWal->hdr.nPage*(i64)szPage;
67648-
testcase( IS_BIG_INT(szDb) );
67649-
rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
67650+
if( rc==SQLITE_OK ){
67651+
i64 szDb = pWal->hdr.nPage*(i64)szPage;
67652+
testcase( IS_BIG_INT(szDb) );
67653+
rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
67654+
}
6765067655
if( rc==SQLITE_OK ){
6765167656
rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
6765267657
}
@@ -89473,6 +89478,7 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
8947389478
#ifdef SQLITE_DEBUG
8947489479
p->nWrite = 0;
8947589480
#endif
89481+
p->isInterrupted = 0;
8947689482

8947789483
/* Save profiling information from this VDBE run.
8947889484
*/
@@ -92277,6 +92283,18 @@ static int sqlite3Step(Vdbe *p){
9227792283
return (rc&db->errMask);
9227892284
}
9227992285

92286+
/*
92287+
** Interrupt the statement.
92288+
*/
92289+
void libsql_stmt_interrupt(sqlite3_stmt *pStmt){
92290+
Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
92291+
if( vdbeSafetyNotNull(v) ){
92292+
(void)SQLITE_MISUSE_BKPT;
92293+
return;
92294+
}
92295+
v->isInterrupted = 1;
92296+
}
92297+
9228092298
/*
9228192299
** This is the top-level implementation of sqlite3_step(). Call
9228292300
** sqlite3Step() to do most of the work. If a schema error occurs,
@@ -92291,6 +92309,9 @@ SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
9229192309
if( vdbeSafetyNotNull(v) ){
9229292310
return SQLITE_MISUSE_BKPT;
9229392311
}
92312+
if( v->isInterrupted ){
92313+
return SQLITE_INTERRUPT;
92314+
}
9229492315
db = v->db;
9229592316
sqlite3_mutex_enter(db->mutex);
9229692317
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA

libsql-ffi/bundled/bindings/bindgen.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ extern "C" {
940940
extern "C" {
941941
pub fn sqlite3_vmprintf(
942942
arg1: *const ::std::os::raw::c_char,
943-
arg2: *mut __va_list_tag,
943+
arg2: va_list,
944944
) -> *mut ::std::os::raw::c_char;
945945
}
946946
extern "C" {
@@ -956,7 +956,7 @@ extern "C" {
956956
arg1: ::std::os::raw::c_int,
957957
arg2: *mut ::std::os::raw::c_char,
958958
arg3: *const ::std::os::raw::c_char,
959-
arg4: *mut __va_list_tag,
959+
arg4: va_list,
960960
) -> *mut ::std::os::raw::c_char;
961961
}
962962
extern "C" {
@@ -1538,6 +1538,9 @@ extern "C" {
15381538
extern "C" {
15391539
pub fn sqlite3_reset(pStmt: *mut sqlite3_stmt) -> ::std::os::raw::c_int;
15401540
}
1541+
extern "C" {
1542+
pub fn libsql_stmt_interrupt(stmt: *mut sqlite3_stmt);
1543+
}
15411544
extern "C" {
15421545
pub fn sqlite3_create_function(
15431546
db: *mut sqlite3,
@@ -2503,7 +2506,7 @@ extern "C" {
25032506
pub fn sqlite3_str_vappendf(
25042507
arg1: *mut sqlite3_str,
25052508
zFormat: *const ::std::os::raw::c_char,
2506-
arg2: *mut __va_list_tag,
2509+
arg2: va_list,
25072510
);
25082511
}
25092512
extern "C" {
@@ -3570,12 +3573,4 @@ extern "C" {
35703573
extern "C" {
35713574
pub static sqlite3_wal_manager: libsql_wal_manager;
35723575
}
3573-
pub type __builtin_va_list = [__va_list_tag; 1usize];
3574-
#[repr(C)]
3575-
#[derive(Debug, Copy, Clone)]
3576-
pub struct __va_list_tag {
3577-
pub gp_offset: ::std::os::raw::c_uint,
3578-
pub fp_offset: ::std::os::raw::c_uint,
3579-
pub overflow_arg_area: *mut ::std::os::raw::c_void,
3580-
pub reg_save_area: *mut ::std::os::raw::c_void,
3581-
}
3576+
pub type __builtin_va_list = *mut ::std::os::raw::c_char;

libsql-ffi/bundled/src/sqlite3.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5779,6 +5779,8 @@ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
57795779
SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
57805780

57815781

5782+
SQLITE_API void libsql_stmt_interrupt(sqlite3_stmt *stmt);
5783+
57825784
/*
57835785
** CAPI3REF: Create Or Redefine SQL Functions
57845786
** KEYWORDS: {function creation routines}
@@ -24195,6 +24197,7 @@ struct Vdbe {
2419524197
int nScan; /* Entries in aScan[] */
2419624198
ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
2419724199
#endif
24200+
u8 isInterrupted; /* True if the statement has been interrupted */
2419824201
};
2419924202

2420024203
void libsql_inc_row_read(Vdbe *p, int count);
@@ -89475,6 +89478,7 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
8947589478
#ifdef SQLITE_DEBUG
8947689479
p->nWrite = 0;
8947789480
#endif
89481+
p->isInterrupted = 0;
8947889482

8947989483
/* Save profiling information from this VDBE run.
8948089484
*/
@@ -92279,6 +92283,18 @@ static int sqlite3Step(Vdbe *p){
9227992283
return (rc&db->errMask);
9228092284
}
9228192285

92286+
/*
92287+
** Interrupt the statement.
92288+
*/
92289+
void libsql_stmt_interrupt(sqlite3_stmt *pStmt){
92290+
Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
92291+
if( vdbeSafetyNotNull(v) ){
92292+
(void)SQLITE_MISUSE_BKPT;
92293+
return;
92294+
}
92295+
v->isInterrupted = 1;
92296+
}
92297+
9228292298
/*
9228392299
** This is the top-level implementation of sqlite3_step(). Call
9228492300
** sqlite3Step() to do most of the work. If a schema error occurs,
@@ -92293,6 +92309,9 @@ SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
9229392309
if( vdbeSafetyNotNull(v) ){
9229492310
return SQLITE_MISUSE_BKPT;
9229592311
}
92312+
if( v->isInterrupted ){
92313+
return SQLITE_INTERRUPT;
92314+
}
9229692315
db = v->db;
9229792316
sqlite3_mutex_enter(db->mutex);
9229892317
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA

libsql-ffi/bundled/src/sqlite3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5390,6 +5390,8 @@ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
53905390
SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
53915391

53925392

5393+
SQLITE_API void libsql_stmt_interrupt(sqlite3_stmt *stmt);
5394+
53935395
/*
53945396
** CAPI3REF: Create Or Redefine SQL Functions
53955397
** KEYWORDS: {function creation routines}

libsql-sqlite3/src/sqlite.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5390,6 +5390,8 @@ int sqlite3_finalize(sqlite3_stmt *pStmt);
53905390
int sqlite3_reset(sqlite3_stmt *pStmt);
53915391

53925392

5393+
void libsql_stmt_interrupt(sqlite3_stmt *stmt);
5394+
53935395
/*
53945396
** CAPI3REF: Create Or Redefine SQL Functions
53955397
** KEYWORDS: {function creation routines}

libsql-sqlite3/src/vdbeInt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ struct Vdbe {
528528
int nScan; /* Entries in aScan[] */
529529
ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
530530
#endif
531+
u8 isInterrupted; /* True if the statement has been interrupted */
531532
};
532533

533534
void libsql_inc_row_read(Vdbe *p, int count);

libsql-sqlite3/src/vdbeapi.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,18 @@ static int sqlite3Step(Vdbe *p){
888888
return (rc&db->errMask);
889889
}
890890

891+
/*
892+
** Interrupt the statement.
893+
*/
894+
void libsql_stmt_interrupt(sqlite3_stmt *pStmt){
895+
Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
896+
if( vdbeSafetyNotNull(v) ){
897+
(void)SQLITE_MISUSE_BKPT;
898+
return;
899+
}
900+
v->isInterrupted = 1;
901+
}
902+
891903
/*
892904
** This is the top-level implementation of sqlite3_step(). Call
893905
** sqlite3Step() to do most of the work. If a schema error occurs,
@@ -902,6 +914,9 @@ int sqlite3_step(sqlite3_stmt *pStmt){
902914
if( vdbeSafetyNotNull(v) ){
903915
return SQLITE_MISUSE_BKPT;
904916
}
917+
if( v->isInterrupted ){
918+
return SQLITE_INTERRUPT;
919+
}
905920
db = v->db;
906921
sqlite3_mutex_enter(db->mutex);
907922
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA

libsql-sqlite3/src/vdbeaux.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,7 @@ int sqlite3VdbeReset(Vdbe *p){
36123612
#ifdef SQLITE_DEBUG
36133613
p->nWrite = 0;
36143614
#endif
3615+
p->isInterrupted = 0;
36153616

36163617
/* Save profiling information from this VDBE run.
36173618
*/

0 commit comments

Comments
 (0)