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        // The XSD declares <keys> as a sequence: primary, then unique*, then index*.
198        if c.has_tag_name((NS, "primary")) {
199            if primary.is_some() {
200                return Err("<keys> may contain only one <primary>".to_string());
201            }
202            if !uniques.is_empty() || !indexes.is_empty() {
203                return Err("<primary> must come before <unique> and <index> in <keys>".to_string());
204            }
205            primary = Some(parse_key_columns_node(c)?);
206        } else if c.has_tag_name((NS, "unique")) {
207            if !indexes.is_empty() {
208                return Err("<unique> must come before <index> in <keys>".to_string());
209            }
210            uniques.push(parse_key_columns_node(c)?);
211        } else if c.has_tag_name((NS, "index")) {
212            indexes.push(parse_index_node(c)?);
213        }
214    }
215
216    Ok(KeysXml {
217        primary,
218        uniques,
219        indexes,
220    })
221}
222
223fn parse_key_columns_node(node: Node) -> Result<KeyColumnsXml, String> {
224    let mut columns = Vec::new();
225    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
226        columns.push(KeyColumnXml {
227            name: attr_string_required(c, "name")?,
228        });
229    }
230    let cluster = attr_bool(node, "cluster")?;
231    let where_clause = attr_string(node, "where");
232    Ok(KeyColumnsXml {
233        columns,
234        cluster,
235        where_clause,
236    })
237}
238
239fn parse_index_node(node: Node) -> Result<IndexXml, String> {
240    let mut columns = Vec::new();
241    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
242        columns.push(KeyColumnXml {
243            name: attr_string_required(c, "name")?,
244        });
245    }
246    Ok(IndexXml {
247        columns,
248        include: attr_string(node, "include"),
249        compress: attr_bool(node, "compress")?,
250        unique: attr_bool(node, "unique")?,
251        where_clause: attr_string(node, "where"),
252    })
253}
254
255fn parse_relations_node(node: Node) -> Result<RelationsXml, String> {
256    let mut rels = Vec::new();
257    let mut composite_rels = Vec::new();
258    for r in node.children().filter(|n| n.is_element()) {
259        if r.has_tag_name((NS, "relation")) {
260            rels.push(RelationXml {
261                src: attr_string_required(r, "src")?,
262                table: attr_string_required(r, "table")?,
263                column: attr_string_required(r, "column")?,
264                r#type: attr_string_required(r, "type")?,
265                disable_usage_checking: attr_bool(r, "disableUsageChecking")?,
266            });
267        } else if r.has_tag_name((NS, "compositeRelation")) {
268            composite_rels.push(parse_composite_relation_node(r)?);
269        }
270    }
271    Ok(RelationsXml {
272        relation: rels,
273        composite_relation: composite_rels,
274    })
275}
276
277fn parse_composite_relation_node(node: Node) -> Result<CompositeRelationXml, String> {
278    let mut columns = Vec::new();
279    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
280        columns.push(CompositeRelationColumnXml {
281            src: attr_string_required(c, "src")?,
282            name: attr_string_required(c, "name")?,
283        });
284    }
285    Ok(CompositeRelationXml {
286        table: attr_string_required(node, "table")?,
287        r#type: attr_string_required(node, "type")?,
288        disable_usage_checking: attr_bool(node, "disableUsageChecking")?,
289        column: columns,
290    })
291}
292
293fn parse_view_node(node: Node) -> Result<ViewXml, String> {
294    Ok(ViewXml {
295        name: attr_string_required(node, "name")?,
296        database_type: attr_string(node, "databaseType"),
297        sql: collect_text(node),
298    })
299}
300
301fn parse_function_node(node: Node) -> Result<FunctionXml, String> {
302    let name = attr_string_required(node, "name")?;
303    let mut sql = Vec::new();
304    for s in node.children().filter(|n| n.has_tag_name((NS, "sql"))) {
305        sql.push(VendorSqlXml {
306            database_type: attr_string_required(s, "databaseType")?,
307            sql: collect_text(s),
308        });
309    }
310    Ok(FunctionXml { name, sql })
311}
312
313fn parse_procedure_node(node: Node) -> Result<ProcedureXml, String> {
314    let name = attr_string_required(node, "name")?;
315    let mut sql = Vec::new();
316    for s in node.children().filter(|n| n.has_tag_name((NS, "sql"))) {
317        sql.push(VendorSqlXml {
318            database_type: attr_string_required(s, "databaseType")?,
319            sql: collect_text(s),
320        });
321    }
322    Ok(ProcedureXml { name, sql })
323}
324
325fn parse_other_sql_node(node: Node) -> Result<OtherSqlXml, String> {
326    let database_type = attr_string_required(node, "databaseType")?;
327    let order_text = attr_string_required(node, "order")?;
328    let order = match order_text.to_ascii_lowercase().as_str() {
329        "top" => OtherSqlOrderXml::Top,
330        "bottom" => OtherSqlOrderXml::Bottom,
331        other => {
332            return Err(format!(
333                "<otherSql> element has an unrecognized order '{}' (expected top or bottom)",
334                other
335            ));
336        }
337    };
338    Ok(OtherSqlXml {
339        database_type,
340        order,
341        sql: collect_text(node),
342    })
343}
344
345fn parse_enum_node(node: Node) -> Result<EnumXml, String> {
346    let name = attr_string_required(node, "name")?;
347    let mut value = Vec::new();
348    for v in node.children().filter(|n| n.has_tag_name((NS, "value"))) {
349        value.push(EnumValueXml {
350            name: attr_string_required(v, "name")?,
351            code: attr_string(v, "code"),
352        });
353    }
354    Ok(EnumXml { name, value })
355}
356
357fn parse_triggers_node(node: Node) -> Result<TriggersXml, String> {
358    let mut update = Vec::new();
359    let mut delete = Vec::new();
360    for c in node.children().filter(|n| n.is_element()) {
361        if c.has_tag_name((NS, "update")) {
362            update.push(TriggerXml {
363                database_type: attr_string_required(c, "databaseType")?,
364                sql: collect_text(c),
365            });
366        } else if c.has_tag_name((NS, "delete")) {
367            delete.push(TriggerXml {
368                database_type: attr_string_required(c, "databaseType")?,
369                sql: collect_text(c),
370            });
371        }
372    }
373    Ok(TriggersXml { update, delete })
374}
375
376fn parse_constraints_node(node: Node) -> Result<ConstraintsXml, String> {
377    let mut constraint = Vec::new();
378    for c in node
379        .children()
380        .filter(|n| n.has_tag_name((NS, "constraint")))
381    {
382        constraint.push(ConstraintXml {
383            name: attr_string_required(c, "name")?,
384            database_type: attr_string(c, "databaseType"),
385            sql: collect_text(c),
386        });
387    }
388    Ok(ConstraintsXml { constraint })
389}
390
391fn parse_aggregations_node(node: Node) -> Result<AggregationsXml, String> {
392    let mut aggregate = Vec::new();
393    for a in node
394        .children()
395        .filter(|n| n.has_tag_name((NS, "aggregate")))
396    {
397        aggregate.push(parse_aggregate_node(a)?);
398    }
399    Ok(AggregationsXml { aggregate })
400}
401
402fn parse_aggregate_node(node: Node) -> Result<AggregateXml, String> {
403    let mut sum = Vec::new();
404    let mut count = Vec::new();
405    let mut group: Option<GroupXml> = None;
406
407    for c in node.children().filter(|n| n.is_element()) {
408        if c.has_tag_name((NS, "sum")) {
409            sum.push(SumXml {
410                source_column: attr_string_required(c, "sourceColumn")?,
411                destination_column: attr_string_required(c, "destinationColumn")?,
412            });
413        } else if c.has_tag_name((NS, "count")) {
414            count.push(CountXml {
415                destination_column: attr_string_required(c, "destinationColumn")?,
416            });
417        } else if c.has_tag_name((NS, "group")) {
418            group = Some(parse_group_node(c)?);
419        }
420    }
421
422    Ok(AggregateXml {
423        sum,
424        count,
425        group: group.unwrap_or_else(|| GroupXml { column: Vec::new() }),
426        destination_table: attr_string_required(node, "destinationTable")?,
427        date_column: attr_string_required(node, "dateColumn")?,
428        timestamp_column: attr_string_required(node, "timestampColumn")?,
429        frequency: attr_string_required(node, "frequency")?,
430        criteria: attr_string(node, "criteria"),
431    })
432}
433
434fn parse_group_node(node: Node) -> Result<GroupXml, String> {
435    let mut column = Vec::new();
436    for c in node.children().filter(|n| n.has_tag_name((NS, "column"))) {
437        column.push(GroupColumnXml {
438            source: attr_string_required(c, "source")?,
439            destination: attr_string_required(c, "destination")?,
440            source_derived_from: attr_string(c, "sourceDerivedFrom"),
441        });
442    }
443    Ok(GroupXml { column })
444}
445
446fn parse_initial_data_node(node: Node) -> InitialDataXml {
447    let mut sql = Vec::new();
448    for s in node.children().filter(|n| n.has_tag_name((NS, "sql"))) {
449        sql.push(InitialSqlXml {
450            database_type: attr_string(s, "databaseType"),
451            sql: collect_text(s),
452        });
453    }
454    InitialDataXml { sql }
455}
456
457// -------- Utils --------
458fn attr_string(node: Node, name: &str) -> Option<String> {
459    node.attribute(name).map(|s| s.to_string())
460}
461
462/// Reads a required attribute. Returns an error (naming the element and attribute)
463/// rather than silently defaulting to `""` when it's absent - a missing `name`/`type`/
464/// `table`/`column`/etc. attribute is a malformed schema definition, and nothing
465/// downstream validates against an empty name, so a silent default would otherwise let
466/// it through as a real (if oddly-named) table/column/relation.
467fn attr_string_required(node: Node, name: &str) -> Result<String, String> {
468    node.attribute(name).map(|s| s.to_string()).ok_or_else(|| {
469        format!(
470            "<{}> element is missing required attribute '{}'",
471            node.tag_name().name(),
472            name
473        )
474    })
475}
476
477/// Reads an optional boolean attribute. Unlike a missing attribute (which is a legitimate
478/// "not specified"), a *present but unparseable* value (`required="yep"`) is a malformed
479/// schema definition - silently falling back to `None`/`false` would hide a typo instead of
480/// reporting it, so this errors instead.
481fn attr_bool(node: Node, name: &str) -> Result<Option<bool>, String> {
482    match node.attribute(name) {
483        None => Ok(None),
484        Some(v) => match v.to_ascii_lowercase().as_str() {
485            "true" | "1" | "yes" | "on" => Ok(Some(true)),
486            "false" | "0" | "no" | "off" => Ok(Some(false)),
487            _ => Err(format!(
488                "<{}> attribute '{}' has invalid boolean value '{}' (expected true/false, 1/0, yes/no, or on/off)",
489                node.tag_name().name(),
490                name,
491                v
492            )),
493        },
494    }
495}
496
497/// Reads an optional integer attribute, erroring (rather than silently defaulting to `None`)
498/// when the attribute is present but not a valid `i32` - a typo like `length="1O0"` or an
499/// overflowing value like `length="99999999999"` should be reported, not turned into 0.
500fn attr_i32(node: Node, name: &str) -> Result<Option<i32>, String> {
501    match node.attribute(name) {
502        None => Ok(None),
503        Some(v) => v.parse::<i32>().map(Some).map_err(|_| {
504            format!(
505                "<{}> attribute '{}' has invalid integer value '{}'",
506                node.tag_name().name(),
507                name,
508                v
509            )
510        }),
511    }
512}
513
514/// Reads an optional numeric attribute, erroring (rather than silently defaulting to `None`)
515/// when the attribute is present but not a finite `f64` - this covers unparseable typos like
516/// `minValue="1o"` as well as `inf`/`nan`, which `f64::parse` otherwise accepts silently.
517fn attr_f64(node: Node, name: &str) -> Result<Option<f64>, String> {
518    match node.attribute(name) {
519        None => Ok(None),
520        Some(v) => match v.parse::<f64>() {
521            Ok(n) if n.is_finite() => Ok(Some(n)),
522            _ => Err(format!(
523                "<{}> attribute '{}' has invalid numeric value '{}'",
524                node.tag_name().name(),
525                name,
526                v
527            )),
528        },
529    }
530}
531
532fn collect_text(node: Node) -> String {
533    let mut out = String::new();
534    for n in node.children() {
535        if n.is_text() {
536            out.push_str(n.text().unwrap_or(""));
537        }
538    }
539    out.trim().to_string()
540}
541
542#[cfg(test)]
543mod tests {
544    use super::*;
545    use std::fs;
546
547    #[test]
548    fn parse_with_roxmltree_sample() {
549        let xml = fs::read_to_string("tests/resources/schema-parser-test-schema.xml")
550            .expect("resource present");
551        let db = parse_database_roxml(&xml).expect("parsed");
552        assert!(!db.tables.is_empty());
553        assert!(db.schemas.is_empty() || !db.schemas[0].tables.is_empty());
554    }
555
556    #[test]
557    fn parse_check_node_joins_text_around_comments() {
558        let doc =
559            Document::parse(r#"<check xmlns="http://stano.com/database">a<!-- note -->b</check>"#)
560                .unwrap();
561        assert_eq!(
562            parse_check_node(doc.root_element()).value.as_deref(),
563            Some("ab")
564        );
565
566        let empty =
567            Document::parse(r#"<check xmlns="http://stano.com/database">  </check>"#).unwrap();
568        assert_eq!(parse_check_node(empty.root_element()).value, None);
569    }
570
571    #[test]
572    fn attr_bool_is_case_insensitive() {
573        let doc = Document::parse(
574            r#"<column xmlns="http://stano.com/database" name="x" type="int" required="TRUE" cluster="No"/>"#,
575        )
576        .unwrap();
577        let node = doc.root_element();
578
579        assert_eq!(attr_bool(node, "required"), Ok(Some(true)));
580        assert_eq!(attr_bool(node, "cluster"), Ok(Some(false)));
581    }
582
583    #[test]
584    fn attr_bool_errors_on_unrecognized_value() {
585        let doc = Document::parse(
586            r#"<column xmlns="http://stano.com/database" name="x" type="int" required="yep"/>"#,
587        )
588        .unwrap();
589        let node = doc.root_element();
590
591        let err = attr_bool(node, "required").unwrap_err();
592        assert!(err.contains("required"));
593        assert!(err.contains("yep"));
594    }
595
596    #[test]
597    fn attr_i32_errors_on_unparseable_value() {
598        let doc = Document::parse(
599            r#"<column xmlns="http://stano.com/database" name="x" type="varchar" length="1O0"/>"#,
600        )
601        .unwrap();
602        let node = doc.root_element();
603
604        let err = attr_i32(node, "length").unwrap_err();
605        assert!(err.contains("length"));
606        assert!(err.contains("1O0"));
607    }
608
609    #[test]
610    fn attr_f64_errors_on_unparseable_value() {
611        let doc = Document::parse(
612            r#"<column xmlns="http://stano.com/database" name="x" type="int" minValue="1o"/>"#,
613        )
614        .unwrap();
615        let node = doc.root_element();
616
617        let err = attr_f64(node, "minValue").unwrap_err();
618        assert!(err.contains("minValue"));
619        assert!(err.contains("1o"));
620    }
621
622    #[test]
623    fn attr_f64_errors_on_infinite_value() {
624        let doc = Document::parse(
625            r#"<column xmlns="http://stano.com/database" name="x" type="int" maxValue="inf"/>"#,
626        )
627        .unwrap();
628        let node = doc.root_element();
629
630        let err = attr_f64(node, "maxValue").unwrap_err();
631        assert!(err.contains("maxValue"));
632        assert!(err.contains("inf"));
633    }
634
635    #[test]
636    fn parse_column_node_honors_uppercase_required_attribute() {
637        let doc = Document::parse(
638            r#"<column xmlns="http://stano.com/database" name="x" type="int" required="TRUE"/>"#,
639        )
640        .unwrap();
641        let node = doc.root_element();
642
643        let column = parse_column_node(node).expect("parse ok");
644
645        assert_eq!(column.required, Some(true));
646    }
647
648    #[test]
649    fn missing_required_attribute_returns_error_instead_of_defaulting_to_empty_string() {
650        let doc =
651            Document::parse(r#"<column xmlns="http://stano.com/database" type="int"/>"#).unwrap();
652        let node = doc.root_element();
653
654        let err = parse_column_node(node).unwrap_err();
655        assert!(err.contains("column"));
656        assert!(err.contains("name"));
657    }
658}