temp code

//#include "StdAfx.h"
//#include <tchar.h>
#include "ConnectPool.h"
#include <fstream>
#include<iostream>
using namespace std;
//#pragma comment(lib, "oci.lib")


#define __int64 long unsigned int

ConnectPool::ConnectPool(void)
{
otl_connect::otl_initialize(1);
}

ConnectPool::~ConnectPool(void)
{
db.logoff();
otl_connect::otl_terminate();
}

int ConnectPool::Connect(char *pcon_str)
{
try
{
db.logoff();
db.rlogon(pcon_str,0); // connect to Oracle
}
catch(otl_exception& p)
{
cout << "数据库连接发生异常,异常信息:" << p.msg <<endl;
char szLogBuf[512];
memset(szLogBuf,0,512);
sprintf(szLogBuf,"数据库连接发生异常,异常信息:%s\n",p.msg);
WriteLog(szLogBuf);
return -1;
}
return 1;
}

int ConnectPool::DisConnect()
{
try
{
db.logoff();
}
catch(otl_exception& p)
{
return -1;
}
return 1;
}
__int64 ConnectPool::htonll(__int64 i64In)
{
__int64 i64Out=0;
for (int i=0;i<8;i++)
{
i64Out = ((i64Out<<8)+((i64In>>8*i)&255));
}
return i64Out;
}
__int64 ConnectPool::ntohll(__int64 i64In)
{
return htonll(i64In);
}

int ConnectPool::ExecuteSQL(char *pSQL)
{
otl_stream o;
try
{
o.open(1,pSQL,db);
db.commit();
}
catch(otl_exception& p)
{
return -2;
}
return 0;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名       : trim
// 功能描述     : 去空格
// 参数         : char *szStr [in][out]
// 返回值       : 去空格后的字符串
//
///////////////////////////////////////////////////////////////////////
char * ConnectPool::trim(char *szStr)
{
char *p = NULL;
p = szStr;
while ( *p == ' ')
{
p++;
}
strcpy(szStr,p);
p = szStr+strlen(szStr)-1;
while (*p == ' ')
{
*p=0;
p--;
}
return szStr;
}

///////////////////////////////////////////////////////////////////////
//
// 函数名       : find
// 功能描述     : 查找字符所在的位置
// 参数         : char *szStr [in][out]
// 返回值       : 字符所在的位置
//
///////////////////////////////////////////////////////////////////////
int ConnectPool::find(char *szStr,char szSubStr)
{
int iRes = 0;
while (( *szStr != szSubStr)&&(szStr!=NULL))
{
szStr++;
iRes++;
}
return iRes;
}

///////////////////////////////////////////////////////////////////////
//
// 函数名       : Mid
// 功能描述     : 定位
// 参数         : char *szStr [in][out]
// 返回值       : 字符串
//
///////////////////////////////////////////////////////////////////////
char * ConnectPool::Mid(char *szStr,int nPos)
{
char *p = NULL;
p = szStr;
while (nPos>0)
{
p++;
nPos--;
}
return p;
}

int ConnectPool::GetDownLoadFile(char *szPlatFormID,char *szFileTable,char *szGID,char *szGFilePath,char *szGFileName,char *szGDealedLine,int nProType,int nTotNumer)
{
char  szSqlBuf[1024] = {0};
sprintf(szSqlBuf,"select to_char(FTP_ID),PATH,NAME,to_char(DEALED_LINE) from %s where MOD(PRO_TYPE,%d)=%d and PROCESSED != '1' and IS_VALID=1 and  PROCESSED_COUNT<MAX_PROC_COUNT  and PLATFORM_CODE=%s",szFileTable,nTotNumer,nProType,szPlatFormID);
try
{
otl_stream o(1,szSqlBuf,db);
//表示数据存在
if(!o.eof())
{
o>>szGID;
o>>szGFilePath;
o>>szGFileName;
o>>szGDealedLine;
trim(szGID);
trim(szGFilePath);
trim(szGFileName);
trim(szGDealedLine);
//sprintf(szSqlBuf,"update %s set processed=2,PROCESSED_COUNT=PROCESSED_COUNT+1,UPDATE_DATE=sysdate where SYNC_NO=%s ",gSysInfo.szPacketTable,szID);
//otl_stream o2(1,szSqlBuf,db);
db.commit();
}
else{
return 1;
}
}
catch(otl_exception& p)
{
char szxx[1024] = { 0 };
strcpy(szxx,(const char *)p.msg);
return -2;
}
return 0;
}

/// check whether have datas needed to be download
bool ConnectPool::bckDownLoadData(char *szCkTable, char *szStatus,int *szReadLine,__int64 *lProID )
{
//char szProcID[10]={0};
double  lProIDs;
//long lProID=0;
char  szSqlBuf[1024] = {0};
//sprintf(szSqlBuf,"select id,row_num from %s where is_valid=1 and is_success=%s",szCkTable,szStatus);///获取需要操作的数据
sprintf(szSqlBuf,"select label_dispatch_id,fetch_process from %s where is_valid=1 and is_success=1 and group_process=0 and FETCH_PROCESS<100",szCkTable);
try
{
otl_stream o(1,szSqlBuf,db);
//表示数据存在
if(!o.eof())
{
o>>lProIDs;
*lProID=lProIDs;
//o>>(*lProID);
o>>(*szReadLine);
/* o>>szDataNums
trim(szProcID);
trim(szDataNums);*/
//sprintf(szSqlBuf,"update %s set processed=2,PROCESSED_COUNT=PROCESSED_COUNT+1,UPDATE_DATE=sysdate where SYNC_NO=%s ",gSysInfo.szPacketTable,szID);
// =2 means the uploading data.
sprintf(szSqlBuf,"update %s set FETCH_PROCESS=2,update_date=sysdate where LABEL_DISPATCH_ID=%ld",szCkTable,lProID);
otl_stream o2(1,szSqlBuf,db);
db.commit();
return true;
}
else{
return false;
}
}
catch(otl_exception& p)
{
char szxx[1024] = { 0 };
strcpy(szxx,(const char *)p.msg);
return -2;
}
return 0;
}
//getData Needed to Be Done
int ConnectPool::RedadMobileArr(char szReadTable[256],int nMohileArr[100],int nFlag)
{
char szSqlBuf[512]={0};
char szLogBuf[512]={0};
memset(szSqlBuf,0,512);
memset(szLogBuf,0,512);
//__int64 num=0;
//distinct  substr(mobile,10,11)
sprintf(szSqlBuf,"select distinct  substr(mobile,10,11) as subMobile from %s where substr(mobile,10,11)>%d order by substr(mobile,10,11) asc ",szReadTable,nFlag);
otl_stream getDt_stream(1,szSqlBuf,db);
int arrCount=0;
char szNum[12]={0};
char szNum1[4]={0};
try
{
while(!(getDt_stream.eof()))
{
getDt_stream>>szNum1;
int ss=atoi(szNum1);
nMohileArr[arrCount]=ss;
arrCount++;
}
}
catch(otl_exception &p)
{
db.rollback();
memset(szLogBuf,0,512);
sprintf(szLogBuf,"SQL=%s||%s\n",p.stm_text,p.msg);
WriteLog(szLogBuf);
cout << "获取手机后两位异常:" << szLogBuf <<endl;
return -2;
}
return 0;
}
///get data

int ConnectPool::ReadData(char szRealFile[256],FILE *pfile,char szReadTable[128],int nFlag,__int64 lckID,int nModBased,int nModValue)
{
//char szMobile[16]={0};
double szMobile=0;
char szSex[32]={0};
char szArea[32]={0};
char szAge[32]={0};
char szBrand[32]={0};
char szGroupNo[32]={0};
char szSqlBuf[512]={0};
char szLogBuf[512]={0};
memset(szSqlBuf,0,512);
memset(szLogBuf,0,512);

char szFullName[1024]={0};
char szLineData[512] = {0};
int nCurReadCount = 0;
int nOneReadCount = 0;
int nTotReadCount = 0;

//sprintf(szSqlBuf,"select mobile,area,sex,age,brand,group_no from %s where is_valid=1",szReadTable);
//sprintf(szSqlBuf,"select mobile,area,sex,age,brand,group_no from %s where is_valid=1 and mod(mobile,100)=%d",szReadTable,nFlag);
sprintf(szSqlBuf,"select mobile,area,sex,age,brand,group_no from %s where is_valid=1 and partition_id=%d",szReadTable,nFlag);
//sprintf(szSqlBuf,"select mobile,area,sex,age,brand,group_no from %s where is_valid=1 and mod(mobile,100)=%d and mod(%d,%d)=%d",szReadTable,nFlag,nFlag,nModBased,nModValue);
try
{
otl_stream getdloadDt_stream(30,szSqlBuf,db);
char szWriteToFile[30]={0};
setbuf(stdout,NULL);
while(!getdloadDt_stream.eof())
{
getdloadDt_stream>>szMobile>>szArea>>szSex>>szAge>>szBrand>>szGroupNo;
sprintf(szWriteToFile,"%.0Lf|%s|%s|%s|%s|%s\r\n",szMobile,szArea,szSex,szAge,szBrand,szGroupNo);
fwrite(szWriteToFile,strlen(szWriteToFile),1,pfile);
memset(szWriteToFile,sizeof(szWriteToFile),0);
}
//更新控制表的状态
switch(nFlag)
{
case 99:
sprintf(szSqlBuf,"update T_LABEL_DISPATCH set is_success=3,FETCH_PROCESS=%d where  LABEL_DISPATCH_ID=%I64d",nFlag,lckID);
break;
default:
sprintf(szSqlBuf,"update T_LABEL_DISPATCH set FETCH_PROCESS=%d where  LABEL_DISPATCH_ID=%I64d",nFlag,lckID);
}
/*
if((99>nFlag)&&(nFlag>0))
{

sprintf(szSqlBuf,"update T_LABEL_DISPATCH set FETCH_PROCESS=%d where  LABEL_DISPATCH_ID=%I64d",nFlag,lckID);
}
else
{
sprintf(szSqlBuf,"update T_LABEL_DISPATCH set is_success=3,FETCH_PROCESS=%d where  LABEL_DISPATCH_ID=%I64d",nFlag,lckID);
}
*/
otl_stream up_stream(1,szSqlBuf,db);
db.commit();
return 0; //读完一个分区
}
catch(otl_exception &p)
{
//pfile->close();
fclose(pfile);
if(nFlag<99)
{
remove(szRealFile);
//pfile=NULL;
//delete pfile;
db.rollback();
}
printf("%s",szLogBuf);
memset(szLogBuf,0,512);
sprintf(szLogBuf,"SQL=%s||%s\n",p.stm_text,p.msg);
WriteLog(szLogBuf);
cout << "文件入库异常:" << szLogBuf <<endl;
return -2;
}
}

int ConnectPool::InsertData(char *szPlatFormID,char *szFileTable,char *szDataTable,char *szExcpTable,int nCommitNum,char *szID,char *szFilePath,char *szFileName,char *szDealedLine)
{
    // traffic data
    char szSSBH[19] = {0};
    char szFXBH[11] = {0};
    char szJGSJ[15] = {0}; //To Time type
    char szCDBH[3] = {0};
    char szHPHM[16] = {0};
    char szHPYS[2] = {0};
    char szCWHPHM[16] = {0};
    char szCWHPYS[2] = {0};
    char szHPYZ[2] = {0};
    char szTXSL[2] = {0}; //To int type
    char szTXMC1[1024] = {0};
    char szTXCM2[1024] = {0};
    char szCLSD[4] ={0}; //To int type
    char szCLXS[4] = {0}; //To int type
    char szCSCD[5] = {0}; //To int type
    char szXSZT[5] = {0};
    char szCLPP[4] = {0};
    char szCLWX[4] = {0};
    char szCSYS[2] ={0};
    char CLLX[4] = {0};
    char HPZL[3] = {0};
    //...end
    
    //Device Status
/*
char szMobile[12]={0};
char szArea[32]={0};
char szArea2[32]={0};
char szArea3[32]={0};
char szSex[32]={0};
char szAge[32]={0};
char szBrand[32]={0};
char szCrowd[64]={0};
char szPartitionID[3]={0};
    */
char szSqlBuf[512]={0};
char szLogBuf[512]={0};
memset(szSqlBuf,0,512);
memset(szLogBuf,0,512);

FILE   *fp = NULL;
char szFullName[1024]={0};
char szLineData[512] = {0};
int nCurReadCount = 0;
int nOneReadCount = 0;
int nTotReadCount = 0;

sprintf(szFullName,"%s%s",szFilePath,szFileName);
//打开文件
if((fp = fopen(szFullName,"r")) == NULL)
{//打开文件失败
cout << "打开文件失败,文件名:" << szFileName << endl;
sprintf(szSqlBuf,"update %s set processed=4,PROCESSED_COUNT=PROCESSED_COUNT+1,UPDATE_DATE=sysdate where FTP_ID=%s ",szFileTable,szID);
otl_stream o1(1,szSqlBuf,db);
sprintf(szSqlBuf,"insert into %s(FTP_ID,START_RECORD,END_RECORD,EXCEPTION_MSG,CREATE_DATE) values(%s,0,0,'打开文件失败!',sysdate)",szExcpTable,szID);
otl_stream o2(1,szSqlBuf,db);
db.commit();
return 1;
}
cout << "成功打开文件名:" << szFileName << "已处理行数:" << szDealedLine <<endl;

otl_stream o1;
try
{
o1.set_commit(0);
o1.open(50, // buffer size
"insert into TCC_USER_LABEL(MOBILE,AREA,SEX,AGE,BRAND,CROWD,PARTITION_ID) values(:f1<char[32]>,:f2<char[257]>,:f3<char[257]>,:f4<char[257]>,:f5<char[257]>,:f6<char[257]>,:f7<char[4]>)",
// SQL statement
db // connect object
);

nTotReadCount = atoi(szDealedLine);
while( !feof( fp ) )
{
if(fgets( szLineData, 512, fp ) == NULL ) break;
nCurReadCount++;
if(nCurReadCount<=nTotReadCount)continue;
trim(szLineData);
int nPos = find(szLineData,'€');
memcpy(szMobile,szLineData,nPos);
strcpy(szLineData,Mid(szLineData,nPos+1));
nPos = find(szLineData,'€');
memcpy(szArea,szLineData,nPos);
strcpy(szLineData,Mid(szLineData,nPos+1));
nPos = find(szLineData,'€');
memcpy(szArea2,szLineData,nPos);
strcpy(szLineData,Mid(szLineData,nPos+1));
nPos = find(szLineData,'€');
memcpy(szArea3,szLineData,nPos);
strcpy(szLineData,Mid(szLineData,nPos+1));
nPos = find(szLineData,'€');
memcpy(szCrowd,szLineData,nPos);
strcpy(szLineData,Mid(szLineData,nPos+1));
nPos = find(szLineData,'€');
memcpy(szBrand,szLineData,nPos);
strcpy(szLineData,Mid(szLineData,nPos+1));
nPos = find(szLineData,'€');
memcpy(szAge,szLineData,nPos);
strcpy(szLineData,Mid(szLineData,nPos+1));
strcpy(szSex,szLineData);
trim(szMobile);
int len = strlen(szMobile);
strcpy(szPartitionID,Mid(szMobile,len-2));

o1<<szMobile<<szArea<<szSex<<szAge<<szBrand<<szCrowd<<szPartitionID;
nOneReadCount++;
if(nOneReadCount>=nCommitNum)
{
nOneReadCount = 0;
nTotReadCount+=nCommitNum;
sprintf(szSqlBuf,"update %s set DEALED_LINE=DEALED_LINE+%d,processed=2,UPDATE_DATE=sysdate where FTP_ID=%s ",szFileTable,nCommitNum,szID);
otl_stream o2(1,szSqlBuf,db);
o1.flush();
db.commit();
cout << "已成功入库,已入库行数:" << nTotReadCount <<endl;
}
}
fclose(fp);
sprintf(szSqlBuf,"update %s set DEALED_LINE=DEALED_LINE+%d,PROCESSED_COUNT=PROCESSED_COUNT+1,processed=1,UPDATE_DATE=sysdate where FTP_ID=%s ",szFileTable,nOneReadCount,szID);
otl_stream o2(1,szSqlBuf,db);
o1.flush();
db.commit();
return 0;
}
catch(otl_exception& p)
{
if(fp!=NULL)fclose(fp);
db.rollback();
memset(szLogBuf,0,512);
sprintf(szLogBuf,"SQL=%s||%s\n",p.stm_text,p.msg);
WriteLog(szLogBuf);
sprintf(szLogBuf,"insert into %s(FTP_ID,START_RECORD,END_RECORD,EXCEPTION_MSG,CREATE_DATE) values(%s,%d,%d,'SQL=%s||%s',sysdate)",szExcpTable,szID,nTotReadCount,nTotReadCount+nCommitNum,p.stm_text,p.msg);
otl_stream o1(1,szSqlBuf,db);
db.commit();
cout << "文件入库异常:" << szLogBuf <<endl;
return -2;
}

}

/*
* 记录log
*/
void ConnectPool::WriteLog(char *pstrMsg)
{
FILE   *fp;
char   strBuf[512];
char   strFileName[40];
time_t lTime;
struct tm *rNewTime;
time(&lTime);
rNewTime = localtime(&lTime);
memset(strFileName,0x00,30);
memset(strBuf,0x00,512);

sprintf(strFileName,"%s%04d-%02d-%02d.txt","ORA-LOG",rNewTime->tm_year+1900,rNewTime->tm_mon+1,rNewTime->tm_mday);         sprintf(strBuf,"%04d-%02d-%02d %02d:%02d:%02d  ",
rNewTime->tm_year+1900,
rNewTime->tm_mon+1,
rNewTime->tm_mday,
rNewTime->tm_hour,
rNewTime->tm_min,
rNewTime->tm_sec);
strcat(strBuf,pstrMsg);
if((fp = fopen(strFileName,"a+")) == NULL)
{
return ;
}
fwrite(strBuf,strlen(strBuf),sizeof(char),fp);
fwrite("\n",sizeof(char),1,fp);
fclose(fp);
return;
}

原文地址:https://www.cnblogs.com/xianqingzh/p/1731060.html