actio_python_utils.database_functions.DBConnection

class actio_python_utils.database_functions.DBConnection(service=None, db_args=None, log_name='sql_debug', commit=False, cursor_factory=<class 'actio_python_utils.database_functions.LoggingCursor'>)[source]

Bases: object

Creates a psycopg2 database connection with the specified parameters and acts as a context manager.

Parameters:
  • service (Optional[str], default: None) – The PostgreSQL service to connect to

  • db_args (Optional[Mapping[str, Any]], default: None) – A mapping of database connection arguments

  • log_name (str, default: 'sql_debug') – The name to give the logger

  • commit (bool, default: False) – Commit the transaction upon closing the connection if no errors were encountered

  • cursor_factory (cursor, default: <class 'actio_python_utils.database_functions.LoggingCursor'>) – The class of cursor to use for the connection by default (used for service/$DB_CONNECTION_STRING)

Raises:

ValueError – If service and db_args are specified

__init__(service=None, db_args=None, log_name='sql_debug', commit=False, cursor_factory=<class 'actio_python_utils.database_functions.LoggingCursor'>)[source]

Methods

__init__([service, db_args, log_name, ...])

connect()

Connects to the database and creates attributes db and cursor for the connection and a cursor, respectively

disconnect(exception)

Disconnect from the database and commit if self.commit = True and no exception has been raised

connect()[source]

Connects to the database and creates attributes db and cursor for the connection and a cursor, respectively

Return type:

None

disconnect(exception)[source]

Disconnect from the database and commit if self.commit = True and no exception has been raised

Parameters:

exception (bool) – Whether an exception has been raised or not

Return type:

None