Re: [cx-oracle-users] How to use module in a class
Brought to you by:
atuining
From: Anthony T. <ant...@gm...> - 2017-10-13 17:20:56
|
Hi, For this sort of thing I would definitely suggest subclassing from cx_Oracle.Connection and cx_Oracle.Cursor. Something like this: class MyConnection(cx_Oracle.Connection): 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): super(MyConnection, self).__init__(user or self.DEFAULT_DB_USER, passw or self.DEFAULT_DB_USER_PASSWD, db or self.DEFAULT_DB) self.outputtypehandler = self.OutputTypeHandler def OutputTypeHandler(cursor, name, defaultType, size, precision, scale): if defaultType == cx_Oralce.NUMBER: return cursor.var(decimal.Decimal, arraysize=cursor.arraysize) def cursor(self): return MyCursor(self) class MyCursor(cx_Oracle.Cursor): pass A few comments: (1) I've simplified the output type handler since cx_Oracle knows how to convert directly to decimal.Decimal (2) Unless you want to override cursor.execute() or add additional functions yourself, there probably isn't any need to subclass the cursor I'm not sure what you mean by your last sentences. If you can explain further I can try to help. On Mon, Sep 25, 2017 at 10:10 PM, Andrew Zyman <fo...@gm...> wrote: > 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. > > > > ------------------------------------------------------------ > ------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, Slashdot.org! http://sdm.link/slashdot > _______________________________________________ > cx-oracle-users mailing list > cx-...@li... > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > |