Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment