Add test_execute_select.

This commit is contained in:
gwenn 2015-08-01 08:09:59 +02:00
parent e8967388e6
commit 1dc144c8c1

View File

@ -580,7 +580,7 @@ impl<'conn> SqliteStatement<'conn> {
/// Get all the column names in the result set of the prepared statement. /// Get all the column names in the result set of the prepared statement.
pub fn column_names(&self) -> Vec<&str> { 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); let mut cols = Vec::with_capacity(n as usize);
for i in 0..n { for i in 0..n {
let slice = unsafe { let slice = unsafe {
@ -625,11 +625,13 @@ impl<'conn> SqliteStatement<'conn> {
self.needs_reset = true; self.needs_reset = true;
let r = ffi::sqlite3_step(self.stmt); let r = ffi::sqlite3_step(self.stmt);
match r { if r == ffi::SQLITE_ROW || self.column_count() != 0 {
ffi::SQLITE_DONE => Ok(self.conn.changes()), Err(SqliteError{ code: r,
ffi::SQLITE_ROW => Err(SqliteError{ code: r, message: "Unexpected row result - did you mean to call query?".to_string() })
message: "Unexpected row result - did you mean to call query?".to_string() }), } else if r == ffi::SQLITE_DONE {
_ => Err(self.conn.decode_result(r).unwrap_err()), 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<()> { fn finalize_(&mut self) -> SqliteResult<()> {
let r = unsafe { ffi::sqlite3_finalize(self.stmt) }; let r = unsafe { ffi::sqlite3_finalize(self.stmt) };
self.stmt = ptr::null_mut(); 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() }); message: "Cannot get values from a row after advancing to next row".to_string() });
} }
unsafe { 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, return Err(SqliteError{ code: ffi::SQLITE_MISUSE,
message: "Invalid column index".to_string() }); 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()); 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] #[test]
fn test_prepare_column_names() { fn test_prepare_column_names() {
let db = checked_memory_handle(); let db = checked_memory_handle();