qraft-core 0.1.2

Core type system, query model, decoding, and SQL lowering primitives for qraft.
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
//! JSON path and predicate expressions.

use std::fmt::Write;

use crate::{
    BigInt, Nullable, Text,
    expression::{Column, Expression, In, InList, LowerIn},
    lower::{Data, Instructions, JsonContainsLower, LowerCtx, Params},
    param::Param,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum JsonPathSegment<'a> {
    Key(&'a str),
    Index(&'a str),
}

fn parse_json_path(path: &str) -> Vec<JsonPathSegment<'_>> {
    assert!(!path.is_empty(), "json path cannot be empty");

    path.split("->")
        .map(|segment| {
            assert!(!segment.is_empty(), "json path contains an empty segment");
            if segment.bytes().all(|byte| byte.is_ascii_digit()) {
                JsonPathSegment::Index(segment)
            } else {
                assert!(
                    segment
                        .bytes()
                        .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_'),
                    "json path segment `{segment}` contains unsupported characters",
                );
                JsonPathSegment::Key(segment)
            }
        })
        .collect()
}

fn write_json_string(out: &mut String, value: &str) {
    out.push('"');
    for ch in value.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\u{08}' => out.push_str("\\b"),
            '\u{0C}' => out.push_str("\\f"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            ch if ch <= '\u{1F}' => {
                let _ = write!(out, "\\u{:04x}", ch as u32);
            }
            ch => out.push(ch),
        }
    }
    out.push('"');
}

#[doc(hidden)]
pub trait JsonLiteral {
    fn write_json(&self, out: &mut String);
    fn is_compound(&self) -> bool {
        false
    }
}

impl JsonLiteral for str {
    fn write_json(&self, out: &mut String) {
        write_json_string(out, self);
    }
}

impl JsonLiteral for String {
    fn write_json(&self, out: &mut String) {
        self.as_str().write_json(out);
    }
}

impl JsonLiteral for &str {
    fn write_json(&self, out: &mut String) {
        (*self).write_json(out);
    }
}

impl JsonLiteral for bool {
    fn write_json(&self, out: &mut String) {
        out.push_str(if *self { "true" } else { "false" });
    }
}

macro_rules! impl_json_number {
    ($($ty:ty),+ $(,)?) => {
        $(
            impl JsonLiteral for $ty {
                fn write_json(&self, out: &mut String) {
                    let _ = write!(out, "{}", self);
                }
            }
        )+
    };
}

impl_json_number!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, f32, f64);

impl<T> JsonLiteral for Vec<T>
where
    T: JsonLiteral,
{
    fn write_json(&self, out: &mut String) {
        out.push('[');
        for (index, item) in self.iter().enumerate() {
            if index > 0 {
                out.push(',');
            }
            item.write_json(out);
        }
        out.push(']');
    }

    fn is_compound(&self) -> bool {
        true
    }
}

impl<T, const N: usize> JsonLiteral for [T; N]
where
    T: JsonLiteral,
{
    fn write_json(&self, out: &mut String) {
        out.push('[');
        for (index, item) in self.iter().enumerate() {
            if index > 0 {
                out.push(',');
            }
            item.write_json(out);
        }
        out.push(']');
    }

    fn is_compound(&self) -> bool {
        true
    }
}

#[cfg(feature = "serde_json")]
impl JsonLiteral for serde_json::Value {
    fn write_json(&self, out: &mut String) {
        out.push_str(&serde_json::to_string(self).expect("json serialization should succeed"));
    }

    fn is_compound(&self) -> bool {
        matches!(
            self,
            serde_json::Value::Array(_) | serde_json::Value::Object(_)
        )
    }
}

pub struct JsonPath<E> {
    inner: E,
    path: &'static str,
}

pub struct JsonContains<E, V> {
    inner: E,
    path: &'static str,
    value: V,
    negated: bool,
}

pub struct JsonContainsKey<E> {
    inner: E,
    path: &'static str,
    negated: bool,
}

pub struct JsonLength<E> {
    inner: E,
    path: &'static str,
}

pub trait JsonPathExt: Sized {
    fn json_path(self, path: &'static str) -> JsonPath<Self>;
}

impl<M> JsonPathExt for Column<M, Text> {
    fn json_path(self, path: &'static str) -> JsonPath<Self> {
        let _ = parse_json_path(path);
        JsonPath { inner: self, path }
    }
}

impl<M> JsonPathExt for Column<M, Nullable<Text>> {
    fn json_path(self, path: &'static str) -> JsonPath<Self> {
        let _ = parse_json_path(path);
        JsonPath { inner: self, path }
    }
}

impl<E> JsonPath<E> {
    pub fn one_of<R>(self, list: R) -> InList<Self, R>
    where
        Self: Expression<Type = Nullable<Text>>,
        R: LowerIn<Nullable<Text>>,
    {
        In::one_of(self, list)
    }

    pub fn json_contains<V>(self, value: V) -> JsonContains<E, V>
    where
        V: JsonLiteral,
    {
        JsonContains {
            inner: self.inner,
            path: self.path,
            value,
            negated: false,
        }
    }

    pub fn json_doesnt_contain<V>(self, value: V) -> JsonContains<E, V>
    where
        V: JsonLiteral,
    {
        JsonContains {
            inner: self.inner,
            path: self.path,
            value,
            negated: true,
        }
    }

