Skip to content

Commit e682819

Browse files
committed
more fixes
1 parent fdbdf11 commit e682819

File tree

3 files changed

+161
-125
lines changed

3 files changed

+161
-125
lines changed

src/lib.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ pub unsafe extern "C" fn sqlite3_bind_int64(
236236
SQLITE_OK
237237
}
238238

239+
#[no_mangle]
240+
pub extern "C" fn sqlite3_bind_null(stmt_ptr: *mut SQLite3PreparedStmt, index: c_int) -> c_int {
241+
if stmt_ptr.is_null() {
242+
return SQLITE_MISUSE;
243+
}
244+
245+
let stmt = unsafe { &mut *stmt_ptr };
246+
247+
if index <= 0 || index > stmt.param_count {
248+
return SQLITE_RANGE;
249+
}
250+
251+
stmt.params.insert(index, Value::Null);
252+
SQLITE_OK
253+
}
254+
239255
#[no_mangle]
240256
pub unsafe extern "C" fn sqlite3_step(stmt_ptr: *mut SQLite3PreparedStmt) -> c_int {
241257
if stmt_ptr.is_null() {
@@ -246,7 +262,7 @@ pub unsafe extern "C" fn sqlite3_step(stmt_ptr: *mut SQLite3PreparedStmt) -> c_i
246262

247263
let mut exec_state = match stmt.execution_state.lock() {
248264
Ok(guard) => guard,
249-
Err(_) => return SQLITE_ERROR, // Lock poisoned
265+
Err(_) => return SQLITE_ERROR,
250266
};
251267

252268
match *exec_state {
@@ -261,17 +277,15 @@ pub unsafe extern "C" fn sqlite3_step(stmt_ptr: *mut SQLite3PreparedStmt) -> c_i
261277
drop(exec_state);
262278

263279
let sql = stmt.sql.to_uppercase();
264-
if sql.starts_with("INSERT") {
265-
return execute_async_task(stmt.db, sqlite::handle_insert(stmt));
266-
} else if sql.starts_with("SELECT") {
280+
if sql.starts_with("SELECT") {
267281
return execute_async_task(stmt.db, sqlite::handle_select(stmt));
268282
} else if sql_is_begin_transaction(&sql) {
269283
return execute_async_task(stmt.db, sqlite::begin_tnx_on_db(stmt.db));
270284
} else if sql_is_commit(&sql) {
271285
return execute_async_task(stmt.db, sqlite::commit_tnx_on_db(stmt.db));
272286
}
273287

274-
SQLITE_ERROR
288+
execute_async_task(stmt.db, sqlite::execute_statement(stmt))
275289
}
276290

277291
#[no_mangle]
@@ -363,18 +377,18 @@ pub extern "C" fn sqlite3_extended_errcode(db: *mut SQLite3) -> c_int {
363377
#[no_mangle]
364378
pub extern "C" fn sqlite3_errmsg(db: *mut SQLite3) -> *const c_char {
365379
if db.is_null() {
366-
return std::ptr::null();
380+
return b"Invalid DB pointer\0".as_ptr() as *const c_char;
367381
}
368382

369383
let db = unsafe { &mut *db };
370384

371385
if let Some(error_entry) = sqlite::get_latest_error(db) {
372386
match CString::new(error_entry.0) {
373-
Ok(c_string) => c_string.into_raw(),
387+
Ok(c_string) => c_string.as_ptr(),
374388
Err(_) => std::ptr::null(),
375389
}
376390
} else {
377-
std::ptr::null()
391+
b"No error\0".as_ptr() as *const c_char
378392
}
379393
}
380394

@@ -594,14 +608,14 @@ pub unsafe extern "C" fn sqlite3_exec(
594608
if sql_is_pragma(&sql) {
595609
return SQLITE_OK;
596610
} else if sql_is_begin_transaction(&sql) {
597-
execute_async_task(db, sqlite::begin_tnx_on_db(db))
611+
return execute_async_task(db, sqlite::begin_tnx_on_db(db));
598612
} else if sql_is_rollback(&sql) {
599-
reset_txn_on_db(db)
613+
return reset_txn_on_db(db);
600614
} else if sql_is_commit(&sql) {
601-
execute_async_task(db, sqlite::commit_tnx_on_db(db))
602-
} else {
603-
execute_async_task(db, sqlite::handle_execute(db, &sql))
615+
return execute_async_task(db, sqlite::commit_tnx_on_db(db));
604616
}
617+
618+
execute_async_task(db, sqlite::handle_execute(db, &sql))
605619
}
606620

607621
#[no_mangle]

src/proxy.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ async fn send_sql_request(
111111
config: &TursoConfig,
112112
request: serde_json::Value,
113113
) -> Result<RemoteSqliteResponse, Box<dyn Error>> {
114+
if cfg!(debug_assertions) {
115+
println!("Sending SQL Request: {}", request);
116+
}
117+
114118
let response: serde_json::Value =
115119
send_remote_request(client, config, "v2/pipeline", request).await?;
120+
116121
let parsed: RemoteSqliteResponse = serde_json::from_value(response)?;
117122
Ok(parsed)
118123
}
@@ -132,14 +137,27 @@ async fn send_remote_request(
132137
.await?;
133138

134139
let status = response.status();
135-
let response_text: String = response.text().await?;
140+
let response_text = response.text().await?;
136141

137-
let parsed_response = serde_json::from_str::<serde_json::Value>(&response_text);
138-
if parsed_response.is_err() && !status.is_success() {
142+
if cfg!(debug_assertions) {
143+
println!("Received Response: {}\n", &response_text);
144+
}
145+
146+
if !status.is_success() {
147+
if let Ok(error_body) = serde_json::from_str::<serde_json::Value>(&response_text) {
148+
if let Some(error_message) = error_body.get("error").and_then(|e| e.as_str()) {
149+
return Err(error_message.into());
150+
}
151+
}
152+
return Err(format!("LibSqlite3_Turso Error: {}", response_text).into());
153+
}
154+
155+
let parsed_response = serde_json::from_str(&response_text);
156+
if parsed_response.is_err() {
139157
return Err(format!("Failed to parse response: {}", parsed_response.unwrap_err()).into());
140158
}
141159

142-
let parsed_response = parsed_response.unwrap();
160+
let parsed_response: serde_json::Value = parsed_response.unwrap();
143161
if let Some(results) = parsed_response.get("results").and_then(|r| r.as_array()) {
144162
for result in results {
145163
if let Some(error) = result
@@ -152,21 +170,6 @@ async fn send_remote_request(
152170
}
153171
}
154172

155-
// Check if the status code indicates success
156-
if !status.is_success() {
157-
if let Ok(error_body) = serde_json::from_str::<serde_json::Value>(&response_text) {
158-
if let Some(error_message) = error_body.get("error").and_then(|e| e.as_str()) {
159-
return Err(error_message.into());
160-
}
161-
}
162-
163-
return Err(format!("LibSqlite3_Turso Error: {}", response_text).into());
164-
}
165-
166-
if cfg!(debug_assertions) {
167-
println!("SQL Request: {}\nResponse: {}\n", request, parsed_response);
168-
}
169-
170173
Ok(parsed_response)
171174
}
172175

@@ -181,12 +184,12 @@ pub fn convert_params_to_json(params: &HashMap<i32, Value>) -> Vec<serde_json::V
181184
.map(|(_, value)| match value {
182185
Value::Integer(i) => serde_json::json!({
183186
"type": "integer",
184-
"value": *i
187+
"value": *i.to_string()
185188
}),
186189

187190
Value::Real(f) => serde_json::json!({
188191
"type": "float",
189-
"value": *f
192+
"value": *f.to_string()
190193
}),
191194
Value::Text(s) => serde_json::json!({
192195
"type": "text",

0 commit comments

Comments
 (0)