disbi.db_utils module

Helper functions for performing operations circumventing the ORM layer.

disbi.db_utils.db_table_exists(table_name)[source]

Check whether a table with a specific name exists in the DB.

Parameters:table_name (str) – The name of the table to check for.
Returns:bool – True if the table exists, else False.
disbi.db_utils.dictfetchall(cursor)[source]

Return all rows from a cursor as a dict.

disbi.db_utils.exec_query(sql, parameters=None)[source]

Execute a plain SQL query.

Use parameterized query if parameters are given.

Parameters:sql (str) – The SQL query.
Keyword Arguments:
 parameters (iterable) – An iterable of parameters, that will be autoescaped.
disbi.db_utils.from_db(sql, parameters=None, fetch_as='ordereddict')[source]

Fetch values from the DB, given a SQL query.

Parameters:

sql (str) – The SQL statement.

Keyword Arguments:
 
  • parameters – Parameters for a parametrized query. If given sql must contain the appropriate palceholders. Defaults to None.
  • fetch_as (str) – The data type as which the values should be fetched. Choices are ‘ordereddict’, ‘dict’, ‘namedtuple’ and ‘tuple’.
Raises:

ValueError – If a unrecognized value for fetch_as is given.

disbi.db_utils.get_columnnames(table_name)[source]

Get the column names of a DB table.

Parameters:table_name (str) – The name of the table.
Returns:tuple – The column names.
disbi.db_utils.get_field_query_name(model, field)[source]

Format the DB column name of a field with the DB table of its model.

disbi.db_utils.get_fk_query_name(model, related_model)[source]

Format the DB column name of a foreign key field of a model with the DB table of the model. Finds the foreign key relating to related model automatically, but assumes that there is only one related field.

Parameters:
  • model (Model) – The model for which the foreign key field is searched.
  • related_model (Model) – A model related to model.
Returns:

str – The formated foreign key column name.

disbi.db_utils.get_m2m_field(intermediary_model, related_model)[source]

Get the field of an intermediary model, that constitutes the relation to related_model.

disbi.db_utils.get_pk_query_name(model)[source]

Format the primary key column of a model with its DB table.

disbi.db_utils.namedtuplefetchall(cursor)[source]

Return all rows from a cursor as a namedtuple.

disbi.db_utils.ordereddictfetchall(cursor)[source]

Return all rows from a cursor as an OrdredeDict.