📄 csqlocilibrary.cpp
字号:
break;
case CORACLEDriver::ORANUMBER:
columnInfo[iLoop].columnType=CVariant::Int;
columnInfo[iLoop].data=malloc(sizeof(OCINumber));
MYCHECK( errorhp ,
OCIDefineByPos(stmthp,&define,errorhp,iLoop+1,
(OCINumber *)(columnInfo[iLoop].data),
sizeof(OCINumber),SQLT_VNU,&(columnInfo[iLoop].ind),
&(columnInfo[iLoop].fetchLength),
&(columnInfo[iLoop].retCode),OCI_DEFAULT));
break;
case CORACLEDriver::ORADATE:
columnInfo[iLoop].columnType=CVariant::mydate;
columnInfo[iLoop].data=malloc(sizeof(OCIDate));
MYCHECK( errorhp ,
OCIDefineByPos(stmthp,&define,errorhp,iLoop+1,
(OCIDate *)(columnInfo[iLoop].data),
sizeof(OCIDate),SQLT_ODT,&(columnInfo[iLoop].ind),
&(columnInfo[iLoop].fetchLength),
&(columnInfo[iLoop].retCode),OCI_DEFAULT));
break;
default:
LOG(NULL , 0 , "UNDeal DataType in this version");
return false ;
break;
}
}
setActive (true); //执行命令成功,设为活动
return true; //成功返回true
}
bool CORACLEResult::multi_reset (const string & query)
{
return false ;
}
int CORACLEResult::get_result()
{
return 1 ;
}
int CORACLEResult::get_columns()
{
return columnCount;
}
bool CORACLEResult::cancel(bool bAll)
{
return true;
}
int CORACLEResult::size ()
{
return 0;
}
int CORACLEResult::numRowsAffected ()
{
return 0;
}
CORACLEBcpResult::CORACLEBcpResult (const CORACLEDriver * db):CSqlBcpResult(db)
//CSqlBcpResult类的Oracle实现,以db为驱动
{
connection = (CORACLEDriver*)db ;
envhp_ctl = db->envhp ;
svchp_ctl = db->svcctxhp ;
errorhp = db->errorhp ;
srvhp_ctl = db->srvhp ;
m_nowRow = 0 ;
m_buffer = NULL ;
m_buf = NULL ;
m_pos = 0 ;
columnInfo = NULL ;
}
CORACLEBcpResult::~CORACLEBcpResult () //析构
{
if (m_buffer ) delete[] m_buffer ;
m_buffer = NULL ;
if ( columnInfo ) delete[] columnInfo ;
columnInfo = NULL ;
m_pos = 0 ;
}
int CORACLEBcpResult::fCheckTable (const string & tableName)
{
/*
//以tableName调用私有类的syb_get_table_columns函数
int nReturn = thePrivate->syb_get_table_columns (tableName.c_str ());
DB_TRACE (5, "SYBASE", ("fCheckTable.columns:%d\n", nReturn));
return nReturn;
*/
return 1 ; // 暂时不用
}
bool CORACLEBcpResult::fCheckPoint ()
{
return connection->_exec("commit") ;
}
bool CORACLEBcpResult::fBind (char (*buf)[SYBASE_MAX_COLUMN_LENGTH], int len)
{
m_buf = buf ;
return true ;
}
bool CORACLEBcpResult::fStart (const string & tableName)
{
char query[256];
sprintf( g_errMsg , "bcp table %s " , tableName.c_str() ) ;
// sprintf( query , "select * from %s for update nowait" , tableName.c_str() );
// LOG1(NULL , 0 , "%s" , query );
// connection->_exec( "ALTER SESSION SET NLS_DATE_FORMAT = 'YYMMDD HH24:MI:SS'");
connection->_exec( "commit" ) ;
LOG(NULL , 0 , "11111111" );
int b_error ;
sword status ;
ub2 col_num ;
setActive( false ) ;
// 利用 select * from tableName 来判断表的结果,包括字段类型,名称等
OCIDescribe *dschp;
OCIParam *parmp, *collst;
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)envhp_ctl,
(dvoid **)&dschp,(ub4) OCI_HTYPE_DESCRIBE,(size_t)0, (dvoid **)0));
MYCHECK( errorhp ,
OCIDescribeAny(svchp_ctl, errorhp, (dvoid *)tableName.c_str(),
(ub4) strlen((char *) tableName.c_str()),
OCI_OTYPE_NAME, (ub1)1,
OCI_PTYPE_TABLE, dschp) ) ;
MYCHECK( errorhp ,
OCIAttrGet((dvoid *)dschp, (ub4)OCI_HTYPE_DESCRIBE,
(dvoid *)&parmp, (ub4 *)0, (ub4)OCI_ATTR_PARAM,
(OCIError *)errorhp));
MYCHECK( errorhp ,
OCIAttrGet((dvoid*) parmp, (ub4) OCI_DTYPE_PARAM,
(dvoid*) &collst, (ub4 *) 0,
(ub4) OCI_ATTR_LIST_COLUMNS, (OCIError *)errorhp));
MYCHECK( errorhp ,
OCIAttrGet((dvoid*) parmp, (ub4) OCI_DTYPE_PARAM,
(dvoid*) &col_num , (ub4 *) 0,
(ub4) OCI_ATTR_NUM_COLS, (OCIError *)errorhp));
// cout<<"row"<<col_num<<endl;
if ( columnInfo != NULL )
{
delete[] columnInfo;
columnInfo = NULL ;
}
columnInfo = new CColumn[ col_num ] ;
int all_col_size = 0 ;
text * columnName;
char cTempName[100] ;
ub4 sizep ;
LOG1(NULL , 0 , "START==>col_num = %d" , col_num );
for(int iLoop=0;iLoop<col_num;iLoop++)
{
OCIParam * param;
ub2 columnType,columnSize;
text * columnName;
MYCHECK( errorhp ,
OCIParamGet((dvoid *)collst, (ub4)OCI_DTYPE_PARAM,
errorhp, (dvoid **)¶m, (ub4) iLoop+1));
// 读取列类型
MYCHECK( errorhp ,
OCIAttrGet(param,OCI_DTYPE_PARAM,&columnType,0,
OCI_ATTR_DATA_TYPE,errorhp));
columnInfo[iLoop].dbType=columnType ;
/*
if ( columnType != SQLT_DAT )
{
// 不是日期类型 , 都可以用字符进行转换
LOG1(NULL , 0 , "other type %d " , columnType );
columnInfo[iLoop].dbType=SQLT_CHR ;
}
else
{
LOG1(NULL , 0 , "date type %d " , columnType );
columnInfo[iLoop].dbType=SQLT_DAT ;
}
*/
// 读列大小
MYCHECK( errorhp ,
OCIAttrGet(param,OCI_DTYPE_PARAM,&columnSize,0,
OCI_ATTR_DATA_SIZE,errorhp));
//all_col_size += columnSize ;
all_col_size += columnSize+1 ;
columnInfo[iLoop].columnSize=columnSize ;
// 读列名
MYCHECK( errorhp ,
OCIAttrGet(param,OCI_DTYPE_PARAM,(dvoid*)&columnName,(ub4*) &sizep ,
OCI_ATTR_NAME,errorhp));
strncpy( cTempName , (char*) columnName , (size_t) sizep ) ;
cTempName[sizep]=0 ;
//checkError(errorhp,status);
columnInfo[iLoop].columnName = cTempName ;
//columnInfo[iLoop].columnName = (char*)columnName ;
// LOG1(NULL , 0 , "column name %s " ,cTempName );
}
LOG1(NULL , 0 , "END==>col_num = %d" , col_num );
MYCHECK( errorhp ,
OCIHandleFree((dvoid *) dschp, (ub4) OCI_HTYPE_DESCRIBE));
/* 表结构读取结束*/
int waitSecond = 300 ;
int waitNum = 0 ;
RESTART:
LOG(NULL , 0 , "DEBUG==> 1111" );
// 初始化 bcp 结构
ub2 columnCount = col_num ;
OCIParam *colDesc ;
OCIParam *colLstDesc_ctl;
// alloc dpctx_ctl
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)envhp_ctl,
(dvoid **)&dpctx_ctl,
(ub4)OCI_HTYPE_DIRPATH_CTX,
(size_t)0, (dvoid **)0));
// 设表名
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)dpctx_ctl, (ub4)OCI_HTYPE_DIRPATH_CTX,
(dvoid *)tableName.c_str(),
(ub4)strlen(tableName.c_str()),
(ub4)OCI_ATTR_NAME, errorhp));
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)dpctx_ctl, (ub4)OCI_HTYPE_DIRPATH_CTX,
(dvoid *)"",
(ub4)0,
(ub4)OCI_ATTR_SCHEMA_NAME, errorhp));
// 设置列数
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)dpctx_ctl, (ub4)OCI_HTYPE_DIRPATH_CTX,
(dvoid *)&columnCount, (ub4)0, (ub4)OCI_ATTR_NUM_COLS, errorhp));
// LOG1(NULL, 0 , "column count %d " , columnCount ) ;
// alloc colLstDesc_ctl
MYCHECK( errorhp ,
OCIAttrGet((dvoid *)dpctx_ctl,
OCI_HTYPE_DIRPATH_CTX,
(dvoid *)&colLstDesc_ctl, (ub4 *)0,
OCI_ATTR_LIST_COLUMNS, errorhp));
LOG(NULL , 0 , "DEBUG==> 3333" );
ub2 type_col ;
ub4 len_col ;
int i ;
LOG(NULL , 0 , "DEBUG==> 4444" );
for ( i = 1 ; i<= columnCount ; i++ )
{
type_col = columnInfo[i-1].dbType ;
if ( type_col != SQLT_DAT )
{
type_col = SQLT_CHR ;
}
len_col = columnInfo[i-1].columnSize;
MYCHECK( errorhp ,
OCIParamGet((CONST dvoid *)colLstDesc_ctl,
(ub4)OCI_DTYPE_PARAM, errorhp,
(dvoid **)&colDesc, i));
// 设置列名
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)colDesc, (ub4)OCI_DTYPE_PARAM,
(dvoid *)columnInfo[i-1].columnName.c_str(),
(ub4)columnInfo[i-1].columnName.length(),
(ub4)OCI_ATTR_NAME, errorhp));
// 设置列类型
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)colDesc, (ub4)OCI_DTYPE_PARAM,
(dvoid *)&type_col, (ub4)0,
(ub4)OCI_ATTR_DATA_TYPE, errorhp));
// 设置列大小
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)colDesc, (ub4)OCI_DTYPE_PARAM,
(dvoid *)&len_col, (ub4)0,
(ub4)OCI_ATTR_DATA_SIZE, errorhp));
OCIDescriptorFree((dvoid *)colDesc, OCI_DTYPE_PARAM);
}
LOG(NULL , 0 , "DEBUG==> 5555" );
ub4 xfrsz_tbl = 64*1024;
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)dpctx_ctl, (ub4)OCI_HTYPE_DIRPATH_CTX,
(dvoid *)&xfrsz_tbl,
(ub4)0, (ub4)OCI_ATTR_BUF_SIZE, errorhp));
LOG(NULL , 0 , "DEBUG==> 6666" );
// 准备 dpctx
MYCHECK_NR( errorhp ,
OCIDirPathPrepare(dpctx_ctl, svchp_ctl, errorhp));
if ( g_status != OCI_SUCCESS ) {
OCIHandleFree((dvoid*)dpctx_ctl, OCI_HTYPE_DIRPATH_CTX);
if ( g_errcode == 604 )
{ // resource busy now
sleep(1) ;
LOG(NULL , 0 , "sleep 1 " );
if ( waitNum++ >= waitSecond )
{
LOG(NULL , 0 , "ERR : lost force " ) ;
return false ;
}
goto RESTART ;
}
else
{
return false ;
}
}
LOG(NULL , 0 , "DEBUG==> 6666==>1111" );
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)dpctx_ctl, (ub4)OCI_HTYPE_DIRPATH_CTX,
(dvoid *)"",
(ub4)0,
(ub4)OCI_ATTR_DATEFORMAT, errorhp));
ub1 parallel = 0 ;
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)dpctx_ctl, (ub4)OCI_HTYPE_DIRPATH_CTX,
(dvoid *)¶llel,
(ub4)0, (ub4)OCI_ATTR_DIRPATH_PARALLEL,errorhp));
// alloc dpca_ctl
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)dpctx_ctl, (dvoid **)&dpca_ctl,
(ub4)OCI_HTYPE_DIRPATH_COLUMN_ARRAY,
(size_t)0, (dvoid **)0));
LOG(NULL , 0 , "DEBUG==> 6666==>2222" );
// alloc dpstr_ctl
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)dpctx_ctl,(dvoid **)&dpstr_ctl,
(ub4)OCI_HTYPE_DIRPATH_STREAM,
(size_t)0, (dvoid **)0));
LOG(NULL , 0 , "DEBUG==> 7777" );
// 取alloc的行数
MYCHECK( errorhp ,
OCIAttrGet(dpca_ctl, (ub4)OCI_HTYPE_DIRPATH_COLUMN_ARRAY,
&nrow_ctl, 0, OCI_ATTR_NUM_ROWS,
errorhp));
LOG1(NULL , 0 , "row num is %d " , nrow_ctl );
// 取alloc的列数
LOG(NULL , 0 , "DEBUG==> 8888" );
MYCHECK( errorhp ,
OCIAttrGet(dpca_ctl, (ub4)OCI_HTYPE_DIRPATH_COLUMN_ARRAY,
&ncol_ctl, 0, OCI_ATTR_NUM_COLS,
errorhp));
// LOG2(NULL , 0 , "col %d row %d " , ncol_ctl , nrow_ctl ) ;
LOG1(NULL , 0 , "col num is %d " , ncol_ctl );
// 置空dpca_ctl
(void) OCIDirPathColArrayReset(dpca_ctl, errorhp);
// 置空dpstr_ctl
(void) OCIDirPathStreamReset(dpstr_ctl, errorhp);
LOG(NULL , 0 , "DEBUG==> 9999" );
// m_buffer 是输入数据的缓冲区
if ( m_buffer )
{
delete[] m_buffer ;
m_buffer = NULL ;
}
LOG(NULL , 0 , "DEBUG==> 0000" );
// new memory
// LOG1(NULL, 0 , "alloc row %d " , nrow_ctl );
m_bufferLen = (nrow_ctl+10) * all_col_size ;
m_buffer = new char[ m_bufferLen ] ;
memset( m_buffer , 0 , m_bufferLen );
m_pos = 0 ; //m_buffer的当前指针
m_BatchRows = nrow_ctl - 4 ;
if ( m_BatchRows < 0 ) m_BatchRows = 0 ;
setActive( true ) ;
return true ;
}
bool CORACLEBcpResult::reset()
{
// 将一批数据入库,并置空dpstr_ctl和dpca_ctl
// LOG( NULL, 0 , "reset") ;
//if ( m_nowRow == 0 ) return true ;
MYCHECK_NR( errorhp ,
OCIDirPathColArrayToStream(dpca_ctl, dpctx_ctl,
dpstr_ctl, errorhp,
m_nowRow+1 , 0));
if ( g_status != OCI_SUCCESS ) {
LOG( m_buffer , m_pos , "row err1" ) ;
OCIDirPathColArrayReset(dpca_ctl, errorhp);
OCIDirPathStreamReset(dpstr_ctl, errorhp);
memset( m_buffer , 0 , m_bufferLen );
m_nowRow = 0 ;
m_pos = 0 ;
return false ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -