Created
May 18, 2018 20:53
-
-
Save danizen/df58bf36759cdd9e329bf91e9fcf452f to your computer and use it in GitHub Desktop.
petl from Django utility
This file contains hidden or 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
from django.db.backends.base.base import BaseDatabaseWrapper | |
from django.db import connections | |
import cx_Oracle | |
__all__ = ( | |
'get_etl_cursor', | |
) | |
class CursorProxy(object): | |
""" | |
A cursor proxy that allows petl to work properly with Oracle | |
""" | |
def __init__(self, cursor): | |
self._cursor = cursor | |
def __iter__(self): | |
return iter(self._cursor) | |
def executemany(self, statement, parameters, **kwargs): | |
parameters = list(parameters) | |
return self._cursor.executemany(statement, parameters, **kwargs) | |
def __getattr__(self, item): | |
return getattr(self._cursor, item) | |
def get_etl_cursor(alias='default'): | |
""" | |
Wraps the creation of a cursor to make sure that cursor is proxied, | |
and unwrapped. | |
:param connection: DBO object | |
:return: a cursor on the underlying database | |
""" | |
def get_connection_cursor(): | |
connection = connections[alias] | |
if isinstance(connection, BaseDatabaseWrapper): | |
if connection.connection is None: | |
connection.connect() | |
connection = connection.connection | |
cursor = connection.cursor() | |
if isinstance(cursor, cx_Oracle.Cursor): | |
cursor = CursorProxy(cursor) | |
return cursor | |
return get_connection_cursor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment