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