#include "lbmapi.h"
#include "KCBPDatabase.h"
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
int MakeResultSetHead(LBMHANDLE hHandle, CKCBPDatabase * pDb)
{
int nRet;
int i;
char szColNames[(MAX_COLNUM+1)*MAXCOLNAMELEN+1];
int nColCount;
SDBCOL * pstColInfo;
szColNames[0]='\0';
nColCount = pDb->GetColCount();
for(i=0;i<nColCount;i++)
{
pstColInfo = (SDBCOL * )pDb->GetColInfo(i);
if(strlen(szColNames)+strlen(pstColInfo->Name)<sizeof(szColNames)-1)
{
strcat(szColNames, pstColInfo->Name);
strcat(szColNames, ",");
}
else
{
return 100;
}
}
if(strlen(szColNames)>0) szColNames[strlen(szColNames)-1]=0;
/*_strupr(szColNames);*/
nRet = KCBP_RsNewTable(hHandle, "", nColCount, szColNames);
if(nRet!=0) return 1;
return 0;
}
int SetLbmError(LBMHANDLE hHandle, int nErrorCode, int nErrorLevel, char *szErrorMsg)
{
char szTmp[12];
KCBP_BeginWrite(hHandle);
KCBP_RsCreate(hHandle, "MESSAGE", 3, "LEVEL,CODE,TEXT");
KCBP_RsAddRow(hHandle);
sprintf(szTmp,"%d",nErrorLevel);
KCBP_RsSetColByName(hHandle, "LEVEL", szTmp);
sprintf(szTmp,"%d",nErrorCode);
KCBP_RsSetColByName(hHandle, "CODE", szTmp);
KCBP_RsSetColByName(hHandle, "TEXT", szErrorMsg);
KCBP_RsSaveRow(hHandle);
return 0;
}
int MakeResultSet(LBMHANDLE hHandle, CKCBPDatabase * pDb, int nMaxFetchRows)
{
int nRet;
int i;
int nColCount;
int nRows;
/*create first result, return with success code 0*/
KCBP_BeginWrite(hHandle);
KCBP_RsCreate(hHandle, "MESSAGE", 3, "LEVEL,CODE,TEXT");
KCBP_RsAddRow(hHandle);
KCBP_RsSetColByName(hHandle, "LEVEL", "0");
KCBP_RsSetColByName(hHandle, "CODE", "0");
KCBP_RsSetColByName(hHandle, "TEXT", "OK");
KCBP_RsSaveRow(hHandle);
nRows=0;
while(pDb->MoveNext())
{
if(nRows == 0)
{
nRet = MakeResultSetHead(hHandle, pDb);
if(nRet!=0) return nRet;
nColCount = pDb->GetColCount();
}
if(nRows++>=nMaxFetchRows) continue;
nRet = KCBP_RsAddRow(hHandle);
if(nRet!=0) break;
for(i=0;i<nColCount;i++)
{
char *pCol = (char*) pDb->GetColData(i);
if (pCol[0] == '.')
{
char szTemp[8] = {0,};
_snprintf(szTemp,sizeof(szTemp)-1,"%.2f",atof(pCol));
KCBP_RsSetColByName(hHandle, ((SDBCOL * )pDb->GetColInfo(i))->Name,szTemp);
}
else
{
KCBP_RsSetColByName(hHandle, ((SDBCOL * )pDb->GetColInfo(i))->Name,(char*) pDb->GetColData(i));
}
}
nRet = KCBP_RsSaveRow(hHandle);
if(nRet!=0) break;
}
if(nRows==0)
{
KCBP_BeginWrite(hHandle);
KCBP_RsCreate(hHandle, "MESSAGE", 3, "LEVEL,CODE,TEXT");
KCBP_RsAddRow(hHandle);
KCBP_RsSetColByName(hHandle, "LEVEL", "0");
KCBP_RsSetColByName(hHandle, "CODE", "100000");
KCBP_RsSetColByName(hHandle, "TEXT", "No result");
KCBP_RsSaveRow(hHandle);
return 100000;
}
return 0;
}
//create table test
// (
// id int,
// name varchar(256)
// )
extern "C" LBMEXPORTS void* LBMDBTest(char *pCA)
{
int nRet;
bool bRet;
char szType[32];
char szTable[32], szCol[32], szVal[32], szCommand[1024];
LBMHANDLE hHandle;
CKCBPDatabase * pDb=NULL;
if(!(hHandle=KCBP_Init(pCA)))
{
return NULL;
}
nRet = KCBP_GetXAHandle(hHandle, "tradedb", (void **)&pDb);
if(nRet!=0 || pDb==NULL)
{
SetLbmError(hHandle, 100000, 0, "KCBP_GetDBProcess fail");
goto __end;
}
KCBP_GetValueN(hHandle,"type",szType,sizeof(szType)-1);
if (strcmp(szType,"insert") == 0)
{
_snprintf(szCommand,sizeof(szCommand)-1,"insert into test values(2,\'ab\')");
}
else
{
_snprintf(szCommand,sizeof(szCommand)-1,"select * from testlbm");
}
bRet = pDb->ExecSql(szCommand);
if(!bRet)
{
SetLbmError(hHandle, pDb->GetErrorCode(), 0, (char *)pDb->GetErrorMsg());
goto __end;
}
if (strstr(szCommand,"select"))
{
nRet = MakeResultSet(hHandle, pDb, 100);
if (nRet != 0)
{
SetLbmError(hHandle, pDb->GetErrorCode(), 0, (char *)pDb->GetErrorMsg());
goto __end;
}
}
else
{
SetLbmError(hHandle,0,0,"success");
}
__end:
KCBP_Exit(hHandle);
return(NULL);
}