Skip to main content

schema_parser/parser/
roxml_parser.rs

1use crate::parser::nodes::*;
2use roxmltree::{Document, Node};
3
4const NS: &str = "http://stano.com/database";
5
6pub fn parse_database_roxml(xml: &str) -> Result<DatabaseXml, String> {
7    let doc = Document::parse(xml).map_err(|e| format!("XML parse error: {e}"))?;
8    let root = doc
9        .descendants()
10        .find(|n| n.has_tag_name((NS, "database")))
11        .ok_or_else(|| "No <database> root element found".to_string())?;
12
13    parse_database_node(root)
14}
15
16fn parse_database_node(node: Node) -> Result<DatabaseXml, String> {
17    let foreign_key_mode = attr_string(node, "foreignKeyMode");
18    let boolean_mode = attr_string(node, "booleanMode");
19    let case_sensitive_text = attr_bool(node, "caseSensitiveText")?;
20
21    let mut tables = Vec::new();
22    let mut enums = Vec::new();
23    let mut views = Vec::new();
24    let mut functions = Vec::new();
25    let mut procedures = Vec::new();
26    let mut other_sql = Vec::new();
27    let mut schemas = Vec::new();
28
29    for child in node.children().filter(|n| n.is_element()) {
30        match child.tag_name().name() {
31            "table" if child.has_tag_name((NS, "table")) => tables.push(parse_table_node(child)?),
32            "enum" if child.has_tag_name((NS, "enum")) => enums.push(parse_enum_node(child)?),
33            "view" if child.has_tag_name((NS, "view")) => views.push(parse_view_node(child)?),
34            "function" if child.has_tag_name((NS, "function")) => {
35                functions.push(parse_function_node(child)?)
36            }
37            "procedure" if child.has_tag_name((NS, "procedure")) => {
38                procedures.push(parse_procedure_node(child)?)
39            }
40            "otherSql" if child.has_tag_name((NS, "otherSql")) => {
41                other_sql.push(parse_other_sql_node(child)?)
42            }
43            "schema" if child.has_tag_name((NS, "schema")) => {
44                schemas.push(parse_schema_node(child)?)
45            }
46            _ => {}
47        }
48    }
49
50    Ok(DatabaseXml {
51        foreign_key_mode,
52        boolean_mode,
53        case_sensitive_text,
54        tables,
55        enums,
56        views,
57        functions,
58        procedures,
59        other_sql,
60        schemas,
61    })
62}
63
64fn parse_schema_node(node: Node) -> Result<SchemaXml, String> {
65    let name = attr_string_required(node, "name")?;
66    let case_sensitive_text = attr_bool(node, "caseSensitiveText")?;
67
68    let mut tables = Vec::new();
69    let mut enums = Vec::new();
70    let mut views = Vec::new();
71    let mut functions = Vec::new();
72    let mut procedures = Vec::new();
73    let mut other_sql = Vec::new();
74
75    for child in node.children().filter(|n| n.is_element()) {
76        match child.tag_name().name() {
77            "table" if child.has_tag_name((NS, "table")) => tables.push(parse_table_node(child)?),
78            "enum" if child.has_tag_name((NS, "enum")) => enums.push(parse_enum_node(child)?),
79            "view" if child.has_tag_name((NS, "view")) => views.push(parse_view_node(child)?),
80            "function" if child.has_tag_name((NS, "function")) => {
81                functions.push(parse_function_node(child)?)
82            }
83            "procedure" if child.has_tag_name((NS, "procedure")) => {
84                procedures.push(parse_procedure_node(child)?)
85            }
86            "otherSql" if child.has_tag_name((NS, "otherSql")) => {
87                other_sql.push(parse_other_sql_node(child)?)
88            }
89            _ => {}
90        }
91    }
92
93    Ok(SchemaXml {
94        name,
95        case_sensitive_text,
96        tables,
97        enums,
98        views,
99        functions,
100        procedures,
101        other_sql,
102    })
103}
104
105fn parse_table_node(node: Node) -> Result<TableXml, String> {
106    let name = attr_string_required(node, "name")?;
107    let data_opt = attr_bool(node, "data")?;
108    let no_export = attr_bool(node, "noExport")?;
109    let export_data_column = attr_string(node, "exportDataColumn");
110    let compress = attr_bool(node, "compress")?;
111    let lock_escalation = attr_string(node, "lockEscalation");
112
113    let mut columns: Option<ColumnsXml> = None;
114    let mut keys: Option<KeysXml> = None;
115    let mut relations: Option<RelationsXml> = None;
116    let mut triggers: Option<TriggersXml> = None;
117    let mut constraints: Option<ConstraintsXml> = None;
118    let mut aggregations: Option<AggregationsXml> = None;
119    let mut initial_data: Option<InitialDataXml> = None;
120
121    for child in node.children().filter(|n| n.is_element()) {
122        if child.has_tag_name((NS, "columns")) {
123            columns = Some(parse_columns_node(child)?);
124        } else if child.has_tag_name((NS, "keys")) {
125            keys = Some(parse_keys_node(child)?);
126        } else if child.has_tag_name((NS, "relations")) {
127            relations = Some(parse_relations_node(child)?);
128        } else if child.has_tag_name((NS, "triggers")) {
129            triggers = Some(parse_triggers_node(child)?);
130        } else if child.has_tag_name((NS, "constraints")) {
131            constraints = Some(parse_constraints_node(child)?);
132        } else if child.has_tag_name((NS, "aggregations")) {
133            aggregations = Some(parse_aggregations_node(child)?);
134        } else if child.has_tag_name((NS, "initialData")) {
135            initial_data = Some(parse_initial_data_node(child));
136        }
137    }
138
139    Ok(TableXml {
140        name,
141        data_opt,
142        no_export,
143        export_data_column,
144        compress,
145        lock_escalation,
146        columns,
147        keys,
148        relations,
149        triggers,
150        constraints,
151        aggregations,
152        initial_data,
153    })
154}
155
156fn parse_columns_node(node: Node) -> Result<ColumnsXml, String> {
157    let mut cols = Vec::new();
158    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
159        cols.push(parse_column_node(c)?);
160    }
161    Ok(ColumnsXml { column: cols })
162}
163
164fn parse_column_node(node: Node) -> Result<ColumnXml, String> {
165    Ok(ColumnXml {
166        name: attr_string_required(node, "name")?,
167        r#type: attr_string_required(node, "type")?,
168        length: attr_i32(node, "length")?,
169        scale: attr_i32(node, "scale")?,
170        required: attr_bool(node, "required")?,
171        default_value: attr_string(node, "default"),
172        generated: attr_string(node, "generated"),
173        enum_type: attr_string(node, "enumType"),
174        element_type: attr_string(node, "elementType"),
175        min_value: attr_f64(node, "minValue")?,
176        max_value: attr_f64(node, "maxValue")?,
177        check: node
178            .children()
179            .find(|n| n.has_tag_name((NS, "check")))
180            .map(parse_check_node),
181    })
182}
183
184fn parse_check_node(node: Node) -> CheckXml {
185    let text = collect_text(node);
186    CheckXml {
187        value: if text.is_empty() { None } else { Some(text) },
188    }
189}
190
191fn parse_keys_node(node: Node) -> Result<KeysXml, String> {
192    let mut primary: Option<KeyColumnsXml> = None;
193    let mut uniques: Vec<KeyColumnsXml> = Vec::new();
194    let mut indexes: Vec<IndexXml> = Vec::new();
195
196    for c in node.children().filter(|n| n.is_element()) {
197        if c.has_tag_name((NS, "primary")) {
198            primary = Some(parse_key_columns_node(c)?);
199        } else if c.has_tag_name((NS, "unique")) {
200            uniques.push(parse_key_columns_node(c)?);
201        } else if c.has_tag_name((NS, "index")) {
202            indexes.push(parse_index_node(c)?);
203        }
204    }
205
206    Ok(KeysXml {
207        primary,
208        uniques,
209        indexes,
210    })
211}
212
213fn parse_key_columns_node(node: Node) -> Result<KeyColumnsXml, String> {
214    let mut columns = Vec::new();
215    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
216        columns.push(KeyColumnXml {
217            name: attr_string_required(c, "name")?,
218        });
219    }
220    let cluster = attr_bool(node, "cluster")?;
221    let where_clause = attr_string(node, "where");
222    Ok(KeyColumnsXml {
223        columns,
224        cluster,
225        where_clause,
226    })
227}
228
229fn parse_index_node(node: Node) -> Result<IndexXml, String> {
230    let mut columns = Vec::new();
231    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
232        columns.push(KeyColumnXml {
233            name: attr_string_required(c, "name")?,
234        });
235    }
236    Ok(IndexXml {
237        columns,
238        include: attr_string(node, "include"),
239        compress: attr_bool(node, "compress")?,
240        unique: attr_bool(node, "unique")?,
241        where_clause: attr_string(node, "where"),
242    })
243}
244
245fn parse_relations_node(node: Node) -> Result<RelationsXml, String> {
246    let mut rels = Vec::new();
247    let mut composite_rels = Vec::new();
248    for r in node.children().filter(|n| n.is_element()) {
249        if r.has_tag_name((NS, "relation")) {
250            rels.push(RelationXml {
251                src: attr_string_required(r, "src")?,
252                table: attr_string_required(r, "table")?,
253                column: attr_string_required(r, "column")?,
254                r#type: attr_string_required(r, "type")?,
255                disable_usage_checking: attr_bool(r, "disableUsageChecking")?,
256            });
257        } else if r.has_tag_name((NS, "compositeRelation")) {
258            composite_rels.push(parse_composite_relation_node(r)?);
259        }
260    }
261    Ok(RelationsXml {
262        relation: rels,
263        composite_relation: composite_rels,
264    })
265}
266
267fn parse_composite_relation_node(node: Node) -> Result<CompositeRelationXml, String> {
268    let mut columns = Vec::new();
269    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
270        columns.push(CompositeRelationColumnXml {
271            src: attr_string_required(c, "src")?,
272            name: attr_string_required(c, "name")?,
273        });
274    }
275    Ok(CompositeRelationXml {
276        table: attr_string_required(node, "table")?,
277        r#type: attr_string_required(node, "type")?,
278        disable_usage_checking: attr_bool(node, "disableUsageChecking")?,
279        column: columns,
280    })
281}
282
283fn parse_view_node(node: Node) -> Result<ViewXml, String> {
284    Ok(ViewXml {
285        name: attr_string_required(node, "name")?,
286        database_type: attr_string(node, "databaseType"),
287        sql: collect_text(node),
288    })
289}
290
291fn parse_function_node(node: Node) -> Result<FunctionXml, String> {
292    let name = attr_string_required(node, "name")?;
293    let mut sql = Vec::new();
294    for s in node.children().filter(|n| n.has_tag_name((NS, "sql"))) {
295        sql.push(VendorSqlXml {
296            database_type: attr_string_required(s, "databaseType")?,
297            sql: collect_text(s),
298        });
299    }
300    Ok(FunctionXml { name, sql })
301}
302
303fn parse_procedure_node(node: Node) -> Result<ProcedureXml, String> {
304    let name = attr_string_required(node, "name")?;
305    let mut sql = Vec::new();
306    for s in node.children().filter(|n| n.has_tag_name((NS, "sql"))) {
307        sql.push(VendorSqlXml {
308            database_type: attr_string_required(s, "databaseType")?,
309            sql: collect_text(s),
310        });
311    }
312    Ok(ProcedureXml { name, sql })
313}
314
315fn parse_other_sql_node(node: Node) -> Result<OtherSqlXml, String> {
316    let database_type = attr_string_required(node, "databaseType")?;
317    let order_text = attr_string_required(node, "order")?;
318    let order = match order_text.to_ascii_lowercase().as_str() {
319        "top" => OtherSqlOrderXml::Top,
320        "bottom" => OtherSqlOrderXml::Bottom,
321        other => {
322            return Err(format!(
323                "<otherSql> element has an unrecognized order '{}' (expected top or bottom)",
324                other
325            ));
326        }
327    };
328    Ok(OtherSqlXml {
329        database_type,
330        order,
331        sql: collect_text(node),
332    })
333}
334
335fn parse_enum_node(node: Node) -> Result<EnumXml, String> {
336    let name = attr_string_required(node, "name")?;
337    let mut value = Vec::new();
338    for v in node.children().filter(|n| n.has_tag_name((NS, "value"))) {
339        value.push(EnumValueXml {
340            name: attr_string_required(v, "name")?,
341            code: attr_string(v, "code"),
342        });
343    }
344    Ok(EnumXml { name, value })
345}
346
347fn parse_triggers_node(node: Node) -> Result<TriggersXml, String> {
348    let mut update = Vec::new();
349    let mut delete = Vec::new();
350    for c in node.children().filter(|n| n.is_element()) {
351        if c.has_tag_name((NS, "update")) {
352            update.push(TriggerXml {
353                database_type: attr_string_required(c, "databaseType")?,
354                sql: collect_text(c),
355            });
356        } else if c.has_tag_name((NS, "delete")) {
357            delete.push(TriggerXml {
358                database_type: attr_string_required(c, "databaseType")?,
359                sql: collect_text(c),
360            });
361        }
362    }
363    Ok(TriggersXml { update, delete })
364}
365
366fn parse_constraints_node(node: Node) -> Result<ConstraintsXml, String> {
367    let mut constraint = Vec::new();
368    for c in node
369        .children()
370        .filter(|n| n.has_tag_name((NS, "constraint")))
371    {
372        constraint.push(ConstraintXml {
373            name: attr_string_required(c, "name")?,
374            database_type: attr_string(c, "databaseType"),
375            sql: collect_text(c),
376        });
377    }
378    Ok(ConstraintsXml { constraint })
379}
380
381fn parse_aggregations_node(node: Node) -> Result<AggregationsXml, String> {
382    let mut aggregate = Vec::new();
383    for a in node
384        .children()
385        .filter(|n| n.has_tag_name((NS, "aggregate")))
386    {
387        aggregate.push(parse_aggregate_node(a)?);
388    }
389    Ok(AggregationsXml { aggregate })
390}
391
392fn parse_aggregate_node(node: Node) -> Result<AggregateXml, String> {
393    let mut sum = Vec::new();
394    let mut count = Vec::new();
395    let mut group: Option<GroupXml> = None;
396
397    for c in node.children().filter(|n| n.is_element()) {
398        if c.has_tag_name((NS, "sum")) {
399            sum.push(SumXml {
400                source_column: attr_string_required(c, "sourceColumn")?,
401                destination_column: attr_string_required(c, "destinationColumn")?,
402            });
403        } else if c.has_tag_name((NS, "count")) {
404            count.push(CountXml {
405                destination_column: attr_string_required(c, "destinationColumn")?,
406            });
407        } else if c.has_tag_name((NS, "group")) {
408            group = Some(parse_group_node(c)?);
409        }
410    }
411
412    Ok(AggregateXml {
413        sum,
414        count,
415        group: group.unwrap_or_else(|| GroupXml { column: Vec::new() }),
416        destination_table: attr_string_required(node, "destinationTable")?,
417        date_column: attr_string_required(node, "dateColumn")?,
418        timestamp_column: attr_string_required(node, "timestampColumn")?,
419        frequency: attr_string_required(node, "frequency")?,
420        criteria: attr_string(node, "criteria"),
421    })
422}
423
424fn parse_group_node(node: Node) -> Result<GroupXml, String> {
425    let mut column = Vec::new();
426    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
427        column.push(GroupColumnXml {
428            source: attr_string_required(c, "source")?,
429            destination: attr_string_required(c, "destination")?,
430            source_derived_from: attr_string(c, "sourceDerivedFrom"),
431        });
432    }
433    Ok(GroupXml { column })
434}
435
436fn parse_initial_data_node(node: Node) -> InitialDataXml {
437    let mut sql = Vec::new();
438    for s in node.children().filter(|n| n.has_tag_name((NS, "sql"))) {
439        sql.push(InitialSqlXml {
440            database_type: attr_string(s, "databaseType"),
441            sql: collect_text(s),
442        });
443    }
444    InitialDataXml { sql }
445}
446
447// -------- Utils --------
448fn attr_string(node: Node, name: &str) -> Option<String> {
449    node.attribute(name).map(|s| s.to_string())
450}
451
452/// Reads a required attribute. Returns an error (naming the element and attribute)
453/// rather than silently defaulting to `""` when it's absent - a missing `name`/`type`/
454/// `table`/`column`/etc. attribute is a malformed schema definition, and nothing
455/// downstream validates against an empty name, so a silent default would otherwise let
456/// it through as a real (if oddly-named) table/column/relation.
457fn attr_string_required(node: Node, name: &str) -> Result<String, String> {
458    node.attribute(name).map(|s| s.to_string()).ok_or_else(|| {
459        format!(
460            "<{}> element is missing required attribute '{}'",
461            node.tag_name().name(),
462            name
463        )
464    })
465}
466
467/// Reads an optional boolean attribute. Unlike a missing attribute (which is a legitimate
468/// "not specified"), a *present but unparseable* value (`required="yep"`) is a malformed
469/// schema definition - silently falling back to `None`/`false` would hide a typo instead of
470/// reporting it, so this errors instead.
471fn attr_bool(node: Node, name: &str) -> Result<Option<bool>, String> {
472    match node.attribute(name) {
473        None => Ok(None),
474        Some(v) => match v.to_ascii_lowercase().as_str() {
475            "true" | "1" | "yes" | "on" => Ok(Some(true)),
476            "false" | "0" | "no" | "off" => Ok(Some(false)),
477            _ => Err(format!(
478                "<{}> attribute '{}' has invalid boolean value '{}' (expected true/false, 1/0, yes/no, or on/off)",
479                node.tag_name().name(),
480                name,
481                v
482            )),
483        },
484    }
485}
486
487/// Reads an optional integer attribute, erroring (rather than silently defaulting to `None`)
488/// when the attribute is present but not a valid `i32` - a typo like `length="1O0"` or an
489/// overflowing value like `length="99999999999"` should be reported, not turned into 0.
490fn attr_i32(node: Node, name: &str) -> Result<Option<i32>, String> {
491    match node.attribute(name) {
492        None => Ok(None),
493        Some(v) => v.parse::<i32>().map(Some).map_err(|_| {
494            format!(
495                "<{}> attribute '{}' has invalid integer value '{}'",
496                node.tag_name().name(),
497                name,
498                v
499            )
500        }),
501    }
502}
503
504/// Reads an optional numeric attribute, erroring (rather than silently defaulting to `None`)
505/// when the attribute is present but not a finite `f64` - this covers unparseable typos like
506/// `minValue="1o"` as well as `inf`/`nan`, which `f64::parse` otherwise accepts silently.
507fn attr_f64(node: Node, name: &str) -> Result<Option<f64>, String> {
508    match node.attribute(name) {
509        None => Ok(None),
510        Some(v) => match v.parse::<f64>() {
511            Ok(n) if n.is_finite() => Ok(Some(n)),
512            _ => Err(format!(
513                "<{}> attribute '{}' has invalid numeric value '{}'",
514                node.tag_name().name(),
515                name,
516                v
517            )),
518        },
519    }
520}
521
522fn collect_text(node: Node) -> String {
523    let mut out = String::new();
524    for n in node.children() {
525        if n.is_text() {
526            out.push_str(n.text().unwrap_or(""));
527        }
528    }
529    out.trim().to_string()
530}
531
532#[cfg(test)]
533mod tests {
534    use super::*;
535    use std::fs;
536
537    #[test]
538    fn parse_with_roxmltree_sample() {
539        let xml = fs::read_to_string("tests/resources/schema-parser-test-schema.xml")
540            .expect("resource present");
541        let db = parse_database_roxml(&xml).expect("parsed");
542        assert!(!db.tables.is_empty());
543        assert!(db.schemas.is_empty() || !db.schemas[0].tables.is_empty());
544    }
545
546    #[test]
547    fn parse_check_node_joins_text_around_comments() {
548        let doc =
549            Document::parse(r#"<check xmlns="http://stano.com/database">a<!-- note -->b</check>"#)
550                .unwrap();
551        assert_eq!(
552            parse_check_node(doc.root_element()).value.as_deref(),
553            Some("ab")
554        );
555
556        let empty =
557            Document::parse(r#"<check xmlns="http://stano.com/database">  </check>"#).unwrap();
558        assert_eq!(parse_check_node(empty.root_element()).value, None);
559    }
560
561    #[test]
562    fn attr_bool_is_case_insensitive() {
563        let doc = Document::parse(
564            r#"<column xmlns="http://stano.com/database" name="x" type="int" required="TRUE" cluster="No"/>"#,
565        )
566        .unwrap();
567        let node = doc.root_element();
568
569        assert_eq!(attr_bool(node, "required"), Ok(Some(true)));
570        assert_eq!(attr_bool(node, "cluster"), Ok(Some(false)));
571    }
572
573    #[test]
574    fn attr_bool_errors_on_unrecognized_value() {
575        let doc = Document::parse(
576            r#"<column xmlns="http://stano.com/database" name="x" type="int" required="yep"/>"#,
577        )
578        .unwrap();
579        let node = doc.root_element();
580
581        let err = attr_bool(node, "required").unwrap_err();
582        assert!(err.contains("required"));
583        assert!(err.contains("yep"));
584    }
585
586    #[test]
587    fn attr_i32_errors_on_unparseable_value() {
588        let doc = Document::parse(
589            r#"<column xmlns="http://stano.com/database" name="x" type="varchar" length="1O0"/>"#,
590        )
591        .unwrap();
592        let node = doc.root_element();
593
594        let err = attr_i32(node, "length").unwrap_err();
595        assert!(err.contains("length"));
596        assert!(err.contains("1O0"));
597    }
598
599    #[test]
600    fn attr_f64_errors_on_unparseable_value() {
601        let doc = Document::parse(
602            r#"<column xmlns="http://stano.com/database" name="x" type="int" minValue="1o"/>"#,
603        )
604        .unwrap();
605        let node = doc.root_element();
606
607        let err = attr_f64(node, "minValue").unwrap_err();
608        assert!(err.contains("minValue"));
609        assert!(err.contains("1o"));
610    }
611
612    #[test]
613    fn attr_f64_errors_on_infinite_value() {
614        let doc = Document::parse(
615            r#"<column xmlns="http://stano.com/database" name="x" type="int" maxValue="inf"/>"#,
616        )
617        .unwrap();
618        let node = doc.root_element();
619
620        let err = attr_f64(node, "maxValue").unwrap_err();
621        assert!(err.contains("maxValue"));
622        assert!(err.contains("inf"));
623    }
624
625    #[test]
626    fn parse_column_node_honors_uppercase_required_attribute() {
627        let doc = Document::parse(
628            r#"<column xmlns="http://stano.com/database" name="x" type="int" required="TRUE"/>"#,
629        )
630        .unwrap();
631        let node = doc.root_element();
632
633        let column = parse_column_node(node).expect("parse ok");
634
635        assert_eq!(column.required, Some(true));
636    }
637
638    #[test]
639    fn missing_required_attribute_returns_error_instead_of_defaulting_to_empty_string() {
640        let doc =
641            Document::parse(r#"<column xmlns="http://stano.com/database" type="int"/>"#).unwrap();
642        let node = doc.root_element();
643
644        let err = parse_column_node(node).unwrap_err();
645        assert!(err.contains("column"));
646        assert!(err.contains("name"));
647    }
648}