teaql-core 5.0.7

TeaQL core, SQL, runtime, dialect, and macro crates for model-driven data access
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
use crate::{EntityDescriptor, SelectQuery, Value};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
    Eq,
    Ne,
    Gt,
    Gte,
    Lt,
    Lte,
    Like,
    NotLike,
    In,
    NotIn,
    InLarge,
    NotInLarge,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExprFunction {
    Soundex,
    Gbk,
    Count,
    Sum,
    Avg,
    Min,
    Max,
    Stddev,
    StddevPop,
    VarSamp,
    VarPop,
    BitAnd,
    BitOr,
    BitXor,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
    Column(String),
    Value(Value),
    /// Compiler provenance for typed LIKE helpers. `pattern` is the unchanged
    /// execution value; `original` is used only for classified intent projection.
    /// Raw `like`/`not_like` expressions deliberately do not create this leaf.
    #[doc(hidden)]
    LikePattern {
        pattern: String,
        original: String,
    },
    Function {
        function: ExprFunction,
        args: Vec<Expr>,
    },
    Binary {
        left: Box<Expr>,
        op: BinaryOp,
        right: Box<Expr>,
    },
    SubQuery {
        left: Box<Expr>,
        op: BinaryOp,
        entity: EntityDescriptor,
        query: Box<SelectQuery>,
    },
    Between {
        expr: Box<Expr>,
        lower: Box<Expr>,
        upper: Box<Expr>,
    },
    IsNull(Box<Expr>),
    IsNotNull(Box<Expr>),
    And(Vec<Expr>),
    Or(Vec<Expr>),
    Not(Box<Expr>),
}

impl Expr {
    /// Match the long-standing Java runtime policy: collections larger than
    /// twenty values use the dialect-specific large-set representation.
    pub const LARGE_IN_THRESHOLD: usize = 20;

    pub fn column(name: impl Into<String>) -> Self {
        Self::Column(name.into())
    }

    pub fn value(value: impl Into<Value>) -> Self {
        Self::Value(value.into())
    }

    pub fn function(function: ExprFunction, args: impl IntoIterator<Item = Expr>) -> Self {
        Self::Function {
            function,
            args: args.into_iter().collect(),
        }
    }

    pub fn soundex(expr: Expr) -> Self {
        Self::function(ExprFunction::Soundex, [expr])
    }

    pub fn gbk(expr: Expr) -> Self {
        Self::function(ExprFunction::Gbk, [expr])
    }

    pub fn count_all() -> Self {
        Self::function(ExprFunction::Count, [])
    }

    pub fn count_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::Count, [expr])
    }

    pub fn sum_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::Sum, [expr])
    }

    pub fn avg_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::Avg, [expr])
    }

    pub fn min_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::Min, [expr])
    }

    pub fn max_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::Max, [expr])
    }

    pub fn stddev_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::Stddev, [expr])
    }

    pub fn stddev_pop_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::StddevPop, [expr])
    }

    pub fn var_samp_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::VarSamp, [expr])
    }

    pub fn var_pop_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::VarPop, [expr])
    }

    pub fn bit_and_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::BitAnd, [expr])
    }

    pub fn bit_or_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::BitOr, [expr])
    }

    pub fn bit_xor_expr(expr: Expr) -> Self {
        Self::function(ExprFunction::BitXor, [expr])
    }

    pub fn sound_like(column: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::binary(
            Self::soundex(Self::column(column)),
            BinaryOp::Eq,
            Self::soundex(Self::value(value)),
        )
    }

    pub fn eq(column: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::binary(Self::column(column), BinaryOp::Eq, Self::value(value))
    }

    pub fn ne(column: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::binary(Self::column(column), BinaryOp::Ne, Self::value(value))
    }

    pub fn gt(column: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::binary(Self::column(column), BinaryOp::Gt, Self::value(value))
    }

    pub fn gte(column: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::binary(Self::column(column), BinaryOp::Gte, Self::value(value))
    }

    pub fn lt(column: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::binary(Self::column(column), BinaryOp::Lt, Self::value(value))
    }

    pub fn lte(column: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::binary(Self::column(column), BinaryOp::Lte, Self::value(value))
    }

    pub fn like(column: impl Into<String>, pattern: impl Into<String>) -> Self {
        Self::binary(
            Self::column(column),
            BinaryOp::Like,
            Self::value(pattern.into()),
        )
    }

    pub fn not_like(column: impl Into<String>, pattern: impl Into<String>) -> Self {
        Self::binary(
            Self::column(column),
            BinaryOp::NotLike,
            Self::value(pattern.into()),
        )
    }

    pub fn contain(column: impl Into<String>, value: impl Into<String>) -> Self {
        Self::typed_like(column, value.into(), BinaryOp::Like, "%", "%")
    }

    pub fn not_contain(column: impl Into<String>, value: impl Into<String>) -> Self {
        Self::typed_like(column, value.into(), BinaryOp::NotLike, "%", "%")
    }

    pub fn begin_with(column: impl Into<String>, value: impl Into<String>) -> Self {
        Self::typed_like(column, value.into(), BinaryOp::Like, "", "%")
    }

    pub fn not_begin_with(column: impl Into<String>, value: impl Into<String>) -> Self {
        Self::typed_like(column, value.into(), BinaryOp::NotLike, "", "%")
    }

    pub fn end_with(column: impl Into<String>, value: impl Into<String>) -> Self {
        Self::typed_like(column, value.into(), BinaryOp::Like, "%", "")
    }

    pub fn not_end_with(column: impl Into<String>, value: impl Into<String>) -> Self {
        Self::typed_like(column, value.into(), BinaryOp::NotLike, "%", "")
    }

    fn typed_like(
        column: impl Into<String>,
        original: String,
        op: BinaryOp,
        prefix: &str,
        suffix: &str,
    ) -> Self {
        Self::binary(
            Self::column(column),
            op,
            Self::LikePattern {
                pattern: format!("{prefix}{original}{suffix}"),
                original,
            },
        )
    }

    pub fn binary(left: Expr, op: BinaryOp, right: Expr) -> Self {
        Self::Binary {
            left: Box::new(left),
            op,
            right: Box::new(right),
        }
    }

    pub fn compare_columns(
        left_column: impl Into<String>,
        op: BinaryOp,
        right_column: impl Into<String>,
    ) -> Self {
        Self::binary(Self::column(left_column), op, Self::column(right_column))
    }

    pub fn in_list(column: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Self {
        let values = values.into_iter().collect::<Vec<_>>();
        Self::binary(
            Self::column(column),
            if values.len() > Self::LARGE_IN_THRESHOLD {
                BinaryOp::InLarge
            } else {
                BinaryOp::In
            },
            Self::Value(Value::List(values)),
        )
    }

    pub fn not_in_list(column: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Self {
        let values = values.into_iter().collect::<Vec<_>>();
        Self::binary(
            Self::column(column),
            if values.len() > Self::LARGE_IN_THRESHOLD {
                BinaryOp::NotInLarge
            } else {
                BinaryOp::NotIn
            },
            Self::Value(Value::List(values)),
        )
    }

    pub fn in_large(column: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Self {
        Self::binary(
            Self::column(column),
            BinaryOp::InLarge,
            Self::Value(Value::List(values.into_iter().collect())),
        )
    }

    pub fn not_in_large(
        column: impl Into<String>,
        values: impl IntoIterator<Item = Value>,
    ) -> Self {
        Self::binary(
            Self::column(column),
            BinaryOp::NotInLarge,
            Self::Value(Value::List(values.into_iter().collect())),
        )
    }

    pub fn in_subquery(
        column: impl Into<String>,
        entity: EntityDescriptor,
        query: SelectQuery,
        field: impl Into<String>,
    ) -> Self {
        Self::subquery(Self::column(column), BinaryOp::In, entity, query, field)
    }

    pub fn not_in_subquery(
        column: impl Into<String>,
        entity: EntityDescriptor,
        query: SelectQuery,
        field: impl Into<String>,
    ) -> Self {
        Self::subquery(Self::column(column), BinaryOp::NotIn, entity, query, field)
    }

    pub fn subquery(
        left: Expr,
        op: BinaryOp,
        entity: EntityDescriptor,
        mut query: SelectQuery,
        field: impl Into<String>,
    ) -> Self {
        let field = field.into();
        // SQL `NOT IN` becomes UNKNOWN for every outer row when the projected
        // set contains NULL. Relation non-existence and negative matching must
        // ignore orphan child rows instead of poisoning the entire predicate.
        if matches!(op, BinaryOp::NotIn | BinaryOp::NotInLarge) {
            query = query.and_filter(Self::is_not_null(field.clone()));
        }
        query.projection = vec![field];
        Self::SubQuery {
            left: Box::new(left),
            op,
            entity,
            query: Box::new(query),
        }
    }

    pub fn between(
        column: impl Into<String>,
        lower: impl Into<Value>,
        upper: impl Into<Value>,
    ) -> Self {
        Self::Between {
            expr: Box::new(Self::column(column)),
            lower: Box::new(Self::value(lower)),
            upper: Box::new(Self::value(upper)),
        }
    }

    pub fn is_null(column: impl Into<String>) -> Self {
        Self::IsNull(Box::new(Self::column(column)))
    }

    pub fn is_not_null(column: impl Into<String>) -> Self {
        Self::IsNotNull(Box::new(Self::column(column)))
    }

    pub fn and(parts: impl IntoIterator<Item = Expr>) -> Self {
        let mut unique = Vec::new();
        for part in parts {
            if !unique.contains(&part) {
                unique.push(part);
            }
        }
        Self::And(unique)
    }

    pub fn or(parts: impl IntoIterator<Item = Expr>) -> Self {
        let mut unique = Vec::new();
        for part in parts {
            if !unique.contains(&part) {
                unique.push(part);
            }
        }
        Self::Or(unique)
    }

    pub fn negate(expr: Expr) -> Self {
        Self::Not(Box::new(expr))
    }

    pub fn and_expr(self, other: Expr) -> Self {
        if self == other {
            return self;
        }
        match self {
            Self::And(mut parts) => {
                if !parts.contains(&other) {
                    parts.push(other);
                }
                Self::And(parts)
            }
            expr => Self::And(vec![expr, other]),
        }
    }

    pub fn or_expr(self, other: Expr) -> Self {
        if self == other {
            return self;
        }
        match self {
            Self::Or(mut parts) => {
                if !parts.contains(&other) {
                    parts.push(other);
                }
                Self::Or(parts)
            }
            expr => Self::Or(vec![expr, other]),
        }
    }
}