def _internalPrepare(self, space, w_stmt, w_tag): # make sure we don't get a situation where nothing is to be executed if w_stmt is None and self.w_statement is None: raise OperationError( interp_error.get(space).w_ProgrammingError, space.wrap("no statement specified " "and no prior statement prepared")) # nothing to do if the statement is identical to the one already stored # but go ahead and prepare anyway for create, alter and drop statments if w_stmt is None or w_stmt == self.w_statement: if self.statementType not in (roci.OCI_STMT_CREATE, roci.OCI_STMT_DROP, roci.OCI_STMT_ALTER): return w_stmt = self.w_statement else: self.w_statement = w_stmt # release existing statement, if necessary self.w_statementTag = w_tag self.freeHandle(space) # prepare statement self.isOwned = False handleptr = lltype.malloc(roci.Ptr(roci.OCIStmt).TO, 1, flavor='raw') stmtBuffer = StringBuffer() tagBuffer = StringBuffer() stmtBuffer.fill(space, w_stmt) tagBuffer.fill(space, w_tag) try: status = roci.OCIStmtPrepare2( self.connection.handle, handleptr, self.environment.errorHandle, stmtBuffer.ptr, stmtBuffer.size, tagBuffer.ptr, tagBuffer.size, roci.OCI_NTV_SYNTAX, roci.OCI_DEFAULT) self.environment.checkForError( status, "Connection_InternalPrepare(): prepare") self.handle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') stmtBuffer.clear() tagBuffer.clear() # clear bind variables, if applicable if not self.setInputSizes: self.bindList = None self.bindDict = None # clear row factory, if applicable self.rowFactory = None # determine if statement is a query self._getStatementType()
def freeHandle(self, space, raiseError=True): if not self.handle: return if self.isOwned: roci.OCIHandleFree(self.handle, roci.OCI_HTYPE_STMT) elif self.connection.handle: tagBuffer = StringBuffer() tagBuffer.fill(space, self.w_statementTag) try: status = roci.OCIStmtRelease( self.handle, self.environment.errorHandle, tagBuffer.ptr, tagBuffer.size, roci.OCI_DEFAULT) self.environment.checkForError( status, "Cursor_FreeHandle()") finally: tagBuffer.clear()
def freeHandle(self, space, raiseError=True): if not self.handle: return if self.isOwned: roci.OCIHandleFree(self.handle, roci.OCI_HTYPE_STMT) elif self.connection.handle: tagBuffer = StringBuffer() tagBuffer.fill(space, self.w_statementTag) try: status = roci.OCIStmtRelease(self.handle, self.environment.errorHandle, tagBuffer.ptr, tagBuffer.size, roci.OCI_DEFAULT) self.environment.checkForError(status, "Cursor_FreeHandle()") finally: tagBuffer.clear()
def getConnection(self, space, pool, w_cclass, purity): """Get a connection using the OCISessionGet() interface rather than using the low level interface for connecting.""" proxyCredentials = False authInfo = lltype.nullptr(roci.OCIAuthInfo.TO) if pool: w_dbname = pool.w_name mode = roci.OCI_SESSGET_SPOOL if not pool.homogeneous and pool.w_username and self.w_username: proxyCredentials = space.is_true( space.ne(pool.w_username, self.w_username)) mode |= roci.OCI_SESSGET_CREDPROXY else: w_dbname = self.w_tnsentry mode = roci.OCI_SESSGET_STMTCACHE stringBuffer = StringBuffer() # set up authorization handle, if needed if not pool or w_cclass or proxyCredentials: # create authorization handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIAuthInfo).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_AUTHINFO, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_GetConnection(): allocate handle") authInfo = handleptr[0] finally: lltype.free(handleptr, flavor='raw') externalCredentials = True # set the user name, if applicable stringBuffer.fill(space, self.w_username) try: if stringBuffer.size > 0: externalCredentials = False status = roci.OCIAttrSet(authInfo, roci.OCI_HTYPE_AUTHINFO, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_USERNAME, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set user name") finally: stringBuffer.clear() # set the password, if applicable stringBuffer.fill(space, self.w_password) try: if stringBuffer.size > 0: externalCredentials = False status = roci.OCIAttrSet(authInfo, roci.OCI_HTYPE_AUTHINFO, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_PASSWORD, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set password") finally: stringBuffer.clear() # if no user name or password are set, using external credentials if not pool and externalCredentials: mode |= roci.OCI_SESSGET_CREDEXT # set the connection class, if applicable if roci.OCI_ATTR_CONNECTION_CLASS is not None: stringBuffer.fill(space, w_cclass) try: if stringBuffer.size > 0: externalCredentials = False status = roci.OCIAttrSet( authInfo, roci.OCI_HTYPE_AUTHINFO, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_CONNECTION_CLASS, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set connection class") finally: stringBuffer.clear() # set the purity, if applicable if (roci.OCI_ATTR_PURITY is not None and purity != roci.OCI_ATTR_PURITY_DEFAULT): purityptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') purityptr[0] = rffi.cast(roci.ub4, purity) try: status = roci.OCIAttrSet(authInfo, roci.OCI_HTYPE_AUTHINFO, rffi.cast(roci.dvoidp, purityptr), rffi.sizeof(roci.ub4), roci.OCI_ATTR_PURITY, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set purity") finally: lltype.free(purityptr, flavor='raw') # acquire the new session stringBuffer.fill(space, w_dbname) foundptr = lltype.malloc(rffi.CArrayPtr(roci.boolean).TO, 1, flavor='raw') handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCISvcCtx).TO, 1, flavor='raw') try: status = roci.OCISessionGet( self.environment.handle, self.environment.errorHandle, handleptr, authInfo, stringBuffer.ptr, stringBuffer.size, None, 0, lltype.nullptr(roci.Ptr(roci.oratext).TO), lltype.nullptr(roci.Ptr(roci.ub4).TO), foundptr, mode) self.environment.checkForError( status, "Connection_GetConnection(): get connection") self.handle = handleptr[0] finally: stringBuffer.clear() lltype.free(foundptr, flavor='raw') lltype.free(handleptr, flavor='raw') # eliminate the authorization handle immediately, if applicable if authInfo: roci.OCIHandleFree(authInfo, roci.OCI_HTYPE_AUTHINFO) # copy members in the case where a pool is being used if pool: if not proxyCredentials: self.w_username = pool.w_username self.w_password = pool.w_password self.w_tnsentry = pool.w_tnsentry self.sessionPool = pool self.release = True
def connect(self, space, mode, twophase): stringBuffer = StringBuffer() # allocate the server handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIServer).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SERVER, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_Connect(): allocate server handle") self.serverHandle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') # attach to the server stringBuffer.fill(space, self.w_tnsentry) try: status = roci.OCIServerAttach(self.serverHandle, self.environment.errorHandle, stringBuffer.ptr, stringBuffer.size, roci.OCI_DEFAULT) self.environment.checkForError( status, "Connection_Connect(): server attach") finally: stringBuffer.clear() # allocate the service context handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCISvcCtx).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SVCCTX, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_Connect(): allocate service context handle") self.handle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') # set attribute for server handle status = roci.OCIAttrSet(self.handle, roci.OCI_HTYPE_SVCCTX, self.serverHandle, 0, roci.OCI_ATTR_SERVER, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set server handle") # set the internal and external names; these are needed for global # transactions but are limited in terms of the lengths of the strings if twophase: status = roci.OCIAttrSet(self.serverHandle, roci.OCI_HTYPE_SERVER, "cx_Oracle", 0, roci.OCI_ATTR_INTERNAL_NAME, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set internal name") status = roci.OCIAttrSet(self.serverHandle, roci.OCI_HTYPE_SERVER, "cx_Oracle", 0, roci.OCI_ATTR_EXTERNAL_NAME, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set external name") # allocate the session handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCISession).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SESSION, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_Connect(): allocate session handle") self.sessionHandle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') credentialType = roci.OCI_CRED_EXT # set user name in session handle stringBuffer.fill(space, self.w_username) try: if stringBuffer.size > 0: credentialType = roci.OCI_CRED_RDBMS status = roci.OCIAttrSet(self.sessionHandle, roci.OCI_HTYPE_SESSION, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_USERNAME, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set user name") finally: stringBuffer.clear() # set password in session handle stringBuffer.fill(space, self.w_password) try: if stringBuffer.size > 0: credentialType = roci.OCI_CRED_RDBMS status = roci.OCIAttrSet(self.sessionHandle, roci.OCI_HTYPE_SESSION, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_PASSWORD, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set password") finally: stringBuffer.clear() # set the session handle on the service context handle status = roci.OCIAttrSet(self.handle, roci.OCI_HTYPE_SVCCTX, self.sessionHandle, 0, roci.OCI_ATTR_SESSION, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set session handle") # if a new password has been specified, change it which will also # establish the session # begin the session status = roci.OCISessionBegin(self.handle, self.environment.errorHandle, self.sessionHandle, credentialType, mode) try: self.environment.checkForError( status, "Connection_Connect(): begin session") except: self.sessionHandle = lltype.nullptr(roci.OCISession.TO) raise
def _internalPrepare(self, space, w_stmt, w_tag): # make sure we don't get a situation where nothing is to be executed if w_stmt is None and self.w_statement is None: raise OperationError( interp_error.get(space).w_ProgrammingError, space.wrap("no statement specified " "and no prior statement prepared")) # nothing to do if the statement is identical to the one already stored # but go ahead and prepare anyway for create, alter and drop statments if w_stmt is None or w_stmt == self.w_statement: if self.statementType not in (roci.OCI_STMT_CREATE, roci.OCI_STMT_DROP, roci.OCI_STMT_ALTER): return w_stmt = self.w_statement else: self.w_statement = w_stmt # release existing statement, if necessary self.w_statementTag = w_tag self.freeHandle(space) # prepare statement self.isOwned = False handleptr = lltype.malloc(roci.Ptr(roci.OCIStmt).TO, 1, flavor='raw') stmtBuffer = StringBuffer() tagBuffer = StringBuffer() stmtBuffer.fill(space, w_stmt) tagBuffer.fill(space, w_tag) try: status = roci.OCIStmtPrepare2(self.connection.handle, handleptr, self.environment.errorHandle, stmtBuffer.ptr, stmtBuffer.size, tagBuffer.ptr, tagBuffer.size, roci.OCI_NTV_SYNTAX, roci.OCI_DEFAULT) self.environment.checkForError( status, "Connection_InternalPrepare(): prepare") self.handle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') stmtBuffer.clear() tagBuffer.clear() # clear bind variables, if applicable if not self.setInputSizes: self.bindList = None self.bindDict = None # clear row factory, if applicable self.rowFactory = None # determine if statement is a query self._getStatementType()
def getConnection(self, space, pool, w_cclass, purity): """Get a connection using the OCISessionGet() interface rather than using the low level interface for connecting.""" proxyCredentials = False authInfo = lltype.nullptr(roci.OCIAuthInfo.TO) if pool: w_dbname = pool.w_name mode = roci.OCI_SESSGET_SPOOL if not pool.homogeneous and pool.w_username and self.w_username: proxyCredentials = space.is_true(space.ne(pool.w_username, self.w_username)) mode |= roci.OCI_SESSGET_CREDPROXY else: w_dbname = self.w_tnsentry mode = roci.OCI_SESSGET_STMTCACHE stringBuffer = StringBuffer() # set up authorization handle, if needed if not pool or w_cclass or proxyCredentials: # create authorization handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIAuthInfo).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_AUTHINFO, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_GetConnection(): allocate handle") authInfo = handleptr[0] finally: lltype.free(handleptr, flavor='raw') externalCredentials = True # set the user name, if applicable stringBuffer.fill(space, self.w_username) try: if stringBuffer.size > 0: externalCredentials = False status = roci.OCIAttrSet( authInfo, roci.OCI_HTYPE_AUTHINFO, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_USERNAME, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set user name") finally: stringBuffer.clear() # set the password, if applicable stringBuffer.fill(space, self.w_password) try: if stringBuffer.size > 0: externalCredentials = False status = roci.OCIAttrSet( authInfo, roci.OCI_HTYPE_AUTHINFO, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_PASSWORD, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set password") finally: stringBuffer.clear() # if no user name or password are set, using external credentials if not pool and externalCredentials: mode |= roci.OCI_SESSGET_CREDEXT # set the connection class, if applicable if roci.OCI_ATTR_CONNECTION_CLASS is not None: stringBuffer.fill(space, w_cclass) try: if stringBuffer.size > 0: externalCredentials = False status = roci.OCIAttrSet( authInfo, roci.OCI_HTYPE_AUTHINFO, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_CONNECTION_CLASS, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set connection class") finally: stringBuffer.clear() # set the purity, if applicable if (roci.OCI_ATTR_PURITY is not None and purity != roci.OCI_ATTR_PURITY_DEFAULT): purityptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') purityptr[0] = rffi.cast(roci.ub4, purity) try: status = roci.OCIAttrSet( authInfo, roci.OCI_HTYPE_AUTHINFO, rffi.cast(roci.dvoidp, purityptr), rffi.sizeof(roci.ub4), roci.OCI_ATTR_PURITY, self.environment.errorHandle) self.environment.checkForError( status, "Connection_GetConnection(): set purity") finally: lltype.free(purityptr, flavor='raw') # acquire the new session stringBuffer.fill(space, w_dbname) foundptr = lltype.malloc(rffi.CArrayPtr(roci.boolean).TO, 1, flavor='raw') handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCISvcCtx).TO, 1, flavor='raw') try: status = roci.OCISessionGet( self.environment.handle, self.environment.errorHandle, handleptr, authInfo, stringBuffer.ptr, stringBuffer.size, None, 0, lltype.nullptr(roci.Ptr(roci.oratext).TO), lltype.nullptr(roci.Ptr(roci.ub4).TO), foundptr, mode) self.environment.checkForError( status, "Connection_GetConnection(): get connection") self.handle = handleptr[0] finally: stringBuffer.clear() lltype.free(foundptr, flavor='raw') # eliminate the authorization handle immediately, if applicable if authInfo: roci.OCIHandleFree(authInfo, roci.OCI_HTYPE_AUTHINFO) # copy members in the case where a pool is being used if pool: if not proxyCredentials: self.w_username = pool.w_username self.w_password = pool.w_password self.w_tnsentry = pool.w_tnsentry self.sessionPool = pool self.release = True
def connect(self, space, mode, twophase): stringBuffer = StringBuffer() # allocate the server handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIServer).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SERVER, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_Connect(): allocate server handle") self.serverHandle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') # attach to the server stringBuffer.fill(space, self.w_tnsentry) try: status = roci.OCIServerAttach( self.serverHandle, self.environment.errorHandle, stringBuffer.ptr, stringBuffer.size, roci.OCI_DEFAULT) self.environment.checkForError( status, "Connection_Connect(): server attach") finally: stringBuffer.clear() # allocate the service context handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCISvcCtx).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SVCCTX, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_Connect(): allocate service context handle") self.handle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') # set attribute for server handle status = roci.OCIAttrSet( self.handle, roci.OCI_HTYPE_SVCCTX, self.serverHandle, 0, roci.OCI_ATTR_SERVER, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set server handle") # set the internal and external names; these are needed for global # transactions but are limited in terms of the lengths of the strings if twophase: raise OperationError( interp_error.get(space).w_NotSupportedError, space.wrap("XXX write me")) # allocate the session handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCISession).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SESSION, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "Connection_Connect(): allocate session handle") self.sessionHandle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') credentialType = roci.OCI_CRED_EXT # set user name in session handle stringBuffer.fill(space, self.w_username) try: if stringBuffer.size > 0: credentialType = roci.OCI_CRED_RDBMS status = roci.OCIAttrSet( self.sessionHandle, roci.OCI_HTYPE_SESSION, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_USERNAME, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set user name") finally: stringBuffer.clear() # set password in session handle stringBuffer.fill(space, self.w_password) try: if stringBuffer.size > 0: credentialType = roci.OCI_CRED_RDBMS status = roci.OCIAttrSet( self.sessionHandle, roci.OCI_HTYPE_SESSION, stringBuffer.ptr, stringBuffer.size, roci.OCI_ATTR_PASSWORD, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set password") finally: stringBuffer.clear() # set the session handle on the service context handle status = roci.OCIAttrSet( self.handle, roci.OCI_HTYPE_SVCCTX, self.sessionHandle, 0, roci.OCI_ATTR_SESSION, self.environment.errorHandle) self.environment.checkForError( status, "Connection_Connect(): set session handle") # if a new password has been specified, change it which will also # establish the session # begin the session status = roci.OCISessionBegin( self.handle, self.environment.errorHandle, self.sessionHandle, credentialType, mode) try: self.environment.checkForError( status, "Connection_Connect(): begin session") except: self.sessionHandle = lltype.nullptr(roci.OCISession.TO) raise