Skip to content

Commit 0badcbc

Browse files
authored
feat: Add sqlite binding for double type (#4)
* expose bind_double & column_double * send float value directly without conversion to string
1 parent 8a391c3 commit 0badcbc

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/lib.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ pub unsafe extern "C" fn sqlite3_bind_text(
217217
SQLITE_OK
218218
}
219219

220+
#[no_mangle]
221+
pub unsafe extern "C" fn sqlite3_bind_double(
222+
stmt_ptr: *mut SQLite3PreparedStmt, // Pointer to the prepared statement
223+
index: c_int, // Index of the parameter to bind
224+
value: f64, // Double value to bind
225+
_: Option<unsafe extern "C" fn(ptr: *mut c_void)>,
226+
) -> i32 {
227+
if stmt_ptr.is_null() {
228+
return SQLITE_MISUSE;
229+
}
230+
231+
let stmt = &mut *stmt_ptr;
232+
if index <= 0 || index > stmt.param_count {
233+
return SQLITE_RANGE;
234+
}
235+
236+
stmt.params.insert(index, Value::Real(value));
237+
SQLITE_OK
238+
}
239+
220240
#[no_mangle]
221241
pub unsafe extern "C" fn sqlite3_bind_int64(
222242
stmt_ptr: *mut SQLite3PreparedStmt, // Pointer to the prepared statement
@@ -505,6 +525,34 @@ pub extern "C" fn sqlite3_column_text(
505525
std::ptr::null() // Invalid column or no current row
506526
}
507527

528+
#[no_mangle]
529+
pub extern "C" fn sqlite3_column_double(stmt: *mut SQLite3PreparedStmt, col_index: i32) -> f64 {
530+
if stmt.is_null() {
531+
return 0.0;
532+
}
533+
534+
let stmt = unsafe { &mut *stmt };
535+
let result_rows = stmt.result_rows.lock().unwrap();
536+
let current_row = stmt.current_row.lock().unwrap();
537+
538+
// Check if there's a current row
539+
if let Some(row_index) = *current_row {
540+
if let Some(value) = result_rows
541+
.get(row_index)
542+
.and_then(|row| row.get(col_index as usize))
543+
{
544+
// Match the value and extract it as f64
545+
return match value {
546+
Value::Real(f) => *f, // Return the float directly
547+
Value::Integer(i) => *i as f64, // Cast integer to float
548+
Value::Text(_) | Value::Null => 0.0, // Non-numeric or NULL
549+
};
550+
}
551+
}
552+
553+
0.0
554+
}
555+
508556
#[no_mangle]
509557
pub extern "C" fn sqlite3_column_int64(stmt: *mut SQLite3PreparedStmt, col_index: i32) -> i64 {
510558
if stmt.is_null() {
@@ -705,10 +753,12 @@ pub extern "C" fn sqlite3_create_function_v2(
705753
_xFinal: Option<extern "C" fn(*mut c_void)>,
706754
_xDestroy: Option<extern "C" fn(*mut c_void)>,
707755
) -> c_int {
708-
println!(
709-
"Not Yet Supported: sqlite3_create_function_v2 : {:?}",
710-
unsafe { CStr::from_ptr(_zFunctionName) }
711-
);
756+
if cfg!(debug_assertions) {
757+
eprintln!(
758+
"Not Yet Supported: sqlite3_create_function_v2 : {:?}",
759+
unsafe { CStr::from_ptr(_zFunctionName) }
760+
);
761+
}
712762
SQLITE_OK
713763
}
714764

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn convert_params_to_json(params: &HashMap<i32, Value>) -> Vec<serde_json::V
9595

9696
Value::Real(f) => serde_json::json!({
9797
"type": "float",
98-
"value": *f.to_string()
98+
"value": f
9999
}),
100100
Value::Text(s) => serde_json::json!({
101101
"type": "text",

0 commit comments

Comments
 (0)