mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 11:31:37 +08:00
Reset in Rows's drop impl instead of waiting for the next query
This commit is contained in:
parent
3a52dd65f0
commit
4a6c7b5329
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
22
src/lib.rs
22
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<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>;
|
||||
|
||||
|
@ -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))
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user