tork-orm-core 0.1.0

Core runtime for the Tork ORM: dialect-agnostic query model, typed columns, and database drivers.
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
721
722
723
724
725
726
//! Typed column handles.
//!
//! `#[derive(Model)]` generates one [`Column`] associated constant per field, so a
//! column is used as a value: `User::is_active`. Its comparison methods build
//! [`Expr`] nodes whose right-hand side is a bound parameter, and the column's Rust
//! type constrains what it can be compared against, so a type mismatch is a compile
//! error rather than a run-time surprise.

use std::marker::PhantomData;

use crate::query::ast::OrderTerm;
use crate::query::expr::{AggFunc, BinaryOp, Expr};
use crate::query::queryset::QuerySet;
use crate::query::write::Assignment;
use crate::value::{BindValue, Value};

/// Accepted by [`Column::set`] — either a type-safe literal (`col.set(42_i64)`)
/// or any expression (`col.set(col.add(1_i64))`).
pub trait IntoAssignExpr<T> {
    /// Converts `self` into an assignment expression.
    fn into_assign_expr(self) -> Expr;
}

macro_rules! impl_assign_literal {
    ($t:ty) => {
        impl IntoAssignExpr<$t> for $t {
            fn into_assign_expr(self) -> Expr {
                Expr::value(self.to_value())
            }
        }
        impl IntoAssignExpr<Option<$t>> for $t {
            fn into_assign_expr(self) -> Expr {
                Expr::value(self.to_value())
            }
        }
    };
}

impl_assign_literal!(i64);
impl_assign_literal!(i32);
impl_assign_literal!(f64);
impl_assign_literal!(bool);
impl_assign_literal!(String);

// &str → String column
impl IntoAssignExpr<String> for &str {
    fn into_assign_expr(self) -> Expr {
        Expr::value(Value::Text(self.to_string()))
    }
}

// Value directly — useful for NULL assignments on nullable columns
impl<T> IntoAssignExpr<T> for Value {
    fn into_assign_expr(self) -> Expr {
        Expr::value(self)
    }
}

// Expr — the escape hatch; accepts any column type
impl<T> IntoAssignExpr<T> for Expr {
    fn into_assign_expr(self) -> Expr {
        self
    }
}

/// Marker for column types that support numeric aggregates (`sum`, `avg`, `min`,
/// `max`).
pub trait Numeric {}

impl Numeric for i64 {}
impl Numeric for i32 {}
impl Numeric for f64 {}

/// Converts a comparison right-hand side into a bound [`Value`], constrained by the
/// column's type `T`.
///
/// There are exactly two ways to satisfy it: any `T` that is itself a
/// [`BindValue`] (the identity case), and a `&str` when the column type is
/// `String`. Keeping the set small avoids the ambiguity a blanket `Into<T>` bound
/// would create for integer literals, while still letting `column.eq("text")` take
/// a string slice.
pub trait IntoSqlValue<T> {
    /// Lowers `self` into a bound value.
    fn into_sql_value(self) -> Value;
}

impl<T: BindValue> IntoSqlValue<T> for T {
    fn into_sql_value(self) -> Value {
        self.to_value()
    }
}

impl IntoSqlValue<String> for &str {
    fn into_sql_value(self) -> Value {
        Value::Text(self.to_string())
    }
}

/// A typed reference to a model column.
///
/// `M` is the owning model and `T` is the column's Rust type. The handle is
/// zero-sized beyond two `&'static str`s and is `Copy`, so passing it by value is
/// free.
///
/// # Examples
///
/// ```
/// use tork_orm_core::Column;
///
/// struct User;
/// const IS_ACTIVE: Column<User, bool> = Column::new("users", "is_active");
/// let predicate = IS_ACTIVE.eq(true);
/// # let _ = predicate;
/// ```
pub struct Column<M, T> {
    table: &'static str,
    name: &'static str,
    // `fn() -> (M, T)` keeps `Column` `Send`, `Sync`, and `Copy` regardless of the
    // model and column types, which are only used to constrain comparisons.
    _marker: PhantomData<fn() -> (M, T)>,
}

