change oraconn

This commit is contained in:
Josue Gomez 2019-03-26 17:39:06 -06:00
parent 2d12eeffd1
commit 70223b9cd3

View File

@ -1,16 +1,29 @@
import cx_Oracle 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): class OraConn(cx_Oracle.Connection):
self.conn = cx_Oracle.connect(str_conn, encoding='UTF-8') # instance can return a cursor or the whole conn
self.cur = self.conn.cursor() def __init__(self, str_conn, ret="conn", module_name=None):
if conn_name is not None: super().__init__(str_conn, encoding='UTF-8')
self.cur.callproc('DBMS_APPLICATION_INFO.SET_MODULE', [conn_name, None]) 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() self.cur.close()
def __enter__(self): def __enter__(self):
return self.conn if self.ret == "cursor":
return self.cur
return self
def __exit__(self, exc_type, exc_val, exc_tb): 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)