Guest User

Untitled

a guest
May 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. from django.db.backends.base.base import BaseDatabaseWrapper
  2. from django.db import connections
  3. import cx_Oracle
  4.  
  5. __all__ = (
  6. 'get_etl_cursor',
  7. )
  8.  
  9.  
  10. class CursorProxy(object):
  11. """
  12. A cursor proxy that allows petl to work properly with Oracle
  13. """
  14.  
  15. def __init__(self, cursor):
  16. self._cursor = cursor
  17.  
  18. def __iter__(self):
  19. return iter(self._cursor)
  20.  
  21. def executemany(self, statement, parameters, **kwargs):
  22. parameters = list(parameters)
  23. return self._cursor.executemany(statement, parameters, **kwargs)
  24.  
  25. def __getattr__(self, item):
  26. return getattr(self._cursor, item)
  27.  
  28.  
  29. def get_etl_cursor(alias='default'):
  30. """
  31. Wraps the creation of a cursor to make sure that cursor is proxied,
  32. and unwrapped.
  33.  
  34. :param connection: DBO object
  35. :return: a cursor on the underlying database
  36. """
  37. def get_connection_cursor():
  38. connection = connections[alias]
  39. if isinstance(connection, BaseDatabaseWrapper):
  40. if connection.connection is None:
  41. connection.connect()
  42. connection = connection.connection
  43. cursor = connection.cursor()
  44. if isinstance(cursor, cx_Oracle.Cursor):
  45. cursor = CursorProxy(cursor)
  46. return cursor
  47. return get_connection_cursor
Add Comment
Please, Sign In to add comment