| 1 | use crate::any::{Any, AnyArguments, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo}; |
| 2 | use crate::describe::Describe; |
| 3 | use either::Either; |
| 4 | use futures_core::future::BoxFuture; |
| 5 | use futures_core::stream::BoxStream; |
| 6 | use std::fmt::Debug; |
| 7 | |
| 8 | pub 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 | fn begin(&mut self) -> BoxFuture<'_, crate::Result<()>>; |
| 30 | |
| 31 | fn commit(&mut self) -> BoxFuture<'_, crate::Result<()>>; |
| 32 | |
| 33 | fn rollback(&mut self) -> BoxFuture<'_, crate::Result<()>>; |
| 34 | |
| 35 | fn start_rollback(&mut self); |
| 36 | |
| 37 | /// The number of statements currently cached in the connection. |
| 38 | fn cached_statements_size(&self) -> usize { |
| 39 | 0 |
| 40 | } |
| 41 | |
| 42 | /// Removes all statements from the cache, closing them on the server if |
| 43 | /// needed. |
| 44 | fn clear_cached_statements(&mut self) -> BoxFuture<'_, crate::Result<()>> { |
| 45 | Box::pin(async move { Ok(()) }) |
| 46 | } |
| 47 | |
| 48 | /// Forward to [`Connection::shrink_buffers()`]. |
| 49 | /// |
| 50 | /// [`Connection::shrink_buffers()`]: method@crate::connection::Connection::shrink_buffers |
| 51 | fn shrink_buffers(&mut self); |
| 52 | |
| 53 | #[doc(hidden)] |
| 54 | fn flush(&mut self) -> BoxFuture<'_, crate::Result<()>>; |
| 55 | |
| 56 | #[doc(hidden)] |
| 57 | fn should_flush(&self) -> bool; |
| 58 | |
| 59 | #[cfg(feature = "migrate")] |
| 60 | fn as_migrate(&mut self) -> crate::Result<&mut (dyn crate::migrate::Migrate + Send + 'static)> { |
| 61 | Err(crate::Error::Configuration( |
| 62 | format!( |
| 63 | "{} driver does not support migrations or `migrate` feature was not enabled", |
| 64 | self.name() |
| 65 | ) |
| 66 | .into(), |
| 67 | )) |
| 68 | } |
| 69 | |
| 70 | fn fetch_many<'q>( |
| 71 | &'q mut self, |
| 72 | query: &'q str, |
| 73 | persistent: bool, |
| 74 | arguments: Option<AnyArguments<'q>>, |
| 75 | ) -> BoxStream<'q, crate::Result<Either<AnyQueryResult, AnyRow>>>; |
| 76 | |
| 77 | fn fetch_optional<'q>( |
| 78 | &'q mut self, |
| 79 | query: &'q str, |
| 80 | persistent: bool, |
| 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 | } |