rok-sqlc 0.3.0

Generate SQLx Rust database modules from SQL schema files
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
//! Enum types (`CREATE`, `ALTER`, and `DROP TYPE`), and the value-list checks
//! that become enums.

use std::collections::HashMap;

use sqlparser::ast::{
    AlterType, AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, Expr, ObjectName,
    Value,
};

use super::alter::drop_column;
use super::names::{ident_name, normalize_schema, object_leaf_name, object_schema_name};
use super::table::rust_type;
use crate::parser::naming;
use crate::schema::{BaseType, Column, EnumType, EnumVariant, Shape, Table};

/// Whether any column of the table has the enum type at `rust_path`, alone
/// or as an array element.
pub(super) fn uses_type(table: &Table, rust_path: &str) -> bool {
    table
        .columns
        .iter()
        .any(|column| uses_type_column(column, rust_path))
}

/// Whether the column has the enum type at `rust_path`, alone or in an array.
fn uses_type_column(column: &Column, rust_path: &str) -> bool {
    column.sqlx_type == rust_path || Shape::of(&column.sqlx_type) == Shape::List(rust_path)
}

/// Turns every value-list column into an enum named after its table and
/// column (`products.product_type` → `ProductProductType`), in table and
/// column order. A table whose value list cannot be generated (unusable or
/// colliding labels, or a name another enum already took) is reported by
/// module name with the reason, and its other value lists are not generated.
/// `earlier` holds value-list enums resolved before, whose names are taken.
pub(super) fn resolve_value_lists(
    tables: &mut [Table],
    enums: &EnumRegistry,
    earlier: &[EnumType],
) -> (Vec<EnumType>, HashMap<String, String>) {
    let mut generated: Vec<EnumType> = Vec::new();
    let mut broken = HashMap::new();
    for table in tables.iter_mut() {
        let mut table_enums = Vec::new();
        let mut failure = None;
        for column in &table.columns {
            let Some(labels) = &column.value_list else {
                continue;
            };
            let rust_name =
                naming::type_name(&format!("{}_{}", table.struct_name, column.rust_name));
            let taken = enums.has_rust_name(&rust_name)
                || earlier
                    .iter()
                    .chain(&generated)
                    .chain(&table_enums)
                    .any(|other: &EnumType| other.rust_name == rust_name);
            let variants = if taken {
                Err(format!(
                    "its Rust name `{rust_name}` is already used by another enum type"
                ))
            } else {
                enum_variants(labels.clone())
            };
            match variants {
                Ok(variants) => table_enums.push(EnumType {
                    schema: None,
                    sql_name: column.sql_name.clone(),
                    rust_name,
                    variants,
                    column: Some(format!("{}.{}", table.display_name(), column.sql_name)),
                }),
                Err(reason) => {
                    failure = Some(format!(
                        "column `{}`: value list cannot be generated: {reason}",
                        column.sql_name
                    ));
                    break;
                }
            }
        }
        if let Some(reason) = failure {
            broken.insert(table.module_name.clone(), reason);
            continue;
        }
        for enum_type in table_enums {
            let column = table
                .columns
                .iter_mut()
                .find(|column| Some(&column.sql_name) == Some(&enum_type.sql_name))
                .expect("value-list enums come from the table's columns");
            column.sqlx_type = enum_type.rust_path();
            column.rust_type = rust_type(&column.sqlx_type, column.nullable);
            generated.push(enum_type);
        }
    }
    (generated, broken)
}

/// Enum types defined so far. A type that cannot be generated (unusable or
/// colliding labels or names, or an `ALTER TYPE` that could not be applied)
/// stays registered with the reason, so a column using it fails its table.
#[derive(Default)]
pub(super) struct EnumRegistry {
    pub(super) entries: Vec<EnumEntry>,
}

pub(super) struct EnumEntry {
    pub(super) enum_type: EnumType,
    pub(super) broken: Option<String>,
}

