107 lines
2.0 KiB
C++
107 lines
2.0 KiB
C++
#ifndef _TABLE_TO_JSON_HPP_
|
|
#define _TABLE_TO_JSON_HPP_
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <iDA/iDA.h>
|
|
|
|
#define NORECORD 100
|
|
|
|
using namespace std;
|
|
using namespace iPlature;
|
|
using namespace iDA;
|
|
using namespace log4cplus;
|
|
|
|
extern iDA::Connection cn;
|
|
|
|
//template <class T> class TabToJson
|
|
class TabToJson
|
|
{
|
|
private:
|
|
iDA::Command cmd;
|
|
char jsonBuffer[20000];
|
|
string tmp;
|
|
int index;
|
|
public:
|
|
TabToJson()
|
|
{
|
|
tmp="";
|
|
index=0;
|
|
memset(jsonBuffer,0x00,sizeof(jsonBuffer));
|
|
}
|
|
~TabToJson()
|
|
{
|
|
return;
|
|
}
|
|
|
|
public:
|
|
/* Read DB Table Data to json */
|
|
/*
|
|
{
|
|
"columns":["c1","c2","c3"],
|
|
"values":[["v11","v12","v13"],["v21","v22","v23"]]
|
|
}
|
|
*/
|
|
string ToJson(char* dbwhere = NULL)
|
|
{
|
|
LOG log("TabToJson::ToJson");
|
|
try
|
|
{
|
|
cmd.SetConnection(&cn);
|
|
cmd.SetCommandText(dbwhere);
|
|
cmd.Execute();
|
|
int icolCount=cmd.FieldCount();
|
|
tmp="{\"columns\":[";
|
|
sprintf(&jsonBuffer[index],"%s",tmp.c_str());
|
|
index+=tmp.size();
|
|
for(int i=1;i<=icolCount;i++)
|
|
{
|
|
tmp="\""+cmd.Field(i).Name()+"\",";
|
|
sprintf(&jsonBuffer[index],"%s",tmp.c_str());
|
|
index+=tmp.size();
|
|
//log.Debug()<<"icols:"<<icols<<endl;
|
|
}
|
|
tmp="],";
|
|
sprintf(&jsonBuffer[index-1],"%s",tmp.c_str());
|
|
index+=tmp.size();
|
|
//
|
|
index-=1;
|
|
tmp="\"values\":[";
|
|
sprintf(&jsonBuffer[index],"%s",tmp.c_str());
|
|
index+=tmp.size();
|
|
|
|
string idata="";
|
|
while(cmd.FetchNext())
|
|
{
|
|
for(int i=1;i<=icolCount;i++)
|
|
{
|
|
idata+="\""+cmd.Field(i).AsString()+"\",";
|
|
}
|
|
tmp="["+idata.substr(0,idata.size()-1)+"],";
|
|
sprintf(&jsonBuffer[index],"%s",tmp.c_str());
|
|
index+=tmp.size();
|
|
idata="";
|
|
}
|
|
tmp="]}";
|
|
sprintf(&jsonBuffer[index-1],"%s",tmp.c_str());
|
|
index+=tmp.size();
|
|
return jsonBuffer;
|
|
}
|
|
catch(iDA::Exception &x)
|
|
{
|
|
log.Debug()<<"Exception:"<<x.ErrMsg()<<endl;
|
|
try
|
|
{
|
|
cn.Rollback();
|
|
}
|
|
catch(iDA::Exception &)
|
|
{
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
};
|
|
|
|
#endif
|