sqlx_core_oldapi/
connection.rs

1use crate::database::{Database, HasStatementCache};
2use crate::error::Error;
3use crate::transaction::Transaction;
4use futures_core::future::BoxFuture;
5use log::LevelFilter;
6use std::fmt::Debug;
7use std::str::FromStr;
8use std::time::Duration;
9
10/// Represents a single database connection.
11pub trait Connection: Send {
12    type Database: Database;
13
14    type Options: ConnectOptions<Connection = Self>;
15
16    /// Explicitly close this database connection.
17    ///
18    /// This method is **not required** for safe and consistent operation. However, it is
19    /// recommended to call it instead of letting a connection `drop` as the database backend
20    /// will be faster at cleaning up resources.
21    fn close(self) -> BoxFuture<'static, Result<(), Error>>;
22
23    /// Immediately close the connection without sending a graceful shutdown.
24    ///
25    /// This should still at least send a TCP `FIN` frame to let the server know we're dying.
26    #[doc(hidden)]
27    fn close_hard(self) -> BoxFuture<'static, Result<(), Error>>;
28
29    /// Checks if a connection to the database is still valid.
30    fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
31
32    /// Begin a new transaction or establish a savepoint within the active transaction.
33    ///
34    /// Returns a [`Transaction`] for controlling and tracking the new transaction.
35    fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
36    where
37        Self: Sized;
38
39    /// Execute the function inside a transaction.
40    ///
41    /// If the function returns an error, the transaction will be rolled back. If it does not
42    /// return an error, the transaction will be committed.
43    ///
44    /// # Example
45    ///
46    /// ```rust
47    /// use sqlx_core_oldapi::connection::Connection;
48    /// use sqlx_core_oldapi::error::Error;
49    /// use sqlx_core_oldapi::executor::Executor;
50    /// use sqlx_core_oldapi::postgres::{PgConnection, PgRow};
51    /// use sqlx_core_oldapi::query::query;
52    ///
53    /// # pub async fn _f(conn: &mut PgConnection) -> Result<Vec<PgRow>, Error> {
54    /// conn.transaction(|conn|Box::pin(async move {
55    ///     query("select * from ..").fetch_all(conn).await
56    /// })).await
57    /// # }
58    /// ```
59    fn transaction<'a, F, R, E>(&'a mut self, callback: F) -> BoxFuture<'a, Result<R, E>>
60    where
61        for<'c> F: FnOnce(&'c mut Transaction<'_, Self::Database>) -> BoxFuture<'c, Result<R, E>>
62            + 'a
63            + Send
64            + Sync,
65        Self: Sized,
66        R: Send,
67        E: From<Error> + Send,
68    {
69        Box::pin(async move {
70            let mut transaction = self.begin().await?;
71            let ret = callback(&mut transaction).await;
72
73            match ret {
74                Ok(ret) => {
75                    transaction.commit().await?;
76
77                    Ok(ret)
78                }
79                Err(err) => {
80                    transaction.rollback().await?;
81
82                    Err(err)
83                }
84            }
85        })
86    }
87
88    /// The number of statements currently cached in the connection.
89    fn cached_statements_size(&self) -> usize
90    where
91        Self::Database: HasStatementCache,
92    {
93        0
94    }
95
96    /// Removes all statements from the cache, closing them on the server if
97    /// needed.
98    fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
99    where
100        Self::Database: HasStatementCache,
101    {
102        Box::pin(async move { Ok(()) })
103    }
104
105    #[doc(hidden)]
106    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
107
108    #[doc(hidden)]
109    fn should_flush(&self) -> bool;
110
111    /// Establish a new database connection.
112    ///
113    /// A value of [`Options`][Self::Options] is parsed from the provided connection string. This parsing
114    /// is database-specific.
115    #[inline]
116    fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
117    where
118        Self: Sized,
119    {
120        let options = url.parse();
121
122        Box::pin(async move { Ok(Self::connect_with(&options?).await?) })
123    }
124
125    /// Establish a new database connection with the provided options.
126    fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
127    where
128        Self: Sized,
129    {
130        options.connect()
131    }
132}
133
134#[derive(Clone, Debug)]
135pub(crate) struct LogSettings {
136    pub(crate) statements_level: LevelFilter,
137    pub(crate) slow_statements_level: LevelFilter,
138    pub(crate) slow_statements_duration: Duration,
139}
140
141impl Default for LogSettings {
142    fn default() -> Self {
143        LogSettings {
144            statements_level: LevelFilter::Info,
145            slow_statements_level: LevelFilter::Warn,
146            slow_statements_duration: Duration::from_secs(1),
147        }
148    }
149}
150
151impl LogSettings {
152    pub(crate) fn log_statements(&mut self, level: LevelFilter) {
153        self.statements_level = level;
154    }
155    pub(crate) fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) {
156        self.slow_statements_level = level;
157        self.slow_statements_duration = duration;
158    }
159}
160
161pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug + Clone {
162    type Connection: Connection + ?Sized;
163
164    /// Establish a new database connection with the options specified by `self`.
165    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
166    where
167        Self::Connection: Sized;
168
169    /// Log executed statements with the specified `level`
170    fn log_statements(&mut self, level: LevelFilter) -> &mut Self;
171
172    /// Log executed statements with a duration above the specified `duration`
173    /// at the specified `level`.
174    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self;
175
176    /// Entirely disables statement logging (both slow and regular).
177    fn disable_statement_logging(&mut self) -> &mut Self {
178        self.log_statements(LevelFilter::Off)
179            .log_slow_statements(LevelFilter::Off, Duration::default())
180    }
181}