同步表结构

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值