@@ -9,16 +9,16 @@ use std::{
99
1010use sqlite:: {
1111 push_error, reset_txn_on_db, ExecutionState , SQLite3 , SQLite3PreparedStmt , Value , SQLITE_BUSY ,
12- SQLITE_DONE , SQLITE_ERROR , SQLITE_FLOAT , SQLITE_INTEGER , SQLITE_MISUSE , SQLITE_NULL , SQLITE_OK ,
13- SQLITE_RANGE , SQLITE_TEXT ,
12+ SQLITE_CANTOPEN , SQLITE_DONE , SQLITE_ERROR , SQLITE_FLOAT , SQLITE_INTEGER , SQLITE_MISUSE ,
13+ SQLITE_NULL , SQLITE_OK , SQLITE_RANGE , SQLITE_TEXT ,
1414} ;
1515use utils:: execute_async_task;
1616
1717use crate :: {
1818 auth:: { DbAuthStrategy , GlobeStrategy } ,
1919 utils:: {
20- count_parameters, extract_column_names, get_tokio, sql_is_begin_transaction , sql_is_commit ,
21- sql_is_pragma, sql_is_rollback,
20+ count_parameters, extract_column_names, get_tokio, is_aligned , sql_is_begin_transaction ,
21+ sql_is_commit , sql_is_pragma, sql_is_rollback,
2222 } ,
2323} ;
2424
@@ -62,8 +62,7 @@ pub unsafe extern "C" fn sqlite3_open_v2(
6262
6363 let filename = CStr :: from_ptr ( filename) . to_str ( ) . unwrap ( ) ;
6464 if filename. contains ( ":memory" ) {
65- eprintln ! ( "LibSqlite3_Turso Error: Memory store is not supported at runtime" ) ;
66- return SQLITE_MISUSE ;
65+ return SQLITE_CANTOPEN ;
6766 }
6867
6968 let reqwest_client = reqwest:: Client :: builder ( )
@@ -74,8 +73,7 @@ pub unsafe extern "C" fn sqlite3_open_v2(
7473 let auth_strategy = Box :: new ( GlobeStrategy ) ;
7574 let turso_config = get_tokio ( ) . block_on ( auth_strategy. resolve ( filename, & reqwest_client) ) ;
7675 if turso_config. is_err ( ) {
77- eprintln ! ( "LibSqlite3_Turso Error: {}" , turso_config. unwrap_err( ) ) ;
78- return SQLITE_ERROR ;
76+ return SQLITE_CANTOPEN ;
7977 }
8078
8179 let mock_db = Box :: into_raw ( Box :: new ( SQLite3 {
@@ -96,8 +94,8 @@ pub unsafe extern "C" fn sqlite3_open_v2(
9694
9795#[ no_mangle]
9896pub extern "C" fn sqlite3_extended_result_codes ( db : * mut SQLite3 , _onoff : i32 ) -> i32 {
99- if db . is_null ( ) {
100- return SQLITE_MISUSE ;
97+ if ! is_aligned ( db ) {
98+ return SQLITE_CANTOPEN ;
10199 }
102100
103101 SQLITE_OK
@@ -341,12 +339,12 @@ pub extern "C" fn sqlite3_reset(stmt: *mut SQLite3PreparedStmt) -> c_int {
341339
342340 stmt. column_names . clear ( ) ;
343341
344- SQLITE_OK // Indicate successful reset
342+ SQLITE_OK
345343}
346344
347345#[ no_mangle]
348346pub unsafe extern "C" fn sqlite3_close_v2 ( db : * mut SQLite3 ) -> c_int {
349- if db . is_null ( ) {
347+ if ! is_aligned ( db ) {
350348 return SQLITE_OK ;
351349 }
352350
@@ -361,8 +359,8 @@ pub unsafe extern "C" fn sqlite3_close_v2(db: *mut SQLite3) -> c_int {
361359
362360#[ no_mangle]
363361pub extern "C" fn sqlite3_extended_errcode ( db : * mut SQLite3 ) -> c_int {
364- if db . is_null ( ) {
365- return SQLITE_OK ;
362+ if ! is_aligned ( db ) {
363+ return SQLITE_CANTOPEN ;
366364 }
367365
368366 let db = unsafe { & mut * db } ;
@@ -377,8 +375,8 @@ pub extern "C" fn sqlite3_extended_errcode(db: *mut SQLite3) -> c_int {
377375
378376#[ no_mangle]
379377pub extern "C" fn sqlite3_errmsg ( db : * mut SQLite3 ) -> * const c_char {
380- if db . is_null ( ) {
381- return b"Invalid DB pointer\0 " . as_ptr ( ) as * const c_char ;
378+ if ! is_aligned ( db ) {
379+ return b"Invalid Database pointer\0 " . as_ptr ( ) as * const c_char ;
382380 }
383381
384382 let db = unsafe { & mut * db } ;
@@ -584,6 +582,7 @@ pub extern "C" fn sqlite3_errstr(errcode: c_int) -> *const c_char {
584582 SQLITE_MISUSE => "Library used incorrectly" ,
585583 SQLITE_RANGE => "2nd parameter to sqlite3_bind out of range" ,
586584 SQLITE_BUSY => "The database file is locked" ,
585+ SQLITE_CANTOPEN => "Either database does not exist or cannot be opened" ,
587586 _ => "Unknown error code" ,
588587 } ;
589588
@@ -598,8 +597,8 @@ pub unsafe extern "C" fn sqlite3_exec(
598597 _: * mut std:: ffi:: c_void ,
599598 _: * mut * mut i8 ,
600599) -> c_int {
601- if db . is_null ( ) || sql . is_null ( ) {
602- return SQLITE_MISUSE ;
600+ if ! is_aligned ( db ) {
601+ return SQLITE_CANTOPEN ;
603602 }
604603
605604 let db = & mut * db;
@@ -625,8 +624,8 @@ pub extern "C" fn sqlite3_update_hook(
625624 callback : Option < sqlite:: SqliteHook > ,
626625 user_data : * mut c_void ,
627626) -> c_int {
628- if db . is_null ( ) {
629- return SQLITE_MISUSE ;
627+ if ! is_aligned ( db ) {
628+ return SQLITE_CANTOPEN ;
630629 }
631630
632631 let db = unsafe { & mut * db } ;
@@ -640,8 +639,8 @@ pub extern "C" fn sqlite3_commit_hook(
640639 x_callback : Option < unsafe extern "C" fn ( * mut c_void ) -> c_int > , // int (*xCallback)(void*)
641640 p_arg : * mut c_void , // void *pArg
642641) -> c_int {
643- if db . is_null ( ) {
644- return SQLITE_MISUSE ;
642+ if ! is_aligned ( db ) {
643+ return SQLITE_CANTOPEN ;
645644 }
646645
647646 SQLITE_OK
@@ -652,17 +651,19 @@ pub extern "C" fn sqlite3_rollback_hook(
652651 db : * mut SQLite3 ,
653652 x_callback : Option < unsafe extern "C" fn ( * mut c_void ) -> c_int > , // int (*xCallback)(void*)
654653 p_arg : * mut c_void , // void *pArg
655- ) {
656- if db . is_null ( ) {
657- return ;
654+ ) -> c_int {
655+ if ! is_aligned ( db ) {
656+ return SQLITE_CANTOPEN ;
658657 }
659658
660659 let db = unsafe { & mut * db } ; // Safely dereference the raw pointer
660+
661+ SQLITE_OK
661662}
662663
663664#[ no_mangle]
664665pub extern "C" fn sqlite3_get_autocommit ( db : * mut SQLite3 ) -> c_int {
665- if db . is_null ( ) {
666+ if ! is_aligned ( db ) {
666667 return 1 ;
667668 }
668669
0 commit comments