    pub fn json_contains_key(self) -> JsonContainsKey<E> {
        JsonContainsKey {
            inner: self.inner,
            path: self.path,
            negated: false,
        }
    }

    pub fn json_doesnt_contain_key(self) -> JsonContainsKey<E> {
        JsonContainsKey {
            inner: self.inner,
            path: self.path,
            negated: true,
        }
    }

    pub fn json_length(self) -> JsonLength<E> {
        JsonLength {
            inner: self.inner,
            path: self.path,
        }
    }
}

#[qraft_expression_macro::as_expression]
impl<E> Expression for JsonPath<E>
where
    E: Expression,
{
    type Type = Nullable<Text>;

    fn lower(&self, ctx: &mut LowerCtx) -> usize {
        let inner = self.inner.lower(ctx);
        ctx.lower_json_extract_text(inner, self.path)
    }
}

#[qraft_expression_macro::as_expression]
impl<E, V> Expression for JsonContains<E, V>
where
    E: Expression,
    V: JsonLiteral,
{
    type Type = crate::Bool;

    fn lower(&self, ctx: &mut LowerCtx) -> usize {
        let inner = self.inner.lower(ctx);
        let mut json = String::new();
        self.value.write_json(&mut json);
        let span = ctx.data.intern_text(&json);
        let id = ctx.params.push_param(Param::Text(Some(span)));
        ctx.instrs.push_param(id);
        ctx.lower_json_contains(
            inner,
            JsonContainsLower {
                rhs: 1,
                path: self.path,
                negated: self.negated,
                compound: self.value.is_compound(),
            },
        )
    }
}

#[qraft_expression_macro::as_expression]
impl<E> Expression for JsonContainsKey<E>
where
    E: Expression,
{
    type Type = crate::Bool;

    fn lower(&self, ctx: &mut LowerCtx) -> usize {
        let inner = self.inner.lower(ctx);
        ctx.lower_json_contains_key(inner, self.path, self.negated)
    }
}

#[qraft_expression_macro::as_expression]
impl<E> Expression for JsonLength<E>
where
    E: Expression,
{
    type Type = BigInt;

    fn lower(&self, ctx: &mut LowerCtx) -> usize {
        let inner = self.inner.lower(ctx);
        ctx.lower_json_length(inner, self.path)
    }
}

#[cfg(test)]
mod tests {
    use super::JsonPathExt;
    use crate::{
        MySql, Postgres, Sqlite, Text,
        expression::{EqExt, OrderExt, col},
        query::select_all,
    };

    #[test]
    fn json_path_eq_sqlite_sql() {
        let preferences = col::<Text>("preferences");
        let sql = select_all()
            .from("users")
            .filter(preferences.json_path("dining->meal").eq("salad"))
            .to_debug_sql::<Sqlite>();

        assert_eq!(
            sql,
            r#"select * from "users" where json_extract("preferences", '$.dining.meal') = ?; params=["salad"]"#
        );
    }

    #[test]
    fn json_path_eq_postgres_sql() {
        let preferences = col::<Text>("preferences");
        let sql = select_all()
            .from("users")
            .filter(preferences.json_path("dining->meal").eq("salad"))
            .to_debug_sql::<Postgres>();

        assert_eq!(
            sql,
            r#"select * from "users" where jsonb_extract_path_text(("preferences")::jsonb, 'dining', 'meal') = $1; params=["salad"]"#
        );
    }

    #[test]
    fn json_path_eq_mariadb_sql() {
        let preferences = col::<Text>("preferences");
        let sql = select_all()
            .from("users")
            .filter(preferences.json_path("dining->meal").eq("salad"))
            .to_debug_sql::<MySql>();

        assert_eq!(
            sql,
            "select * from `users` where json_unquote(json_extract(`preferences`, '$.dining.meal')) = ?; params=[\"salad\"]"
        );
    }

    #[test]
    fn json_path_one_of_sqlite_sql() {
        let preferences = col::<Text>("preferences");
        let sql = select_all()
            .from("users")
            .filter(
                preferences
                    .json_path("dining->meal")
                    .one_of(["pasta", "salad"]),
            )
            .to_debug_sql::<Sqlite>();

        assert_eq!(
            sql,
            r#"select * from "users" where json_extract("preferences", '$.dining.meal') in (?, ?); params=["pasta", "salad"]"#
        );
    }

    #[test]
    fn json_contains_key_and_length_sqlite_sql() {
        let options = col::<Text>("options");
        let contains = select_all()
            .from("users")
            .filter(options.json_path("languages").json_contains_key())
            .to_debug_sql::<Sqlite>();
        let length = select_all()
            .from("users")
            .filter(options.json_path("languages").json_length().gt(1_i64))
            .to_debug_sql::<Sqlite>();

        assert_eq!(
            contains,
            r#"select * from "users" where json_type("options", '$.languages') is not null; params=[]"#
        );
        assert_eq!(
            length,
            r#"select * from "users" where json_array_length(json_extract("options", '$.languages')) > ?; params=[1]"#
        );
    }

    #[test]
    fn json_contains_sqlite_sql() {
        let options = col::<Text>("options");
        let sql = select_all()
            .from("users")
            .filter(options.json_path("languages").json_contains("en"))
            .to_debug_sql::<Sqlite>();

        assert_eq!(
            sql,
            r#"select * from "users" where exists (select 1 from json_each(json_extract("options", '$.languages')) where json_each.value = json_extract(?, '$')); params=[""en""]"#
        );
    }
}