utopia-query 0.1.0

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
//! The compilation steps dialects share: PHP's base `Schema` methods and
//! its `Schema\Trait\*` traits, as functions a dialect's [`Schema`]
//! implementation calls (and wraps, where PHP calls `parent::`).

use super::Schema;
use super::column::Column;
use super::enums::{IndexType, Order, ParameterDirection, TriggerEvent, TriggerTiming};
use super::feature::ViewBody;
use super::index::{Index, IndexOptions};
use super::names;
use super::table::{ForeignKey, Table};
use crate::builder::{AnyBuilder, Statement};
use crate::error::{Error, Result};
use crate::value::Value;

/// A statement of `query` run by the schema's executor.
pub(crate) fn statement<S: Schema>(schema: &S, query: String, bindings: Vec<Value>) -> Statement {
    Statement::with_parts(query, bindings, false, schema.executor().cloned(), None)
}

/// A statement without bindings.
pub(crate) fn plain<S: Schema>(schema: &S, query: String) -> Result<Statement> {
    Ok(statement(schema, query, Vec::new()))
}

/// `FOREIGN KEY (col) REFERENCES table (col) [ON DELETE ...] [ON UPDATE ...]`.
pub(crate) fn foreign_key_clause<S: Schema>(schema: &S, key: &ForeignKey) -> Result<String> {
    let mut def = format!(
        "FOREIGN KEY ({}) REFERENCES {} ({})",
        schema.quote_literal(&key.column)?,
        schema.quote(&key.ref_table)?,
        schema.quote_literal(&key.ref_column)?
    );
    if let Some(action) = key.on_delete {
        def.push_str(" ON DELETE ");
        def.push_str(action.to_sql());
    }
    if let Some(action) = key.on_update {
        def.push_str(" ON UPDATE ");
        def.push_str(action.to_sql());
    }
    Ok(def)
}

/// PHP: `Schema::compileCreate()`.
pub(crate) fn create<S: Schema>(schema: &S, table: &Table<S>, if_not_exists: bool) -> Result<Statement> {
    let t = table.definition();
    let mut defs = Vec::new();
    let mut primary = Vec::new();
    let mut unique = Vec::new();
    for column in &t.columns {
        defs.push(schema.compile_column_definition(column)?);
        if column.def.primary {
            primary.push(schema.quote_literal(&column.def.name)?);
        }
        if column.def.unique {
            unique.push(column.def.name.as_str());
        }
    }
    if !t.composite_primary_key.is_empty() && !primary.is_empty() {
        return Err(Error::Validation(
            "Cannot combine column-level primary() with Table::primary() composite key.".into(),
        ));
    }
    defs.extend(t.raw_column_defs.iter().cloned());
    if !primary.is_empty() {
        defs.push(format!("PRIMARY KEY ({})", primary.join(", ")));
    } else if !t.composite_primary_key.is_empty() {
        let keys = t.composite_primary_key.iter().map(|c| schema.quote_literal(c)).collect::<Result<Vec<_>>>()?;
        defs.push(format!("PRIMARY KEY ({})", keys.join(", ")));
    }
    for column in unique {
        defs.push(format!("UNIQUE ({})", schema.quote_literal(column)?));
    }
    for check in &t.checks {
        defs.push(format!("CONSTRAINT {} CHECK ({})", schema.quote(&check.name)?, check.expression));
    }
    for index in &t.indexes {
        let keyword = match index.kind {
            IndexType::Unique => "UNIQUE INDEX",
            IndexType::Fulltext => "FULLTEXT INDEX",
            IndexType::Spatial => "SPATIAL INDEX",
            _ => "INDEX",
        };
        defs.push(format!("{keyword} {} ({})", schema.quote(&index.name)?, schema.compile_index_columns(index)?));
    }
    defs.extend(t.raw_index_defs.iter().cloned());
    for key in &t.foreign_keys {
        defs.push(foreign_key_clause(schema, key)?);
    }
    let mut sql = format!(
        "CREATE TABLE {}{} ({})",
        if if_not_exists { "IF NOT EXISTS " } else { "" },
        schema.quote(&t.name)?,
        defs.join(", ")
    );
    let suffix = schema.compile_create_suffix(table);
    if !suffix.is_empty() {
        sql.push(' ');
        sql.push_str(&suffix);
    }
    plain(schema, sql)
}

