Struct diesel::Connection [] [src]

pub struct Connection {
    // some fields omitted
}

Methods

impl Connection
[src]

fn establish(database_url: &str) -> ConnectionResult<Connection>

Establishes a new connection to the database at the given URL. The URL should be a PostgreSQL connection string, as documented at http://www.postgresql.org/docs/9.4/static/libpq-connect.html#LIBPQ-CONNSTRING

fn transaction<T, E, F>(&self, f: F) -> TransactionResult<T, E> where F: FnOnce() -> Result<T, E>

Executes the given function inside of a database transaction. When a transaction is already occurring, savepoints will be used to emulate a nested transaction.

If the function returns an Ok, that value will be returned. If the function returns an Err, TransactionError::UserReturnedError will be returned wrapping that value.

fn begin_test_transaction(&self) -> QueryResult<usize>

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>(&self, f: F) -> T where F: FnOnce() -> Result<T, E>

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

fn query_one<T, U>(&self, source: T) -> QueryResult<Option<U>> where T: AsQuery, U: Queriable<T::SqlType>

Executes the given query, returning a single value. Identical to source.first(&connection). See the documentation for first for more.

fn query_all<T, U>(&self, source: T) -> QueryResult<Cursor<T::SqlType, U>> where T: AsQuery, U: Queriable<T::SqlType>

Executes the given query, returning an Iterator over the returned rows. Identical to source.load(&connection). See the documentation for load for more.

fn find<T, U, PK>(&self, source: T, id: PK) -> QueryResult<Option<U>> where T: Table + FilterDsl<FindPredicate<T, PK>>, FindBy<T, T::PrimaryKey, PK>: LimitDsl, U: Queriable<Limit<FindBy<T, T::PrimaryKey, PK>>::SqlType>, PK: AsExpression<PkType<T>>, AsExpr<PK, T::PrimaryKey>: NonAggregate

Attempts to find a single record from the given table by primary key.

Example

let sean = (1, "Sean".to_string());
let tess = (2, "Tess".to_string());
assert_eq!(Some(sean), connection.find(users, 1).unwrap());
assert_eq!(Some(tess), connection.find(users, 2).unwrap());
assert_eq!(None::<(i32, String)>, connection.find(users, 3).unwrap());

fn insert<T, U, Out>(&self, _source: &T, records: U) -> QueryResult<Cursor<T::AllColumns::SqlType, Out>> where T: Table, U: Insertable<T>, Out: Queriable<T::AllColumns::SqlType>

Inserts new records in the database, and returns an Iterator of models created from the inserted rows. If you do not want to use the returned models, you should call insert_returning_count instead.

fn insert_returning_count<T, U>(&self, _source: &T, records: U) -> QueryResult<usize> where T: Table, U: Insertable<T>

Inserts new records into the database, returning the number of rows that were saved.

fn execute_returning_count<T>(&self, source: &T) -> QueryResult<usize> where T: QueryFragment

Executes the given command, returning the number of rows affected. Used in conjunction with update and delete

Trait Implementations

impl Drop for Connection
[src]

fn drop(&mut self)

A method called when the value goes out of scope. Read more