Reset in Rows's drop impl instead of waiting for the next query

This commit is contained in:
John Gallagher 2016-05-16 14:02:39 -05:00
parent 3a52dd65f0
commit 4a6c7b5329
4 changed files with 12 additions and 24 deletions

View File

@ -23,16 +23,14 @@ impl<'conn> Statement<'conn> {
/// Return `true` if a query in the SQL statement it executes returns one or more rows
/// and `false` if the SQL returns an empty set.
pub fn exists(&mut self, params: &[&ToSql]) -> Result<bool> {
self.reset_if_needed();
let exists = {
let mut rows = try!(self.query(params));
let exists = {
match rows.next() {
Some(_) => Ok(true),
None => Ok(false),
Some(_) => true,
None => false,
}
};
self.reset_if_needed();
return exists;
Ok(exists)
}
}

View File

@ -708,7 +708,6 @@ pub type SqliteStatement<'conn> = Statement<'conn>;
pub struct Statement<'conn> {
conn: &'conn Connection,
stmt: *mut ffi::sqlite3_stmt,
needs_reset: bool,
column_count: c_int,
}
@ -717,7 +716,6 @@ impl<'conn> Statement<'conn> {
Statement {
conn: conn,
stmt: stmt,
needs_reset: false,
column_count: unsafe { ffi::sqlite3_column_count(stmt) },
}
}
@ -826,13 +824,10 @@ impl<'conn> Statement<'conn> {
///
/// Will return `Err` if binding parameters fails.
pub fn query<'a>(&'a mut self, params: &[&ToSql]) -> Result<Rows<'a>> {
self.reset_if_needed();
unsafe {
try!(self.bind_parameters(params));
}
self.needs_reset = true;
Ok(Rows::new(self))
}
@ -906,15 +901,6 @@ impl<'conn> Statement<'conn> {
Ok(())
}
fn reset_if_needed(&mut self) {
if self.needs_reset {
unsafe {
ffi::sqlite3_reset(self.stmt);
};
self.needs_reset = false;
}
}
fn finalize_(&mut self) -> Result<()> {
let r = unsafe { ffi::sqlite3_finalize(self.stmt) };
self.stmt = ptr::null_mut();
@ -1064,6 +1050,14 @@ impl<'stmt> Iterator for Rows<'stmt> {
}
}
impl<'stmt> Drop for Rows<'stmt> {
fn drop(&mut self) {
unsafe {
ffi::sqlite3_reset(self.stmt.stmt);
}
}
}
/// Old name for `Row`. `SqliteRow` is deprecated.
pub type SqliteRow<'stmt> = Row<'stmt>;

View File

@ -113,10 +113,7 @@ impl<'conn> Statement<'conn> {
///
/// Will return `Err` if binding parameters fails.
pub fn query_named<'a>(&'a mut self, params: &[(&str, &ToSql)]) -> Result<Rows<'a>> {
self.reset_if_needed();
try!(self.bind_parameters_named(params));
self.needs_reset = true;
Ok(Rows::new(self))
}

View File

@ -4,7 +4,6 @@ use libc::{c_char, c_int, c_void};
use std::ffi::{CStr, CString};
use std::mem;
use std::ptr;
use std::str;
use std::time::Duration;
use super::ffi;