⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ocidbclass.cpp

📁 通过oci实现的oracle查询分析器功能和可以 尤其对BLOB字段的度曲
💻 CPP
📖 第 1 页 / 共 4 页
字号:

	status = OCIStmtExecute( DBHandle.svchp, stmthp, DBHandle.errhp, 1,0,
			 NULL,NULL,OCI_DEFAULT );
	if( status != OCI_SUCCESS ){
		OCIErrorGet( DBHandle.errhp, 1, NULL, &errcode, (text*)ErrMsg, 300, OCI_HTYPE_ERROR );

		sprintf( DBErrMsg,ErrMsg );
                // throw Exception(DBErrMsg);
		goto ret;
	}
ret:
	if( stmthp != NULL )
		OCIHandleFree( stmthp , OCI_HTYPE_STMT );

	return status;
}



void TOCIQuery::checkerr(OCIError *errhp,sword status)

{
  text errbuf[512];
  sb4 errcode = 0;

  switch (status)
  {
  case OCI_SUCCESS:
    break;
  case OCI_SUCCESS_WITH_INFO:
      throw Exception("Error - OCI_SUCCESS_WITH_INFO");

    break;
  case OCI_NEED_DATA:
     throw Exception("Error - OCI_NEED_DATA");
     break;
  case OCI_NO_DATA:
     throw Exception("Error - OCI_NODATA");
    break;
  case OCI_ERROR:
    (void) OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,
                        errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
     throw Exception((char *)errbuf);
    break;
  case OCI_INVALID_HANDLE:
    throw Exception("Error - OCI_INVALID_HANDLE");
    break;
  case OCI_STILL_EXECUTING:
      throw Exception("Error - OCI_STILL_EXECUTE");
    break;
  case OCI_CONTINUE:
   throw Exception("Error - OCI_CONTINUE");
    break;
  default:
    break;
  }
}