impl<M, T> Column<M, T> {
    /// Creates a column handle for `table`.`name`.
    pub const fn new(table: &'static str, name: &'static str) -> Self {
        Self {
            table,
            name,
            _marker: PhantomData,
        }
    }

    /// Returns the owning table name.
    pub fn table(&self) -> &'static str {
        self.table
    }

    /// Returns the column name.
    pub fn name(&self) -> &'static str {
        self.name
    }

    /// Returns this column as an expression node.
    pub fn expr(&self) -> Expr {
        Expr::column(self.table, self.name)
    }

    /// `column = value`
    pub fn eq<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.compare(BinaryOp::Eq, value)
    }

    /// `column <> value`
    pub fn ne<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.compare(BinaryOp::Ne, value)
    }

    /// `column > value`
    pub fn gt<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.compare(BinaryOp::Gt, value)
    }

    /// `column >= value`
    pub fn ge<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.compare(BinaryOp::Ge, value)
    }

    /// `column < value`
    pub fn lt<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.compare(BinaryOp::Lt, value)
    }

    /// `column <= value`
    pub fn le<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.compare(BinaryOp::Le, value)
    }

    /// `column IN (values...)`
    pub fn in_list<V, I>(self, values: I) -> Expr
    where
        V: IntoSqlValue<T>,
        I: IntoIterator<Item = V>,
    {
        let values = values.into_iter().map(IntoSqlValue::into_sql_value).collect();
        Expr::in_list(self.expr(), values)
    }

    /// `column NOT IN (values...)`
    ///
    /// An empty iterator matches all rows (SQL: `NOT (0 = 1)` = always true).
    pub fn not_in<V, I>(self, values: I) -> Expr
    where
        V: IntoSqlValue<T>,
        I: IntoIterator<Item = V>,
    {
        Expr::not(self.in_list(values))
    }

    /// `column IN (SELECT ...)` — matches rows where the column value appears in
    /// the subquery result. The subquery must return a single column.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// Post::query()
    ///     .filter(Post::user_id.in_subquery(
    ///         User::query().filter(User::is_active.eq(true)).select((User::id,)),
    ///     ))
    ///     .all(&db)
    ///     .await?
    /// ```
    pub fn in_subquery<X: crate::model::Model>(self, qs: QuerySet<X>) -> Expr {
        Expr::in_subquery(self.expr(), qs.into_statement(), false)
    }

    /// `column NOT IN (SELECT ...)` — excludes rows where the column value
    /// appears in the subquery result.
    pub fn not_in_subquery<X: crate::model::Model>(self, qs: QuerySet<X>) -> Expr {
        Expr::in_subquery(self.expr(), qs.into_statement(), true)
    }

    /// `column BETWEEN low AND high` (inclusive on both ends).
    pub fn between<V: IntoSqlValue<T>>(self, low: V, high: V) -> Expr {
        Expr::between(
            self.expr(),
            Expr::value(low.into_sql_value()),
            Expr::value(high.into_sql_value()),
        )
    }

    /// `column IS NULL`
    pub fn is_null(self) -> Expr {
        Expr::is_null(self.expr(), false)
    }

    /// `column IS NOT NULL`
    pub fn is_not_null(self) -> Expr {
        Expr::is_null(self.expr(), true)
    }

    /// Orders by this column ascending.
    pub fn asc(self) -> OrderTerm {
        OrderTerm::new(self.expr(), false)
    }

    /// Orders by this column descending.
    pub fn desc(self) -> OrderTerm {
        OrderTerm::new(self.expr(), true)
    }

    /// Builds a `SET column = …` assignment for use with
    /// [`QuerySet::update`](crate::QuerySet::update).
    ///
    /// Accepts either a type-safe literal or any [`Expr`]:
    /// ```ignore
    /// Post::title.set("New Title")                           // literal
    /// Post::view_count.set(Post::view_count.add(1_i64))      // expression
    /// ```
    pub fn set<A: IntoAssignExpr<T>>(self, value: A) -> Assignment {
        Assignment::new(self.name, value.into_assign_expr())
    }

    /// Aliases this column in a projection, `column AS "alias"`.
    pub fn as_(self, alias: &'static str) -> Expr {
        self.expr().as_(alias)
    }

    /// `COUNT(column)` for use in a projection or `HAVING`.
    pub fn count(self) -> Expr {
        Expr::aggregate(AggFunc::Count, [self.expr()])
    }

    /// `lower(column)`.
    pub fn lower(self) -> Expr {
        Expr::func("lower", [self.expr()])
    }

    /// `upper(column)`.
    pub fn upper(self) -> Expr {
        Expr::func("upper", [self.expr()])
    }

    /// `length(column)`.
    pub fn length(self) -> Expr {
        Expr::func("length", [self.expr()])
    }

    /// `trim(column)`.
    pub fn trim(self) -> Expr {
        Expr::func("trim", [self.expr()])
    }

    /// `abs(column)`.
    pub fn abs(self) -> Expr {
        Expr::func("abs", [self.expr()])
    }

    /// `column IS DISTINCT FROM value` — NULL-safe inequality.
    ///
    /// Two NULLs are not distinct (equal), a NULL and a non-NULL are distinct.
    pub fn is_distinct_from<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        Expr::binary(
            self.expr(),
            BinaryOp::IsDistinctFrom,
            Expr::value(value.into_sql_value()),
        )
    }

    /// `column IS NOT DISTINCT FROM value` — NULL-safe equality.
    ///
    /// Two NULLs are not distinct (i.e. equal), a NULL and a non-NULL are distinct.
    pub fn is_not_distinct_from<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        Expr::binary(
            self.expr(),
            BinaryOp::IsNotDistinctFrom,
            Expr::value(value.into_sql_value()),
        )
    }

    /// Builds a binary comparison against a bound value.
    fn compare<V: IntoSqlValue<T>>(self, op: BinaryOp, value: V) -> Expr {
        Expr::binary(self.expr(), op, Expr::value(value.into_sql_value()))
    }
}

