[][src]Struct postgres_query::Query

pub struct Query<'a> { /* fields omitted */ }

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)?;

Methods

impl<'a> Query<'a>[src]

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

Execute this query and return the number of affected rows.

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

Execute this query and return the resulting values.

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

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

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

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

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

Execute this query and return the resulting rows.

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

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

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

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

impl<'a> Query<'a>[src]

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

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, ...).

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

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, ...).

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

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.

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

Get this query as an SQL string.

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

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

Trait Implementations

impl<'a> Clone for Query<'a>[src]

impl<'a> Debug for Query<'a>[src]

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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