1.类定义
/// 表字段与UMX项(包括参数和行集列)映射项定义
struct CField2UMXItem {
char fld_name_[32];
char para_name_[32];
};
/// 表操作Helper,适用于基于UMX协议时服务端数据库处理(查询,新增,修改),可简化代码
class CDbHelper {
CField2UMXItem *vmi_; ///< 数据库表与UMX项(参数或者行集列映射表)
unsigned int vmi_size_; ///< 映射表项数
const char* FindField(const char *para_name);
public:
CDbHelper():vmi_(0),vmi_size_(0) {
}
CDbHelper(CField2UMXItem *vmi,unsigned int size) {
Attach(vmi,size);
}
void Attach(CField2UMXItem *vmi,unsigned int size) {
vmi_ = vmi;
vmi_size_ = size;
}
/// 把UMX消息与表绑定,用于新增,修改数据
int Bind(ITableHandler *th,CMsg *msg);
///< 生成辅助生成SELECT字段的字符串,用于从协议项(如参数名称)生成查询语句
int GenFldString(const char *alias,string &ret_val,char *fld,...) ;
};
2.使用示例:
///< 商品信息表字段与协议数据项映射表
CField2UMXItem goods_para_map[]= {
{"GLN","GLN"},///供应商GLN
{"barcode","GTIN"},/// 商品条码
{"f004v_3350","PCode"},/// 供应商产品代码
{"f005v_3350","Name"},/// 商品名称(中文)
};
int Test() {
CDbHelper db_helper;
///< 与映射表关联
db_helper.Attach(goods_para_map,sizeof(goods_para_map)/sizeof(goods_para_map[0]));
///< 用法1: 协议参数绑定到表字段(手动)
ITableHandler* th = CBasePluginModule::db_helper_->NewTableHandler(pdbor, "tb_3350");
AUTO_POINTER_NODECLARE(ITableHandler,th);
th->BindField(db_helper.FindField("GLN"),msg,"GLN");
th->BindField(db_helper.FindField("GTIN"),msg,"GTIN");
th->BindField(db_helper.FindField("PCode"),msg,"PCode");
th->BindField(db_helper.FindField("Name"),msg,"Name");
///< 用法2:自动与消息的所有参数绑定
db_helper.Bind(th,msg);
/// 对于不在UMX消息中的列或者需要对参数进行处理的,可手动绑定
///< 用法3: 根据指定的协议项名称生成查询的select字段列表.
string fld_str;
int ret = db_helper.GenFldString(0,fld_str,"RStatus","Status","RegTime",0);
string sql;
sql = LogMsg("select %s from tb_3350",fld_str.c_str());
return 0;
/// 表字段与UMX项(包括参数和行集列)映射项定义
struct CField2UMXItem {
char fld_name_[32];
char para_name_[32];
};
/// 表操作Helper,适用于基于UMX协议时服务端数据库处理(查询,新增,修改),可简化代码
class CDbHelper {
CField2UMXItem *vmi_; ///< 数据库表与UMX项(参数或者行集列映射表)
unsigned int vmi_size_; ///< 映射表项数
const char* FindField(const char *para_name);
public:
CDbHelper():vmi_(0),vmi_size_(0) {
}
CDbHelper(CField2UMXItem *vmi,unsigned int size) {
Attach(vmi,size);
}
void Attach(CField2UMXItem *vmi,unsigned int size) {
vmi_ = vmi;
vmi_size_ = size;
}
/// 把UMX消息与表绑定,用于新增,修改数据
int Bind(ITableHandler *th,CMsg *msg);
///< 生成辅助生成SELECT字段的字符串,用于从协议项(如参数名称)生成查询语句
int GenFldString(const char *alias,string &ret_val,char *fld,...) ;
};
2.使用示例:
///< 商品信息表字段与协议数据项映射表
CField2UMXItem goods_para_map[]= {
{"GLN","GLN"},///供应商GLN
{"barcode","GTIN"},/// 商品条码
{"f004v_3350","PCode"},/// 供应商产品代码
{"f005v_3350","Name"},/// 商品名称(中文)
};
int Test() {
CDbHelper db_helper;
///< 与映射表关联
db_helper.Attach(goods_para_map,sizeof(goods_para_map)/sizeof(goods_para_map[0]));
///< 用法1: 协议参数绑定到表字段(手动)
ITableHandler* th = CBasePluginModule::db_helper_->NewTableHandler(pdbor, "tb_3350");
AUTO_POINTER_NODECLARE(ITableHandler,th);
th->BindField(db_helper.FindField("GLN"),msg,"GLN");
th->BindField(db_helper.FindField("GTIN"),msg,"GTIN");
th->BindField(db_helper.FindField("PCode"),msg,"PCode");
th->BindField(db_helper.FindField("Name"),msg,"Name");
///< 用法2:自动与消息的所有参数绑定
db_helper.Bind(th,msg);
/// 对于不在UMX消息中的列或者需要对参数进行处理的,可手动绑定
///< 用法3: 根据指定的协议项名称生成查询的select字段列表.
string fld_str;
int ret = db_helper.GenFldString(0,fld_str,"RStatus","Status","RegTime",0);
string sql;
sql = LogMsg("select %s from tb_3350",fld_str.c_str());
return 0;
}
3.最简代码示例
以下是处理商品注册协议的函数:
int CGDSNSUP_Plugin::OnReceive11121(CWrappedMsg<> *in,vector<CWrappedMsg<> *> &out,DISPATCH_RESULT &or) {
CMsg *msg = in->msg;
CDbHelper db_helper;
db_helper.Attach(goods_para_map,sizeof(goods_para_map)/sizeof(goods_para_map[0]));
GETDBC(pdbor,this->local_dbc_.c_str());
ITableHandler* th = CBasePluginModule::db_helper_->NewTableHandler(pdbor, "tb_3350");
AUTO_POINTER_NODECLARE(ITableHandler,th);
db_helper.Bind(th,msg);
if (th->Insert())
return -1;
return 0;
}