Created
June 26, 2014 18:19
-
-
Save mmattice/bec94dd77f6671472bad to your computer and use it in GitHub Desktop.
There must be a better way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import informixdb | |
class TransactionInformixDict: | |
"""A lightweight wrapper for a DB-API 'cursor' object. | |
Relays attribute access to the DB cursor. That is, you can call | |
execute(), fetchall(), etc., and they will be called on the | |
underlying DB-API cursor object. Attributes will also be | |
retrieved from there. | |
""" | |
_cursor = None | |
def __init__(self, pool, connection): | |
self._pool = pool | |
self._connection = connection | |
self.reopen() | |
def close(self): | |
_cursor = self._cursor | |
self._cursor = None | |
_cursor.close() | |
def reopen(self): | |
if self._cursor is not None: | |
self.close() | |
try: | |
self._cursor = self._connection.cursor(rowformat=informixdb.ROW_AS_DICT) | |
return | |
except: | |
if not self._pool.reconnect: | |
raise | |
else: | |
log.err(None, "Cursor creation failed") | |
if self._pool.noisy: | |
log.msg('Connection lost, reconnecting') | |
self.reconnect() | |
self._cursor = self._connection.cursor(rowformat=informixdb.ROW_AS_DICT) | |
def reconnect(self): | |
self._connection.reconnect() | |
self._cursor = None | |
def __getattr__(self, name): | |
return getattr(self._cursor, name) | |
class informixDictCP( adbapi.ConnectionPool ): | |
transactionFactory = TransactionInformixDict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment