我是靠谱客的博主 靓丽黑米,最近开发中收集的这篇文章主要介绍同步表结构,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

bool CDBStructSyncManagerSingleton::syncDBTableStruct(CDBTable *table, bool upgradeField)
{
nlassert(table != NULL);
std::vector<std::string> allTables;
allTables.insert(allTables.end(), table->_TableNames.begin(), table->_TableNames.end());
allTables.insert(allTables.end(), table->_BackupTableNames.begin(), table->_BackupTableNames.end());
for (uint32 i = 0; i < allTables.size(); i++)
{
std::string tableName = allTables[i];
std::string SQL = table->getCreateTableSQL(tableName);
if(!_Connection.query(SQL, false))
{
nlwarning("create  table failed !!!!!!");
return false;
}
SQL.clear();
SQL = "DESC " + tableName;
if(!_Connection.query(SQL) )
{
nlwarning("desc table failed !!!!!!");
return false;
}
MSW::CStoreResult* result = _Connection.storeResult();


std::set<string> dbFields;
for (uint32 i= 0; i < result->getNumRows() ; i++)
{
result->fetchRow();
std::string fieldName;
result->getField(0, fieldName);
dbFields.insert(fieldName);
}


_Connection.freeResult();
delete result;
result = NULL;


CDBTable::TFiels::iterator iField = table->_Fields.begin();
for (;iField != table->_Fields.end(); ++iField)
{
SQL.clear();
if (dbFields.find(iField->_Name) != dbFields.end())
{
if (upgradeField)
{
SQL = table->getAlterTableModifyFieldSQL(iField->_Name, tableName);
}
else
{
continue;
}
}
else
{
SQL = table->getAlterTableAddFieldSQL(iField->_Name, tableName);
}
if (SQL.empty())
{
nlwarning(" get alter sql is empty");
return false;
}


if(!_Connection.query(SQL, false))
{
nlwarning("alter table failed !!!!!!");
return false;
}
}
CDBTable::TRepeatFields::iterator itRepeatField = table->_RepeatFields.begin();
for (; itRepeatField != table->_RepeatFields.end();++itRepeatField)
{
for (uint32 i = 0 ; i < itRepeatField->_Fields.size(); i++)
{
SQL.clear();
if (dbFields.find(itRepeatField->_Fields[i]) != dbFields.end())
{
if (upgradeField)
{
SQL = table->getAlterTableModifyFieldSQL(itRepeatField->_Fields[i], tableName);
}
else
{
continue;
}


}
else
{
SQL = table->getAlterTableAddFieldSQL(itRepeatField->_Fields[i], tableName);
}
if (SQL.empty())
{
nlwarning(" get alter sql is empty");
return false;
}


if(!_Connection.query(SQL, false))
{
nlwarning("alter table failed !!!!!!");
return false;
}
}
}
}
return true;
}

最后

以上就是靓丽黑米为你收集整理的同步表结构的全部内容,希望文章能够帮你解决同步表结构所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部