Skip to content

Instantly share code, notes, and snippets.

@danizen
Created May 18, 2018 20:53
Show Gist options
  • Save danizen/df58bf36759cdd9e329bf91e9fcf452f to your computer and use it in GitHub Desktop.
Save danizen/df58bf36759cdd9e329bf91e9fcf452f to your computer and use it in GitHub Desktop.
petl from Django utility
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