11import logging
22
33import click
4- import logging
5- import semsql .builder .builder as builder
64from linkml_runtime import SchemaView
75from linkml_runtime .utils .formatutils import underscore
8- from semsql .sqlutils .viewgen import get_viewdef
96from sqlalchemy import text
107
8+ import semsql .builder .builder as builder
9+ from semsql .sqlutils .viewgen import get_viewdef
10+
1111
1212@click .group ()
1313@click .option ("-v" , "--verbose" , count = True )
@@ -25,12 +25,13 @@ def main(verbose: int, quiet: bool):
2525
2626
2727@main .command ()
28- @click .argument ('path' )
29- @click .option ('--docker/--no-docker' ,
30- default = False ,
31- show_default = True ,
32- help = "Uses ODK docker image"
33- )
28+ @click .argument ("path" )
29+ @click .option (
30+ "--docker/--no-docker" ,
31+ default = False ,
32+ show_default = True ,
33+ help = "Uses ODK docker image" ,
34+ )
3435def make (path , docker ):
3536 """
3637 Makes a specified target, such as a db file
@@ -49,8 +50,8 @@ def make(path, docker):
4950
5051
5152@main .command ()
52- @click .option ('-o' , ' --output' )
53- @click .argument (' ontology' )
53+ @click .option ("-o" , " --output" )
54+ @click .argument (" ontology" )
5455def download (ontology , output ):
5556 """
5657 Download a read-made SQLite db for an OBO ontology
@@ -63,8 +64,8 @@ def download(ontology, output):
6364
6465
6566@main .command ()
66- @click .option ('-i' , ' --input' )
67- @click .argument (' query' )
67+ @click .option ("-i" , " --input" )
68+ @click .argument (" query" )
6869def query (input , query ):
6970 """
7071 Performs a SQL query on an OWL file
@@ -81,9 +82,13 @@ def query(input, query):
8182
8283
8384@main .command ()
84- @click .argument ('inputs' , nargs = - 1 )
85- @click .option ('--index/--no-index' , default = True , help = 'Create indexes on each column' )
86- @click .option ('--name' , '-n' , help = 'Name of class/view to materialize. If blank, will perform for ALL' )
85+ @click .argument ("inputs" , nargs = - 1 )
86+ @click .option ("--index/--no-index" , default = True , help = "Create indexes on each column" )
87+ @click .option (
88+ "--name" ,
89+ "-n" ,
90+ help = "Name of class/view to materialize. If blank, will perform for ALL" ,
91+ )
8792def view2table (inputs , name : str , index : bool ):
8893 """
8994 Generates a command that turns a view into a table
@@ -96,24 +101,23 @@ def view2table(inputs, name: str, index: bool):
96101 ```
97102 """
98103 for input in inputs :
99- with open (input , 'r' ) as stream :
100- sv = SchemaView (input )
101- schema = sv .schema
102- for cn , c in sv .all_classes ().items ():
103- tn = underscore (cn )
104- if name is None or str (cn ) == name or tn == name :
105- view = get_viewdef (c )
106- if view is not None :
107- print (f'DROP VIEW { tn } ;' )
108- print (f'CREATE TABLE { tn } AS { view } ;' )
109- if index :
110- for sn in sv .class_slots (cn ):
111- colname = underscore (sn )
112- print (f'CREATE INDEX { tn } _{ colname } ON { tn } ({ colname } );' )
113- else :
114- logging .error (f'No view for { cn } ' )
115-
116-
117-
118- if __name__ == '__main__' :
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 } " )
120+
121+
122+ if __name__ == "__main__" :
119123 main ()
0 commit comments