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:
objectCreates 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 todb_args (
Optional[Mapping[str,Any]], default:None) – A mapping of database connection argumentslog_name (
str, default:'sql_debug') – The name to give the loggercommit (
bool, default:False) – Commit the transaction upon closing the connection if no errors were encounteredcursor_factory (
cursor, default:<class 'actio_python_utils.database_functions.LoggingCursor'>) – The class of cursor to use for the connection by default (used forservice/$DB_CONNECTION_STRING)
- Raises:
ValueError – If
serviceanddb_argsare 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
dbandcursorfor the connection and a cursor, respectivelydisconnect(exception)Disconnect from the database and commit if
self.commit = Trueand no exception has been raised