-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathmod.rs
More file actions
155 lines (129 loc) · 4.23 KB
/
mod.rs
File metadata and controls
155 lines (129 loc) · 4.23 KB
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
use futures_core::future::BoxFuture;
use std::future::Future;
use crate::any::{Any, AnyConnectOptions};
use crate::connection::{ConnectOptions, Connection};
use crate::error::Error;
use crate::config;
use crate::database::Database;
use crate::sql_str::SqlSafeStr;
use crate::transaction::Transaction;
pub use backend::AnyConnectionBackend;
mod backend;
mod executor;
/// A connection to _any_ SQLx database.
///
/// The database driver used is determined by the scheme
/// of the connection url.
///
/// ```text
/// postgres://postgres@localhost/test
/// sqlite://a.sqlite
/// ```
#[derive(Debug)]
pub struct AnyConnection {
pub(crate) backend: Box<dyn AnyConnectionBackend>,
}
impl AnyConnection {
/// Returns the name of the database backend in use (e.g. PostgreSQL, MySQL, SQLite, etc.)
pub fn backend_name(&self) -> &str {
self.backend.name()
}
pub(crate) fn connect(options: &AnyConnectOptions) -> BoxFuture<'_, crate::Result<Self>> {
Box::pin(async {
let driver = crate::any::driver::from_url(&options.database_url)?;
(*driver.connect)(options, None).await
})
}
/// UNSTABLE: for use with `sqlx-cli`
///
/// Connect to the database, and instruct the nested driver to
/// read options from the sqlx.toml file as appropriate.
#[doc(hidden)]
pub async fn connect_with_driver_config(
url: &str,
driver_config: &config::drivers::Config,
) -> Result<Self, Error>
where
Self: Sized,
{
let options: AnyConnectOptions = url.parse()?;
let driver = crate::any::driver::from_url(&options.database_url)?;
(*driver.connect)(&options, Some(driver_config)).await
}
pub(crate) fn connect_with_db<'a, DB: Database>(
options: &'a AnyConnectOptions,
driver_config: Option<&'a config::drivers::Config>,
) -> BoxFuture<'a, crate::Result<Self>>
where
DB::Connection: AnyConnectionBackend,
<DB::Connection as Connection>::Options:
for<'b> TryFrom<&'b AnyConnectOptions, Error = Error>,
{
let res = TryFrom::try_from(options);
Box::pin(async move {
let mut options: <DB::Connection as Connection>::Options = res?;
if let Some(config) = driver_config {
options = options.__unstable_apply_driver_config(config)?;
}
Ok(AnyConnection {
backend: Box::new(options.connect().await?),
})
})
}
#[cfg(feature = "migrate")]
pub(crate) fn get_migrate(
&mut self,
) -> crate::Result<&mut (dyn crate::migrate::Migrate + Send + 'static)> {
self.backend.as_migrate()
}
}
impl Connection for AnyConnection {
type Database = Any;
type Options = AnyConnectOptions;
fn close(self) -> impl Future<Output = Result<(), Error>> + Send + 'static {
self.backend.close()
}
fn close_hard(self) -> impl Future<Output = Result<(), Error>> + Send + 'static {
self.backend.close()
}
fn ping(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_ {
self.backend.ping()
}
fn begin(
&mut self,
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
where
Self: Sized,
{
Transaction::begin(self, None)
}
fn begin_with(
&mut self,
statement: impl SqlSafeStr,
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
where
Self: Sized,
{
Transaction::begin(self, Some(statement.into_sql_str()))
}
fn cached_statements_size(&self) -> usize {
self.backend.cached_statements_size()
}
fn clear_cached_statements(&mut self) -> impl Future<Output = crate::Result<()>> + Send + '_ {
self.backend.clear_cached_statements()
}
fn shrink_buffers(&mut self) {
self.backend.shrink_buffers()
}
fn buffer_stats(&self) -> Option<crate::net::BufferStats> {
self.backend.buffer_stats()
}
#[doc(hidden)]
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_ {
self.backend.flush()
}
#[doc(hidden)]
fn should_flush(&self) -> bool {
self.backend.should_flush()
}
}