概述
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;
}
最后
以上就是靓丽黑米为你收集整理的同步表结构的全部内容,希望文章能够帮你解决同步表结构所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复