Connection

Trait Connection 

Source
pub trait Connection: Send {
    type Database: Database;
    type Options: ConnectOptions<Connection = Self>;

    // Required methods
    fn close(self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
    fn ping(
        &mut self,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>;
    fn begin(
        &mut self,
    ) -> Pin<Box<dyn Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_>>
       where Self: Sized;
    fn dbms_name(
        &mut self,
    ) -> Pin<Box<dyn Future<Output = Result<String, Error>> + Send + '_>>;

    // Provided methods
    fn transaction<'a, F, R, E>(
        &'a mut self,
        callback: F,
    ) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'a>>
       where F: for<'c> FnOnce(&'c mut Transaction<'_, Self::Database>) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'c>> + 'a + for<'c> Send + for<'c> 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,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>
       where Self::Database: HasStatementCache { ... }
    fn connect(
        url: &str,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send>>
       where Self: Sized { ... }
    fn connect_with(
        options: &Self::Options,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + '_>>
       where Self: Sized { ... }
}
Expand description

Represents a single database connection.

Required Associated Types§

Source

type Database: Database

Source

type Options: ConnectOptions<Connection = Self>

Required Methods§

Source

fn close(self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>

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.

Source

fn ping( &mut self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>

Checks if a connection to the database is still valid.

Source

fn begin( &mut self, ) -> Pin<Box<dyn Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_>>
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.

Source

fn dbms_name( &mut self, ) -> Pin<Box<dyn Future<Output = Result<String, Error>> + Send + '_>>

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§

Source

fn transaction<'a, F, R, E>( &'a mut self, callback: F, ) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'a>>
where F: for<'c> FnOnce(&'c mut Transaction<'_, Self::Database>) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'c>> + 'a + for<'c> Send + for<'c> Sync, Self: Sized, R: Send, E: From<Error> + Send,

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
Source

fn cached_statements_size(&self) -> usize

The number of statements currently cached in the connection.

Source

fn clear_cached_statements( &mut self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>

Removes all statements from the cache, closing them on the server if needed.

Source

fn connect( url: &str, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send>>
where Self: Sized,

Establish a new database connection.

A value of Options is parsed from the provided connection string. This parsing is database-specific.

Source

fn connect_with( options: &Self::Options, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + '_>>
where Self: Sized,

Establish a new database connection with the provided options.

Implementors§