Trait diesel::prelude::Connection[][src]

pub trait Connection: SimpleConnection + Sized + Send {
    type Backend: Backend;
    fn establish(database_url: &str) -> ConnectionResult<Self>;

    fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E>
    where
        F: FnOnce(&mut Self) -> Result<T, E>,
        E: From<Error>
, { ... }
fn begin_test_transaction(&mut self) -> QueryResult<()> { ... }
fn test_transaction<T, E, F>(&mut self, f: F) -> T
    where
        F: FnOnce(&mut Self) -> Result<T, E>,
        E: Debug
, { ... } }

A connection to a database

Associated Types

type Backend: Backend[src]

The backend this type connects to

Loading content...

Required methods

fn establish(database_url: &str) -> ConnectionResult<Self>[src]

Establishes a new connection to the database

The argument to this method and the method’s behavior varies by backend. See the documentation for that backend’s connection class for details about what it accepts and how it behaves.

Loading content...

Provided methods

fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E> where
    F: FnOnce(&mut Self) -> Result<T, E>,
    E: From<Error>, 
[src]

Executes the given function inside of a database transaction

If there is already an open transaction, savepoints will be used instead.

If the transaction fails to commit due to a SerializationFailure or a ReadOnlyTransaction a rollback will be attempted. If the rollback succeeds, the original error will be returned, otherwise the error generated by the rollback will be returned. In the second case the connection should be considered broken as it contains a uncommitted unabortable open transaction.

If a nested transaction fails to release the corresponding savepoint a rollback will be attempted. If the rollback succeeds, the original error will be returned, otherwise the error generated by the rollback will be returned.

Example

use diesel::result::Error;

conn.transaction::<_, Error, _>(|conn| {
    diesel::insert_into(users)
        .values(name.eq("Ruby"))
        .execute(conn)?;

    let all_names = users.select(name).load::<String>(conn)?;
    assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names);

    Ok(())
})?;

conn.transaction::<(), _, _>(|conn| {
    diesel::insert_into(users)
        .values(name.eq("Pascal"))
        .execute(conn)?;

    let all_names = users.select(name).load::<String>(conn)?;
    assert_eq!(vec!["Sean", "Tess", "Ruby", "Pascal"], all_names);

    // If we want to roll back the transaction, but don't have an
    // actual error to return, we can return `RollbackTransaction`.
    Err(Error::RollbackTransaction)
});

let all_names = users.select(name).load::<String>(conn)?;
assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names);

fn begin_test_transaction(&mut self) -> QueryResult<()>[src]

Creates a transaction that will never be committed. This is useful for tests. Panics if called while inside of a transaction.

fn test_transaction<T, E, F>(&mut self, f: F) -> T where
    F: FnOnce(&mut Self) -> Result<T, E>,
    E: Debug
[src]

Executes the given function inside a transaction, but does not commit it. Panics if the given function returns an error.

Example

use diesel::result::Error;

conn.test_transaction::<_, Error, _>(|conn| {
    diesel::insert_into(users)
        .values(name.eq("Ruby"))
        .execute(conn)?;

    let all_names = users.select(name).load::<String>(conn)?;
    assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names);

    Ok(())
});

// Even though we returned `Ok`, the transaction wasn't committed.
let all_names = users.select(name).load::<String>(conn)?;
assert_eq!(vec!["Sean", "Tess"], all_names);
Loading content...

Implementors

impl Connection for MysqlConnection[src]

impl Connection for PgConnection[src]

impl Connection for SqliteConnection[src]

type Backend = Sqlite

type TransactionManager = AnsiTransactionManager

fn establish(database_url: &str) -> ConnectionResult<Self>[src]

Establish a connection to the database specified by database_url.

See SqliteConnection for supported database_url.

If the database does not exist, this method will try to create a new database and then establish a connection to it.

impl<M> Connection for PooledConnection<M> where
    M: ManageConnection,
    M::Connection: Connection + R2D2Connection + Send + 'static, 
[src]

type Backend = <M::Connection as Connection>::Backend

type TransactionManager = PoolTransactionManager<<M::Connection as Connection>::TransactionManager>

Loading content...