本文整理汇总了C++中DBTransaction::commit方法的典型用法代码示例。如果您正苦于以下问题:C++ DBTransaction::commit方法的具体用法?C++ DBTransaction::commit怎么用?C++ DBTransaction::commit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBTransaction
的用法示例。
在下文中一共展示了DBTransaction::commit方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: saveMap
bool IOMapSerialize::saveMap()
{
int64_t start = OTSYS_TIME();
Database* db = Database::getInstance();
std::ostringstream query;
//Start the transaction
DBTransaction transaction;
if (!transaction.begin()) {
return false;
}
//clear old tile data
if (!db->executeQuery("DELETE FROM `tile_store`")) {
return false;
}
DBInsert stmt;
stmt.setQuery("INSERT INTO `tile_store` (`house_id`, `data`) VALUES ");
PropWriteStream stream;
for (const auto& it : Houses::getInstance().getHouses()) {
//save house items
House* house = it.second;
for (HouseTile* tile : house->getTiles()) {
saveTile(stream, tile);
size_t attributesSize;
const char* attributes = stream.getStream(attributesSize);
if (attributesSize > 0) {
query << house->getId() << ',' << db->escapeBlob(attributes, attributesSize);
if (!stmt.addRow(query)) {
return false;
}
}
stream.clear();
}
}
if (!stmt.execute()) {
return false;
}
//End the transaction
bool success = transaction.commit();
std::cout << "> Saved house items in: " <<
(OTSYS_TIME() - start) / (1000.) << " s" << std::endl;
return success;
}
示例2: savePlayer
//.........这里部分代码省略.........
DBInsert spellsQuery("INSERT INTO `player_spells` (`player_id`, `name` ) VALUES ");
for (const std::string& spellName : player->learnedInstantSpellList) {
query << player->getGUID() << ',' << db->escapeString(spellName);
if (!spellsQuery.addRow(query)) {
return false;
}
}
if (!spellsQuery.execute()) {
return false;
}
//item saving
query << "DELETE FROM `player_items` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
DBInsert itemsQuery("INSERT INTO `player_items` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES ");
ItemBlockList itemList;
for (int32_t slotId = 1; slotId <= 10; ++slotId) {
Item* item = player->inventory[slotId];
if (item) {
itemList.emplace_back(slotId, item);
}
}
if (!saveItems(player, itemList, itemsQuery, propWriteStream)) {
return false;
}
if (player->lastDepotId != -1) {
//save depot items
query.str("");
query << "DELETE FROM `player_depotitems` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
DBInsert depotQuery("INSERT INTO `player_depotitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES ");
itemList.clear();
for (const auto& it : player->depotChests) {
DepotChest* depotChest = it.second;
for (Item* item : depotChest->getItemList()) {
itemList.emplace_back(it.first, item);
}
}
if (!saveItems(player, itemList, depotQuery, propWriteStream)) {
return false;
}
}
//save inbox items
query.str("");
query << "DELETE FROM `player_inboxitems` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
DBInsert inboxQuery("INSERT INTO `player_inboxitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES ");
itemList.clear();
for (Item* item : player->getInbox()->getItemList()) {
itemList.emplace_back(0, item);
}
if (!saveItems(player, itemList, inboxQuery, propWriteStream)) {
return false;
}
query.str("");
query << "DELETE FROM `player_storage` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
query.str("");
DBInsert storageQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES ");
player->genReservedStorageRange();
for (const auto& it : player->storageMap) {
query << player->getGUID() << ',' << it.first << ',' << it.second;
if (!storageQuery.addRow(query)) {
return false;
}
}
if (!storageQuery.execute()) {
return false;
}
//End the transaction
return transaction.commit();
}
示例3: saveHouseInfo
bool IOMapSerialize::saveHouseInfo()
{
Database& db = Database::getInstance();
DBTransaction transaction;
if (!transaction.begin()) {
return false;
}
if (!db.executeQuery("DELETE FROM `house_lists`")) {
return false;
}
std::ostringstream query;
for (const auto& it : g_game.map.houses.getHouses()) {
House* house = it.second;
query << "SELECT `id` FROM `houses` WHERE `id` = " << house->getId();
DBResult_ptr result = db.storeQuery(query.str());
if (result) {
query.str(std::string());
query << "UPDATE `houses` SET `owner` = " << house->getOwner() << ", `paid` = " << house->getPaidUntil() << ", `warnings` = " << house->getPayRentWarnings() << ", `name` = " << db.escapeString(house->getName()) << ", `town_id` = " << house->getTownId() << ", `rent` = " << house->getRent() << ", `size` = " << house->getTiles().size() << ", `beds` = " << house->getBedCount() << " WHERE `id` = " << house->getId();
} else {
query.str(std::string());
query << "INSERT INTO `houses` (`id`, `owner`, `paid`, `warnings`, `name`, `town_id`, `rent`, `size`, `beds`) VALUES (" << house->getId() << ',' << house->getOwner() << ',' << house->getPaidUntil() << ',' << house->getPayRentWarnings() << ',' << db.escapeString(house->getName()) << ',' << house->getTownId() << ',' << house->getRent() << ',' << house->getTiles().size() << ',' << house->getBedCount() << ')';
}
db.executeQuery(query.str());
query.str(std::string());
}
DBInsert stmt("INSERT INTO `house_lists` (`house_id` , `listid` , `list`) VALUES ");
for (const auto& it : g_game.map.houses.getHouses()) {
House* house = it.second;
std::string listText;
if (house->getAccessList(GUEST_LIST, listText) && !listText.empty()) {
query << house->getId() << ',' << GUEST_LIST << ',' << db.escapeString(listText);
if (!stmt.addRow(query)) {
return false;
}
listText.clear();
}
if (house->getAccessList(SUBOWNER_LIST, listText) && !listText.empty()) {
query << house->getId() << ',' << SUBOWNER_LIST << ',' << db.escapeString(listText);
if (!stmt.addRow(query)) {
return false;
}
listText.clear();
}
for (Door* door : house->getDoors()) {
if (door->getAccessList(listText) && !listText.empty()) {
query << house->getId() << ',' << door->getDoorId() << ',' << db.escapeString(listText);
if (!stmt.addRow(query)) {
return false;
}
listText.clear();
}
}
}
if (!stmt.execute()) {
return false;
}
return transaction.commit();
}
示例4: savePlayer
//.........这里部分代码省略.........
return false;
}
}
if (!stmt.execute()) {
return false;
}
//item saving
query << "DELETE FROM `player_items` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
stmt.setQuery("INSERT INTO `player_items` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES ");
ItemBlockList itemList;
for (int32_t slotId = 1; slotId <= 10; ++slotId) {
Item* item = player->inventory[slotId];
if (item) {
itemList.push_back(itemBlock(slotId, item));
}
}
if (!saveItems(player, itemList, stmt)) {
return false;
}
if (player->depotChange) {
//save depot items
query.str("");
query << "DELETE FROM `player_depotitems` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
stmt.setQuery("INSERT INTO `player_depotitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES ");
itemList.clear();
for (DepotMap::iterator it = player->depotChests.begin(); it != player->depotChests.end() ; ++it) {
DepotChest* depotChest = it->second;
for (ItemDeque::const_iterator iit = depotChest->getItems(), end = depotChest->getEnd(); iit != end; ++iit) {
itemList.push_back(itemBlock(it->first, *iit));
}
}
if (!saveItems(player, itemList, stmt)) {
return false;
}
}
//save inbox items
query.str("");
query << "DELETE FROM `player_inboxitems` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
stmt.setQuery("INSERT INTO `player_inboxitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES ");
itemList.clear();
for (ItemDeque::const_iterator it = player->getInbox()->getItems(), end = player->getInbox()->getEnd(); it != end; ++it) {
itemList.push_back(itemBlock(0, *it));
}
if (!saveItems(player, itemList, stmt)) {
return false;
}
query.str("");
query << "DELETE FROM `player_storage` WHERE `player_id` = " << player->getGUID();
if (!db->executeQuery(query.str())) {
return false;
}
query.str("");
stmt.setQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES ");
player->genReservedStorageRange();
for (StorageMap::const_iterator cit = player->getStorageIteratorBegin(), end = player->getStorageIteratorEnd(); cit != end; ++cit) {
query << player->getGUID() << "," << cit->first << "," << cit->second;
if (!stmt.addRow(query)) {
return false;
}
}
if (!stmt.execute()) {
return false;
}
//End the transaction
return transaction.commit();
}