Skip to main content

ontocore_query/
schema.rs

1//! SQL virtual table schema metadata for the query workbench.
2
3use ontocore_catalog::OntologyCatalog;
4use serde::Serialize;
5
6#[derive(Debug, Clone, Serialize)]
7pub struct SqlColumnSchema {
8    pub name: String,
9    #[serde(rename = "type")]
10    pub column_type: String,
11}
12
13#[derive(Debug, Clone, Serialize)]
14pub struct SqlTableSchema {
15    pub name: String,
16    pub columns: Vec<SqlColumnSchema>,
17}
18
19/// Static table definitions (columns are stable for v0.13).
20pub fn list_sql_tables() -> Vec<SqlTableSchema> {
21    vec![
22        table(
23            "ontologies",
24            &["id", "path", "format", "base_iri", "parse_status", "content_hash", "modified_time"],
25        ),
26        table(
27            "classes",
28            &[
29                "iri",
30                "short_name",
31                "kind",
32                "ontology_id",
33                "labels",
34                "comments",
35                "deprecated",
36                "obo_id",
37            ],
38        ),
39        table(
40            "object_properties",
41            &[
42                "iri",
43                "short_name",
44                "kind",
45                "ontology_id",
46                "labels",
47                "comments",
48                "deprecated",
49                "obo_id",
50            ],
51        ),
52        table(
53            "data_properties",
54            &[
55                "iri",
56                "short_name",
57                "kind",
58                "ontology_id",
59                "labels",
60                "comments",
61                "deprecated",
62                "obo_id",
63            ],
64        ),
65        table(
66            "annotation_properties",
67            &[
68                "iri",
69                "short_name",
70                "kind",
71                "ontology_id",
72                "labels",
73                "comments",
74                "deprecated",
75                "obo_id",
76            ],
77        ),
78        table(
79            "individuals",
80            &[
81                "iri",
82                "short_name",
83                "kind",
84                "ontology_id",
85                "labels",
86                "comments",
87                "deprecated",
88                "obo_id",
89            ],
90        ),
91        table(
92            "entities",
93            &[
94                "iri",
95                "short_name",
96                "kind",
97                "ontology_id",
98                "labels",
99                "comments",
100                "deprecated",
101                "obo_id",
102            ],
103        ),
104        table(
105            "properties",
106            &[
107                "iri",
108                "short_name",
109                "kind",
110                "ontology_id",
111                "labels",
112                "comments",
113                "deprecated",
114                "obo_id",
115            ],
116        ),
117        table("annotations", &["subject", "predicate", "object", "ontology_id"]),
118        table("axioms", &["id", "ontology_id", "subject", "predicate", "object", "axiom_kind"]),
119        table("restrictions", &["class_iri", "property_iri", "restriction_kind", "filler"]),
120        table("equivalent_class_axioms", &["class_iri", "expression"]),
121        table("disjoint_class_axioms", &["class_iri", "disjoint_with"]),
122        table("domain_axioms", &["property_iri", "domain"]),
123        table("range_axioms", &["property_iri", "range"]),
124        table("namespaces", &["prefix", "iri", "ontology_id"]),
125        table("imports", &["ontology_id", "import_iri"]),
126        table(
127            "diagnostics",
128            &["code", "severity", "message", "file", "line", "column", "entity_iri"],
129        ),
130    ]
131}
132
133fn table(name: &str, columns: &[&str]) -> SqlTableSchema {
134    SqlTableSchema {
135        name: name.to_string(),
136        columns: columns
137            .iter()
138            .map(|c| SqlColumnSchema { name: (*c).to_string(), column_type: "string".to_string() })
139            .collect(),
140    }
141}
142
143/// Returns schema; `catalog` reserved for future dynamic columns.
144pub fn list_sql_schema(_catalog: &OntologyCatalog) -> Vec<SqlTableSchema> {
145    list_sql_tables()
146}
147
148#[cfg(test)]
149mod tests {
150    use super::*;
151
152    #[test]
153    fn includes_axiom_tables() {
154        let names: Vec<_> = list_sql_tables().into_iter().map(|t| t.name).collect();
155        for table in [
156            "restrictions",
157            "equivalent_class_axioms",
158            "disjoint_class_axioms",
159            "domain_axioms",
160            "range_axioms",
161        ] {
162            assert!(names.contains(&table.to_string()), "missing table {table}");
163        }
164    }
165
166    #[test]
167    fn restrictions_table_has_expected_columns() {
168        let table = list_sql_tables()
169            .into_iter()
170            .find(|t| t.name == "restrictions")
171            .expect("restrictions table");
172        let cols: Vec<_> = table.columns.iter().map(|c| c.name.as_str()).collect();
173        assert_eq!(cols, vec!["class_iri", "property_iri", "restriction_kind", "filler"]);
174    }
175}