From 7ddf1316a28cd37ce1e0ca44a885773027e65462 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sun, 4 Jul 2021 16:04:13 +0200 Subject: [PATCH 1/4] Replace some usages of checked_memory_handle In tests which return Result, we don't need to unwrap. --- src/lib.rs | 69 +++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0edcbb4..ce66d85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1083,7 +1083,7 @@ mod test { ensure_sync::(); } - pub fn checked_memory_handle() -> Connection { + fn checked_memory_handle() -> Connection { Connection::open_in_memory().unwrap() } @@ -1211,7 +1211,7 @@ mod test { #[test] fn test_close_retry() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; // force the DB to be busy by preparing a statement; this must be done at the // FFI level to allow us to call .close() without dropping the prepared @@ -1264,7 +1264,7 @@ mod test { #[test] fn test_execute_batch() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER); INSERT INTO foo VALUES(1); @@ -1282,7 +1282,7 @@ mod test { #[test] fn test_execute() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER)")?; assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [1i32])?); @@ -1323,7 +1323,7 @@ mod test { #[test] fn test_prepare_column_names() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?; let stmt = db.prepare("SELECT * FROM foo")?; @@ -1338,7 +1338,7 @@ mod test { #[test] fn test_prepare_execute() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?; let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?; @@ -1359,7 +1359,7 @@ mod test { #[test] fn test_prepare_query() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?; let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?; @@ -1394,7 +1394,7 @@ mod test { #[test] fn test_query_map() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1413,7 +1413,7 @@ mod test { #[test] fn test_query_row() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER); INSERT INTO foo VALUES(1); @@ -1442,7 +1442,7 @@ mod test { #[test] fn test_optional() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let result: Result = db.query_row("SELECT 1 WHERE 0 <> 0", [], |r| r.get(0)); let result = result.optional(); @@ -1466,7 +1466,7 @@ mod test { #[test] fn test_pragma_query_row() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; assert_eq!( "memory", @@ -1481,7 +1481,7 @@ mod test { #[test] fn test_prepare_failures() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?; let err = db.prepare("SELECT * FROM does_not_exist").unwrap_err(); @@ -1491,7 +1491,7 @@ mod test { #[test] fn test_last_insert_rowid() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?; db.execute_batch("INSERT INTO foo DEFAULT VALUES")?; @@ -1506,18 +1506,19 @@ mod test { } #[test] - fn test_is_autocommit() { - let db = checked_memory_handle(); + fn test_is_autocommit() -> Result<()> { + let db = Connection::open_in_memory()?; assert!( db.is_autocommit(), "autocommit expected to be active by default" ); + Ok(()) } #[test] #[cfg(feature = "modern_sqlite")] fn test_is_busy() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; assert!(!db.is_busy()); let mut stmt = db.prepare("PRAGMA schema_version")?; assert!(!db.is_busy()); @@ -1534,7 +1535,7 @@ mod test { #[test] fn test_statement_debugging() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let query = "SELECT 12345"; let stmt = db.prepare(query)?; @@ -1553,7 +1554,7 @@ mod test { #[cfg(not(feature = "modern_sqlite"))] fn check_extended_code(_extended_code: c_int) {} - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x NOT NULL)")?; let result = db.execute("INSERT INTO foo (x) VALUES (NULL)", []); @@ -1582,7 +1583,7 @@ mod test { #[test] #[cfg(feature = "functions")] fn test_interrupt() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let interrupt_handle = db.get_interrupt_handle(); @@ -1630,7 +1631,7 @@ mod test { #[test] fn test_get_raw() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(i, x);")?; let vals = ["foobar", "1234", "qwerty"]; let mut insert_stmt = db.prepare("INSERT INTO foo(i, x) VALUES(?, ?)")?; @@ -1663,7 +1664,7 @@ mod test { #[test] fn test_from_handle() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let handle = unsafe { db.handle() }; { let db = unsafe { Connection::from_handle(handle) }?; @@ -1715,7 +1716,7 @@ mod test { #[test] fn test_query_and_then() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1735,7 +1736,7 @@ mod test { #[test] fn test_query_and_then_fails() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1765,7 +1766,7 @@ mod test { #[test] fn test_query_and_then_custom_error() -> CustomResult<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1786,7 +1787,7 @@ mod test { #[test] fn test_query_and_then_custom_error_fails() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1828,7 +1829,7 @@ mod test { #[test] fn test_query_row_and_then_custom_error() -> CustomResult<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1845,7 +1846,7 @@ mod test { #[test] fn test_query_row_and_then_custom_error_fails() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1882,7 +1883,7 @@ mod test { #[test] fn test_dynamic() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, \"hello\"); @@ -1896,7 +1897,7 @@ mod test { } #[test] fn test_dyn_box() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?; let b: Box = Box::new(5); db.execute("INSERT INTO foo VALUES(?)", [b])?; @@ -1908,7 +1909,7 @@ mod test { #[test] fn test_params() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.query_row( "SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @@ -1929,7 +1930,7 @@ mod test { #[test] #[cfg(not(feature = "extra_check"))] fn test_alter_table() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE x(t);")?; // `execute_batch` should be used but `execute` should also work db.execute("ALTER TABLE x RENAME TO y;", [])?; @@ -1938,7 +1939,7 @@ mod test { #[test] fn test_batch() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; let sql = r" CREATE TABLE tbl1 (col); CREATE TABLE tbl2 (col); @@ -1954,7 +1955,7 @@ mod test { #[test] #[cfg(all(feature = "bundled", not(feature = "bundled-sqlcipher")))] // SQLite >= 3.35.0 fn test_returning() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?; let row_id = db.query_row::("INSERT INTO foo DEFAULT VALUES RETURNING ROWID", [], |r| { @@ -1967,7 +1968,7 @@ mod test { #[test] #[cfg(feature = "modern_sqlite")] fn test_cache_flush() -> Result<()> { - let db = checked_memory_handle(); + let db = Connection::open_in_memory()?; db.cache_flush() } } From 2dea70304d224c666449b8a047b12718359c91cd Mon Sep 17 00:00:00 2001 From: gwenn Date: Sun, 4 Jul 2021 16:53:40 +0200 Subject: [PATCH 2/4] Retrieve error message from database connection handle --- src/backup.rs | 8 ++++---- src/blob/pos_io.rs | 13 ++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/backup.rs b/src/backup.rs index 88677fe..5104f73 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -40,7 +40,7 @@ use std::time::Duration; use crate::ffi; -use crate::error::{error_from_handle, error_from_sqlite_code}; +use crate::error::error_from_handle; use crate::{Connection, DatabaseName, Result}; impl Connection { @@ -169,7 +169,7 @@ pub struct Progress { /// A handle to an online backup. pub struct Backup<'a, 'b> { phantom_from: PhantomData<&'a Connection>, - phantom_to: PhantomData<&'b Connection>, + to: &'b Connection, b: *mut ffi::sqlite3_backup, } @@ -223,7 +223,7 @@ impl Backup<'_, '_> { Ok(Backup { phantom_from: PhantomData, - phantom_to: PhantomData, + to, b, }) } @@ -263,7 +263,7 @@ impl Backup<'_, '_> { ffi::SQLITE_OK => Ok(More), ffi::SQLITE_BUSY => Ok(Busy), ffi::SQLITE_LOCKED => Ok(Locked), - _ => Err(error_from_sqlite_code(rc, None)), + _ => self.to.decode_result(rc).map(|_| More), } } diff --git a/src/blob/pos_io.rs b/src/blob/pos_io.rs index dd6167d..01f9c34 100644 --- a/src/blob/pos_io.rs +++ b/src/blob/pos_io.rs @@ -44,15 +44,14 @@ impl<'conn> Blob<'conn> { // losslessly converted to i32, since `len` came from an i32. // Sanity check the above. debug_assert!(i32::try_from(write_start).is_ok() && i32::try_from(buf.len()).is_ok()); - unsafe { - check!(ffi::sqlite3_blob_write( + self.conn.decode_result(unsafe { + ffi::sqlite3_blob_write( self.blob, buf.as_ptr() as *const _, buf.len() as i32, write_start as i32, - )); - } - Ok(()) + ) + }) } /// An alias for `write_at` provided for compatibility with the conceptually @@ -151,12 +150,12 @@ impl<'conn> Blob<'conn> { debug_assert!(i32::try_from(read_len).is_ok()); unsafe { - check!(ffi::sqlite3_blob_read( + self.conn.decode_result(ffi::sqlite3_blob_read( self.blob, buf.as_mut_ptr() as *mut _, read_len as i32, read_start as i32, - )); + ))?; Ok(from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, read_len)) } From 7c2263dabc2c04d6908d83eede603456ae2c6a33 Mon Sep 17 00:00:00 2001 From: gwenn Date: Wed, 21 Jul 2021 18:48:05 +0200 Subject: [PATCH 3/4] Upgrage bindgen to version 0.59 --- libsqlite3-sys/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsqlite3-sys/Cargo.toml b/libsqlite3-sys/Cargo.toml index 5c55eb6..37e6c52 100644 --- a/libsqlite3-sys/Cargo.toml +++ b/libsqlite3-sys/Cargo.toml @@ -42,7 +42,7 @@ winsqlite3 = ["min_sqlite_version_3_7_16"] openssl-sys = { version = "0.9.58", optional = true } [build-dependencies] -bindgen = { version = "0.58", optional = true, default-features = false, features = ["runtime"] } +bindgen = { version = "0.59", optional = true, default-features = false, features = ["runtime"] } pkg-config = { version = "0.3.19", optional = true } cc = { version = "1.0", optional = true } vcpkg = { version = "0.2", optional = true } From 0241cb03888c2ea1810c2dab0f582dd9e3dcaef4 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sun, 25 Jul 2021 17:05:35 +1000 Subject: [PATCH 4/4] Fix the typename used in doc for VTab and VTabCursor Signed-off-by: Jiahao XU --- src/vtab/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 966c918..12d71e2 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -198,7 +198,7 @@ impl VTabConnection { /// #[repr(C)] /// struct MyTab { /// /// Base class. Must be first -/// base: ffi::sqlite3_vtab, +/// base: rusqlite::vtab::sqlite3_vtab, /// /* Virtual table implementations will typically add additional fields */ /// } /// ``` @@ -488,7 +488,7 @@ impl OrderBy<'_> { /// #[repr(C)] /// struct MyTabCursor { /// /// Base class. Must be first -/// base: ffi::sqlite3_vtab_cursor, +/// base: rusqlite::vtab::sqlite3_vtab_cursor, /// /* Virtual table implementations will typically add additional fields */ /// } /// ```