PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/oracle/interp_environ.py

https://bitbucket.org/dac_io/pypy
Python | 99 lines | 77 code | 17 blank | 5 comment | 14 complexity | 3189454d47f09d02ec6c8ee1a985b0e7 MD5 | raw file
  1. from pypy.rpython.lltypesystem import rffi, lltype
  2. from pypy.module.oracle import roci, config
  3. from pypy.interpreter.error import OperationError
  4. from pypy.module.oracle.interp_error import W_Error, get
  5. class Environment(object):
  6. def __init__(self, space, handle):
  7. self.space = space
  8. self.handle = handle
  9. # create the error handle
  10. handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIError).TO,
  11. 1, flavor='raw')
  12. try:
  13. status = roci.OCIHandleAlloc(
  14. self.handle,
  15. handleptr, roci.OCI_HTYPE_ERROR, 0,
  16. lltype.nullptr(rffi.CArray(roci.dvoidp)))
  17. self.checkForError(
  18. status, "Environment_New(): create error handle")
  19. self.errorHandle = handleptr[0]
  20. finally:
  21. lltype.free(handleptr, flavor='raw')
  22. def checkForError(self, status, context):
  23. if status in (roci.OCI_SUCCESS, roci.OCI_SUCCESS_WITH_INFO):
  24. return
  25. if status != roci.OCI_INVALID_HANDLE:
  26. # At this point it is assumed that the Oracle
  27. # environment is fully initialized
  28. error = W_Error(self.space, self, context, 1)
  29. if error.code in (1, 1400, 2290, 2291, 2292):
  30. w_type = get(self.space).w_IntegrityError
  31. elif error.code in (1012, 1033, 1034, 1089, 3113, 3114,
  32. 12203, 12500, 12571):
  33. w_type = get(self.space).w_OperationalError
  34. else:
  35. w_type = get(self.space).w_DatabaseError
  36. raise OperationError(w_type, self.space.wrap(error))
  37. error = W_Error(self.space, self, context, 0)
  38. error.code = 0
  39. error.w_message = self.space.wrap("Invalid handle!")
  40. raise OperationError(get(self.space).w_DatabaseError,
  41. self.space.wrap(error))
  42. @staticmethod
  43. def create(space, threaded, events):
  44. "Create a new environment object from scratch"
  45. mode = roci.OCI_OBJECT
  46. if threaded:
  47. mode |= roci.OCI_THREADED
  48. if events:
  49. mode |= roci.OCI_EVENTS
  50. handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIEnv).TO,
  51. 1, flavor='raw')
  52. try:
  53. status = roci.OCIEnvNlsCreate(
  54. handleptr, mode,
  55. None,
  56. None, None, None,
  57. 0, lltype.nullptr(rffi.CArray(roci.dvoidp)),
  58. config.CHARSETID, config.CHARSETID)
  59. if not handleptr[0] or status not in (roci.OCI_SUCCESS,
  60. roci.OCI_SUCCESS_WITH_INFO):
  61. raise OperationError(
  62. get(space).w_InterfaceError,
  63. space.wrap(
  64. "Unable to acquire Oracle environment handle"))
  65. handle = handleptr[0]
  66. finally:
  67. lltype.free(handleptr, flavor='raw')
  68. try:
  69. newenv = Environment(space, handle)
  70. except:
  71. roci.OCIHandleFree(handle, roci.OCI_HTYPE_ENV)
  72. raise
  73. newenv.maxBytesPerCharacter = config.BYTES_PER_CHAR
  74. newenv.maxStringBytes = config.BYTES_PER_CHAR * config.MAX_STRING_CHARS
  75. return newenv
  76. def clone(self):
  77. """Clone an existing environment.
  78. used when acquiring a connection from a session pool, for example."""
  79. newenv = Environment(self.space, self.handle)
  80. newenv.maxBytesPerCharacter = self.maxBytesPerCharacter
  81. newenv.maxStringBytes = self.maxStringBytes
  82. return newenv