@@ -88,27 +88,33 @@ def list_backends(self):
8888 print ("DuckDB : In-process SQL backend optimized for fast analytics on large datasets.\n " )
8989 print ()
9090
91- def schema (self , filename ):
91+ def schema (self , filename = None ):
9292 """
93- Loads a relational database schema into DSI from a specified `filename`
93+ Either loads a relational database schema into DSI with a specified `filename` OR returns this database's structural schema.
9494
95- `filename` : str
96- Path to a JSON file describing the structure of a relational database.
95+ `filename` : str, optional
96+ Path to a JSON file describing the relationships of the tables in a database.
9797 The schema should follow the format described in :ref:`user_schema_example_label`
98-
99- **Must be called before reading in any data files associated with the schema**
98+
99+ `return` : If filename = None, returns the structural schema of this database - table/col names and their units.
100+ **If loading a relational schema, this function must be called before reading in any associated data files**
100101 """
101- if not os .path .exists (filename ):
102- sys .exit ("schema() ERROR: Input schema file must have a valid filepath. Please check again." )
102+ if filename :
103+ if not os .path .exists (filename ):
104+ sys .exit ("schema() ERROR: Input schema file must have a valid filepath. Please check again." )
103105
104- fnull = open (os .devnull , 'w' )
105- with redirect_stdout (fnull ):
106- self .t .load_module ('plugin' , 'Schema' , 'reader' , filename = filename )
107- self .schema_read = True
108- pk_tables = set (t [0 ] for t in self .t .active_metadata ["dsi_relations" ]["primary_key" ])
109- fk_tables = set (t [0 ] for t in self .t .active_metadata ["dsi_relations" ]["foreign_key" ] if t [0 ] != None )
110- self .schema_tables = pk_tables .union (fk_tables )
111- print (f"Successfully loaded the schema file: { filename } " )
106+ fnull = open (os .devnull , 'w' )
107+ with redirect_stdout (fnull ):
108+ self .t .load_module ('plugin' , 'Schema' , 'reader' , filename = filename )
109+ self .schema_read = True
110+ pk_tables = set (t [0 ] for t in self .t .active_metadata ["dsi_relations" ]["primary_key" ])
111+ fk_tables = set (t [0 ] for t in self .t .active_metadata ["dsi_relations" ]["foreign_key" ] if t [0 ] != None )
112+ self .schema_tables = pk_tables .union (fk_tables )
113+ print (f"Successfully loaded the schema file: { filename } " )
114+ else :
115+ fnull = open (os .devnull , 'w' )
116+ with redirect_stdout (fnull ):
117+ return self .t .get_schema ()
112118
113119 def list_readers (self ):
114120 """
0 commit comments