39 lines
1002 B
Python
39 lines
1002 B
Python
import oracledb
|
|
import os
|
|
import traceback
|
|
import sys
|
|
|
|
|
|
def connect(alias):
|
|
|
|
username = os.getenv(alias + "_DB_USER")
|
|
password = os.getenv(alias + "_DB_PASS")
|
|
tnsalias = os.getenv(alias + "_DB_TNS")
|
|
connstr = username + "/" + password + "@" + tnsalias
|
|
|
|
oracledb.init_oracle_client()
|
|
|
|
try:
|
|
conn = oracledb.connect(connstr)
|
|
return conn
|
|
except oracledb.DatabaseError as db_err:
|
|
tb = traceback.format_exc()
|
|
print(f"DatabaseError connecting to '{alias}': {db_err}\n{tb}", file=sys.stderr)
|
|
sys.exit(1)
|
|
except Exception as exc:
|
|
tb = traceback.format_exc()
|
|
print(f"Unexpected error connecting to '{alias}': {exc}\n{tb}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
def run_proc(connection, proc: str, param: []):
|
|
curs = connection.cursor()
|
|
curs.callproc(proc, param)
|
|
|
|
|
|
def run_func(connection, proc: str, rettype, param: []):
|
|
curs = connection.cursor()
|
|
ret = curs.callfunc(proc, rettype, param)
|
|
|
|
return ret
|