bout_runners.database.database_reader¶
Module containing the DatabaseReader class.
Classes
|
Class for reading the schema of the database. |
-
class
bout_runners.database.database_reader.
DatabaseReader
(db_connector: bout_runners.database.database_connector.DatabaseConnector)[source]¶ Class for reading the schema of the database.
Examples
Import dependencies
>>> from pathlib import Path >>> from bout_runners.executor.bout_paths import BoutPaths >>> from bout_runners.parameters.default_parameters import DefaultParameters >>> from bout_runners.parameters.final_parameters import FinalParameters >>> from bout_runners.database.database_connector import DatabaseConnector >>> from bout_runners.database.database_creator import DatabaseCreator >>> from bout_runners.database.database_writer import DatabaseWriter
Create the bout_paths object
>>> project_path = Path().joinpath('path', 'to', 'project') >>> bout_inp_src_dir = Path().joinpath('path', 'to', 'source', 'BOUT.inp') >>> bout_inp_dst_dir = Path().joinpath('path', 'to', 'destination', 'BOUT.inp') >>> bout_paths = BoutPaths(project_path=project_path, ... bout_inp_src_dir=bout_inp_src_dir, ... bout_inp_dst_dir=bout_inp_dst_dir)
Obtain the parameters
>>> default_parameters = DefaultParameters(bout_paths) >>> final_parameters = FinalParameters(default_parameters) >>> final_parameters_dict = final_parameters.get_final_parameters() >>> final_parameters_as_sql_types = \ ... final_parameters.cast_to_sql_type(final_parameters_dict)
Create the database
>>> db_connector = DatabaseConnector('name', project_path) >>> db_creator = DatabaseCreator(db_connector) >>> db_creator.create_all_schema_tables(final_parameters_as_sql_types)
Write to the database
>>> db_writer = DatabaseWriter(db_connector) >>> dummy_split_dict = {'number_of_processors': 1, ... 'number_of_nodes': 2, ... 'processors_per_node': 3} >>> db_writer.create_entry('split', dummy_split_dict)
Read from the database
>>> db_reader = DatabaseReader(db_connector) >>> db_reader.query('SELECT * FROM split') id number_of_processors number_of_nodes processors_per_node 0 1 1 1 1
>>> db_reader.get_latest_row_id() 1
>>> db_reader.get_entry_id('split', dummy_split_dict) 1
>>> db_reader.check_tables_created() True
- Attributes
- db_connectorDatabaseConnector
The database object to read from
Methods
query(query_str)
Make a query to the database
get_latest_row_id()
Return the latest row id
get_entry_id(table_name, entries_dict)
Get the id of a table entry
check_tables_created()
Check if the tables is created in the database
Set the database to use.
- Parameters
- db_connectorDatabaseConnector
The database object to read from
Methods
Check if the tables is created in the database.
get_entry_id
(table_name, entries_dict)Get the id of a table entry.
Return the latest row id.
query
(query_str[, params])Make a query to the database.
-
check_tables_created
() → bool[source]¶ Check if the tables is created in the database.
- Returns
- bool
Whether or not the tables are created
-
get_entry_id
(table_name: str, entries_dict: Mapping[str, Optional[Union[int, str, float]]]) → Optional[int][source]¶ Get the id of a table entry.
- Parameters
- table_namestr
Name of the table to check
- entries_dictdict
Dictionary containing the entries as key value pairs
- Returns
- row_idint or None
The id of the hit If none is found, None is returned
-
get_latest_row_id
() → numpy.int64[source]¶ Return the latest row id.
- Returns
- row_idint
The latest row inserted row id
-
query
(query_str: str, params: Optional[Iterable[Optional[Union[str, float, int, bool]]]] = None, **kwargs) → pandas.core.frame.DataFrame[source]¶ Make a query to the database.
- Parameters
- query_strstr
The query to execute
- paramsarray_like
Array of parameters Used to protect against SQL injection
>>> self.query("SELECT ? FROM table WHERE foo = ?", params=("bar", "baz"))
- kwargsdict
Additional keyword parameters to pd.read_sql_query
- Returns
- tablepd.DataFrame
The result of a query as a DataFrame