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::fmt::Debug;
7
8pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
9    /// The backend name.
10    fn name(&self) -> &str;
11
12    /// Explicitly close this database connection.
13    ///
14    /// This method is **not required** for safe and consistent operation. However, it is
15    /// recommended to call it instead of letting a connection `drop` as the database backend
16    /// will be faster at cleaning up resources.
17    fn close(self: Box<Self>) -> BoxFuture<'static, crate::Result<()>>;
18
19    /// Immediately close the connection without sending a graceful shutdown.
20    ///
21    /// This should still at least send a TCP `FIN` frame to let the server know we're dying.
22    #[doc(hidden)]
23    fn close_hard(self: Box<Self>) -> BoxFuture<'static, crate::Result<()>>;
24
25    /// Checks if a connection to the database is still valid.
26    fn ping(&mut self) -> BoxFuture<'_, crate::Result<()>>;
27
28    /// Begin a new transaction or establish a savepoint within the active transaction.
29    ///
30    /// Returns a [`Transaction`] for controlling and tracking the new transaction.
31    fn begin(&mut self) -> BoxFuture<'_, crate::Result<()>>;
32
33    fn commit(&mut self) -> BoxFuture<'_, crate::Result<()>>;
34
35    fn rollback(&mut self) -> BoxFuture<'_, crate::Result<()>>;
36
37    fn start_rollback(&mut self);
38
39    /// The number of statements currently cached in the connection.
40    fn cached_statements_size(&self) -> usize {
41        0
42    }
43
44    /// Removes all statements from the cache, closing them on the server if
45    /// needed.
46    fn clear_cached_statements(&mut self) -> BoxFuture<'_, crate::Result<()>> {
47        Box::pin(async move { Ok(()) })
48    }
49
50    /// Forward to [`Connection::shrink_buffers()`].
51    ///
52    /// [`Connection::shrink_buffers()`]: method@crate::connection::Connection::shrink_buffers
53    fn shrink_buffers(&mut self);
54
55    #[doc(hidden)]
56    fn flush(&mut self) -> BoxFuture<'_, crate::Result<()>>;
57
58    #[doc(hidden)]
59    fn should_flush(&self) -> bool;
60
61    #[cfg(feature = "migrate")]
62    fn as_migrate(&mut self) -> crate::Result<&mut (dyn crate::migrate::Migrate + Send + 'static)> {
63        Err(crate::Error::Configuration(
64            format!(
65                "{} driver does not support migrations or `migrate` feature was not enabled",
66                self.name()
67            )
68            .into(),
69        ))
70    }
71
72    fn fetch_many<'q>(
73        &'q mut self,
74        query: &'q str,
75        arguments: Option<AnyArguments<'q>>,
76    ) -> BoxStream<'q, crate::Result<Either<AnyQueryResult, AnyRow>>>;
77
78    fn fetch_optional<'q>(
79        &'q mut self,
80        query: &'q str,
81        arguments: Option<AnyArguments<'q>>,
82    ) -> BoxFuture<'q, crate::Result<Option<AnyRow>>>;
83
84    fn prepare_with<'c, 'q: 'c>(
85        &'c mut self,
86        sql: &'q str,
87        parameters: &[AnyTypeInfo],
88    ) -> BoxFuture<'c, crate::Result<AnyStatement<'q>>>;
89
90    fn describe<'q>(&'q mut self, sql: &'q str) -> BoxFuture<'q, crate::Result<Describe<Any>>>;
91}