Skip to content

Commit 55432ec

Browse files
committed
chore: fix sqlite_step function
1 parent 9e6002f commit 55432ec

File tree

9 files changed

+187
-194
lines changed

9 files changed

+187
-194
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ serde = { version = "1.0.216", features = ["derive"] }
1414
serde_json = "1.0.134"
1515
tokio = { version = "1.42.0", features = ["rt-multi-thread"] }
1616
reqwest = { version = "0.12.9", features = ["json", "blocking", "gzip"] }
17+
num_cpus = "1.17.0"

pubspec.lock

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@ packages:
55
dependency: transitive
66
description:
77
name: _fe_analyzer_shared
8-
sha256: "16e298750b6d0af7ce8a3ba7c18c69c3785d11b15ec83f6dcd0ad2a0009b3cab"
8+
sha256: da0d9209ca76bde579f2da330aeb9df62b6319c834fa7baae052021b0462401f
99
url: "https://pub.dev"
1010
source: hosted
11-
version: "76.0.0"
12-
_macros:
13-
dependency: transitive
14-
description: dart
15-
source: sdk
16-
version: "0.3.3"
11+
version: "85.0.0"
1712
analyzer:
1813
dependency: transitive
1914
description:
2015
name: analyzer
21-
sha256: "1f14db053a8c23e260789e9b0980fa27f2680dd640932cae5e1137cce0e46e1e"
16+
sha256: "974859dc0ff5f37bc4313244b3218c791810d03ab3470a579580279ba971a48d"
2217
url: "https://pub.dev"
2318
source: hosted
24-
version: "6.11.0"
19+
version: "7.7.1"
2520
args:
2621
dependency: transitive
2722
description:
@@ -86,14 +81,6 @@ packages:
8681
url: "https://pub.dev"
8782
source: hosted
8883
version: "3.0.6"
89-
dotenv:
90-
dependency: "direct main"
91-
description:
92-
name: dotenv
93-
sha256: "379e64b6fc82d3df29461d349a1796ecd2c436c480d4653f3af6872eccbc90e1"
94-
url: "https://pub.dev"
95-
source: hosted
96-
version: "4.2.0"
9784
ffi:
9885
dependency: transitive
9986
description:
@@ -174,14 +161,6 @@ packages:
174161
url: "https://pub.dev"
175162
source: hosted
176163
version: "1.3.0"
177-
macros:
178-
dependency: transitive
179-
description:
180-
name: macros
181-
sha256: "1d9e801cd66f7ea3663c45fc708450db1fa57f988142c64289142c9b7ee80656"
182-
url: "https://pub.dev"
183-
source: hosted
184-
version: "0.1.3-main.0"
185164
matcher:
186165
dependency: transitive
187166
description:
@@ -314,18 +293,18 @@ packages:
314293
dependency: transitive
315294
description:
316295
name: sqlite3
317-
sha256: dd806fff004a0aeb01e208b858dbc649bc72104670d425a81a6dd17698535f6e
296+
sha256: f393d92c71bdcc118d6203d07c991b9be0f84b1a6f89dd4f7eed348131329924
318297
url: "https://pub.dev"
319298
source: hosted
320-
version: "2.8.0"
299+
version: "2.9.0"
321300
sqlite3_web:
322301
dependency: transitive
323302
description:
324303
name: sqlite3_web
325-
sha256: "967e076442f7e1233bd7241ca61f3efe4c7fc168dac0f38411bdb3bdf471eb3c"
304+
sha256: "0f6ebcb4992d1892ac5c8b5ecd22a458ab9c5eb6428b11ae5ecb5d63545844da"
326305
url: "https://pub.dev"
327306
source: hosted
328-
version: "0.3.1"
307+
version: "0.3.2"
329308
sqlite_async:
330309
dependency: "direct main"
331310
description:
@@ -370,26 +349,26 @@ packages:
370349
dependency: "direct dev"
371350
description:
372351
name: test
373-
sha256: "713a8789d62f3233c46b4a90b174737b2c04cb6ae4500f2aa8b1be8f03f5e67f"
352+
sha256: "75906bf273541b676716d1ca7627a17e4c4070a3a16272b7a3dc7da3b9f3f6b7"
374353
url: "https://pub.dev"
375354
source: hosted
376-
version: "1.25.8"
355+
version: "1.26.3"
377356
test_api:
378357
dependency: transitive
379358
description:
380359
name: test_api
381-
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
360+
sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55
382361
url: "https://pub.dev"
383362
source: hosted
384-
version: "0.7.3"
363+
version: "0.7.7"
385364
test_core:
386365
dependency: transitive
387366
description:
388367
name: test_core
389-
sha256: "12391302411737c176b0b5d6491f466b0dd56d4763e347b6714efbaa74d7953d"
368+
sha256: "0cc24b5ff94b38d2ae73e1eb43cc302b77964fbf67abad1e296025b78deb53d0"
390369
url: "https://pub.dev"
391370
source: hosted
392-
version: "0.6.5"
371+
version: "0.6.12"
393372
typed_data:
394373
dependency: transitive
395374
description:

pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ environment:
77
sdk: ^3.3.1
88

99
dependencies:
10-
dotenv: ^4.2.0
1110
path: ^1.9.1
1211
sqlite_async: ^0.11.8
1312

src/auth.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ impl DbAuthStrategy for GlobeStrategy {
2222
Box::pin(async move {
2323
let globe_auth_api = std::env::var("GLOBE_DS_API")?;
2424

25-
let request_body = serde_json::json!({ "db_name": db_name });
25+
let clean_db_name = db_name.split('.').next().unwrap_or(db_name);
26+
let request_body = serde_json::json!({ "db_name": clean_db_name });
2627

2728
let response = client
2829
.post(format!("{}/db/auth", globe_auth_api))

src/lib.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ use std::{
88
};
99

1010
use 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

1716
use 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]
293306
pub 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 {
595608
pub 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

Comments
 (0)