Trait diesel::connection::Connection [−][src]
pub trait Connection: SimpleConnection + Sized + Send { type Backend: Backend; fn establish(database_url: &str) -> ConnectionResult<Self>; fn transaction<T, E, F>(&self, f: F) -> Result<T, E>
where
F: FnOnce() -> Result<T, E>,
E: From<Error>, { ... } fn begin_test_transaction(&self) -> QueryResult<()> { ... } fn test_transaction<T, E, F>(&self, f: F) -> T
where
F: FnOnce() -> Result<T, E>,
E: Debug, { ... } }
A connection to a database
Associated Types
Required Methods
fn establish(database_url: &str) -> ConnectionResult<Self>
Establishes a new connection to the database
The argument to this method varies by backend. See the documentation for that backend's connection class for details about what it accepts.
Provided Methods
fn transaction<T, E, F>(&self, f: F) -> Result<T, E> where
F: FnOnce() -> Result<T, E>,
E: From<Error>,
F: FnOnce() -> Result<T, E>,
E: From<Error>,
Executes the given function inside of a database transaction
If there is already an open transaction, savepoints will be used instead.
Example
use diesel::result::Error; conn.transaction::<_, Error, _>(|| { 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::<(), _, _>(|| { 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(&self) -> QueryResult<()>
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>,
E: Debug,
F: FnOnce() -> Result<T, E>,
E: Debug,
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, _>(|| { 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);
Implementations on Foreign Types
impl<C> Connection for PooledConnection<ConnectionManager<C>> where
C: Connection<TransactionManager = AnsiTransactionManager> + Send + 'static,
C::Backend: UsesAnsiSavepointSyntax,
[src]
impl<C> Connection for PooledConnection<ConnectionManager<C>> where
C: Connection<TransactionManager = AnsiTransactionManager> + Send + 'static,
C::Backend: UsesAnsiSavepointSyntax,
type Backend = C::Backend
type TransactionManager = C::TransactionManager
fn establish(_: &str) -> ConnectionResult<Self>
[src]
fn establish(_: &str) -> ConnectionResult<Self>
fn execute(&self, query: &str) -> QueryResult<usize>
[src]
fn execute(&self, query: &str) -> QueryResult<usize>
fn query_by_index<T, U>(&self, source: T) -> QueryResult<Vec<U>> where
T: AsQuery,
T::Query: QueryFragment<Self::Backend> + QueryId,
Self::Backend: HasSqlType<T::SqlType>,
U: Queryable<T::SqlType, Self::Backend>,
[src]
fn query_by_index<T, U>(&self, source: T) -> QueryResult<Vec<U>> where
T: AsQuery,
T::Query: QueryFragment<Self::Backend> + QueryId,
Self::Backend: HasSqlType<T::SqlType>,
U: Queryable<T::SqlType, Self::Backend>,
fn query_by_name<T, U>(&self, source: &T) -> QueryResult<Vec<U>> where
T: QueryFragment<Self::Backend> + QueryId,
U: QueryableByName<Self::Backend>,
[src]
fn query_by_name<T, U>(&self, source: &T) -> QueryResult<Vec<U>> where
T: QueryFragment<Self::Backend> + QueryId,
U: QueryableByName<Self::Backend>,
fn execute_returning_count<T>(&self, source: &T) -> QueryResult<usize> where
T: QueryFragment<Self::Backend> + QueryId,
[src]
fn execute_returning_count<T>(&self, source: &T) -> QueryResult<usize> where
T: QueryFragment<Self::Backend> + QueryId,
fn transaction_manager(&self) -> &Self::TransactionManager
[src]
fn transaction_manager(&self) -> &Self::TransactionManager
fn transaction<T, E, F>(&self, f: F) -> Result<T, E> where
F: FnOnce() -> Result<T, E>,
E: From<Error>,
[src]
fn transaction<T, E, F>(&self, f: F) -> Result<T, E> where
F: FnOnce() -> Result<T, E>,
E: From<Error>,
fn begin_test_transaction(&self) -> QueryResult<()>
[src]
fn begin_test_transaction(&self) -> QueryResult<()>
fn test_transaction<T, E, F>(&self, f: F) -> T where
F: FnOnce() -> Result<T, E>,
E: Debug,
[src]
fn test_transaction<T, E, F>(&self, f: F) -> T where
F: FnOnce() -> Result<T, E>,
E: Debug,
Implementors
impl Connection for MysqlConnection type Backend = Mysql; type TransactionManager = AnsiTransactionManager;
impl Connection for PgConnection type Backend = Pg; type TransactionManager = AnsiTransactionManager;
impl Connection for SqliteConnection type Backend = Sqlite; type TransactionManager = AnsiTransactionManager;