数据库中的表字段有多种的数据类型,在进行处理的时候都必须知道对应的数据类型在写相应的代码,尤其是条件语句的输入,
可以使用BOOST的any来实现,判断输入的参数类型组成静态的sql,下面为BaseOtl.h的代码
#ifndef _BASE_OTL__
#define _BASE_OTL__
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include "any.h"
#include "otl.h"
using namespace std;
class RuntimeStringCmp
{
public:
enum cmp_mode{normal,nocase};
private:
const cmp_mode mode;
static bool nocase_compare(char c1,char c2)
{
return toupper(c1) < toupper(c2);
}
public:
RuntimeStringCmp(cmp_mode m=normal):mode(m){ }
bool operator()(const string &s1,const string &s2) const{
if( mode == normal )
return s1 < s2;
else
return lexicographical_compare(s1.begin(),s1.end(),s2.begin(),s2.end(),nocase_compare);
}
};
const string FIX_STRING="zzzzzzzz";
typedef map<string,any,RuntimeStringCmp> COLUMNMAP;
enum G_CONDITION_OPR{
G_EQUAL=0, //=
G_GREATER, //>
G_LESS, //<
G_GREATER_EQUAL, //>=
G_LESS_EQUAL, //<=
G_UNEQUAL //!=
};
struct CONDITION{
G_CONDITION_OPR opr;
any value;
CONDITION(){}
CONDITION(const G_CONDITION_OPR &a,const any &b):opr(a),value(b){}
CONDITION& operator=(const any &b){opr=G_EQUAL;value=b;return *this;}
CONDITION& operator==(const any &b){opr=G_EQUAL;value=b;return *this;}
CONDITION& operator>(const any &b){opr=G_GREATER;value=b;return *this;}
CONDITION& operator<(const any &b){opr=G_LESS;value=b;return *this;}
CONDITION& operator>=(const any &b){opr=G_GREATER_EQUAL;value=b;return *this;}
CONDITION& operator<=(const any &b){opr=G_LESS_EQUAL;value=b;return *this;}
CONDITION& operator!=(const any &b){opr=G_UNEQUAL;value=b;return *this;}
};
typedef map<string,CONDITION,RuntimeStringCmp> WHEREMAP;
bool is_int(const any & operand);
bool is_float(const any & operand);
bool is_llong(const any & operand);
bool is_string(const any & operand);
bool is_datetime(const any & operand);
otl_stream& operator<<(otl_stream& o, const any& rcf);
ostream& operator<<(ostream& o, const any& rcf);
string getWhereString(WHEREMAP &whereMap);
string getColumnString(COLUMNMAP &colMap,int flag=1);
string toString(WHEREMAP &whereMap);
string toString(COLUMNMAP &colMap);
//通用的数据库修改函数
void c_updateRows(otl_connect &cDb,const string &strTable,WHEREMAP &whereMap,COLUMNMAP &colMap);
//通用的数据库删除函数
void c_deleteRows(otl_connect &cDb,const string &strTable,WHEREMAP &whereMap);
//通用的数据库插入函数
void c_insertRow(otl_connect &cDb,const string &strTable,COLUMNMAP &colMap);
//通用的查询函数(单条查询) --wList为绑定的值
bool c_getRow(otl_connect &cDb,const string &strSql,vector<any> &wList,COLUMNMAP &colMap);
//通用的查询xml生成
string& lowerCase( string& str );
string c_getXMLRow(otl_connect &cDb,const string &strSql,vector<any> &wList,int nMax);
#endif