xitca_postgres/execute/
sync_impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::{
    driver::codec::AsParams,
    error::Error,
    prepare::Prepare,
    query::{Query, RowAffected, RowSimpleStream, RowStream, RowStreamGuarded},
    statement::{
        Statement, StatementCreateBlocking, StatementGuarded, StatementNamed, StatementQuery, StatementUnnamedBind,
        StatementUnnamedQuery,
    },
};

use super::ExecuteBlocking;

impl<'s, C> ExecuteBlocking<'_, C> for &'s Statement
where
    C: Query,
{
    type ExecuteOutput = Result<u64, Error>;
    type QueryOutput = Result<RowStream<'s>, Error>;

    #[inline]
    fn execute_blocking(self, cli: &C) -> Self::ExecuteOutput {
        let stream = self.query_blocking(cli)?;
        RowAffected::from(stream).wait()
    }

    #[inline]
    fn query_blocking(self, cli: &C) -> Self::QueryOutput {
        cli._query(self)
    }
}

impl<C> ExecuteBlocking<'_, C> for &str
where
    C: Query,
{
    type ExecuteOutput = Result<u64, Error>;
    type QueryOutput = Result<RowSimpleStream, Error>;

    #[inline]
    fn execute_blocking(self, cli: &C) -> Self::ExecuteOutput {
        let stream = self.query_blocking(cli)?;
        RowAffected::from(stream).wait()
    }

    #[inline]
    fn query_blocking(self, cli: &C) -> Self::QueryOutput {
        cli._query(self)
    }
}

impl<'c, 's, C> ExecuteBlocking<'c, C> for StatementNamed<'s>
where
    C: Prepare + 'c,
    's: 'c,
{
    type ExecuteOutput = Result<StatementGuarded<'c, C>, Error>;
    type QueryOutput = Self::ExecuteOutput;

    #[inline]
    fn execute_blocking(self, cli: &'c C) -> Self::ExecuteOutput {
        let stmt = cli._query(StatementCreateBlocking::from((self, cli)))??;
        Ok(stmt.into_guarded(cli))
    }

    #[inline]
    fn query_blocking(self, cli: &'c C) -> Self::QueryOutput {
        self.execute_blocking(cli)
    }
}

impl<'s, C, P> ExecuteBlocking<'_, C> for StatementQuery<'s, P>
where
    C: Query,
    P: AsParams + 's,
{
    type ExecuteOutput = Result<u64, Error>;
    type QueryOutput = Result<RowStream<'s>, Error>;

    #[inline]
    fn execute_blocking(self, cli: &C) -> Result<u64, Error> {
        let stream = self.query_blocking(cli)?;
        RowAffected::from(stream).wait()
    }

    #[inline]
    fn query_blocking(self, cli: &C) -> Self::QueryOutput {
        cli._query(self)
    }
}

impl<'s, 'c, C, P> ExecuteBlocking<'c, C> for StatementUnnamedBind<'s, P>
where
    C: Prepare + 'c,
    P: AsParams + 'c,
    's: 'c,
{
    type ExecuteOutput = Result<u64, Error>;
    type QueryOutput = Result<RowStreamGuarded<'c, C>, Error>;

    #[inline]
    fn execute_blocking(self, cli: &C) -> Result<u64, Error> {
        let stream = self.query_blocking(cli)?;
        RowAffected::from(stream).wait()
    }

    #[inline]
    fn query_blocking(self, cli: &'c C) -> Self::QueryOutput {
        cli._query(StatementUnnamedQuery::from((self, cli)))
    }
}

impl<'c, C> ExecuteBlocking<'c, C> for &std::path::Path
where
    C: Query + 'c,
{
    type ExecuteOutput = Result<u64, Error>;
    type QueryOutput = Result<RowSimpleStream, Error>;

    #[inline]
    fn execute_blocking(self, cli: &'c C) -> Self::ExecuteOutput {
        std::fs::read_to_string(self)?.execute_blocking(cli)
    }

    #[inline]
    fn query_blocking(self, cli: &'c C) -> Self::QueryOutput {
        std::fs::read_to_string(self)?.query_blocking(cli)
    }
}