/// PostgreSQL-specific aggregate helpers on any column.
///
/// These are only available when the `postgres` feature is enabled. The
/// underlying SQL functions (`string_agg`, `array_agg`, `json_agg`, `jsonb_agg`)
/// are PostgreSQL-specific.
#[cfg(feature = "postgres")]
impl<M, T: crate::value::BindValue> Column<M, T> {
    /// `string_agg(column, delimiter)` — concatenates non-null values with a delimiter.
    pub fn string_aggregation(self, delimiter: &str) -> Expr {
        Expr::aggregate(AggFunc::StringAggregation, [
            self.expr(),
            Expr::value(Value::Text(delimiter.to_string())),
        ])
    }

    /// `array_agg(column)` — collects values into a PostgreSQL array.
    pub fn array_aggregation(self) -> Expr {
        Expr::aggregate(AggFunc::ArrayAggregation, [self.expr()])
    }

    /// `json_agg(column)` — aggregates values as a JSON array.
    pub fn json_aggregation(self) -> Expr {
        Expr::aggregate(AggFunc::JsonAggregation, [self.expr()])
    }

    /// `jsonb_agg(column)` — aggregates values as a JSONB array.
    pub fn jsonb_aggregation(self) -> Expr {
        Expr::aggregate(AggFunc::JsonbAggregation, [self.expr()])
    }
}

impl<M, T> From<Column<M, T>> for Expr {
    fn from(column: Column<M, T>) -> Self {
        column.expr()
    }
}

impl<M, T: Numeric> Column<M, T> {
    /// `SUM(column)`.
    pub fn sum(self) -> Expr {
        Expr::aggregate(AggFunc::Sum, [self.expr()])
    }

    /// `AVG(column)`.
    pub fn avg(self) -> Expr {
        Expr::aggregate(AggFunc::Avg, [self.expr()])
    }

    /// `MIN(column)`.
    pub fn min(self) -> Expr {
        Expr::aggregate(AggFunc::Min, [self.expr()])
    }

    /// `MAX(column)`.
    pub fn max(self) -> Expr {
        Expr::aggregate(AggFunc::Max, [self.expr()])
    }

