Trait xitca_postgres::dev::Query

source ·
pub trait Query {
    // Required method
    fn _send_encode_query<S, I>(
        &self,
        stmt: S,
        params: I,
    ) -> Result<Response, Error>
       where S: Encode,
             I: AsParams;

    // Provided methods
    fn _query<'a, S>(
        &self,
        stmt: S,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<S::RowStream<'a>, Error>
       where S: Encode + IntoStream + 'a { ... }
    fn _query_raw<'a, S, I>(
        &self,
        stmt: S,
        params: I,
    ) -> Result<S::RowStream<'a>, Error>
       where S: Encode + IntoStream + 'a,
             I: AsParams { ... }
    fn _execute<S>(
        &self,
        stmt: S,
        params: &[&(dyn ToSql + Sync)],
    ) -> ExecuteFuture
       where S: Encode { ... }
    fn _execute_raw<S, I>(&self, stmt: S, params: I) -> ExecuteFuture
       where S: Encode,
             I: AsParams { ... }
}
Expand description

trait generic over api used for querying with typed prepared statement.

types like Transaction and CopyIn accept generic client type and they are able to use user supplied client new type to operate and therefore reduce less new types and methods boilerplate.

Required Methods§

source

fn _send_encode_query<S, I>( &self, stmt: S, params: I, ) -> Result<Response, Error>
where S: Encode, I: AsParams,

encode statement and params and send it to client driver

Provided Methods§

source

fn _query<'a, S>( &self, stmt: S, params: &[&(dyn ToSql + Sync)], ) -> Result<S::RowStream<'a>, Error>
where S: Encode + IntoStream + 'a,

query with statement and dynamic typed params.

statement must be a type impl Encode trait and there are currently 3 major types available:

§Statement type category

This category includes multiple types that can be dereferenced/borrowed as Statement

§Examples
// prepare a statement with client type.
let stmt = client._prepare("SELECT id from users", &[Type::INT4]).await?;
// query with statement and typed params for a stream of rows
let mut stream = client._query(&stmt, &[&996i32])?;
// obtain the first row and get user id.
let row = stream.try_next().await?.unwrap();      
let _id: i32 = row.try_get("id")?;
§StatementUnnamed type category

This category is for embedding prepare statement to the query request itself. Meaning query would finish in one round trip to database. However it should also noted that the client type must be referenced during the whole progress and associated client must be kept around util streaming is finished.

§Examples
// construct an unnamed statement.
let stmt = Statement::unnamed(client, "SELECT id from users", &[Type::INT4]);
// query with the unnamed statement.
// under the hood the statement is prepared in background and used for query and stream row parsing
let mut stream = client._query(stmt, &[&996i32])?;
// obtain the first row and get user id.
let row = stream.try_next().await?.unwrap();      
let _id: i32 = row.try_get("id")?;
§str type

This category includes multiple types that can be dereferenced/borrowed as str

§Examples
// query with a string. the string can contain multiple sql query and they have to be separated by semicolons
let mut stream = client._query("SELECT 1;SELECT 1", &[])?;
let _ = stream.try_next().await?;      
source

fn _query_raw<'a, S, I>( &self, stmt: S, params: I, ) -> Result<S::RowStream<'a>, Error>
where S: Encode + IntoStream + 'a, I: AsParams,

flexible version of Query::_query

source

fn _execute<S>(&self, stmt: S, params: &[&(dyn ToSql + Sync)]) -> ExecuteFuture
where S: Encode,

query that don’t return any row but number of rows affected by it

source

fn _execute_raw<S, I>(&self, stmt: S, params: I) -> ExecuteFuture
where S: Encode, I: AsParams,

flexible version of Query::_execute

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Query for Arc<Client>

source§

fn _send_encode_query<S, I>( &self, stmt: S, params: I, ) -> Result<Response, Error>
where S: Encode, I: AsParams,

Implementors§