mae 0.3.12

Opinionated async Rust framework for building Mae-Technologies micro-services — app scaffolding, repo layer, middleware, and test utilities.
Documentation
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use super::default::DomainStatus;
use super::type_def::{ToField, ToInsertRow, ToPatch, ToUpdateRow};
use sqlx::Arguments;
use std::fmt::{Debug, Display};
// /////
// INTERNAL: CONVERT THE BUILDER PARTS TO SQL
// /////

// There are two interfaces to all the SQL parts:

//  - BindArgs
//      If there are arguments, they need to be safely inserted into the SQL Query with PgArguments
pub trait BindArgs {
    // TODO: if any of these are to panic, this method should return a Result
    fn bind(&self, args: &mut sqlx::postgres::PgArguments);
    fn bind_len(&self) -> usize;
}

//  - ToSqlParts
//      If there are column representations inside the types, they need to be extracted.
//      This is done with the Dispay impl
pub type AsSqlParts = (Vec<String>, Option<Vec<String>>);
pub trait ToSqlParts {
    fn to_sql_parts(&self) -> AsSqlParts;
}

pub fn concat_sql_parts(parts: Vec<(Vec<String>, Option<Vec<String>>)>) -> AsSqlParts {
    let mut cols = Vec::new();
    let mut binds: Option<Vec<String>> = None;

    for (c, b) in parts {
        cols.extend(c);

        if let Some(bv) = b {
            binds.get_or_insert_with(Vec::new).extend(bv);
        }
    }

    (cols, binds)
}

// SQL Statements
pub enum SqlStatement<I: ToInsertRow, U: ToUpdateRow, F: ToField, P: ToPatch> {
    // TODO: I want an upsert() in here -> Insert, Update on conflict:
    // INSERT INTO users (email, name)
    // VALUES
    //   ('a@x.com', 'Alice'),
    //   ('b@x.com', 'Bob')
    // ON CONFLICT (email)
    // DO UPDATE SET
    //   name = EXCLUDED.name,
    //   updated_at = now()
    // RETURNING *;
    Select(Vec<F>),
    InsertOne(I),
    InsertMany(Vec<I>),
    Update(U),
    Patch(Vec<P>)
}

impl<I: ToInsertRow, U: ToUpdateRow, F: ToField, P: ToPatch> BindArgs for SqlStatement<I, U, F, P> {
    // Bind the Statement values to the query
    // (Ie - Struct{value: 1} or Enum::value(1)) -> iter.v / v -> PgArguments.add(v)
    fn bind(&self, args: &mut sqlx::postgres::PgArguments) {
        match self {
            Self::Select(v) => {
                for ele in v {
                    let _ = args.add(ele.to_string());
                }
            }
            Self::InsertOne(v) => v.bind(args),
            Self::InsertMany(v) => v.iter().for_each(|f| f.bind(args)),
            Self::Update(v) => v.bind(args),
            Self::Patch(v) => v.iter().for_each(|f| f.bind(args))
        }
    }

    // Get the count of arg's that are to be bound
    fn bind_len(&self) -> usize {
        match self {
            Self::Update(v) => v.bind_len(),
            Self::InsertOne(v) => v.bind_len(),
            // NOTE: There are no bindings for select statements
            Self::Select(_) => 0,
            Self::InsertMany(v) => v.iter().map(|v| v.bind_len()).sum(),
            Self::Patch(v) => v.len()
        }
    }
}

impl<I: ToInsertRow, U: ToUpdateRow, F: ToField, P: ToPatch> Debug for SqlStatement<I, U, F, P> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SqlStatement::Select(_fields) => {
                // no fields to write
                std::fmt::Result::Ok(())
            }
            SqlStatement::InsertOne(row) => std::fmt::Debug::fmt(&row, f),
            SqlStatement::InsertMany(_row) => {
                todo!()
            }
            SqlStatement::Update(row) => std::fmt::Debug::fmt(&row, f),
            SqlStatement::Patch(fields) => {
                for (i, field) in fields.iter().enumerate() {
                    write!(f, "\n\t${} = ", i + 1)?;
                    std::fmt::Debug::fmt(field, f)?;
                }
                std::fmt::Result::Ok(())
            }
        }
    }
}

