Пример #1
0
// 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;
}
Пример #2
0
JP_INTERNAL(int) JP_Logon(TJConnector &conn, const text* aUser, const text* aPassword, const text* aServer)
{
  conn.user = strdup((char*) aUser);
  conn.server = strdup((char*) aServer);
  conn.result = OCIInitialize(OCI_DEFAULT, 0, 0, 0, 0);
  if (conn.result == 0)
    conn.result = OCIEnvInit(&conn.ociEnv, OCI_DEFAULT, 0, 0);
  if (conn.result == 0)
    conn.result = OCIHandleAlloc(conn.ociEnv, (void **)&conn.ociError, OCI_HTYPE_ERROR, 0, 0);
  if (conn.result == 0)
    conn.result = OCILogon(conn.ociEnv, conn.ociError, &conn.ociSvcCtx,
                      TJ_CAST aUser,     strlen((char*)aUser),
                      TJ_CAST aPassword, strlen((char*)aPassword),
                      TJ_CAST aServer,   strlen((char*)aServer));
  return _Result(conn);
}
Пример #3
0
char *prefix(int version)  {
if (!ora_mutex)
 { int cnt=0; int mode = /*OCI_DEFAULT*/OCI_THREADED;
 //return 0;
  ora_mutex = mutex_create();
 //printf("mutex created!\n");
   // Без этой штуки Oracle не хочет работать в потоках!
 if(1) (void) OCIInitialize((ub4)mode, (dvoid *)0,
                       (dvoid * (*)(dvoid *, size_t)) 0,
                      (dvoid * (*)(dvoid *, dvoid *, size_t))0,
                     (void (*)(dvoid *, dvoid *)) 0 );
                     //OCI_UTF16ID
 //OCIEnvNlsCreate(&env, mode , 0 , 0,0,0,0,0,OCI_UTF16ID,OCI_UTF16ID);  //my_malloc,my_realloc,	my_free,0,0); //&o->p);
 OCIEnvCreate(&env, mode , 0 , 0,0,0,0,0); //,OCI_UTF16ID,OCI_UTF16ID);  //my_malloc,my_realloc,	my_free,0,0); //&o->p);
 debugf(" created OCI:{env:0x%p,mode:%d,prefix:'_ora'\n",env,mode);
 }
return "ora_";
}
Пример #4
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;
}
Пример #5
0
Файл: main.c Проект: mlange/dev
int main(int argc, char *argv[])
{
    long ii,
	 rows,
	 status;

    sb2 indicator;

    char data[80];

    char *dbuser,
	 *dbpass,
	 *dbconn;

    OCIEnv    *envhp  = NULL; 
    OCIStmt   *stmthp = NULL;
    OCIDefine *defnhp = NULL;
    OCIParam  *parmhp = NULL;
    OCIError  *errhp  = NULL; 
    OCISvcCtx *svchp  = NULL; 

    /* Set the database user, password and connection. */
    dbuser = DBUSER ? DBUSER : "";
    dbpass = DBPASS ? DBPASS : "";
    dbconn = DBCONN ? DBCONN : "";

    /* Initialize the OCI process environment. */
    status = OCIInitialize(OCI_DEFAULT, 0, 0, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIInitialize: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Allocate and initialize the environment handle pointer. */
    status = OCIEnvInit(&envhp, OCI_DEFAULT, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIEnvInit: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Allocate the error handle pointer. */
    status = OCIHandleAlloc(envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIHandleAlloc: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Connect to the database. */
    status = OCILogon(envhp, errhp, &svchp,
                      (text *) dbuser, strlen(dbuser),
                      (text *) dbpass, strlen(dbpass),
                      (text *) dbconn, strlen(dbconn));
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCILogon: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Allocate a statement handle for ourselves. */
    status = OCIHandleAlloc(envhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIHandleAlloc: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Prepare the SQL statement. */
    status = OCIStmtPrepare(stmthp, errhp, (text *) SQLSTMT, strlen(SQLSTMT), 
			    OCI_NTV_SYNTAX, OCI_DEFAULT);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIStmtPrepare: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    for (ii=0; ii<2; ii++)
    {

    /* Execute the SQL statement. */
    status = OCIStmtExecute(svchp, stmthp, errhp, 0, 0, NULL, NULL,OCI_DEFAULT);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIStmtExecute: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Bind the the output variable for this column.              */
    status = OCIDefineByPos(stmthp, &defnhp, errhp, 1, data, sizeof(data), 
			    SQLT_STR, &indicator, NULL, NULL, OCI_DEFAULT);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIDefineByPos: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Fetch each row. */
    for (rows=0; ; rows++)
    {
	status = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
	if (status == OCI_NO_DATA)
	{
	    break;
	}
        else if (status != OCI_SUCCESS)
        {
            fprintf(stderr, "OCIStmtFetch: %ld\n", status);
            exit(EXIT_FAILURE);
        }

	printf("INDICATOR [%d]  VALUE [%s]\n", indicator, data);
    }

    printf("Command affected %ld row(s)\n\n", rows);

    }

    exit(EXIT_SUCCESS);
}
Пример #6
0
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;
}
Пример #7
0
int main(int argc,char** argv)  
{  
	int nRet;  
	OCIEnv*      phOCIEnv;    //OCI环境句柄  
	OCIError*    phOCIErr;    //OCI错误句柄  
	OCISvcCtx*   phOCIConn;   //OCI数据库服务连接句柄  
 
	char acDBName[20],acUserName[20],acPwd[20];  
 
	//初始化OCI应用环境  
	nRet = OCIInitialize((ub4)OCI_DEFAULT,(dvoid*)0,(dvoid* (*)(dvoid*,size_t))0,
			(dvoid* (*)(dvoid*,dvoid*,size_t))0,(void (*)(dvoid*,dvoid*))0);  
	if(nRet)  
	{  
		printf("OCIInitialize() Failed: %d\n",(int)OCI_ERROR);  
		return (int)OCI_ERROR;  
	}  
 
	//初始化OCI环境句柄(由系统来负责分配OCI环境句柄)  
	nRet = OCIEnvInit(&phOCIEnv,(ub4)OCI_DEFAULT,(size_t)0,(dvoid**)0);  
	if(nRet)  
	{  
		printf("OCIEnvInit() Failed: %d\n",(int)OCI_ERROR);  
		return (int)OCI_ERROR;  
	}  
 
	//在OCI应用环境中依据OCI环境句柄来分配OCI的错误报告句柄  
	nRet = OCIHandleAlloc((dvoid*)phOCIEnv,(dvoid**)&phOCIErr,
		    (ub4)OCI_HTYPE_ERROR,(size_t)0,(dvoid**)0);  
	if(nRet)  
	{  
		printf("OCIHandleAlloc() Failed: %d\n",(int)OCI_ERROR);  
		return (int)OCI_ERROR;  
	}  
 
	//建立与Oracle的连接  
	memset(acDBName,0,sizeof(acDBName));  
	memset(acUserName,0,sizeof(acUserName));  
	memset(acPwd,0,sizeof(acPwd));  
 
	strcpy(acDBName,"simplegw");  
	strcpy(acUserName,"simplegw");  
	strcpy(acPwd,"simplegw");  
 
	//连接数据库  
	nRet = OCILogon(phOCIEnv,phOCIErr,&phOCIConn,(text*)acUserName,
		    strlen(acUserName),(text*)acPwd,strlen(acPwd),
	    (OraText*)acDBName,strlen(acDBName));  
	if(nRet)  
	{  
		printf("Connection Failed: %d\n\n",nRet);  
		exit(1);  
	}  
	printf("Connection OK: %d\n",nRet);  
	printf("执行一些SQL语句和数据库操作命令 ......\n\n");  







 
	//断开与数据库的连接  
	nRet = OCILogoff(phOCIConn,phOCIErr);  
	if(nRet)  
	{  
		printf("OCILogoff() Failed.\n\n");  
	}
	else 
	{  
		printf("Connecttion closed.\n\n");  
	}  
	//连接已经断开,把连接句柄置为空  
	phOCIConn = NULL;  
 
	//销毁OCI应用环境与相关的句柄  
	if(phOCIErr)  
	{  
		OCIHandleFree((dvoid*)phOCIErr,OCI_HTYPE_ERROR);  
		printf("OCI错误报告句柄已经释放.\n\n");  
	}  
 
	if(phOCIEnv)  
	{  
		OCIHandleFree((dvoid*)phOCIEnv,(ub4)OCI_HTYPE_ENV);  
		printf("OCI环境句柄已经释放.\n\n");  
	}  
	printf("OCI环境释放结束.\n\n");  
	return 0;  
} 
Пример #8
0
int start_oracle(int s, char *ip, int port, unsigned char options, char *miscptr, FILE * fp) {
  char *empty = "";
  char *login, *pass, buffer[200], sid[100];

  if (strlen(login = hydra_get_next_login()) == 0)
    login = empty;
  if (strlen(pass = hydra_get_next_password()) == 0)
    pass = empty;

  strncpy(sid, miscptr, sizeof(sid));
  snprintf(buffer, sizeof(buffer), "//%s:%d/%s", hydra_address2string(ip), port, sid);

  /*

     To use the Easy Connect naming method, PHP must be linked with Oracle 10g or greater Client libraries.
     The Easy Connect string for Oracle 10g is of the form: [//]host_name[:port][/service_name].
     With Oracle 11g, the syntax is: [//]host_name[:port][/service_name][:server_type][/instance_name].
     Service names can be found by running the Oracle utility lsnrctl status on the database server machine.

     The tnsnames.ora file can be in the Oracle Net search path, which includes $ORACLE_HOME/network/admin
     and /etc. Alternatively set TNS_ADMIN so that $TNS_ADMIN/tnsnames.ora is read. Make sure the web
     daemon has read access to the file. 

   */

  if (OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL)) {
    print_oracle_error("OCIInitialize");
    return 4;
  }
  if (OCIEnvInit(&o_environment, OCI_DEFAULT, 0, NULL)) {
    print_oracle_error("OCIEnvInit");
    return 4;
  }
  if (OCIEnvInit(&o_environment, OCI_DEFAULT, 0, NULL)) {
    print_oracle_error("OCIEnvInit 2");
    return 4;
  }
  if (OCIHandleAlloc(o_environment, (dvoid **) & o_error, OCI_HTYPE_ERROR, (size_t) 0, NULL)) {
    print_oracle_error("OCIHandleAlloc");
    return 4;
  }

  if (OCILogon(o_environment, o_error, &o_servicecontext, (const OraText *) login, strlen(login), (const OraText *) pass, strlen(pass), (const OraText *) buffer, strlen(buffer))) {
    OCIErrorGet(o_error, 1, NULL, &o_errorcode, o_errormsg, sizeof(o_errormsg), OCI_HTYPE_ERROR);
    //database: oracle_error: ORA-01017: invalid username/password; logon denied
    //database: oracle_error: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
    //database: oracle_error: ORA-28000: the account is locked
    //Failed login attempts is set to 10 by default
    if (verbose) {
      hydra_report(stderr, "[VERBOSE] database: oracle_error: %s\n", o_errormsg);
    }
    if (strstr((const char *) o_errormsg, "ORA-12514") != NULL) {
      hydra_report(stderr, "[ERROR] ORACLE SID is not valid, you should try to enumerate them.\n");
    }
    if (strstr((const char *) o_errormsg, "ORA-28000") != NULL) {
      hydra_report(stderr, "[ERROR] ORACLE account %s is locked.\n", login);
    }

    if (o_error) {
      OCIHandleFree((dvoid *) o_error, OCI_HTYPE_ERROR);
    }

    hydra_completed_pair();
    //by default, set in sqlnet.ora, the trace file is generated in pwd to log any errors happening,
    //as we don't care, we are deleting the file
    //set these parameters to not generate the file
    //LOG_DIRECTORY_CLIENT = /dev/null
    //LOG_FILE_CLIENT = /dev/null
    unlink("sqlnet.log");

    return 2;
  } else {
    OCILogoff(o_servicecontext, o_error);
    if (o_error) {
      OCIHandleFree((dvoid *) o_error, OCI_HTYPE_ERROR);
    }
    hydra_report_found_host(port, ip, "oracle", fp);
    hydra_completed_pair_found();
  }
  if (memcmp(hydra_get_next_pair(), &HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0)
    return 3;
  return 1;
}
Пример #9
0
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;
}