2018-09-16 15:49:23 +08:00
|
|
|
use std::iter::IntoIterator;
|
2020-03-26 02:20:05 +08:00
|
|
|
use std::os::raw::{c_int, c_void};
|
2018-06-10 18:16:54 +08:00
|
|
|
#[cfg(feature = "array")]
|
|
|
|
use std::rc::Rc;
|
2017-03-08 23:48:14 +08:00
|
|
|
use std::slice::from_raw_parts;
|
2020-04-07 05:43:06 +08:00
|
|
|
use std::{convert, fmt, mem, ptr, str};
|
2017-03-08 23:48:14 +08:00
|
|
|
|
|
|
|
use super::ffi;
|
2020-04-14 14:47:12 +08:00
|
|
|
use super::{len_as_c_int, str_for_sqlite};
|
2018-08-11 18:48:21 +08:00
|
|
|
use super::{
|
2020-11-03 15:34:08 +08:00
|
|
|
AndThenRows, Connection, Error, MappedRows, Params, RawStatement, Result, Row, Rows, ValueRef,
|
2018-08-11 18:48:21 +08:00
|
|
|
};
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::types::{ToSql, ToSqlOutput};
|
2018-06-10 18:16:54 +08:00
|
|
|
#[cfg(feature = "array")]
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::vtab::array::{free_array, ARRAY_TYPE};
|
2017-03-08 23:48:14 +08:00
|
|
|
|
|
|
|
/// A prepared statement.
|
|
|
|
pub struct Statement<'conn> {
|
|
|
|
conn: &'conn Connection,
|
2019-03-20 03:33:36 +08:00
|
|
|
pub(crate) stmt: RawStatement,
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl Statement<'_> {
|
2017-03-08 23:48:14 +08:00
|
|
|
/// Execute the prepared statement.
|
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// On success, returns the number of rows that were changed or inserted or
|
|
|
|
/// deleted (via `sqlite3_changes`).
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ### Use with positional parameters
|
|
|
|
///
|
2017-03-08 23:48:14 +08:00
|
|
|
/// ```rust,no_run
|
2020-11-03 15:34:08 +08:00
|
|
|
/// # use rusqlite::{Connection, Result, params};
|
2017-03-08 23:48:14 +08:00
|
|
|
/// fn update_rows(conn: &Connection) -> Result<()> {
|
2018-10-31 03:11:35 +08:00
|
|
|
/// let mut stmt = conn.prepare("UPDATE foo SET bar = 'baz' WHERE qux = ?")?;
|
2020-11-03 16:46:54 +08:00
|
|
|
/// // The `rusqlite::params!` macro is mostly useful when the parameters do not
|
|
|
|
/// // all have the same type, or if there are more than 32 parameters
|
|
|
|
/// // at once.
|
2020-11-03 15:34:08 +08:00
|
|
|
/// stmt.execute(params![1i32])?;
|
2020-11-03 16:46:54 +08:00
|
|
|
/// // However, it's not required, many cases are fine as:
|
2020-11-03 15:34:08 +08:00
|
|
|
/// stmt.execute(&[&2i32])?;
|
2020-11-03 16:46:54 +08:00
|
|
|
/// // Or even:
|
|
|
|
/// stmt.execute([2i32])?;
|
2017-03-08 23:48:14 +08:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ### Use with named parameters
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2019-01-30 07:33:57 +08:00
|
|
|
/// # use rusqlite::{Connection, Result, named_params};
|
2020-11-03 16:46:54 +08:00
|
|
|
/// fn insert(conn: &Connection) -> Result<()> {
|
|
|
|
/// let mut stmt = conn.prepare("INSERT INTO test (key, value) VALUES (:key, :value)")?;
|
|
|
|
/// // The `rusqlite::named_params!` macro (like `params!`) is useful for heterogeneous
|
|
|
|
/// // sets of parameters (where all parameters are not the same type), or for queries
|
|
|
|
/// // with many (more than 32) statically known parameters.
|
|
|
|
/// stmt.execute(named_params!{ ":key": "one", ":val": 2 })?;
|
|
|
|
/// // However, named parameters can also be passed like:
|
|
|
|
/// stmt.execute(&[(":key", "three"), (":val", "four")])?;
|
|
|
|
/// // Or even: (note that a &T is required for the value type, currently)
|
|
|
|
/// stmt.execute(&[(":key", &100), (":val", &200)])?;
|
|
|
|
/// Ok(())
|
2020-11-03 15:34:08 +08:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Use without parameters
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result, params};
|
|
|
|
/// fn delete_all(conn: &Connection) -> Result<()> {
|
|
|
|
/// let mut stmt = conn.prepare("DELETE FROM users")?;
|
|
|
|
/// stmt.execute([])?;
|
|
|
|
/// Ok(())
|
2019-01-30 07:33:57 +08:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2017-03-09 00:20:43 +08:00
|
|
|
/// # Failure
|
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Will return `Err` if binding parameters fails, the executed statement
|
|
|
|
/// returns rows (in which case `query` should be used instead), or the
|
2019-09-01 17:41:51 +08:00
|
|
|
/// underlying SQLite call fails.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub fn execute<P: Params>(&mut self, params: P) -> Result<usize> {
|
2021-02-01 07:40:55 +08:00
|
|
|
params.__bind_in(self)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
self.execute_with_bound_parameters()
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:34:08 +08:00
|
|
|
/// Execute the prepared statement with named parameter(s).
|
|
|
|
///
|
|
|
|
/// Note: This function is deprecated in favor of [`Statement::execute`],
|
|
|
|
/// which can now take named parameters directly.
|
|
|
|
///
|
|
|
|
/// If any parameters that were in the prepared statement are not included
|
|
|
|
/// in `params`, they will continue to use the most-recently bound value
|
|
|
|
/// from a previous call to `execute_named`, or `NULL` if they have never
|
|
|
|
/// been bound.
|
|
|
|
///
|
|
|
|
/// On success, returns the number of rows that were changed or inserted or
|
|
|
|
/// deleted (via `sqlite3_changes`).
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if binding parameters fails, the executed statement
|
|
|
|
/// returns rows (in which case `query` should be used instead), or the
|
|
|
|
/// underlying SQLite call fails.
|
|
|
|
#[deprecated = "You can use `execute` with named params now."]
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub fn execute_named(&mut self, params: &[(&str, &dyn ToSql)]) -> Result<usize> {
|
|
|
|
self.execute(params)
|
|
|
|
}
|
|
|
|
|
2017-03-09 00:26:25 +08:00
|
|
|
/// Execute an INSERT and return the ROWID.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
2021-04-03 17:03:50 +08:00
|
|
|
/// This function is a convenience wrapper around
|
|
|
|
/// [`execute()`](Statement::execute) intended for queries that insert a
|
|
|
|
/// single item. It is possible to misuse this function in a way that it
|
|
|
|
/// cannot detect, such as by calling it on a statement which _updates_
|
|
|
|
/// a single item rather than inserting one. Please don't do that.
|
2017-03-09 00:26:25 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if no row is inserted or many rows are inserted.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub fn insert<P: Params>(&mut self, params: P) -> Result<i64> {
|
2018-10-31 03:11:35 +08:00
|
|
|
let changes = self.execute(params)?;
|
2017-03-09 00:26:25 +08:00
|
|
|
match changes {
|
|
|
|
1 => Ok(self.conn.last_insert_rowid()),
|
|
|
|
_ => Err(Error::StatementChangedRows(changes)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Execute the prepared statement, returning a handle to the resulting
|
|
|
|
/// rows.
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
2021-09-23 11:44:26 +08:00
|
|
|
/// Due to lifetime restrictions, the rows handle returned by `query` does
|
|
|
|
/// not implement the `Iterator` trait. Consider using
|
2021-05-08 01:39:53 +08:00
|
|
|
/// [`query_map`](Statement::query_map) or
|
|
|
|
/// [`query_and_then`](Statement::query_and_then) instead, which do.
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ### Use without parameters
|
|
|
|
///
|
2017-03-08 23:48:14 +08:00
|
|
|
/// ```rust,no_run
|
2020-11-03 15:34:08 +08:00
|
|
|
/// # use rusqlite::{Connection, Result};
|
2017-03-08 23:48:14 +08:00
|
|
|
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
2018-10-31 03:11:35 +08:00
|
|
|
/// let mut stmt = conn.prepare("SELECT name FROM people")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
/// let mut rows = stmt.query([])?;
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
|
|
|
/// let mut names = Vec::new();
|
2019-02-09 13:38:24 +08:00
|
|
|
/// while let Some(row) = rows.next()? {
|
2019-02-22 03:48:09 +08:00
|
|
|
/// names.push(row.get(0)?);
|
2017-03-08 23:48:14 +08:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(names)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ### Use with positional parameters
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn query(conn: &Connection, name: &str) -> Result<()> {
|
|
|
|
/// let mut stmt = conn.prepare("SELECT * FROM test where name = ?")?;
|
|
|
|
/// let mut rows = stmt.query(rusqlite::params![name])?;
|
|
|
|
/// while let Some(row) = rows.next()? {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// Or, equivalently (but without the [`params!`] macro).
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn query(conn: &Connection, name: &str) -> Result<()> {
|
|
|
|
/// let mut stmt = conn.prepare("SELECT * FROM test where name = ?")?;
|
2021-01-20 04:16:08 +08:00
|
|
|
/// let mut rows = stmt.query([name])?;
|
2020-11-03 15:34:08 +08:00
|
|
|
/// while let Some(row) = rows.next()? {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Use with named parameters
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn query(conn: &Connection) -> Result<()> {
|
2018-10-31 03:11:35 +08:00
|
|
|
/// let mut stmt = conn.prepare("SELECT * FROM test where name = :name")?;
|
2021-01-20 04:16:08 +08:00
|
|
|
/// let mut rows = stmt.query(&[(":name", "one")])?;
|
2019-02-09 13:38:24 +08:00
|
|
|
/// while let Some(row) = rows.next()? {
|
2017-03-09 00:20:43 +08:00
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2019-01-31 03:14:46 +08:00
|
|
|
/// Note, the `named_params!` macro is provided for syntactic convenience,
|
|
|
|
/// and so the above example could also be written as:
|
2019-01-30 07:33:57 +08:00
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result, named_params};
|
|
|
|
/// fn query(conn: &Connection) -> Result<()> {
|
|
|
|
/// let mut stmt = conn.prepare("SELECT * FROM test where name = :name")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
/// let mut rows = stmt.query(named_params!{ ":name": "one" })?;
|
2019-02-09 13:38:24 +08:00
|
|
|
/// while let Some(row) = rows.next()? {
|
2017-03-09 00:20:43 +08:00
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ## Failure
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
|
|
|
/// Will return `Err` if binding parameters fails.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub fn query<P: Params>(&mut self, params: P) -> Result<Rows<'_>> {
|
2021-02-01 07:40:55 +08:00
|
|
|
params.__bind_in(self)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
Ok(Rows::new(self))
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:34:08 +08:00
|
|
|
/// Execute the prepared statement with named parameter(s), returning a
|
|
|
|
/// handle for the resulting rows.
|
|
|
|
///
|
|
|
|
/// Note: This function is deprecated in favor of [`Statement::query`],
|
|
|
|
/// which can now take named parameters directly.
|
|
|
|
///
|
|
|
|
/// If any parameters that were in the prepared statement are not included
|
|
|
|
/// in `params`, they will continue to use the most-recently bound value
|
|
|
|
/// from a previous call to `query_named`, or `NULL` if they have never been
|
|
|
|
/// bound.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if binding parameters fails.
|
|
|
|
#[deprecated = "You can use `query` with named params now."]
|
|
|
|
pub fn query_named(&mut self, params: &[(&str, &dyn ToSql)]) -> Result<Rows<'_>> {
|
|
|
|
self.query(params)
|
|
|
|
}
|
|
|
|
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Executes the prepared statement and maps a function over the resulting
|
|
|
|
/// rows, returning an iterator over the mapped function results.
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
2021-05-02 19:46:04 +08:00
|
|
|
/// `f` is used to transform the _streaming_ iterator into a _standard_
|
2020-11-03 15:34:08 +08:00
|
|
|
/// iterator.
|
|
|
|
///
|
2020-11-04 11:10:23 +08:00
|
|
|
/// This is equivalent to `stmt.query(params)?.mapped(f)`.
|
|
|
|
///
|
2017-03-08 23:48:14 +08:00
|
|
|
/// ## Example
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ### Use with positional params
|
|
|
|
///
|
2017-03-08 23:48:14 +08:00
|
|
|
/// ```rust,no_run
|
2020-11-03 15:34:08 +08:00
|
|
|
/// # use rusqlite::{Connection, Result};
|
2017-03-08 23:48:14 +08:00
|
|
|
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
2018-10-31 03:11:35 +08:00
|
|
|
/// let mut stmt = conn.prepare("SELECT name FROM people")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
/// let rows = stmt.query_map([], |row| row.get(0))?;
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
|
|
|
/// let mut names = Vec::new();
|
|
|
|
/// for name_result in rows {
|
2018-10-31 03:11:35 +08:00
|
|
|
/// names.push(name_result?);
|
2017-03-08 23:48:14 +08:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(names)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ### Use with named params
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
|
|
|
/// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = :id")?;
|
|
|
|
/// let rows = stmt.query_map(&[(":id", &"one")], |row| row.get(0))?;
|
|
|
|
///
|
|
|
|
/// let mut names = Vec::new();
|
|
|
|
/// for name_result in rows {
|
|
|
|
/// names.push(name_result?);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(names)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-03-08 23:48:14 +08:00
|
|
|
/// ## Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if binding parameters fails.
|
2019-02-02 18:08:04 +08:00
|
|
|
pub fn query_map<T, P, F>(&mut self, params: P, f: F) -> Result<MappedRows<'_, F>>
|
2018-08-11 18:48:21 +08:00
|
|
|
where
|
2020-11-03 15:34:08 +08:00
|
|
|
P: Params,
|
2019-03-10 18:12:14 +08:00
|
|
|
F: FnMut(&Row<'_>) -> Result<T>,
|
2017-03-08 23:48:14 +08:00
|
|
|
{
|
2020-11-04 11:10:23 +08:00
|
|
|
self.query(params).map(|rows| rows.mapped(f))
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Execute the prepared statement with named parameter(s), returning an
|
|
|
|
/// iterator over the result of calling the mapping function over the
|
2020-11-03 15:34:08 +08:00
|
|
|
/// query's rows.
|
|
|
|
///
|
|
|
|
/// Note: This function is deprecated in favor of [`Statement::query_map`],
|
|
|
|
/// which can now take named parameters directly.
|
|
|
|
///
|
|
|
|
/// If any parameters that were in the prepared statement
|
2018-08-17 00:29:46 +08:00
|
|
|
/// are not included in `params`, they will continue to use the
|
|
|
|
/// most-recently bound value from a previous call to `query_named`,
|
|
|
|
/// or `NULL` if they have never been bound.
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
2021-05-02 19:46:04 +08:00
|
|
|
/// `f` is used to transform the _streaming_ iterator into a _standard_
|
2020-06-03 01:05:09 +08:00
|
|
|
/// iterator.
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
|
|
|
/// ## Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if binding parameters fails.
|
2020-11-03 15:34:08 +08:00
|
|
|
#[deprecated = "You can use `query_map` with named params now."]
|
2019-02-03 18:02:38 +08:00
|
|
|
pub fn query_map_named<T, F>(
|
|
|
|
&mut self,
|
2018-12-08 04:57:04 +08:00
|
|
|
params: &[(&str, &dyn ToSql)],
|
2018-08-11 18:48:21 +08:00
|
|
|
f: F,
|
2019-02-03 18:02:38 +08:00
|
|
|
) -> Result<MappedRows<'_, F>>
|
2018-08-11 18:48:21 +08:00
|
|
|
where
|
2019-03-10 18:12:14 +08:00
|
|
|
F: FnMut(&Row<'_>) -> Result<T>,
|
2017-03-09 00:20:43 +08:00
|
|
|
{
|
2020-11-03 15:34:08 +08:00
|
|
|
self.query_map(params, f)
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-08 23:48:14 +08:00
|
|
|
/// Executes the prepared statement and maps a function over the resulting
|
2018-08-17 00:29:46 +08:00
|
|
|
/// rows, where the function returns a `Result` with `Error` type
|
|
|
|
/// implementing `std::convert::From<Error>` (so errors can be unified).
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
2020-11-04 11:10:23 +08:00
|
|
|
/// This is equivalent to `stmt.query(params)?.and_then(f)`.
|
|
|
|
///
|
2017-03-09 00:20:43 +08:00
|
|
|
/// ## Example
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// ### Use with named params
|
|
|
|
///
|
2017-03-09 00:20:43 +08:00
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
2018-08-17 00:29:46 +08:00
|
|
|
/// struct Person {
|
|
|
|
/// name: String,
|
|
|
|
/// };
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
|
|
|
/// fn name_to_person(name: String) -> Result<Person> {
|
|
|
|
/// // ... check for valid name
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Ok(Person { name: name })
|
2017-03-09 00:20:43 +08:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn get_names(conn: &Connection) -> Result<Vec<Person>> {
|
2018-10-31 03:11:35 +08:00
|
|
|
/// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = :id")?;
|
2018-08-17 00:29:46 +08:00
|
|
|
/// let rows =
|
2021-01-20 04:16:08 +08:00
|
|
|
/// stmt.query_and_then(&[(":id", "one")], |row| name_to_person(row.get(0)?))?;
|
2020-11-03 15:34:08 +08:00
|
|
|
///
|
|
|
|
/// let mut persons = Vec::new();
|
|
|
|
/// for person_result in rows {
|
|
|
|
/// persons.push(person_result?);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(persons)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Use with positional params
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
|
|
|
/// let mut stmt = conn.prepare("SELECT name FROM people WHERE id = ?")?;
|
2021-01-20 04:16:08 +08:00
|
|
|
/// let rows = stmt.query_and_then(["one"], |row| row.get::<_, String>(0))?;
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
|
|
|
/// let mut persons = Vec::new();
|
|
|
|
/// for person_result in rows {
|
2018-10-31 03:11:35 +08:00
|
|
|
/// persons.push(person_result?);
|
2017-03-09 00:20:43 +08:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(persons)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-11-03 15:34:08 +08:00
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if binding parameters fails.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub fn query_and_then<T, E, P, F>(&mut self, params: P, f: F) -> Result<AndThenRows<'_, F>>
|
|
|
|
where
|
|
|
|
P: Params,
|
|
|
|
E: convert::From<Error>,
|
|
|
|
F: FnMut(&Row<'_>) -> Result<T, E>,
|
|
|
|
{
|
2020-11-04 11:10:23 +08:00
|
|
|
self.query(params).map(|rows| rows.and_then(f))
|
2020-11-03 15:34:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the prepared statement with named parameter(s), returning an
|
|
|
|
/// iterator over the result of calling the mapping function over the
|
|
|
|
/// query's rows.
|
|
|
|
///
|
2021-04-03 17:03:50 +08:00
|
|
|
/// Note: This function is deprecated in favor of
|
|
|
|
/// [`Statement::query_and_then`], which can now take named parameters
|
|
|
|
/// directly.
|
2020-11-03 15:34:08 +08:00
|
|
|
///
|
|
|
|
/// If any parameters that were in the prepared statement are not included
|
|
|
|
/// in `params`, they will continue to use the most-recently bound value
|
|
|
|
/// from a previous call to `query_named`, or `NULL` if they have never been
|
|
|
|
/// bound.
|
|
|
|
///
|
2017-03-09 00:20:43 +08:00
|
|
|
/// ## Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if binding parameters fails.
|
2020-11-03 15:34:08 +08:00
|
|
|
#[deprecated = "You can use `query_and_then` with named params now."]
|
2019-02-03 18:02:38 +08:00
|
|
|
pub fn query_and_then_named<T, E, F>(
|
|
|
|
&mut self,
|
2018-12-08 04:57:04 +08:00
|
|
|
params: &[(&str, &dyn ToSql)],
|
2018-08-11 18:48:21 +08:00
|
|
|
f: F,
|
2019-02-03 18:02:38 +08:00
|
|
|
) -> Result<AndThenRows<'_, F>>
|
2018-08-11 18:48:21 +08:00
|
|
|
where
|
|
|
|
E: convert::From<Error>,
|
2020-04-07 05:43:06 +08:00
|
|
|
F: FnMut(&Row<'_>) -> Result<T, E>,
|
2017-03-09 00:20:43 +08:00
|
|
|
{
|
2020-11-03 15:34:08 +08:00
|
|
|
self.query_and_then(params, f)
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Return `true` if a query in the SQL statement it executes returns one
|
|
|
|
/// or more rows and `false` if the SQL returns an empty set.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub fn exists<P: Params>(&mut self, params: P) -> Result<bool> {
|
2018-10-31 03:11:35 +08:00
|
|
|
let mut rows = self.query(params)?;
|
2019-02-03 21:01:42 +08:00
|
|
|
let exists = rows.next()?.is_some();
|
2017-03-09 00:26:25 +08:00
|
|
|
Ok(exists)
|
|
|
|
}
|
|
|
|
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Convenience method to execute a query that is expected to return a
|
|
|
|
/// single row.
|
2017-03-09 00:26:25 +08:00
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// If the query returns more than one row, all rows except the first are
|
|
|
|
/// ignored.
|
2017-03-09 00:26:25 +08:00
|
|
|
///
|
2018-12-20 04:58:33 +08:00
|
|
|
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
|
2021-04-03 17:03:50 +08:00
|
|
|
/// query truly is optional, you can call
|
|
|
|
/// [`.optional()`](crate::OptionalExtension::optional) on the result of
|
|
|
|
/// this to get a `Result<Option<T>>` (requires that the trait
|
|
|
|
/// `rusqlite::OptionalExtension` is imported).
|
2018-12-16 18:55:04 +08:00
|
|
|
///
|
2017-03-09 00:26:25 +08:00
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if the underlying SQLite call fails.
|
2018-09-16 15:49:23 +08:00
|
|
|
pub fn query_row<T, P, F>(&mut self, params: P, f: F) -> Result<T>
|
2018-08-11 18:48:21 +08:00
|
|
|
where
|
2020-11-03 15:34:08 +08:00
|
|
|
P: Params,
|
2019-03-10 18:12:14 +08:00
|
|
|
F: FnOnce(&Row<'_>) -> Result<T>,
|
2017-03-09 00:26:25 +08:00
|
|
|
{
|
2018-10-31 03:11:35 +08:00
|
|
|
let mut rows = self.query(params)?;
|
2017-03-09 00:26:25 +08:00
|
|
|
|
2021-10-02 02:09:48 +08:00
|
|
|
rows.get_expected_row().and_then(f)
|
2017-03-09 00:26:25 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 01:18:57 +08:00
|
|
|
/// Convenience method to execute a query with named parameter(s) that is
|
|
|
|
/// expected to return a single row.
|
|
|
|
///
|
2021-04-03 17:03:50 +08:00
|
|
|
/// Note: This function is deprecated in favor of
|
|
|
|
/// [`Statement::query_and_then`], which can now take named parameters
|
|
|
|
/// directly.
|
2020-11-03 15:34:08 +08:00
|
|
|
///
|
2019-06-13 01:18:57 +08:00
|
|
|
/// If the query returns more than one row, all rows except the first are
|
|
|
|
/// ignored.
|
|
|
|
///
|
|
|
|
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
|
2021-04-03 17:03:50 +08:00
|
|
|
/// query truly is optional, you can call
|
|
|
|
/// [`.optional()`](crate::OptionalExtension::optional) on the result of
|
|
|
|
/// this to get a `Result<Option<T>>` (requires that the trait
|
|
|
|
/// `rusqlite::OptionalExtension` is imported).
|
2019-06-13 01:18:57 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
|
|
|
/// or if the underlying SQLite call fails.
|
2020-11-03 15:34:08 +08:00
|
|
|
#[deprecated = "You can use `query_row` with named params now."]
|
2019-06-13 01:18:57 +08:00
|
|
|
pub fn query_row_named<T, F>(&mut self, params: &[(&str, &dyn ToSql)], f: F) -> Result<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(&Row<'_>) -> Result<T>,
|
|
|
|
{
|
2020-11-03 15:34:08 +08:00
|
|
|
self.query_row(params, f)
|
2017-03-09 00:26:25 +08:00
|
|
|
}
|
|
|
|
|
2017-03-08 23:48:14 +08:00
|
|
|
/// Consumes the statement.
|
|
|
|
///
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Functionally equivalent to the `Drop` implementation, but allows
|
|
|
|
/// callers to see any errors that occur.
|
2017-03-08 23:48:14 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if the underlying SQLite call fails.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2017-03-08 23:48:14 +08:00
|
|
|
pub fn finalize(mut self) -> Result<()> {
|
|
|
|
self.finalize_()
|
|
|
|
}
|
|
|
|
|
2020-11-22 16:34:03 +08:00
|
|
|
/// Return the (one-based) index of an SQL parameter given its name.
|
2020-04-07 01:44:00 +08:00
|
|
|
///
|
|
|
|
/// Note that the initial ":" or "$" or "@" or "?" used to specify the
|
|
|
|
/// parameter is included as part of the name.
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn example(conn: &Connection) -> Result<()> {
|
|
|
|
/// let stmt = conn.prepare("SELECT * FROM test WHERE name = :example")?;
|
|
|
|
/// let index = stmt.parameter_index(":example")?;
|
|
|
|
/// assert_eq!(index, Some(1));
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-03-09 00:20:43 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return Err if `name` is invalid. Will return Ok(None) if the name
|
|
|
|
/// is valid but not a bound parameter of this statement.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2018-05-24 03:04:13 +08:00
|
|
|
pub fn parameter_index(&self, name: &str) -> Result<Option<usize>> {
|
2020-04-14 14:47:12 +08:00
|
|
|
Ok(self.stmt.bind_parameter_index(name))
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 01:39:53 +08:00
|
|
|
/// Return the SQL parameter name given its (one-based) index (the inverse
|
|
|
|
/// of [`Statement::parameter_index`]).
|
2021-04-26 07:53:37 +08:00
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn example(conn: &Connection) -> Result<()> {
|
|
|
|
/// let stmt = conn.prepare("SELECT * FROM test WHERE name = :example")?;
|
|
|
|
/// let index = stmt.parameter_name(1);
|
|
|
|
/// assert_eq!(index, Some(":example"));
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
2021-05-08 01:39:53 +08:00
|
|
|
/// Will return `None` if the column index is out of bounds or if the
|
|
|
|
/// parameter is positional.
|
2021-04-26 07:53:37 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn parameter_name(&self, index: usize) -> Option<&'_ str> {
|
|
|
|
self.stmt.bind_parameter_name(index as i32).map(|name| {
|
|
|
|
str::from_utf8(name.to_bytes()).expect("Invalid UTF-8 sequence in parameter name")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub(crate) fn bind_parameters<P>(&mut self, params: P) -> Result<()>
|
2018-09-16 15:49:23 +08:00
|
|
|
where
|
|
|
|
P: IntoIterator,
|
|
|
|
P::Item: ToSql,
|
|
|
|
{
|
|
|
|
let expected = self.stmt.bind_parameter_count();
|
|
|
|
let mut index = 0;
|
|
|
|
for p in params.into_iter() {
|
|
|
|
index += 1; // The leftmost SQL parameter has an index of 1.
|
|
|
|
if index > expected {
|
|
|
|
break;
|
|
|
|
}
|
2018-10-31 03:11:35 +08:00
|
|
|
self.bind_parameter(&p, index)?;
|
2018-09-16 15:49:23 +08:00
|
|
|
}
|
2020-04-07 03:47:35 +08:00
|
|
|
if index != expected {
|
2020-07-01 20:16:15 +08:00
|
|
|
Err(Error::InvalidParameterCount(index, expected))
|
2020-04-07 03:47:35 +08:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-11-03 15:34:08 +08:00
|
|
|
pub(crate) fn bind_parameters_named<T: ?Sized + ToSql>(
|
|
|
|
&mut self,
|
|
|
|
params: &[(&str, &T)],
|
|
|
|
) -> Result<()> {
|
2017-03-09 00:20:43 +08:00
|
|
|
for &(name, value) in params {
|
2018-10-31 03:11:35 +08:00
|
|
|
if let Some(i) = self.parameter_index(name)? {
|
2020-11-03 15:34:08 +08:00
|
|
|
let ts: &dyn ToSql = &value;
|
|
|
|
self.bind_parameter(ts, i)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
} else {
|
|
|
|
return Err(Error::InvalidParameterName(name.into()));
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 00:20:43 +08:00
|
|
|
Ok(())
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 01:44:00 +08:00
|
|
|
/// Return the number of parameters that can be bound to this statement.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-07 01:44:00 +08:00
|
|
|
pub fn parameter_count(&self) -> usize {
|
|
|
|
self.stmt.bind_parameter_count()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Low level API to directly bind a parameter to a given index.
|
|
|
|
///
|
2020-11-22 16:34:03 +08:00
|
|
|
/// Note that the index is one-based, that is, the first parameter index is
|
2020-04-07 01:44:00 +08:00
|
|
|
/// 1 and not 0. This is consistent with the SQLite API and the values given
|
|
|
|
/// to parameters bound as `?NNN`.
|
|
|
|
///
|
|
|
|
/// The valid values for `one_based_col_index` begin at `1`, and end at
|
|
|
|
/// [`Statement::parameter_count`], inclusive.
|
|
|
|
///
|
|
|
|
/// # Caveats
|
|
|
|
///
|
|
|
|
/// This should not generally be used, but is available for special cases
|
|
|
|
/// such as:
|
|
|
|
///
|
|
|
|
/// - binding parameters where a gap exists.
|
|
|
|
/// - binding named and positional parameters in the same query.
|
|
|
|
/// - separating parameter binding from query execution.
|
|
|
|
///
|
|
|
|
/// Statements that have had their parameters bound this way should be
|
|
|
|
/// queried or executed by [`Statement::raw_query`] or
|
|
|
|
/// [`Statement::raw_execute`]. Other functions are not guaranteed to work.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # use rusqlite::{Connection, Result};
|
|
|
|
/// fn query(conn: &Connection) -> Result<()> {
|
|
|
|
/// let mut stmt = conn.prepare("SELECT * FROM test WHERE name = :name AND value > ?2")?;
|
|
|
|
/// let name_index = stmt.parameter_index(":name")?.expect("No such parameter");
|
|
|
|
/// stmt.raw_bind_parameter(name_index, "foo")?;
|
|
|
|
/// stmt.raw_bind_parameter(2, 100)?;
|
|
|
|
/// let mut rows = stmt.raw_query();
|
|
|
|
/// while let Some(row) = rows.next()? {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-07 01:44:00 +08:00
|
|
|
pub fn raw_bind_parameter<T: ToSql>(
|
|
|
|
&mut self,
|
|
|
|
one_based_col_index: usize,
|
|
|
|
param: T,
|
|
|
|
) -> Result<()> {
|
|
|
|
// This is the same as `bind_parameter` but slightly more ergonomic and
|
|
|
|
// correctly takes `&mut self`.
|
|
|
|
self.bind_parameter(¶m, one_based_col_index)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Low level API to execute a statement given that all parameters were
|
|
|
|
/// bound explicitly with the [`Statement::raw_bind_parameter`] API.
|
|
|
|
///
|
|
|
|
/// # Caveats
|
|
|
|
///
|
|
|
|
/// Any unbound parameters will have `NULL` as their value.
|
|
|
|
///
|
|
|
|
/// This should not generally be used outside of special cases, and
|
|
|
|
/// functions in the [`Statement::execute`] family should be preferred.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if the executed statement returns rows (in which case
|
|
|
|
/// `query` should be used instead), or the underlying SQLite call fails.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-07 01:44:00 +08:00
|
|
|
pub fn raw_execute(&mut self) -> Result<usize> {
|
|
|
|
self.execute_with_bound_parameters()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Low level API to get `Rows` for this query given that all parameters
|
|
|
|
/// were bound explicitly with the [`Statement::raw_bind_parameter`] API.
|
|
|
|
///
|
|
|
|
/// # Caveats
|
|
|
|
///
|
|
|
|
/// Any unbound parameters will have `NULL` as their value.
|
|
|
|
///
|
|
|
|
/// This should not generally be used outside of special cases, and
|
|
|
|
/// functions in the [`Statement::query`] family should be preferred.
|
|
|
|
///
|
|
|
|
/// Note that if the SQL does not return results, [`Statement::raw_execute`]
|
|
|
|
/// should be used instead.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-04-07 01:44:00 +08:00
|
|
|
pub fn raw_query(&mut self) -> Rows<'_> {
|
|
|
|
Rows::new(self)
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
// generic because many of these branches can constant fold away.
|
|
|
|
fn bind_parameter<P: ?Sized + ToSql>(&self, param: &P, col: usize) -> Result<()> {
|
2018-10-31 03:11:35 +08:00
|
|
|
let value = param.to_sql()?;
|
2017-03-08 23:48:14 +08:00
|
|
|
|
|
|
|
let ptr = unsafe { self.stmt.ptr() };
|
|
|
|
let value = match value {
|
|
|
|
ToSqlOutput::Borrowed(v) => v,
|
|
|
|
ToSqlOutput::Owned(ref v) => ValueRef::from(v),
|
|
|
|
|
|
|
|
#[cfg(feature = "blob")]
|
|
|
|
ToSqlOutput::ZeroBlob(len) => {
|
2018-08-11 18:48:21 +08:00
|
|
|
return self
|
|
|
|
.conn
|
|
|
|
.decode_result(unsafe { ffi::sqlite3_bind_zeroblob(ptr, col as c_int, len) });
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
2018-06-10 18:16:54 +08:00
|
|
|
#[cfg(feature = "array")]
|
|
|
|
ToSqlOutput::Array(a) => {
|
2018-08-11 19:37:56 +08:00
|
|
|
return self.conn.decode_result(unsafe {
|
|
|
|
ffi::sqlite3_bind_pointer(
|
|
|
|
ptr,
|
|
|
|
col as c_int,
|
|
|
|
Rc::into_raw(a) as *mut c_void,
|
|
|
|
ARRAY_TYPE,
|
|
|
|
Some(free_array),
|
|
|
|
)
|
|
|
|
});
|
2018-06-10 18:16:54 +08:00
|
|
|
}
|
2017-03-08 23:48:14 +08:00
|
|
|
};
|
2018-08-11 18:48:21 +08:00
|
|
|
self.conn.decode_result(match value {
|
|
|
|
ValueRef::Null => unsafe { ffi::sqlite3_bind_null(ptr, col as c_int) },
|
|
|
|
ValueRef::Integer(i) => unsafe { ffi::sqlite3_bind_int64(ptr, col as c_int, i) },
|
|
|
|
ValueRef::Real(r) => unsafe { ffi::sqlite3_bind_double(ptr, col as c_int, r) },
|
|
|
|
ValueRef::Text(s) => unsafe {
|
2019-02-27 11:38:41 +08:00
|
|
|
let (c_str, len, destructor) = str_for_sqlite(s)?;
|
2019-03-10 18:12:14 +08:00
|
|
|
ffi::sqlite3_bind_text(ptr, col as c_int, c_str, len, destructor)
|
2017-03-08 23:48:14 +08:00
|
|
|
},
|
2018-08-11 18:48:21 +08:00
|
|
|
ValueRef::Blob(b) => unsafe {
|
2019-02-27 11:38:41 +08:00
|
|
|
let length = len_as_c_int(b.len())?;
|
|
|
|
if length == 0 {
|
2018-05-24 03:04:13 +08:00
|
|
|
ffi::sqlite3_bind_zeroblob(ptr, col as c_int, 0)
|
2017-03-08 23:48:14 +08:00
|
|
|
} else {
|
2018-08-11 18:48:21 +08:00
|
|
|
ffi::sqlite3_bind_blob(
|
|
|
|
ptr,
|
|
|
|
col as c_int,
|
|
|
|
b.as_ptr() as *const c_void,
|
2019-02-27 11:38:41 +08:00
|
|
|
length,
|
2018-08-11 18:48:21 +08:00
|
|
|
ffi::SQLITE_TRANSIENT(),
|
|
|
|
)
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
},
|
2018-06-10 18:16:54 +08:00
|
|
|
})
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2018-05-24 03:23:28 +08:00
|
|
|
fn execute_with_bound_parameters(&mut self) -> Result<usize> {
|
2019-08-27 02:21:23 +08:00
|
|
|
self.check_update()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
let r = self.stmt.step();
|
|
|
|
self.stmt.reset();
|
|
|
|
match r {
|
2020-03-05 03:26:31 +08:00
|
|
|
ffi::SQLITE_DONE => Ok(self.conn.changes()),
|
2017-03-09 00:20:43 +08:00
|
|
|
ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults),
|
|
|
|
_ => Err(self.conn.decode_result(r).unwrap_err()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2017-03-09 00:20:43 +08:00
|
|
|
fn finalize_(&mut self) -> Result<()> {
|
2020-06-27 01:35:14 +08:00
|
|
|
let mut stmt = unsafe { RawStatement::new(ptr::null_mut(), 0) };
|
2017-03-09 00:20:43 +08:00
|
|
|
mem::swap(&mut stmt, &mut self.stmt);
|
|
|
|
self.conn.decode_result(stmt.finalize())
|
|
|
|
}
|
2018-08-11 02:48:13 +08:00
|
|
|
|
2020-01-15 00:11:36 +08:00
|
|
|
#[cfg(all(feature = "modern_sqlite", feature = "extra_check"))]
|
2019-08-27 02:21:23 +08:00
|
|
|
#[inline]
|
|
|
|
fn check_update(&self) -> Result<()> {
|
2020-03-05 03:26:31 +08:00
|
|
|
// sqlite3_column_count works for DML but not for DDL (ie ALTER)
|
2020-06-25 01:33:34 +08:00
|
|
|
if self.column_count() > 0 && self.stmt.readonly() {
|
2019-08-27 02:21:23 +08:00
|
|
|
return Err(Error::ExecuteReturnedResults);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:11:36 +08:00
|
|
|
#[cfg(all(not(feature = "modern_sqlite"), feature = "extra_check"))]
|
2019-08-27 02:21:23 +08:00
|
|
|
#[inline]
|
|
|
|
fn check_update(&self) -> Result<()> {
|
2020-03-05 03:26:31 +08:00
|
|
|
// sqlite3_column_count works for DML but not for DDL (ie ALTER)
|
2019-08-27 02:21:23 +08:00
|
|
|
if self.column_count() > 0 {
|
|
|
|
return Err(Error::ExecuteReturnedResults);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "extra_check"))]
|
|
|
|
#[inline]
|
2021-02-28 20:19:58 +08:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2019-08-27 02:21:23 +08:00
|
|
|
fn check_update(&self) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-17 00:29:46 +08:00
|
|
|
/// Returns a string containing the SQL text of prepared statement with
|
|
|
|
/// bound parameters expanded.
|
2020-01-15 00:11:36 +08:00
|
|
|
#[cfg(feature = "modern_sqlite")]
|
2021-06-13 15:17:35 +08:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "modern_sqlite")))]
|
2019-07-28 14:53:26 +08:00
|
|
|
pub fn expanded_sql(&self) -> Option<String> {
|
2020-04-16 22:24:03 +08:00
|
|
|
self.stmt
|
|
|
|
.expanded_sql()
|
|
|
|
.map(|s| s.to_string_lossy().to_string())
|
2018-08-11 02:52:11 +08:00
|
|
|
}
|
2018-10-28 17:28:19 +08:00
|
|
|
|
2019-01-25 13:43:39 +08:00
|
|
|
/// Get the value for one of the status counters for this statement.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2019-01-25 13:43:39 +08:00
|
|
|
pub fn get_status(&self, status: StatementStatus) -> i32 {
|
|
|
|
self.stmt.get_status(status, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reset the value of one of the status counters for this statement,
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2019-01-25 13:43:39 +08:00
|
|
|
/// returning the value it had before resetting.
|
|
|
|
pub fn reset_status(&self, status: StatementStatus) -> i32 {
|
|
|
|
self.stmt.get_status(status, true)
|
|
|
|
}
|
2019-02-02 20:22:40 +08:00
|
|
|
|
2019-08-27 02:43:39 +08:00
|
|
|
#[cfg(feature = "extra_check")]
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2018-10-28 17:28:19 +08:00
|
|
|
pub(crate) fn check_no_tail(&self) -> Result<()> {
|
|
|
|
if self.stmt.has_tail() {
|
|
|
|
Err(Error::MultipleStatement)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 02:43:39 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "extra_check"))]
|
|
|
|
#[inline]
|
2021-02-28 20:19:58 +08:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2019-08-27 02:43:39 +08:00
|
|
|
pub(crate) fn check_no_tail(&self) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-06-07 08:27:14 +08:00
|
|
|
/// Safety: This is unsafe, because using `sqlite3_stmt` after the
|
|
|
|
/// connection has closed is illegal, but `RawStatement` does not enforce
|
|
|
|
/// this, as it loses our protective `'conn` lifetime bound.
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-06-07 08:27:14 +08:00
|
|
|
pub(crate) unsafe fn into_raw(mut self) -> RawStatement {
|
2020-06-27 01:35:14 +08:00
|
|
|
let mut stmt = RawStatement::new(ptr::null_mut(), 0);
|
2017-03-09 00:20:43 +08:00
|
|
|
mem::swap(&mut stmt, &mut self.stmt);
|
|
|
|
stmt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl fmt::Debug for Statement<'_> {
|
2018-12-08 04:57:04 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-10-30 02:24:18 +08:00
|
|
|
let sql = if self.stmt.is_null() {
|
|
|
|
Ok("")
|
|
|
|
} else {
|
|
|
|
str::from_utf8(self.stmt.sql().unwrap().to_bytes())
|
|
|
|
};
|
2017-03-09 00:20:43 +08:00
|
|
|
f.debug_struct("Statement")
|
|
|
|
.field("conn", self.conn)
|
|
|
|
.field("stmt", &self.stmt)
|
|
|
|
.field("sql", &sql)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl Drop for Statement<'_> {
|
2017-03-09 00:20:43 +08:00
|
|
|
#[allow(unused_must_use)]
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2017-03-09 00:20:43 +08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.finalize_();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:02:38 +08:00
|
|
|
impl Statement<'_> {
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-05-16 16:18:25 +08:00
|
|
|
pub(super) fn new(conn: &Connection, stmt: RawStatement) -> Statement<'_> {
|
2018-08-11 18:48:21 +08:00
|
|
|
Statement { conn, stmt }
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:18:25 +08:00
|
|
|
pub(super) fn value_ref(&self, col: usize) -> ValueRef<'_> {
|
2017-03-08 23:48:14 +08:00
|
|
|
let raw = unsafe { self.stmt.ptr() };
|
|
|
|
|
2020-03-25 01:35:24 +08:00
|
|
|
match self.stmt.column_type(col) {
|
2017-03-08 23:48:14 +08:00
|
|
|
ffi::SQLITE_NULL => ValueRef::Null,
|
|
|
|
ffi::SQLITE_INTEGER => {
|
2018-05-24 03:04:13 +08:00
|
|
|
ValueRef::Integer(unsafe { ffi::sqlite3_column_int64(raw, col as c_int) })
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
2018-08-11 18:48:21 +08:00
|
|
|
ffi::SQLITE_FLOAT => {
|
|
|
|
ValueRef::Real(unsafe { ffi::sqlite3_column_double(raw, col as c_int) })
|
|
|
|
}
|
2020-03-25 01:35:24 +08:00
|
|
|
ffi::SQLITE_TEXT => {
|
|
|
|
let s = unsafe {
|
2020-03-26 02:20:05 +08:00
|
|
|
// Quoting from "Using SQLite" book:
|
2020-04-11 21:03:24 +08:00
|
|
|
// To avoid problems, an application should first extract the desired type using
|
|
|
|
// a sqlite3_column_xxx() function, and then call the
|
|
|
|
// appropriate sqlite3_column_bytes() function.
|
2020-03-25 01:35:24 +08:00
|
|
|
let text = ffi::sqlite3_column_text(raw, col as c_int);
|
2020-03-26 02:20:05 +08:00
|
|
|
let len = ffi::sqlite3_column_bytes(raw, col as c_int);
|
2020-03-25 01:35:24 +08:00
|
|
|
assert!(
|
|
|
|
!text.is_null(),
|
|
|
|
"unexpected SQLITE_TEXT column type with NULL data"
|
|
|
|
);
|
2020-03-26 02:20:05 +08:00
|
|
|
from_raw_parts(text as *const u8, len as usize)
|
2020-03-25 01:35:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ValueRef::Text(s)
|
|
|
|
}
|
|
|
|
ffi::SQLITE_BLOB => {
|
2017-03-08 23:48:14 +08:00
|
|
|
let (blob, len) = unsafe {
|
2018-08-11 18:48:21 +08:00
|
|
|
(
|
|
|
|
ffi::sqlite3_column_blob(raw, col as c_int),
|
|
|
|
ffi::sqlite3_column_bytes(raw, col as c_int),
|
|
|
|
)
|
2017-03-08 23:48:14 +08:00
|
|
|
};
|
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(
|
|
|
|
len >= 0,
|
|
|
|
"unexpected negative return from sqlite3_column_bytes"
|
|
|
|
);
|
2020-03-25 01:35:24 +08:00
|
|
|
if len > 0 {
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(
|
|
|
|
!blob.is_null(),
|
2020-03-25 01:35:24 +08:00
|
|
|
"unexpected SQLITE_BLOB column type with NULL data"
|
2018-08-11 18:48:21 +08:00
|
|
|
);
|
2020-03-25 01:35:24 +08:00
|
|
|
ValueRef::Blob(unsafe { from_raw_parts(blob as *const u8, len as usize) })
|
2017-03-08 23:48:14 +08:00
|
|
|
} else {
|
|
|
|
// The return value from sqlite3_column_blob() for a zero-length BLOB
|
|
|
|
// is a NULL pointer.
|
2020-03-25 01:35:24 +08:00
|
|
|
ValueRef::Blob(&[])
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!("sqlite3_column_type returned invalid value"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-05-16 16:18:25 +08:00
|
|
|
pub(super) fn step(&self) -> Result<bool> {
|
2017-03-09 00:04:22 +08:00
|
|
|
match self.stmt.step() {
|
|
|
|
ffi::SQLITE_ROW => Ok(true),
|
|
|
|
ffi::SQLITE_DONE => Ok(false),
|
|
|
|
code => Err(self.conn.decode_result(code).unwrap_err()),
|
|
|
|
}
|
2017-03-08 23:48:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:10:23 +08:00
|
|
|
#[inline]
|
2020-05-16 16:18:25 +08:00
|
|
|
pub(super) fn reset(&self) -> c_int {
|
2017-03-08 23:48:14 +08:00
|
|
|
self.stmt.reset()
|
|
|
|
}
|
|
|
|
}
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2019-01-25 13:43:39 +08:00
|
|
|
/// Prepared statement status counters.
|
|
|
|
///
|
2020-11-07 19:32:41 +08:00
|
|
|
/// See `https://www.sqlite.org/c3ref/c_stmtstatus_counter.html`
|
2019-01-25 13:43:39 +08:00
|
|
|
/// for explanations of each.
|
|
|
|
///
|
|
|
|
/// Note that depending on your version of SQLite, all of these
|
|
|
|
/// may not be available.
|
|
|
|
#[repr(i32)]
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
2020-04-07 03:01:39 +08:00
|
|
|
#[non_exhaustive]
|
2019-01-25 13:43:39 +08:00
|
|
|
pub enum StatementStatus {
|
|
|
|
/// Equivalent to SQLITE_STMTSTATUS_FULLSCAN_STEP
|
|
|
|
FullscanStep = 1,
|
|
|
|
/// Equivalent to SQLITE_STMTSTATUS_SORT
|
|
|
|
Sort = 2,
|
|
|
|
/// Equivalent to SQLITE_STMTSTATUS_AUTOINDEX
|
|
|
|
AutoIndex = 3,
|
|
|
|
/// Equivalent to SQLITE_STMTSTATUS_VM_STEP
|
|
|
|
VmStep = 4,
|
|
|
|
/// Equivalent to SQLITE_STMTSTATUS_REPREPARE
|
|
|
|
RePrepare = 5,
|
|
|
|
/// Equivalent to SQLITE_STMTSTATUS_RUN
|
|
|
|
Run = 6,
|
|
|
|
/// Equivalent to SQLITE_STMTSTATUS_MEMUSED
|
|
|
|
MemUsed = 99,
|
|
|
|
}
|
|
|
|
|
2017-03-09 00:20:43 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2019-01-26 15:03:59 +08:00
|
|
|
use crate::types::ToSql;
|
2020-11-03 17:32:46 +08:00
|
|
|
use crate::{params_from_iter, Connection, Error, Result};
|
2017-03-09 00:20:43 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-11-03 15:34:08 +08:00
|
|
|
#[allow(deprecated)]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_execute_named() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
|
|
|
db.execute_batch("CREATE TABLE foo(x INTEGER)")?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
assert_eq!(
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &1i32)])?,
|
2018-08-11 18:48:21 +08:00
|
|
|
1
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute("INSERT INTO foo(x) VALUES (:x)", &[(":x", &2i32)])?,
|
2018-08-11 18:48:21 +08:00
|
|
|
1
|
|
|
|
);
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(
|
|
|
|
db.execute(
|
|
|
|
"INSERT INTO foo(x) VALUES (:x)",
|
|
|
|
crate::named_params! {":x": 3i32}
|
2020-11-06 05:14:00 +08:00
|
|
|
)?,
|
2020-11-03 15:34:08 +08:00
|
|
|
1
|
|
|
|
);
|
2018-08-11 18:48:21 +08:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-11-03 15:34:08 +08:00
|
|
|
6i32,
|
2018-08-11 18:48:21 +08:00
|
|
|
db.query_row_named::<i32, _>(
|
|
|
|
"SELECT SUM(x) FROM foo WHERE x > :x",
|
|
|
|
&[(":x", &0i32)],
|
|
|
|
|r| r.get(0)
|
2020-11-06 05:14:00 +08:00
|
|
|
)?
|
2018-08-11 18:48:21 +08:00
|
|
|
);
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(
|
|
|
|
5i32,
|
|
|
|
db.query_row::<i32, _, _>(
|
|
|
|
"SELECT SUM(x) FROM foo WHERE x > :x",
|
|
|
|
&[(":x", &1i32)],
|
|
|
|
|r| r.get(0)
|
2020-11-06 05:14:00 +08:00
|
|
|
)?
|
2020-11-03 15:34:08 +08:00
|
|
|
);
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-03 15:34:08 +08:00
|
|
|
#[allow(deprecated)]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_stmt_execute_named() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag \
|
|
|
|
INTEGER)";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut stmt = db.prepare("INSERT INTO test (name) VALUES (:name)")?;
|
|
|
|
stmt.execute_named(&[(":name", &"one")])?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut stmt = db.prepare("SELECT COUNT(*) FROM test WHERE name = :name")?;
|
2018-08-11 18:48:21 +08:00
|
|
|
assert_eq!(
|
|
|
|
1i32,
|
2020-11-06 05:14:00 +08:00
|
|
|
stmt.query_row_named::<i32, _>(&[(":name", &"one")], |r| r.get(0))?
|
2018-08-11 18:48:21 +08:00
|
|
|
);
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(
|
|
|
|
1i32,
|
2021-01-20 04:16:08 +08:00
|
|
|
stmt.query_row::<i32, _, _>(&[(":name", "one")], |r| r.get(0))?
|
2020-11-03 15:34:08 +08:00
|
|
|
);
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-03 15:34:08 +08:00
|
|
|
#[allow(deprecated)]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_query_named() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
let sql = r#"
|
|
|
|
CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER);
|
|
|
|
INSERT INTO test(id, name) VALUES (1, "one");
|
|
|
|
"#;
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut stmt = db.prepare("SELECT id FROM test where name = :name")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
// legacy `_named` api
|
|
|
|
{
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut rows = stmt.query_named(&[(":name", &"one")])?;
|
|
|
|
let id: Result<i32> = rows.next()?.unwrap().get(0);
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(Ok(1), id);
|
|
|
|
}
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-03 15:34:08 +08:00
|
|
|
// plain api
|
|
|
|
{
|
2021-01-20 04:16:08 +08:00
|
|
|
let mut rows = stmt.query(&[(":name", "one")])?;
|
2020-11-06 05:14:00 +08:00
|
|
|
let id: Result<i32> = rows.next()?.unwrap().get(0);
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(Ok(1), id);
|
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-03 15:34:08 +08:00
|
|
|
#[allow(deprecated)]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_query_map_named() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
let sql = r#"
|
|
|
|
CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER);
|
|
|
|
INSERT INTO test(id, name) VALUES (1, "one");
|
|
|
|
"#;
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut stmt = db.prepare("SELECT id FROM test where name = :name")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
// legacy `_named` api
|
|
|
|
{
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut rows = stmt.query_map_named(&[(":name", &"one")], |row| {
|
|
|
|
let id: Result<i32> = row.get(0);
|
|
|
|
id.map(|i| 2 * i)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let doubled_id: i32 = rows.next().unwrap()?;
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(2, doubled_id);
|
|
|
|
}
|
|
|
|
// plain api
|
|
|
|
{
|
2021-01-20 04:16:08 +08:00
|
|
|
let mut rows = stmt.query_map(&[(":name", "one")], |row| {
|
2020-11-06 05:14:00 +08:00
|
|
|
let id: Result<i32> = row.get(0);
|
|
|
|
id.map(|i| 2 * i)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let doubled_id: i32 = rows.next().unwrap()?;
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(2, doubled_id);
|
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2020-11-03 15:34:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[allow(deprecated)]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_query_and_then_named() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2020-11-03 15:34:08 +08:00
|
|
|
let sql = r#"
|
|
|
|
CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER);
|
|
|
|
INSERT INTO test(id, name) VALUES (1, "one");
|
|
|
|
INSERT INTO test(id, name) VALUES (2, "one");
|
|
|
|
"#;
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
|
|
|
|
|
|
|
let mut stmt = db.prepare("SELECT id FROM test where name = :name ORDER BY id ASC")?;
|
|
|
|
let mut rows = stmt.query_and_then_named(&[(":name", &"one")], |row| {
|
|
|
|
let id: i32 = row.get(0)?;
|
|
|
|
if id == 1 {
|
|
|
|
Ok(id)
|
|
|
|
} else {
|
|
|
|
Err(Error::SqliteSingleThreadedMode)
|
|
|
|
}
|
|
|
|
})?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-03 15:34:08 +08:00
|
|
|
// first row should be Ok
|
2020-11-06 05:14:00 +08:00
|
|
|
let doubled_id: i32 = rows.next().unwrap()?;
|
2020-11-03 15:34:08 +08:00
|
|
|
assert_eq!(1, doubled_id);
|
|
|
|
|
|
|
|
// second row should be Err
|
|
|
|
#[allow(clippy::match_wild_err_arm)]
|
|
|
|
match rows.next().unwrap() {
|
|
|
|
Ok(_) => panic!("invalid Ok"),
|
|
|
|
Err(Error::SqliteSingleThreadedMode) => (),
|
|
|
|
Err(_) => panic!("invalid Err"),
|
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_query_and_then_by_name() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
let sql = r#"
|
|
|
|
CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER);
|
|
|
|
INSERT INTO test(id, name) VALUES (1, "one");
|
|
|
|
INSERT INTO test(id, name) VALUES (2, "one");
|
|
|
|
"#;
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
|
|
|
|
|
|
|
let mut stmt = db.prepare("SELECT id FROM test where name = :name ORDER BY id ASC")?;
|
2021-01-20 04:16:08 +08:00
|
|
|
let mut rows = stmt.query_and_then(&[(":name", "one")], |row| {
|
2020-11-06 05:14:00 +08:00
|
|
|
let id: i32 = row.get(0)?;
|
|
|
|
if id == 1 {
|
|
|
|
Ok(id)
|
|
|
|
} else {
|
|
|
|
Err(Error::SqliteSingleThreadedMode)
|
|
|
|
}
|
|
|
|
})?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
|
|
|
// first row should be Ok
|
2020-11-06 05:14:00 +08:00
|
|
|
let doubled_id: i32 = rows.next().unwrap()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
assert_eq!(1, doubled_id);
|
|
|
|
|
|
|
|
// second row should be Err
|
2019-02-02 18:37:26 +08:00
|
|
|
#[allow(clippy::match_wild_err_arm)]
|
2017-03-09 00:20:43 +08:00
|
|
|
match rows.next().unwrap() {
|
|
|
|
Ok(_) => panic!("invalid Ok"),
|
|
|
|
Err(Error::SqliteSingleThreadedMode) => (),
|
|
|
|
Err(_) => panic!("invalid Err"),
|
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-03 15:34:08 +08:00
|
|
|
#[allow(deprecated)]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_unbound_parameters_are_null() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
let sql = "CREATE TABLE test (x TEXT, y TEXT)";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?;
|
|
|
|
stmt.execute_named(&[(":x", &"one")])?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let result: Option<String> =
|
|
|
|
db.query_row("SELECT y FROM test WHERE x = 'one'", [], |row| row.get(0))?;
|
2017-03-09 00:20:43 +08:00
|
|
|
assert!(result.is_none());
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 01:44:00 +08:00
|
|
|
#[test]
|
|
|
|
fn test_raw_binding() -> Result<()> {
|
2020-11-06 05:14:00 +08:00
|
|
|
let db = Connection::open_in_memory()?;
|
2020-04-07 01:44:00 +08:00
|
|
|
db.execute_batch("CREATE TABLE test (name TEXT, value INTEGER)")?;
|
|
|
|
{
|
|
|
|
let mut stmt = db.prepare("INSERT INTO test (name, value) VALUES (:name, ?3)")?;
|
|
|
|
|
|
|
|
let name_idx = stmt.parameter_index(":name")?.unwrap();
|
|
|
|
stmt.raw_bind_parameter(name_idx, "example")?;
|
|
|
|
stmt.raw_bind_parameter(3, 50i32)?;
|
|
|
|
let n = stmt.raw_execute()?;
|
|
|
|
assert_eq!(n, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut stmt = db.prepare("SELECT name, value FROM test WHERE value = ?2")?;
|
|
|
|
stmt.raw_bind_parameter(2, 50)?;
|
|
|
|
let mut rows = stmt.raw_query();
|
|
|
|
{
|
|
|
|
let row = rows.next()?.unwrap();
|
|
|
|
let name: String = row.get(0)?;
|
|
|
|
assert_eq!(name, "example");
|
|
|
|
let value: i32 = row.get(1)?;
|
|
|
|
assert_eq!(value, 50);
|
|
|
|
}
|
|
|
|
assert!(rows.next()?.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-09 00:20:43 +08:00
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_unbound_parameters_are_reused() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:20:43 +08:00
|
|
|
let sql = "CREATE TABLE test (x TEXT, y TEXT)";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?;
|
2021-01-20 04:16:08 +08:00
|
|
|
stmt.execute(&[(":x", "one")])?;
|
|
|
|
stmt.execute(&[(":y", "two")])?;
|
2017-03-09 00:20:43 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
let result: String =
|
|
|
|
db.query_row("SELECT x FROM test WHERE y = 'two'", [], |row| row.get(0))?;
|
2017-03-09 00:20:43 +08:00
|
|
|
assert_eq!(result, "one");
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|
2017-03-09 00:26:25 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_insert() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
|
|
|
db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)")?;
|
|
|
|
let mut stmt = db.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?)")?;
|
2021-01-20 04:16:08 +08:00
|
|
|
assert_eq!(stmt.insert([1i32])?, 1);
|
|
|
|
assert_eq!(stmt.insert([2i32])?, 2);
|
|
|
|
match stmt.insert([1i32]).unwrap_err() {
|
2017-03-09 00:26:25 +08:00
|
|
|
Error::StatementChangedRows(0) => (),
|
|
|
|
err => panic!("Unexpected error {}", err),
|
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
let mut multi = db.prepare("INSERT INTO foo (x) SELECT 3 UNION ALL SELECT 4")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
match multi.insert([]).unwrap_err() {
|
2017-03-09 00:26:25 +08:00
|
|
|
Error::StatementChangedRows(2) => (),
|
|
|
|
err => panic!("Unexpected error {}", err),
|
|
|
|
}
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:26:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_insert_different_tables() -> Result<()> {
|
2020-04-16 12:07:23 +08:00
|
|
|
// Test for https://github.com/rusqlite/rusqlite/issues/171
|
2020-11-06 05:14:00 +08:00
|
|
|
let db = Connection::open_in_memory()?;
|
2018-08-11 18:48:21 +08:00
|
|
|
db.execute_batch(
|
|
|
|
r"
|
2017-03-09 00:26:25 +08:00
|
|
|
CREATE TABLE foo(x INTEGER);
|
|
|
|
CREATE TABLE bar(x INTEGER);
|
2018-08-11 18:48:21 +08:00
|
|
|
",
|
2020-11-06 05:14:00 +08:00
|
|
|
)?;
|
2018-08-11 18:48:21 +08:00
|
|
|
|
2020-11-06 05:14:00 +08:00
|
|
|
assert_eq!(db.prepare("INSERT INTO foo VALUES (10)")?.insert([])?, 1);
|
|
|
|
assert_eq!(db.prepare("INSERT INTO bar VALUES (10)")?.insert([])?, 1);
|
|
|
|
Ok(())
|
2017-03-09 00:26:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_exists() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:26:25 +08:00
|
|
|
let sql = "BEGIN;
|
|
|
|
CREATE TABLE foo(x INTEGER);
|
|
|
|
INSERT INTO foo VALUES(1);
|
|
|
|
INSERT INTO foo VALUES(2);
|
|
|
|
END;";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
|
|
|
let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?")?;
|
|
|
|
assert!(stmt.exists([1i32])?);
|
2021-01-20 04:16:08 +08:00
|
|
|
assert!(stmt.exists([2i32])?);
|
|
|
|
assert!(!stmt.exists([0i32])?);
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-03-09 00:26:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_query_row() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2017-03-09 00:26:25 +08:00
|
|
|
let sql = "BEGIN;
|
|
|
|
CREATE TABLE foo(x INTEGER, y INTEGER);
|
|
|
|
INSERT INTO foo VALUES(1, 3);
|
|
|
|
INSERT INTO foo VALUES(2, 4);
|
|
|
|
END;";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
|
|
|
let mut stmt = db.prepare("SELECT y FROM foo WHERE x = ?")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
let y: Result<i64> = stmt.query_row([1i32], |r| r.get(0));
|
2020-11-06 05:14:00 +08:00
|
|
|
assert_eq!(3i64, y?);
|
|
|
|
Ok(())
|
2017-03-09 00:26:25 +08:00
|
|
|
}
|
2018-03-28 02:07:46 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_query_by_column_name() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2018-03-28 02:07:46 +08:00
|
|
|
let sql = "BEGIN;
|
|
|
|
CREATE TABLE foo(x INTEGER, y INTEGER);
|
|
|
|
INSERT INTO foo VALUES(1, 3);
|
|
|
|
END;";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
|
|
|
let mut stmt = db.prepare("SELECT y FROM foo")?;
|
2020-11-03 15:34:08 +08:00
|
|
|
let y: Result<i64> = stmt.query_row([], |r| r.get("y"));
|
2020-11-06 05:14:00 +08:00
|
|
|
assert_eq!(3i64, y?);
|
|
|
|
Ok(())
|
2018-03-28 02:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_query_by_column_name_ignore_case() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2018-03-28 02:07:46 +08:00
|
|
|
let sql = "BEGIN;
|
|
|
|
CREATE TABLE foo(x INTEGER, y INTEGER);
|
|
|
|
INSERT INTO foo VALUES(1, 3);
|
|
|
|
END;";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch(sql)?;
|
|
|
|
let mut stmt = db.prepare("SELECT y as Y FROM foo")?;
|
2020-11-03 17:32:46 +08:00
|
|
|
let y: Result<i64> = stmt.query_row([], |r| r.get("y"));
|
2020-11-06 05:14:00 +08:00
|
|
|
assert_eq!(3i64, y?);
|
|
|
|
Ok(())
|
2018-03-28 02:07:46 +08:00
|
|
|
}
|
2018-08-11 02:52:11 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-01-15 00:11:36 +08:00
|
|
|
#[cfg(feature = "modern_sqlite")]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_expanded_sql() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
|
|
|
let stmt = db.prepare("SELECT ?")?;
|
|
|
|
stmt.bind_parameter(&1, 1)?;
|
2019-07-28 14:53:26 +08:00
|
|
|
assert_eq!(Some("SELECT 1".to_owned()), stmt.expanded_sql());
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2018-08-11 02:52:11 +08:00
|
|
|
}
|
2019-01-26 15:03:59 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_bind_parameters() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2019-01-26 15:03:59 +08:00
|
|
|
// dynamic slice:
|
|
|
|
db.query_row(
|
|
|
|
"SELECT ?1, ?2, ?3",
|
2019-01-26 17:55:14 +08:00
|
|
|
&[&1u8 as &dyn ToSql, &"one", &Some("one")],
|
2019-01-26 15:03:59 +08:00
|
|
|
|row| row.get::<_, u8>(0),
|
2020-11-06 05:14:00 +08:00
|
|
|
)?;
|
2019-01-26 15:03:59 +08:00
|
|
|
// existing collection:
|
|
|
|
let data = vec![1, 2, 3];
|
2020-11-03 15:34:08 +08:00
|
|
|
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(&data), |row| {
|
|
|
|
row.get::<_, u8>(0)
|
2020-11-06 05:14:00 +08:00
|
|
|
})?;
|
2020-11-03 15:34:08 +08:00
|
|
|
db.query_row(
|
|
|
|
"SELECT ?1, ?2, ?3",
|
|
|
|
params_from_iter(data.as_slice()),
|
|
|
|
|row| row.get::<_, u8>(0),
|
2020-11-06 05:14:00 +08:00
|
|
|
)?;
|
2020-11-03 15:34:08 +08:00
|
|
|
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(data), |row| {
|
2019-01-26 15:03:59 +08:00
|
|
|
row.get::<_, u8>(0)
|
2020-11-06 05:14:00 +08:00
|
|
|
})?;
|
2019-01-26 15:03:59 +08:00
|
|
|
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
let data: BTreeSet<String> = ["one", "two", "three"]
|
2019-02-02 18:37:26 +08:00
|
|
|
.iter()
|
2019-11-03 18:19:07 +08:00
|
|
|
.map(|s| (*s).to_string())
|
2019-01-26 15:03:59 +08:00
|
|
|
.collect();
|
2020-11-03 15:34:08 +08:00
|
|
|
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(&data), |row| {
|
|
|
|
row.get::<_, String>(0)
|
2020-11-06 05:14:00 +08:00
|
|
|
})?;
|
2019-01-26 15:03:59 +08:00
|
|
|
|
|
|
|
let data = [0; 3];
|
2020-11-03 15:34:08 +08:00
|
|
|
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(&data), |row| {
|
|
|
|
row.get::<_, u8>(0)
|
2020-11-06 05:14:00 +08:00
|
|
|
})?;
|
2020-11-03 15:34:08 +08:00
|
|
|
db.query_row("SELECT ?1, ?2, ?3", params_from_iter(data.iter()), |row| {
|
|
|
|
row.get::<_, u8>(0)
|
2020-11-06 05:14:00 +08:00
|
|
|
})?;
|
|
|
|
Ok(())
|
2019-01-26 15:03:59 +08:00
|
|
|
}
|
2019-10-31 03:08:56 +08:00
|
|
|
|
2021-04-26 07:53:37 +08:00
|
|
|
#[test]
|
|
|
|
fn test_parameter_name() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
|
|
|
db.execute_batch("CREATE TABLE test (name TEXT, value INTEGER)")?;
|
|
|
|
let stmt = db.prepare("INSERT INTO test (name, value) VALUES (:name, ?3)")?;
|
|
|
|
assert_eq!(stmt.parameter_name(0), None);
|
|
|
|
assert_eq!(stmt.parameter_name(1), Some(":name"));
|
|
|
|
assert_eq!(stmt.parameter_name(2), None);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-31 03:08:56 +08:00
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_empty_stmt() -> Result<()> {
|
|
|
|
let conn = Connection::open_in_memory()?;
|
|
|
|
let mut stmt = conn.prepare("")?;
|
2019-10-31 03:08:56 +08:00
|
|
|
assert_eq!(0, stmt.column_count());
|
|
|
|
assert!(stmt.parameter_index("test").is_ok());
|
|
|
|
assert!(stmt.step().is_err());
|
|
|
|
stmt.reset();
|
2020-11-03 17:32:46 +08:00
|
|
|
assert!(stmt.execute([]).is_err());
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2019-10-31 03:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_comment_stmt() -> Result<()> {
|
|
|
|
let conn = Connection::open_in_memory()?;
|
|
|
|
conn.prepare("/*SELECT 1;*/")?;
|
|
|
|
Ok(())
|
2019-10-31 03:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_comment_and_sql_stmt() -> Result<()> {
|
|
|
|
let conn = Connection::open_in_memory()?;
|
|
|
|
let stmt = conn.prepare("/*...*/ SELECT 1;")?;
|
2019-10-31 03:08:56 +08:00
|
|
|
assert_eq!(1, stmt.column_count());
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2019-10-31 03:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_semi_colon_stmt() -> Result<()> {
|
|
|
|
let conn = Connection::open_in_memory()?;
|
|
|
|
let stmt = conn.prepare(";")?;
|
2019-10-31 03:08:56 +08:00
|
|
|
assert_eq!(0, stmt.column_count());
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2019-10-31 03:08:56 +08:00
|
|
|
}
|
2020-03-26 02:20:05 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_utf16_conversion() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
|
|
|
db.pragma_update(None, "encoding", &"UTF-16le")?;
|
|
|
|
let encoding: String = db.pragma_query_value(None, "encoding", |row| row.get(0))?;
|
2020-03-26 02:20:05 +08:00
|
|
|
assert_eq!("UTF-16le", encoding);
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute_batch("CREATE TABLE foo(x TEXT)")?;
|
2020-03-26 02:20:05 +08:00
|
|
|
let expected = "テスト";
|
2020-11-06 05:14:00 +08:00
|
|
|
db.execute("INSERT INTO foo(x) VALUES (?)", &[&expected])?;
|
|
|
|
let actual: String = db.query_row("SELECT x FROM foo", [], |row| row.get(0))?;
|
2020-03-26 02:20:05 +08:00
|
|
|
assert_eq!(expected, actual);
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2020-03-26 02:20:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_nul_byte() -> Result<()> {
|
|
|
|
let db = Connection::open_in_memory()?;
|
2020-03-26 02:20:05 +08:00
|
|
|
let expected = "a\x00b";
|
2020-11-06 05:14:00 +08:00
|
|
|
let actual: String = db.query_row("SELECT ?", [expected], |row| row.get(0))?;
|
2020-03-26 02:20:05 +08:00
|
|
|
assert_eq!(expected, actual);
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2020-03-26 02:20:05 +08:00
|
|
|
}
|
2017-03-09 00:20:43 +08:00
|
|
|
}
|