// Filter / Where block of the Query
pub enum Filter {
    StatusIs(DomainStatus),
    Equals(i32),
    NotEquals(i32),
    In(Vec<i32>),
    NotIn(Vec<i32>),
    Like(String),
    NotLike(String),
    Ilike(String),
    NotIlike(String),
    StringIs(String),
    StringIsNot(String),
    /// Compare a Postgres enum column to a text bind param with an explicit cast.
    PgEnumCast(String, &'static str),
    Gt(i32),
    Gte(i32),
    Lt(i32),
    Lte(i32),
    IsNull
}

impl BindArgs for Filter {
    fn bind(&self, args: &mut sqlx::postgres::PgArguments) {
        let _ = match self {
            Self::Equals(v) => args.add(v),
            Self::NotEquals(v) => args.add(v),
            Self::In(v) => args.add(v),
            Self::NotIn(v) => args.add(v),
            Self::Like(v) => args.add(v),
            Self::NotLike(v) => args.add(v),
            Self::Ilike(v) => args.add(v),
            Self::NotIlike(v) => args.add(v),
            Self::StringIs(v) => args.add(v.to_owned()),
            Self::StringIsNot(v) => args.add(v),
            Self::PgEnumCast(v, _) => args.add(v.to_owned()),
            Self::Gt(v) => args.add(v),
            Self::Gte(v) => args.add(v),
            Self::Lt(v) => args.add(v),
            Self::Lte(v) => args.add(v),
            Self::IsNull => Ok(()),
            Self::StatusIs(v) => args.add(v)
        };
    }
    fn bind_len(&self) -> usize {
        match self {
            Self::IsNull => 0,
            _ => 1
        }
    }
}

impl std::fmt::Debug for Filter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Equals(v) => write!(f, "{:?}", v),
            Self::NotEquals(v) => write!(f, "{:?}", v),
            Self::In(v) => write!(f, "{:?}", v),
            Self::NotIn(v) => write!(f, "{:?}", v),
            Self::Like(v) => write!(f, "{:?}", v),
            Self::NotLike(v) => write!(f, "{:?}", v),
            Self::Ilike(v) => write!(f, "{:?}", v),
            Self::NotIlike(v) => write!(f, "{:?}", v),
            Self::StringIs(v) => write!(f, "{:?}", v),
            Self::StringIsNot(v) => write!(f, "{:?}", v),
            Self::PgEnumCast(v, pg_type) => write!(f, "{:?}::{pg_type}", v),
            Self::Gt(v) => write!(f, "{:?}", v),
            Self::Gte(v) => write!(f, "{:?}", v),
            Self::Lt(v) => write!(f, "{:?}", v),
            Self::Lte(v) => write!(f, "{:?}", v),
            Self::StatusIs(v) => write!(f, "{:?}", v),
            Self::IsNull => Ok(())
        }
    }
}

impl Display for Filter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Filter::Equals(_) => write!(f, "="),
            Filter::NotEquals(_) => write!(f, "!="),
            Filter::In(_) => write!(f, "IN"),
            Filter::NotIn(_) => write!(f, "NOT IN"),
            Filter::Like(_) => write!(f, "LIKE"),
            Filter::NotLike(_) => write!(f, "NOT LIKE"),
            Filter::Ilike(_) => write!(f, "ILIKE"),
            Filter::NotIlike(_) => write!(f, "NOT ILIKE"),
            Filter::StringIs(_) => write!(f, "="),
            Filter::StringIsNot(_) => write!(f, "!="),
            Filter::PgEnumCast(_, _) => write!(f, "="),
            Filter::Gt(_) => write!(f, ">"),
            Filter::Gte(_) => write!(f, ">="),
            Filter::Lt(_) => write!(f, "<"),
            Filter::Lte(_) => write!(f, "<="),
            Filter::IsNull => write!(f, "IS NULL"),
            Filter::StatusIs(_) => write!(f, "=")
        }
    }
}

// Filter / Where Operators
pub enum FilterOp<F: ToField> {
    And(F, Filter),
    Or(F, Filter),
    Begin(F, Filter)
}

impl<F: ToField> Display for FilterOp<F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FilterOp::Begin(field, cond) => write!(f, "{} {}", field, cond),
            FilterOp::And(field, cond) => write!(f, "AND {} {}", field, cond),
            FilterOp::Or(field, cond) => write!(f, "OR {} {}", field, cond)
        }
    }
}

impl<F: ToField> Debug for FilterOp<F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FilterOp::Begin(_, cond) => write!(f, "{:?}", cond),
            FilterOp::And(_, cond) => write!(f, "{:?}", cond),
            FilterOp::Or(_, cond) => write!(f, "{:?}", cond)
        }
    }
}

impl<F: ToField> BindArgs for FilterOp<F> {
    fn bind(&self, args: &mut sqlx::postgres::PgArguments) {
        match self {
            Self::Begin(_, w) => w.bind(args),
            Self::And(_, w) => w.bind(args),
            Self::Or(_, w) => w.bind(args)
        }
    }
    fn bind_len(&self) -> usize {
        match self {
            Self::Begin(_, w) => w.bind_len(),
            Self::And(_, w) => w.bind_len(),
            Self::Or(_, w) => w.bind_len()
        }
    }
}

