diff --git a/src/cache.rs b/src/cache.rs index 28b047d..5522f06 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -4,12 +4,13 @@ use std::cell::RefCell; use std::collections::VecDeque; use std::ops::{Deref, DerefMut}; use {Result, Connection, Statement}; +use raw_statement::RawStatement; /// Prepared statements LRU cache. #[derive(Debug)] pub struct StatementCache<'conn> { conn: &'conn Connection, - cache: RefCell>>, // back = LRU + cache: RefCell>, // back = LRU } /// Cacheable statement. @@ -39,7 +40,7 @@ impl<'c, 's> Drop for CachedStatement<'c, 's> { #[allow(unused_must_use)] fn drop(&mut self) { if let Some(stmt) = self.stmt.take() { - self.cache.cache_stmt(stmt); + self.cache.cache_stmt(stmt.into()); } } } @@ -74,15 +75,18 @@ impl<'conn> StatementCache<'conn> { /// Will return `Err` if no cached statement can be found and the underlying SQLite prepare call fails. pub fn get<'s>(&'s self, sql: &str) -> Result> { let mut cache = self.cache.borrow_mut(); - let stmt = match cache.iter().rposition(|entry| entry.eq(sql)) { - Some(index) => Ok(cache.swap_remove_front(index).unwrap()), // FIXME Not LRU compliant + let stmt = match cache.iter().rposition(|entry| entry.sql().to_bytes().eq(sql.as_bytes())) { + Some(index) => { + let raw_stmt = cache.swap_remove_front(index).unwrap(); // FIXME Not LRU compliant + Ok(Statement::new(self.conn, raw_stmt)) + } _ => self.conn.prepare(sql), }; stmt.map(|stmt| CachedStatement::new(stmt, self)) } // Return a statement to the cache. - fn cache_stmt(&self, mut stmt: Statement<'conn>) { + fn cache_stmt(&self, stmt: RawStatement) { let mut cache = self.cache.borrow_mut(); if cache.capacity() == cache.len() { // is full diff --git a/src/lib.rs b/src/lib.rs index 62b9106..8512857 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -901,17 +901,6 @@ impl<'conn> Statement<'conn> { Ok(()) } - #[cfg(feature = "cache")] - fn clear_bindings(&mut self) { - self.stmt.clear_bindings(); - } - - #[cfg(feature = "cache")] - fn eq(&self, sql: &str) -> bool { - let c_slice = self.stmt.sql().to_bytes(); - sql.as_bytes().eq(c_slice) - } - fn finalize_(&mut self) -> Result<()> { let mut stmt = RawStatement::new(ptr::null_mut()); mem::swap(&mut stmt, &mut self.stmt); @@ -919,6 +908,14 @@ impl<'conn> Statement<'conn> { } } +impl<'conn> Into for Statement<'conn> { + fn into(mut self) -> RawStatement { + let mut stmt = RawStatement::new(ptr::null_mut()); + mem::swap(&mut stmt, &mut self.stmt); + stmt + } +} + impl<'conn> fmt::Debug for Statement<'conn> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let sql = str::from_utf8(self.stmt.sql().to_bytes());