caller

This module uses sqlalchemy and the user’s database credentials to make calls directly to the database.

This module requires two environment variables to be set: - USER_DB_USERNAME: The user’s username for the database. - USER_DB_PASSWORD: The user’s password for the database.

Exceptions

DatabaseCallerException

Common base class for all non-exit exceptions.

Classes

DatabaseCaller

Makes calls to the database using the user's credentials.

Module Contents

exception caller.DatabaseCallerException

Bases: Exception

Common base class for all non-exit exceptions.

class caller.DatabaseCaller(username: str | None = None, password: str | None = None, db_url: str | None = None, db_read_only_url: str | None = None, db_name: str | None = 'platform')

Makes calls to the database using the user’s credentials.

Parameters:
  • username (Optional[str]) –

  • password (Optional[str]) –

  • db_url (Optional[str]) –

  • db_read_only_url (Optional[str]) –

  • db_name (Optional[str]) –

execute_sql_statement(statement: str | sqlalchemy.TextClause) sqlalchemy.Result

Executes a SQL statement.

Parameters:

statement (str | sqlalchemy.TextClause) – The SQL statement to execute. Can be either a string or sqlalchemy TextClause.

Returns:

Sqlalchemy result object. See sqlalchemy documentation on

how to handle this further. result.all() will return all results if applicable. result.rowcount will return affected row count if applicable.

Return type:

Result

read_sql_to_df(statement: str | sqlalchemy.TextClause, data: List[dict] | dict | None = None, **kwargs) pandas.DataFrame

Reads a SQL statement and returns the data as a dataframe.

Parameters:
  • statement (str | sqlalchemy.TextClause) – The SQL statement to execute. Can be either a string or sqlalchemy TextClause. The format for data placeholders in the statement should be :key.

  • data (List[dict] | dict | None) – Optional dict or list of dicts to pass to the statement. This will replace the :key placeholders in the statement with the value. If a list of dicts is passed, the statement will be executed for each dict in the list.

  • kwargs – A dictionary of additional arguments to pass to the underlying pandas.read_sql function

Returns:

The returned data from the SQL statement in a dataframe.

Return type:

pandas.DataFrame

write_df_to_db_table(df, table_name: str, schema_name: str) int

Inserts a dataframe into an existing table.

Ignores index.

Parameters:
  • df – The dataframe to insert.

  • table_name (str) – The name of the table to insert into.

  • schema_name (str) – The name of the schema to insert into.

Returns:

The number of rows inserted.

Return type:

int

_name
_password
_read_url
_username
_write_url
read_engine
write_engine