/* * Reconnect to server (after error) */ sword db_oracle_reconnect(ora_con_t* con) { sword status; if (con->connected) db_oracle_disconnect(con); /* timelimited operation, but OCI tcp-network does not support it :( */ status = OCIServerAttach(con->srvhp, con->errhp, (OraText*)con->uri, con->uri_len, 0); if (status == OCI_SUCCESS) { ++con->connected; /* * timelimited operation, but OCI has BUG in asynch * implementation of OCISessionBegin :(. * * Next code is 'empiric hack' that work (tested) in v10/v11. */ status = begin_timelimit(con, 1); if (status != OCI_SUCCESS) goto done; status = OCISessionBegin(con->svchp, con->errhp, con->authp, OCI_CRED_RDBMS, OCI_DEFAULT); while (wait_timelimit(con, status)) { sword code; status = OCIServerVersion(con->svchp, con->errhp, NULL, 0, OCI_HTYPE_SVCCTX); if ( status != OCI_ERROR || OCIErrorGet(con->errhp, 1, NULL, &code, NULL, 0, OCI_HTYPE_ERROR) != OCI_SUCCESS) break; switch (code) { case 24909: /* other call in progress */ status = OCI_STILL_EXECUTING; continue; case 3127: /* no new operation until active ends */ status = OCISessionBegin(con->svchp, con->errhp, con->authp, OCI_CRED_RDBMS, OCI_DEFAULT); default: break; } break; } if (done_timelimit(con, status)) goto done; if (status == OCI_SUCCESS) ++con->connected; } done: return status; }
static int _doConnect(T C, URL_T url, char** error) { #undef ERROR #define ERROR(e) do {*error = Str_dup(e); return false;} while (0) #define ORAERROR(e) do{ *error = Str_dup(OracleConnection_getLastError(e)); return false;} while(0) const char *database, *username, *password; const char *host = URL_getHost(url); int port = URL_getPort(url); if (! (username = URL_getUser(url))) if (! (username = URL_getParameter(url, "user"))) ERROR("no username specified in URL"); if (! (password = URL_getPassword(url))) if (! (password = URL_getParameter(url, "password"))) ERROR("no password specified in URL"); if (! (database = URL_getPath(url))) ERROR("no database specified in URL"); ++database; /* Create a thread-safe OCI environment with N' substitution turned on. */ if (OCIEnvCreate(&C->env, OCI_THREADED | OCI_OBJECT | OCI_NCHAR_LITERAL_REPLACE_ON, 0, 0, 0, 0, 0, 0)) ERROR("Create a OCI environment failed"); /* allocate an error handle */ if (OCI_SUCCESS != OCIHandleAlloc(C->env, (dvoid**)&C->err, OCI_HTYPE_ERROR, 0, 0)) ERROR("Allocating error handler failed"); /* server contexts */ if (OCI_SUCCESS != OCIHandleAlloc(C->env, (dvoid**)&C->srv, OCI_HTYPE_SERVER, 0, 0)) ERROR("Allocating server context failed"); /* allocate a service handle */ if (OCI_SUCCESS != OCIHandleAlloc(C->env, (dvoid**)&C->svc, OCI_HTYPE_SVCCTX, 0, 0)) ERROR("Allocating service handle failed"); StringBuffer_clear(C->sb); /* Oracle connect string is on the form: //host[:port]/service name */ if (host) { StringBuffer_append(C->sb, "//%s", host); if (port > 0) StringBuffer_append(C->sb, ":%d", port); StringBuffer_append(C->sb, "/%s", database); } else /* Or just service name */ StringBuffer_append(C->sb, "%s", database); /* Create a server context */ C->lastError = OCIServerAttach(C->srv, C->err, StringBuffer_toString(C->sb), StringBuffer_length(C->sb), 0); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); /* Set attribute server context in the service context */ C->lastError = OCIAttrSet(C->svc, OCI_HTYPE_SVCCTX, C->srv, 0, OCI_ATTR_SERVER, C->err); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCIHandleAlloc(C->env, (void**)&C->usr, OCI_HTYPE_SESSION, 0, NULL); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCIAttrSet(C->usr, OCI_HTYPE_SESSION, (dvoid *)username, (int)strlen(username), OCI_ATTR_USERNAME, C->err); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCIAttrSet(C->usr, OCI_HTYPE_SESSION, (dvoid *)password, (int)strlen(password), OCI_ATTR_PASSWORD, C->err); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); C->lastError = OCISessionBegin(C->svc, C->err, C->usr, OCI_CRED_RDBMS, OCI_DEFAULT); if (C->lastError != OCI_SUCCESS && C->lastError != OCI_SUCCESS_WITH_INFO) ORAERROR(C); OCIAttrSet(C->svc, OCI_HTYPE_SVCCTX, C->usr, 0, OCI_ATTR_SESSION, C->err); return true; }
void open(const std::string& service, const std::string& username, const std::string& password, int mode) { if (OCIServerAttach(server, error, (text*) service.c_str(), service.length(), OCI_DEFAULT)) throw_error(); if (OCIAttrSet(svcctx, OCI_HTYPE_SVCCTX, server, 0, OCI_ATTR_SERVER, error)) throw_error(); if (OCIAttrSet(session, OCI_HTYPE_SESSION, (dvoid*) username.c_str(), username.length(), OCI_ATTR_USERNAME, error)) throw_error(); if (OCIAttrSet(session, OCI_HTYPE_SESSION, (dvoid*) password.c_str(), password.length(), OCI_ATTR_PASSWORD, error)) throw_error(); if (OCISessionBegin(svcctx, error, session, OCI_CRED_RDBMS, mode)) throw_error(); if (OCIAttrSet(svcctx, OCI_HTYPE_SVCCTX, session, 0, OCI_ATTR_SESSION, error)) throw_error(); }
// Begins an Oracle session. void OraSession::startSession(const std::string& sConnectAsUser, const std::string& sConnectAsUserPwd, const std::string& sConnectDBLink) { dvoid* tmp; sword status; status = OCIInitialize((ub4) OCI_THREADED | OCI_OBJECT, 0, 0, 0, 0); status = OCIHandleAlloc((dvoid *)NULL, (dvoid **)&ociHandles.envhp, (ub4)OCI_HTYPE_ENV, 52, (dvoid **)&tmp); status = OCIEnvInit(&ociHandles.envhp, (ub4)OCI_DEFAULT, 21, (dvoid **)&tmp); status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.errhp, (ub4)OCI_HTYPE_ERROR, 52, (dvoid **)&tmp); status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.srvhp, (ub4)OCI_HTYPE_SERVER, 52, (dvoid **)&tmp); if(checkerr(&ociHandles, OCIServerAttach(ociHandles.srvhp, ociHandles.errhp, (text *)sConnectDBLink.c_str(), (sb4)sConnectDBLink.length(), (ub4)OCI_DEFAULT)) != 0) { std::cout << "OCIServerAttach failed." << std::endl << std::endl; return; } status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.svchp, (ub4)OCI_HTYPE_SVCCTX, 52, (dvoid **)&tmp); /* set attribute server context in the service context */ status = OCIAttrSet((dvoid *) ociHandles.svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)ociHandles.srvhp, (ub4)0, (ub4)OCI_ATTR_SERVER, (OCIError *)ociHandles.errhp); /* allocate a user context handle */ status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.usrhp, (ub4)OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0); status = OCIAttrSet((dvoid *)ociHandles.usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)sConnectAsUser.c_str(), (ub4)sConnectAsUser.length(), OCI_ATTR_USERNAME, ociHandles.errhp); status = OCIAttrSet((dvoid *)ociHandles.usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)sConnectAsUserPwd.c_str(), (ub4)sConnectAsUserPwd.length(), OCI_ATTR_PASSWORD, ociHandles.errhp); if(checkerr(&ociHandles, OCISessionBegin(ociHandles.svchp, ociHandles.errhp, ociHandles.usrhp, OCI_CRED_RDBMS, OCI_DEFAULT)) != 0) { std::cout << "OCISessionBegin failed." << std::endl << std::endl; return; } status = OCIAttrSet((dvoid *)ociHandles.svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)ociHandles.usrhp, (ub4)0, OCI_ATTR_SESSION, ociHandles.errhp); status = OCIHandleAlloc((dvoid *)ociHandles.envhp, (dvoid **)&ociHandles.stmthp, (ub4)OCI_HTYPE_STMT, 50, (dvoid **)&tmp); connectedUser = sConnectAsUser; }
sword ConnectToDb(const char* const uid, const char* const pwd) { sword orc; /* allocate Server and Authentication (Session) handles */ orc = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, (ub4) OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0); orc = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &authp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0); /* attach to the server */ orc = OCIServerAttach(srvhp, errhp, (text *) 0, 0, (ub4) OCI_DEFAULT); orc = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) uid, (ub4) strlen((char *)uid), (ub4) OCI_ATTR_USERNAME, errhp); orc = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) pwd, (ub4) strlen((char *)pwd), (ub4) OCI_ATTR_PASSWORD, errhp); /* set the server attribute in the service context */ orc = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) srvhp, (ub4) 0, (ub4) OCI_ATTR_SERVER, errhp); /* log on */ orc = OCISessionBegin(svchp, errhp, authp, (ub4) OCI_CRED_RDBMS, (ub4) OCI_DEFAULT); CheckErr(errhp, orc); /* set the session attribute in the service context */ orc = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) authp, (ub4) 0, (ub4) OCI_ATTR_SESSION, errhp); return (orc); } /* ConnectToDb */
/* =begin --- OCISession#begin(svc [, credt [, mode]]) start user session under the specified server context. :svc ((<OCISvcCtx>)). :credt ((|OCI_CRED_RDBMS|)) or ((|OCI_CRED_EXT|)). Default value is ((|OCI_CRED_RDBMS|)). If you use ((|OCI_CRED_RDBMS|)), set ((<OCI_ATTR_USERNAME>)) and ((<OCI_ATTR_PASSWORD>)) in advance. :mode ((|OCI_DEFAULT|)), ((|OCI_MIGRATE|)), ((|OCI_SYSDBA|)), ((|OCI_SYSOPER|)), (((|OCI_SYSDBA|)) | ((|OCI_PRELIM_AUTH|))), or (((|OCI_SYSOPER|)) | ((|OCI_PRELIM_AUTH|))). Default value is ((|OCI_DEFAULT|)). If you need SYSDBA or SYSOPER privilege, use ((|OCI_SYSDBA|)) or ((|OCI_SYSOPER|)) respectively. correspond native OCI function: ((|OCISessionBegin|)) =end */ static VALUE oci8_session_begin(int argc, VALUE *argv, VALUE self) { VALUE vsvc, vcredt, vmode; oci8_handle_t *h; oci8_handle_t *svch; ub4 credt; ub4 mode; sword rv; rb_scan_args(argc, argv, "12", &vsvc, &vcredt, &vmode); Get_Handle(self, h); /* 0 */ Check_Handle(vsvc, OCISvcCtx, svch); /* 1 */ Get_Int_With_Default(argc, 2, vcredt, credt, OCI_NTV_SYNTAX); /* 2 */ Get_Int_With_Default(argc, 3, vmode, mode, OCI_DEFAULT); /* 3 */ rv = OCISessionBegin(svch->hp, h->errhp, h->hp, credt, mode); if (rv != OCI_SUCCESS) oci8_raise(h->errhp, rv, NULL); return self; }
int db_init(char *servername, char *username, char *passwd) { OCIEnvCreate(&myenvhp, OCI_THREADED|OCI_OBJECT, (dvoid *)0, 0, 0, 0, (size_t)0, (dvoid **)0); OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&mysrvhp, OCI_HTYPE_SERVER, 0, (dvoid **)0); OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&myerrhp, OCI_HTYPE_ERROR, 0, (dvoid **)0); /* 服务名 */ status = OCIServerAttach(mysrvhp, myerrhp, (const OraText*)servername, strlen(servername), OCI_DEFAULT); if (status != OCI_SUCCESS && status != OCI_SUCCESS_WITH_INFO) { checkerr(status, myerrhp); OCIServerDetach(mysrvhp, myerrhp, OCI_DEFAULT); return -1; } OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&mysvchp, OCI_HTYPE_SVCCTX, 0, (dvoid **)0); OCIAttrSet((dvoid *)mysvchp, OCI_HTYPE_SVCCTX, (dvoid *)mysrvhp, (ub4)0, OCI_ATTR_SERVER, myerrhp); OCIHandleAlloc((dvoid *)myenvhp, (dvoid **)&myusrhp, OCI_HTYPE_SESSION, 0, (dvoid **)0); OCIAttrSet((dvoid *)myusrhp, OCI_HTYPE_SESSION, (dvoid *)username, (ub4)strlen(username), OCI_ATTR_USERNAME, myerrhp); OCIAttrSet((dvoid *)myusrhp, OCI_HTYPE_SESSION, (dvoid *)passwd, (ub4)strlen(passwd), OCI_ATTR_PASSWORD, myerrhp); if(OCISessionBegin((dvoid *)mysvchp, myerrhp, myusrhp, OCI_CRED_RDBMS, OCI_DEFAULT) != OCI_SUCCESS) { DEBUG("Build the talking fail!\n"); OCIHandleFree((void *)myenvhp, OCI_HTYPE_ENV); return -1; } OCIAttrSet((dvoid *)mysvchp, OCI_HTYPE_SVCCTX, (dvoid *)myusrhp, (ub4)0, OCI_ATTR_SESSION, myerrhp); INFO("Oracle connect successful!\n"); return 0; }
int DataBase::_DB_Connect() { if( OCIInitialize((ub4) OCI_OBJECT, (dvoid *)0,(dvoid * (*)(dvoid *, size_t)) 0,(dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0 ) ) { strcpy(errStr, "FAILED:OCIInitialize"); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIEnvInit( (OCIEnv **) &(DBSession->envhp), OCI_DEFAULT, (size_t) 0, (dvoid **) 0 ) ) { strcpy(errStr, "FAILED:OCIEnvInit"); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIHandleAlloc( (dvoid *) (DBSession->envhp), (dvoid **) &(DBSession->errhp), OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0 ) ) { strcpy(errStr, "FAILED:OCIHandleAlloc On Error Handle"); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIHandleAlloc( (dvoid *) (DBSession->envhp), (dvoid **) &(DBSession->srvhp), OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0 ) ) { strcpy(errStr, "FAILED:OCIHandleAlloc On Srv Handle"); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIHandleAlloc( (dvoid *) (DBSession->envhp), (dvoid **) &(DBSession->svchp), OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0 ) ) { strcpy(errStr, "FAILED:OCIHandleAlloc On Service Context Handle"); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIServerAttach( DBSession->srvhp, DBSession->errhp, (text *)DB_server, strlen(DB_server), 0 ) ) { sprintf(errStr, "FAILED:OCIServerAttach (Can't connect to %s)", DB_server ); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIAttrSet( (dvoid *) (DBSession->svchp), OCI_HTYPE_SVCCTX, (dvoid *)(DBSession->srvhp), (ub4) 0, OCI_ATTR_SERVER, (OCIError *) (DBSession->errhp) ) ) { strcpy(errStr, "FAILED:OCIAttrSet" ); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIHandleAlloc((dvoid *) (DBSession->envhp), (dvoid **)&(DBSession->authp), (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0 ) ) { strcpy(errStr, "FAILED:OCIHandleAlloc On Authentication Handle" ); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIAttrSet((dvoid *) (DBSession->authp), (ub4) OCI_HTYPE_SESSION, (dvoid *) DB_user, (ub4) strlen(DB_user), (ub4) OCI_ATTR_USERNAME, DBSession->errhp ) ) { strcpy(errStr, "FAILED:OCIAttrSet on USERID" ); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } if( OCIAttrSet((dvoid *) (DBSession->authp), (ub4) OCI_HTYPE_SESSION, (dvoid *)DB_password, (ub4) strlen(DB_password), (ub4) OCI_ATTR_PASSWORD, DBSession->errhp ) ) { strcpy(errStr, "FAILED:OCIAttrSet on PASSWD"); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } //认证用户并建立会话 if( _DB_CheckErr(OCISessionBegin ( DBSession->svchp, DBSession->errhp, DBSession->authp, OCI_CRED_RDBMS, (ub4) OCI_DEFAULT)) < 0 ) { return OCI_ERROR; } if( OCIAttrSet((dvoid *) (DBSession->svchp), (ub4) OCI_HTYPE_SVCCTX, (dvoid *) (DBSession->authp), (ub4) 0, (ub4) OCI_ATTR_SESSION, (DBSession->errhp) ) ) { strcpy(errStr, "FAILED:OCIAttrSet on session" ); cout<<"[ "<<__FILE__<<" ] [ "<<__LINE__<<" ] errStr = "<<errStr<<endl; return OCI_ERROR; } //申请语句句柄 if( _DB_CheckErr(OCIHandleAlloc( (dvoid *) (DBSession->envhp), (dvoid **) &(DBSession->stmthp), OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0))<0) { return OCI_ERROR; } if( _DB_CheckErr(OCIHandleAlloc( (dvoid *) (DBSession->envhp), (dvoid **) &(DBSession->selectp), OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0)) < 0 ) { return OCI_ERROR; } return OCI_SUCCESS; }
OWConnection::OWConnection( const char* pszUserIn, const char* pszPasswordIn, const char* pszServerIn ) { pszUser = CPLStrdup( pszUserIn ); pszPassword = CPLStrdup( pszPasswordIn ); pszServer = CPLStrdup( pszServerIn ); hEnv = NULL; hError = NULL; hSvcCtx = NULL; hDescribe = NULL; hNumArrayTDO = NULL; hGeometryTDO = NULL; hGeoRasterTDO = NULL; hElemArrayTDO = NULL; hOrdnArrayTDO = NULL; bSuceeeded = false; nCharSize = 1; // ------------------------------------------------------ // Operational Systems's authentication option // ------------------------------------------------------ const char* pszUserId = "/"; ub4 eCred = OCI_CRED_RDBMS; if( EQUAL(pszServer, "") && EQUAL(pszPassword, "") && EQUAL(pszUser, "") ) { eCred = OCI_CRED_EXT; } else { pszUserId = pszUser; } // ------------------------------------------------------ // Initialize Environment handler // ------------------------------------------------------ if( OCIEnvCreate( &hEnv, (ub4) ( OCI_DEFAULT | OCI_OBJECT | OCI_THREADED ), (dvoid *) 0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t)) 0, (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0), NULL ) { return; } // ------------------------------------------------------ // Initialize Error handler // ------------------------------------------------------ if( CheckError( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hError, OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0), NULL ) ) { return; } // ------------------------------------------------------ // Initialize Server Context // ------------------------------------------------------ if( CheckError( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hSvcCtx, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0), hError ) ) { return; } // ------------------------------------------------------ // Allocate Server and Authentication (Session) handler // ------------------------------------------------------ if( CheckError( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hServer, (ub4) OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0), hError ) ) { return; } if( CheckError( OCIHandleAlloc((dvoid *) hEnv, (dvoid **)&hSession, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0), hError ) ) { return; } // ------------------------------------------------------ // Attach to the server // ------------------------------------------------------ if( CheckError( OCIServerAttach( hServer, hError, (text*) pszServer, strlen((char*) pszServer), 0), hError ) ) { return; } if( CheckError( OCIAttrSet((dvoid *) hSession, (ub4) OCI_HTYPE_SESSION, (dvoid *) pszUserId, (ub4) strlen( pszUserId), (ub4) OCI_ATTR_USERNAME, hError), hError ) ) { return; } if( CheckError( OCIAttrSet((dvoid *) hSession, (ub4) OCI_HTYPE_SESSION, (dvoid *) pszPassword, (ub4) strlen((char *) pszPassword), (ub4) OCI_ATTR_PASSWORD, hError), hError ) ) { return; } if( CheckError( OCIAttrSet( (dvoid *) hSvcCtx, OCI_HTYPE_SVCCTX, (dvoid *)hServer, (ub4) 0, OCI_ATTR_SERVER, (OCIError *) hError), hError ) ) { return; } // ------------------------------------------------------ // Initialize Session // ------------------------------------------------------ if( CheckError( OCISessionBegin(hSvcCtx, hError, hSession, eCred, (ub4) OCI_DEFAULT), hError ) ) { return; } // ------------------------------------------------------ // Initialize Service // ------------------------------------------------------ if( CheckError( OCIAttrSet((dvoid *) hSvcCtx, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) hSession, (ub4) 0, (ub4) OCI_ATTR_SESSION, hError), hError ) ) { return; } bSuceeeded = true; // ------------------------------------------------------ // Get Character Size based on current Locale // ------------------------------------------------------ OCINlsNumericInfoGet( hEnv, hError, &nCharSize, OCI_NLS_CHARSET_MAXBYTESZ ); // ------------------------------------------------------ // Get Server Version // ------------------------------------------------------ char szVersionTxt[OWTEXT]; OCIServerVersion ( hSvcCtx, hError, (text*) szVersionTxt, (ub4) OWTEXT, (ub1) OCI_HTYPE_SVCCTX ); nVersion = OWParseServerVersion( szVersionTxt ); // ------------------------------------------------------ // Initialize/Describe types // ------------------------------------------------------ CheckError( OCIHandleAlloc( (dvoid*) hEnv, (dvoid**) (dvoid*) &hDescribe, (ub4) OCI_HTYPE_DESCRIBE, (size_t) 0, (dvoid**) NULL ), hError ); hNumArrayTDO = DescribeType( SDO_NUMBER_ARRAY ); hGeometryTDO = DescribeType( SDO_GEOMETRY ); hGeoRasterTDO = DescribeType( SDO_GEORASTER ); hElemArrayTDO = DescribeType( SDO_ELEM_INFO_ARRAY); hOrdnArrayTDO = DescribeType( SDO_ORDINATE_ARRAY); if( nVersion > 10 ) { hPCTDO = DescribeType( SDO_PC ); } }
static int yada_oracle_connect(yada_t *_yada, char *yada_user, char *yada_pass) { char errbuf[1024]; sb4 errcode; /* check cached user/pass */ if(_yada->_mod->dbuser && yada_user && strcmp(_yada->_mod->dbuser, yada_user)) { free(_yada->_mod->dbuser); _yada->_mod->dbuser = NULL; } if(_yada->_mod->dbpass && yada_pass && strcmp(_yada->_mod->dbpass, yada_pass)) { free(_yada->_mod->dbpass); _yada->_mod->dbpass = NULL; } /* cache user/pass */ if(!_yada->_mod->dbuser) { if(!yada_user) { _yada_set_err(_yada, -1, "invalid username"); return(0); } if(!(_yada->_mod->dbuser = strdup(yada_user))) { _yada_set_err(_yada, -1, strerror(errno)); return(0); } } if(!_yada->_mod->dbpass) { if(!yada_user) { _yada_set_err(_yada, -1, "invalid username"); return(0); } if(!(_yada->_mod->dbpass = strdup(yada_pass))) { _yada_set_err(_yada, -1, strerror(errno)); return(0); } } /* create environment */ if(OCIEnvCreate(&_yada->_mod->env, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL) != OCI_SUCCESS) { _yada_set_err(_yada, -1, "OCIEnvCreate failed"); return(0); } /* allocate error handle */ if(OCIHandleAlloc(_yada->_mod->env, (dvoid**)&_yada->_mod->err, OCI_HTYPE_ERROR, 0, NULL) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->env, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ENV); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* allocate server handle */ if(OCIHandleAlloc(_yada->_mod->env, (dvoid**)&_yada->_mod->srv, OCI_HTYPE_SERVER, 0, NULL) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->env, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ENV); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* attach to server */ if(OCIServerAttach(_yada->_mod->srv, _yada->_mod->err, (text*)_yada->dbstr, strlen(_yada->dbstr), OCI_DEFAULT) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->err, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* allocate context handle */ if(OCIHandleAlloc(_yada->_mod->env, (dvoid**)&_yada->_mod->ctx, OCI_HTYPE_SVCCTX, 0, NULL) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->env, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ENV); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* set server attribute of context handle */ if(OCIAttrSet(_yada->_mod->ctx, OCI_HTYPE_SVCCTX, _yada->_mod->srv, 0, OCI_ATTR_SERVER, _yada->_mod->err) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->err, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* allocate session handle */ if(OCIHandleAlloc(_yada->_mod->env, (dvoid**)&_yada->_mod->ses, OCI_HTYPE_SESSION, 0, NULL) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->env, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ENV); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* set username/password attributes of session handle */ if(OCIAttrSet(_yada->_mod->ses, OCI_HTYPE_SESSION, (dvoid*)_yada->_mod->dbuser, strlen(_yada->_mod->dbuser), OCI_ATTR_USERNAME, _yada->_mod->err) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->err, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } if(OCIAttrSet(_yada->_mod->ses, OCI_HTYPE_SESSION, (dvoid*)_yada->_mod->dbpass, strlen(_yada->_mod->dbpass), OCI_ATTR_PASSWORD, _yada->_mod->err) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->err, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* start the session */ if(OCISessionBegin(_yada->_mod->ctx, _yada->_mod->err, _yada->_mod->ses, OCI_CRED_RDBMS, OCI_DEFAULT) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->err, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } /* set session attribute of context handle */ if(OCIAttrSet(_yada->_mod->ctx, OCI_HTYPE_SVCCTX, _yada->_mod->ses, 0, OCI_ATTR_SESSION, _yada->_mod->err) != OCI_SUCCESS) { OCIErrorGet(_yada->_mod->err, 1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR); _yada_set_err(_yada, errcode, errbuf); _yada->disconnect(_yada); return(0); } return(1); }
// open a connection to the database void open_db_connection (TM_CONN_HNDL *conn_hndl) { int ret_val; TM_CONNECTION **conn; // Allocate memory for the connection handle object. conn = (TM_CONNECTION **) conn_hndl; *conn = (TM_CONNECTION *) tman_malloc(sizeof(TM_CONNECTION)); /* Initialize the environment */ ret_val = OCIEnvCreate( &(*(TM_CONNECTION **) conn)->envhp, OCI_THREADED | OCI_OBJECT, NULL, NULL, NULL, NULL, 0, NULL); if( ret_val != OCI_SUCCESS ) { printf("Internal Error: Could not create environment to open a connection to the DBMS. "); printf("Please contact Velara Technical Support. Shutting down...\n"); exit(EXIT_FAILURE); } // allocate the neccessary handles to open and maintain a connection to the database OCIHandleAlloc ( (*conn)->envhp, &((*conn)->errhp), OCI_HTYPE_ERROR, 0, NULL); OCIHandleAlloc ( (*conn)->envhp, &((*conn)->svchp), OCI_HTYPE_SVCCTX, 0, NULL); OCIHandleAlloc ( (*conn)->envhp, &((*conn)->srvhp), OCI_HTYPE_SERVER, 0, NULL); OCIHandleAlloc ( (*conn)->envhp, &((*conn)->authp), OCI_HTYPE_SESSION, 0, NULL); ASSERT((*conn)->errhp); ASSERT((*conn)->svchp); ASSERT((*conn)->srvhp); ASSERT((*conn)->authp); // initialize the service context handle ret_val = OCIAttrSet ((*conn)->svchp, OCI_HTYPE_SVCCTX, (*conn)->srvhp, 0, OCI_ATTR_SERVER, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf("Internal Error: Could not initialize the service context handle. "); printf("Please contact Velara Technical Support. Shutting down...\n"); exit(EXIT_FAILURE); } // set the username in the user session handle ret_val = OCIAttrSet ((*conn)->authp, OCI_HTYPE_SESSION, username, strlen(username), OCI_ATTR_USERNAME, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf("Internal Error: Could not initialize the user session handle. "); printf("Please contact Velara Technical Support. Shutting down...\n"); exit(EXIT_FAILURE); } // set the password in the user session handle ret_val = OCIAttrSet ((*(TM_CONNECTION **)conn)->authp, OCI_HTYPE_SESSION, password, strlen(password), OCI_ATTR_PASSWORD, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf("Internal Error: Could not initialize the user session handle. "); printf("Please contact Velara Technical Support. Shutting down...\n"); exit(EXIT_FAILURE); } // attach the error handle to the server handle ret_val = OCIServerAttach( (*conn)->srvhp, (*conn)->errhp, hostdb, strlen(hostdb), OCI_DEFAULT); if( ret_val != OCI_SUCCESS ) { printf("Internal Error: startup failed. Unable to attach error handle to server. "); printf("Please contact Velara Technical Support. Shutting down...\n"); exit (EXIT_FAILURE); } // log on to the database ret_val = OCISessionBegin ((*conn)->svchp, (*conn)->errhp, (*conn)->authp, OCI_CRED_RDBMS, OCI_DEFAULT); if( ret_val != OCI_SUCCESS ) { printf("Error: unable to log on to %s. ", hostdb); printf("Verify that the username and password are valid for this database.\n"); exit(EXIT_FAILURE); } // set the user session attribute in the service context handle ret_val = OCIAttrSet ((*conn)->svchp, OCI_HTYPE_SVCCTX, (*conn)->authp, 0, OCI_ATTR_SESSION, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf("Internal Error: unable to set user session attribute in service context handle. "); printf("Please contact Velara Technical Support. Shutting down...\n"); exit(EXIT_FAILURE); } }
void init_conn(OCI_CONNECTION **conn) { int ret_val; *conn = (OCI_CONNECTION *) malloc(sizeof(OCI_CONNECTION)); /* Initialize the environment */ ret_val = OCIEnvCreate(&(*(OCI_CONNECTION**)conn)->envhp, OCI_THREADED | OCI_OBJECT, NULL, NULL, NULL, NULL, 0, NULL); if( ret_val != OCI_SUCCESS ) { printf("Error: unable to create an environment to open a connection to the DBMS.\n"); exit(EXIT_FAILURE); } // allocate the neccessary handles to open and maintain a connection to the database OCIHandleAlloc ((*conn)->envhp, &((*conn)->errhp), OCI_HTYPE_ERROR, 0, NULL); OCIHandleAlloc ((*conn)->envhp, &((*conn)->svchp), OCI_HTYPE_SVCCTX, 0, NULL); OCIHandleAlloc ((*conn)->envhp, &((*conn)->srvhp), OCI_HTYPE_SERVER, 0, NULL); OCIHandleAlloc ((*conn)->envhp, &((*conn)->authp), OCI_HTYPE_SESSION, 0, NULL); // initialize the service context handle ret_val = OCIAttrSet ((*conn)->svchp, OCI_HTYPE_SVCCTX, (*conn)->srvhp, 0, OCI_ATTR_SERVER, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf ("Error: unable to initialize the service context handle.\n"); exit(EXIT_FAILURE); } // set the username in the user session handle ret_val = OCIAttrSet ((*conn)->authp, OCI_HTYPE_SESSION, username, strlen(username), OCI_ATTR_USERNAME, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf ("Error: unable to initialize the user session handle.\n"); exit(EXIT_FAILURE); } // set the password in the user session handle ret_val = OCIAttrSet ((*conn)->authp, OCI_HTYPE_SESSION, password, strlen(password), OCI_ATTR_PASSWORD, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf ("Error: unable to initialize the user session handle.\n"); exit(EXIT_FAILURE); } // attach the error handle to the server handle ret_val = OCIServerAttach((*conn)->srvhp, (*conn)->errhp, (unsigned char *)connect_string, strlen(connect_string), OCI_DEFAULT); if( ret_val != OCI_SUCCESS ) { printf ("Error: unable to attach error handle to server.\n"); exit (EXIT_FAILURE); } // log on to the database ret_val = OCISessionBegin((*conn)->svchp, (*conn)->errhp, (*conn)->authp, OCI_CRED_RDBMS, OCI_DEFAULT); if( ret_val != OCI_SUCCESS ) { printf ("Error: unable to log on to %s.\n", connect_string); printf ("Verify that the username and password are valid for this database.\n"); exit(EXIT_FAILURE); } // set the user session attribute in the service context handle ret_val = OCIAttrSet((*conn)->svchp, OCI_HTYPE_SVCCTX, (*conn)->authp, 0, OCI_ATTR_SESSION, (*conn)->errhp); if( ret_val != OCI_SUCCESS ) { printf ("Error: unable to set user session attribute in service context handle.\n"); exit(EXIT_FAILURE); } }
gboolean oracle_session_open (GSQLEOracleSession *oracle_session, gchar *username, gchar *password, gchar *database, gchar *buffer) { GSQL_TRACE_FUNC; unsigned char buf[64]; gint ret; /* initialize the mode to be the threaded and object environment */ if ( OCIEnvNlsCreate(&(oracle_session->envhp), OCI_THREADED|OCI_OBJECT, (dvoid *)0, 0, 0, 0, (size_t) 0, (dvoid **)0, 0, 0) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIEnvNlsCreate... failed"); return FALSE; }; /* allocate a server handle */ if ( OCIHandleAlloc ((dvoid *)(oracle_session->envhp), (dvoid **)&(oracle_session->srvhp), OCI_HTYPE_SERVER, 0, (dvoid **) 0) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIHandleAlloc (allocate a server handle)... failed"); return FALSE; }; /* allocate an error handle */ if ( OCIHandleAlloc ((dvoid *)(oracle_session->envhp), (dvoid **)&(oracle_session->errhp), OCI_HTYPE_ERROR, 0, (dvoid **) 0) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIHandleAlloc (allocate an error handle)... failed"); return FALSE; }; /* create a server context */ if ( OCIServerAttach (oracle_session->srvhp, oracle_session->errhp, (text *) database, g_utf8_strlen (database, 64), OCI_DEFAULT) == OCI_ERROR ) { g_snprintf (buffer, 256, "%s", oracle_get_error_string(oracle_session->errhp) ); return FALSE; }; /* allocate a service handle */ if ( OCIHandleAlloc ((dvoid *) (oracle_session->envhp), (dvoid **)&(oracle_session->svchp), OCI_HTYPE_SVCCTX, 0, (dvoid **) 0) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIHandleAlloc (allocate a service handle)... failed"); return FALSE; }; /* set the server attribute in the service context handle*/ if ( OCIAttrSet ((dvoid *) oracle_session->svchp, OCI_HTYPE_SVCCTX, (dvoid *) oracle_session->srvhp, (ub4) 0, OCI_ATTR_SERVER, oracle_session->errhp) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIAttrSet... failed"); return FALSE; }; /* allocate a user session handle */ if ( OCIHandleAlloc ((dvoid *) (oracle_session->envhp), (dvoid **)&(oracle_session->usrhp), OCI_HTYPE_SESSION, 0, (dvoid **) 0) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIHandleAlloc (allocate a user session handle)... failed"); return FALSE; }; /* set user name attribute in user session handle */ if ( OCIAttrSet ((dvoid *) oracle_session->usrhp, OCI_HTYPE_SESSION, (dvoid *) username, (ub4) g_utf8_strlen(username, 64), OCI_ATTR_USERNAME, oracle_session->errhp) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIAttrSet (OCI_ATTR_USERNAME)... failed"); return FALSE; }; /* set password attribute in user session handle */ if ( OCIAttrSet ((dvoid *)(oracle_session->usrhp), OCI_HTYPE_SESSION, (dvoid *)password, (ub4) g_utf8_strlen(password, 64), OCI_ATTR_PASSWORD, oracle_session->errhp) == OCI_ERROR ) { g_snprintf (buffer, 256,"OCIAttrSet(OCI_ATTR_PASSWORD)... failed"); return FALSE; }; /* make the connection*/ if ( OCISessionBegin ((dvoid *) oracle_session->svchp, oracle_session->errhp, oracle_session->usrhp, OCI_CRED_RDBMS, oracle_session->mode) == OCI_ERROR ) { g_snprintf (buffer, 256, "%s", oracle_get_error_string(oracle_session->errhp) ); return FALSE; }; oracle_client_info(oracle_session->client_version); if ( OCIAttrSet ((dvoid *)(oracle_session->usrhp), OCI_HTYPE_SESSION, (dvoid *) oracle_session->client_version, (ub4) g_utf8_strlen(oracle_session->client_version, 64), OCI_ATTR_CLIENT_INFO, oracle_session->errhp) == OCI_ERROR ) { g_snprintf (buffer, 256, "OCIAttrSet(OCI_ATTR_CLIENT_INFO)... failed"); return FALSE; }; /* set the user session attribute in the service context handle*/ if ( OCIAttrSet ((dvoid *)oracle_session->svchp, OCI_HTYPE_SVCCTX, (dvoid *) oracle_session->usrhp, (ub4) 0, OCI_ATTR_SESSION, oracle_session->errhp) == OCI_ERROR ) { g_snprintf (buffer, 256, "OCIAttrSet (OCI_ATTR_SESSION)... failed"); return FALSE; }; OCIServerVersion ((dvoid *) oracle_session->svchp, oracle_session->errhp, oracle_session->server_version, 1024, OCI_HTYPE_SVCCTX); GSQL_DEBUG ("oracle_session->server_version=[%s]", oracle_session->server_version); oracle_session->dbms_output = FALSE; oracle_session->debug_mode = FALSE; return TRUE; };
/*--------------------------------------------------------------------- * connect_db - Connect to the database. *---------------------------------------------------------------------*/ void connect_db(conn_info_t *conn, oci_t ** ociptr, ub2 char_csid, ub2 nchar_csid) { oci_t *ocip; printf ("\nConnect to Oracle as %.*s/%.*s@%.*s\n", conn->userlen, conn->user, conn->passwlen, conn->passw, conn->dbnamelen, conn->dbname); if (char_csid && nchar_csid) printf ("using char csid=%d and nchar csid=%d\n", char_csid, nchar_csid); ocip = (oci_t *)malloc(sizeof(oci_t)); ocip->attached = FALSE; /* Create UTF8 environment */ if (OCIEnvNlsCreate(&ocip->envp, OCI_OBJECT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0, char_csid, nchar_csid)) { ocierror(ocip, (char *)"OCIEnvCreate() failed", TRUE); } if (OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **) &ocip->errp, (ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0)) { ocierror(ocip, (char *)"OCIHandleAlloc(OCI_HTYPE_ERROR) failed", TRUE); } /* allocate the server handle */ OCICALL(ocip, OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **) &ocip->srvp, OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0)); /* create a server context */ OCICALL(ocip, OCIServerAttach (ocip->srvp, ocip->errp, (oratext *)conn->dbname, (sb4)conn->dbnamelen, OCI_DEFAULT)); /* allocate the service handle */ OCICALL(ocip, OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **) &ocip->svcp, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0)); /* set attribute server context in the service context */ OCICALL(ocip, OCIAttrSet((dvoid *) ocip->svcp, OCI_HTYPE_SVCCTX, (dvoid *) ocip->srvp, (ub4) 0, OCI_ATTR_SERVER, (OCIError *) ocip->errp)); /* allocate a session handle */ OCICALL(ocip, OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **)&ocip->authp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0)); /* set the username in the session */ OCICALL(ocip, OCIAttrSet((dvoid *) ocip->authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) conn->user, (ub4) conn->userlen, (ub4) OCI_ATTR_USERNAME, ocip->errp)); /* set the password in the session */ OCICALL(ocip, OCIAttrSet((dvoid *) ocip->authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) conn->passw, (ub4) conn->passwlen, (ub4) OCI_ATTR_PASSWORD, ocip->errp)); OCICALL(ocip, OCISessionBegin(ocip->svcp, ocip->errp, ocip->authp, OCI_CRED_RDBMS, (ub4)OCI_DEFAULT)); OCICALL(ocip, OCIAttrSet((dvoid *) ocip->svcp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) ocip->authp, (ub4) 0, (ub4) OCI_ATTR_SESSION, ocip->errp)); if (OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **) &ocip->stmtp, (ub4) OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0)) { ocierror(ocip, (char *)"OCIHandleAlloc(OCI_HTYPE_STMT) failed", TRUE); } if (OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **) &ocip->stmt2p, (ub4) OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0)) { ocierror(ocip, (char *)"OCIHandleAlloc(OCI_HTYPE_STMT-2) failed", TRUE); } if (*ociptr == (oci_t *)NULL) { *ociptr = ocip; } /* restore the interrupt signal handler */ signal(SIGINT, SIG_DFL); }
void qdbl_oracle_session_start(struct QdblOracleConnection* self, const char* username, const char* password) { /* variables */ sword status; /* create a service handle */ status = OCIHandleAlloc(self->ocienv, (void**)&(self->ocicontext), OCI_HTYPE_SVCCTX, 0, 0); if( qdbl_oracle_error(self->ocierr, status, self->logger, "create svcctx") ) { /* set to connect */ self->ocicontext = 0; return; } /* setting the server to the service context */ status = OCIAttrSet(self->ocicontext, OCI_HTYPE_SVCCTX, self->ocisvr, 0, OCI_ATTR_SERVER, self->ocierr); if( qdbl_oracle_error(self->ocierr, status, self->logger, "set svcctx") ) { OCIHandleFree(self->ocicontext, OCI_HTYPE_SVCCTX); /* set to connect */ self->ocicontext = 0; return; } /* create session handle */ status = OCIHandleAlloc(self->ocienv, (void**)&(self->ocisession), OCI_HTYPE_SESSION, 0, 0); if( qdbl_oracle_error(self->ocierr, status, self->logger, "create session") ) { OCIHandleFree(self->ocicontext, OCI_HTYPE_SVCCTX); /* set to connect */ self->ocisession = 0; self->ocicontext = 0; return; } /* setting the session to the service context */ status = OCIAttrSet( self->ocicontext, OCI_HTYPE_SVCCTX, self->ocisession, 0, OCI_ATTR_SESSION, self->ocierr ); if( qdbl_oracle_error(self->ocierr, status, self->logger, "set session") ) { OCIHandleFree(self->ocisession, OCI_HTYPE_SESSION); OCIHandleFree(self->ocicontext, OCI_HTYPE_SVCCTX); /* set to connect */ self->ocisession = 0; self->ocicontext = 0; return; } /* setting username and password */ status = OCIAttrSet(self->ocisession, OCI_HTYPE_SESSION, (char*)username, strlen(username), OCI_ATTR_USERNAME, self->ocierr); if( qdbl_oracle_error(self->ocierr, status, self->logger, "set username") ) { OCIHandleFree(self->ocisession, OCI_HTYPE_SESSION); OCIHandleFree(self->ocicontext, OCI_HTYPE_SVCCTX); /* set to connect */ self->ocisession = 0; self->ocicontext = 0; return; } status = OCIAttrSet(self->ocisession, OCI_HTYPE_SESSION, (char*)password, strlen(password), OCI_ATTR_PASSWORD, self->ocierr); if( qdbl_oracle_error(self->ocierr, status, self->logger, "set password") ) { OCIHandleFree(self->ocisession, OCI_HTYPE_SESSION); OCIHandleFree(self->ocicontext, OCI_HTYPE_SVCCTX); /* set to connect */ self->ocisession = 0; self->ocicontext = 0; return; } /* begin session and authenticating */ status = OCISessionBegin(self->ocicontext, self->ocierr, self->ocisession, OCI_CRED_RDBMS, OCI_DEFAULT); if( qdbl_oracle_error(self->ocierr, status, self->logger, "begin session") ) { OCIHandleFree(self->ocisession, OCI_HTYPE_SESSION); OCIHandleFree(self->ocicontext, OCI_HTYPE_SVCCTX); /* set to connect */ self->ocisession = 0; self->ocicontext = 0; return; } /* success */ qclogger_log(self->logger, LOGMSG_SUCCESS, "ORAC", "new session started" ); }
void* COracleDB::oracle_connect( const char* addr, const unsigned short port, const char *user, const char* pass, const char* db_name ) { char service[256] = {0}; sprintf( service, "//%s:%d/%s", addr, port, db_name ); ORACLE_HANDLE* handle = new ORACLE_HANDLE; memset( handle, 0, sizeof(ORACLE_HANDLE) ); int status = 0; OCIEnvCreate( ( OCIEnv ** ) & ( handle->envhp ), ( ub4 ) OCI_THREADED, ( dvoid * ) 0, ( dvoid * (*)( dvoid *, size_t )) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0 , (size_t) 0, (dvoid **) 0 ); ( void ) OCIHandleAlloc( ( dvoid * ) ( handle->envhp ), ( dvoid ** ) & ( handle->errhp ), OCI_HTYPE_ERROR, ( size_t ) 0, ( dvoid ** ) 0 ); /* server contexts */ ( void ) OCIHandleAlloc( ( dvoid * ) ( handle->envhp ), ( dvoid ** ) & ( handle->srvhp ), OCI_HTYPE_SERVER, ( size_t ) 0, ( dvoid ** ) 0 ); // service context ( void ) OCIHandleAlloc( ( dvoid * ) ( handle->envhp ), ( dvoid ** ) & ( handle->svchp ), OCI_HTYPE_SVCCTX, ( size_t ) 0, ( dvoid ** ) 0 ); status = OCIServerAttach( handle->srvhp, handle->errhp, ( text * ) service, strlen( ( const char * ) service ), 0 ); status = checkerr( handle->errhp, status, _errinfo ); if ( status != 0 ) { _handle = handle; oracle_close(); _handle = NULL; return NULL; } /* set attribute server context in the service context */ OCIAttrSet( ( dvoid * ) handle->svchp, OCI_HTYPE_SVCCTX, ( dvoid * ) handle->srvhp, ( ub4 ) 0, OCI_ATTR_SERVER, ( OCIError * ) handle->errhp ); OCIHandleAlloc( ( dvoid * ) handle->envhp, ( dvoid ** ) & ( handle->authp ), ( ub4 ) OCI_HTYPE_SESSION, ( size_t ) 0, ( dvoid ** ) 0 ); ( void ) OCIAttrSet( ( dvoid * ) handle->authp, ( ub4 ) OCI_HTYPE_SESSION, ( dvoid * ) user, ( ub4 ) strlen( ( char * ) user ), ( ub4 ) OCI_ATTR_USERNAME, handle->errhp ); OCIAttrSet( ( dvoid * ) handle->authp, ( ub4 ) OCI_HTYPE_SESSION, ( dvoid * ) pass, ( ub4 ) strlen( ( char * ) pass ), ( ub4 ) OCI_ATTR_PASSWORD, handle->errhp ); status = OCISessionBegin( handle->svchp, handle->errhp, handle->authp, OCI_CRED_RDBMS, ( ub4 ) OCI_DEFAULT ); status = checkerr( handle->errhp, status, _errinfo ); if ( status != 0 ) { _handle = handle; oracle_close(); _handle = NULL; return NULL; } status = OCIAttrSet( ( dvoid * ) ( handle->svchp ), ( ub4 ) OCI_HTYPE_SVCCTX, ( dvoid * ) ( handle->authp ), ( ub4 ) 0, ( ub4 ) OCI_ATTR_SESSION, handle->errhp ); status = checkerr( handle->errhp, status, _errinfo ); if ( status ) { _handle = handle; oracle_close(); _handle = NULL; return NULL; } status = OCIHandleAlloc( ( dvoid * ) ( handle->envhp ), ( dvoid ** ) & ( handle->stmthp ), OCI_HTYPE_STMT, ( size_t ) 0, ( dvoid ** ) 0 ); status = checkerr( handle->errhp, status, _errinfo ); if ( status ) { _handle = handle; oracle_close(); _handle = NULL; return NULL; } /* OCIAttrSet ( handle->srvhp, (ub4) OCI_HTYPE_SERVER, (dvoid *) 0, (ub4) 0, (ub4) OCI_ATTR_NONBLOCKING_MODE, handle->errhp) ; */ return ( void* ) handle; }
bool OracleConnection::Open( String const &strServer, String const &strUsername, String const &strPassword ) { StartTrace(OracleConnection.Open); if ( fStatus < eHandlesAllocated ) { SYSERROR("Allocation of OCI handles failed, can not connect to server [" << strServer << "] with user [" << strUsername << "]!"); return false; } if ( fStatus > eHandlesAllocated ) { SYSERROR("tried to open already opened connection to server [" << strServer << "] and user [" << strUsername << "]!"); return false; } text const *server( reinterpret_cast<const text *> ( (const char *) strServer ) ); text const *username( reinterpret_cast<const text *> ( (const char *) strUsername ) ); text const *password( reinterpret_cast<const text *> ( (const char *) strPassword ) ); String strErr( 128L ); // --- attach server if ( checkError( OCIServerAttach( fSrvhp.getHandle(), fErrhp.getHandle(), server, strlen( (const char *) server ), (ub4) OCI_DEFAULT ), strErr ) ) { SystemLog::Error( String( "FAILED: OCIServerAttach() to server [" ) << strServer << "] failed (" << strErr << ")" ); return false; } fStatus = eServerAttached; // --- set attribute server context in the service context if ( checkError( OCIAttrSet( fSvchp.getHandle(), (ub4) OCI_HTYPE_SVCCTX, fSrvhp.getHandle(), (ub4) 0, (ub4) OCI_ATTR_SERVER, fErrhp.getHandle() ), strErr ) ) { SystemLog::Error( String( "FAILED: OCIAttrSet(): setting attribute <server> into the service context failed (" ) << strErr << ")" ); return false; } // --- set attributes in the authentication handle if ( checkError( OCIAttrSet( fUsrhp.getHandle(), (ub4) OCI_HTYPE_SESSION, (dvoid *) username, (ub4) strlen( (const char *) username ), (ub4) OCI_ATTR_USERNAME, fErrhp.getHandle() ), strErr ) ) { SystemLog::Error( String( "FAILED: OCIAttrSet(): setting attribute <username> in the authentication handle failed (" ) << strErr << ")" ); return false; } if ( checkError( OCIAttrSet( fUsrhp.getHandle(), (ub4) OCI_HTYPE_SESSION, (dvoid *) password, (ub4) strlen( (const char *) password ), (ub4) OCI_ATTR_PASSWORD, fErrhp.getHandle() ), strErr ) ) { SystemLog::Error( String( "FAILED: OCIAttrSet(): setting attribute <password> in the authentication handle failed (" ) << strErr << ")" ); return false; } if ( checkError( OCISessionBegin( fSvchp.getHandle(), fErrhp.getHandle(), fUsrhp.getHandle(), OCI_CRED_RDBMS, (ub4) OCI_DEFAULT ), strErr ) ) { SystemLog::Error( String( "FAILED: OCISessionBegin() with user [" ) << strUsername << "] failed (" << strErr << ")" ); return false; } Trace( "connected to oracle as " << strUsername ) // --- Set the authentication handle into the Service handle if ( checkError( OCIAttrSet( fSvchp.getHandle(), (ub4) OCI_HTYPE_SVCCTX, fUsrhp.getHandle(), (ub4) 0, OCI_ATTR_SESSION, fErrhp.getHandle() ), strErr ) ) { SystemLog::Error( String( "FAILED: OCIAttrSet(): setting attribute <session> into the service context failed (" ) << strErr << ")" ); return false; } fStatus = eSessionValid; return true; }
/* * Connect to database. * db_login - database login * db_password - database password * return : DB_CONNECT_NEW_CONNECTION if a new connection was estabilished * DB_CONNECT_ALREADY_CONNECTED or * DB_CONNECT_ERROR */ int oralog_db_connect(char *db_login, char *db_password, int quiet) { int print_errors = (quiet) ? 0 : 1; int errors = 0; sword retval = 0; OCIError *hp_error = NULL; pthread_mutex_lock(&oralog_oper_lock); if (oralog_is_connected()) { debug("[logsoracle] already connected\n"); pthread_mutex_unlock(&oralog_oper_lock); return DB_CONNECT_ALREADY_CONNECTED; } debug("[logsoracle] connecting.. "); /* initialize the mode to be the threaded and object environment */ OCIEnvCreate(&hp_env, OCI_THREADED|OCI_OBJECT, (dvoid *)0, 0, 0, 0, (size_t) 0, (dvoid **)0); /* allocate a server handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_server, OCI_HTYPE_SERVER, 0, (dvoid **) 0); /* allocate an error handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_error, OCI_HTYPE_ERROR, 0, (dvoid **) 0); /* create a server context */ /* TODO: dblink can be set here */ retval = OCIServerAttach(hp_server, hp_error, (text *)0, 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* allocate a service handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_service, OCI_HTYPE_SVCCTX, 0, (dvoid **) 0); /* associate server handle with service handle*/ retval = OCIAttrSet((dvoid *)hp_service, OCI_HTYPE_SVCCTX, (dvoid *)hp_server, (ub4) 0, OCI_ATTR_SERVER, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* allocate a session handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_session, OCI_HTYPE_SESSION, 0, (dvoid **) 0); /* set username in session handle */ retval = OCIAttrSet((dvoid *)hp_session, OCI_HTYPE_SESSION, (dvoid *)db_login, (ub4)ora_strlen(db_login), OCI_ATTR_USERNAME, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* set password in session handle */ retval = OCIAttrSet((dvoid *)hp_session, OCI_HTYPE_SESSION, (dvoid *)db_password, (ub4)ora_strlen(db_password), OCI_ATTR_PASSWORD, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; retval = OCISessionBegin ((dvoid *)hp_service, hp_error, hp_session, OCI_CRED_RDBMS, OCI_DEFAULT); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* associate session with service context */ retval = OCIAttrSet ((dvoid *)hp_service, OCI_HTYPE_SVCCTX, (dvoid *)hp_session, (ub4) 0, OCI_ATTR_SESSION, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* free local handles */ if(hp_error) OCIHandleFree(hp_error, OCI_HTYPE_ERROR); if (errors) { debug("[logsoracle] errors encounterd - cleaning up connection\n"); free_global_handles(); set_connection_status(0); pthread_mutex_unlock(&oralog_oper_lock); return DB_CONNECT_ERROR; } set_connection_status(1); debug("connected\n"); pthread_mutex_unlock(&oralog_oper_lock); return DB_CONNECT_NEW_CONNECTION; }
int _ora_connect(database *db,char *srvname,char *username,char *password) { t_ora * o; void *p; //printf("Connecting???\n"); // Создаем симпле-ора, ошибки памяти не проверяем... o = malloc( sizeof(t_ora) ); if (!o) { sprintf(db->error,"vora - no memory"); return 0;} memset(o,0,sizeof(t_ora)); db->h = o; db->disconnect = ora_disconnect; db->compile = ora_compile; db->exec = ora_exec; db->commit = ora_commit; db->rollback = ora_rollback; db->open = ora_open; db->fetch = ora_fetch; db->bind = ora_bind; o->envhp=env; // Copy It // Инициализиреум envhp - "сессионный набор" (для дальнейшего ЮЗА) OCIEnvInit( (OCIEnv **) &o->envhp, OCI_DEFAULT, (size_t) 0,(dvoid **) 0 ); debugf(" ..try connect to oracle {user:'******',pass:'******',server:'%s',env:0x%p}\n",username,password,srvname,o->envhp); // Инитим обьект типа ошибка Оракл OCIHandleAlloc( (dvoid *) o->envhp, (dvoid **) &o->errhp, OCI_HTYPE_ERROR,(size_t) 0, (dvoid **) 0); debugf(".. OCIHandleAlloc errhp:0x%p\n", o->errhp); //printf("ErrHP=%d\n",o->errhp); // Инитим "серверОракл" OCIHandleAlloc( (dvoid *) o->envhp, (dvoid **) &o->srvhp, OCI_HTYPE_SERVER,(size_t) 0, (dvoid **) 0); debugf(".. OCUHandleAlloc srvhp:0x%p, attaching\n", o->srvhp); // Следующие две команды совершенно непонятны... if (OCI_SUCCESS!=OCIServerAttach( o->srvhp, o->errhp, (text *)srvname, strlen(srvname), OCI_DEFAULT)) { debugf(" -- failed Server Attach"); ora_error(db); ora_disconnect(db); return 0; } debugf(".. Server Attached... srvhp:0x%p\n", o->srvhp); //return ora_disconnect(db); OCIHandleAlloc( (dvoid *) o->envhp, (dvoid **) &o->svchp, OCI_HTYPE_SVCCTX,(size_t) 0, (dvoid **) 0); // Забираем с сервера настройки для нашей сессии OCIAttrSet( (dvoid *) o->svchp, OCI_HTYPE_SVCCTX, (dvoid *)o->srvhp,(ub4) 0, OCI_ATTR_SERVER, (OCIError *) o->errhp); // Создаем обьет "авторизованность" OCIHandleAlloc((dvoid *) o->envhp, (dvoid **)&o->authp,(ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0); // Записыавем имя пользователя OCIAttrSet((dvoid *) o->authp, (ub4) OCI_HTYPE_SESSION,(dvoid *) username, (ub4) strlen((char *)username), (ub4) OCI_ATTR_USERNAME, o->errhp); // Записываем пароль OCIAttrSet((dvoid *) o->authp, (ub4) OCI_HTYPE_SESSION, (dvoid *) password, (ub4) strlen((char *)password), (ub4) OCI_ATTR_PASSWORD, o->errhp); // Compile statement... OCIHandleAlloc( (dvoid *) o->envhp, (dvoid **) &o->stmt, OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0); // Это для получения "describe" от SELECT OCIHandleAlloc((dvoid *) o->envhp, (dvoid **) &o->dschp, (ub4) OCI_HTYPE_DESCRIBE,(size_t) 0, (dvoid **) 0); // Для блобчика готовим... OCIDescriptorAlloc((dvoid *) o->envhp, (dvoid **) &o->blob, (ub4) OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0); OCIAttrSet((dvoid *) o->blob, (ub4) OCI_DTYPE_LOB, 0,0, (ub4) OCI_ATTR_LOBEMPTY, o->errhp); //OCIDescriptorAlloc((dvoid *) o->envhp, (dvoid **) &o->blob_read, // (ub4) OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0); //OCIAttrSet((dvoid *) o->blob_read, (ub4) OCI_HTYPE_SESSION, // 0,0, (ub4) 0 /*OCI_ATTR_LOBEMPTY*/, o->errhp); debugf(" initing OCISessionBegin {svchp:%p,errhp:%p,authp:%p}\n",o->svchp,o->errhp,o->authp); if (OCI_SUCCESS!=OCISessionBegin (o->svchp,o->errhp,o->authp, OCI_CRED_RDBMS,(ub4) OCI_DEFAULT)) { ora_error(db); ora_disconnect(db); return 0; } debugf(" ora_session inited, set some attributes\n"); //return 1; // Его (blob) еще нужно создавать... интересно, что это за действо? //OCILobCreateTemporary(o->svchp,o->errhp,o->blob, // OCI_DEFAULT,OCI_DEFAULT,OCI_TEMP_BLOB,FALSE,OCI_DURATION_SESSION); //OCILobCreateTemporary(o->svchp,o->errhp,o->blob_read, // OCI_DEFAULT,OCI_DEFAULT,OCI_TEMP_BLOB,FALSE,OCI_DURATION_SESSION); // Нужное какое-то подсоединение... OCIAttrSet((dvoid *) o->svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) o->authp, (ub4) 0, (ub4) OCI_ATTR_SESSION, o->errhp); if (0) { // Как установить аттрибут??? ub1 yes = 1; int res; res = OCIAttrSet((dvoid *) o->stmt, (ub4) OCI_PTYPE_DATABASE, (dvoid *) &yes, (ub4) 1, (ub4) OCI_ATTR_AUTOCOMMIT_DDL, o->errhp); printf("\n ------------- Autocommit set result=%d, ok=%d\n",res, OCI_SUCCESS); if (res) { { ora_error(db); ora_disconnect(db); return 0; } } } debugf(" ok, oracle sever connected\n"); return 1; // Connected OK! }
int main (void) { printf ("cdemoin1 - Demonstrating select, insert and update on a subtype table\n"); /* Initializing the environment in the Object mode*/ OCIEnvCreate((OCIEnv **) &envhp, OCI_OBJECT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0 ); OCIHandleAlloc (envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); OCIHandleAlloc(envhp, (dvoid **)&svrhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0); status = OCIServerAttach(svrhp, errhp, (text *)database, (sb4)strlen((char *)database), OCI_DEFAULT); if (status != OCI_SUCCESS) { printf("OCIServerAttach failed \n"); } else printf("OCIServerAttach - Success \n"); OCIHandleAlloc(envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0); OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, (dvoid *)svrhp, (ub4)0, OCI_ATTR_SERVER, errhp); OCIHandleAlloc(envhp, (dvoid **)&sesnhp, OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0); OCIAttrSet(sesnhp, OCI_HTYPE_SESSION, (dvoid *)username, (ub4)strlen((char *)username), OCI_ATTR_USERNAME, errhp); OCIAttrSet(sesnhp, OCI_HTYPE_SESSION, (dvoid*)password, (ub4)strlen((char *)password), OCI_ATTR_PASSWORD, errhp); printf("Connecting as %s/%s@%s\n",username,password,database); status = OCISessionBegin(svchp, errhp, sesnhp, OCI_CRED_RDBMS, OCI_DEFAULT); if (status != OCI_SUCCESS) { printf("Connection failed \n"); } else printf("Connection - Success \n"); OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, sesnhp, (ub4)0, OCI_ATTR_SESSION, errhp); /* Calling function to get an array of REFs to populate the varray */ getRef(); /* Function to insert an instance of the subtype into a supertype table */ insertFunction(); /* Function to modify the data of an inherited object */ modifyFunction(); /* Function to display data from the table */ selectFunction(); /* Free the allocated handles */ cleanup(); printf("cdemoin1 - Done\n"); return 1; } /* End of main() */
int OGROCISession::EstablishSession( const char *pszUserid, const char *pszPassword, const char *pszDatabase ) { /* -------------------------------------------------------------------- */ /* Operational Systems's authentication option */ /* -------------------------------------------------------------------- */ ub4 eCred = OCI_CRED_RDBMS; if( EQUAL(pszDatabase, "") && EQUAL(pszPassword, "") && EQUAL(pszUserid, "/") ) { eCred = OCI_CRED_EXT; } /* -------------------------------------------------------------------- */ /* Initialize Environment handler */ /* -------------------------------------------------------------------- */ if( Failed( OCIInitialize((ub4) (OCI_DEFAULT | OCI_OBJECT), (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0 ) ) ) { return FALSE; } if( Failed( OCIEnvInit( (OCIEnv **) &hEnv, OCI_DEFAULT, (size_t) 0, (dvoid **) 0 ) ) ) { return FALSE; } if( Failed( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hError, OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Server Context */ /* -------------------------------------------------------------------- */ if( Failed( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hServer, OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } if( Failed( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hSvcCtx, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } if( Failed( OCIServerAttach( hServer, hError, (text*) pszDatabase, strlen((char*) pszDatabase), 0) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Service Context */ /* -------------------------------------------------------------------- */ if( Failed( OCIAttrSet( (dvoid *) hSvcCtx, OCI_HTYPE_SVCCTX, (dvoid *)hServer, (ub4) 0, OCI_ATTR_SERVER, (OCIError *) hError) ) ) { return FALSE; } if( Failed( OCIHandleAlloc((dvoid *) hEnv, (dvoid **)&hSession, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } if( Failed( OCIAttrSet((dvoid *) hSession, (ub4) OCI_HTYPE_SESSION, (dvoid *) pszUserid, (ub4) strlen((char *) pszUserid), (ub4) OCI_ATTR_USERNAME, hError) ) ) { return FALSE; } if( Failed( OCIAttrSet((dvoid *) hSession, (ub4) OCI_HTYPE_SESSION, (dvoid *) pszPassword, (ub4) strlen((char *) pszPassword), (ub4) OCI_ATTR_PASSWORD, hError) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Session */ /* -------------------------------------------------------------------- */ if( Failed( OCISessionBegin(hSvcCtx, hError, hSession, eCred, (ub4) OCI_DEFAULT) ) ) { CPLDebug("OCI", "OCISessionBegin() failed to intialize session"); return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Service */ /* -------------------------------------------------------------------- */ if( Failed( OCIAttrSet((dvoid *) hSvcCtx, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) hSession, (ub4) 0, (ub4) OCI_ATTR_SESSION, hError) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Create a describe handle. */ /* -------------------------------------------------------------------- */ if( Failed( OCIHandleAlloc( hEnv, (dvoid **) &hDescribe, (ub4)OCI_HTYPE_DESCRIBE, (size_t)0, (dvoid **)0 ), "OCIHandleAlloc(Describe)" ) ) return FALSE; /* -------------------------------------------------------------------- */ /* Try to get the MDSYS.SDO_GEOMETRY type object. */ /* -------------------------------------------------------------------- */ /* If we have no MDSYS.SDO_GEOMETRY then we consider we are working along with the VRT driver and access non spatial tables. See #2202 for more details (Tamas Szekeres)*/ if (OCIDescribeAny(hSvcCtx, hError, (text *) SDO_GEOMETRY, (ub4) strlen(SDO_GEOMETRY), OCI_OTYPE_NAME, (ub1) OCI_DEFAULT, (ub1)OCI_PTYPE_TYPE, hDescribe ) != OCI_ERROR) { hGeometryTDO = PinTDO( SDO_GEOMETRY ); if( hGeometryTDO == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Try to get the MDSYS.SDO_ORDINATE_ARRAY type object. */ /* -------------------------------------------------------------------- */ hOrdinatesTDO = PinTDO( "MDSYS.SDO_ORDINATE_ARRAY" ); if( hOrdinatesTDO == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Try to get the MDSYS.SDO_ELEM_INFO_ARRAY type object. */ /* -------------------------------------------------------------------- */ hElemInfoTDO = PinTDO( "MDSYS.SDO_ELEM_INFO_ARRAY" ); if( hElemInfoTDO == NULL ) return FALSE; } /* -------------------------------------------------------------------- */ /* Record information about the session. */ /* -------------------------------------------------------------------- */ this->pszUserid = CPLStrdup(pszUserid); this->pszPassword = CPLStrdup(pszPassword); this->pszDatabase = CPLStrdup(pszDatabase); /* -------------------------------------------------------------------- */ /* Setting upt the OGR compatible time formating rules. */ /* -------------------------------------------------------------------- */ OGROCIStatement oSetNLSTimeFormat( this ); if( oSetNLSTimeFormat.Execute( "ALTER SESSION SET NLS_DATE_FORMAT='YYYY/MM/DD' \ NLS_TIME_FORMAT='HH24:MI:SS' NLS_TIME_TZ_FORMAT='HH24:MI:SS TZHTZM' \ NLS_TIMESTAMP_FORMAT='YYYY/MM/DD HH24:MI:SS' \ NLS_TIMESTAMP_TZ_FORMAT='YYYY/MM/DD HH24:MI:SS TZHTZM'" ) != CE_None ) return OGRERR_FAILURE; return TRUE; }
int OGROCISession::EstablishSession( const char *pszUseridIn, const char *pszPasswordIn, const char *pszDatabaseIn ) { /* -------------------------------------------------------------------- */ /* Operational Systems's authentication option */ /* -------------------------------------------------------------------- */ ub4 eCred = OCI_CRED_RDBMS; if( EQUAL(pszDatabaseIn, "") && EQUAL(pszPasswordIn, "") && EQUAL(pszUseridIn, "/") ) { eCred = OCI_CRED_EXT; } /* -------------------------------------------------------------------- */ /* Initialize Environment handler */ /* -------------------------------------------------------------------- */ if( Failed( OCIInitialize((ub4) (OCI_DEFAULT | OCI_OBJECT), (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0 ) ) ) { return FALSE; } if( Failed( OCIEnvInit( (OCIEnv **) &hEnv, OCI_DEFAULT, (size_t) 0, (dvoid **) 0 ) ) ) { return FALSE; } if( Failed( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hError, OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Server Context */ /* -------------------------------------------------------------------- */ if( Failed( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hServer, OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } if( Failed( OCIHandleAlloc( (dvoid *) hEnv, (dvoid **) &hSvcCtx, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } if( Failed( OCIServerAttach( hServer, hError, (text*) pszDatabaseIn, static_cast<int>(strlen((char*) pszDatabaseIn)), 0) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Service Context */ /* -------------------------------------------------------------------- */ if( Failed( OCIAttrSet( (dvoid *) hSvcCtx, OCI_HTYPE_SVCCTX, (dvoid *)hServer, (ub4) 0, OCI_ATTR_SERVER, (OCIError *) hError) ) ) { return FALSE; } if( Failed( OCIHandleAlloc((dvoid *) hEnv, (dvoid **)&hSession, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0) ) ) { return FALSE; } if( Failed( OCIAttrSet((dvoid *) hSession, (ub4) OCI_HTYPE_SESSION, (dvoid *) pszUseridIn, (ub4) strlen((char *) pszUseridIn), (ub4) OCI_ATTR_USERNAME, hError) ) ) { return FALSE; } if( Failed( OCIAttrSet((dvoid *) hSession, (ub4) OCI_HTYPE_SESSION, (dvoid *) pszPasswordIn, (ub4) strlen((char *) pszPasswordIn), (ub4) OCI_ATTR_PASSWORD, hError) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Session */ /* -------------------------------------------------------------------- */ if( Failed( OCISessionBegin(hSvcCtx, hError, hSession, eCred, (ub4) OCI_DEFAULT) ) ) { CPLDebug("OCI", "OCISessionBegin() failed to initialize session"); return FALSE; } /* -------------------------------------------------------------------- */ /* Initialize Service */ /* -------------------------------------------------------------------- */ if( Failed( OCIAttrSet((dvoid *) hSvcCtx, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) hSession, (ub4) 0, (ub4) OCI_ATTR_SESSION, hError) ) ) { return FALSE; } /* -------------------------------------------------------------------- */ /* Create a describe handle. */ /* -------------------------------------------------------------------- */ if( Failed( OCIHandleAlloc( hEnv, (dvoid **) &hDescribe, (ub4)OCI_HTYPE_DESCRIBE, (size_t)0, (dvoid **)0 ), "OCIHandleAlloc(Describe)" ) ) return FALSE; /* -------------------------------------------------------------------- */ /* Try to get the MDSYS.SDO_GEOMETRY type object. */ /* -------------------------------------------------------------------- */ /* If we have no MDSYS.SDO_GEOMETRY then we consider we are working along with the VRT driver and access non spatial tables. See #2202 for more details (Tamas Szekeres)*/ if (OCIDescribeAny(hSvcCtx, hError, (text *) SDO_GEOMETRY, (ub4) strlen(SDO_GEOMETRY), OCI_OTYPE_NAME, (ub1) OCI_DEFAULT, (ub1)OCI_PTYPE_TYPE, hDescribe ) != OCI_ERROR) { hGeometryTDO = PinTDO( SDO_GEOMETRY ); if( hGeometryTDO == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Try to get the MDSYS.SDO_ORDINATE_ARRAY type object. */ /* -------------------------------------------------------------------- */ hOrdinatesTDO = PinTDO( "MDSYS.SDO_ORDINATE_ARRAY" ); if( hOrdinatesTDO == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Try to get the MDSYS.SDO_ELEM_INFO_ARRAY type object. */ /* -------------------------------------------------------------------- */ hElemInfoTDO = PinTDO( "MDSYS.SDO_ELEM_INFO_ARRAY" ); if( hElemInfoTDO == NULL ) return FALSE; } /* -------------------------------------------------------------------- */ /* Record information about the session. */ /* -------------------------------------------------------------------- */ pszUserid = CPLStrdup(pszUseridIn); pszPassword = CPLStrdup(pszPasswordIn); pszDatabase = CPLStrdup(pszDatabaseIn); /* -------------------------------------------------------------------- */ /* Get server version information */ /* -------------------------------------------------------------------- */ char szVersionTxt[256]; OCIServerVersion( hSvcCtx, hError, (text*) szVersionTxt, (ub4) sizeof(szVersionTxt), (ub1) OCI_HTYPE_SVCCTX ); char** papszNameValue = CSLTokenizeString2( szVersionTxt, " .", CSLT_STRIPLEADSPACES ); int count = CSLCount( papszNameValue); for( int i = 0; i < count; i++) { if( EQUAL(papszNameValue[i], "Release") ) { if( i + 1 < count ) { nServerVersion = atoi(papszNameValue[i + 1]); } if( i + 2 < count ) { nServerRelease = atoi(papszNameValue[i + 2]); } break; } } CPLDebug("OCI", "From '%s' :", szVersionTxt); CPLDebug("OCI", "Version:%d", nServerVersion); CPLDebug("OCI", "Release:%d", nServerRelease); /* -------------------------------------------------------------------- */ /* Set maximun name length (before 12.2 ? 30 : 128) */ /* -------------------------------------------------------------------- */ if( nServerVersion >= 12 && nServerRelease >= 2 ) { nMaxNameLength = 128; } CPLFree( papszNameValue ); /* -------------------------------------------------------------------- */ /* Setting up the OGR compatible time formatting rules. */ /* -------------------------------------------------------------------- */ OGROCIStatement oSetNLSTimeFormat( this ); if( oSetNLSTimeFormat.Execute( "ALTER SESSION SET NLS_DATE_FORMAT='YYYY/MM/DD' \ NLS_TIME_FORMAT='HH24:MI:SS' NLS_TIME_TZ_FORMAT='HH24:MI:SS TZHTZM' \ NLS_TIMESTAMP_FORMAT='YYYY/MM/DD HH24:MI:SS' \ NLS_TIMESTAMP_TZ_FORMAT='YYYY/MM/DD HH24:MI:SS TZHTZM' \ NLS_NUMERIC_CHARACTERS = '. '" ) != CE_None ) return OGRERR_FAILURE; return TRUE; }