@@ -8,16 +8,15 @@ use std::{
88} ;
99
1010use sqlite:: {
11- push_error, reset_txn_on_db, ExecutionState , SQLite3 , SQLite3PreparedStmt , Value , SQLITE_BUSY ,
12- SQLITE_CANTOPEN , SQLITE_DONE , SQLITE_ERROR , SQLITE_FLOAT , SQLITE_INTEGER , SQLITE_MISUSE ,
13- SQLITE_NULL , SQLITE_OK , SQLITE_RANGE , SQLITE_TEXT ,
11+ push_error, reset_txn_on_db, ExecutionState , SQLite3 , SQLite3ExecCallback , SQLite3PreparedStmt ,
12+ Value , SQLITE_BUSY , SQLITE_CANTOPEN , SQLITE_DONE , SQLITE_ERROR , SQLITE_FLOAT , SQLITE_INTEGER ,
13+ SQLITE_MISUSE , SQLITE_NULL , SQLITE_OK , SQLITE_RANGE , SQLITE_TEXT ,
1414} ;
15- use utils:: execute_async_task;
1615
1716use crate :: {
1817 auth:: { DbAuthStrategy , GlobeStrategy } ,
1918 utils:: {
20- count_parameters, extract_column_names , get_tokio, is_aligned, sql_is_begin_transaction,
19+ count_parameters, execute_async_task , get_tokio, is_aligned, sql_is_begin_transaction,
2120 sql_is_commit, sql_is_pragma, sql_is_rollback,
2221 } ,
2322} ;
@@ -139,7 +138,6 @@ pub unsafe extern "C" fn sqlite3_prepare_v3(
139138 } ;
140139
141140 let param_count = count_parameters ( & sql) ;
142- let column_names = extract_column_names ( & sql) ;
143141
144142 // Mock unparsed portion of SQL
145143 if !pz_tail. is_null ( ) {
@@ -156,7 +154,7 @@ pub unsafe extern "C" fn sqlite3_prepare_v3(
156154 execution_state : Mutex :: new ( ExecutionState :: Prepared ) , // Start in the "Prepared" state
157155 result_rows : Mutex :: new ( vec ! [ ] ) , // Initialize an empty result set
158156 current_row : Mutex :: new ( None ) , // No current row initially
159- column_names,
157+ column_names : Vec :: new ( ) ,
160158 } ) ;
161159 * pp_stmt = Box :: into_raw ( stmt) ;
162160
@@ -277,21 +275,36 @@ pub unsafe extern "C" fn sqlite3_step(stmt_ptr: *mut SQLite3PreparedStmt) -> c_i
277275 }
278276 drop ( exec_state) ;
279277
280- let sql = stmt. sql . to_uppercase ( ) ;
281- if sql. starts_with ( "SELECT" ) {
282- return execute_async_task ( stmt. db , sqlite:: handle_select ( stmt) ) ;
283- } else if sql_is_begin_transaction ( & sql) {
284- return execute_async_task ( stmt. db , sqlite:: begin_tnx_on_db ( stmt. db ) ) ;
285- } else if sql_is_commit ( & sql) {
286- return execute_async_task ( stmt. db , sqlite:: commit_tnx_on_db ( stmt. db ) ) ;
278+ let needs_execution = stmt. result_rows . lock ( ) . unwrap ( ) . is_empty ( ) ;
279+ if needs_execution {
280+ let sql = stmt. sql . to_uppercase ( ) ;
281+ let sql_result_code = {
282+ if sql_is_begin_transaction ( & sql) {
283+ execute_async_task ( stmt. db , sqlite:: begin_tnx_on_db ( stmt. db , & sql) )
284+ } else if sql_is_commit ( & sql) {
285+ execute_async_task ( stmt. db , sqlite:: commit_tnx_on_db ( stmt. db , & sql) )
286+ } else {
287+ execute_async_task ( stmt. db , sqlite:: execute_stmt ( stmt) )
288+ }
289+ } ;
290+
291+ if sql_result_code != SQLITE_OK {
292+ return sql_result_code;
293+ }
287294 }
288295
289- execute_async_task ( stmt. db , sqlite:: execute_statement ( stmt) )
296+ let sql_result_code = sqlite:: iterate_rows ( stmt) ;
297+ if let Err ( error) = sql_result_code {
298+ push_error ( stmt. db , ( error. to_string ( ) , SQLITE_ERROR ) ) ;
299+ return SQLITE_ERROR ;
300+ }
301+
302+ sql_result_code. unwrap ( )
290303}
291304
292305#[ no_mangle]
293306pub extern "C" fn sqlite3_column_count ( stmt : * mut SQLite3PreparedStmt ) -> i32 {
294- if stmt . is_null ( ) {
307+ if ! is_aligned ( stmt ) {
295308 return 0 ;
296309 }
297310
@@ -595,9 +608,9 @@ pub extern "C" fn sqlite3_errstr(errcode: c_int) -> *const c_char {
595608pub unsafe extern "C" fn sqlite3_exec (
596609 db : * mut SQLite3 ,
597610 sql : * const c_char ,
598- _ : Option < extern "C" fn ( * mut std :: ffi :: c_void , i32 , * const * const i8 , * const * const i8 ) -> i32 > ,
599- _ : * mut std :: ffi :: c_void ,
600- _ : * mut * mut i8 ,
611+ callback : SQLite3ExecCallback , // Callback function
612+ arg : * mut c_void ,
613+ errmsg : * mut * mut c_char ,
601614) -> c_int {
602615 if !is_aligned ( db) {
603616 return SQLITE_CANTOPEN ;
@@ -610,11 +623,11 @@ pub unsafe extern "C" fn sqlite3_exec(
610623 if sql_is_pragma ( & sql) {
611624 return SQLITE_OK ;
612625 } else if sql_is_begin_transaction ( & sql) {
613- return execute_async_task ( db, sqlite:: begin_tnx_on_db ( db) ) ;
626+ return execute_async_task ( db, sqlite:: begin_tnx_on_db ( db, & sql ) ) ;
614627 } else if sql_is_rollback ( & sql) {
615628 return reset_txn_on_db ( db) ;
616629 } else if sql_is_commit ( & sql) {
617- return execute_async_task ( db, sqlite:: commit_tnx_on_db ( db) ) ;
630+ return execute_async_task ( db, sqlite:: commit_tnx_on_db ( db, & sql ) ) ;
618631 }
619632
620633 execute_async_task ( db, sqlite:: handle_execute ( db, & sql) )
0 commit comments