oneiriq-surql 0.2.2

Code-first database toolkit for SurrealDB - schema definitions, migrations, query building, and typed CRUD (Rust port of oneiriq-surql). Published as the `oneiriq-surql` crate; imported as `use surql::...`.
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//! Query operators for building type-safe SurrealDB expressions.
//!
//! Port of `surql/types/operators.py`. Python subclasses are represented
//! here by a single [`Operator`] enum plus type aliases that reuse its
//! variants via the specific constructor helpers.

use serde_json::Value;

use super::record_id::RecordIdValue;
use super::record_ref::{record_ref, RecordRef};
use super::surreal_fn::SurrealFn;

use crate::query::expressions::Expression;

/// Trait implemented by every operator so they can all produce SurrealQL.
pub trait OperatorExpr {
    /// Render this operator as a SurrealQL expression.
    fn to_surql(&self) -> String;
}

/// A composed query expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Operator {
    /// `field = value`
    Eq(Eq),
    /// `field != value`
    Ne(Ne),
    /// `field > value`
    Gt(Gt),
    /// `field >= value`
    Gte(Gte),
    /// `field < value`
    Lt(Lt),
    /// `field <= value`
    Lte(Lte),
    /// `field CONTAINS value`
    Contains(Contains),
    /// `field CONTAINSNOT value`
    ContainsNot(ContainsNot),
    /// `field CONTAINSALL [...]`
    ContainsAll(ContainsAll),
    /// `field CONTAINSANY [...]`
    ContainsAny(ContainsAny),
    /// `field INSIDE [...]`
    Inside(Inside),
    /// `field NOTINSIDE [...]`
    NotInside(NotInside),
    /// `field IS NULL`
    IsNull(IsNull),
    /// `field IS NOT NULL`
    IsNotNull(IsNotNull),
    /// `(left) AND (right)`
    And(And),
    /// `(left) OR (right)`
    Or(Or),
    /// `NOT (operand)`
    Not(Not),
}

impl OperatorExpr for Operator {
    fn to_surql(&self) -> String {
        match self {
            Self::Eq(x) => x.to_surql(),
            Self::Ne(x) => x.to_surql(),
            Self::Gt(x) => x.to_surql(),
            Self::Gte(x) => x.to_surql(),
            Self::Lt(x) => x.to_surql(),
            Self::Lte(x) => x.to_surql(),
            Self::Contains(x) => x.to_surql(),
            Self::ContainsNot(x) => x.to_surql(),
            Self::ContainsAll(x) => x.to_surql(),
            Self::ContainsAny(x) => x.to_surql(),
            Self::Inside(x) => x.to_surql(),
            Self::NotInside(x) => x.to_surql(),
            Self::IsNull(x) => x.to_surql(),
            Self::IsNotNull(x) => x.to_surql(),
            Self::And(x) => x.to_surql(),
            Self::Or(x) => x.to_surql(),
            Self::Not(x) => x.to_surql(),
        }
    }
}

