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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2020 rust-mysql-simple contributors
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use mysql_common::row::convert::FromRowError;

use std::{borrow::Cow, result::Result as StdResult};

use crate::{
    conn::query_result::{Binary, Text},
    from_row, from_row_opt,
    prelude::FromRow,
    Params, QueryResult, Result, Statement,
};

/// Something, that eventualy is a `Statement` in the context of a `T: Queryable`.
pub trait AsStatement {
    /// Make a statement out of `Self`.
    fn as_statement<Q: Queryable>(&self, queryable: &mut Q) -> Result<Cow<'_, Statement>>;
}

/// Queryable object.
pub trait Queryable {
    /// Perfoms text query.
    fn query_iter<Q: AsRef<str>>(&mut self, query: Q) -> Result<QueryResult<'_, '_, '_, Text>>;

    /// Performs text query and collects the first result set.
    fn query<T, Q>(&mut self, query: Q) -> Result<Vec<T>>
    where
        Q: AsRef<str>,
        T: FromRow,
    {
        self.query_map(query, from_row)
    }

    /// Same as [`Queryable::query`] but useful when you not sure what your schema is.
    fn query_opt<T, Q>(&mut self, query: Q) -> Result<Vec<StdResult<T, FromRowError>>>
    where
        Q: AsRef<str>,
        T: FromRow,
    {
        self.query_map(query, from_row_opt)
    }

    /// Performs text query and returns the first row of the first result set.
    fn query_first<T, Q>(&mut self, query: Q) -> Result<Option<T>>
    where
        Q: AsRef<str>,
        T: FromRow,
    {
        self.query_iter(query)?
            .next()
            .map(|row| row.map(from_row))
            .transpose()
    }

    /// Same as [`Queryable::query_first`] but useful when you not sure what your schema is.
    fn query_first_opt<T, Q>(&mut self, query: Q) -> Result<Option<StdResult<T, FromRowError>>>
    where
        Q: AsRef<str>,
        T: FromRow,
    {
        self.query_iter(query)?
            .next()
            .map(|row| row.map(from_row_opt))
            .transpose()
    }

    /// Performs text query and maps each row of the first result set.
    fn query_map<T, F, Q, U>(&mut self, query: Q, mut f: F) -> Result<Vec<U>>
    where
        Q: AsRef<str>,
        T: FromRow,
        F: FnMut(T) -> U,
    {
        self.query_fold(query, Vec::new(), |mut acc, row| {
            acc.push(f(row));
            acc
        })
    }

    /// Same as [`Queryable::query_map`] but useful when you not sure what your schema is.
    fn query_map_opt<T, F, Q, U>(&mut self, query: Q, mut f: F) -> Result<Vec<U>>
    where
        Q: AsRef<str>,
        T: FromRow,
        F: FnMut(StdResult<T, FromRowError>) -> U,
    {
        self.query_fold_opt(query, Vec::new(), |mut acc, row| {
            acc.push(f(row));
            acc
        })
    }

    /// Performs text query and folds the first result set to a single value.
    fn query_fold<T, F, Q, U>(&mut self, query: Q, init: U, mut f: F) -> Result<U>
    where
        Q: AsRef<str>,
        T: FromRow,
        F: FnMut(U, T) -> U,
    {
        self.query_iter(query)?
            .map(|row| row.map(from_row::<T>))
            .try_fold(init, |acc, row: Result<T>| row.map(|row| f(acc, row)))
    }

    /// Same as [`Queryable::query_fold`] but useful when you not sure what your schema is.
    fn query_fold_opt<T, F, Q, U>(&mut self, query: Q, init: U, mut f: F) -> Result<U>
    where
        Q: AsRef<str>,
        T: FromRow,
        F: FnMut(U, StdResult<T, FromRowError>) -> U,
    {
        self.query_iter(query)?
            .map(|row| row.map(from_row_opt::<T>))
            .try_fold(init, |acc, row: Result<StdResult<T, FromRowError>>| {
                row.map(|row| f(acc, row))
            })
    }

    /// Performs text query and drops the query result.
    fn query_drop<Q>(&mut self, query: Q) -> Result<()>
    where
        Q: AsRef<str>,
    {
        self.query_iter(query).map(drop)
    }

