打包
const std::string CDBObjectManager::GetDetails()
{
std::string str;
if(GetElementCount()==0)
{
str="";
return str;
}
sg_mgrStringMap.PutInt(str,GetElementCount()); //数量
sg_mgrStringMap.PutInt(str,GetAttributeCount());//属性数量
for(iterator iter = Begin(); iter != End(); ++iter)
{
CDBObject* pObject = iter->second;
if(pObject)
pObject->BlobCompress(str);
}
if(str.size() > 1)
str.erase(str.size() -1, 1);
return str;
}
int CDBObject::BlobCompress(std::string &strBuf)
{
int nFieldSize = 0;
while(1)
{
int nAttribute = getAttribute(nFieldSize);
if(nAttribute < 0)
break;
PutInt(strBuf,nAttribute);
nFieldSize ++;
}
return nFieldSize;
}
int CPetEgg::getAttribute(int nIndex)
{
switch(nIndex)
{
case 0:
return GetPetEggID();
break;
case 1:
return GetPetEggType();
break;
case 2:
return GetHatchStartTime();
break;
case 3:
return GetHatchEndTime();
break;
default:
return -1;
}
}
bool CPetEgg::setAttribute(int nAttribute,int nIndex)
{
switch(nIndex)
{
case 0:
SetPetEggID(nAttribute);
break;
case 1:
SetPetEggType(nAttribute);
break;
case 2:
SetHatchStartTime(nAttribute);
break;
case 3:
SetHatchEndTime(nAttribute);
break;
default:
return false;
}
return true;
}
解包
int nQueryResult = pDBReader->query(SQL_QUERY_ROLE_SUPER,pRole->GetID());
if(nQueryResult != 0)
{
return false;
}
int nRow = pDBReader->get_result_rows();
if(nRow != 1)
{
return false;
}
char *pValue = NULL;
//取出详细记录
int nLen = pDBReader->get_result(0, db_challenge_details, pValue);
if (nLen > 0 && pValue != NULL)
{
pRole->m_Challenge.LoadCompress(pValue);
}
nLen = pDBReader->get_result(0, db_property_details, pValue);
if (nLen > 0 && pValue != NULL)
{
pRole->m_mgrOBPropertyManager.BaseLoadCompress(pValue);
}
nLen = pDBReader->get_result(0, db_pet_details, pValue);
if (nLen > 0 && pValue != NULL)
{
pRole->m_mgrPetManager.BaseLoadCompress(pValue);
}
bool CDBObjectManager::BaseLoadCompress(char *pValue)
{
if(NULL == pValue)
return false;
std::vector<std::string> vectTmpRecords; //所有信息
std::vector<int> vectAttribute; //详细每一个的信息
StringSplit(vectTmpRecords,pValue,FIELD_SPLIT);
int nAttributeCount = 0;
int nDbAttributeCount = 0;
int nCount = 0;
int nSize = vectTmpRecords.size();
for (int i = 0; i < nSize; i++)
{
if (i == 0)
{
nCount = String2int(vectTmpRecords[i]);//取出的数量
}
else if (i == 1)
{
int nAttrCount = String2int(vectTmpRecords[i]);//取出属性数量
nDbAttributeCount = nAttrCount;
int nAllSize = int(nDbAttributeCount*nCount)+2;
if(nSize != nAllSize)
{
// break;
}
}
else
{
GetAttribute(vectAttribute, vectTmpRecords[i]);
nAttributeCount++;
}
if (nDbAttributeCount > 0 && nAttributeCount == nDbAttributeCount )
{
//break;
if(vectAttribute.size() <= 0)
{
vectAttribute.clear();
nAttributeCount = 0;
continue;
}
int nID=vectAttribute[0];
if(nID <= 0)
{
vectAttribute.clear();
nAttributeCount = 0;
continue;
}
/*CDBObject* pObject = new CDBObject(db::CDBObject::SAVE_MODE_NORMAL, nRoleID);
for (int j = 0; j < vectAttribute.size(); j++)
{
pObject->FindFuc(vectAttribute[j], j);
}
if (!Insert(nID, pObject))
{
delete pObject;
pObject = NULL;
}*/
NewBlobObject(vectAttribute);
vectAttribute.clear();
nAttributeCount = 0;
}
}
return true;
}
bool CPetEggManager::NewBlobObject(std::vector<int> &vectAttribute)
{
if(vectAttribute.size() < 1)
return false;
int nID=vectAttribute[0];
if(nID <= 0)
return false;
db::CPetEgg* pPetEgg = new db::CPetEgg(db::CPetEgg::SAVE_MODE_NORMAL, GetRoleID());
if(pPetEgg == NULL)
return false;
for(int j=0;j<vectAttribute.size();j++)
{
pPetEgg->setAttribute(vectAttribute[j],j);
}
if (!Insert(nID, pPetEgg))
{
delete pPetEgg;
pPetEgg = NULL;
}
return true;
}