pypy /pypy/module/oracle/interp_environ.py

Language Python Lines 100
MD5 Hash 3189454d47f09d02ec6c8ee1a985b0e7 Estimated Cost $1,805 (why?)
Repository https://bitbucket.org/dac_io/pypy View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.oracle import roci, config
from pypy.interpreter.error import OperationError

from pypy.module.oracle.interp_error import W_Error, get

class Environment(object):
    def __init__(self, space, handle):
        self.space = space
        self.handle = handle

        # create the error handle
        handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIError).TO,
                                  1, flavor='raw')
        try:
            status = roci.OCIHandleAlloc(
                self.handle,
                handleptr, roci.OCI_HTYPE_ERROR, 0,
                lltype.nullptr(rffi.CArray(roci.dvoidp)))
            self.checkForError(
                status, "Environment_New(): create error handle")
            self.errorHandle = handleptr[0]
        finally:
            lltype.free(handleptr, flavor='raw')


    def checkForError(self, status, context):
        if status in (roci.OCI_SUCCESS, roci.OCI_SUCCESS_WITH_INFO):
            return

        if status != roci.OCI_INVALID_HANDLE:
            # At this point it is assumed that the Oracle
            # environment is fully initialized
            error = W_Error(self.space, self, context, 1)
            if error.code in (1, 1400, 2290, 2291, 2292):
                w_type = get(self.space).w_IntegrityError
            elif error.code in (1012, 1033, 1034, 1089, 3113, 3114,
                                12203, 12500, 12571):
                w_type = get(self.space).w_OperationalError
            else:
                w_type = get(self.space).w_DatabaseError
            raise OperationError(w_type, self.space.wrap(error))

        error = W_Error(self.space, self, context, 0)
        error.code = 0
        error.w_message = self.space.wrap("Invalid handle!")
        raise OperationError(get(self.space).w_DatabaseError,
                             self.space.wrap(error))

    @staticmethod
    def create(space, threaded, events):
        "Create a new environment object from scratch"
        mode = roci.OCI_OBJECT
        if threaded:
            mode |= roci.OCI_THREADED
        if events:
            mode |= roci.OCI_EVENTS

        handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIEnv).TO,
                                  1, flavor='raw')

        try:

            status = roci.OCIEnvNlsCreate(
                handleptr, mode,
                None,
                None, None, None,
                0, lltype.nullptr(rffi.CArray(roci.dvoidp)),
                config.CHARSETID, config.CHARSETID)

            if not handleptr[0] or status not in (roci.OCI_SUCCESS,
                                                  roci.OCI_SUCCESS_WITH_INFO):
                raise OperationError(
                    get(space).w_InterfaceError,
                    space.wrap(
                        "Unable to acquire Oracle environment handle"))

            handle = handleptr[0]
        finally:
            lltype.free(handleptr, flavor='raw')

        try:
            newenv = Environment(space, handle)
        except:
            roci.OCIHandleFree(handle, roci.OCI_HTYPE_ENV)
            raise

        newenv.maxBytesPerCharacter = config.BYTES_PER_CHAR
        newenv.maxStringBytes = config.BYTES_PER_CHAR * config.MAX_STRING_CHARS
        return newenv

    def clone(self):
        """Clone an existing environment.
        used when acquiring a connection from a session pool, for example."""
        newenv = Environment(self.space, self.handle)
        newenv.maxBytesPerCharacter = self.maxBytesPerCharacter
        newenv.maxStringBytes = self.maxStringBytes
        return newenv
Back to Top