xitca_postgres/execute/
sync_impl.rs

1use crate::{
2    driver::codec::AsParams,
3    error::Error,
4    prepare::Prepare,
5    query::{Query, RowAffected, RowSimpleStream, RowStream, RowStreamGuarded},
6    statement::{
7        Statement, StatementCreateBlocking, StatementGuarded, StatementNamed, StatementQuery, StatementUnnamedBind,
8        StatementUnnamedQuery,
9    },
10};
11
12use super::ExecuteBlocking;
13
14impl<'s, C> ExecuteBlocking<'_, C> for &'s Statement
15where
16    C: Query,
17{
18    type ExecuteOutput = Result<u64, Error>;
19    type QueryOutput = Result<RowStream<'s>, Error>;
20
21    #[inline]
22    fn execute_blocking(self, cli: &C) -> Self::ExecuteOutput {
23        let stream = self.query_blocking(cli)?;
24        RowAffected::from(stream).wait()
25    }
26
27    #[inline]
28    fn query_blocking(self, cli: &C) -> Self::QueryOutput {
29        cli._query(self)
30    }
31}
32
33impl<C> ExecuteBlocking<'_, C> for &str
34where
35    C: Query,
36{
37    type ExecuteOutput = Result<u64, Error>;
38    type QueryOutput = Result<RowSimpleStream, Error>;
39
40    #[inline]
41    fn execute_blocking(self, cli: &C) -> Self::ExecuteOutput {
42        let stream = self.query_blocking(cli)?;
43        RowAffected::from(stream).wait()
44    }
45
46    #[inline]
47    fn query_blocking(self, cli: &C) -> Self::QueryOutput {
48        cli._query(self)
49    }
50}
51
52impl<'c, C> ExecuteBlocking<'c, C> for StatementNamed<'_>
53where
54    C: Prepare + 'c,
55{
56    type ExecuteOutput = Result<StatementGuarded<'c, C>, Error>;
57    type QueryOutput = Self::ExecuteOutput;
58
59    #[inline]
60    fn execute_blocking(self, cli: &'c C) -> Self::ExecuteOutput {
61        let stmt = cli._query(StatementCreateBlocking::from((self, cli)))??;
62        Ok(stmt.into_guarded(cli))
63    }
64
65    #[inline]
66    fn query_blocking(self, cli: &'c C) -> Self::QueryOutput {
67        self.execute_blocking(cli)
68    }
69}
70
71impl<'s, C, P> ExecuteBlocking<'_, C> for StatementQuery<'s, P>
72where
73    C: Query,
74    P: AsParams,
75{
76    type ExecuteOutput = Result<u64, Error>;
77    type QueryOutput = Result<RowStream<'s>, Error>;
78
79    #[inline]
80    fn execute_blocking(self, cli: &C) -> Result<u64, Error> {
81        let stream = self.query_blocking(cli)?;
82        RowAffected::from(stream).wait()
83    }
84
85    #[inline]
86    fn query_blocking(self, cli: &C) -> Self::QueryOutput {
87        cli._query(self)
88    }
89}
90
91impl<'c, C, P> ExecuteBlocking<'c, C> for StatementUnnamedBind<'_, P>
92where
93    C: Prepare + 'c,
94    P: AsParams,
95{
96    type ExecuteOutput = Result<u64, Error>;
97    type QueryOutput = Result<RowStreamGuarded<'c, C>, Error>;
98
99    #[inline]
100    fn execute_blocking(self, cli: &C) -> Result<u64, Error> {
101        let stream = self.query_blocking(cli)?;
102        RowAffected::from(stream).wait()
103    }
104
105    #[inline]
106    fn query_blocking(self, cli: &'c C) -> Self::QueryOutput {
107        cli._query(StatementUnnamedQuery::from((self, cli)))
108    }
109}
110
111impl<'c, C> ExecuteBlocking<'c, C> for &std::path::Path
112where
113    C: Query + 'c,
114{
115    type ExecuteOutput = Result<u64, Error>;
116    type QueryOutput = Result<RowSimpleStream, Error>;
117
118    #[inline]
119    fn execute_blocking(self, cli: &'c C) -> Self::ExecuteOutput {
120        std::fs::read_to_string(self)?.execute_blocking(cli)
121    }
122
123    #[inline]
124    fn query_blocking(self, cli: &'c C) -> Self::QueryOutput {
125        std::fs::read_to_string(self)?.query_blocking(cli)
126    }
127}