    /// `column + value`
    pub fn add<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.expr().add(Expr::value(value.into_sql_value()))
    }

    /// `column - value`
    pub fn sub<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.expr().sub(Expr::value(value.into_sql_value()))
    }

    /// `column * value`
    pub fn mul<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.expr().mul(Expr::value(value.into_sql_value()))
    }

    /// `column / value`
    pub fn div<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.expr().div(Expr::value(value.into_sql_value()))
    }

    /// `column % value`
    pub fn rem<V: IntoSqlValue<T>>(self, value: V) -> Expr {
        self.expr().rem(Expr::value(value.into_sql_value()))
    }

    /// `round(column)`.
    pub fn round(self) -> Expr {
        Expr::func("round", [self.expr()])
    }

    /// `ceil(column)`.
    pub fn ceil(self) -> Expr {
        Expr::func("ceil", [self.expr()])
    }

    /// `floor(column)`.
    pub fn floor(self) -> Expr {
        Expr::func("floor", [self.expr()])
    }
}

impl<M> Column<M, String> {
    /// `column LIKE pattern`
    ///
    /// Wildcards (`%` for any sequence, `_` for a single character) are the
    /// caller's responsibility and are passed through unchanged.
    pub fn like(self, pattern: &str) -> Expr {
        Expr::binary(
            self.expr(),
            crate::query::expr::BinaryOp::Like,
            Expr::value(Value::Text(pattern.to_string())),
        )
    }

    /// `column ILIKE pattern` — case-insensitive LIKE.
    ///
    /// On SQLite this is rendered as `lower(col) LIKE lower(pattern)`, which
    /// covers non-ASCII Unicode correctly. On backends that have a native
    /// `ILIKE` keyword it is rendered verbatim.
    pub fn ilike(self, pattern: &str) -> Expr {
        Expr::binary(
            self.expr(),
            crate::query::expr::BinaryOp::ILike,
            Expr::value(Value::Text(pattern.to_string())),
        )
    }

    /// `column LIKE 'prefix%'` — matches rows where the column starts with `prefix`.
    pub fn starts_with(self, prefix: &str) -> Expr {
        self.like(&format!("{prefix}%"))
    }

    /// `column LIKE '%suffix'` — matches rows where the column ends with `suffix`.
    pub fn ends_with(self, suffix: &str) -> Expr {
        self.like(&format!("%{suffix}"))
    }

    /// `column LIKE '%needle%'` — matches rows where the column contains `needle`.
    pub fn contains(self, needle: &str) -> Expr {
        self.like(&format!("%{needle}%"))
    }

    /// Case-insensitive `starts_with`.
    pub fn istarts_with(self, prefix: &str) -> Expr {
        self.ilike(&format!("{prefix}%"))
    }

    /// Case-insensitive `ends_with`.
    pub fn iends_with(self, suffix: &str) -> Expr {
        self.ilike(&format!("%{suffix}"))
    }

    /// Case-insensitive `contains`.
    pub fn icontains(self, needle: &str) -> Expr {
        self.ilike(&format!("%{needle}%"))
    }

    /// `substr(column, start)` — 1-based, returns from `start` to end.
    pub fn substr(self, start: i64) -> Expr {
        Expr::func("substr", [self.expr(), Expr::value(Value::Int(start))])
    }

    /// `substr(column, start, len)` — 1-based, returns `len` characters.
    pub fn substr_len(self, start: i64, len: i64) -> Expr {
        Expr::func(
            "substr",
            [
                self.expr(),
                Expr::value(Value::Int(start)),
                Expr::value(Value::Int(len)),
            ],
        )
    }

    /// `position(substring IN column)` — returns the position of the first occurrence.
    pub fn position(self, substring: &str) -> Expr {
        Expr::func("position", [
            Expr::value(Value::Text(substring.to_string())),
            self.expr(),
        ])
    }

    /// `replace(column, from, to)` — replaces all occurrences of `from` with `to`.
    ///
    /// SQL-standard function, available in SQLite and PostgreSQL alike.
    pub fn replace(self, from: &str, to: &str) -> Expr {
        Expr::func("replace", [
            self.expr(),
            Expr::value(Value::Text(from.to_string())),
            Expr::value(Value::Text(to.to_string())),
        ])
    }
}

