Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ pub unsafe extern "C" fn sqlite3_bind_text(
SQLITE_OK
}

#[no_mangle]
pub unsafe extern "C" fn sqlite3_bind_double(
stmt_ptr: *mut SQLite3PreparedStmt, // Pointer to the prepared statement
index: c_int, // Index of the parameter to bind
value: f64, // Double value to bind
_: Option<unsafe extern "C" fn(ptr: *mut c_void)>,
) -> i32 {
if stmt_ptr.is_null() {
return SQLITE_MISUSE;
}

let stmt = &mut *stmt_ptr;
if index <= 0 || index > stmt.param_count {
return SQLITE_RANGE;
}

stmt.params.insert(index, Value::Real(value));
SQLITE_OK
}

#[no_mangle]
pub unsafe extern "C" fn sqlite3_bind_int64(
stmt_ptr: *mut SQLite3PreparedStmt, // Pointer to the prepared statement
Expand Down Expand Up @@ -505,6 +525,34 @@ pub extern "C" fn sqlite3_column_text(
std::ptr::null() // Invalid column or no current row
}

#[no_mangle]
pub extern "C" fn sqlite3_column_double(stmt: *mut SQLite3PreparedStmt, col_index: i32) -> f64 {
if stmt.is_null() {
return 0.0;
}

let stmt = unsafe { &mut *stmt };
let result_rows = stmt.result_rows.lock().unwrap();
let current_row = stmt.current_row.lock().unwrap();

// Check if there's a current row
if let Some(row_index) = *current_row {
if let Some(value) = result_rows
.get(row_index)
.and_then(|row| row.get(col_index as usize))
{
// Match the value and extract it as f64
return match value {
Value::Real(f) => *f, // Return the float directly
Value::Integer(i) => *i as f64, // Cast integer to float
Value::Text(_) | Value::Null => 0.0, // Non-numeric or NULL
};
}
}

0.0
}

#[no_mangle]
pub extern "C" fn sqlite3_column_int64(stmt: *mut SQLite3PreparedStmt, col_index: i32) -> i64 {
if stmt.is_null() {
Expand Down Expand Up @@ -705,10 +753,12 @@ pub extern "C" fn sqlite3_create_function_v2(
_xFinal: Option<extern "C" fn(*mut c_void)>,
_xDestroy: Option<extern "C" fn(*mut c_void)>,
) -> c_int {
println!(
"Not Yet Supported: sqlite3_create_function_v2 : {:?}",
unsafe { CStr::from_ptr(_zFunctionName) }
);
if cfg!(debug_assertions) {
eprintln!(
"Not Yet Supported: sqlite3_create_function_v2 : {:?}",
unsafe { CStr::from_ptr(_zFunctionName) }
);
}
SQLITE_OK
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn convert_params_to_json(params: &HashMap<i32, Value>) -> Vec<serde_json::V

Value::Real(f) => serde_json::json!({
"type": "float",
"value": *f.to_string()
"value": f
}),
Value::Text(s) => serde_json::json!({
"type": "text",
Expand Down