schema-parser 0.7.1

A set of tools to manage relational database schemas
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
use crate::parser::convert::{DatabaseTarget, agg_frequency_from_str};
use crate::parser::nodes::{AggregateColumnXml, TableXml};
use schema_model::builder::{ColumnBuilder, KeyBuilder};
use schema_model::model::aggregation::{
    Aggregation, AggregationColumn, AggregationGroup, AggregationType,
};
use schema_model::model::column::Column;
use schema_model::model::column_type::ColumnType;
use schema_model::model::constraint::Constraint;
use schema_model::model::initial_data::InitialData;
use schema_model::model::key::Key;
use schema_model::model::relation::Relation;
use schema_model::model::table::Table;
use schema_model::model::trigger::Trigger;
use schema_model::model::types::{KeyType, LockEscalation, RelationType, TableOption, TriggerType};

pub(crate) fn parse_table(
    table_xml: &TableXml,
    schema_name: Option<&str>,
) -> Result<Table, String> {
    let columns = parse_columns(table_xml, schema_name)?;
    let keys = parse_keys(table_xml)?;
    let indexes = parse_indexes(table_xml);
    let relations = parse_relations(table_xml)?;
    let triggers = parse_triggers(table_xml)?;
    let constraints = parse_constraints(table_xml)?;
    let aggregations = parse_aggregations(table_xml)?;
    let initial_data = parse_initial_data(table_xml)?;
    let options = parse_table_options(table_xml);

    Ok(Table::new(
        schema_name,
        table_xml.name.as_str(),
        table_xml.export_data_column.as_deref(),
        lock_escalation(table_xml)?,
        table_xml.no_export.unwrap_or(false),
        columns,
        keys,
        indexes,
        relations,
        triggers,
        constraints,
        initial_data,
        options,
        aggregations,
    ))
}

fn lock_escalation(table_xml: &TableXml) -> Result<LockEscalation, String> {
    use std::str::FromStr;
    match table_xml.lock_escalation.as_deref() {
        Some(s) => {
            LockEscalation::from_str(s).map_err(|e| format!("table '{}': {}", table_xml.name, e))
        }
        None => Ok(LockEscalation::Auto),
    }
}

fn parse_columns(table_xml: &TableXml, schema_name: Option<&str>) -> Result<Vec<Column>, String> {
    let mut columns = Vec::new();

    if let Some(columns_xml) = &table_xml.columns {
        for column_xml in columns_xml.column.iter() {
            let column_type = ColumnType::from_type_name(&column_xml.r#type).map_err(|e| {
                format!(
                    "table '{}', column '{}': {}",
                    table_xml.name, column_xml.name, e
                )
            })?;
            let mut builder = ColumnBuilder::new(schema_name, &column_xml.name, column_type)
                .length(column_xml.length.unwrap_or(0))
                .scale(column_xml.scale.unwrap_or(0))
                .required(column_xml.required.unwrap_or(false));
            let mut seen_checks: Vec<Option<&str>> = Vec::new();
            for check_xml in column_xml.check.iter() {
                let target = check_xml.database_type.as_deref();
                if seen_checks.contains(&target) {
                    return Err(format!(
                        "table '{}', column '{}': more than one check for {}",
                        table_xml.name,
                        column_xml.name,
                        target.map_or("every database (no databaseType)".to_string(), |t| {
                            format!("databaseType '{t}'")
                        })
                    ));
                }
                seen_checks.push(target);
                let Some(check) = check_xml.value.clone() else {
                    continue;
                };
                match check_xml.database_type.as_deref() {
                    None => builder = builder.check_constraint(Some(check)),
                    Some(name) => {
                        // A check for a database this library never generates for (e.g. h2)
                        // cannot apply to anything, so it is dropped.
                        let context =
                            format!("table '{}', column '{}'", table_xml.name, column_xml.name);
                        if let Some(database_type) =
                            crate::parser::convert::required_database_type(Some(name), &context)?
                        {
                            builder = builder.database_check(database_type, check);
                        }
                    }
                }
            }
            let column = builder
                .default_constraint(column_xml.default_value.clone())
                .generated(column_xml.generated.clone())
                .min_value(column_xml.min_value.clone())
                .max_value(column_xml.max_value.clone())
                .enum_type(column_xml.enum_type.clone())
                .element_type(column_xml.element_type.clone())
                .build();
            columns.push(column);
        }
    }

    Ok(columns)
}

