📄 datasplit.pc
字号:
WriteLog( g_strLogLine );
/*return 0; 不结束,以后可手动建索引*/
}
else
{
sprintf( g_strLogLine, " repeat create %s index success!\n",pTabParameter->Table_Name);
WriteLog( g_strLogLine );
}
iRtn = ReleaseDB();
/******************************重新给原表创建索引:结束*********************************/
/*drop 在线用户在另一用户下创建的临时表(函数内部连库)*/
DropTempTab(pTabParameter->Table_Name);
sprintf( g_strLogLine, " deal B Class Success! \n\n" );
WriteLog( g_strLogLine );
return 1;
}/*end fun*/
/************************************************************************************
功能:倒计费详单表;倒当前账务月对应的表,然后清除第N个月前对应的表数据。如账务月为200507,
N=3,则备份*_07表,清除第*_04表数据,同时作安全检查判断。
表命名规则:
组合出表名,由于详细单表是一个月一个表,在线库中是"mm"结尾,历史库中是"yyyymm"结尾,
所以在配置表中是共同的部分,因此,加上当前帐务月的月就是在线库中的当前帐务月的表,
如果是加上当前帐务月就是历史库中的当前帐务月的表了.
参数:参数:表名、保留月份数等
返回值:0表示失败,1表示成功
************************************************************************************/
int DoCClass(pTabParameter)
struct Tab_Parameter *pTabParameter;
{
char szCurTime[15];
char szCreateSQL[2001];
char szRenameStmt[80], szNewTableName[60];
int iRtn = 0; /*获得函数调用的返回值*/
long lSRows = 0; /*记录表处理之前的记录数*/
long lRows = 0; /*中间变量*/
char szANYMonth[7]; /*本次帐务月的N个月前的帐务月的年月*/
char szCntSQL[256]; /*构建统计表记录数的SQL语句*/
char szIMPstmt[256]; /*构建IMPORT的系统语句*/
char szEXPstmt[256]; /*构建EXPORT的系统语句*/
struct PFILE pFile; /*记录创建EXPORT参数文件的必须内容*/
char achSql[1000];
memset(szCurTime,'\0',sizeof(szCurTime));
memset(szCreateSQL,'\0',sizeof(szCreateSQL));
if (atoi(pTabParameter->Account_Date)>0)/*如果配置表设定了账务月就用设定值。*/
{
sprintf(g_strYearMonth,"%s",pTabParameter->Account_Date);
}
sprintf( pFile.TABLE, "%s%s", pTabParameter->Table_Name, g_strYearMonth + 4 );
WriteLog( "\n" );
sprintf( g_strLogLine, "Do table [%s] N=[%d] \n", pFile.TABLE,pTabParameter->Reserve_Months);
WriteLog( g_strLogLine );
/***************************做安全检查 begin************************************/
/*检查当前要倒换的表*/
sprintf( szNewTableName, "%s%s", pTabParameter->Table_Name, g_strYearMonth );
if ( ConnectDB(g_strDestDB)!=1 )
{
sprintf( g_strLogLine, " Connect to source db failed,please check! \n\n" );
WriteLog( g_strLogLine );
return 0;
}
if(iRtn=CheckTab(szNewTableName)==1)
{
sprintf( g_strLogLine, "当前账务月(%s)己做过一次,如要再做请先drop %s! \n\n",g_strYearMonth,szNewTableName);
WriteLog( g_strLogLine );
return 0;
}
else if(iRtn<0)
return 0;/*出错了*/
iRtn = ReleaseDB();
/*检查当前要清除数据的表*/
if ( ConnectDB(g_strDestDB)!=1 )
{
sprintf( g_strLogLine, " Connect to source db failed,please check! \n\n" );
WriteLog( g_strLogLine );
return 0;
}
strcpy( szANYMonth, GetNAYMonth( g_strYearMonth, pTabParameter->Reserve_Months ) );
sprintf( szNewTableName, "%s%s", pTabParameter->Table_Name, szANYMonth );
if(iRtn=CheckTab(szNewTableName)==0)
{
sprintf( g_strLogLine, "表[%s]在历史库中不存在,请先倒换该表对应的表! \n\n",szNewTableName);
WriteLog( g_strLogLine );
return 0;
}
else if(iRtn<0)
return 0;/*出错了*/
iRtn = ReleaseDB();
/***************************做安全检查 end ************************************/
memset(szNewTableName,'\0',sizeof(szNewTableName));
memset(szANYMonth,'\0',sizeof(szANYMonth));
/*第一步,统计表需要Export出来的数据量——即当前帐务月的数据*/
/*连接源数据库*/
iRtn = ConnectDB( g_strSrcDB );
if ( iRtn != 1 )
{
sprintf( g_strLogLine, "DoCClass Connect to %s failed,please check!!!\n\n" ,g_strSrcDB);
WriteLog( g_strLogLine );
return 0;
}
lSRows = CountRows( pFile.TABLE );
sprintf( g_strLogLine, " all data is rows = %d \n", lSRows );
WriteLog( g_strLogLine );
if ( lSRows<0 )
{
sprintf( g_strLogLine, "table %s does not exist \n\n" ,pFile.TABLE);
WriteLog( g_strLogLine );
return 0;
}
ReleaseDB();
/*第二步,准备Export的参数文件*/
strcpy( pFile.USERID, g_strSrcDB );
pFile.PARTITION[0] = '\0';
pFile.QUERY[0] = '\0';
if ( WritePFile( &pFile ) != 1 )
{
sprintf( g_strLogLine, " Create parfile %s failed!!!\n\n",pFile.parFileName );
WriteLog( g_strLogLine );
return 0;
}
/*参数文件生成成功!*/
sprintf( g_strLogLine, " Create parfile sucess,parfile name is %s\n", pFile.parFileName );
WriteLog( g_strLogLine );
/*第三步,EXPORT数据,全倒出*/
sprintf( szEXPstmt, "exp parfile = %s", pFile.parFileName );
system( szEXPstmt );
sprintf( g_strLogLine, " Export data complete!file: %s \n\t\t\tlog: %s\n", pFile.dmpFileName, pFile.logFileName );
WriteLog( g_strLogLine );
/*第四步,通过读日志文件判断EXP的正确性*/
lRows = readLogFile( pFile.logFileName );
if ( lRows == lSRows )
{
/*EXP的记录数与需要的一致*/
sprintf( g_strLogLine, " Export data succeed! export out rows is %d\n", lRows );
WriteLog( g_strLogLine );
}
else
{
sprintf( g_strLogLine, "Export data failed! export out rows is %d\n\n", lRows );
WriteLog( g_strLogLine );
return 0;
}
/*接下来是IMP、改名、建索引*/
/*第七步,IMPORT数据到目标数据库的对应表中 import自动建表*/
GetSysTime(szCurTime);
sprintf( pFile.logFileName, "./log/imp%s%s.log", pFile.TABLE, szCurTime );
sprintf( szIMPstmt, "imp %s tables = %s ignore = y commit=y indexes = n grants = n buffer=4096000 feedback=500000 file = %s log = %s", g_strDestDB, pFile.TABLE, pFile.dmpFileName, pFile.logFileName );
system( szIMPstmt );
WriteLog( szIMPstmt );
WriteLog( "\n" );
sprintf( g_strLogLine, " Import data complete! log file is : %s\n", pFile.logFileName );
WriteLog( g_strLogLine );
/*第八步,通过读日志文件判断IMP的正确性*/
lRows = readLogFile( pFile.logFileName );
if ( lRows == lSRows )
{
/*IMP的记录数与需要的一致*/
sprintf( g_strLogLine, " Import data succeed! Import rows is %d\n", lRows );
WriteLog( g_strLogLine );
}
else
{
sprintf( g_strLogLine, "Import data failed! Import rows is %d\n", lRows );
WriteLog( g_strLogLine );
return 0;
}
/*第九步,改名、建索引*/
/*改名,历史库中表名是以yyyymm结尾*/
/*连接目标数据库,即历史库*/
iRtn = ConnectDB( g_strDestDB );
if ( iRtn != 1 )
{
sprintf( g_strLogLine, "Connect to %s failed,please check!!!\n\n", g_strDestDB);
WriteLog( g_strLogLine );
return 0;
}
sprintf( szNewTableName, "%s%s", pTabParameter->Table_Name, g_strYearMonth );
sprintf( szRenameStmt, "rename %s to %s", pFile.TABLE, szNewTableName );
EXEC SQL EXECUTE IMMEDIATE :szRenameStmt;
if ( sqlca.sqlcode != 0 )
{
sprintf( g_strLogLine, "rename %s to %s failed! rename sql: %s[%d]\n\n", pFile.TABLE, szNewTableName, szRenameStmt,sqlca.sqlcode);
WriteLog( g_strLogLine );
iRtn = ReleaseDB();
/*return 0;不退出,但要保证建索引在else中*/
}
else
{
sprintf( g_strLogLine, " rename %s to %s succeed!!!\n", pFile.TABLE, szNewTableName );
WriteLog( g_strLogLine );
/*建索引,如果建索引的脚本不为空则建索引*/
sprintf( g_strLogLine, " begin create index!!!\n" );
WriteLog( g_strLogLine );
iRtn = crtIndex( pFile.TABLE, szNewTableName );
}
/*断开与目标数据库的连接*/
iRtn = ReleaseDB();
/*第十步,开始清除数据,清除之前第N个帐务月的数据,也就是说如果当前帐务月是12月,N为2个月,则清楚帐务月为10月的数据*/
/*求出N个月前的帐务月,即要清除数据的帐务月,如果N等于0则不做清除工作*/
if ( pTabParameter->Reserve_Months == 0 )
{
/*如果保留月数为0则不清除数据*/
sprintf( g_strLogLine, "reserve months is 0,don`t clear data Do success!!!\n" );
WriteLog( g_strLogLine );
return 1;
}
strcpy( szANYMonth, GetNAYMonth( g_strYearMonth, pTabParameter->Reserve_Months ) );
/*组合出要清除数据帐务月对应的表名*/
sprintf( pFile.TABLE, "%s%s", pTabParameter->Table_Name, szANYMonth + 4 );
sprintf( g_strLogLine, " [%s] will be truncate table\n", pFile.TABLE );
WriteLog( g_strLogLine );
/*连接源数据库*/
iRtn = ConnectDB( g_strSrcDB );
if ( iRtn != 1 )
{
sprintf( g_strLogLine, "DoCClass Connect to %s failed,please check!!!\n\n",g_strSrcDB );
WriteLog(g_strLogLine);
return 0;
}
/*清除表中的数据*/
if(iRtn=TruncateTab(pFile.TABLE)<=0)
{
sprintf( g_strLogLine, "Truncate table %s failed,please check!!!\n\n",pFile.TABLE );
WriteLog( g_strLogLine );
return 0;
}
iRtn = ReleaseDB();
sprintf( g_strLogLine, " Truncate table %s Success!!!\n",pFile.TABLE );
WriteLog( g_strLogLine );
sprintf( g_strLogLine, " deal table %s Success!!!\n\n",pFile.TABLE);
WriteLog( g_strLogLine );
return 1;
goto_err01:
printf("DoCClass Cursor fetch fail! error code: %d --- %s\n",sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK;
return -900;
}/*end fun*/
/************************************************************************************
//功能:倒计费统计表
//参数:参数:表名、保留月份数等
//返回值:0表示失败,1表示成功
************************************************************************************/
int DoDClass(pTabParameter)
struct Tab_Parameter *pTabParameter;
{
char szCurTime[15];
int iRtn = 0; /*获得函数调用的返回值*/
long lSRows = 0; /*记录表处理之前的记录数*/
long lRows = 0; /*中间变量*/
char szANYMonth[7]; /*本次帐务月的N个月前的帐务月的年月*/
char szCntSQL[256]; /*构建统计表记录数的SQL语句*/
char szIMPstmt[256]; /*构建IMPORT的系统语句*/
char szEXPstmt[256]; /*构建EXPORT的系统语句*/
struct PFILE pFile; /*记录创建EXPORT参数文件的必须内容*/
int i=0;
char pAccount_date[9]; /*待处理账务月*/
char achSql[1000];
memset(szCurTime,'\0',sizeof(szCurTime));
memset(pAccount_date,'\0',sizeof(pAccount_date));
sprintf( g_strLogLine, "Do table %s\n", pTabParameter->Table_Name );
WriteLog( g_strLogLine );
/*从历史库中stat_*表中取最大账务周期*/
/*连接历史数据库*/
if ( ConnectDB(g_strDestDB)!=1 )
{
printf("connect hisDB failed!!\n\n");
return 0;
}
EXEC SQL WHENEVER SQLERROR CONTINUE;
sprintf(achSql,"SELECT nvl(max(account_date),'200101') FROM %s ",pTabParameter->Table_Name);
EXEC SQL PREPARE stmt20 FROM :achSql;
EXEC SQL DECLARE cur20 CURSOR FOR stmt20;
EXEC SQL OPEN cur20;
if (sqlca.sqlcode)
{
sprintf( g_strLogLine, " cur20 error code: %d---%s\n",sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
WriteLog( g_strLogLine );
return -900;
}
EXEC SQL FETCH cur20 INTO :pAccount_date;
DelBackBlank(pAccount_date);
EXEC SQL CLOSE cur20;
iRtn = ReleaseDB();
/*连接源数据库*/
iRtn = ConnectDB( g_strSrcDB );
if ( iRtn != 1 )
{
sprintf( g_strLogLine, "DoDClass Connect to %s failed,please check!!!\n",g_strSrcDB );
WriteLog( g_strLogLine );
return 0;
}
/*第一步,统计需要Export出来的数据量——即未倒入到历史库的数据*/
sprintf( g_strLogLine, " deal table %s data of late month [%s]\n" ,pTabParameter->Table_Name,pAccount_date);
WriteLog( g_strLogLine );
sprintf( szCntSQL, "%s where account_date > '%s' ", pTabParameter->Table_Name, pAccount_date );
lSRows = CountRows( szCntSQL );
/*断开与源数据库的连接*/
iRtn = ReleaseDB();
sprintf( g_strLogLine, " Step1: to exp rows = %d\n", lSRows );
WriteLog( g_strLogLine );
if ( 0==lSRows )
{
sprintf( g_strLogLine, "\ttable %s does not exits or have no reacorder\n" ,pTabParameter->Table_Name);
WriteLog( g_strLogLine );
return 0;
}
/*第一个临时表 his*/
if ( ConnectDB(g_strSrcDB)!=1 )
{
sprintf( g_strLogLine, " DoDClass Connect to source failed,please check! \n\n" );
WriteLog( g_strLogLine );
return 0;
}
iRtn=CrtStat(pTabParameter->Table_Name,pAccount_date);
if (iRtn != 1 )
{
sprintf( g_strLogLine, " CrtStat() failed,please check! \n\n" );
WriteLog( g_strLogLine );
return 0;
}
iRtn = ReleaseDB();
/*第二步,准备Export的参数文件*/
strcpy( pFile.USERID, g_strTempDB );
strcpy( pFile.TABLE, pTabParameter->Table_Name );
pFile.PARTITION[0] = '\0';
pFile.QUERY[0] = '\0';
if ( WritePFile( &pFile ) != 1 )
{
strcpy( g_strLogLine, "\tCreate parfile failed!!!\n" );
WriteLog( g_strLogLine );
return 0;
}
/*参数文件生成成功!!!!*/
sprintf( g_strLogLine, " Create parfile sucess! parfile name is %s\n", pFile.parFileName );
WriteLog( g_strLogLine );
/*第三步,EXPORT数据*/
sprintf( szEXPstmt, "exp parfile = %s",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -