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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::{
ast,
connector::{self, ConnectionInfo, Queryable, SqlFamily, TransactionCapable, DBIO},
};
use futures::lock::Mutex;
use url::Url;
use std::sync::Arc;
#[cfg(feature = "sqlite")]
use std::convert::TryFrom;
#[derive(Clone)]
pub struct Quaint {
inner: Arc<Mutex<Box<dyn Queryable + Send + Sync>>>,
connection_info: Arc<ConnectionInfo>,
}
impl TransactionCapable for Quaint {}
impl Quaint {
pub async fn new(url_str: &str) -> crate::Result<Self> {
let url = Url::parse(url_str)?;
let inner = match url.scheme() {
#[cfg(feature = "sqlite")]
"file" | "sqlite" => {
let params = connector::SqliteParams::try_from(url_str)?;
let mut sqlite = connector::Sqlite::new(¶ms.file_path)?;
sqlite.attach_database(¶ms.db_name)?;
Mutex::new(Box::new(sqlite) as Box<dyn Queryable + Send + Sync>)
}
#[cfg(feature = "mysql")]
"mysql" => {
let url = connector::MysqlUrl::new(url)?;
let mysql = connector::Mysql::new(url)?;
Mutex::new(Box::new(mysql) as Box<dyn Queryable + Send + Sync>)
}
#[cfg(feature = "postgresql")]
"postgres" | "postgresql" => {
let url = connector::PostgresUrl::new(url)?;
let psql = connector::PostgreSql::new(url).await?;
Mutex::new(Box::new(psql) as Box<dyn Queryable + Send + Sync>)
}
_ => unimplemented!("Supported url schemes: file or sqlite, mysql, postgres or postgresql."),
};
let connection_info = Arc::new(ConnectionInfo::from_url(url_str)?);
Self::log_start(connection_info.sql_family(), 1);
let inner = Arc::new(inner);
Ok(Self { inner, connection_info })
}
pub fn connection_info(&self) -> &ConnectionInfo {
&self.connection_info
}
fn log_start(family: SqlFamily, connection_limit: u32) {
#[cfg(not(feature = "tracing-log"))]
{
info!("Starting a {} pool with {} connections.", family, connection_limit);
}
#[cfg(feature = "tracing-log")]
{
tracing::info!("Starting a {} pool with {} connections.", family, connection_limit);
}
}
}
impl Queryable for Quaint {
fn execute<'a>(&'a self, q: ast::Query<'a>) -> DBIO<'a, Option<ast::Id>> {
DBIO::new(async move { self.inner.lock().await.execute(q).await })
}
fn query<'a>(&'a self, q: ast::Query<'a>) -> DBIO<'a, connector::ResultSet> {
DBIO::new(async move { self.inner.lock().await.query(q).await })
}
fn query_raw<'a>(&'a self, sql: &'a str, params: &'a [ast::ParameterizedValue]) -> DBIO<'a, connector::ResultSet> {
DBIO::new(async move { self.inner.lock().await.query_raw(sql, params).await })
}
fn execute_raw<'a>(&'a self, sql: &'a str, params: &'a [ast::ParameterizedValue]) -> DBIO<'a, u64> {
DBIO::new(async move { self.inner.lock().await.execute_raw(sql, params).await })
}
fn raw_cmd<'a>(&'a self, cmd: &'a str) -> DBIO<'a, ()> {
DBIO::new(async move { self.inner.lock().await.raw_cmd(cmd).await })
}
}