fn parse_keys(table_xml: &TableXml) -> Result<Vec<Key>, String> {
    let mut keys = Vec::new();

    if let Some(keys_xml) = &table_xml.keys {
        if let Some(primary_key_xml) = &keys_xml.primary {
            let mut kb = KeyBuilder::new(KeyType::Primary);
            for kc in primary_key_xml.columns.iter() {
                kb = kb.add_column(&kc.name);
            }
            if let Some(c) = primary_key_xml.cluster {
                kb = kb.cluster(c);
            }
            keys.push(kb.build());
        }
        for unique_key_xml in keys_xml.uniques.iter() {
            if unique_key_xml.where_clause.is_some() {
                if unique_key_xml.cluster == Some(true) {
                    return Err(format!(
                        "table '{}': a filtered/partial unique index cannot be clustered",
                        table_xml.name
                    ));
                }
                continue;
            }
            let mut kb = KeyBuilder::new(KeyType::Unique);
            for kc in unique_key_xml.columns.iter() {
                kb = kb.add_column(&kc.name);
            }
            if let Some(c) = unique_key_xml.cluster {
                kb = kb.cluster(c);
            }
            keys.push(kb.build());
        }
    }

    Ok(keys)
}

fn parse_indexes(table_xml: &TableXml) -> Vec<Key> {
    let mut indexes = Vec::new();

    if let Some(keys_xml) = &table_xml.keys {
        for index_xml in keys_xml.indexes.iter() {
            let mut key_builder = KeyBuilder::new(KeyType::Index);
            for key_column_xml in index_xml.columns.iter() {
                key_builder = key_builder.add_column(&key_column_xml.name);
            }
            if let Some(s) = &index_xml.include {
                key_builder = key_builder.include(s);
            }
            if let Some(v) = index_xml.compress {
                key_builder = key_builder.compress(v);
            }
            if let Some(v) = index_xml.unique {
                key_builder = key_builder.unique(v);
            }
            if let Some(s) = &index_xml.where_clause {
                key_builder = key_builder.filter(s);
            }
            indexes.push(key_builder.build());
        }

        for unique_key_xml in keys_xml.uniques.iter().filter(|u| u.where_clause.is_some()) {
            let mut key_builder = KeyBuilder::new(KeyType::Index);
            for kc in unique_key_xml.columns.iter() {
                key_builder = key_builder.add_column(&kc.name);
            }
            key_builder = key_builder.unique(true);
            if let Some(s) = &unique_key_xml.where_clause {
                key_builder = key_builder.filter(s);
            }
            indexes.push(key_builder.build());
        }
    }

    indexes
}

fn parse_relation_type(
    table_xml: &TableXml,
    target_table: &str,
    type_str: &str,
) -> Result<RelationType, String> {
    match type_str.to_ascii_lowercase().as_str() {
        "cascade" => Ok(RelationType::Cascade),
        "enforce" => Ok(RelationType::Enforce),
        "setnull" => Ok(RelationType::SetNull),
        "donothing" => Ok(RelationType::DoNothing),
        other => Err(format!(
            "table '{}': relation to '{}' has an unrecognized type '{}' (expected cascade, enforce, setnull, or donothing)",
            table_xml.name, target_table, other
        )),
    }
}

