Partial fix following John suggestions.

This commit is contained in:
Gwenael Treguier 2015-12-15 21:49:59 +01:00
parent 108b6b6fcd
commit 880a78ae83
2 changed files with 36 additions and 8 deletions

View File

@ -49,22 +49,27 @@ impl<'conn> StatementCache<'conn> {
return stmt.finalize(); return stmt.finalize();
} }
stmt.reset_if_needed(); stmt.reset_if_needed();
// clear bindings ??? stmt.clear_bindings();
self.cache.insert(stmt.sql(), stmt).map_or(Ok(()), |stmt| stmt.finalize()) self.cache.insert(stmt.sql(), stmt).map_or(Ok(()), |stmt| stmt.finalize())
} }
/// Flush the prepared statement cache /// Flush the prepared statement cache
pub fn flush(&mut self) { pub fn clear(&mut self) {
self.cache.clear(); self.cache.clear();
} }
/// Return (current, max) sizes. /// Return current cache size.
pub fn size(&self) -> (usize, usize) { pub fn len(&self) -> usize {
(self.cache.len(), self.cache.capacity()) self.cache.len()
}
/// Return maximum cache size.
pub fn capacity(&self) -> usize {
self.cache.capacity()
} }
/// Set the maximum number of cached statements. /// Set the maximum number of cached statements.
pub fn set_size(&mut self, capacity: usize) { pub fn set_capacity(&mut self, capacity: usize) {
self.cache.set_capacity(capacity); self.cache.set_capacity(capacity);
} }
} }
@ -78,13 +83,28 @@ mod test {
fn test_cache() { fn test_cache() {
let db = Connection::open_in_memory().unwrap(); let db = Connection::open_in_memory().unwrap();
let mut cache = StatementCache::new(&db, 10); let mut cache = StatementCache::new(&db, 10);
assert_eq!(0, cache.len());
assert_eq!(10, cache.capacity());
let sql = "PRAGMA schema_version"; let sql = "PRAGMA schema_version";
let mut stmt = cache.get(sql).unwrap(); let mut stmt = cache.get(sql).unwrap();
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i64>(0));
// println!("NEW {:?}", stmt); // println!("NEW {:?}", stmt);
cache.release(stmt, false).unwrap(); cache.release(stmt, false).unwrap();
assert_eq!(1, cache.len());
stmt = cache.get(sql).unwrap(); stmt = cache.get(sql).unwrap();
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i64>(0));
// println!("CACHED {:?}", stmt); // println!("CACHED {:?}", stmt);
cache.release(stmt, true).unwrap(); cache.release(stmt, true).unwrap();
cache.flush(); assert_eq!(0, cache.len());
cache.clear();
assert_eq!(0, cache.len());
assert_eq!(10, cache.capacity());
} }
} }

View File

@ -929,7 +929,15 @@ impl<'conn> Statement<'conn> {
} }
} }
fn sql(&self) -> String { // TODO Maybe SQL should by kept as an SqliteStatement field ? #[cfg(feature = "cache")]
fn clear_bindings(&mut self) {
unsafe {
ffi::sqlite3_clear_bindings(self.stmt);
};
}
#[cfg(feature = "cache")]
fn sql(&self) -> String {
unsafe { unsafe {
let c_slice = CStr::from_ptr(ffi::sqlite3_sql(self.stmt)).to_bytes(); let c_slice = CStr::from_ptr(ffi::sqlite3_sql(self.stmt)).to_bytes();
let utf8_str = str::from_utf8(c_slice); let utf8_str = str::from_utf8(c_slice);