mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-29 21:52:12 +08:00
Simplify CachedStatement lifetimes
This commit is contained in:
parent
3c15eb0218
commit
bd81b727f0
26
src/cache.rs
26
src/cache.rs
@ -33,7 +33,7 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
|
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
|
||||||
/// underlying SQLite call fails.
|
/// underlying SQLite call fails.
|
||||||
pub fn prepare_cached<'a>(&'a self, sql: &str) -> Result<CachedStatement<'a, 'a>> {
|
pub fn prepare_cached<'a>(&'a self, sql: &str) -> Result<CachedStatement<'a>> {
|
||||||
self.cache.get(&self, sql)
|
self.cache.get(&self, sql)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,26 +48,26 @@ pub struct StatementCache {
|
|||||||
///
|
///
|
||||||
/// Statement will return automatically to the cache by default.
|
/// Statement will return automatically to the cache by default.
|
||||||
/// If you want the statement to be discarded, call `discard()` on it.
|
/// If you want the statement to be discarded, call `discard()` on it.
|
||||||
pub struct CachedStatement<'c: 's, 's> {
|
pub struct CachedStatement<'conn> {
|
||||||
stmt: Option<Statement<'c>>,
|
stmt: Option<Statement<'conn>>,
|
||||||
cache: &'s StatementCache,
|
cache: &'conn StatementCache,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c, 's> Deref for CachedStatement<'c, 's> {
|
impl<'conn> Deref for CachedStatement<'conn> {
|
||||||
type Target = Statement<'c>;
|
type Target = Statement<'conn>;
|
||||||
|
|
||||||
fn deref(&self) -> &Statement<'c> {
|
fn deref(&self) -> &Statement<'conn> {
|
||||||
self.stmt.as_ref().unwrap()
|
self.stmt.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c, 's> DerefMut for CachedStatement<'c, 's> {
|
impl<'conn> DerefMut for CachedStatement<'conn> {
|
||||||
fn deref_mut(&mut self) -> &mut Statement<'c> {
|
fn deref_mut(&mut self) -> &mut Statement<'conn> {
|
||||||
self.stmt.as_mut().unwrap()
|
self.stmt.as_mut().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c, 's> Drop for CachedStatement<'c, 's> {
|
impl<'conn> Drop for CachedStatement<'conn> {
|
||||||
#[allow(unused_must_use)]
|
#[allow(unused_must_use)]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(stmt) = self.stmt.take() {
|
if let Some(stmt) = self.stmt.take() {
|
||||||
@ -76,8 +76,8 @@ impl<'c, 's> Drop for CachedStatement<'c, 's> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c, 's> CachedStatement<'c, 's> {
|
impl<'conn> CachedStatement<'conn> {
|
||||||
fn new(stmt: Statement<'c>, cache: &'s StatementCache) -> CachedStatement<'c, 's> {
|
fn new(stmt: Statement<'conn>, cache: &'conn StatementCache) -> CachedStatement<'conn> {
|
||||||
CachedStatement {
|
CachedStatement {
|
||||||
stmt: Some(stmt),
|
stmt: Some(stmt),
|
||||||
cache: cache,
|
cache: cache,
|
||||||
@ -103,7 +103,7 @@ 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 call fails.
|
||||||
pub fn get<'conn, 's>(&'s self, conn: &'conn Connection, sql: &str) -> Result<CachedStatement<'conn, 's>> {
|
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) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user