macro_rules! binary_comparison {
    ($(#[$meta:meta])* $name:ident, $sql:literal) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq)]
        pub struct $name {
            /// Field name.
            pub field: String,
            /// Right-hand value.
            pub value: Value,
        }

        impl $name {
            /// Construct a new operator.
            pub fn new(field: impl Into<String>, value: impl Into<Value>) -> Self {
                Self {
                    field: field.into(),
                    value: value.into(),
                }
            }
        }

        impl OperatorExpr for $name {
            fn to_surql(&self) -> String {
                format!("{} {} {}", self.field, $sql, quote_value(&self.value))
            }
        }
    };
}

binary_comparison!(
    /// `field = value`
    Eq,
    "="
);
binary_comparison!(
    /// `field != value`
    Ne,
    "!="
);
binary_comparison!(
    /// `field > value`
    Gt,
    ">"
);
binary_comparison!(
    /// `field >= value`
    Gte,
    ">="
);
binary_comparison!(
    /// `field < value`
    Lt,
    "<"
);
binary_comparison!(
    /// `field <= value`
    Lte,
    "<="
);
binary_comparison!(
    /// `field CONTAINS value`
    Contains,
    "CONTAINS"
);
binary_comparison!(
    /// `field CONTAINSNOT value`
    ContainsNot,
    "CONTAINSNOT"
);

macro_rules! array_comparison {
    ($(#[$meta:meta])* $name:ident, $sql:literal) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq)]
        pub struct $name {
            /// Field name.
            pub field: String,
            /// Right-hand list of values.
            pub values: Vec<Value>,
        }

        impl $name {
            /// Construct a new operator.
            pub fn new(field: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Self {
                Self {
                    field: field.into(),
                    values: values.into_iter().collect(),
                }
            }
        }

        impl OperatorExpr for $name {
            fn to_surql(&self) -> String {
                let rendered = self
                    .values
                    .iter()
                    .map(quote_value)
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{} {} [{}]", self.field, $sql, rendered)
            }
        }
    };
}

array_comparison!(
    /// `field CONTAINSALL [...]`
    ContainsAll,
    "CONTAINSALL"
);
array_comparison!(
    /// `field CONTAINSANY [...]`
    ContainsAny,
    "CONTAINSANY"
);
array_comparison!(
    /// `field INSIDE [...]`
    Inside,
    "INSIDE"
);
array_comparison!(
    /// `field NOTINSIDE [...]`
    NotInside,
    "NOTINSIDE"
);

/// `field IS NULL`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IsNull {
    /// Field name.
    pub field: String,
}

impl IsNull {
    /// Construct `IS NULL` for the given field.
    pub fn new(field: impl Into<String>) -> Self {
        Self {
            field: field.into(),
        }
    }
}

impl OperatorExpr for IsNull {
    fn to_surql(&self) -> String {
        format!("{} IS NULL", self.field)
    }
}

/// `field IS NOT NULL`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IsNotNull {
    /// Field name.
    pub field: String,
}

impl IsNotNull {
    /// Construct `IS NOT NULL` for the given field.
    pub fn new(field: impl Into<String>) -> Self {
        Self {
            field: field.into(),
        }
    }
}

impl OperatorExpr for IsNotNull {
    fn to_surql(&self) -> String {
        format!("{} IS NOT NULL", self.field)
    }
}

/// Logical AND of two operators.
#[derive(Debug, Clone, PartialEq)]
pub struct And {
    /// Left operand.
    pub left: Box<Operator>,
    /// Right operand.
    pub right: Box<Operator>,
}

impl OperatorExpr for And {
    fn to_surql(&self) -> String {
        format!("({}) AND ({})", self.left.to_surql(), self.right.to_surql())
    }
}

/// Logical OR of two operators.
#[derive(Debug, Clone, PartialEq)]
pub struct Or {
    /// Left operand.
    pub left: Box<Operator>,
    /// Right operand.
    pub right: Box<Operator>,
}

impl OperatorExpr for Or {
    fn to_surql(&self) -> String {
        format!("({}) OR ({})", self.left.to_surql(), self.right.to_surql())
    }
}

/// Logical NOT.
#[derive(Debug, Clone, PartialEq)]
pub struct Not {
    /// Inner operator.
    pub operand: Box<Operator>,
}

impl OperatorExpr for Not {
    fn to_surql(&self) -> String {
        format!("NOT ({})", self.operand.to_surql())
    }
}

// ---------------------------------------------------------------------------
// Functional helpers (match the Python API: `eq`, `ne`, `and_`, ...).
// ---------------------------------------------------------------------------

/// Build an [`struct@Eq`] operator.
pub fn eq(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::Eq(Eq::new(field, value))
}

/// Build a [`Ne`] operator.
pub fn ne(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::Ne(Ne::new(field, value))
}

/// Build a [`Gt`] operator.
pub fn gt(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::Gt(Gt::new(field, value))
}

/// Build a [`Gte`] operator.
pub fn gte(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::Gte(Gte::new(field, value))
}

/// Build a [`Lt`] operator.
pub fn lt(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::Lt(Lt::new(field, value))
}

/// Build an [`Lte`] operator.
pub fn lte(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::Lte(Lte::new(field, value))
}

/// Build a [`Contains`] operator.
pub fn contains(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::Contains(Contains::new(field, value))
}

/// Build a [`ContainsNot`] operator.
pub fn contains_not(field: impl Into<String>, value: impl Into<Value>) -> Operator {
    Operator::ContainsNot(ContainsNot::new(field, value))
}

/// Build a [`ContainsAll`] operator.
pub fn contains_all(field: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Operator {
    Operator::ContainsAll(ContainsAll::new(field, values))
}

/// Build a [`ContainsAny`] operator.
pub fn contains_any(field: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Operator {
    Operator::ContainsAny(ContainsAny::new(field, values))
}

/// Build an [`Inside`] operator.
pub fn inside(field: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Operator {
    Operator::Inside(Inside::new(field, values))
}

/// Build a [`NotInside`] operator.
pub fn not_inside(field: impl Into<String>, values: impl IntoIterator<Item = Value>) -> Operator {
    Operator::NotInside(NotInside::new(field, values))
}

/// Build an [`IsNull`] operator.
pub fn is_null(field: impl Into<String>) -> Operator {
    Operator::IsNull(IsNull::new(field))
}

/// Build an [`IsNotNull`] operator.
pub fn is_not_null(field: impl Into<String>) -> Operator {
    Operator::IsNotNull(IsNotNull::new(field))
}

/// Combine two operators with logical AND.
pub fn and_(left: Operator, right: Operator) -> Operator {
    Operator::And(And {
        left: Box::new(left),
        right: Box::new(right),
    })
}

/// Combine two operators with logical OR.
pub fn or_(left: Operator, right: Operator) -> Operator {
    Operator::Or(Or {
        left: Box::new(left),
        right: Box::new(right),
    })
}

/// Negate an operator.
pub fn not_(operand: Operator) -> Operator {
    Operator::Not(Not {
        operand: Box::new(operand),
    })
}

// ---------------------------------------------------------------------------
// Value quoting (mirrors Python's `_quote_value`).
// ---------------------------------------------------------------------------

/// Public wrapper around the internal `quote_value` helper for other
/// crate modules that need the same SurrealQL literal rendering.
pub fn quote_value_public(value: &Value) -> String {
    quote_value(value)
}

/// Quote a [`Value`] for inclusion in a SurrealQL expression.
///
/// - `null` becomes `NULL`.
/// - bool becomes `true`/`false`.
/// - numbers stringify directly.
/// - strings are single-quoted and escape `\` and `'`.
/// - [`SurrealFn`] and [`RecordRef`] encoded as JSON objects (via
///   `serde_json::to_value`) render their raw `to_surql()` expression.
pub(crate) fn quote_value(value: &Value) -> String {
    match value {
        Value::Null => "NULL".to_string(),
        Value::Bool(b) => if *b { "true" } else { "false" }.to_string(),
        Value::Number(n) => n.to_string(),
        Value::String(s) => {
            let escaped = s.replace('\\', "\\\\").replace('\'', "\\'");
            format!("'{escaped}'")
        }
        Value::Array(arr) => {
            let inner = arr.iter().map(quote_value).collect::<Vec<_>>().join(", ");
            format!("[{inner}]")
        }
        Value::Object(obj) => {
            // Detect `SurrealFn` / `RecordRef` shapes.
            if let Some(raw) = try_wrapped_raw(obj) {
                return raw;
            }
            let inner = obj
                .iter()
                .map(|(k, v)| format!("{}: {}", quote_key(k), quote_value(v)))
                .collect::<Vec<_>>()
                .join(", ");
            format!("{{ {inner} }}")
        }
    }
}

fn quote_key(key: &str) -> String {
    if key.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
        key.to_owned()
    } else {
        let escaped = key.replace('\\', "\\\\").replace('\'', "\\'");
        format!("'{escaped}'")
    }
}

// ---------------------------------------------------------------------------
// type::record / type::thing first-class helpers
// ---------------------------------------------------------------------------

/// Build a `type::record('<table>', <id>)` expression.
///
/// Mirrors the ergonomics of the Python `type_record()` helper: callers pass
/// a table name and any [`RecordIdValue`]-convertible id, and receive an
/// [`Expression`] (tagged [`crate::query::expressions::ExpressionKind::Function`])
/// that can be embedded anywhere a target, value, or SurrealQL fragment is
/// accepted. The returned expression renders identically to
/// [`RecordRef::to_surql`].
///
/// ## Examples
///
/// ```
/// use surql::types::operators::type_record;
///
/// let target = type_record("task", "abc-123");
/// assert_eq!(target.to_surql(), "type::record('task', 'abc-123')");
///
/// let numeric = type_record("post", 42_i64);
/// assert_eq!(numeric.to_surql(), "type::record('post', 42)");
/// ```
pub fn type_record(table: impl Into<String>, record_id: impl Into<RecordIdValue>) -> Expression {
    Expression::function(record_ref(table, record_id).to_surql())
}

/// Build a `type::thing('<table>', <id>)` expression.
///
/// `type::thing` is the SurrealDB alias for `type::record`. This helper is
/// provided for parity with the SurrealQL function set; the rendered SurrealQL
/// uses `type::thing(...)` verbatim so query plans that expect the literal
/// `thing` function call continue to match.
///
/// ## Examples
///
/// ```
/// use surql::types::operators::type_thing;
///
/// let target = type_thing("user", "alice");
/// assert_eq!(target.to_surql(), "type::thing('user', 'alice')");
///
/// let numeric = type_thing("post", 123_i64);
/// assert_eq!(numeric.to_surql(), "type::thing('post', 123)");
/// ```
pub fn type_thing(table: impl Into<String>, record_id: impl Into<RecordIdValue>) -> Expression {
    let rendered = match record_id.into() {
        RecordIdValue::Int(n) => format!("type::thing('{}', {n})", table.into()),
        RecordIdValue::String(s) => {
            let escaped = s.replace('\\', "\\\\").replace('\'', "\\'");
            format!("type::thing('{}', '{escaped}')", table.into())
        }
    };
    Expression::function(rendered)
}

fn try_wrapped_raw(obj: &serde_json::Map<String, Value>) -> Option<String> {
    if let Ok(fnv) = serde_json::from_value::<SurrealFn>(Value::Object(obj.clone())) {
        return Some(fnv.to_surql());
    }
    if let Ok(rr) = serde_json::from_value::<RecordRef>(Value::Object(obj.clone())) {
        return Some(rr.to_surql());
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn eq_renders() {
        assert_eq!(eq("name", "Alice").to_surql(), "name = 'Alice'");
    }

    #[test]
    fn ne_renders() {
        assert_eq!(ne("status", "deleted").to_surql(), "status != 'deleted'");
    }

    #[test]
    fn gt_renders_integer() {
        assert_eq!(gt("age", 18).to_surql(), "age > 18");
    }

    #[test]
    fn lt_renders_float() {
        assert_eq!(lt("price", 50.0).to_surql(), "price < 50.0");
    }

    #[test]
    fn gte_and_lte() {
        assert_eq!(gte("score", 100).to_surql(), "score >= 100");
        assert_eq!(lte("quantity", 10).to_surql(), "quantity <= 10");
    }

    #[test]
    fn contains_renders() {
        assert_eq!(
            contains("email", "@example.com").to_surql(),
            "email CONTAINS '@example.com'"
        );
    }

    #[test]
    fn contains_not_renders() {
        assert_eq!(
            contains_not("tags", "spam").to_surql(),
            "tags CONTAINSNOT 'spam'"
        );
    }

    #[test]
    fn contains_all_renders() {
        let op = contains_all("tags", [json!("python"), json!("database")]);
        assert_eq!(op.to_surql(), "tags CONTAINSALL ['python', 'database']");
    }

    #[test]
    fn contains_any_renders() {
        let op = contains_any("tags", [json!("python"), json!("javascript")]);
        assert_eq!(op.to_surql(), "tags CONTAINSANY ['python', 'javascript']");
    }

    #[test]
    fn inside_renders() {
        let op = inside("status", [json!("active"), json!("pending")]);
        assert_eq!(op.to_surql(), "status INSIDE ['active', 'pending']");
    }

    #[test]
    fn not_inside_renders() {
        let op = not_inside("status", [json!("deleted"), json!("archived")]);
        assert_eq!(op.to_surql(), "status NOTINSIDE ['deleted', 'archived']");
    }

    #[test]
    fn is_null_and_not_null() {
        assert_eq!(is_null("deleted_at").to_surql(), "deleted_at IS NULL");
        assert_eq!(
            is_not_null("created_at").to_surql(),
            "created_at IS NOT NULL"
        );
    }

    #[test]
    fn and_renders() {
        let op = and_(gt("age", 18), eq("status", "active"));
        assert_eq!(op.to_surql(), "(age > 18) AND (status = 'active')");
    }

    #[test]
    fn or_renders() {
        let op = or_(eq("type", "admin"), eq("type", "moderator"));
        assert_eq!(op.to_surql(), "(type = 'admin') OR (type = 'moderator')");
    }

    #[test]
    fn not_renders() {
        let op = not_(eq("status", "deleted"));
        assert_eq!(op.to_surql(), "NOT (status = 'deleted')");
    }

    #[test]
    fn null_quoted_as_keyword() {
        assert_eq!(
            eq("deleted_at", Value::Null).to_surql(),
            "deleted_at = NULL"
        );
    }

    #[test]
    fn bool_quoted_lowercase() {
        assert_eq!(eq("active", true).to_surql(), "active = true");
        assert_eq!(eq("active", false).to_surql(), "active = false");
    }

    #[test]
    fn string_escapes_single_quote() {
        assert_eq!(eq("name", "O'Brien").to_surql(), "name = 'O\\'Brien'");
    }

    #[test]
    fn string_escapes_backslash() {
        assert_eq!(eq("path", "a\\b").to_surql(), "path = 'a\\\\b'");
    }

    #[test]
    fn surrealfn_value_renders_raw() {
        let fnv =
            serde_json::to_value(super::super::surreal_fn::surql_fn("time::now", &[])).unwrap();
        assert_eq!(eq("created_at", fnv).to_surql(), "created_at = time::now()");
    }

    #[test]
    fn record_ref_value_renders_raw() {
        let rr =
            serde_json::to_value(super::super::record_ref::record_ref("user", "alice")).unwrap();
        assert_eq!(
            eq("author", rr).to_surql(),
            "author = type::record('user', 'alice')"
        );
    }

    #[test]
    fn type_record_string_id_renders() {
        assert_eq!(
            type_record("task", "abc-123").to_surql(),
            "type::record('task', 'abc-123')"
        );
    }

    #[test]
    fn type_record_int_id_renders() {
        assert_eq!(
            type_record("post", 42_i64).to_surql(),
            "type::record('post', 42)"
        );
    }

    #[test]
    fn type_record_escapes_single_quote() {
        assert_eq!(
            type_record("user", "o'brien").to_surql(),
            "type::record('user', 'o\\'brien')"
        );
    }

    #[test]
    fn type_record_is_function_expression() {
        let expr = type_record("task", "abc");
        assert_eq!(
            expr.kind,
            crate::query::expressions::ExpressionKind::Function
        );
    }

    #[test]
    fn type_thing_string_id_renders() {
        assert_eq!(
            type_thing("user", "alice").to_surql(),
            "type::thing('user', 'alice')"
        );
    }

    #[test]
    fn type_thing_int_id_renders() {
        assert_eq!(
            type_thing("post", 123_i64).to_surql(),
            "type::thing('post', 123)"
        );
    }

    #[test]
    fn type_thing_escapes_backslash() {
        assert_eq!(
            type_thing("path", "a\\b").to_surql(),
            "type::thing('path', 'a\\\\b')"
        );
    }

    #[test]
    fn type_thing_is_function_expression() {
        let expr = type_thing("user", "alice");
        assert_eq!(
            expr.kind,
            crate::query::expressions::ExpressionKind::Function
        );
    }
}