[−][src]Trait sql_support::ConnExt
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
fn conn(&self) -> &Connection
The method you need to implement to opt in to all of this.
Provided methods
fn set_pragma<T>(&self, pragma_name: &str, pragma_value: T) -> SqlResult<&Self> where
T: ToSql,
Self: Sized,
T: ToSql,
Self: Sized,
Set the value of the pragma on the main database. Returns the same object, for chaining.
fn prepare_maybe_cached<'conn>(
&'conn self,
sql: &str,
cache: bool
) -> SqlResult<MaybeCached<'conn>>
&'conn self,
sql: &str,
cache: bool
) -> SqlResult<MaybeCached<'conn>>
Get a cached or uncached statement based on a flag.
fn execute_all(&self, stmts: &[&str]) -> SqlResult<()>
Execute all the provided statements.
fn execute_cached<P>(&self, sql: &str, params: P) -> SqlResult<usize> where
P: IntoIterator,
P::Item: ToSql,
P: IntoIterator,
P::Item: ToSql,
Equivalent to Connection::execute_named
but caches the statement so that subsequent
calls to execute_cached
will have improved performance.
fn execute_named_cached(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)]
) -> SqlResult<usize>
&self,
sql: &str,
params: &[(&str, &dyn ToSql)]
) -> SqlResult<usize>
Equivalent to Connection::execute_named
but caches the statement so that subsequent
calls to execute_named_cached
will have imprroved performance.
fn query_one<T: FromSql>(&self, sql: &str) -> SqlResult<T>
Execute a query that returns a single result column, and return that result.
fn try_query_one<T: FromSql>(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
cache: bool
) -> SqlResult<Option<T>> where
Self: Sized,
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
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.
fn query_row_and_then_named<T, E, F>(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F,
cache: bool
) -> Result<T, E> where
Self: Sized,
E: From<Error>,
F: FnOnce(&Row<'_>) -> Result<T, E>,
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F,
cache: bool
) -> Result<T, E> where
Self: Sized,
E: From<Error>,
F: FnOnce(&Row<'_>) -> Result<T, E>,
Equivalent to rusqlite::Connection::query_row_and_then
but allows use
of named parameters, and allows passing a flag to indicate that it's cached.
fn query_rows_and_then_named<T, E, F>(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Vec<T>, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Vec<T>, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
Helper for when you'd like to get a Vecquery_rows_and_then_named_cached
.
fn query_rows_and_then_named_cached<T, E, F>(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Vec<T>, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Vec<T>, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
Helper for when you'd like to get a Vec
fn query_rows_into<Coll, T, E, F>(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Coll, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
Coll: FromIterator<T>,
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Coll, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
Coll: FromIterator<T>,
Like query_rows_and_then_named
, 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<_>, _, _, _>(...)
.
fn query_rows_into_cached<Coll, T, E, F>(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Coll, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
Coll: FromIterator<T>,
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F
) -> Result<Coll, E> where
Self: Sized,
E: From<Error>,
F: FnMut(&Row<'_>) -> Result<T, E>,
Coll: FromIterator<T>,
Same as query_rows_into
, but caches the stmt if possible.
fn try_query_row<T, E, F>(
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F,
cache: bool
) -> Result<Option<T>, E> where
Self: Sized,
E: From<Error>,
F: FnOnce(&Row<'_>) -> Result<T, E>,
&self,
sql: &str,
params: &[(&str, &dyn ToSql)],
mapper: F,
cache: bool
) -> Result<Option<T>, E> where
Self: Sized,
E: From<Error>,
F: FnOnce(&Row<'_>) -> Result<T, E>,
Like query_row_and_then_named
but returns None instead of erroring if no such row exists.
fn unchecked_transaction(&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.