diff --git a/oracle.py b/oracle.py index 9a63fb0..a609622 100644 --- a/oracle.py +++ b/oracle.py @@ -1,16 +1,29 @@ import cx_Oracle +APP_CTX_NAMESPACE = "USERENV" +APP_CTX_ENTRIES = [ + ( APP_CTX_NAMESPACE, "MODULE", "modulo prueba"), +] -class OraConn: - def __init__(self, str_conn, conn_name=None): - self.conn = cx_Oracle.connect(str_conn, encoding='UTF-8') - self.cur = self.conn.cursor() - if conn_name is not None: - self.cur.callproc('DBMS_APPLICATION_INFO.SET_MODULE', [conn_name, None]) - self.cur.close() + +class OraConn(cx_Oracle.Connection): + # instance can return a cursor or the whole conn + def __init__(self, str_conn, ret="conn", module_name=None): + super().__init__(str_conn, encoding='UTF-8') + self.cur = super().cursor() + self.ret = ret + if module_name is not None: + self.cur.callproc('DBMS_APPLICATION_INFO.SET_MODULE', [module_name, None]) + + if self.ret == "conn": + self.cur.close() def __enter__(self): - return self.conn + if self.ret == "cursor": + return self.cur + return self def __exit__(self, exc_type, exc_val, exc_tb): - self.conn.close() + if self.ret == "cursor": + return self.cur.close() + super().__exit__(exc_type, exc_val, exc_tb)