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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::common::StatementCache;
use crate::connection::{Connection, LogSettings};
use crate::error::Error;
use crate::sqlite::statement::{StatementWorker, VirtualStatement};
use crate::sqlite::{Sqlite, SqliteConnectOptions};
use crate::transaction::Transaction;
use futures_core::future::BoxFuture;
use futures_util::future;
use libsqlite3_sys::sqlite3;
use std::cmp::Ordering;
use std::fmt::{self, Debug, Formatter};

mod collation;
mod describe;
pub(crate) mod establish;
mod executor;
mod explain;
mod handle;

pub(crate) use handle::ConnectionHandle;

/// A connection to a [Sqlite] database.
pub struct SqliteConnection {
    pub(crate) handle: ConnectionHandle,
    pub(crate) worker: StatementWorker,

    // transaction status
    pub(crate) transaction_depth: usize,

    // cache of semi-persistent statements
    pub(crate) statements: StatementCache<VirtualStatement>,

    // most recent non-persistent statement
    pub(crate) statement: Option<VirtualStatement>,

    log_settings: LogSettings,
}

impl SqliteConnection {
    /// Returns the underlying sqlite3* connection handle
    pub fn as_raw_handle(&mut self) -> *mut sqlite3 {
        self.handle.as_ptr()
    }

    pub fn create_collation(
        &mut self,
        name: &str,
        compare: impl Fn(&str, &str) -> Ordering + Send + Sync + 'static,
    ) -> Result<(), Error> {
        collation::create_collation(&self.handle, name, compare)
    }
}

impl Debug for SqliteConnection {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("SqliteConnection").finish()
    }
}

impl Connection for SqliteConnection {
    type Database = Sqlite;

    type Options = SqliteConnectOptions;

    fn close(self) -> BoxFuture<'static, Result<(), Error>> {
        // nothing explicit to do; connection will close in drop
        Box::pin(future::ok(()))
    }

    fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
        // For SQLite connections, PING does effectively nothing
        Box::pin(future::ok(()))
    }

    fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
    where
        Self: Sized,
    {
        Transaction::begin(self)
    }

    fn cached_statements_size(&self) -> usize {
        self.statements.len()
    }

    fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
        Box::pin(async move {
            self.statements.clear();
            Ok(())
        })
    }

    #[doc(hidden)]
    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
        // For SQLite, FLUSH does effectively nothing
        Box::pin(future::ok(()))
    }

    #[doc(hidden)]
    fn should_flush(&self) -> bool {
        false
    }
}

impl Drop for SqliteConnection {
    fn drop(&mut self) {
        // before the connection handle is dropped,
        // we must explicitly drop the statements as the drop-order in a struct is undefined
        self.statements.clear();
        self.statement.take();
    }
}