将mysql中查询出来的数据 序列化到protobuffer消息结构体中
来源:互联网 发布:com域名删除的准确时间 编辑:程序博客网 时间:2024/05/16 07:23
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;
}
}
}
}
{
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
- 将mysql中查询出来的数据 序列化到protobuffer消息结构体中
- 如何将navicat for mysql 中查询出来的内容导出到xls文件中
- sql 将一个表中的查询出来的数据更新到另外一个表中
- MySQL将表a中查询的数据插入到表b中
- 将SQL查询到的数据导出到excle中
- 使用SQLyog将sqlserver的数据及表结构迁移到mysql中
- 将ActiveMQ的消息保存到MySQL中
- C#学习实例-将比较复杂的结构序列化到文件中
- 在Java编程中怎么将从数据库查询出来的数据导成Excel文件?
- 将mysql的查询结果保存到文件中
- 如何将查询到的数据显示在DataGridView中
- 将查询的数据写到redis缓存中
- 使用sqoop将mysql的数据导入到HBase中
- 将MySQL数据映射到Memcached中
- 将MySQL数据映射到Memcached中
- 将MySQL数据映射到Memcached中
- 将MySQL数据映射到Memcached中
- mysql将数据导入到excel中
- CVE-2016-4655 windknown
- 欢迎使用CSDN-markdown编辑器
- openssl制作证书的过程
- Example of Reveal animations on Android 5.0
- export,echo
- 将mysql中查询出来的数据 序列化到protobuffer消息结构体中
- 第一次blog记
- 1039. Course List for Student (25)
- 331. Verify Preorder Serialization of a Binary Tree
- 排序算法(二)——冒泡排序法
- 设计模式-工厂方法模式
- UGUI内核大探究(十一)ScrollRect与ScrollBar
- HDU 5878 I Count Two Three (暴力) 2016 ACM/ICPC Asia Regional Qingdao Online
- MATLAB小记- polybool与polyarea