|
3 | 3 | import click |
4 | 4 | from linkml_runtime import SchemaView |
5 | 5 | from linkml_runtime.utils.formatutils import underscore |
| 6 | +from semsql.linkml import path_to_schema |
6 | 7 | from sqlalchemy import text |
7 | 8 |
|
8 | 9 | import semsql.builder.builder as builder |
|
13 | 14 | @click.option("-v", "--verbose", count=True) |
14 | 15 | @click.option("-q", "--quiet") |
15 | 16 | def main(verbose: int, quiet: bool): |
16 | | - """Run the SSSOM CLI.""" |
| 17 | + """Run the SemSQL CLI.""" |
17 | 18 | if verbose >= 2: |
18 | 19 | logging.basicConfig(level=logging.DEBUG) |
19 | 20 | elif verbose == 1: |
@@ -82,41 +83,40 @@ def query(input, query): |
82 | 83 |
|
83 | 84 |
|
84 | 85 | @main.command() |
85 | | -@click.argument("inputs", nargs=-1) |
| 86 | +@click.argument("views", nargs=-1) |
86 | 87 | @click.option("--index/--no-index", default=True, help="Create indexes on each column") |
87 | 88 | @click.option( |
88 | | - "--name", |
89 | | - "-n", |
90 | | - help="Name of class/view to materialize. If blank, will perform for ALL", |
| 89 | + "--schema", |
| 90 | + "-s", |
| 91 | + help="Path o schema (optional)", |
91 | 92 | ) |
92 | | -def view2table(inputs, name: str, index: bool): |
| 93 | +def view2table(views, schema, index: bool): |
93 | 94 | """ |
94 | 95 | Generates a command that turns a view into a table |
95 | 96 |
|
96 | | - See https://github.com/cmungall/semantic-sql/issues/9 |
97 | | -
|
98 | 97 | Example usage: |
99 | | - ``` |
100 | | - semsql view2table src/linkml/rdf.yaml -n rdfs_label_statement | sqlite3 db/pato.db |
101 | | - ``` |
| 98 | +
|
| 99 | + semsql view2table rdfs_label_statement | sqlite3 db/pato.db |
| 100 | +
|
102 | 101 | """ |
103 | | - for input in inputs: |
104 | | - sv = SchemaView(input) |
105 | | - for cn, c in sv.all_classes().items(): |
106 | | - tn = underscore(cn) |
107 | | - if name is None or str(cn) == name or tn == name: |
108 | | - view = get_viewdef(c) |
109 | | - if view is not None: |
110 | | - print(f"DROP VIEW {tn};") |
111 | | - print(f"CREATE TABLE {tn} AS {view};") |
112 | | - if index: |
113 | | - for sn in sv.class_slots(cn): |
114 | | - colname = underscore(sn) |
115 | | - print( |
116 | | - f"CREATE INDEX {tn}_{colname} ON {tn}({colname});" |
117 | | - ) |
118 | | - else: |
119 | | - logging.error(f"No view for {cn}") |
| 102 | + if not schema: |
| 103 | + schema = str(path_to_schema()) |
| 104 | + sv = SchemaView(schema) |
| 105 | + for cn, c in sv.all_classes().items(): |
| 106 | + tn = underscore(cn) |
| 107 | + if not views or str(cn) in views or tn in views: |
| 108 | + view = get_viewdef(c) |
| 109 | + if view is not None: |
| 110 | + print(f"DROP VIEW {tn};") |
| 111 | + print(f"CREATE TABLE {tn} AS {view};") |
| 112 | + if index: |
| 113 | + for sn in sv.class_slots(cn): |
| 114 | + colname = underscore(sn) |
| 115 | + print( |
| 116 | + f"CREATE INDEX {tn}_{colname} ON {tn}({colname});" |
| 117 | + ) |
| 118 | + else: |
| 119 | + logging.error(f"No view for {cn}") |
120 | 120 |
|
121 | 121 |
|
122 | 122 | if __name__ == "__main__": |
|
0 commit comments