/// PostgreSQL-specific string functions.
///
/// These functions (`regexp_like`, `regexp_replace`, `split_part`, `left`,
/// `right`, `repeat`, `reverse`) are PostgreSQL-specific. Only available when
/// the `postgres` feature is enabled.
#[cfg(feature = "postgres")]
impl<M> Column<M, String> {
    /// `regexp_like(column, pattern)` — tests whether the column matches the regex pattern.
    pub fn regex_match(self, pattern: &str) -> Expr {
        Expr::func("regexp_like", [self.expr(), Expr::value(Value::Text(pattern.to_string()))])
    }

    /// `regexp_replace(column, pattern, replacement)` — replaces regex matches.
    pub fn regex_replace(self, pattern: &str, replacement: &str) -> Expr {
        Expr::func("regexp_replace", [
            self.expr(),
            Expr::value(Value::Text(pattern.to_string())),
            Expr::value(Value::Text(replacement.to_string())),
        ])
    }

    /// `split_part(column, delimiter, field)` — splits on `delimiter` and returns the
    /// `field`-th part (1-based).
    pub fn split_part(self, delimiter: &str, field: i64) -> Expr {
        Expr::func("split_part", [
            self.expr(),
            Expr::value(Value::Text(delimiter.to_string())),
            Expr::value(Value::Int(field)),
        ])
    }

    /// `left(column, n)` — returns the first `n` characters.
    pub fn left(self, n: i64) -> Expr {
        Expr::func("left", [self.expr(), Expr::value(Value::Int(n))])
    }

    /// `right(column, n)` — returns the last `n` characters.
    pub fn right(self, n: i64) -> Expr {
        Expr::func("right", [self.expr(), Expr::value(Value::Int(n))])
    }

    /// `repeat(column, n)` — repeats the string `n` times.
    pub fn repeat(self, n: i64) -> Expr {
        Expr::func("repeat", [self.expr(), Expr::value(Value::Int(n))])
    }

    /// `reverse(column)` — reverses the string.
    pub fn reverse(self) -> Expr {
        Expr::func("reverse", [self.expr()])
    }
}

/// PostgreSQL-specific boolean aggregates.
///
/// `bool_and` and `bool_or` are PostgreSQL-specific. Only available when the
/// `postgres` feature is enabled.
#[cfg(feature = "postgres")]
impl<M> Column<M, bool> {
    /// `bool_and(column)` — true if every non-null value is true.
    pub fn bool_and(self) -> Expr {
        Expr::aggregate(AggFunc::BoolAnd, [self.expr()])
    }

    /// `bool_or(column)` — true if any non-null value is true.
    pub fn bool_or(self) -> Expr {
        Expr::aggregate(AggFunc::BoolOr, [self.expr()])
    }
}

/// JSON column operators (PostgreSQL `jsonb`).
impl<M> Column<M, serde_json::Value> {
    /// `column -> key` — extracts a JSON field by name, as JSON.
    pub fn json_get(self, key: &str) -> Expr {
        Expr::binary(
            self.expr(),
            BinaryOp::JsonGet,
            Expr::value(Value::Text(key.to_string())),
        )
    }

    /// `column ->> key` — extracts a JSON field by name, as text.
    ///
    /// Returns a text-valued expression; chain a comparison such as `.eq("active")`
    /// to use it as a filter.
    pub fn json_get_text(self, key: &str) -> Expr {
        Expr::binary(
            self.expr(),
            BinaryOp::JsonGetText,
            Expr::value(Value::Text(key.to_string())),
        )
    }

    /// `column @> value` — tests whether the JSON document contains `value`.
    pub fn json_contains(self, value: serde_json::Value) -> Expr {
        Expr::binary(self.expr(), BinaryOp::Contains, Expr::value(Value::Json(value)))
    }

    /// `column ? key` — does the JSON key exist as a top-level key?
    pub fn json_has_key(self, key: &str) -> Expr {
        Expr::binary(
            self.expr(),
            BinaryOp::JsonKeyExists,
            Expr::value(Value::Text(key.to_string())),
        )
    }

