Trait Params

Source
pub trait Params {
    // Required method
    fn bind_params(self, stmt: &mut Statement) -> Result<()>;
}
Expand description

Trait for collections of parameters to a query.

This is a private trait with no public API. There are existing implementations which should cover most use cases:

  • An empty tuple (()) binds no parameters to the query.
  • An array binds parameters that are all the same type.
  • The params! macro binds parameters of arbitrary types.
  • A closure can arbitrarily bind parameters.

Named parameters are implemented by using a tuple of ("name", value), and can be in any order. See params! for an example.

§Using a closure

If you are dynamically creating SQL queries and need to dynamically bind parameters to them, you can use a closure to accomplish this.

use sqlite3_ext::{Connection, Result, query::{ Statement, ToParam }};

fn do_thing(conn: &Connection) -> Result<i64> {
    conn.prepare("INSERT INTO tbl VALUES (?, ?)")?
        .execute(|stmt: &mut Statement| {
            "foo".bind_param(stmt, 1)?;
            "bar".bind_param(stmt, 2)?;
            Ok(())
        })
}

Required Methods§

Source

fn bind_params(self, stmt: &mut Statement) -> Result<()>

Implementations on Foreign Types§

Source§

impl Params for &mut [&mut ValueRef]

Source§

fn bind_params(self, stmt: &mut Statement) -> Result<()>

Source§

impl Params for ()

Source§

fn bind_params(self, _: &mut Statement) -> Result<()>

Source§

impl<T: ToParam> Params for Vec<T>

Source§

fn bind_params(self, stmt: &mut Statement) -> Result<()>

Source§

impl<T: ToParam, const N: usize> Params for [T; N]

Source§

fn bind_params(self, stmt: &mut Statement) -> Result<()>

Implementors§

Source§

impl<T> Params for T
where T: FnOnce(&mut Statement) -> Result<()>,