sql_support/
maybe_cached.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use rusqlite::{self, CachedStatement, Connection, Statement};

use std::ops::{Deref, DerefMut};

/// MaybeCached is a type that can be used to help abstract
/// over cached and uncached rusqlite statements in a transparent manner.
pub enum MaybeCached<'conn> {
    Uncached(Statement<'conn>),
    Cached(CachedStatement<'conn>),
}

impl<'conn> Deref for MaybeCached<'conn> {
    type Target = Statement<'conn>;
    #[inline]
    fn deref(&self) -> &Statement<'conn> {
        match self {
            MaybeCached::Cached(cached) => Deref::deref(cached),
            MaybeCached::Uncached(uncached) => uncached,
        }
    }
}

impl<'conn> DerefMut for MaybeCached<'conn> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Statement<'conn> {
        match self {
            MaybeCached::Cached(cached) => DerefMut::deref_mut(cached),
            MaybeCached::Uncached(uncached) => uncached,
        }
    }
}

impl<'conn> From<Statement<'conn>> for MaybeCached<'conn> {
    #[inline]
    fn from(stmt: Statement<'conn>) -> Self {
        MaybeCached::Uncached(stmt)
    }
}

impl<'conn> From<CachedStatement<'conn>> for MaybeCached<'conn> {
    #[inline]
    fn from(stmt: CachedStatement<'conn>) -> Self {
        MaybeCached::Cached(stmt)
    }
}

impl<'conn> MaybeCached<'conn> {
    #[inline]
    pub fn prepare(
        conn: &'conn Connection,
        sql: &str,
        cached: bool,
    ) -> rusqlite::Result<MaybeCached<'conn>> {
        if cached {
            Ok(MaybeCached::Cached(conn.prepare_cached(sql)?))
        } else {
            Ok(MaybeCached::Uncached(conn.prepare(sql)?))
        }
    }
}