/// PHP: `Schema::compileAlter()`.
pub(crate) fn alter<S: Schema>(schema: &S, table: &Table<S>) -> Result<Statement> {
    let t = table.definition();
    let mut alterations = Vec::new();
    for column in &t.columns {
        let keyword = if column.def.modify { "MODIFY COLUMN" } else { "ADD COLUMN" };
        let mut def = format!("{keyword} {}", schema.compile_column_definition(column)?);
        if let Some(after) = &column.def.after {
            def.push_str(" AFTER ");
            def.push_str(&schema.quote_literal(after)?);
        }
        alterations.push(def);
    }
    for rename in &t.rename_columns {
        alterations.push(format!(
            "RENAME COLUMN {} TO {}",
            schema.quote_literal(&rename.from)?,
            schema.quote_literal(&rename.to)?
        ));
    }
    for column in &t.drop_columns {
        alterations.push(format!("DROP COLUMN {}", schema.quote_literal(column)?));
    }
    for index in &t.indexes {
        let keyword = match index.kind {
            IndexType::Unique => "ADD UNIQUE INDEX",
            IndexType::Fulltext => "ADD FULLTEXT INDEX",
            IndexType::Spatial => "ADD SPATIAL INDEX",
            _ => "ADD INDEX",
        };
        alterations.push(format!(
            "{keyword} {} ({})",
            schema.quote(&index.name)?,
            schema.compile_index_columns(index)?
        ));
    }
    for name in &t.drop_indexes {
        alterations.push(format!("DROP INDEX {}", schema.quote(name)?));
    }
    for key in &t.foreign_keys {
        alterations.push(format!("ADD {}", foreign_key_clause(schema, key)?));
    }
    for name in &t.drop_foreign_keys {
        alterations.push(format!("DROP FOREIGN KEY {}", schema.quote(name)?));
    }
    plain(schema, format!("ALTER TABLE {} {}", schema.quote(&t.name)?, alterations.join(", ")))
}

/// The `DEFAULT` literal of a value: `NULL`, `1` / `0` for booleans,
/// numbers as PHP prints them, anything else a quoted string.
///
/// PHP: `Schema::compileDefaultValue()`.
pub(crate) fn default_value<S: Schema>(schema: &S, value: &Value) -> Result<String> {
    Ok(match value {
        Value::Null => "NULL".into(),
        Value::Bool(b) => if *b { "1" } else { "0" }.into(),
        Value::Int(i) => i.to_string(),
        Value::Float(f) => php_std::number::to_string(*f),
        other => return schema.literal().quote(&crate::value::to_php_string(other)),
    })
}

/// Adds the parts of a definition to `out`, a space before each.
pub(crate) fn add(out: &mut String, parts: &[&str]) {
    out.push(' ');
    for part in parts {
        out.push_str(part);
    }
}

/// The name and type every column definition starts with.
pub(crate) fn column_start<S: Schema>(schema: &S, column: &Column<S>) -> Result<String> {
    let mut out = schema.quote_literal(&column.def.name)?;
    add(&mut out, &[&schema.compile_column_type(column)?]);
    Ok(out)
}

/// `DEFAULT ...`, the raw default first.
pub(crate) fn add_default<S: Schema>(schema: &S, out: &mut String, column: &Column<S>) -> Result<()> {
    if let Some(raw) = &column.def.default_raw {
        add(out, &["DEFAULT ", raw]);
    } else if let Some(value) = &column.def.default {
        add(out, &["DEFAULT ", &default_value(schema, value)?]);
    }
    Ok(())
}

