70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
from logging_config import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
class OracleDB:
|
|
def __init__(self, connector):
|
|
self.connector = connector
|
|
|
|
def fetch_all(self, sql):
|
|
conn = cur = None
|
|
try:
|
|
conn = self.connector.get_connection()
|
|
cur = conn.cursor()
|
|
cur.execute(sql)
|
|
cols = [c[0] for c in cur.description]
|
|
return [dict(zip(cols, r)) for r in cur.fetchall()]
|
|
except Exception:
|
|
logger.error("Error executing fetch_all", exc_info=True)
|
|
raise
|
|
finally:
|
|
if cur:
|
|
cur.close()
|
|
if conn:
|
|
conn.close()
|
|
|
|
def execute(self, sql, params):
|
|
conn = cur = None
|
|
try:
|
|
conn = self.connector.get_connection()
|
|
cur = conn.cursor()
|
|
cur.execute(sql, params)
|
|
conn.commit()
|
|
except Exception:
|
|
if conn:
|
|
conn.rollback()
|
|
logger.error("Error executing SQL", exc_info=True)
|
|
raise
|
|
finally:
|
|
if cur:
|
|
cur.close()
|
|
if conn:
|
|
conn.close()
|
|
|
|
def call_procedure(self, proc_name):
|
|
conn = cur = None
|
|
try:
|
|
conn = self.connector.get_connection()
|
|
cur = conn.cursor()
|
|
logger.info("Calling stored procedure: %s", proc_name)
|
|
try:
|
|
cur.callproc(proc_name)
|
|
except Exception:
|
|
cur.execute(f"BEGIN {proc_name}; END;")
|
|
conn.commit()
|
|
logger.info("Stored procedure %s executed successfully", proc_name)
|
|
return True
|
|
except Exception as e:
|
|
logger.error(
|
|
"Stored procedure %s failed: %s",
|
|
proc_name,
|
|
str(e),
|
|
exc_info=True
|
|
)
|
|
return False
|
|
finally:
|
|
if cur:
|
|
cur.close()
|
|
if conn:
|
|
conn.close()
|