fn parse_relations(table_xml: &TableXml) -> Result<Vec<Relation>, String> {
    let mut relations = Vec::new();

    if let Some(relations_xml) = &table_xml.relations {
        for relation_xml in relations_xml.relation.iter() {
            let relation_type =
                parse_relation_type(table_xml, &relation_xml.table, &relation_xml.r#type)?;
            relations.push(Relation::new(
                relation_xml.table.clone(),
                relation_xml.column.clone(),
                table_xml.name.clone(),
                relation_xml.src.clone(),
                relation_type,
                relation_xml.disable_usage_checking.unwrap_or(false),
            ));
        }

        for composite_relation_xml in relations_xml.composite_relation.iter() {
            let relation_type = parse_relation_type(
                table_xml,
                &composite_relation_xml.table,
                &composite_relation_xml.r#type,
            )?;
            let column_pairs: Vec<(String, String)> = composite_relation_xml
                .column
                .iter()
                .map(|c| (c.src.clone(), c.name.clone()))
                .collect();
            relations.push(Relation::new_composite(
                composite_relation_xml.table.clone(),
                table_xml.name.clone(),
                column_pairs,
                relation_type,
                composite_relation_xml
                    .disable_usage_checking
                    .unwrap_or(false),
            )?);
        }
    }

    Ok(relations)
}

fn parse_triggers(table_xml: &TableXml) -> Result<Vec<Trigger>, String> {
    let mut triggers = Vec::new();

    if let Some(triggers_xml) = &table_xml.triggers {
        for trigger_xml in triggers_xml.update.iter() {
            let Some(database_type) = crate::parser::convert::required_database_type(
                Some(&trigger_xml.database_type),
                &format!("update trigger on table '{}'", table_xml.name),
            )?
            else {
                continue;
            };
            triggers.push(Trigger::new(
                &trigger_xml.sql,
                TriggerType::Update,
                database_type,
            ));
        }

        for trigger_xml in triggers_xml.delete.iter() {
            let Some(database_type) = crate::parser::convert::required_database_type(
                Some(&trigger_xml.database_type),
                &format!("delete trigger on table '{}'", table_xml.name),
            )?
            else {
                continue;
            };
            triggers.push(Trigger::new(
                &trigger_xml.sql,
                TriggerType::Delete,
                database_type,
            ));
        }
    }

    Ok(triggers)
}

fn parse_constraints(table_xml: &TableXml) -> Result<Vec<Constraint>, String> {
    let mut constraints = Vec::new();

    if let Some(constraints_xml) = &table_xml.constraints {
        for constraint_xml in constraints_xml.constraint.iter() {
            let target = crate::parser::convert::optional_database_type(
                constraint_xml.database_type.as_deref(),
                &format!(
                    "constraint '{}' on table '{}'",
                    constraint_xml.name, table_xml.name
                ),
            )?;
            constraints.push(match target {
                DatabaseTarget::Every => {
                    Constraint::for_every_database(&constraint_xml.name, &constraint_xml.sql)
                }
                DatabaseTarget::Only(database_type) => {
                    Constraint::new(&constraint_xml.name, &constraint_xml.sql, database_type)
                }
                DatabaseTarget::Ignored => continue,
            });
        }
    }

    Ok(constraints)
}

fn parse_aggregations(table_xml: &TableXml) -> Result<Vec<Aggregation>, String> {
    let mut aggregations = Vec::new();

    if let Some(aggregations_xml) = &table_xml.aggregations {
        for aggregate_xml in aggregations_xml.aggregate.iter() {
            let cols: Vec<AggregationColumn> = aggregate_xml
                .columns
                .iter()
                .map(|column_xml| match column_xml {
                    AggregateColumnXml::Sum(sum_xml) => AggregationColumn::new(
                        AggregationType::Sum,
                        &sum_xml.source_column,
                        &sum_xml.destination_column,
                    ),
                    AggregateColumnXml::Count(count_xml) => AggregationColumn::new(
                        AggregationType::Count,
                        "",
                        &count_xml.destination_column,
                    ),
                })
                .collect();
            let mut groups: Vec<AggregationGroup> = Vec::new();
            for group_column_xml in aggregate_xml.group.column.iter() {
                groups.push(AggregationGroup::new(
                    group_column_xml.source.clone(),
                    group_column_xml.destination.clone(),
                    group_column_xml.source_derived_from.clone(),
                ));
            }
            let freq = agg_frequency_from_str(&aggregate_xml.frequency).map_err(|e| {
                format!(
                    "table '{}', aggregation to '{}': {}",
                    table_xml.name, aggregate_xml.destination_table, e
                )
            })?;
            aggregations.push(Aggregation::new(
                aggregate_xml.destination_table.clone(),
                aggregate_xml.date_column.clone(),
                aggregate_xml.criteria.clone(),
                aggregate_xml.timestamp_column.clone(),
                freq,
                cols,
                groups,
            ));
        }
    }

    Ok(aggregations)
}

fn parse_initial_data(table_xml: &TableXml) -> Result<Vec<InitialData>, String> {
    let mut initial_data = Vec::new();

    if let Some(intial_data_xml) = &table_xml.initial_data {
        for initial_sql_xml in intial_data_xml.sql.iter() {
            let database_type = match crate::parser::convert::optional_database_type(
                initial_sql_xml.database_type.as_deref(),
                &format!("initialData on table '{}'", table_xml.name),
            )? {
                DatabaseTarget::Every => None,
                DatabaseTarget::Only(database_type) => Some(database_type),
                DatabaseTarget::Ignored => continue,
            };
            initial_data.push(InitialData::new(&initial_sql_xml.sql, database_type));
        }
    }

    Ok(initial_data)
}

fn parse_table_options(table_xml: &TableXml) -> Vec<TableOption> {
    let mut options = Vec::new();

    if table_xml.data_opt.unwrap_or(false) {
        options.push(TableOption::Data);
    }
    if table_xml.no_export.unwrap_or(false) {
        options.push(TableOption::NoExport);
    }
    if table_xml.compress.unwrap_or(false) {
        options.push(TableOption::Compress);
    }

    options
}