Struct diesel::prelude::SqliteConnection
source · pub struct SqliteConnection { /* private fields */ }
sqlite
only.Expand description
Connections for the SQLite backend. Unlike other backends, SQLite supported connection URLs are:
- File paths (
test.db
) - URIs (
file://test.db
) - Special identifiers (
:memory:
)
Supported loading model implementations
As SqliteConnection
only supports a single loading mode implementation
it is not required to explicitly specify a loading mode
when calling RunQueryDsl::load_iter()
or LoadConnection::load
DefaultLoadingMode
SqliteConnection
only supports a single loading mode, which loads
values row by row from the result set.
use diesel::connection::DefaultLoadingMode;
{ // scope to restrict the lifetime of the iterator
let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
for r in iter1 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}
}
// works without specifying the loading mode
let iter2 = users::table.load_iter::<(i32, String), _>(connection)?;
for r in iter2 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}
This mode does not support creating multiple iterators using the same connection.
use diesel::connection::DefaultLoadingMode;
let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
let iter2 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
for r in iter1 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}
for r in iter2 {
let (id, name) = r?;
println!("Id: {} Name: {}", id, name);
}
Implementations§
source§impl SqliteConnection
impl SqliteConnection
sourcepub fn immediate_transaction<T, E, F>(&mut self, f: F) -> Result<T, E>where
F: FnOnce(&mut Self) -> Result<T, E>,
E: From<Error>,
pub fn immediate_transaction<T, E, F>(&mut self, f: F) -> Result<T, E>where F: FnOnce(&mut Self) -> Result<T, E>, E: From<Error>,
Run a transaction with BEGIN IMMEDIATE
This method will return an error if a transaction is already open.
Example
conn.immediate_transaction(|conn| {
// Do stuff in a transaction
Ok(())
})
sourcepub fn exclusive_transaction<T, E, F>(&mut self, f: F) -> Result<T, E>where
F: FnOnce(&mut Self) -> Result<T, E>,
E: From<Error>,
pub fn exclusive_transaction<T, E, F>(&mut self, f: F) -> Result<T, E>where F: FnOnce(&mut Self) -> Result<T, E>, E: From<Error>,
Run a transaction with BEGIN EXCLUSIVE
This method will return an error if a transaction is already open.
Example
conn.exclusive_transaction(|conn| {
// Do stuff in a transaction
Ok(())
})
sourcepub fn register_collation<F>(
&mut self,
collation_name: &str,
collation: F
) -> QueryResult<()>where
F: Fn(&str, &str) -> Ordering + Send + 'static + UnwindSafe,
pub fn register_collation<F>( &mut self, collation_name: &str, collation: F ) -> QueryResult<()>where F: Fn(&str, &str) -> Ordering + Send + 'static + UnwindSafe,
Register a collation function.
collation
must always return the same answer given the same inputs.
If collation
panics and unwinds the stack, the process is aborted, since it is used
across a C FFI boundary, which cannot be unwound across and there is no way to
signal failures via the SQLite interface in this case..
If the name is already registered it will be overwritten.
This method will return an error if registering the function fails, either due to an out-of-memory situation or because a collation with that name already exists and is currently being used in parallel by a query.
The collation needs to be specified when creating a table:
CREATE TABLE my_table ( str TEXT COLLATE MY_COLLATION )
,
where MY_COLLATION
corresponds to name passed as collation_name
.
Example
// sqlite NOCASE only works for ASCII characters,
// this collation allows handling UTF-8 (barring locale differences)
conn.register_collation("RUSTNOCASE", |rhs, lhs| {
rhs.to_lowercase().cmp(&lhs.to_lowercase())
})
Trait Implementations§
source§impl Connection for SqliteConnection
impl Connection for SqliteConnection
source§fn establish(database_url: &str) -> ConnectionResult<Self>
fn establish(database_url: &str) -> ConnectionResult<Self>
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.
§type TransactionManager = AnsiTransactionManager
type TransactionManager = AnsiTransactionManager
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>where
T: QueryFragment<Self::Backend> + QueryId,
fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>where T: QueryFragment<Self::Backend> + QueryId,
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn transaction_state(&mut self) -> &mut AnsiTransactionManagerwhere
Self: Sized,
fn transaction_state(&mut self) -> &mut AnsiTransactionManagerwhere Self: Sized,
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E>where
F: FnOnce(&mut Self) -> Result<T, E>,
E: From<Error>,
fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E>where F: FnOnce(&mut Self) -> Result<T, E>, E: From<Error>,
source§fn begin_test_transaction(&mut self) -> QueryResult<()>
fn begin_test_transaction(&mut self) -> QueryResult<()>
source§impl<'conn, 'query> ConnectionGatWorkaround<'conn, 'query, Sqlite, DefaultLoadingMode> for SqliteConnection
impl<'conn, 'query> ConnectionGatWorkaround<'conn, 'query, Sqlite, DefaultLoadingMode> for SqliteConnection
§type Cursor = StatementIterator<'conn, 'query>
type Cursor = StatementIterator<'conn, 'query>
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.LoadConnection::load
Read more§type Row = SqliteRow<'conn, 'query>
type Row = SqliteRow<'conn, 'query>
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.Iterator::Item
for the iterator implementation
of ConnectionGatWorkaround::Cursor
source§impl LoadConnection<DefaultLoadingMode> for SqliteConnection
impl LoadConnection<DefaultLoadingMode> for SqliteConnection
source§fn load<'conn, 'query, T>(
&'conn mut self,
source: T
) -> QueryResult<LoadRowIter<'conn, 'query, Self, Self::Backend>>where
T: Query + QueryFragment<Self::Backend> + QueryId + 'query,
Self::Backend: QueryMetadata<T::SqlType>,
fn load<'conn, 'query, T>( &'conn mut self, source: T ) -> QueryResult<LoadRowIter<'conn, 'query, Self, Self::Backend>>where T: Query + QueryFragment<Self::Backend> + QueryId + 'query, Self::Backend: QueryMetadata<T::SqlType>,
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§impl R2D2Connection for SqliteConnection
Available on crate feature r2d2
only.
impl R2D2Connection for SqliteConnection
r2d2
only.source§impl SimpleConnection for SqliteConnection
impl SimpleConnection for SqliteConnection
source§fn batch_execute(&mut self, query: &str) -> QueryResult<()>
fn batch_execute(&mut self, query: &str) -> QueryResult<()>
source§impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for SqliteConnectionwhere
Changes: Copy + Identifiable + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
Changes::Table: FindDsl<Changes::Id>,
Update<Changes, Changes>: ExecuteDsl<SqliteConnection>,
Find<Changes::Table, Changes::Id>: LoadQuery<'b, SqliteConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates<No, Output = No>,
impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for SqliteConnectionwhere Changes: Copy + Identifiable + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget, Changes::Table: FindDsl<Changes::Id>, Update<Changes, Changes>: ExecuteDsl<SqliteConnection>, Find<Changes::Table, Changes::Id>: LoadQuery<'b, SqliteConnection, Output>, <Changes::Table as Table>::AllColumns: ValidGrouping<()>, <<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates<No, Output = No>,
source§fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output>
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output>
impl Send for SqliteConnection
Auto Trait Implementations§
impl RefUnwindSafe for SqliteConnection
impl !Sync for SqliteConnection
impl Unpin for SqliteConnection
impl UnwindSafe for SqliteConnection
Blanket Implementations§
source§impl<C> BoxableConnection<<C as Connection>::Backend> for Cwhere
C: Connection + Any,
impl<C> BoxableConnection<<C as Connection>::Backend> for Cwhere C: Connection + Any,
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> AsExprOf<Self, T>where
Self: AsExpression<T> + Sized,
T: SqlType + TypedExpressionType,
fn into_sql<T>(self) -> AsExprOf<Self, T>where Self: AsExpression<T> + Sized, T: SqlType + TypedExpressionType,
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> AsExprOf<&'a Self, T>where
&'a Self: AsExpression<T>,
T: SqlType + TypedExpressionType,
fn as_sql<'a, T>(&'a self) -> AsExprOf<&'a Self, T>where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,
&self
to an expression for Diesel’s query builder. Read more