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;
}
{
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;
}