forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.rs
More file actions
81 lines (68 loc) · 2.56 KB
/
Copy pathconnection.rs
File metadata and controls
81 lines (68 loc) · 2.56 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
//! Provides the [`Connection`] trait to represent a single database connection.
use crate::database::HasStatementCache;
use crate::error::Error;
use crate::execute::Execute;
use crate::{database::Database, options::ConnectOptions};
use futures_core::future::BoxFuture;
// TODO: Connection#transaction()
// TODO: Connection#begin()
/// Represents a single database connection.
pub trait Connection: Send {
type Database: Database;
type Options: ConnectOptions<Connection = Self>;
/// Execute the SQL query.
///
/// Returns a value of [`Done`] which signals successful query completion and provides
/// the number of rows affected; plus, any additional database-specific information (such as
/// the last inserted ID).
fn execute<'x, 'c: 'x, 'q: 'x, E: 'x + Execute<'q, Self::Database>>(
&'c mut self,
query: E,
) -> BoxFuture<'x, Result<u64, Error>>;
/// Explicitly close this database connection.
///
/// This method is **not required** for safe and consistent operation. However, it is
/// recommended to call it instead of letting a connection `drop` as the database backend
/// will be faster at cleaning up resources.
fn close(self) -> BoxFuture<'static, Result<(), Error>>;
/// Checks if a connection to the database is still valid.
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
/// The number of statements currently cached in the connection.
fn cached_statements_size(&self) -> usize
where
Self::Database: HasStatementCache,
{
0
}
/// Removes all statements from the cache, closing them on the server if
/// needed.
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
where
Self::Database: HasStatementCache,
{
Box::pin(async move { Ok(()) })
}
#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
#[doc(hidden)]
fn should_flush(&self) -> bool;
/// Establish a new database connection.
///
/// A value of `Options` is parsed from the provided connection string. This parsing
/// is database-specific.
#[inline]
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
where
Self: Sized,
{
let options = url.parse();
Box::pin(async move { Ok(Self::connect_with(&options?).await?) })
}
/// Establish a new database connection with the provided options.
fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
where
Self: Sized,
{
options.connect()
}
}