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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::time::Duration;
mod connection;
mod db_connection;
#[cfg(feature = "mock")]
mod mock;
mod statement;
mod stream;
mod transaction;
pub use connection::*;
pub use db_connection::*;
#[cfg(feature = "mock")]
pub use mock::*;
pub use statement::*;
pub use stream::*;
use tracing::instrument;
pub use transaction::*;
use crate::DbErr;
#[derive(Debug, Default)]
pub struct Database;
#[derive(Debug)]
pub struct ConnectOptions {
pub(crate) url: String,
pub(crate) max_connections: Option<u32>,
pub(crate) min_connections: Option<u32>,
pub(crate) connect_timeout: Option<Duration>,
pub(crate) idle_timeout: Option<Duration>,
pub(crate) max_lifetime: Option<Duration>,
pub(crate) sqlx_logging: bool,
}
impl Database {
#[instrument(level = "trace", skip(opt))]
pub async fn connect<C>(opt: C) -> Result<DatabaseConnection, DbErr>
where
C: Into<ConnectOptions>,
{
let opt: ConnectOptions = opt.into();
#[cfg(feature = "sqlx-mysql")]
if DbBackend::MySql.is_prefix_of(&opt.url) {
return crate::SqlxMySqlConnector::connect(opt).await;
}
#[cfg(feature = "sqlx-postgres")]
if DbBackend::Postgres.is_prefix_of(&opt.url) {
return crate::SqlxPostgresConnector::connect(opt).await;
}
#[cfg(feature = "sqlx-sqlite")]
if DbBackend::Sqlite.is_prefix_of(&opt.url) {
return crate::SqlxSqliteConnector::connect(opt).await;
}
#[cfg(feature = "mock")]
if crate::MockDatabaseConnector::accepts(&opt.url) {
return crate::MockDatabaseConnector::connect(&opt.url).await;
}
Err(DbErr::Conn(format!(
"The connection string '{}' has no supporting driver.",
opt.url
)))
}
}
impl From<&str> for ConnectOptions {
fn from(string: &str) -> ConnectOptions {
ConnectOptions::from_str(string)
}
}
impl From<&String> for ConnectOptions {
fn from(string: &String) -> ConnectOptions {
ConnectOptions::from_str(string.as_str())
}
}
impl From<String> for ConnectOptions {
fn from(string: String) -> ConnectOptions {
ConnectOptions::new(string)
}
}
impl ConnectOptions {
pub fn new(url: String) -> Self {
Self {
url,
max_connections: None,
min_connections: None,
connect_timeout: None,
idle_timeout: None,
max_lifetime: None,
sqlx_logging: true,
}
}
fn from_str(url: &str) -> Self {
Self::new(url.to_owned())
}
#[cfg(feature = "sqlx-dep")]
pub fn pool_options<DB>(self) -> sqlx::pool::PoolOptions<DB>
where
DB: sqlx::Database,
{
let mut opt = sqlx::pool::PoolOptions::new();
if let Some(max_connections) = self.max_connections {
opt = opt.max_connections(max_connections);
}
if let Some(min_connections) = self.min_connections {
opt = opt.min_connections(min_connections);
}
if let Some(connect_timeout) = self.connect_timeout {
opt = opt.connect_timeout(connect_timeout);
}
if let Some(idle_timeout) = self.idle_timeout {
opt = opt.idle_timeout(Some(idle_timeout));
}
if let Some(max_lifetime) = self.max_lifetime {
opt = opt.max_lifetime(Some(max_lifetime));
}
opt
}
pub fn get_url(&self) -> &str {
&self.url
}
pub fn max_connections(&mut self, value: u32) -> &mut Self {
self.max_connections = Some(value);
self
}
pub fn get_max_connections(&self) -> Option<u32> {
self.max_connections
}
pub fn min_connections(&mut self, value: u32) -> &mut Self {
self.min_connections = Some(value);
self
}
pub fn get_min_connections(&self) -> Option<u32> {
self.min_connections
}
pub fn connect_timeout(&mut self, value: Duration) -> &mut Self {
self.connect_timeout = Some(value);
self
}
pub fn get_connect_timeout(&self) -> Option<Duration> {
self.connect_timeout
}
pub fn idle_timeout(&mut self, value: Duration) -> &mut Self {
self.idle_timeout = Some(value);
self
}
pub fn get_idle_timeout(&self) -> Option<Duration> {
self.idle_timeout
}
pub fn max_lifetime(&mut self, lifetime: Duration) -> &mut Self {
self.max_lifetime = Some(lifetime);
self
}
pub fn get_max_lifetime(&self) -> Option<Duration> {
self.max_lifetime
}
pub fn sqlx_logging(&mut self, value: bool) -> &mut Self {
self.sqlx_logging = value;
self
}
pub fn get_sqlx_logging(&self) -> bool {
self.sqlx_logging
}
}