Database I/O subpackage¶
All functions of datesy taking care of database I/O are listed here.
The functionality is the same for all supported database types.
Simply exchange _db_helper.Database
with the desired database.
Database¶
-
class
datesy.database_IO._db_helper.
Database
(host, port, user, password, database, auto_creation=False)¶ Representing a database as an object
On initialization the connection to the database is established. For clean working please call
close()
at end of db usage.Parameters: - host (str) – url or ip of host
- port (int) – port_no
- user (str) – user_name
- password (str) – password for this user
- database (str) – the database to connect to
- auto_creation (bool, optional) – if all tables shall be initiated as variables of object
- kwargs – specific information to database, see details to each database
-
close
()¶ Close connection to database
-
name
¶ Name of the database
Returns: Return type: str
-
table
(table_name)¶ Return a database_table as an object
Parameters: table_name (str) – the desired table Returns: class Table as representation of table Return type: Table
-
tables
¶ Get the available tables of database
Returns: representing all tables of database Return type: list
-
update_table_data
()¶ Update the data concerning the list of available tables
Returns: available tables at database Return type: list
Table¶
-
class
datesy.database_IO._db_helper.
Table
(table_name, database)¶ Create a representation of a database table
Parameters: - table_name (str) –
- database (Database) –
-
__getitem__
(key)¶ Get row of primary key
works like
value = database.table[key]
Parameters: key (any) – matching value in primary column Returns: tuple items representing every matched row in database Return type: Row
-
__setitem__
(primary_key, row)¶ Set/update a single row for primary key
works like
database.table[key] = row
Parameters: - primary_key (any, None) – the value of the primary column. If None -> new row inserted
- row (list, dict) – the row data in either correct order or in a dict with column_name
-
__delitem__
(key)¶ Delete single row for given primary key
works like
del database.table[key]
Parameters: key (any) – matching value in primary column
-
delete_where
(*args, **kwargs)¶ Delete rows matching the where conditions
Parameters: - args (conditions) –
- kwargs (conditions) –
-
execute_raw_sql
(sql_query)¶ Execute raw sql statements
Parameters: sql_query (str) – sql query Returns: data from database Return type: list
-
get_where
(*args, **kwargs)¶ Get rows where value matches the defined column:
columns=key
Parameters: **kwargs – column = key values Returns: tuple items representing every matched row in database Return type: list(Row)
-
insert
(row: (<class 'list'>, <class 'dict'>), primary_key=None)¶ Insert new row
Parameters: - row (list, dict) – row_data
- primary_key (any, optional) – primary_key optional for tables with primary_key
-
primary
¶ Get the primary key of this table
Returns: the primary column as string if one exists Return type: str, None
-
run_query
(debug=False)¶ Run the currently composed query :param debug: if the query shall be printed to command_line :type debug: bool, optional
Returns: data from database Return type: list
-
schema
¶ Get schema of table
Returns: dictionary containing the column as key and schema_info as dictionary for every column Return type: OrderedDict
-
update_primary_data
()¶ Update the primary key of the table
Returns: the primary column as string if one exists Return type: str, None
-
update_schema_data
()¶ Update the schema of the table
Returns: dictionary containing the column as key and schema_info as dictionary for every column Return type: OrderedDict
-
update_where
(values: (<class 'list'>, <class 'dict'>), *args, primary_key=None, limit_rows: int = False, **kwargs)¶ Update all rows based on given conditions
Parameters: - values (list, dict) – new values to set. either as a full row in a list or specified columns with dictionary
- args – conditions
- primary_key (any, optional) – the value of the primary column (if table has primary_key)
- limit_rows (int) – number of rows to affect
- kwargs – conditions
Row¶
-
class
datesy.database_IO._db_helper.
Row
(table, data)¶ Representation of a database row entry
Parameters: - table (Table) – table belonging to
- data (list, tuple, dict) – data to represent
-
__getitem__
(column)¶ Get column_value of row
works like
value = database.table.row[column]
Parameters: column (int, str) – position in table or column_name Returns: Return type: Item
-
__setitem__
(column, value)¶ Set new value to column
works like
database.table.row[column] = value
Parameters: - column (int, str) – position in table or column_name
- value (any) – new value
-
__delitem__
(column)¶ Delete/reset to default a column
works like
del database.table.row[column]
Parameters: column (int, str) – position in table or column_name
-
sync
(*missing_columns)¶ Update row from database to local
Parameters: missing_columns (str, optional) – if rows shall be left out when updating (e.g. if known that a timestamp has changed and it shall be fetched)
Item¶
-
class
datesy.database_IO._db_helper.
Item
(row, column, table, value=None)¶ Representation of a database entry
Parameters: -
__set__
(instance, value)¶ Set new value
Parameters: - instance –
- value (any) – new value
-
__delete__
(instance)¶ Delete/reset to default this value
-
column
¶ Column this item belonging to
Returns: column Return type: str
-
sync
()¶ Update entry from database to local
-
value
¶ Value of this item
Returns: value Return type: any
-