From f1c2277543bbd3d47cfc2db35b4a445618439e29 Mon Sep 17 00:00:00 2001 From: danawan Date: Wed, 20 Aug 2025 09:34:30 +0700 Subject: [PATCH] change column type using value type --- sqlite3/src/lib.rs | 22 ++++++++++------------ sqlite3/tests/compat/mod.rs | 4 ++-- sqlite3/tests/sqlite3_tests.c | 4 ++-- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/sqlite3/src/lib.rs b/sqlite3/src/lib.rs index d4657f370..66d4ff1a1 100644 --- a/sqlite3/src/lib.rs +++ b/sqlite3/src/lib.rs @@ -708,20 +708,18 @@ pub unsafe extern "C" fn sqlite3_column_type( idx: ffi::c_int, ) -> ffi::c_int { let stmt = &mut *stmt; + let row = stmt + .stmt + .row() + .expect("Function should only be called after `SQLITE_ROW`"); - if let Some(val) = stmt.stmt.get_column_type(idx as usize) { - match val.as_str() { - "INTEGER" => return SQLITE_INTEGER, - "REAL" => return SQLITE_FLOAT, - "TEXT" => return SQLITE_TEXT, - "BLOB" => return SQLITE_BLOB, - - //SQLite3 column type doesn't cover numeric value - _ => return SQLITE_NULL, - } + match row.get::<&Value>(idx as usize) { + Ok(turso_core::Value::Integer(_)) => SQLITE_INTEGER, + Ok(turso_core::Value::Text(_)) => SQLITE_TEXT, + Ok(turso_core::Value::Float(_)) => SQLITE_FLOAT, + Ok(turso_core::Value::Blob(_)) => SQLITE_BLOB, + _ => SQLITE_NULL, } - - SQLITE_NULL } #[no_mangle] diff --git a/sqlite3/tests/compat/mod.rs b/sqlite3/tests/compat/mod.rs index efc018487..bc9fffa98 100644 --- a/sqlite3/tests/compat/mod.rs +++ b/sqlite3/tests/compat/mod.rs @@ -666,7 +666,7 @@ mod tests { assert_eq!( sqlite3_prepare_v2( db, - c"CREATE TABLE test_types (col_int INTEGER, col_float REAL, col_text TEXT, col_blob BLOB, col_null NULL)".as_ptr(), + c"CREATE TABLE test_types (col_int INTEGER, col_float REAL, col_text TEXT, col_blob BLOB, col_null text)".as_ptr(), -1, &mut stmt, std::ptr::null_mut(), @@ -680,7 +680,7 @@ mod tests { assert_eq!( sqlite3_prepare_v2( db, - c"INSERT INTO test_types VALUES (123, 45.67, 'hello', x'010203', 'null')" + c"INSERT INTO test_types VALUES (123, 45.67, 'hello', x'010203', null)" .as_ptr(), -1, &mut stmt, diff --git a/sqlite3/tests/sqlite3_tests.c b/sqlite3/tests/sqlite3_tests.c index 6f39f4773..3315c8273 100644 --- a/sqlite3/tests/sqlite3_tests.c +++ b/sqlite3/tests/sqlite3_tests.c @@ -494,12 +494,12 @@ void test_sqlite3_column_type() assert(rc == SQLITE_OK); rc = sqlite3_exec(db, - "CREATE TABLE test_column_type (col_int INTEGER, col_float REAL, col_text TEXT, col_blob BLOB, col_null NULL);", + "CREATE TABLE test_column_type (col_int INTEGER, col_float REAL, col_text TEXT, col_blob BLOB, col_null TEXT);", NULL, NULL, NULL); assert(rc == SQLITE_OK); rc = sqlite3_exec(db, - "INSERT INTO test_column_type VALUES (42, 3.14, 'hello', x'010203', 'null');", + "INSERT INTO test_column_type VALUES (42, 3.14, 'hello', x'010203', NULL);", NULL, NULL, NULL); assert(rc == SQLITE_OK);