    /// `column ?| keys` — do any of the given keys exist as top-level keys?
    pub fn json_has_any(self, keys: &[&str]) -> Expr {
        Expr::binary(
            self.expr(),
            BinaryOp::JsonKeyExistsAny,
            Expr::value(Value::Array(keys.iter().map(|k| Value::Text(k.to_string())).collect())),
        )
    }

    /// `column ?& keys` — do all of the given keys exist as top-level keys?
    pub fn json_has_all(self, keys: &[&str]) -> Expr {
        Expr::binary(
            self.expr(),
            BinaryOp::JsonKeyExistsAll,
            Expr::value(Value::Array(keys.iter().map(|k| Value::Text(k.to_string())).collect())),
        )
    }

    /// `column #> path` — extracts a JSON sub-object at the given path, as JSON.
    ///
    /// The path is a `&[&str]` of key names, rendered as a PostgreSQL text array
    /// literal (e.g. `'{a,b}'`).
    pub fn json_path(self, path: &[&str]) -> Expr {
        let path_array: Vec<Value> = path.iter().map(|k| Value::Text(k.to_string())).collect();
        Expr::binary(
            self.expr(),
            BinaryOp::JsonPath,
            Expr::value(Value::Array(path_array)),
        )
    }

    /// `column #>> path` — extracts a JSON sub-object at the given path, as text.
    pub fn json_path_text(self, path: &[&str]) -> Expr {
        let path_array: Vec<Value> = path.iter().map(|k| Value::Text(k.to_string())).collect();
        Expr::binary(
            self.expr(),
            BinaryOp::JsonPathText,
            Expr::value(Value::Array(path_array)),
        )
    }
}

/// Array column operators (PostgreSQL `element[]`).
///
/// Bounding on `T: BindValue` excludes `Vec<u8>` blob columns (`u8` is not
/// `BindValue`), which therefore get none of these methods.
impl<M, T: BindValue> Column<M, Vec<T>> {
    /// `value = ANY(column)` — tests whether the array contains `value`.
    pub fn any(self, value: T) -> Expr {
        Expr::binary(
            Expr::value(value.to_value()),
            BinaryOp::Eq,
            Expr::func("ANY", [self.expr()]),
        )
    }

    /// `column @> items` — tests whether the array contains every one of `items`.
    pub fn array_contains(self, items: impl IntoIterator<Item = T>) -> Expr {
        let array = Value::Array(items.into_iter().map(|item| item.to_value()).collect());
        Expr::binary(self.expr(), BinaryOp::Contains, Expr::value(array))
    }

    /// `column && items` — tests whether the array overlaps `items` (shares any element).
    pub fn overlaps(self, items: impl IntoIterator<Item = T>) -> Expr {
        let array = Value::Array(items.into_iter().map(|item| item.to_value()).collect());
        Expr::binary(self.expr(), BinaryOp::Overlap, Expr::value(array))
    }

    /// `column <@ items` — tests whether the array is contained by `items`
    /// (every element of the column is in items).
    pub fn contained_by(self, items: impl IntoIterator<Item = T>) -> Expr {
        let array = Value::Array(items.into_iter().map(|item| item.to_value()).collect());
        Expr::binary(self.expr(), BinaryOp::ArrayContainedBy, Expr::value(array))
    }

    /// `array_append(column, value)` — appends an element to the array.
    pub fn array_append(self, value: T) -> Expr {
        Expr::func("array_append", [self.expr(), Expr::value(value.to_value())])
    }

    /// `array_remove(column, value)` — removes all occurrences of `value` from the array.
    pub fn array_remove(self, value: T) -> Expr {
        Expr::func("array_remove", [self.expr(), Expr::value(value.to_value())])
    }

    /// `array_position(column, value)` — returns the index of the first occurrence of `value` (1-based).
    pub fn array_position(self, value: T) -> Expr {
        Expr::func("array_position", [self.expr(), Expr::value(value.to_value())])
    }

    /// `array_length(column, dimension)` — returns the length of the array along the given dimension (1-based).
    pub fn array_length(self, dimension: i64) -> Expr {
        Expr::func("array_length", [self.expr(), Expr::value(Value::Int(dimension))])
    }
}

impl<M, T> Clone for Column<M, T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<M, T> Copy for Column<M, T> {}