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    /// Returns the name of the Database Management System (DBMS) this connection
106    /// is talking to.
107    ///
108    /// This is intended to be compatible with the ODBC `SQL_DBMS_NAME` info value
109    /// and should generally return the same string as an ODBC driver for the same
110    /// database. This makes it easier to write conditional SQL or feature probes
111    /// that work across both native and ODBC connections.
112    ///
113    /// Typical return values include:
114    /// - "PostgreSQL"
115    /// - "MySQL"
116    /// - "SQLite"
117    /// - "Microsoft SQL Server" (includes Azure SQL)
118    /// - ODBC: the exact string reported by the driver via `SQL_DBMS_NAME`
119    ///
120    /// Implementations for built-in drivers return a well-known constant string,
121    /// while the ODBC driver queries the underlying driver at runtime.
122    fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>>;
123
124    #[doc(hidden)]
125    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
126
127    #[doc(hidden)]
128    fn should_flush(&self) -> bool;
129
130    /// Establish a new database connection.
131    ///
132    /// A value of [`Options`][Self::Options] is parsed from the provided connection string. This parsing
133    /// is database-specific.
134    #[inline]
135    fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
136    where
137        Self: Sized,
138    {
139        let options = url.parse();
140
141        Box::pin(async move { Self::connect_with(&options?).await })
142    }
143
144    /// Establish a new database connection with the provided options.
145    fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
146    where
147        Self: Sized,
148    {
149        options.connect()
150    }
151}
152
153#[derive(Clone, Debug)]
154pub(crate) struct LogSettings {
155    pub(crate) statements_level: LevelFilter,
156    pub(crate) slow_statements_level: LevelFilter,
157    pub(crate) slow_statements_duration: Duration,
158}
159
160impl Default for LogSettings {
161    fn default() -> Self {
162        LogSettings {
163            statements_level: LevelFilter::Info,
164            slow_statements_level: LevelFilter::Warn,
165            slow_statements_duration: Duration::from_secs(1),
166        }
167    }
168}
169
170impl LogSettings {
171    pub(crate) fn log_statements(&mut self, level: LevelFilter) {
172        self.statements_level = level;
173    }
174    pub(crate) fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) {
175        self.slow_statements_level = level;
176        self.slow_statements_duration = duration;
177    }
178}
179
180pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug + Clone {
181    type Connection: Connection + ?Sized;
182
183    /// Establish a new database connection with the options specified by `self`.
184    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
185    where
186        Self::Connection: Sized;
187
188    /// Log executed statements with the specified `level`
189    fn log_statements(&mut self, level: LevelFilter) -> &mut Self;
190
191    /// Log executed statements with a duration above the specified `duration`
192    /// at the specified `level`.
193    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self;
194
195    /// Entirely disables statement logging (both slow and regular).
196    fn disable_statement_logging(&mut self) -> &mut Self {
197        self.log_statements(LevelFilter::Off)
198            .log_slow_statements(LevelFilter::Off, Duration::default())
199    }
200}