Struct Query

Source
pub struct Query<'a> { /* private fields */ }
Expand description

A static query with dynamic parameters.

§Usage

§Constructing

The preferred way of constructing a Query is by using the query! and query_dyn! macros.

You may also use the Query::parse, Query::new_static or Query::new methods.

§Executing

When executing the query you have two options, either:

  1. use the provided methods: execute, fetch, query, etc.
  2. use the sql and parameters fields as arguments to the standard Client methods
#[derive(FromSqlRow)]
struct Person {
    age: i32,
    name: String,
}

let client: Client = connect(/* ... */);
let query = query!("SELECT age, name FROM people");

// Option 1
let people: Vec<Person> = query.fetch(&client).await?;

// Option 2
let rows: Vec<Row> = client.query(query.sql(), query.parameters()).await?;
let people: Vec<Person> = Person::from_row_multi(&rows)?;

Implementations§

Source§

impl<'a> Query<'a>

Source

pub async fn execute<C>(&self, client: &C) -> Result<u64>
where C: GenericClient + Sync,

Execute this query and return the number of affected rows.

Source

pub async fn fetch<T, C>(&self, client: &C) -> Result<Vec<T>>
where T: FromSqlRow, C: GenericClient + Sync,

Execute this query and return the resulting values.

Source

pub async fn fetch_one<T, C>(&self, client: &C) -> Result<T>
where T: FromSqlRow, C: GenericClient + Sync,

Execute this query and return the resulting value. This method will return an error if, not exactly one row was returned by the query.

Source

pub async fn fetch_streaming<T, C>( &self, client: &C, ) -> Result<impl Stream<Item = Result<T>>>
where T: FromSqlRow, C: GenericClient + Sync,

Execute this query and return the resulting values as an asynchronous stream of values.

Source

pub async fn query<C>(&self, client: &C) -> Result<Vec<Row>>
where C: GenericClient + Sync,

Execute this query and return the resulting rows.

Source

pub async fn query_one<C>(&self, client: &C) -> Result<Row>
where C: GenericClient + Sync,

Execute this query and return the resulting row. This method will return an error if, not exactly one row was returned by the query.

Source

pub async fn query_streaming<C>( &self, client: &C, ) -> Result<impl Stream<Item = Result<Row>>>
where C: GenericClient + Sync,

Execute this query and return the resulting values as an asynchronous stream of values.

Source§

impl<'a> Query<'a>

Source

pub fn new(sql: String, parameters: Vec<Parameter<'a>>) -> Query<'a>

Create a new query an already prepared string.

IMPORTANT: This does not allow you to pass named parameter bindings ($name, $abc_123, etc.). For that behaviour, refer to the query! macro. Instead bindings and parameters are given in the same format required by tokio_postgres ($1, $2, …).

Source

pub fn new_static( sql: &'static str, parameters: Vec<Parameter<'a>>, ) -> Query<'a>

Create a new query with a static query string.

IMPORTANT: This does not allow you to pass named parameter bindings ($name, $abc_123, etc.), For that behaviour, refer to the query_dyn! macro. Instead bindings and parameters are given in the same format required by tokio_postgres ($1, $2, …).

Source

pub fn parse( text: &str, bindings: &[(&str, Parameter<'a>)], ) -> Result<Query<'a>>

Parses a string that may contain parameter bindings on the form $abc_123. This is the same function that is called when passing dynamically generated strings to the query_dyn! macro.

Because this is a function there will some runtime overhead unlike the query! macro which has zero overhead when working with string literals.

Source

pub fn sql(&'a self) -> &'a str

Get this query as an SQL string.

Source

pub fn parameters(&'a self) -> &[Parameter<'a>]

Get the parameters of this query in the order expected by the query returned by Query::sql.

Trait Implementations§

Source§

impl<'a> Clone for Query<'a>

Source§

fn clone(&self) -> Query<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Query<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Query<'a>

§

impl<'a> !RefUnwindSafe for Query<'a>

§

impl<'a> Send for Query<'a>

§

impl<'a> Sync for Query<'a>

§

impl<'a> Unpin for Query<'a>

§

impl<'a> !UnwindSafe for Query<'a>

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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V