impl EnumEntry {
    /// The column type, or why the enum cannot be generated.
    pub(super) fn rust_path(&self) -> std::result::Result<String, String> {
        match &self.broken {
            None => Ok(self.enum_type.rust_path()),
            Some(reason) => Err(format!(
                "type `{}` cannot be generated: {reason}",
                self.enum_type.display_name()
            )),
        }
    }

    /// Recomputes the variants from `labels`, breaking the enum when two
    /// labels collide or a label has no usable characters.
    fn set_labels(&mut self, labels: Vec<String>) {
        match enum_variants(labels) {
            Ok(variants) => self.enum_type.variants = variants,
            Err(reason) => self.broken = Some(reason),
        }
    }

    fn labels(&self) -> Vec<String> {
        self.enum_type
            .variants
            .iter()
            .map(|variant| variant.label.clone())
            .collect()
    }
}

/// An `ALTER TYPE … RENAME TO`: the columns of the enum type at `from` now
/// have the type at `to`.
pub(super) struct RenamedEnum {
    from: String,
    to: String,
}

/// An `ALTER TYPE` that broke an enum: every table using it fails.
pub(super) struct BrokenEnum {
    pub(super) rust_path: String,
    pub(super) reason: String,
}

impl EnumRegistry {
    fn position(&self, name: &ObjectName) -> Option<usize> {
        let schema = normalize_schema(object_schema_name(name));
        let sql_name = object_leaf_name(name);
        self.entries.iter().position(|entry| {
            entry.enum_type.sql_name == sql_name && entry.enum_type.schema == schema
        })
    }

    /// Whether any enum type, generatable or not, has this Rust name.
    fn has_rust_name(&self, rust_name: &str) -> bool {
        self.entries
            .iter()
            .any(|entry| entry.enum_type.rust_name == rust_name)
    }

    pub(super) fn lookup(&self, name: &ObjectName) -> Option<&EnumEntry> {
        self.position(name).map(|index| &self.entries[index])
    }

    /// The Rust name of the enum type `schema.sql_name`: the type name in
    /// UpperCamelCase, prefixed with the schema when another enum (other
    /// than the one at `except`) already took that name. Also returns why
    /// the name cannot be generated.
    fn rust_name_for(
        &self,
        schema: Option<&str>,
        sql_name: &str,
        except: Option<usize>,
    ) -> (String, Option<String>) {
        let taken = |rust_name: &str| {
            self.entries.iter().enumerate().any(|(index, entry)| {
                Some(index) != except && entry.enum_type.rust_name == rust_name
            })
        };
        let mut rust_name = naming::type_name(sql_name);
        if taken(&rust_name)
            && let Some(schema) = schema
        {
            rust_name = naming::type_name(&format!("{schema}_{sql_name}"));
        }
        let broken = if rust_name.is_empty() {
            Some(format!(
                "`{sql_name}` does not contain any characters usable in a Rust identifier"
            ))
        } else if taken(&rust_name) {
            Some(format!(
                "its Rust name `{rust_name}` is already used by another enum type"
            ))
        } else {
            None
        };
        (rust_name, broken)
    }

    /// Registers a `CREATE TYPE … AS ENUM`, named by [`Self::rust_name_for`].
    pub(super) fn define(&mut self, name: &ObjectName, labels: impl Iterator<Item = String>) {
        if let Some(index) = self.position(name) {
            self.entries[index].broken = Some("the type is defined more than once".to_owned());
            return;
        }
        let schema = normalize_schema(object_schema_name(name));
        let sql_name = object_leaf_name(name);
        let (rust_name, broken) = self.rust_name_for(schema.as_deref(), &sql_name, None);
        let mut entry = EnumEntry {
            enum_type: EnumType {
                schema,
                sql_name,
                rust_name,
                variants: Vec::new(),
                column: None,
            },
            broken,
        };
        entry.set_labels(labels.collect());
        self.entries.push(entry);
    }

    /// Applies an `ALTER TYPE` to a known enum. A `RENAME TO` returns the
    /// enum's old and new paths, for the columns using it to follow. An
    /// alteration that breaks a generatable enum returns its path and the
    /// reason. Unknown and already broken types are left alone.
    pub(super) fn alter(
        &mut self,
        alter: &AlterType,
    ) -> std::result::Result<Option<RenamedEnum>, BrokenEnum> {
        let Some(index) = self.position(&alter.name) else {
            return Ok(None);
        };
        if self.entries[index].broken.is_some() {
            return Ok(None);
        }
        let mut labels = self.entries[index].labels();
        let mut renamed = None;
        let applied = match &alter.operation {
            AlterTypeOperation::AddValue(add) => add_enum_value(&mut labels, add),
            AlterTypeOperation::RenameValue(rename) => {
                let (from, to) = (&rename.from.value, &rename.to.value);
                match labels.iter().position(|label| label == from) {
                    _ if labels.contains(to) => {
                        Err(format!("RENAME VALUE: label `{to}` already exists"))
                    }
                    Some(index) => {
                        labels[index] = to.clone();
                        Ok(())
                    }
                    None => Err(format!("RENAME VALUE: label `{from}` does not exist")),
                }
            }
            AlterTypeOperation::Rename(rename) => {
                let sql_name = ident_name(&rename.new_name);
                let schema = self.entries[index].enum_type.schema.clone();
                let exists = self.entries.iter().any(|entry| {
                    entry.enum_type.sql_name == sql_name && entry.enum_type.schema == schema
                });
                let (rust_name, broken) =
                    self.rust_name_for(schema.as_deref(), &sql_name, Some(index));
                match (exists, broken) {
                    (true, _) => {
                        let name = match &schema {
                            Some(schema) => format!("{schema}.{sql_name}"),
                            None => sql_name,
                        };
                        Err(format!("RENAME TO: type `{name}` already exists"))
                    }
                    (false, Some(reason)) => Err(format!("RENAME TO: {reason}")),
                    (false, None) => {
                        let enum_type = &mut self.entries[index].enum_type;
                        let from = enum_type.rust_path();
                        enum_type.sql_name = sql_name;
                        enum_type.rust_name = rust_name;
                        renamed = Some(RenamedEnum {
                            from,
                            to: enum_type.rust_path(),
                        });
                        Ok(())
                    }
                }
            }
        };
        let entry = &mut self.entries[index];
        let reason = match applied {
            Ok(()) => {
                entry.set_labels(labels);
                match entry.broken.clone() {
                    None => return Ok(renamed),
                    Some(reason) => reason,
                }
            }
            Err(reason) => reason,
        };
        entry.broken = Some(reason.clone());
        Err(BrokenEnum {
            rust_path: entry.enum_type.rust_path(),
            reason: format!("ALTER TYPE `{}`: {reason}", entry.enum_type.display_name()),
        })
    }

    /// Applies a `DROP TYPE` to one name: a known enum is forgotten, and its
    /// column type returned so the tables using it can follow.
    pub(super) fn drop(&mut self, name: &ObjectName) -> Option<String> {
        let index = self.position(name)?;
        Some(self.entries.remove(index).enum_type.rust_path())
    }

    /// The enums the `types` module generates.
    pub(super) fn into_generatable(self) -> Vec<EnumType> {
        self.entries
            .into_iter()
            .filter(|entry| entry.broken.is_none())
            .map(|entry| entry.enum_type)
            .collect()
    }
}

fn add_enum_value(
    labels: &mut Vec<String>,
    add: &AlterTypeAddValue,
) -> std::result::Result<(), String> {
    let value = &add.value.value;
    if labels.contains(value) {
        if add.if_not_exists {
            return Ok(());
        }
        return Err(format!("ADD VALUE: label `{value}` already exists"));
    }
    let index = match &add.position {
        None => labels.len(),
        Some(
            AlterTypeAddValuePosition::Before(neighbor)
            | AlterTypeAddValuePosition::After(neighbor),
        ) => {
            let found = labels
                .iter()
                .position(|label| *label == neighbor.value)
                .ok_or_else(|| format!("ADD VALUE: label `{}` does not exist", neighbor.value))?;
            match &add.position {
                Some(AlterTypeAddValuePosition::After(_)) => found + 1,
                _ => found,
            }
        }
    };
    labels.insert(index, value.clone());
    Ok(())
}

