Trait sqlx_core::connection::Connection
source · [−]pub trait Connection: Send {
type Database: Database;
type Options: ConnectOptions<Connection = Self>;
fn close(self) -> BoxFuture<'static, Result<(), Error>>;
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
fn begin(
&mut self
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
where
Self: Sized;
fn transaction<'a, F, R, E>(
&'a mut self,
callback: F
) -> BoxFuture<'a, Result<R, E>>
where
for<'c> F: FnOnce(&'c mut Transaction<'_, Self::Database>) -> BoxFuture<'c, Result<R, E>> + 'a + Send + Sync,
Self: Sized,
R: Send,
E: From<Error> + Send,
{ ... }
fn cached_statements_size(&self) -> usize
where
Self::Database: HasStatementCache,
{ ... }
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
where
Self::Database: HasStatementCache,
{ ... }
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
where
Self: Sized,
{ ... }
fn connect_with(
options: &Self::Options
) -> BoxFuture<'_, Result<Self, Error>>
where
Self: Sized,
{ ... }
}
Expand description
Represents a single database connection.
Required Associated Types
Required Methods
Explicitly close this database connection.
This method is not required for safe and consistent operation. However, it is
recommended to call it instead of letting a connection drop
as the database backend
will be faster at cleaning up resources.
Checks if a connection to the database is still valid.
Begin a new transaction or establish a savepoint within the active transaction.
Returns a Transaction
for controlling and tracking the new transaction.
Provided Methods
Execute the function inside a transaction.
If the function returns an error, the transaction will be rolled back. If it does not return an error, the transaction will be committed.
Example
use sqlx_core::connection::Connection;
use sqlx_core::error::Error;
use sqlx_core::executor::Executor;
use sqlx_core::postgres::{PgConnection, PgRow};
use sqlx_core::query::query;
conn.transaction(|conn|Box::pin(async move {
query("select * from ..").fetch_all(conn).await
})).await
fn cached_statements_size(&self) -> usize where
Self::Database: HasStatementCache,
fn cached_statements_size(&self) -> usize where
Self::Database: HasStatementCache,
The number of statements currently cached in the connection.
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> where
Self::Database: HasStatementCache,
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> where
Self::Database: HasStatementCache,
Removes all statements from the cache, closing them on the server if needed.
Establish a new database connection.
A value of Options
is parsed from the provided connection string. This parsing
is database-specific.