utopia-query 0.1.1

Queries as data, compiled to SQL (MySQL, MariaDB, PostgreSQL, SQLite, ClickHouse) and MongoDB commands; schema builders; a SQL tokenizer, parser, serializer and statement classifier.
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
//! [`Column`]: a column of a table blueprint, with the modifiers its
//! dialect supports.
//!
//! Modifiers every dialect has are inherent methods. The others are gated
//! by marker traits the dialects implement ([`Unique`], [`Collation`],
//! [`Generated`], ...), so a modifier a dialect cannot express does not
//! exist on its columns, as in PHP where each dialect's column class only
//! uses the traits it supports.

use std::fmt;
use std::marker::PhantomData;

use super::enums::ColumnType;
use super::{ClickHouse, PostgreSql, Schema, names};
use crate::error::{Error, Result};
use crate::value::{IntoValue, Value};

/// What a column is: its name, type, sizes and every modifier set on it.
///
/// PHP: the public properties of `Utopia\Query\Schema\Column` (and
/// `Column\ClickHouse`).
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnDefinition {
    /// The name.
    pub name: String,
    /// The portable type.
    pub kind: ColumnType,
    /// The length (strings).
    pub length: Option<i64>,
    /// The precision (decimals, date-times).
    pub precision: Option<i64>,
    /// The scale (decimals).
    pub scale: Option<i64>,
    /// Whether the column accepts `NULL`.
    pub nullable: bool,
    /// The default value, rendered as a literal (`Some(Value::Null)` is
    /// `DEFAULT NULL`; `None` is no default).
    pub default: Option<Value>,
    /// A raw default expression, emitted verbatim.
    pub default_raw: Option<String>,
    /// Whether the column is unsigned.
    pub unsigned: bool,
    /// Whether the column is unique.
    pub unique: bool,
    /// Whether the column is (part of) the primary key.
    pub primary: bool,
    /// Whether the server increments the column.
    pub auto_increment: bool,
    /// The column this one is placed after (MySQL `ALTER`).
    pub after: Option<String>,
    /// The comment.
    pub comment: Option<String>,
    /// The allowed values of an enum column.
    pub enum_values: Vec<String>,
    /// The spatial reference system of a geometry column.
    pub srid: Option<i64>,
    /// The width of a vector column.
    pub dimensions: Option<i64>,
    /// Whether an `ALTER` modifies the column instead of adding it.
    pub modify: bool,
    /// The collation.
    pub collation: Option<String>,
    /// A column-level `CHECK` expression.
    pub check: Option<String>,
    /// The expression of a generated column.
    pub generated_expression: Option<String>,
    /// Whether a generated column is stored (`Some(true)`) or virtual
    /// (`Some(false)`); `None` when not set.
    pub generated_stored: Option<bool>,
    /// A column-level TTL expression (ClickHouse).
    pub ttl: Option<String>,
    /// A user-defined type name (PostgreSQL).
    pub user_type: Option<String>,
    /// Whether the type is wrapped in `LowCardinality(...)` (ClickHouse).
    pub low_cardinality: bool,
    /// The width of a `FixedString(N)` column (ClickHouse).
    pub fixed_string_length: Option<i64>,
    /// The `CODEC(...)` clauses (ClickHouse).
    pub codecs: Vec<String>,
    /// The element type of an `Array(T)` column (ClickHouse).
    pub array_element_type: Option<ColumnType>,
    /// The element types of a `Tuple(...)` column (ClickHouse).
    pub tuple_element_types: Vec<ColumnType>,
}

/// A column of a table blueprint for the dialect `S`.
///
/// Built by the table's factories ([`Table::string`](super::Table::string),
/// [`Table::integer`](super::Table::integer), ...), which return it to
/// chain modifiers: `table.string("email", 255).unique().nullable()`.
///
/// PHP: `Utopia\Query\Schema\Column` and its dialect classes
/// (`Column\MySQL`, `Column\PostgreSQL`, `Column\SQLite`,
/// `Column\ClickHouse`, `Column\MongoDB`).
pub struct Column<S> {
    pub(crate) def: ColumnDefinition,
    dialect: PhantomData<fn() -> S>,
}

impl<S> Clone for Column<S> {
    fn clone(&self) -> Self {
        Column { def: self.def.clone(), dialect: PhantomData }
    }
}

impl<S> fmt::Debug for Column<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.def.fmt(f)
    }
}

impl<S> PartialEq for Column<S> {
    fn eq(&self, other: &Self) -> bool {
        self.def == other.def
    }
}

/// Dialects whose columns can be server-incremented.
///
/// PHP: `Column\Trait\AutoIncrement`.
pub trait AutoIncrement: Schema {}
/// Dialects with column-level collations.
///
/// PHP: `Column\Trait\Collation`.
pub trait Collation: Schema {}
/// Dialects with inline column comments.
///
/// PHP: `Column\Trait\Comment`.
pub trait Comment: Schema {}
/// Dialects whose vector columns have a declared width.
///
/// PHP: `Column\Trait\Dimensions`.
pub trait Dimensions: Schema {}
/// Dialects with stored generated columns.
///
/// PHP: `Column\Trait\Generated`.
pub trait Generated: Schema {}
/// Dialects that place columns with `AFTER`.
///
/// PHP: `Column\Trait\Positioning`.
pub trait Positioning: Schema {}
/// Dialects whose geometry columns carry an SRID.
///
/// PHP: `Column\Trait\Srid`.
pub trait Srid: Schema {}
/// Dialects with column-level `UNIQUE`.
///
/// PHP: `Column\Trait\Unique`.
pub trait Unique: Schema {}
/// Dialects with virtual generated columns.
///
/// PHP: `Column\Trait\VirtualGenerated`.
pub trait VirtualGenerated: Schema {}

/// A trimmed expression that is not empty and has no `;`.
pub(crate) fn expression(value: &str, empty: &str, semicolon: &str) -> Result<String> {
    let trimmed = php_std::string::trim(value.as_bytes(), php_std::string::TRIM_CHARACTERS);
    let trimmed = String::from_utf8_lossy(trimmed).into_owned();
    if trimmed.is_empty() {
        return Err(Error::Validation(empty.into()));
    }
    if trimmed.contains(';') {
        return Err(Error::Validation(semicolon.into()));
    }
    Ok(trimmed)
}

impl<S> Column<S> {
    /// A column `name` of type `kind`, with no size and no modifier (PHP's
    /// table factories make columns; this one belongs to no table).
    ///
    /// PHP: `new Column($table, $name, $type)`.
    pub fn new(name: impl Into<String>, kind: ColumnType) -> Self {
        Column {
            def: ColumnDefinition {
                name: name.into(),
                kind,
                length: None,
                precision: None,
                scale: None,
                nullable: false,
                default: None,
                default_raw: None,
                unsigned: false,
                unique: false,
                primary: false,
                auto_increment: false,
                after: None,
                comment: None,
                enum_values: Vec::new(),
                srid: None,
                dimensions: None,
                modify: false,
                collation: None,
                check: None,
                generated_expression: None,
                generated_stored: None,
                ttl: None,
                user_type: None,
                low_cardinality: false,
                fixed_string_length: None,
                codecs: Vec::new(),
                array_element_type: None,
                tuple_element_types: Vec::new(),
            },
            dialect: PhantomData,
        }
    }

    /// The column with a length (strings).
    ///
    /// PHP: the `$length` constructor parameter.
    pub fn with_length(mut self, length: Option<i64>) -> Self {
        self.def.length = length;
        self
    }

    /// The column with a precision.
    ///
    /// PHP: the `$precision` constructor parameter.
    pub fn with_precision(mut self, precision: Option<i64>) -> Self {
        self.def.precision = precision;
        self
    }

    /// The column with a scale.
    ///
    /// PHP: the `$scale` constructor parameter.
    pub fn with_scale(mut self, scale: Option<i64>) -> Self {
        self.def.scale = scale;
        self
    }

    /// The column with a spatial reference system.
    ///
    /// PHP: the `$srid` constructor parameter.
    pub fn with_srid(mut self, srid: Option<i64>) -> Self {
        self.def.srid = srid;
        self
    }

    /// The column with a vector width.
    ///
    /// PHP: the `$dimensions` constructor parameter.
    pub fn with_dimensions(mut self, dimensions: Option<i64>) -> Self {
        self.def.dimensions = dimensions;
        self
    }

    /// The column, server-incremented or not.
    ///
    /// PHP: the `$autoIncrement` constructor parameter.
    pub fn with_auto_increment(mut self, auto_increment: bool) -> Self {
        self.def.auto_increment = auto_increment;
        self
    }

