good_ormning/sqlite/query/
helpers.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
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
use {
    super::expr::{
        BinOp,
        ComputeType,
        Expr,
        Binding,
    },
    crate::sqlite::{
        schema::field::Field,
        types::{
            SimpleSimpleType,
            SimpleType,
            Type,
        },
    },
    flowcontrol::shed,
};

/// Generates a field element for instert and update statements, to set a field
/// from a parameter of the same type.
pub fn set_field(param_name: impl Into<String>, f: &Field) -> (Field, Expr) {
    (f.clone(), field_param(param_name, f))
}

/// Generates a param matching a field in name in type
pub fn field_param(param_name: impl Into<String>, f: &Field) -> Expr {
    Expr::Param {
        name: param_name.into(),
        type_: f.type_.type_.clone(),
    }
}

/// Generates an expression checking for equality of a field and a parameter and
/// the same type.
pub fn expr_field_eq(param_name: impl Into<String>, f: &Field) -> Expr {
    Expr::BinOp {
        left: Box::new(Expr::Binding(Binding::field(f))),
        op: BinOp::Equals,
        right: Box::new(Expr::Param {
            name: param_name.into(),
            type_: f.type_.type_.clone(),
        }),
    }
}

/// Generates an expression selecting field values greater than a corresponding
/// parameter
pub fn expr_field_gt(param_name: impl Into<String>, f: &Field) -> Expr {
    Expr::BinOp {
        left: Box::new(Expr::Binding(Binding::field(f))),
        op: BinOp::GreaterThan,
        right: Box::new(Expr::Param {
            name: param_name.into(),
            type_: f.type_.type_.clone(),
        }),
    }
}

/// Generates an expression selecting field values greater than or equal to a
/// corresponding parameter
pub fn expr_field_gte(param_name: impl Into<String>, f: &Field) -> Expr {
    Expr::BinOp {
        left: Box::new(Expr::Binding(Binding::field(f))),
        op: BinOp::GreaterThanEqualTo,
        right: Box::new(Expr::Param {
            name: param_name.into(),
            type_: f.type_.type_.clone(),
        }),
    }
}

/// Generates an expression selecting field values greater than a corresponding
/// parameter
pub fn expr_field_lt(param_name: impl Into<String>, f: &Field) -> Expr {
    Expr::BinOp {
        left: Box::new(Expr::Binding(Binding::field(f))),
        op: BinOp::LessThan,
        right: Box::new(Expr::Param {
            name: param_name.into(),
            type_: f.type_.type_.clone(),
        }),
    }
}

/// Generates an expression selecting field values greater than or equal to a
/// corresponding parameter
pub fn expr_field_lte(param_name: impl Into<String>, f: &Field) -> Expr {
    Expr::BinOp {
        left: Box::new(Expr::Binding(Binding::field(f))),
        op: BinOp::LessThanEqualTo,
        right: Box::new(Expr::Param {
            name: param_name.into(),
            type_: f.type_.type_.clone(),
        }),
    }
}

/// Shortcut for AND expressions.
pub fn expr_and(exprs: Vec<Expr>) -> Expr {
    Expr::BinOpChain {
        op: BinOp::And,
        exprs: exprs,
    }
}

#[cfg(feature = "chrono")]
pub fn as_utc(expr: Expr) -> Expr {
    return Expr::Call {
        func: "strftime".to_string(),
        args: vec![Expr::LitString("%Y-%m-%dT%H:%M:%f".to_string()), expr],
        compute_type: ComputeType::new(|ctx, path, args| {
            shed!{
                let arg = args.get(1).unwrap();
                let Some(type_) = arg.0.iter().next() else {
                    break;
                };
                if !matches!(type_.1.type_.type_, SimpleSimpleType::FixedOffsetTimeMs) {
                    ctx
                        .errs
                        .err(
                            path,
                            format!(
                                "This method only operates on fixed-offset timestamps, but the argument is of type {:?}",
                                type_.1.type_.type_
                            ),
                        );
                }
            };
            return Some(Type {
                type_: SimpleType {
                    type_: SimpleSimpleType::UtcTimeMs,
                    custom: None,
                },
                opt: false,
            });
        }),
    }
}

pub fn fn_min(expr: Expr) -> Expr {
    return Expr::Call {
        func: "min".to_string(),
        args: vec![expr],
        compute_type: ComputeType::new(|ctx, path, args| {
            let Some(t) = args.get(0).unwrap().assert_scalar(&mut ctx.errs, path) else {
                return None;
            };
            return Some(t.1);
        }),
    }
}

pub fn fn_max(expr: Expr) -> Expr {
    return Expr::Call {
        func: "max".to_string(),
        args: vec![expr],
        compute_type: ComputeType::new(|ctx, path, args| {
            let Some(t) = args.get(0).unwrap().assert_scalar(&mut ctx.errs, path) else {
                return None;
            };
            return Some(t.1);
        }),
    }
}

pub fn fn_avg(expr: Expr) -> Expr {
    return Expr::Call {
        func: "avg".to_string(),
        args: vec![expr],
        compute_type: ComputeType::new(|ctx, path, args| {
            let Some(t) = args.get(0).unwrap().assert_scalar(&mut ctx.errs, path) else {
                return None;
            };
            return Some(t.1);
        }),
    }
}

pub fn fn_count(expr: Expr) -> Expr {
    return Expr::Call {
        func: "count".to_string(),
        args: vec![expr],
        compute_type: ComputeType::new(|_ctx, _path, _args| {
            return Some(Type {
                type_: SimpleType {
                    type_: SimpleSimpleType::I64,
                    custom: None,
                },
                opt: false,
            });
        }),
    }
}