int TOCIQuery::_OCIErrorGet( DATABASEHANDLE & DBHandle,char * _ErrMsg )
{
	int  errcode;

	memset( tmpbuf, 0, 300 );

	OCIErrorGet( DBHandle.errhp, 1, NULL, &errcode, (text*)tmpbuf, 300, OCI_HTYPE_ERROR );
	if( tmpbuf[strlen(tmpbuf)-1] == '\n' )
		tmpbuf[strlen(tmpbuf)-1] = 0;

	strcpy( _ErrMsg, tmpbuf );

	return 0;
}
//---------------------------------------------------------------------------
int TOCIQuery:: SelectPrepare()
{

  
  text	    * colname;
  status=0;
  ocierr:
  if( status == OCI_ERROR )
  {

    _OCIErrorGet( DBHandle,tmpbuf );
    status = -4;/* 一般错误 */
    sprintf( DBErrMsg,"=>调用 %s 错误(%s)",tmpname,tmpbuf);
    throw Exception(DBErrMsg);
    //return status;
  }
err:
  if( status < 0 )
  {
    sprintf( DBErrMsg,"=>由于调用 %s 错误( %s )",tmpname, logmsg );
    throw Exception(DBErrMsg);
   // return status;
  }


  status = OCIStmtExecute (svchp, selecthp, errhp, (ub4)0, (ub4)0,
                      (const OCISnapshot*) 0, (OCISnapshot*)0, OCI_DESCRIBE_ONLY);
  if( status == OCI_ERROR )
  {
      strcpy( tmpname, "OCIStmtPrepare()" );
              goto ocierr;
  }
  FieldCount=0;
  status = OCIAttrGet ((dvoid *)selecthp, (ub4)OCI_HTYPE_STMT, (dvoid *)
                        &FieldCount, (ub4 *) 0, (ub4)OCI_ATTR_PARAM_COUNT, errhp);
  if( status == OCI_ERROR )
  {
     strcpy( tmpname, "OCIStmtPrepare()" );
     goto ocierr;
  }
  if( FieldCount!= 0 )
  {
    (*selectinfo).outputnums = FieldCount;
    (*selectinfo).outputinfo = ( WY_DEFINEINFO * ) malloc( sizeof( WY_DEFINEINFO ) * ( FieldCount ) );
    if( (*selectinfo).outputinfo == NULL )
    {
      strcpy( tmpname, "malloc()" );
      sprintf( logmsg, "分配%i个outputinfo错误.", parmcnt );
      status = -3;
      goto err;
    }
    memset( (*selectinfo).outputinfo, 0, sizeof( WY_DEFINEINFO ) * ( FieldCount ) );
    offset = 0;
    //int i = 0,j,k;
    for( int j = 0 ; j < FieldCount ; j ++ )
    {
      status = OCIParamGet ((dvoid *)selecthp, OCI_HTYPE_STMT, errhp, (void**)&parmdp, j+1);
      if( status == OCI_ERROR )
      {
         strcpy( tmpname, "OCIParamGet()" );
         goto ocierr;
      }

      fieldlen = 0;
      fieldtype = 0 ;
      if( (status = OCIAttrGet( parmdp, OCI_DTYPE_PARAM,
                                      &fieldtype,&tmplen,
                                      OCI_ATTR_DATA_TYPE, errhp )) != OCI_SUCCESS )
      {
         _OCIErrorGet( DBHandle,tmpbuf );
          sprintf( logmsg, tmpbuf );
          strcpy( tmpname, "OCIAttrGet()(OCI_ATTR_DATA_TYPE)" );
          goto err;

      }
      if( (status = OCIAttrGet( parmdp, OCI_DTYPE_PARAM,
                                      &(fieldlen),&tmplen,
                                      OCI_ATTR_DATA_SIZE, errhp )) != OCI_SUCCESS )
      {
         _OCIErrorGet( DBHandle,tmpbuf );
         sprintf( logmsg, tmpbuf );
         strcpy( tmpname, "OCIAttrGet()(OCI_ATTR_DATA_SIZE)" );
         goto err;
      }
      if( fieldtype == OCI_TYPECODE_DATE )
      {
        fieldlen = 100;
      }
       if( fieldtype == SQLT_LNG)
      {
        fieldlen = 1000;
      }
       if( fieldtype == 112)
      {
        fieldlen = 500000;
      }
       if( fieldtype == 104)//rowid
      {
        fieldlen = 100;
      }

      if( (status = OCIAttrGet( parmdp, (ub4)OCI_DTYPE_PARAM,
                              (dvoid**)&colname,(ub4*)&tmplen,
                              (ub4)OCI_ATTR_NAME, errhp )) != OCI_SUCCESS )
      {
          _OCIErrorGet( DBHandle,tmpbuf );
          sprintf( logmsg, tmpbuf );
          strcpy( tmpname, "OCIAttrGet()(OCI_ATTR_NAME)" );
          goto err;
      }

      strncpy( (*selectinfo).outputinfo[j].FieldName, colname,tmplen );
      (*selectinfo).outputinfo[j].Type = fieldtype;
      if( fieldtype == OCI_TYPECODE_BLOB )
      {
        (*selectinfo).outputinfo[j].Value = ( char * ) malloc( sizeof( char ) * (10) );
         if( (*selectinfo).outputinfo[j].Value == NULL )
         {
            strcpy( tmpname, "malloc()" );
            sprintf( logmsg, "分配%i个字节长的outputinfo[].value错误.", 10 );
            status = -3;
            goto err;
         }
         memset( (*selectinfo).outputinfo[j].Value, 0, sizeof( char ) * (10) );

        if( OCIDescriptorAlloc( (dvoid*) envhp, (dvoid**) &((*selectinfo).outputinfo[j].blob),
                                (ub4) OCI_DTYPE_LOB, (size_t)0,(dvoid**)0 ) != OCI_SUCCESS )
        {
                 _OCIErrorGet( DBHandle,tmpbuf );
                 sprintf( logmsg, tmpbuf );
                strcpy( tmpname, "OCIDescriptorAlloc()(OCI_DTYPE_LOB)" );
                goto err;
        }

      }
      else
      {
        (*selectinfo).outputinfo[j].Value = ( char * ) malloc( sizeof( char ) * (fieldlen+48) );
         if( (*selectinfo).outputinfo[j].Value == NULL )
         {
            strcpy( tmpname, "malloc()" );
            sprintf( logmsg, "分配%i个字节长的outputinfo[].value错误.", fieldlen+48 );
            status = -3;
            goto err;
         }
         memset( (*selectinfo).outputinfo[j].Value, 0, sizeof( char ) * (fieldlen+48) );
      }
      (*selectinfo).outputinfo[j].Size = fieldlen;//(fieldtype==OCI_TYPECODE_DATE?8:fieldlen);

    }
    for( int i = 0 ; i < (*selectinfo).outputnums ; i ++ )
    {

      if( (*selectinfo).outputinfo[i].Type == OCI_TYPECODE_BLOB )/*BLOB*/
      {
        status = OCIDefineByPos( selecthp, & ((*selectinfo).outputinfo[i].defhp), errhp, i+1,
                                               (dvoid*) &((*selectinfo).outputinfo[i].blob),(sb4)-1,SQLT_BLOB,
                                               /*(dvoid*)0*/
                                               (dvoid*)&((*selectinfo).outputinfo[i].indicator), 0, 0, OCI_DEFAULT );
        if( status != OCI_SUCCESS )
        {
           _OCIErrorGet( DBHandle,tmpbuf );
          sprintf( logmsg, tmpbuf );
          strcpy( tmpname, "OCIDefineByPos()" );
          goto err;
        }
      }
      else
      {
        status = OCIDefineByPos( selecthp, & ((*selectinfo).outputinfo[i].defhp), errhp, i+1,
                                 (*selectinfo).outputinfo[i].Value,(*selectinfo).outputinfo[i].Size+1,SQLT_STR,
                                 (dvoid*)&((*selectinfo).outputinfo[i].indicator), 0, 0, OCI_DEFAULT );
        if( status == OCI_ERROR )
        {
                 _OCIErrorGet( DBHandle,tmpbuf );
                sprintf( logmsg, tmpbuf );
                strcpy( tmpname, "OCIDefineByPos()" );
                goto err;
        }
      }
    }
  }

  return status;
}
int TOCIQuery::FreeSelectInfo( SELECTINFO * selectinfo )
{
	int j;

	if( selectinfo == NULL )
		return 0;

	if( (* selectinfo).inputnums != 0 && (*selectinfo).inputinfo != NULL )
	{

		for( j = 0 ; j < (* selectinfo).inputnums ; j ++ )
		{
			if( (*selectinfo).inputinfo[j].value != NULL )
				free( (*selectinfo).inputinfo[j].value );
		}

		free( (*selectinfo).inputinfo );
	}

	if( (* selectinfo).outputnums != 0 && (*selectinfo).outputinfo != NULL )
	{

		for( j = 0 ; j < (* selectinfo).outputnums ; j ++ )
		{
                   if( (*selectinfo).outputinfo[j].Type != OCI_TYPECODE_BLOB )
			if( (*selectinfo).outputinfo[j].Value != NULL )
				free( (*selectinfo).outputinfo[j].Value );

			/*BLOB*/
			if( (*selectinfo).outputinfo[j].Type == OCI_TYPECODE_BLOB )
			{
				OCIDescriptorFree( (dvoid*)((*selectinfo).outputinfo[j].blob), (ub4) OCI_DTYPE_LOB );
			}
		}

		free( (*selectinfo).outputinfo );
	}

	memset( selectinfo, 0 , sizeof ( SELECTINFO ) );

	return 0;
}
int TOCIQuery::FreeSelectout( SELECTINFO * selectinfo )
{
	int j;

	if( selectinfo == NULL )
		return 0;

	if( (* selectinfo).outputnums != 0 && (*selectinfo).outputinfo != NULL )
	{

		for( j = 0 ; j < (* selectinfo).outputnums ; j ++ )
		{
			if( (*selectinfo).outputinfo[j].Value != NULL )
			(*selectinfo).outputinfo[j].Value=NULL;

		}

	}

      //	memset( selectinfo, 0 , sizeof ( SELECTINFO ) );

	return 0;
}