    /// What the column is.
    ///
    /// PHP: the column's public properties.
    pub fn definition(&self) -> &ColumnDefinition {
        &self.def
    }

    /// What the column is, to set directly: nothing here is validated, as
    /// nothing is when PHP sets a column's properties.
    pub fn definition_mut(&mut self) -> &mut ColumnDefinition {
        &mut self.def
    }

    /// The name.
    ///
    /// PHP: `$column->name`.
    pub fn name(&self) -> &str {
        &self.def.name
    }

    /// Accepts `NULL`.
    ///
    /// PHP: `nullable()`.
    pub fn nullable(&mut self) -> &mut Self {
        self.def.nullable = true;
        self
    }

    /// Sets the default value, rendered as a literal (`None` for
    /// `DEFAULT NULL`).
    ///
    /// PHP: `default()`.
    pub fn default(&mut self, value: impl IntoValue) -> &mut Self {
        self.def.default = Some(value.into_value());
        self
    }

    /// Sets a raw default expression (`now()`, `gen_random_uuid()`),
    /// trimmed; it must not be empty nor contain `;`.
    ///
    /// PHP: `defaultRaw()`.
    pub fn default_raw(&mut self, expression: &str) -> Result<&mut Self> {
        self.def.default_raw = Some(self::expression(
            expression,
            "Raw default expression must not be empty.",
            "Raw default expression must not contain \";\".",
        )?);
        Ok(self)
    }

    /// Marks the column unsigned.
    ///
    /// PHP: `unsigned()`.
    pub fn unsigned(&mut self) -> &mut Self {
        self.def.unsigned = true;
        self
    }

    /// Marks the column as the primary key (a composite key is
    /// [`Table::primary`](super::Table::primary)).
    ///
    /// PHP: `primary()` without arguments.
    pub fn primary(&mut self) -> &mut Self {
        self.def.primary = true;
        self
    }

    /// Sets the allowed values of an enum column; there must be one at least.
    ///
    /// PHP: `enum()` with one array argument.
    pub fn enum_values<V: Into<String>>(&mut self, values: impl IntoIterator<Item = V>) -> Result<&mut Self> {
        let values: Vec<String> = values.into_iter().map(Into::into).collect();
        if values.is_empty() {
            return Err(Error::Validation("enum() requires at least one allowed value.".into()));
        }
        self.def.enum_values = values;
        Ok(self)
    }

    /// Makes an `ALTER` modify the column instead of adding it.
    ///
    /// PHP: `modify()`.
    pub fn modify(&mut self) -> &mut Self {
        self.def.modify = true;
        self
    }
}

impl<S: AutoIncrement> Column<S> {
    /// Marks the column server-incremented.
    ///
    /// PHP: `autoIncrement()`.
    pub fn auto_increment(&mut self) -> &mut Self {
        self.def.auto_increment = true;
        self
    }
}

impl<S: Collation> Column<S> {
    /// Sets the collation.
    ///
    /// PHP: `collation()`.
    pub fn collation(&mut self, collation: impl Into<String>) -> &mut Self {
        self.def.collation = Some(collation.into());
        self
    }
}

impl<S: Comment> Column<S> {
    /// Sets the comment.
    ///
    /// PHP: `comment()`.
    pub fn comment(&mut self, comment: impl Into<String>) -> &mut Self {
        self.def.comment = Some(comment.into());
        self
    }
}

impl<S: Dimensions> Column<S> {
    /// Sets the width of a vector column.
    ///
    /// PHP: `dimensions()`.
    pub fn dimensions(&mut self, dimensions: i64) -> &mut Self {
        self.def.dimensions = Some(dimensions);
        self
    }
}

impl<S: Generated> Column<S> {
    /// Makes the column generated from `expression`.
    ///
    /// PHP: `generatedAs()`.
    pub fn generated_as(&mut self, expression: impl Into<String>) -> &mut Self {
        self.def.generated_expression = Some(expression.into());
        self
    }

    /// Stores the generated column.
    ///
    /// PHP: `stored()`.
    pub fn stored(&mut self) -> &mut Self {
        self.def.generated_stored = Some(true);
        self
    }
}

impl<S: VirtualGenerated> Column<S> {
    /// Computes the generated column on read.
    ///
    /// PHP: `virtual()`.
    pub fn virtual_(&mut self) -> &mut Self {
        self.def.generated_stored = Some(false);
        self
    }
}

