This commit is contained in:
John Gallagher 2016-05-17 12:14:21 -05:00
parent bd81b727f0
commit 20b93bdb96

View File

@ -92,9 +92,7 @@ impl<'conn> CachedStatement<'conn> {
impl StatementCache { impl StatementCache {
/// Create a statement cache. /// Create a statement cache.
pub fn with_capacity(capacity: usize) -> StatementCache { pub fn with_capacity(capacity: usize) -> StatementCache {
StatementCache { StatementCache { cache: RefCell::new(VecDeque::with_capacity(capacity)) }
cache: RefCell::new(VecDeque::with_capacity(capacity)),
}
} }
/// Search the cache for a prepared-statement object that implements `sql`. /// Search the cache for a prepared-statement object that implements `sql`.
@ -102,10 +100,15 @@ impl StatementCache {
/// ///
/// # Failure /// # Failure
/// ///
/// Will return `Err` if no cached statement can be found and the underlying SQLite prepare call fails. /// Will return `Err` if no cached statement can be found and the underlying SQLite prepare
pub fn get<'conn>(&'conn self, conn: &'conn Connection, sql: &str) -> Result<CachedStatement<'conn>> { /// call fails.
pub fn get<'conn>(&'conn self,
conn: &'conn Connection,
sql: &str)
-> Result<CachedStatement<'conn>> {
let mut cache = self.cache.borrow_mut(); let mut cache = self.cache.borrow_mut();
let stmt = match cache.iter().rposition(|entry| entry.sql().to_bytes().eq(sql.as_bytes())) { let stmt = match cache.iter()
.rposition(|entry| entry.sql().to_bytes().eq(sql.as_bytes())) {
Some(index) => { Some(index) => {
let raw_stmt = cache.swap_remove_front(index).unwrap(); // FIXME Not LRU compliant let raw_stmt = cache.swap_remove_front(index).unwrap(); // FIXME Not LRU compliant
Ok(Statement::new(conn, raw_stmt)) Ok(Statement::new(conn, raw_stmt))
@ -159,7 +162,7 @@ mod test {
let mut stmt = db.prepare_cached(sql).unwrap(); let mut stmt = db.prepare_cached(sql).unwrap();
assert_eq!(0, cache.len()); assert_eq!(0, cache.len());
assert_eq!(0, assert_eq!(0,
stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i32,i64>(0)); stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i32, i64>(0));
} }
assert_eq!(1, cache.len()); assert_eq!(1, cache.len());
@ -167,7 +170,7 @@ mod test {
let mut stmt = db.prepare_cached(sql).unwrap(); let mut stmt = db.prepare_cached(sql).unwrap();
assert_eq!(0, cache.len()); assert_eq!(0, cache.len());
assert_eq!(0, assert_eq!(0,
stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i32,i64>(0)); stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i32, i64>(0));
} }
assert_eq!(1, cache.len()); assert_eq!(1, cache.len());
@ -186,7 +189,7 @@ mod test {
let mut stmt = db.prepare_cached(sql).unwrap(); let mut stmt = db.prepare_cached(sql).unwrap();
assert_eq!(0, cache.len()); assert_eq!(0, cache.len());
assert_eq!(0, assert_eq!(0,
stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i32,i64>(0)); stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i32, i64>(0));
stmt.discard(); stmt.discard();
} }
assert_eq!(0, cache.len()); assert_eq!(0, cache.len());