int TOCIQuery::FreeSel_cursor( OCI_CURSOR * cursor )
{
  if( cursor == NULL )
       return 0;
   free( (*cursor).sSQLStmt );
   if(  (*cursor).selhp != 0 )
     OCIHandleFree(  (*cursor).selhp , OCI_HTYPE_STMT );
  free( cursor );

  return 0;
}

int TOCIQuery::OCI_CloseCursor(void * & Handle)
{
	if( (*(OCI_CURSOR*)Handle).selhp != 0 )
		OCIHandleFree( (*(OCI_CURSOR*)Handle).selhp , OCI_HTYPE_STMT );
	FreeSelectInfo( &((*(OCI_CURSOR*)Handle).selinfo));

	delete Handle;
	return 0;
}


//---------------------------------------------------------------------------
int TOCIQuery::AddSQL(char sqlstmt[1024*4])
{


  if( sel_cursor != 0 )	OCI_CloseCursor( sel_cursor );

  SetProcessWorkingSetSize(GetCurrentProcess(), 0xFFFFFFFF, 0xFFFFFFFF);
  envhp = DBHandle.envhp;
  errhp = DBHandle.errhp;
  svchp = DBHandle.svchp;
  srvhp = DBHandle.srvhp;
  authp = DBHandle.authp;
  status = 0;
  sel_cursor=0;
  csCount=0;
  stmthp1 = 0;

  status = OCIHandleAlloc( DBHandle.envhp, (dvoid**) &stmthp1,
	   		   OCI_HTYPE_STMT,0,0 );
  if( status != OCI_SUCCESS )
  {

          _OCIErrorGet( DBHandle,tmpbuf );
          strcpy( tmpname, "OCIHandleAlloc()" );
          sprintf( DBErrMsg,"=>调用 %s 错误(%s)",tmpname,tmpbuf);
          throw Exception(DBErrMsg);
        //  return status;
  }

  StrCopy(SQLStr,sqlstmt);
  sel_cursor = new OCI_CURSOR;
  memset(sel_cursor, 0, sizeof(OCI_CURSOR) );
  (*(OCI_CURSOR*)sel_cursor).DBHandle = DBHandle;
  OCIStmt * *selhp=&((*(OCI_CURSOR*)sel_cursor).selhp) ;
  status = OCIHandleAlloc( envhp, (dvoid**) selhp,OCI_HTYPE_STMT,0,0 );
  if( status == OCI_ERROR )
  {
     _OCIErrorGet( DBHandle,tmpbuf );
     strcpy( tmpname, "OCIHandleAlloc()" );
     sprintf( DBErrMsg,"=>调用 %s 错误(%s)",tmpname,tmpbuf);
     throw Exception(DBErrMsg);
     //return status;
  }
  selecthp = *selhp;
  status = OCIStmtPrepare( selecthp, errhp,
				   (text*)SQLStr,(ub4)strlen(SQLStr),
				   (ub4)OCI_NTV_SYNTAX,(ub4)OCI_DEFAULT );

  if( status == OCI_ERROR )
  {
    _OCIErrorGet( DBHandle,tmpbuf );
     strcpy( tmpname, "OCIStmtPrepare()" );
     sprintf( DBErrMsg,"=>调用 %s 错误(%s)",tmpname,tmpbuf);
     throw Exception(DBErrMsg);
     //return status;
  }

  int optype=0;
  OCIAttrGet(selecthp, OCI_HTYPE_STMT, &optype, NULL, OCI_ATTR_STMT_TYPE, errhp);
  switch (optype)
  {
    case OCI_STMT_SELECT:StrCopy(FSQLType,"SQL_SELECT");
    break;
    case OCI_STMT_UPDATE:StrCopy(FSQLType,"SQL_UPDATE") ;
    break;
    case OCI_STMT_DELETE:StrCopy(FSQLType,"SQL_DELETE") ;
    break;
    case OCI_STMT_INSERT:StrCopy(FSQLType,"SQL_INSERT") ;
    break;
     case OCI_STMT_BEGIN:StrCopy(FSQLType,"SQL_PLSQL") ;
    break;
     case OCI_STMT_DECLARE:StrCopy(FSQLType,"SQL_PLSQL") ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -