xitca_postgres/
query.rs

1mod stream;
2
3#[cfg(feature = "compat")]
4pub(crate) mod compat;
5
6pub use stream::{RowAffected, RowSimpleStream, RowSimpleStreamOwned, RowStream, RowStreamGuarded, RowStreamOwned};
7
8use super::{
9    driver::codec::{Response, encode::Encode, response::IntoResponse},
10    error::Error,
11};
12
13/// trait generic over api used for querying with typed prepared statement.
14///
15/// types like [Transaction] and [CopyIn] accept generic client type and they are able to use user supplied
16/// client new type to operate and therefore reduce less new types and methods boilerplate.
17///
18/// [Transaction]: crate::transaction::Transaction
19/// [CopyIn]: crate::copy::CopyIn
20pub trait Query {
21    /// query with statement and dynamic typed params.
22    ///
23    /// statement must be a type impl [`Encode`] trait
24    #[inline]
25    fn _query<S>(&self, stmt: S) -> Result<<S::Output as IntoResponse>::Response, Error>
26    where
27        S: Encode,
28    {
29        self._send_encode_query(stmt)
30            .map(|(stream, res)| stream.into_response(res))
31    }
32
33    /// encode statement and params and send it to client driver
34    fn _send_encode_query<S>(&self, stmt: S) -> Result<(S::Output, Response), Error>
35    where
36        S: Encode;
37}
38
39impl<T> Query for &T
40where
41    T: Query,
42{
43    #[inline]
44    fn _send_encode_query<S>(&self, stmt: S) -> Result<(S::Output, Response), Error>
45    where
46        S: Encode,
47    {
48        T::_send_encode_query(*self, stmt)
49    }
50}
51
52impl<T> Query for &mut T
53where
54    T: Query,
55{
56    #[inline]
57    fn _send_encode_query<S>(&self, stmt: S) -> Result<(S::Output, Response), Error>
58    where
59        S: Encode,
60    {
61        T::_send_encode_query(&**self, stmt)
62    }
63}