From 4a6c7b5329de2fcec7cb99bf0357df80d7d896c5 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Mon, 16 May 2016 14:02:39 -0500 Subject: [PATCH] Reset in Rows's drop impl instead of waiting for the next query --- src/convenient.rs | 10 ++++------ src/lib.rs | 22 ++++++++-------------- src/named_params.rs | 3 --- src/trace.rs | 1 - 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/convenient.rs b/src/convenient.rs index 0e2625e..5f867b4 100644 --- a/src/convenient.rs +++ b/src/convenient.rs @@ -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 { - self.reset_if_needed(); + let mut rows = try!(self.query(params)); let exists = { - let mut rows = try!(self.query(params)); match rows.next() { - Some(_) => Ok(true), - None => Ok(false), + Some(_) => true, + None => false, } }; - self.reset_if_needed(); - return exists; + Ok(exists) } } diff --git a/src/lib.rs b/src/lib.rs index 6dc0f87..4e2e6e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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> { - 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>; diff --git a/src/named_params.rs b/src/named_params.rs index 6271142..3e62610 100644 --- a/src/named_params.rs +++ b/src/named_params.rs @@ -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> { - self.reset_if_needed(); try!(self.bind_parameters_named(params)); - - self.needs_reset = true; Ok(Rows::new(self)) } diff --git a/src/trace.rs b/src/trace.rs index 7dd1417..4cea711 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -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;