您的位置:首页 > Web前端

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

2016-09-17 17:42 302 查看
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;
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: