forked from launchbadge/sqlx
/
mod.rs
102 lines (80 loc) 路 2.6 KB
/
mod.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
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
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
use futures_core::future::BoxFuture;
use futures_util::future;
use hashbrown::HashMap;
use crate::connection::{Connect, Connection};
use crate::error::Error;
use crate::ext::ustr::UStr;
use crate::sqlite::connection::establish::establish;
use crate::sqlite::statement::{SqliteStatement, StatementWorker};
use crate::sqlite::{Sqlite, SqliteConnectOptions};
mod establish;
mod executor;
mod handle;
pub(crate) use handle::ConnectionHandle;
/// A connection to a [Sqlite] database.
pub struct SqliteConnection {
pub(crate) handle: ConnectionHandle,
pub(crate) worker: StatementWorker,
// cache of semi-persistent statements
pub(crate) statements: HashMap<String, SqliteStatement>,
// most recent non-persistent statement
pub(crate) statement: Option<SqliteStatement>,
// working memory for the active row's column information
scratch_row_column_names: Arc<HashMap<UStr, usize>>,
}
impl Debug for SqliteConnection {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("SqliteConnection").finish()
}
}
impl Connection for SqliteConnection {
type Database = Sqlite;
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(()))
}
#[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
}
#[doc(hidden)]
fn get_ref(&self) -> &Self {
self
}
#[doc(hidden)]
fn get_mut(&mut self) -> &mut Self {
self
}
}
impl Connect for SqliteConnection {
type Options = SqliteConnectOptions;
#[inline]
fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>> {
Box::pin(async move {
let conn = establish(options).await?;
// TODO: Apply any connection options once we have them defined
Ok(conn)
})
}
}
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();
// we then explicitly close the worker
self.worker.close();
}
}