impl<S: Positioning> Column<S> {
    /// Places the column after `column` (in an `ALTER`).
    ///
    /// PHP: `after()`.
    pub fn after(&mut self, column: impl Into<String>) -> &mut Self {
        self.def.after = Some(column.into());
        self
    }
}

impl<S: Srid> Column<S> {
    /// Sets the spatial reference system.
    ///
    /// PHP: `srid()`.
    pub fn srid(&mut self, srid: i64) -> &mut Self {
        self.def.srid = Some(srid);
        self
    }
}

impl<S: Unique> Column<S> {
    /// Marks the column unique.
    ///
    /// PHP: `unique()`.
    pub fn unique(&mut self) -> &mut Self {
        self.def.unique = true;
        self
    }
}

impl<S: super::table::Checks> Column<S> {
    /// Sets a column-level `CHECK` expression (a named table-level check is
    /// [`Table::check`](super::Table::check)).
    ///
    /// PHP: `check()` with one argument.
    pub fn check(&mut self, expression: impl Into<String>) -> &mut Self {
        self.def.check = Some(expression.into());
        self
    }
}

impl Column<PostgreSql> {
    /// Uses the user-defined type `name` (a `CREATE TYPE` enum), which must
    /// be an identifier.
    ///
    /// PHP: `Column\PostgreSQL::userType()`.
    pub fn user_type(&mut self, name: &str) -> Result<&mut Self> {
        if !names::is_identifier(name) {
            return Err(Error::Validation(format!("Invalid user-defined type name: {name}")));
        }
        self.def.user_type = Some(name.to_owned());
        Ok(self)
    }
}

impl Column<ClickHouse> {
    /// Makes the column `FixedString(length)`; the length is at least 1.
    ///
    /// PHP: `Column\ClickHouse::asFixedString()`.
    pub fn as_fixed_string(&mut self, length: i64) -> Result<&mut Self> {
        if length < 1 {
            return Err(Error::Validation("FixedString length must be at least 1.".into()));
        }
        self.def.fixed_string_length = Some(length);
        Ok(self)
    }

    /// Whether the column is `FixedString(N)`.
    ///
    /// PHP: `isFixedString()`.
    pub fn is_fixed_string(&self) -> bool {
        self.def.fixed_string_length.is_some()
    }

    /// Makes the column `Array(element)`.
    ///
    /// PHP: `asArray()`.
    pub fn as_array(&mut self, element: ColumnType) -> &mut Self {
        self.def.array_element_type = Some(element);
        self
    }

    /// Whether the column is `Array(T)`.
    ///
    /// PHP: `isArray()`.
    pub fn is_array(&self) -> bool {
        self.def.array_element_type.is_some()
    }

    /// Makes the column `Tuple(elements...)`; there must be one at least.
    ///
    /// PHP: `asTuple()`.
    pub fn as_tuple(&mut self, elements: Vec<ColumnType>) -> Result<&mut Self> {
        if elements.is_empty() {
            return Err(Error::Validation("Tuple() requires at least one element type.".into()));
        }
        self.def.tuple_element_types = elements;
        Ok(self)
    }

    /// Whether the column is `Tuple(...)`.
    ///
    /// PHP: `isTuple()`.
    pub fn is_tuple(&self) -> bool {
        !self.def.tuple_element_types.is_empty()
    }

    /// Wraps the type in `LowCardinality(...)`.
    ///
    /// PHP: `lowCardinality()`.
    pub fn low_cardinality(&mut self) -> &mut Self {
        self.def.low_cardinality = true;
        self
    }

    /// Adds a `CODEC(...)` clause (`LZ4`, `Delta(4)`), trimmed; it must not
    /// be empty nor contain `;`.
    ///
    /// PHP: `codec()`.
    pub fn codec(&mut self, codec: &str) -> Result<&mut Self> {
        let codec =
            expression(codec, "CODEC expression must not be empty.", "CODEC expression must not contain \";\".")?;
        self.def.codecs.push(codec);
        Ok(self)
    }

    /// Sets a column-level TTL expression, trimmed; it must not be empty nor
    /// contain `;`.
    ///
    /// PHP: `Column\ClickHouse::ttl()`.
    pub fn ttl(&mut self, expression: &str) -> Result<&mut Self> {
        self.def.ttl = Some(self::expression(
            expression,
            "TTL expression must not be empty.",
            "TTL expression must not contain \";\".",
        )?);
        Ok(self)
    }
}