将mysql中查询出来的数据 序列化到protobuffer消息结构体中

void CDBTable::fillMessageField(Gmessage &message,  MSW::CStoreResult *result )
{
BOMB_IF(result==NULL, "result null", return);
const Reflection * reflection = message.GetReflection();
const Descriptor * desc = message.GetDescriptor();
//下面的代码可以用这个宏 代替。。可惜个人比较不喜欢宏,所以只实现了没有用
/*/#define CASE_SET_VALUE_BY_TYPE(cppType, valueType, func) \
// case FieldDescriptor::cppType: \
// {  \
// valueType typeValue;  \
// result->getField(index, typeValue); \
// reflection->func(&message, fieldDesc, typeValue); \
// }  \
// break; /*/
//fill guid
//{
// const FieldDescriptor* fieldDesc = desc->FindFieldByName(_KeyName);
// if (fieldDesc != NULL)
// {
// //这里潜规则, 主键 暂时都是 uint64
// uint64 typeValue;
// result->getField(index, typeValue);
// reflection->SetUInt64(&message, fieldDesc, typeValue);
// index++;
// }
//}


// mysql result 索引
uint32 index = 0;
TFiels::iterator iter = _Fields.begin();
for (; iter != _Fields.end(); ++iter,index++)
{
const FieldDescriptor* fieldDesc = desc->FindFieldByName(iter->_Name);
BOMB_IF(fieldDesc == NULL, "fieldDesc null " << iter->_Name ,continue );
switch (iter->_Type)
{
case FieldDescriptor::CPPTYPE_INT32:
{
sint32 typeValue;
result->getField(index , typeValue);
reflection->SetInt32(&message, fieldDesc, typeValue);
}
break;
case FieldDescriptor::CPPTYPE_INT64:
{
sint64 typeValue;
result->getField(index, typeValue);
reflection->SetInt64(&message, fieldDesc, typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_UINT32:
{
uint32 typeValue;
result->getField(index, typeValue);
reflection->SetUInt32(&message, fieldDesc, typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_UINT64:
{
uint64 typeValue;
result->getField(index, typeValue);
reflection->SetUInt64(&message, fieldDesc, typeValue);
}
break;
case FieldDescriptor::CPPTYPE_FLOAT:
{
float typeValue;
result->getField(index, typeValue);
reflection->SetFloat(&message, fieldDesc, typeValue);
}
break;
case FieldDescriptor::CPPTYPE_STRING:
{
std::string typeValue;
result->getField(index, typeValue);
reflection->SetString(&message, fieldDesc ,typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_DOUBLE:
{
double typeValue;
result->getField(index,typeValue);
reflection->SetDouble(&message, fieldDesc, typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_BOOL:
{
bool typeValue;
result->getField(index, typeValue);
reflection->SetBool(&message, fieldDesc, typeValue);
}
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
{
uint32 length = 0;
const char* str = result->getRawField(index, length);
if (length != 0 && str != NULL)
{
google::protobuf::Message* subMessage = reflection->MutableMessage(&message, fieldDesc);//不会为null;
subMessage->ParseFromArray(str, length);
}
}
break;
default:
nlwarning("this is a cpptype ?:%d", iter->_Type);
return;
}
}
TRepeatFields::iterator iterRepeat = _RepeatFields.begin();


for (; iterRepeat != _RepeatFields.end(); ++iterRepeat)
{
const FieldDescriptor* fieldDesc = desc->FindFieldByName(iterRepeat->_Name);
BOMB_IF( fieldDesc == NULL, "fieldDesc null " << iterRepeat->_Name, continue);
std::vector<std::string>::iterator itRepeat = iterRepeat->_Fields.begin();


for (; itRepeat != iterRepeat->_Fields.end(); ++itRepeat,++index )
{
switch(iterRepeat->_Type)
{
case FieldDescriptor::CPPTYPE_INT32:
{
sint32 typeValue;
result->getField(index , typeValue);
reflection->AddInt32(&message, fieldDesc, typeValue);
}
break;
case FieldDescriptor::CPPTYPE_INT64:
{
sint64 typeValue;
result->getField(index, typeValue);
reflection->AddInt64(&message, fieldDesc, typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_UINT32:
{
uint32 typeValue;
result->getField(index, typeValue);
reflection->AddUInt32(&message, fieldDesc, typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_UINT64:
{
uint64 typeValue;
result->getField(index, typeValue);
reflection->AddUInt64(&message, fieldDesc, typeValue);
}
break;
case FieldDescriptor::CPPTYPE_FLOAT:
{
float typeValue;
result->getField(index, typeValue);
reflection->AddFloat(&message, fieldDesc, typeValue);
}
break;
case FieldDescriptor::CPPTYPE_STRING:
{
std::string typeValue;
result->getField(index, typeValue);
reflection->AddString(&message, fieldDesc ,typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_DOUBLE:
{
double typeValue;
result->getField(index,typeValue);
reflection->AddDouble(&message, fieldDesc, typeValue);
}
break;
case  FieldDescriptor::CPPTYPE_BOOL:
{
bool typeValue;
result->getField(index, typeValue);
reflection->AddBool(&message, fieldDesc, typeValue);
}
break;
default:
nlwarning("this is a cpptype ?:%d", iter->_Type);
return;
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值