// Static method to extract the Where block of the Sql Query. They will always be the same / have
// the same structure
pub fn sql_where<F: ToField>(
    w: &[FilterOp<F>],
    idx: usize,
    from_update_patch: Option<String>
) -> String {
    let update_batch_ref_table = match from_update_patch {
        Some(t) => format!("{t}."),
        None => "".into()
    };

    let mut f_idx = 0;
    let whr = w
        .iter()
        .map(|f| {
            let (kw, field_str) = match f {
                FilterOp::Begin(c, _) => ("", format!("{c}")),
                FilterOp::And(c, _) => ("AND ", format!("{c}")),
                FilterOp::Or(c, _) => ("OR ", format!("{c}"))
            };
            let filter_val = match f {
                FilterOp::Begin(_, v) | FilterOp::And(_, v) | FilterOp::Or(_, v) => v
            };
            match filter_val {
                Filter::IsNull => {
                    format!("\n\t{}{}{} IS NULL", kw, update_batch_ref_table, field_str)
                }
                Filter::PgEnumCast(_, pg_type) => {
                    f_idx += f.bind_len();
                    format!(
                        "\n\t{}{}{} = ${}::{pg_type}",
                        kw,
                        update_batch_ref_table,
                        field_str,
                        f_idx + idx
                    )
                }
                Filter::In(_) => {
                    f_idx += f.bind_len();
                    format!(
                        "\n\t{}{}{} = ANY(${})",
                        kw,
                        update_batch_ref_table,
                        field_str,
                        f_idx + idx
                    )
                }
                Filter::NotIn(_) => {
                    f_idx += f.bind_len();
                    format!(
                        "\n\t{}{}{} <> ALL(${})",
                        kw,
                        update_batch_ref_table,
                        field_str,
                        f_idx + idx
                    )
                }
                v => {
                    f_idx += f.bind_len();
                    format!(
                        "\n\t{}{}{} {} ${}",
                        kw,
                        update_batch_ref_table,
                        field_str,
                        v,
                        f_idx + idx
                    )
                }
            }
        })
        .collect::<Vec<_>>()
        .join(" ");
    if !whr.is_empty() {
        return format!("\nWHERE{}", whr);
    }
    whr
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::repo::filter::{Filter, FilterOp};
    use crate::testing::must::must_be_true;

    /// Minimal field enum used only in unit tests so we don't need a real schema.
    #[derive(Clone, Copy)]
    enum TestField {
        Name,
        Age
    }

    impl Display for TestField {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                TestField::Name => write!(f, "name"),
                TestField::Age => write!(f, "age")
            }
        }
    }

    impl ToSqlParts for TestField {
        fn to_sql_parts(&self) -> AsSqlParts {
            (vec![format!("{}", self)], None)
        }
    }

    /// Regression test for #95: with a table alias, `FilterOp::And` must produce
    /// `AND alias.field = $N`, not `alias.AND field = $N`.
    #[test]
    fn sql_where_alias_precedes_keyword_not_field() {
        let filters = vec![
            FilterOp::Begin(TestField::Name, Filter::Equals(1)),
            FilterOp::And(TestField::Age, Filter::Equals(30)),
        ];

        let sql = sql_where(&filters, 0, Some("_x_".into()));

        must_be_true(sql.contains("AND _x_.age"));
        must_be_true(!sql.contains("_x_.AND"));
    }

    /// Same regression check for `FilterOp::Or`.
    #[test]
    fn sql_where_or_alias_precedes_keyword_not_field() {
        let filters = vec![
            FilterOp::Begin(TestField::Name, Filter::Equals(1)),
            FilterOp::Or(TestField::Age, Filter::Equals(30)),
        ];

        let sql = sql_where(&filters, 0, Some("_x_".into()));

        must_be_true(sql.contains("OR _x_.age"));
        must_be_true(!sql.contains("_x_.OR"));
    }

    /// `FilterOp::Begin` (no keyword) should still get the alias applied to the field.
    #[test]
    fn sql_where_begin_with_alias() {
        let filters = vec![FilterOp::Begin(TestField::Name, Filter::Equals(1))];

        let sql = sql_where(&filters, 0, Some("_x_".into()));

        must_be_true(sql.contains("_x_.name"));
    }

    /// `Filter::IsNull` with an alias and `FilterOp::And` must produce
    /// `AND alias.field IS NULL`, not `alias.AND field IS NULL`.
    #[test]
    fn sql_where_is_null_with_alias_and_keyword() {
        let filters = vec![
            FilterOp::Begin(TestField::Name, Filter::Equals(1)),
            FilterOp::And(TestField::Age, Filter::IsNull),
        ];

        let sql = sql_where(&filters, 0, Some("_x_".into()));

        must_be_true(sql.contains("AND _x_.age IS NULL"));
        must_be_true(!sql.contains("_x_.AND"));
    }

    /// Without an alias the output should be unchanged: keyword first, then field.
    #[test]
    fn sql_where_no_alias_and_keyword() {
        let filters = vec![
            FilterOp::Begin(TestField::Name, Filter::Equals(1)),
            FilterOp::And(TestField::Age, Filter::Equals(30)),
        ];

        let sql = sql_where(&filters, 0, None);

        must_be_true(sql.contains("AND age"));
    }

    #[test]
    fn sql_where_in_uses_any() {
        let filters = vec![FilterOp::Begin(TestField::Age, Filter::In(vec![1, 2, 3]))];

        let sql = sql_where(&filters, 0, None);

        must_be_true(sql.contains("age = ANY($1)"));
        must_be_true(!sql.contains("age IN $1"));
    }
}