pub trait Connection: Send {
type Database: Database;
type Options: ConnectOptions<Connection = Self>;
// Required methods
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 dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>>;
// Provided methods
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§
Sourcefn close(self) -> BoxFuture<'static, Result<(), Error>>
fn close(self) -> BoxFuture<'static, Result<(), Error>>
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.
Sourcefn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>
Checks if a connection to the database is still valid.
Sourcefn begin(
&mut self,
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>where
Self: Sized,
fn begin(
&mut self,
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>where
Self: Sized,
Begin a new transaction or establish a savepoint within the active transaction.
Returns a Transaction
for controlling and tracking the new transaction.
Sourcefn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>>
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>>
Returns the name of the Database Management System (DBMS) this connection is talking to.
This is intended to be compatible with the ODBC SQL_DBMS_NAME
info value
and should generally return the same string as an ODBC driver for the same
database. This makes it easier to write conditional SQL or feature probes
that work across both native and ODBC connections.
Typical return values include:
- “PostgreSQL”
- “MySQL”
- “SQLite”
- “Microsoft SQL Server” (includes Azure SQL)
- ODBC: the exact string reported by the driver via
SQL_DBMS_NAME
Implementations for built-in drivers return a well-known constant string, while the ODBC driver queries the underlying driver at runtime.
Provided Methods§
Sourcefn transaction<'a, F, R, E>(
&'a mut self,
callback: F,
) -> BoxFuture<'a, Result<R, E>>
fn transaction<'a, F, R, E>( &'a mut self, callback: F, ) -> BoxFuture<'a, Result<R, E>>
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_oldapi::connection::Connection;
use sqlx_core_oldapi::error::Error;
use sqlx_core_oldapi::executor::Executor;
use sqlx_core_oldapi::postgres::{PgConnection, PgRow};
use sqlx_core_oldapi::query::query;
conn.transaction(|conn|Box::pin(async move {
query("select * from ..").fetch_all(conn).await
})).await
Sourcefn cached_statements_size(&self) -> usizewhere
Self::Database: HasStatementCache,
fn cached_statements_size(&self) -> usizewhere
Self::Database: HasStatementCache,
The number of statements currently cached in the connection.
Sourcefn 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.
Implementors§
Source§impl Connection for AnyConnection
Available on (crate features postgres
or mysql
or mssql
or sqlite
or odbc
) and crate feature any
only.
impl Connection for AnyConnection
postgres
or mysql
or mssql
or sqlite
or odbc
) and crate feature any
only.Source§impl Connection for MssqlConnection
Available on crate feature mssql
only.
impl Connection for MssqlConnection
mssql
only.Source§impl Connection for MySqlConnection
Available on crate feature mysql
only.
impl Connection for MySqlConnection
mysql
only.Source§impl Connection for OdbcConnection
Available on crate feature odbc
only.
impl Connection for OdbcConnection
odbc
only.Source§impl Connection for PgConnection
Available on crate feature postgres
only.
impl Connection for PgConnection
postgres
only.Source§impl Connection for SqliteConnection
Available on crate feature sqlite
only.
impl Connection for SqliteConnection
sqlite
only.