def allocateHandle(self): handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIStmt).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_STMT, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError(status, "Cursor_New()") self.handle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') self.isOwned = True
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 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 initialize(self, connection, param): nameptr = lltype.malloc(rffi.CArrayPtr(roci.oratext).TO, 1, flavor='raw') lenptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') try: # determine the schema of the type status = roci.OCIAttrGet( param, roci.OCI_HTYPE_DESCRIBE, rffi.cast(roci.dvoidp, nameptr), lenptr, roci.OCI_ATTR_SCHEMA_NAME, self.environment.errorHandle) self.environment.checkForError( status, "ObjectType_Initialize(): get schema name") self.schema = rffi.charpsize2str(nameptr[0], lenptr[0]) # determine the name of the type status = roci.OCIAttrGet( param, roci.OCI_HTYPE_DESCRIBE, rffi.cast(roci.dvoidp, nameptr), lenptr, roci.OCI_ATTR_TYPE_NAME, self.environment.errorHandle) self.environment.checkForError( status, "ObjectType_Initialize(): get schema name") self.name = rffi.charpsize2str(nameptr[0], lenptr[0]) finally: lltype.free(nameptr, flavor='raw') lltype.free(lenptr, flavor='raw') # retrieve TDO (type descriptor object) of the parameter tdorefptr = lltype.malloc(rffi.CArrayPtr(roci.OCIRef).TO, 1, flavor='raw') try: status = roci.OCIAttrGet( param, roci.OCI_HTYPE_DESCRIBE, rffi.cast(roci.dvoidp, tdorefptr), lltype.nullptr(roci.Ptr(roci.ub4).TO), roci.OCI_ATTR_REF_TDO, self.environment.errorHandle) self.environment.checkForError( status, "ObjectType_Initialize(): get TDO reference") tdoref = tdorefptr[0] finally: lltype.free(tdorefptr, flavor='raw') tdoptr = lltype.malloc(rffi.CArrayPtr(roci.dvoidp).TO, 1, flavor='raw') try: status = roci.OCIObjectPin( self.environment.handle, self.environment.errorHandle, tdoref, None, roci.OCI_PIN_ANY, roci.OCI_DURATION_SESSION, roci.OCI_LOCK_NONE, tdoptr) self.environment.checkForError( status, "ObjectType_Initialize(): pin TDO reference") self.tdo = tdoptr[0] finally: lltype.free(tdoptr, flavor='raw') # acquire a describe handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIDescribe).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_DESCRIBE, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "ObjectType_Initialize(): allocate describe handle") describeHandle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') # describe the type try: self.describe(connection, describeHandle) finally: roci.OCIHandleFree(describeHandle, roci.OCI_HTYPE_DESCRIBE)
def descr_new(space, w_subtype, w_user, w_password, w_dsn, min, max, increment, w_connectiontype=Null, threaded=False, getmode=roci.OCI_SPOOL_ATTRVAL_NOWAIT, events=False, homogeneous=True): self = space.allocate_instance(W_SessionPool, w_subtype) W_SessionPool.__init__(self) if w_connectiontype is not None: if not space.is_true( space.issubtype(w_connectiontype, get(space).w_Connection)): raise OperationError( interp_error.get(space).w_ProgrammingError, space.wrap( "connectiontype must be a subclass of Connection")) self.w_connectionType = w_connectiontype else: self.w_connectionType = get(space).w_Connection self.w_username = w_user self.w_password = w_password self.w_tnsentry = w_dsn self.minSessions = min self.maxSessions = max self.sessionIncrement = increment self.homogeneous = homogeneous # set up the environment self.environment = interp_environ.Environment.create( space, threaded, events) # create the session pool handle handleptr = lltype.malloc(rffi.CArrayPtr(roci.OCIServer).TO, 1, flavor='raw') try: status = roci.OCIHandleAlloc( self.environment.handle, handleptr, roci.OCI_HTYPE_SPOOL, 0, lltype.nullptr(rffi.CArray(roci.dvoidp))) self.environment.checkForError( status, "SessionPool_New(): allocate handle") self.handle = handleptr[0] finally: lltype.free(handleptr, flavor='raw') # prepare pool mode poolMode = roci.OCI_SPC_STMTCACHE if self.homogeneous: poolMode |= roci.OCI_SPC_HOMOGENEOUS # create the session pool user_buf = config.StringBuffer() user_buf.fill(space, self.w_username) password_buf = config.StringBuffer() password_buf.fill(space, self.w_password) dsn_buf = config.StringBuffer() dsn_buf.fill(space, self.w_tnsentry) poolnameptr = lltype.malloc(rffi.CArrayPtr(roci.oratext).TO, 1, flavor='raw') poolnamelenptr = lltype.malloc(rffi.CArrayPtr(roci.ub4).TO, 1, flavor='raw') try: status = roci.OCISessionPoolCreate( self.environment.handle, self.environment.errorHandle, self.handle, poolnameptr, poolnamelenptr, dsn_buf.ptr, dsn_buf.size, min, max, increment, user_buf.ptr, user_buf.size, password_buf.ptr, password_buf.size, poolMode) self.environment.checkForError(status, "SessionPool_New(): create pool") self.w_name = config.w_string( space, poolnameptr[0], rffi.cast(lltype.Signed, poolnamelenptr[0])) finally: user_buf.clear() password_buf.clear() dsn_buf.clear() lltype.free(poolnameptr, flavor='raw') lltype.free(poolnamelenptr, flavor='raw') return space.wrap(self)