/// One variant per label, in label order, each named by its label in
/// UpperCamelCase (`Very High` → `VeryHigh`). An enum with no labels, a
/// label with no usable characters, or two labels naming the same variant
/// cannot be generated.
fn enum_variants(labels: Vec<String>) -> std::result::Result<Vec<EnumVariant>, String> {
    if labels.is_empty() {
        return Err("the enum has no labels".to_owned());
    }
    let mut variants: Vec<EnumVariant> = Vec::with_capacity(labels.len());
    for label in labels {
        let rust_name = naming::variant_name(&label).ok_or_else(|| {
            format!("label `{label}` has no characters usable in a Rust identifier")
        })?;
        if let Some(other) = variants
            .iter()
            .find(|variant| variant.rust_name == rust_name)
        {
            return Err(format!(
                "labels `{}` and `{label}` both map to the variant `{rust_name}`",
                other.label
            ));
        }
        variants.push(EnumVariant { label, rust_name });
    }
    Ok(variants)
}

/// Moves every column of a renamed enum type (or an array of it) to the
/// enum's new path.
pub(super) fn retype_columns(table: &mut Table, renamed: &RenamedEnum) {
    for column in &mut table.columns {
        if column.sqlx_type == renamed.from {
            column.sqlx_type.clone_from(&renamed.to);
        } else if Shape::of(&column.sqlx_type) == Shape::List(&renamed.from) {
            column.sqlx_type = format!("Vec<{}>", renamed.to);
        } else {
            continue;
        }
        column.rust_type = rust_type(&column.sqlx_type, column.nullable);
    }
}

/// Drops every column of the enum type at `rust_path` (or an array of it),
/// as `DROP TYPE … CASCADE` does.
pub(super) fn drop_columns_of_type(table: &mut Table, rust_path: &str) {
    let doomed: Vec<String> = table
        .columns
        .iter()
        .filter(|column| uses_type_column(column, rust_path))
        .map(|column| column.sql_name.clone())
        .collect();
    for name in doomed {
        // The column exists, so dropping it cannot fail.
        let _ = drop_column(table, &name, true);
    }
}

/// The column and values of a value-list check, `col IN ('a', 'b', …)`
/// (parenthesized or not), with duplicate values dropped. Any other shape,
/// including `NOT IN` and lists holding anything but string literals, is
/// `None`.
pub(super) fn value_list_check(expr: &Expr) -> Option<(String, Vec<String>)> {
    match expr {
        Expr::Nested(inner) => value_list_check(inner),
        Expr::InList {
            expr,
            list,
            negated: false,
        } => {
            let Expr::Identifier(column) = expr.as_ref() else {
                return None;
            };
            let mut labels: Vec<String> = Vec::with_capacity(list.len());
            for item in list {
                let Expr::Value(value) = item else {
                    return None;
                };
                let Value::SingleQuotedString(label) = &value.value else {
                    return None;
                };
                if !labels.contains(label) {
                    labels.push(label.clone());
                }
            }
            Some((ident_name(column), labels))
        }
        _ => None,
    }
}

/// Restricts a text column to a list of values. A second value list on the
/// same column keeps only the values both allow. Other column types are left
/// alone, like checks of any other shape.
pub(super) fn restrict_to_values(column: &mut Column, labels: Vec<String>) {
    if column.base_type() != BaseType::Text {
        return;
    }
    column.value_list = Some(match column.value_list.take() {
        Some(existing) => existing
            .into_iter()
            .filter(|label| labels.contains(label))
            .collect(),
        None => labels,
    });
}