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§
Provided Methods§
sourcefn set_pragma<T>(&self, pragma_name: &str, pragma_value: T) -> SqlResult<&Self>where
T: ToSql,
Self: Sized,
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.
sourcefn prepare_maybe_cached<'conn>(
&'conn self,
sql: &str,
cache: bool,
) -> SqlResult<MaybeCached<'conn>>
fn prepare_maybe_cached<'conn>( &'conn self, sql: &str, cache: bool, ) -> SqlResult<MaybeCached<'conn>>
Get a cached or uncached statement based on a flag.
sourcefn execute_all(&self, stmts: &[&str]) -> SqlResult<()>
fn execute_all(&self, stmts: &[&str]) -> SqlResult<()>
Execute all the provided statements.
sourcefn execute_one(&self, stmt: &str) -> SqlResult<()>
fn execute_one(&self, stmt: &str) -> SqlResult<()>
Execute a single statement.
sourcefn execute_cached<P: Params>(&self, sql: &str, params: P) -> SqlResult<usize>
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.
sourcefn query_one<T: FromSql>(&self, sql: &str) -> SqlResult<T>
fn query_one<T: FromSql>(&self, sql: &str) -> SqlResult<T>
Execute a query that returns a single result column, and return that result.
sourcefn exists<P: Params>(&self, sql: &str, params: P) -> SqlResult<bool>
fn exists<P: Params>(&self, sql: &str, params: P) -> SqlResult<bool>
Return true if a query returns any rows
sourcefn try_query_one<T: FromSql, P: Params>(
&self,
sql: &str,
params: P,
cache: bool,
) -> SqlResult<Option<T>>where
Self: Sized,
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.
sourcefn query_row_and_then_cachable<T, E, P, F>(
&self,
sql: &str,
params: P,
mapper: F,
cache: bool,
) -> Result<T, E>
fn query_row_and_then_cachable<T, E, P, F>( &self, sql: &str, params: P, mapper: F, cache: bool, ) -> Result<T, E>
Equivalent to rusqlite::Connection::query_row_and_then
but allows
passing a flag to indicate that it’s cached.
sourcefn query_rows_and_then<T, E, P, F>(
&self,
sql: &str,
params: P,
mapper: F,
) -> Result<Vec<T>, E>
fn query_rows_and_then<T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Vec<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
.
sourcefn query_rows_and_then_cached<T, E, P, F>(
&self,
sql: &str,
params: P,
mapper: F,
) -> Result<Vec<T>, E>
fn query_rows_and_then_cached<T, E, P, F>( &self, sql: &str, params: P, mapper: F, ) -> Result<Vec<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.
sourcefn 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<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<_>, _, _, _>(...)
.
sourcefn 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 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.
sourcefn try_query_row<T, E, P, F>(
&self,
sql: &str,
params: P,
mapper: F,
cache: bool,
) -> Result<Option<T>, E>
fn try_query_row<T, E, P, F>( &self, sql: &str, params: P, mapper: F, cache: bool, ) -> Result<Option<T>, E>
Like query_row_and_then_cacheable
but returns None instead of erroring
if no such row exists.
sourcefn unchecked_transaction(&self) -> SqlResult<UncheckedTransaction<'_>>
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)
sourcefn unchecked_transaction_imm(&self) -> SqlResult<UncheckedTransaction<'_>>
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.
sourcefn get_db_size(&self) -> Result<u32, Error>
fn get_db_size(&self) -> Result<u32, Error>
Get the DB size in bytes