From 1dc144c8c1ba68155ecb82967523057093e9bf83 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 1 Aug 2015 08:09:59 +0200 Subject: [PATCH] Add test_execute_select. --- src/lib.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f98780b..cd83fe2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -580,7 +580,7 @@ impl<'conn> SqliteStatement<'conn> { /// Get all the column names in the result set of the prepared statement. pub fn column_names(&self) -> Vec<&str> { - let n = unsafe { ffi::sqlite3_column_count(self.stmt) }; + let n = self.column_count(); let mut cols = Vec::with_capacity(n as usize); for i in 0..n { let slice = unsafe { @@ -625,11 +625,13 @@ impl<'conn> SqliteStatement<'conn> { self.needs_reset = true; let r = ffi::sqlite3_step(self.stmt); - match r { - ffi::SQLITE_DONE => Ok(self.conn.changes()), - ffi::SQLITE_ROW => Err(SqliteError{ code: r, - message: "Unexpected row result - did you mean to call query?".to_string() }), - _ => Err(self.conn.decode_result(r).unwrap_err()), + if r == ffi::SQLITE_ROW || self.column_count() != 0 { + Err(SqliteError{ code: r, + message: "Unexpected row result - did you mean to call query?".to_string() }) + } else if r == ffi::SQLITE_DONE { + Ok(self.conn.changes()) + } else { + Err(self.conn.decode_result(r).unwrap_err()) } } } @@ -710,6 +712,10 @@ impl<'conn> SqliteStatement<'conn> { } } + fn column_count(&self) -> c_int { + unsafe { ffi::sqlite3_column_count(self.stmt) } + } + fn finalize_(&mut self) -> SqliteResult<()> { let r = unsafe { ffi::sqlite3_finalize(self.stmt) }; self.stmt = ptr::null_mut(); @@ -895,7 +901,7 @@ impl<'stmt> SqliteRow<'stmt> { message: "Cannot get values from a row after advancing to next row".to_string() }); } unsafe { - if idx < 0 || idx >= ffi::sqlite3_column_count(self.stmt.stmt) { + if idx < 0 || idx >= self.stmt.column_count() { return Err(SqliteError{ code: ffi::SQLITE_MISUSE, message: "Invalid column index".to_string() }); } @@ -993,6 +999,14 @@ mod test { assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)).unwrap()); } + #[test] + fn test_execute_select() { + let db = checked_memory_handle(); + let err = db.execute("SELECT 1 WHERE 1 < ?", &[&1i32]).unwrap_err(); + assert!(err.code == ffi::SQLITE_DONE); + assert!(err.message == "Unexpected row result - did you mean to call query?"); + } + #[test] fn test_prepare_column_names() { let db = checked_memory_handle();