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 {
/// Create a statement cache.
pub fn with_capacity(capacity: usize) -> StatementCache {
StatementCache {
cache: RefCell::new(VecDeque::with_capacity(capacity)),
}
StatementCache { cache: RefCell::new(VecDeque::with_capacity(capacity)) }
}
/// Search the cache for a prepared-statement object that implements `sql`.
@ -102,10 +100,15 @@ impl StatementCache {
///
/// # Failure
///
/// Will return `Err` if no cached statement can be found and the underlying SQLite prepare call fails.
pub fn get<'conn>(&'conn self, conn: &'conn Connection, sql: &str) -> Result<CachedStatement<'conn>> {
/// Will return `Err` if no cached statement can be found and the underlying SQLite prepare
/// call fails.
pub fn get<'conn>(&'conn self,
conn: &'conn Connection,
sql: &str)
-> Result<CachedStatement<'conn>> {
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) => {
let raw_stmt = cache.swap_remove_front(index).unwrap(); // FIXME Not LRU compliant
Ok(Statement::new(conn, raw_stmt))