[cx-oracle-users] How to use module in a class
Brought to you by:
atuining
|
From: Andrew Z. <fo...@gm...> - 2017-09-26 04:11:04
|
Hello,
i'm trying to use the module within a designated "Database" class and
having all sorts of funny situations.
I'd appreciate some guidance on how to architecture the interaction in a
better way.
Task:
I'd like to separate all database related operations into one class (
let's call it DB).
for example:
import cx_Oracle
import decimal
class DB():
DEFAULT_DB = '127.0.0.1/TT_1122:timesten:direct'
DEFAULT_DB_USER = "username"
DEFAULT_DB_USER_PASSWD = "password"
def __init__(self, db = None, user = None, passw = None):
if db is None:
db = self.DEFAULT_DB
if user is None:
user = self.DEFAULT_DB_USER
if passw is None:
passw = self.DEFAULT_DB_USER_PASSWD
self.connection = cx_Oracle.Connection(user + "/" + passw + "@"
+ self.DEFAULT_DB)
self.connection.outputtypehandler = self.OutputTypeHandler
def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
if defaultType == cx_Oralce.NUMBER:
return cursor.var(str, size=200, arraysize=Cursor.arraysize,
outconverter=decimal.Decimal)
def close(self, commit=True):
"""Close connection"""
if commit:
self.connection.commit()
self.connection.close()
def checkData(self, minutes):
cursor = self.connection.cursor()
cursor.execute("select sum(reqid) from movaver")
rows = cursor.fetchone()
cursor.close()
if rows is None:
raise Exception("No rows returned during checkData({})".format(minutes))
return rows[0]
There are a few issues with this approach. For instance the "var" and
"arraysize" in cursor ( function OutputTypeHandler" are not been resolved
as attributes of "cursor"
though, if i do not try to wrap this functionality in the class and just
simply import the cx_Oracle and start writing the functions - everything
gets resolved as expected.
My other attempt was to import Connection, Cursor etc from cx_Oracle and
define DB as a child of many parents... This is how i learned that python
doesn't support such arrangements.
Appreciate your advice.
|