[−][src]Struct postgres_query::Query
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)?;
Methods
impl<'a> Query<'a>
[src]
pub async fn execute<'_, '_, C>(&'_ self, client: &'_ C) -> Result<u64> where
C: GenericClient + Sync,
[src]
C: GenericClient + Sync,
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]
T: FromSqlRow,
C: GenericClient + Sync,
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]
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.
pub async fn fetch_streaming<'_, '_, T, C>(
&'_ self,
client: &'_ C
) -> Result<impl Stream<Item = Result<T>>> where
T: FromSqlRow,
C: GenericClient + Sync,
[src]
&'_ 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.
pub async fn query<'_, '_, C>(&'_ self, client: &'_ C) -> Result<Vec<Row>> where
C: GenericClient + Sync,
[src]
C: GenericClient + Sync,
Execute this query and return the resulting rows.
pub async fn query_one<'_, '_, C>(&'_ self, client: &'_ C) -> Result<Row> where
C: GenericClient + Sync,
[src]
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.
pub async fn query_streaming<'_, '_, C>(
&'_ self,
client: &'_ C
) -> Result<impl Stream<Item = Result<Row>>> where
C: GenericClient + Sync,
[src]
&'_ 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.
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]
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
, ...).
pub fn parse(
text: &str,
bindings: &[(&str, Parameter<'a>)]
) -> Result<Query<'a>>
[src]
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.
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
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,