Merge pull request #646 from gwenn/execute_alter

Fix error while executing ALTER statement
This commit is contained in:
gwenn 2020-03-04 20:56:50 +01:00 committed by GitHub
commit f2063c5bec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -1118,6 +1118,7 @@ mod test {
} }
#[test] #[test]
#[cfg(feature = "extra_check")]
fn test_execute_select() { fn test_execute_select() {
let db = checked_memory_handle(); let db = checked_memory_handle();
let err = db.execute("SELECT 1 WHERE 1 < ?", &[1i32]).unwrap_err(); let err = db.execute("SELECT 1 WHERE 1 < ?", &[1i32]).unwrap_err();
@ -1745,5 +1746,14 @@ mod test {
) )
.unwrap(); .unwrap();
} }
#[test]
#[cfg(not(feature = "extra_check"))]
fn test_alter_table() {
let db = checked_memory_handle();
db.execute_batch("CREATE TABLE x(t);").unwrap();
// `execute_batch` should be used but `execute` should also work
db.execute("ALTER TABLE x RENAME TO y;", params![]).unwrap();
}
} }
} }

View File

@ -515,13 +515,7 @@ impl Statement<'_> {
let r = self.stmt.step(); let r = self.stmt.step();
self.stmt.reset(); self.stmt.reset();
match r { match r {
ffi::SQLITE_DONE => { ffi::SQLITE_DONE => Ok(self.conn.changes()),
if self.column_count() == 0 {
Ok(self.conn.changes())
} else {
Err(Error::ExecuteReturnedResults)
}
}
ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults), ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults),
_ => Err(self.conn.decode_result(r).unwrap_err()), _ => Err(self.conn.decode_result(r).unwrap_err()),
} }
@ -551,6 +545,7 @@ impl Statement<'_> {
#[cfg(all(feature = "modern_sqlite", feature = "extra_check"))] #[cfg(all(feature = "modern_sqlite", feature = "extra_check"))]
#[inline] #[inline]
fn check_update(&self) -> Result<()> { fn check_update(&self) -> Result<()> {
// sqlite3_column_count works for DML but not for DDL (ie ALTER)
if self.column_count() > 0 || self.stmt.readonly() { if self.column_count() > 0 || self.stmt.readonly() {
return Err(Error::ExecuteReturnedResults); return Err(Error::ExecuteReturnedResults);
} }
@ -560,6 +555,7 @@ impl Statement<'_> {
#[cfg(all(not(feature = "modern_sqlite"), feature = "extra_check"))] #[cfg(all(not(feature = "modern_sqlite"), feature = "extra_check"))]
#[inline] #[inline]
fn check_update(&self) -> Result<()> { fn check_update(&self) -> Result<()> {
// sqlite3_column_count works for DML but not for DDL (ie ALTER)
if self.column_count() > 0 { if self.column_count() > 0 {
return Err(Error::ExecuteReturnedResults); return Err(Error::ExecuteReturnedResults);
} }