hypernets_processor.data_io.database_util module
DatabaseUtil class
- class hypernets_processor.data_io.database_util.DatabaseUtil
Bases:
object
Class to provide utilities for generating databases
- static apply_schema_dict(db, schema_dict)
Initialise database with defined schema
- Parameters:
db (dataset.Dataset) – empty database to initialise
schema_dict (dict) – dictionary defining database schema
Each entry in the dictionary is the name of a table to be defined, the value of each entry is a dictionary that defines the table, it has the following entries:
“columns” - defines the table columns
each entry in the dictionary is the name of a column to be defined, the value of each entry is a dictionary that defines the column, this has entries:
“type” - defines the column type, value should be python type
“foreign_key” (optional) - defines if column is foreign key to reference table. Value is dictionary structured as: {“reference_table”: “<table_name>”, “reference_column”: “column_name”}. NB: This is not supported for defining sqlite databases. If this is required, please define as SQL command (option b)
other entries may be kwargs supported by dataset.table.Table.create_column
“primary_key” - defines the tables primary key, value is the column name.
other entries may be kwargs supported by dataset.database.Database.create_table
- static create_db(url)
Create database at defined url (sqlite or postgresql)
- Parameters:
url (str) – url of database to create
- Returns:
New database
- Return type:
dataset.Database
- static delete_db(url)
- static get_db_type(python_type)
Returns sqlalchemy type equivalent to given python type
- Parameters:
python_type (type) – python type
- Returns:
equivalent sqlalchemy type
- Return type:
sqlalchemy.sql.visitors.VisitableType
- static update_to_foreign_key(db, table, column, reference_table, reference_column)
Update database to set column as foreign key to reference table and column
- Parameters:
db (dataset.Dataset) – empty database to initialise
table (str) – name of table with column to set as foreign key
column (str) – name of column to set as foreign key
reference_table (str) – name of table with column to set foreign key to
reference_column (str) – name of column to set foreign key to
- hypernets_processor.data_io.database_util.create_template_db(url, schema_dict=None, schema_sql=None)
Create database at defined url and initialise database with defined schema
- Parameters:
url (str) – url of database to create
schema_dict (dict) – dictionary defining database schema
Each entry in the dictionary is the name of a table to be defined, the value of each entry is a dictionary that defines the table, it has the following entries:
“columns” - defines the table columns
each entry in the dictionary is the name of a column to be defined, the value of each entry is a dictionary that defines the column, this has entries:
“type” - defines the column type, value should be python type
“foreign_key” (optional) - defines if column is foreign key to reference table. Value is dictionary structured as: {“reference_table”: “<table_name>”, “reference_column”: “column_name”}. NB: This is not supported for defining sqlite databases. If this is required, please define as SQL command (option b)
other entries may be kwargs supported by dataset.table.Table.create_column
“primary_key” - defines the tables primary key, value is the column name.
other entries may be kwargs supported by dataset.database.Database.create_table
- Parameters:
schema_sql (str) – sql command to create database schema
Databases may be defined with SQL commands as strings
- Returns:
Initialised database
- Return type:
dataset.Database