sql_support

Trait ConnExt

source
pub trait ConnExt {
Show 18 methods // Required method fn conn(&self) -> &Connection; // Provided methods fn set_pragma<T>( &self, pragma_name: &str, pragma_value: T, ) -> SqlResult<&Self> where T: ToSql, Self: Sized { ... } fn prepare_maybe_cached<'conn>( &'conn self, sql: &str, cache: bool, ) -> SqlResult<MaybeCached<'conn>> { ... } fn execute_all(&self, stmts: &[&str]) -> SqlResult<()> { ... } fn execute_one(&self, stmt: &str) -> SqlResult<()> { ... } fn execute_cached<P: Params>( &self, sql: &str, params: P, ) -> SqlResult<usize> { ... } fn query_one<T: FromSql>(&self, sql: &str) -> SqlResult<T> { ... } fn exists<P: Params>(&self, sql: &str, params: P) -> SqlResult<bool> { ... } fn try_query_one<T: FromSql, P: Params>( &self, sql: &str, params: P, cache: bool, ) -> SqlResult<Option<T>> where Self: Sized { ... } fn query_row_and_then_cachable<T, E, P, F>( &self, sql: &str, params: P, mapper: F, cache: bool, ) -> Result<T, E> where Self: Sized, P: Params, E: From<Error>, F: FnOnce(&Row<'_>) -> Result<T, E> { ... } fn query_rows_and_then<T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Vec<T>, E> where Self: Sized, P: Params, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E> { ... } fn query_rows_and_then_cached<T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Vec<T>, E> where Self: Sized, P: Params, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E> { ... } fn query_rows_into<Coll, T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Coll, E> where Self: Sized, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E>, Coll: FromIterator<T>, P: Params { ... } fn query_rows_into_cached<Coll, T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Coll, E> where Self: Sized, P: Params, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E>, Coll: FromIterator<T> { ... } fn try_query_row<T, E, P, F>( &self, sql: &str, params: P, mapper: F, cache: bool, ) -> Result<Option<T>, E> where Self: Sized, P: Params, E: From<Error>, F: FnOnce(&Row<'_>) -> Result<T, E> { ... } fn unchecked_transaction(&self) -> SqlResult<UncheckedTransaction<'_>> { ... } fn unchecked_transaction_imm(&self) -> SqlResult<UncheckedTransaction<'_>> { ... } fn get_db_size(&self) -> Result<u32, Error> { ... }
}
Expand description

This trait exists so that we can use these helpers on rusqlite::{Transaction, Connection}. Note that you must import ConnExt in order to call these methods on anything.

Required Methods§

source

fn conn(&self) -> &Connection

The method you need to implement to opt in to all of this.

Provided Methods§

source

fn set_pragma<T>(&self, pragma_name: &str, pragma_value: T) -> SqlResult<&Self>
where T: ToSql, Self: Sized,

Set the value of the pragma on the main database. Returns the same object, for chaining.

source

fn prepare_maybe_cached<'conn>( &'conn self, sql: &str, cache: bool, ) -> SqlResult<MaybeCached<'conn>>

Get a cached or uncached statement based on a flag.

source

fn execute_all(&self, stmts: &[&str]) -> SqlResult<()>

Execute all the provided statements.

source

fn execute_one(&self, stmt: &str) -> SqlResult<()>

Execute a single statement.

source

fn execute_cached<P: Params>(&self, sql: &str, params: P) -> SqlResult<usize>

Equivalent to Connection::execute but caches the statement so that subsequent calls to execute_cached will have improved performance.

source

fn query_one<T: FromSql>(&self, sql: &str) -> SqlResult<T>

Execute a query that returns a single result column, and return that result.

source

fn exists<P: Params>(&self, sql: &str, params: P) -> SqlResult<bool>

Return true if a query returns any rows

source

fn try_query_one<T: FromSql, P: Params>( &self, sql: &str, params: P, cache: bool, ) -> SqlResult<Option<T>>
where Self: Sized,

Execute a query that returns 0 or 1 result columns, returning None if there were no rows, or if the only result was NULL.

source

fn query_row_and_then_cachable<T, E, P, F>( &self, sql: &str, params: P, mapper: F, cache: bool, ) -> Result<T, E>
where Self: Sized, P: Params, E: From<Error>, F: FnOnce(&Row<'_>) -> Result<T, E>,

Equivalent to rusqlite::Connection::query_row_and_then but allows passing a flag to indicate that it’s cached.

source

fn query_rows_and_then<T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Vec<T>, E>
where Self: Sized, P: Params, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E>,

Helper for when you’d like to get a Vec<T> of all the rows returned by a query that takes named arguments. See also query_rows_and_then_cached.

source

fn query_rows_and_then_cached<T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Vec<T>, E>
where Self: Sized, P: Params, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E>,

Helper for when you’d like to get a Vec<T> of all the rows returned by a query that takes named arguments.

source

fn query_rows_into<Coll, T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Coll, E>
where Self: Sized, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E>, Coll: FromIterator<T>, P: Params,

Like query_rows_and_then_cachable, but works if you want a non-Vec as a result.

§Example:
fn get_visit_tombstones(conn: &Connection, id: i64) -> rusqlite::Result<HashSet<i64>> {
    Ok(conn.query_rows_into(
        "SELECT visit_date FROM moz_historyvisit_tombstones
         WHERE place_id = :place_id",
        &[(":place_id", &id)],
        |row| row.get::<_, i64>(0))?)
}

Note if the type isn’t inferred, you’ll have to do something gross like conn.query_rows_into::<HashSet<_>, _, _, _>(...).

source

fn query_rows_into_cached<Coll, T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Coll, E>
where Self: Sized, P: Params, E: From<Error>, F: FnMut(&Row<'_>) -> Result<T, E>, Coll: FromIterator<T>,

Same as query_rows_into, but caches the stmt if possible.

source

fn try_query_row<T, E, P, F>( &self, sql: &str, params: P, mapper: F, cache: bool, ) -> Result<Option<T>, E>
where Self: Sized, P: Params, E: From<Error>, F: FnOnce(&Row<'_>) -> Result<T, E>,

Like query_row_and_then_cacheable but returns None instead of erroring if no such row exists.

source

fn unchecked_transaction(&self) -> SqlResult<UncheckedTransaction<'_>>

Caveat: This won’t actually get used most of the time, and calls will usually invoke rusqlite’s method with the same name. See comment on UncheckedTransaction for details (generally you probably don’t need to care)

source

fn unchecked_transaction_imm(&self) -> SqlResult<UncheckedTransaction<'_>>

Begin unchecked_transaction with TransactionBehavior::Immediate. Use when the first operation will be a read operation, that further writes depend on for correctness.

source

fn get_db_size(&self) -> Result<u32, Error>

Get the DB size in bytes

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl ConnExt for Connection

source§

fn conn(&self) -> &Connection

source§

impl ConnExt for Savepoint<'_>

source§

fn conn(&self) -> &Connection

source§

impl ConnExt for Transaction<'_>

source§

fn conn(&self) -> &Connection

Implementors§