📄 csqlocilibrary.cpp
字号:
}
MYCHECK_NR( errorhp ,
OCIDirPathLoadStream(dpctx_ctl, dpstr_ctl,
errorhp));
if ( g_status != OCI_SUCCESS ) {
ub4 commit_col ;
// LOG1( m_buffer , m_pos , "row err2 %d " , m_nowRow ) ;
// OCIAttrGet( dpctx_ctl , OCI_HTYPE_DIRPATH_CTX, (dvoid*) &commit_col , 0 , OCI_ATTR_ROW_COUNT , errorhp ) ;
// LOG1( NULL , 0, "commit row %d " , commit_col ) ;
OCIDirPathStreamReset(dpstr_ctl, errorhp);
OCIDirPathColArrayReset(dpca_ctl, errorhp);
memset( m_buffer , 0 , m_bufferLen );
m_nowRow = 0 ;
m_pos = 0 ;
return false ;
}
OCIDirPathColArrayReset(dpca_ctl, errorhp);
OCIDirPathStreamReset(dpstr_ctl, errorhp);
memset( m_buffer , 0 , m_bufferLen );
m_nowRow = 0 ;
m_pos = 0 ;
return true ;
}
bool CORACLEBcpResult::fSend ()
{
int i ;
ub4 len ;
// LOG1( NULL, 0 , "send col %d " , ncol_ctl );
for ( i = 1 ; i <= ncol_ctl ; i++ )
{
if ( columnInfo[i-1].dbType == SQLT_DAT )
{ // 日期类型
char* ptrBuffer = m_buffer+m_pos ;
char sDate[30] ;
char* pDate ;
strncpy( sDate , m_buf[i] , 30 ) ;
// 拼凑日期类型,共7个字节
int century = 120 ; // 世纪 2000 20+100 = 120
int year=0 , mon=0 , day=0 ;
int hour=0 , min=0 , sec=0 ;
len = 7;
pDate = sDate ;
*(pDate+4) = 0 ; year = atoi( pDate ) ; pDate+=(4+1) ;
*(pDate+2) = 0 ; mon = atoi( pDate ) ; pDate+=(2+1) ;
*(pDate+2) = 0 ; day = atoi( pDate ) ; pDate+=(2+1) ;
*(pDate+2) = 0 ; hour = atoi( pDate ) ; pDate+=(2+1) ;
*(pDate+2) = 0 ; min = atoi( pDate ) ; pDate+=(2+1) ;
*(pDate+2) = 0 ; sec = atoi( pDate ) ; pDate+=(2+1) ;
// sscanf( m_buf[i] , "%04d/%02d/%02d %02d:%02d:%02d" ,
// &year , &mon , &day ,
// &hour , &min , &sec );
if ( mon < 1 || mon > 12 || day < 1 || day > 31 || hour <0 || hour > 23 || min <0 || min > 59 || sec <0 || sec > 59 ) {
LOG4(NULL , 0 , "col = %d ==>error date %s mon %d day %d " ,i, m_buf[i] , mon , day );
LOG4(NULL , 0 , "col = %d ==>hour %d min %d sec %d " , i,hour , min , sec );
}
year = year%100 ;
year += 100 ; // 年需要+100
hour++ ; // 小时+1
min++ ; // 分钟+1
sec++ ; // 妙+1
(*(ptrBuffer)) = century ;
(*(ptrBuffer+1)) = year ;
(*(ptrBuffer+2)) = mon ;
(*(ptrBuffer+3)) = day ;
(*(ptrBuffer+4)) = hour ;
(*(ptrBuffer+5)) = min ;
(*(ptrBuffer+6)) = sec ;
// 设置列数据
g_status = OCIDirPathColArrayEntrySet(dpca_ctl, errorhp,
m_nowRow, i-1 ,
(ub1*)m_buffer + m_pos , len, OCI_DIRPATH_COL_COMPLETE) ;
m_pos += ( len +1 ) ;
}
else
{
if ( m_buf[i][0] == 0 ) {
len = 1 ;
if ( columnInfo[i-1].dbType == 2 ) //number
{
strcpy( m_buffer+m_pos , "0" );
}
else
{
strcpy( m_buffer+m_pos , " " );
}
}
else
{
len = strlen( m_buf[i] ) ;
if ( len > columnInfo[i-1].columnSize ) {
LOG3(NULL , 0 , "colmne %s is too large : column size need %d , now value %s " , columnInfo[i-1].columnName.c_str() , columnInfo[i-1].columnSize , m_buf[i] ) ;
len = columnInfo[i-1].columnSize ;
}
strncpy( m_buffer + m_pos , m_buf[i] , columnInfo[i-1].columnSize);
}
g_status = OCIDirPathColArrayEntrySet(dpca_ctl, errorhp,
m_nowRow, i-1 ,
(ub1*)m_buffer + m_pos , len, OCI_DIRPATH_COL_COMPLETE) ;
m_pos += (len+1) ;
}
// LOG3(NULL , 0 , "col %d pos %d value <%s> " , i , m_pos, m_buf[i] );
if ( g_status != OCI_SUCCESS ) {
LOG1(NULL , 0 , "bcp column %d fail " , i ) ;
}
}
//m_nowRow++ ;
// 当dpca_ctl满后录入一批
if ( m_nowRow == m_BatchRows )
{
reset();
return true ;
}
m_nowRow++ ;
return true ;
}
bool CORACLEBcpResult::fEnd (bool all)
{
if ( m_nowRow )
{
m_nowRow--;
reset(); // 把剩余的录入
}
if ( all == false )
{
return true ;
}
// 结束bcp
MYCHECK( errorhp ,
OCIDirPathFinish(dpctx_ctl, errorhp));
if ( m_buffer ) delete[] m_buffer ;
m_buffer = NULL ;
m_pos = 0 ;
//释放资源
OCIHandleFree((dvoid*)dpca_ctl , OCI_HTYPE_DIRPATH_COLUMN_ARRAY );
OCIHandleFree((dvoid*)dpstr_ctl , OCI_HTYPE_DIRPATH_STREAM );
OCIHandleFree((dvoid*)dpctx_ctl, OCI_HTYPE_DIRPATH_CTX);
connection->_exec("commit");
return true ;
}
CORACLEDriver::CORACLEDriver () //构造函数
{
errhp2_ctl = NULL;
authp_ctl = NULL ;
envhp = NULL ;
errorhp = NULL ;
srvhp = NULL ;
svcctxhp = NULL ;
}
CORACLEDriver::~CORACLEDriver () //析构函数
{
close();
}
bool CORACLEDriver::open (const string & db,
const string & user,
const string & password,
const string & host, const string & appname, int port)
{
int b_error ;
LOG3(NULL,0,"user = %s,password = %s,host = %s",user.c_str(),password.c_str(),host.c_str());
if (isOpen ()) //如果当前驱动处于打开状态
{
close (); //将之关闭
}
sword status;
setOpen (false); //置打开未为false
setOpenError (true); //置错误位为true
MYCHECK( errorhp ,
OCIInitialize((ub4) OCI_OBJECT, (dvoid *)0,
NULL , NULL , NULL ));
MYCHECK( errorhp ,
OCIEnvInit((OCIEnv **)&envhp, OCI_DEFAULT, (size_t)0,
(dvoid **)0));
/* allocate error handles */
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)envhp,
(dvoid **)&errorhp, OCI_HTYPE_ERROR,
(size_t)0, (dvoid **)0));
/*
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)envhp,
(dvoid **)&ctlp->errhp2_ctl, OCI_HTYPE_ERROR,
(size_t)0, (dvoid **)0));
*/
/* server contexts */
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)envhp,
(dvoid **)&srvhp, OCI_HTYPE_SERVER,
(size_t)0, (dvoid **)0));
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)envhp,
(dvoid **)&svcctxhp, OCI_HTYPE_SVCCTX,
(size_t)0, (dvoid **)0));
// 设置数据库
MYCHECK( errorhp ,
OCIServerAttach(srvhp, errorhp,
(text*)host.c_str(),
(sb4)strlen((const char *)host.c_str()),
OCI_DEFAULT));
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)svcctxhp, OCI_HTYPE_SVCCTX,
(dvoid *)srvhp, (ub4)0, OCI_ATTR_SERVER,
errorhp));
MYCHECK( errorhp ,
OCIHandleAlloc((dvoid *)envhp,
(dvoid **)&authp_ctl, (ub4)OCI_HTYPE_SESSION,
(size_t)0, (dvoid **)0));
// 设置用户名
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)authp_ctl, (ub4)OCI_HTYPE_SESSION,
(dvoid *)user.c_str(),
(ub4)strlen((char *)user.c_str()),
(ub4)OCI_ATTR_USERNAME, errorhp));
// 设置密码
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)authp_ctl, (ub4)OCI_HTYPE_SESSION,
(dvoid *)password.c_str(),
(ub4)strlen((char *)password.c_str()),
(ub4)OCI_ATTR_PASSWORD, errorhp));
MYCHECK( errorhp ,
OCISessionBegin(svcctxhp, errorhp, authp_ctl,
OCI_CRED_RDBMS, (ub4)OCI_DEFAULT));
MYCHECK( errorhp ,
OCIAttrSet((dvoid *)svcctxhp, (ub4)OCI_HTYPE_SVCCTX,
(dvoid *)authp_ctl, (ub4)0, (ub4)OCI_ATTR_SESSION,
errorhp));
setOpen (true); //置打开位为true
setOpenError (false); //置错误位为false
// _exec( "ALTER SESSION SET NLS_DATE_FORMAT = 'YYMMDD HH24:MI:SS'");
_exec("commit" );
return true;
}
void CORACLEDriver::reset(sb4 ORAERRCODE)
{
if((ORAERRCODE == 12560)
||(ORAERRCODE == 1012)
||(ORAERRCODE == 3113)
||(ORAERRCODE == 3114)
||(ORAERRCODE == 12571))
{
close();
}
}
bool CORACLEDriver::_exec( const string& sql )
{
text errbuf[512];
ub4 col_num ;
OCIStmt * stmthp;
LOG1(NULL,0,"CORACLEDriver::_exec==>%s",sql.c_str());
MYCHECK_NR( errorhp ,
OCIHandleAlloc(
(dvoid *)envhp,
(dvoid **)&stmthp,
(ub4)OCI_HTYPE_STMT,
(size_t)0, (dvoid **)0
));
if(g_error)
{
if(errorhp!=NULL)
{
OCIErrorGet((dvoid *) errorhp, (ub4) 1,(text *)NULL,&g_errcode,
errbuf,(sb4)sizeof(errbuf),(ub4)OCI_HTYPE_ERROR);
LOG2(NULL,0,"OCIHandleAlloc==>g_errcode = %d==>errmsg = %s",g_errcode,errbuf);
ERRLOG1(NULL,0,"CORACLEDriver::_exec==>%s",sql.c_str());
ERRLOG2(NULL,0,"OCIHandleAlloc==>g_errcode = %d==>errmsg = %s",g_errcode,errbuf);
reset(g_errcode);
}
else
{
LOG(NULL , 0 , "OCIHandleAlloc==>errorhp==NULL==>Error No Reason");
ERRLOG1(NULL,0,"CORACLEDriver::_exec==>%s",sql.c_str());
ERRLOG(NULL , 0 , "OCIHandleAlloc==>errorhp==NULL==>Error No Reason");
}
return false ;
}
MYCHECK_NR( errorhp ,
OCIStmtPrepare(
stmthp,errorhp,
(text *)sql.c_str(),(ub4)strlen(sql.c_str()),
OCI_NTV_SYNTAX,OCI_DEFAULT
));
if(g_error)
{
if(errorhp!=NULL)
{
OCIErrorGet((dvoid *) errorhp, (ub4) 1,(text *)NULL,&g_errcode,
errbuf,(sb4)sizeof(errbuf),(ub4)OCI_HTYPE_ERROR);
LOG2(NULL,0,"OCIStmtPrepare==>g_errcode = %d==>errmsg = %s",g_errcode,errbuf);
ERRLOG1(NULL,0,"CORACLEDriver::_exec==>%s",sql.c_str());
ERRLOG2(NULL,0,"OCIStmtPrepare==>g_errcode = %d==>errmsg = %s",g_errcode,errbuf);
reset(g_errcode);
}
else
{
LOG(NULL , 0 , "OCIStmtPrepare==>errorhp==NULL==>Error No Reason");
ERRLOG1(NULL,0,"CORACLEDriver::_exec==>%s",sql.c_str());
ERRLOG(NULL , 0 , "OCIStmtPrepare==>errorhp==NULL==>Error No Reason");
}
return false ;
}
MYCHECK_NR( errorhp ,
OCIStmtExecute(svcctxhp,stmthp,errorhp,1,0,
NULL,NULL,OCI_COMMIT_ON_SUCCESS));
if(g_error)
{
if(errorhp!=NULL)
{
OCIErrorGet((dvoid *) errorhp, (ub4) 1,(text *)NULL,&g_errcode,
errbuf,(sb4)sizeof(errbuf),(ub4)OCI_HTYPE_ERROR);
LOG2(NULL,0,"OCIStmtExecute==>g_errcode = %d==>errmsg = %s",g_errcode,errbuf);
ERRLOG1(NULL,0,"CORACLEDriver::_exec==>%s",sql.c_str());
ERRLOG2(NULL,0,"OCIStmtExecute==>g_errcode = %d==>errmsg = %s",g_errcode,errbuf);
reset(g_errcode);
}
else
{
LOG(NULL , 0 , "OCIStmtExecute==>errorhp==NULL==>Error No Reason");
ERRLOG1(NULL,0,"CORACLEDriver::_exec==>%s",sql.c_str());
ERRLOG(NULL , 0 , "OCIStmtExecute==>errorhp==NULL==>Error No Reason");
}
return false ;
}
OCIHandleFree(stmthp,OCI_HTYPE_STMT);
return true ;
}
void CORACLEDriver::close ()
{
if (errorhp && srvhp)
{
(void) OCIServerDetach(srvhp, errorhp, OCI_DEFAULT );
OCIHandleFree((dvoid *)srvhp, OCI_HTYPE_SERVER);
}
if (svcctxhp)
(void) OCIHandleFree((dvoid *) svcctxhp, OCI_HTYPE_SVCCTX);
if (errorhp)
(void) OCIHandleFree((dvoid *) errorhp, OCI_HTYPE_ERROR);
errhp2_ctl = NULL;
authp_ctl = NULL ;
envhp = NULL ;
errorhp = NULL ;
srvhp = NULL ;
svcctxhp = NULL ;
setOpen (false); //置打开位为false
setOpenError (false); //置错误位为false
}
bool CORACLEDriver::useDb (const string & db) const
{
return true ;
}
//返回基于当前驱动查询结果集的查询处理类
CSqlQuery CORACLEDriver::createQuery () const
{
return CSqlQuery (new CORACLEResult (this)); //以当前驱动为参数,构造查询结果集,返回基于此结果集的查询处理类
}
//返回基于当前驱动bcp结果集的bcp处理类
CSqlBcp CORACLEDriver::createBcp () const
{
return CSqlBcp (new CORACLEBcpResult (this)); //以当前驱动为参数,构造bcp结果集,返回基于此结果集的bcp处理类
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -