Skip to main content

Prepared

Struct Prepared 

Source
pub struct Prepared { /* private fields */ }
Expand description

A prepared statement.

The statement is parsed here and bound at each execution, with the values in hand. That is the opposite of the usual arrangement, where a prepared statement is planned once and the values are pushed into the plan, and it is on purpose for now: an analytical query is planned against the data it reads, so a plan built without knowing that $1 is 1 or 1000000 is a plan built blind. Parsing is the part that is pure overhead, and that happens once.

What a parameter can be written as is DuckDB’s list: ? numbered by where it is, ?1 and $1 numbered by hand, and $name. A parameter used twice is one parameter, because it is one value to provide.

use rudb::Database;
use rudb_common::Value;

let db = Database::new();
db.execute("CREATE TABLE t (a INTEGER)")?;
db.execute("INSERT INTO t VALUES (1), (2), (3)")?;

let counted = db.prepare("SELECT count(*) FROM t WHERE a > ?")?;
assert_eq!(counted.value(&[Value::Integer(1)])?, Value::BigInt(2));
assert_eq!(counted.value(&[Value::Integer(2)])?, Value::BigInt(1));

Implementations§

Source§

impl Prepared

Source

pub fn sql(&self) -> &str

The statement as it was written.

Source

pub fn parameters(&self) -> &[String]

The parameters the statement uses, once each, in the order they were written.

The identifier of a positional parameter is its number as a string, so a statement written with ? twice has parameters 1 and 2.

Source

pub fn execute(&self, values: &[Value]) -> Result<QueryResult>

Runs the statement with values by position, numbered from one.

§Errors

If a parameter was given no value, if a value was given for a parameter the statement does not use, or anything binding and running the statement reports.

Source

pub fn execute_named(&self, values: &[(&str, Value)]) -> Result<QueryResult>

Runs the statement with values by name, which is what $name wants.

A name is matched without regard to case, which is what DuckDB does for this and for nothing else. Positional parameters can be given this way too, under the identifiers 1, 2 and so on, since a position is only a name that happens to be a number.

§Errors

The same as Prepared::execute.

Source

pub fn value(&self, values: &[Value]) -> Result<Value>

Runs the statement with values by position and returns the single value it produced.

§Errors

Everything Prepared::execute can raise, plus an error if the result is not one row of one column.

Trait Implementations§

Source§

impl Clone for Prepared

Source§

fn clone(&self) -> Prepared

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Prepared

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.