Skip to content

Commit 5efbb52

Browse files
committed
better handling for when cannot open database
1 parent 0961076 commit 5efbb52

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

src/auth.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,18 @@ impl DbAuthStrategy for GlobeStrategy {
3131
.await
3232
.map_err(|_| "Failed to fetch auth credentials for database")?;
3333

34-
if !response.status().is_success() {
35-
return Err(format!("Failed to get Auth Token: {}", response.status()).into());
34+
let status_code = response.status();
35+
if !status_code.is_success() {
36+
let error_message = response.text().await?;
37+
if cfg!(debug_assertions) {
38+
eprintln!("Error: {}", error_message);
39+
}
40+
41+
return Err(format!(
42+
"Failed to authenticate database. Http Status Code: {}",
43+
status_code,
44+
)
45+
.into());
3646
}
3747

3848
let json = response.json().await?;

src/lib.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ use std::{
99

1010
use 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
};
1515
use utils::execute_async_task;
1616

1717
use 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]
9896
pub 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]
348346
pub 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]
363361
pub 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]
379377
pub 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]
664665
pub 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

src/sqlite.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub const SQLITE_ROW: c_int = 100;
2020
pub const SQLITE_DONE: c_int = 101;
2121
pub const SQLITE_RANGE: c_int = 25;
2222
pub const SQLITE_BUSY: c_int = 5;
23+
pub const SQLITE_CANTOPEN: c_int = 14;
2324

2425
pub const SQLITE_INTEGER: c_int = 1;
2526
pub const SQLITE_FLOAT: c_int = 2;

src/utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,8 @@ pub fn sql_is_rollback(sql: &String) -> bool {
9292
pub fn sql_is_commit(sql: &String) -> bool {
9393
sql.starts_with("COMMIT")
9494
}
95+
96+
#[inline]
97+
pub fn is_aligned<T>(ptr: *const T) -> bool {
98+
!ptr.is_null() && (ptr as usize) % std::mem::align_of::<T>() == 0
99+
}

0 commit comments

Comments
 (0)