sqlx_core/any/connection/
backend.rs

1use crate::any::{Any, AnyArguments, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo};
2use crate::describe::Describe;
3use either::Either;
4use futures_core::future::BoxFuture;
5use futures_core::stream::BoxStream;
6use std::borrow::Cow;
7use std::fmt::Debug;
8
9pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
10    /// The backend name.
11    fn name(&self) -> &str;
12
13    /// Explicitly close this database connection.
14    ///
15    /// This method is **not required** for safe and consistent operation. However, it is
16    /// recommended to call it instead of letting a connection `drop` as the database backend
17    /// will be faster at cleaning up resources.
18    fn close(self: Box<Self>) -> BoxFuture<'static, crate::Result<()>>;
19
20    /// Immediately close the connection without sending a graceful shutdown.
21    ///
22    /// This should still at least send a TCP `FIN` frame to let the server know we're dying.
23    #[doc(hidden)]
24    fn close_hard(self: Box<Self>) -> BoxFuture<'static, crate::Result<()>>;
25
26    /// Checks if a connection to the database is still valid.
27    fn ping(&mut self) -> BoxFuture<'_, crate::Result<()>>;
28
29    /// Begin a new transaction or establish a savepoint within the active transaction.
30    ///
31    /// If this is a new transaction, `statement` may be used instead of the
32    /// default "BEGIN" statement.
33    ///
34    /// If we are already inside a transaction and `statement.is_some()`, then
35    /// `Error::InvalidSavePoint` is returned without running any statements.
36    fn begin(&mut self, statement: Option<Cow<'static, str>>) -> BoxFuture<'_, crate::Result<()>>;
37
38    fn commit(&mut self) -> BoxFuture<'_, crate::Result<()>>;
39
40    fn rollback(&mut self) -> BoxFuture<'_, crate::Result<()>>;
41
42    fn start_rollback(&mut self);
43
44    /// Returns the current transaction depth.
45    ///
46    /// Transaction depth indicates the level of nested transactions:
47    /// - Level 0: No active transaction.
48    /// - Level 1: A transaction is active.
49    /// - Level 2 or higher: A transaction is active and one or more SAVEPOINTs have been created within it.
50    fn get_transaction_depth(&self) -> usize {
51        unimplemented!("get_transaction_depth() is not implemented for this backend. This is a provided method to avoid a breaking change, but it will become a required method in version 0.9 and later.");
52    }
53
54    /// Checks if the connection is currently in a transaction.
55    ///
56    /// This method returns `true` if the current transaction depth is greater than 0,
57    /// indicating that a transaction is active. It returns `false` if the transaction depth is 0,
58    /// meaning no transaction is active.
59    #[inline]
60    fn is_in_transaction(&self) -> bool {
61        self.get_transaction_depth() != 0
62    }
63
64    /// The number of statements currently cached in the connection.
65    fn cached_statements_size(&self) -> usize {
66        0
67    }
68
69    /// Removes all statements from the cache, closing them on the server if
70    /// needed.
71    fn clear_cached_statements(&mut self) -> BoxFuture<'_, crate::Result<()>> {
72        Box::pin(async move { Ok(()) })
73    }
74
75    /// Forward to [`Connection::shrink_buffers()`].
76    ///
77    /// [`Connection::shrink_buffers()`]: method@crate::connection::Connection::shrink_buffers
78    fn shrink_buffers(&mut self);
79
80    #[doc(hidden)]
81    fn flush(&mut self) -> BoxFuture<'_, crate::Result<()>>;
82
83    #[doc(hidden)]
84    fn should_flush(&self) -> bool;
85
86    #[cfg(feature = "migrate")]
87    fn as_migrate(&mut self) -> crate::Result<&mut (dyn crate::migrate::Migrate + Send + 'static)> {
88        Err(crate::Error::Configuration(
89            format!(
90                "{} driver does not support migrations or `migrate` feature was not enabled",
91                self.name()
92            )
93            .into(),
94        ))
95    }
96
97    fn fetch_many<'q>(
98        &'q mut self,
99        query: &'q str,
100        persistent: bool,
101        arguments: Option<AnyArguments<'q>>,
102    ) -> BoxStream<'q, crate::Result<Either<AnyQueryResult, AnyRow>>>;
103
104    fn fetch_optional<'q>(
105        &'q mut self,
106        query: &'q str,
107        persistent: bool,
108        arguments: Option<AnyArguments<'q>>,
109    ) -> BoxFuture<'q, crate::Result<Option<AnyRow>>>;
110
111    fn prepare_with<'c, 'q: 'c>(
112        &'c mut self,
113        sql: &'q str,
114        parameters: &[AnyTypeInfo],
115    ) -> BoxFuture<'c, crate::Result<AnyStatement<'q>>>;
116
117    fn describe<'q>(&'q mut self, sql: &'q str) -> BoxFuture<'q, crate::Result<Describe<Any>>>;
118}