pub struct SqliteConnection { /* private fields */ }
Available on crate feature 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§

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(())
})

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(())
})

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§

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.

The backend this type connects to
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The transaction manager implementation used by this connection
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Execute a single SQL statements given by a query and return number of affected rows Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Get access to the current transaction state of this connection Read more
Executes the given function inside of a database transaction Read more
Creates a transaction that will never be committed. This is useful for tests. Panics if called while inside of a transaction or if called with a connection containing a broken transaction Read more
Executes the given function inside a transaction, but does not commit it. Panics if the given function returns an error. Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The cursor type returned by LoadConnection::load Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
The row type used as Iterator::Item for the iterator implementation of ConnectionGatWorkaround::Cursor Read more
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes only.
Executes a given query and returns any requested values Read more
Setup the following table: Read more
Check if a connection is still valid
Checks if the connection is broken and should not be reused Read more
Execute multiple SQL statements within the same string. Read more
See the traits documentation.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Convert self to an expression for Diesel’s query builder. Read more
Convert &self to an expression for Diesel’s query builder. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.