    /// Prepares the given `query` as a prepared statement.
    fn prep<Q: AsRef<str>>(&mut self, query: Q) -> Result<crate::Statement>;

    /// This function will close the given statement on the server side.
    fn close(&mut self, stmt: Statement) -> Result<()>;

    /// Executes the given `stmt` with the given `params`.
    fn exec_iter<S, P>(&mut self, stmt: S, params: P) -> Result<QueryResult<'_, '_, '_, Binary>>
    where
        S: AsStatement,
        P: Into<Params>;

    /// Prepares the given statement, and executes it with each item in the given params iterator.
    fn exec_batch<S, P, I>(&mut self, stmt: S, params: I) -> Result<()>
    where
        Self: Sized,
        S: AsStatement,
        P: Into<Params>,
        I: IntoIterator<Item = P>,
    {
        let stmt = stmt.as_statement(self)?;
        for params in params {
            self.exec_drop(stmt.as_ref(), params)?;
        }

        Ok(())
    }

    /// Executes the given `stmt` and collects the first result set.
    fn exec<T, S, P>(&mut self, stmt: S, params: P) -> Result<Vec<T>>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
    {
        self.exec_map(stmt, params, from_row)
    }

    /// Same as [`Queryable::exec`] but useful when you not sure what your schema is.
    fn exec_opt<T, S, P>(&mut self, stmt: S, params: P) -> Result<Vec<StdResult<T, FromRowError>>>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
    {
        self.exec_map(stmt, params, from_row_opt)
    }

    /// Executes the given `stmt` and returns the first row of the first result set.
    fn exec_first<T, S, P>(&mut self, stmt: S, params: P) -> Result<Option<T>>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
    {
        self.exec_iter(stmt, params)?
            .next()
            .map(|row| row.map(crate::from_row))
            .transpose()
    }

    /// Same as [`Queryable::exec_first`] but useful when you not sure what your schema is.
    fn exec_first_opt<T, S, P>(
        &mut self,
        stmt: S,
        params: P,
    ) -> Result<Option<StdResult<T, FromRowError>>>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
    {
        self.exec_iter(stmt, params)?
            .next()
            .map(|row| row.map(from_row_opt))
            .transpose()
    }

    /// Executes the given `stmt` and maps each row of the first result set.
    fn exec_map<T, S, P, F, U>(&mut self, stmt: S, params: P, mut f: F) -> Result<Vec<U>>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
        F: FnMut(T) -> U,
    {
        self.exec_fold(stmt, params, Vec::new(), |mut acc, row| {
            acc.push(f(row));
            acc
        })
    }

    /// Same as [`Queryable::exec_map`] but useful when you not sure what your schema is.
    fn exec_map_opt<T, S, P, F, U>(&mut self, stmt: S, params: P, mut f: F) -> Result<Vec<U>>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
        F: FnMut(StdResult<T, FromRowError>) -> U,
    {
        self.exec_fold_opt(stmt, params, Vec::new(), |mut acc, row| {
            acc.push(f(row));
            acc
        })
    }

    /// Executes the given `stmt` and folds the first result set to a signel value.
    fn exec_fold<T, S, P, U, F>(&mut self, stmt: S, params: P, init: U, mut f: F) -> Result<U>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
        F: FnMut(U, T) -> U,
    {
        let mut result = self.exec_iter(stmt, params)?;
        result.try_fold(init, |init, row| row.map(|row| f(init, from_row(row))))
    }

    /// Same as [`Queryable::exec_fold`] but useful when you not sure what your schema is.
    fn exec_fold_opt<T, S, P, U, F>(&mut self, stmt: S, params: P, init: U, mut f: F) -> Result<U>
    where
        S: AsStatement,
        P: Into<Params>,
        T: FromRow,
        F: FnMut(U, StdResult<T, FromRowError>) -> U,
    {
        let mut result = self.exec_iter(stmt, params)?;
        result.try_fold(init, |init, row| row.map(|row| f(init, from_row_opt(row))))
    }

    /// Executes the given `stmt` and drops the result.
    fn exec_drop<S, P>(&mut self, stmt: S, params: P) -> Result<()>
    where
        S: AsStatement,
        P: Into<Params>,
    {
        self.exec_iter(stmt, params).map(drop)
    }
}