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:
- use the provided methods:
execute
,fetch
,query
, etc. - use the
sql
andparameters
fields as arguments to the standardClient
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>
impl<'a> Query<'a>
Sourcepub async fn execute<C>(&self, client: &C) -> Result<u64>where
C: GenericClient + Sync,
pub async fn execute<C>(&self, client: &C) -> Result<u64>where
C: GenericClient + Sync,
Execute this query and return the number of affected rows.
Sourcepub async fn fetch<T, C>(&self, client: &C) -> Result<Vec<T>>
pub async fn fetch<T, C>(&self, client: &C) -> Result<Vec<T>>
Execute this query and return the resulting values.
Sourcepub async fn fetch_one<T, C>(&self, client: &C) -> Result<T>
pub async fn fetch_one<T, C>(&self, client: &C) -> Result<T>
Execute this query and return the resulting value. This method will return an error if, not exactly one row was returned by the query.
Sourcepub async fn fetch_streaming<T, C>(
&self,
client: &C,
) -> Result<impl Stream<Item = Result<T>>>
pub async fn fetch_streaming<T, C>( &self, client: &C, ) -> Result<impl Stream<Item = Result<T>>>
Execute this query and return the resulting values as an asynchronous stream of values.
Sourcepub async fn query<C>(&self, client: &C) -> Result<Vec<Row>>where
C: GenericClient + Sync,
pub async fn query<C>(&self, client: &C) -> Result<Vec<Row>>where
C: GenericClient + Sync,
Execute this query and return the resulting rows.
Sourcepub async fn query_one<C>(&self, client: &C) -> Result<Row>where
C: GenericClient + Sync,
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.
Sourcepub async fn query_streaming<C>(
&self,
client: &C,
) -> Result<impl Stream<Item = Result<Row>>>where
C: GenericClient + Sync,
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>
impl<'a> Query<'a>
Sourcepub fn new(sql: String, parameters: Vec<Parameter<'a>>) -> Query<'a>
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
, …).
Sourcepub fn new_static(
sql: &'static str,
parameters: Vec<Parameter<'a>>,
) -> Query<'a>
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
, …).
Sourcepub fn parse(
text: &str,
bindings: &[(&str, Parameter<'a>)],
) -> Result<Query<'a>>
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.
Sourcepub fn parameters(&'a self) -> &[Parameter<'a>] ⓘ
pub fn parameters(&'a self) -> &[Parameter<'a>] ⓘ
Get the parameters of this query in the order expected by the query returned by
Query::sql
.