/// PHP: `Schema::compileColumnDefinition()`.
pub(crate) fn column_definition<S: Schema>(schema: &S, column: &Column<S>) -> Result<String> {
    let c = &column.def;
    let mut out = column_start(schema, column)?;
    if let Some(collation) = &c.collation {
        add(&mut out, &["COLLATE ", &schema.quote_collation(collation)?]);
    }
    if c.unsigned {
        let unsigned = schema.compile_unsigned();
        if !unsigned.is_empty() {
            add(&mut out, &[unsigned]);
        }
    }
    let generated = c.generated_expression.is_some();
    if generated {
        add(&mut out, &[&schema.compile_generated_clause(column)]);
    } else if c.auto_increment {
        add(&mut out, &[schema.compile_auto_increment()]);
    }
    add(&mut out, &[if c.nullable { "NULL" } else { "NOT NULL" }]);
    if !generated {
        add_default(schema, &mut out, column)?;
    }
    if let Some(check) = &c.check {
        add(&mut out, &["CHECK (", check, ")"]);
    }
    if let Some(text) = &c.comment {
        add(&mut out, &["COMMENT ", &schema.literal().quote(text)?]);
    }
    Ok(out)
}

/// PHP: `Schema::compileGeneratedClause()`.
pub(crate) fn generated_clause<S>(column: &Column<S>) -> String {
    let stored = column.def.generated_stored.unwrap_or(false);
    format!(
        "GENERATED ALWAYS AS ({}) {}",
        column.def.generated_expression.as_deref().unwrap_or_default(),
        if stored { "STORED" } else { "VIRTUAL" }
    )
}

/// PHP: `Schema::compileIndexColumns()`.
pub(crate) fn index_columns<S: Schema>(schema: &S, index: &Index) -> Result<String> {
    let mut parts = Vec::with_capacity(index.columns.len() + index.raw_columns.len());
    for column in &index.columns {
        let mut part = schema.quote_literal(column)?;
        if let Some(collation) = index.collation(column) {
            if !names::is_word(collation) {
                return Err(Error::Validation(format!("Invalid collation: {collation}")));
            }
            part.push_str(" COLLATE ");
            part.push_str(collation);
        }
        if let Some(length) = index.length(column) {
            part.push_str(&format!("({length})"));
        }
        if !index.operator_class.is_empty() {
            part.push(' ');
            part.push_str(&index.operator_class);
        }
        if let Some(order) = index.order(column) {
            let upper = order.to_ascii_uppercase();
            if Order::try_from_value(&upper).is_none() {
                return Err(Error::Validation(format!("Invalid index order: {order}")));
            }
            part.push(' ');
            part.push_str(&upper);
        }
        parts.push(part);
    }
    parts.extend(index.raw_columns.iter().cloned());
    Ok(parts.join(", "))
}

/// PHP: `Schema::createIndex()`.
pub(crate) fn create_index<S: Schema>(
    schema: &S,
    table: &str,
    name: &str,
    columns: &[&str],
    options: &IndexOptions,
) -> Result<Statement> {
    let keyword = match (options.unique, options.kind) {
        (true, _) => "CREATE UNIQUE INDEX",
        (false, Some(IndexType::Fulltext)) => "CREATE FULLTEXT INDEX",
        (false, Some(IndexType::Spatial)) => "CREATE SPATIAL INDEX",
        _ => "CREATE INDEX",
    };
    let index = options.index(name, columns)?;
    let mut sql = format!("{keyword} {} ON {}", schema.quote(name)?, schema.quote(table)?);
    if !options.method.is_empty() {
        sql.push_str(" USING ");
        sql.push_str(&options.method.to_ascii_uppercase());
    }
    sql.push_str(&format!(" ({})", schema.compile_index_columns(&index)?));
    plain(schema, sql)
}

/// PHP: `Schema\Trait\Views::createView()` and `Trait\ReplaceView`.
pub(crate) fn create_view<S: Schema>(
    schema: &S,
    keyword: &str,
    name: &str,
    query: &mut dyn AnyBuilder,
) -> Result<Statement> {
    let built = query.build()?;
    let sql = format!("{keyword} {} AS {}", schema.quote(name)?, built.query);
    Ok(statement(schema, sql, built.bindings))
}

/// PHP: `Schema\Trait\ForeignKeys::addForeignKey()`.
pub(crate) fn add_foreign_key<S: Schema>(schema: &S, table: &str, name: &str, key: &ForeignKey) -> Result<Statement> {
    let sql = format!(
        "ALTER TABLE {} ADD CONSTRAINT {} {}",
        schema.quote(table)?,
        schema.quote(name)?,
        foreign_key_clause(schema, key)?
    );
    plain(schema, sql)
}

/// PHP: `Schema\Trait\Partitioning::compileCreatePartitioning()`.
pub(crate) fn partitioning<S>(table: &Table<S>, with_count: bool) -> String {
    let t = table.definition();
    let Some(kind) = t.partition_type else {
        return String::new();
    };
    let mut sql = format!("PARTITION BY {}({})", kind.as_str(), t.partition_expression);
    if with_count && let Some(count) = t.partition_count {
        sql.push_str(&format!(" PARTITIONS {count}"));
    }
    sql
}

/// PHP: `Schema\Trait\Procedures::compileProcedureParams()`.
pub(crate) fn procedure_params<S: Schema>(
    schema: &S,
    params: &[(ParameterDirection, &str, &str)],
) -> Result<Vec<String>> {
    let mut list = Vec::with_capacity(params.len());
    for (direction, name, kind) in params {
        let name = schema.quote(name)?;
        if !names::is_type_declaration(kind) {
            return Err(Error::Validation(format!("Invalid procedure parameter type: {kind}")));
        }
        list.push(format!("{} {name} {kind}", direction.as_str()));
    }
    Ok(list)
}

/// PHP: `Schema\Trait\Procedures::createProcedure()`.
pub(crate) fn create_procedure<S: Schema>(
    schema: &S,
    name: &str,
    params: &[(ParameterDirection, &str, &str)],
    body: &str,
) -> Result<Statement> {
    let params = procedure_params(schema, params)?;
    plain(schema, format!("CREATE PROCEDURE {}({}) BEGIN {body} END", schema.quote(name)?, params.join(", ")))
}

/// PHP: `Schema\Trait\Triggers::createTrigger()`.
pub(crate) fn create_trigger<S: Schema>(
    schema: &S,
    name: &str,
    table: &str,
    timing: TriggerTiming,
    event: TriggerEvent,
    body: &str,
) -> Result<Statement> {
    plain(
        schema,
        format!(
            "CREATE TRIGGER {} {} {} ON {} FOR EACH ROW BEGIN {body} END",
            schema.quote(name)?,
            timing.as_str(),
            event.as_str(),
            schema.quote(table)?
        ),
    )
}

/// PHP: `Schema\Trait\MaterializedViews::createMaterializedView()`.
pub(crate) fn create_materialized_view<S: Schema>(
    schema: &S,
    name: &str,
    body: ViewBody<'_>,
    target_table: Option<&str>,
    if_not_exists: bool,
) -> Result<Statement> {
    let (body, bindings) = match body {
        ViewBody::Builder(builder) => {
            let built = builder.build()?;
            (built.query, built.bindings)
        }
        ViewBody::Sql(sql) => (sql.to_owned(), Vec::new()),
    };
    let target = match target_table {
        Some(target) => format!(" TO {}", schema.quote(target)?),
        None => String::new(),
    };
    let sql = format!(
        "CREATE MATERIALIZED VIEW {}{}{target} AS {body}",
        if if_not_exists { "IF NOT EXISTS " } else { "" },
        schema.quote(name)?
    );
    Ok(statement(schema, sql, bindings))
}

/// PHP: `Schema\Trait\MaterializedViews::dropMaterializedView()`.
pub(crate) fn drop_materialized_view<S: Schema>(schema: &S, name: &str, if_exists: bool) -> Result<Statement> {
    plain(schema, format!("DROP VIEW {}{}", if if_exists { "IF EXISTS " } else { "" }, schema.quote(name)?))
}