void Item::SaveToDB() { uint32 guid = GetGUIDLow(); switch (uState) { case ITEM_NEW: { static SqlStatementID delItem ; static SqlStatementID insItem ; SqlStatement stmt = CharacterDatabase.CreateStatement(delItem, "DELETE FROM item_instance WHERE guid = ?"); stmt.PExecute(guid); std::ostringstream ss; for (uint16 i = 0; i < m_valuesCount; ++i) ss << GetUInt32Value(i) << " "; stmt = CharacterDatabase.CreateStatement(insItem, "INSERT INTO item_instance (guid,owner_guid,data) VALUES (?, ?, ?)"); stmt.PExecute(guid, GetOwnerGuid().GetCounter(), ss.str().c_str()); } break; case ITEM_CHANGED: { static SqlStatementID updInstance ; static SqlStatementID updGifts ; SqlStatement stmt = CharacterDatabase.CreateStatement(updInstance, "UPDATE item_instance SET data = ?, owner_guid = ? WHERE guid = ?"); std::ostringstream ss; for (uint16 i = 0; i < m_valuesCount; ++i) ss << GetUInt32Value(i) << " "; stmt.PExecute(ss.str().c_str(), GetOwnerGuid().GetCounter(), guid); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED)) { stmt = CharacterDatabase.CreateStatement(updGifts, "UPDATE character_gifts SET guid = ? WHERE item_guid = ?"); stmt.PExecute(GetOwnerGuid().GetCounter(), GetGUIDLow()); } } break; case ITEM_REMOVED: { static SqlStatementID delItemText; static SqlStatementID delInst ; static SqlStatementID delGifts ; static SqlStatementID delLoot ; if (uint32 item_text_id = GetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID)) { SqlStatement stmt = CharacterDatabase.CreateStatement(delItemText, "DELETE FROM item_text WHERE id = ?"); stmt.PExecute(item_text_id); } SqlStatement stmt = CharacterDatabase.CreateStatement(delInst, "DELETE FROM item_instance WHERE guid = ?"); stmt.PExecute(guid); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED)) { stmt = CharacterDatabase.CreateStatement(delGifts, "DELETE FROM character_gifts WHERE item_guid = ?"); stmt.PExecute(GetGUIDLow()); } if (HasSavedLoot()) { stmt = CharacterDatabase.CreateStatement(delLoot, "DELETE FROM item_loot WHERE guid = ?"); stmt.PExecute(GetGUIDLow()); } delete this; return; } case ITEM_UNCHANGED: return; } if (m_lootState == ITEM_LOOT_CHANGED || m_lootState == ITEM_LOOT_REMOVED) { static SqlStatementID delLoot ; SqlStatement stmt = CharacterDatabase.CreateStatement(delLoot, "DELETE FROM item_loot WHERE guid = ?"); stmt.PExecute(GetGUIDLow()); } if (loot && (m_lootState == ITEM_LOOT_NEW || m_lootState == ITEM_LOOT_CHANGED)) { if (Player* owner = GetOwner()) { static SqlStatementID saveGold ; static SqlStatementID saveLoot ; // save money as 0 itemid data if (loot->GetGoldAmount()) { SqlStatement stmt = CharacterDatabase.CreateStatement(saveGold, "INSERT INTO item_loot (guid,owner_guid,itemid,amount,property) VALUES (?, ?, 0, ?, 0)"); stmt.PExecute(GetGUIDLow(), owner->GetGUIDLow(), loot->GetGoldAmount()); } SqlStatement stmt = CharacterDatabase.CreateStatement(saveLoot, "INSERT INTO item_loot (guid,owner_guid,itemid,amount,property) VALUES (?, ?, ?, ?, ?)"); // save items and quest items (at load its all will added as normal, but this not important for item loot case) LootItemList lootList; loot->GetLootItemsListFor(owner, lootList); for (LootItemList::const_iterator lootItr = lootList.begin(); lootItr != lootList.end(); ++lootItr) { LootItem* lootItem = *lootItr; stmt.addUInt32(GetGUIDLow()); stmt.addUInt32(owner->GetGUIDLow()); stmt.addUInt32(lootItem->itemId); stmt.addUInt8(lootItem->count); stmt.addInt32(lootItem->randomPropertyId); stmt.Execute(); } } } if (m_lootState != ITEM_LOOT_NONE && m_lootState != ITEM_LOOT_TEMPORARY) SetLootState(ITEM_LOOT_UNCHANGED); SetState(ITEM_UNCHANGED); }
void Item::SaveToDB() { uint32 guid = GetGUIDLow(); switch(m_state) { case ITEM_NEW: { DeleteFromDB(guid); std::ostringstream ss; for (uint16 i = 0; i < m_valuesCount; ++i) ss << GetUInt32Value(i) << " "; static SqlStatementID insItem; SqlStatement stmt = CharacterDatabase.CreateStatement(insItem, "INSERT INTO item_instance (guid, owner_guid, data, text) VALUES (?, ?, ?, ?)"); stmt.PExecute(guid, GetOwnerGuid().GetCounter(), ss.str().c_str(), m_text.c_str()); break; } case ITEM_CHANGED: { std::ostringstream ss; for (uint16 i = 0; i < m_valuesCount; ++i) ss << GetUInt32Value(i) << " "; static SqlStatementID updInstance; SqlStatement stmt = CharacterDatabase.CreateStatement(updInstance, "UPDATE item_instance SET data = ?, owner_guid = ?, text = ? WHERE guid = ?"); stmt.PExecute(ss.str().c_str(), GetOwnerGuid().GetCounter(), m_text.c_str(), guid); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED)) { static SqlStatementID updGifts; stmt = CharacterDatabase.CreateStatement(updGifts, "UPDATE character_gifts SET guid = ? WHERE item_guid = ?"); stmt.PExecute(GetOwnerGuid().GetCounter(), GetGUIDLow()); } break; } case ITEM_REMOVED: { DeleteFromDB(guid); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED)) DeleteGiftsFromDB(); if (HasSavedLoot()) DeleteLootFromDB(); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAG_REFUNDABLE)) DeleteRefundDataFromDB(); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_BOP_TRADEABLE)) DeleteSoulboundTradeableFromDB(); delete this; return; } case ITEM_UNCHANGED: return; } if (m_lootState == ITEM_LOOT_CHANGED || m_lootState == ITEM_LOOT_REMOVED) DeleteLootFromDB(); if (m_lootState == ITEM_LOOT_NEW || m_lootState == ITEM_LOOT_CHANGED) { if (Player* owner = GetOwner()) { // save money as 0 itemid data if (loot.gold) { static SqlStatementID saveGold; SqlStatement stmt = CharacterDatabase.CreateStatement(saveGold, "INSERT INTO item_loot (guid, owner_guid, itemid, amount, suffix, property) VALUES (?, ?, 0, ?, 0, 0)"); stmt.PExecute(GetGUIDLow(), owner->GetGUIDLow(), loot.gold); } static SqlStatementID saveLoot; SqlStatement stmt = CharacterDatabase.CreateStatement(saveLoot, "INSERT INTO item_loot (guid, owner_guid, itemid, amount, suffix, property) VALUES (?, ?, ?, ?, ?, ?)"); // save items and quest items (at load its all will added as normal, but this not important for item loot case) for (size_t i = 0; i < loot.GetMaxSlotInLootFor(owner); ++i) { QuestItem* qitem = NULL; LootItem* item = loot.LootItemInSlot(i, owner, &qitem); if (!item) continue; // questitems use the blocked field for other purposes if (!qitem && item->is_blocked) continue; stmt.addUInt32(GetGUIDLow()); stmt.addUInt32(owner->GetGUIDLow()); stmt.addUInt32(item->itemid); stmt.addUInt8(item->count); stmt.addUInt32(item->randomSuffix); stmt.addInt32(item->randomPropertyId); stmt.Execute(); } } } if (m_lootState != ITEM_LOOT_NONE && m_lootState != ITEM_LOOT_TEMPORARY) SetLootState(ITEM_LOOT_UNCHANGED); SetState(ITEM_UNCHANGED); }
void Item::SaveToDB() { uint32 guid = GetGUIDLow(); switch (uState) { case ITEM_NEW: { std::string text = m_text; CharacterDatabase.escape_string(text); CharacterDatabase.PExecute( "DELETE FROM item_instance WHERE guid = '%u'", guid ); std::ostringstream ss; ss << "INSERT INTO item_instance (guid,owner_guid,data,text) VALUES (" << guid << "," << GetOwnerGuid().GetCounter() << ",'"; for(uint16 i = 0; i < m_valuesCount; ++i ) ss << GetUInt32Value(i) << " "; ss << "', '" << text << "')"; CharacterDatabase.Execute( ss.str().c_str() ); } break; case ITEM_CHANGED: { std::string text = m_text; CharacterDatabase.escape_string(text); std::ostringstream ss; ss << "UPDATE item_instance SET data = '"; for(uint16 i = 0; i < m_valuesCount; ++i ) ss << GetUInt32Value(i) << " "; ss << "', owner_guid = '" << GetOwnerGuid().GetCounter(); ss << "', text = '" << text << "' WHERE guid = '" << guid << "'"; CharacterDatabase.Execute( ss.str().c_str() ); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED)) CharacterDatabase.PExecute("UPDATE character_gifts SET guid = '%u' WHERE item_guid = '%u'", GetOwnerGuid().GetCounter(), GetGUIDLow()); } break; case ITEM_REMOVED: { CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'", guid); if (HasFlag(ITEM_FIELD_FLAGS, ITEM_DYNFLAG_WRAPPED)) CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", GetGUIDLow()); if (HasSavedLoot()) CharacterDatabase.PExecute("DELETE FROM item_loot WHERE guid = '%u'", GetGUIDLow()); delete this; return; } case ITEM_UNCHANGED: return; } if (m_lootState == ITEM_LOOT_CHANGED || m_lootState == ITEM_LOOT_REMOVED) CharacterDatabase.PExecute("DELETE FROM item_loot WHERE guid = '%u'", GetGUIDLow()); if (m_lootState == ITEM_LOOT_NEW || m_lootState == ITEM_LOOT_CHANGED) { if(Player* owner = GetOwner()) { // save money as 0 itemid data if (loot.gold) CharacterDatabase.PExecute("INSERT INTO item_loot (guid,owner_guid,itemid,amount,suffix,property) " "VALUES (%u, %u, 0, %u, 0, 0)", GetGUIDLow(), owner->GetGUIDLow(), loot.gold); // save items and quest items (at load its all will added as normal, but this not important for item loot case) for (size_t i = 0; i < loot.GetMaxSlotInLootFor(owner); ++i) { QuestItem *qitem = NULL; LootItem *item = loot.LootItemInSlot(i,owner,&qitem); if(!item) continue; // questitems use the blocked field for other purposes if (!qitem && item->is_blocked) continue; CharacterDatabase.PExecute("INSERT INTO item_loot (guid,owner_guid,itemid,amount,suffix,property) " "VALUES (%u, %u, %u, %u, %u, %i)", GetGUIDLow(), owner->GetGUIDLow(), item->itemid, item->count, item->randomSuffix, item->randomPropertyId); } } } if (m_lootState != ITEM_LOOT_NONE && m_lootState != ITEM_LOOT_TEMPORARY) SetLootState(ITEM_LOOT_UNCHANGED); SetState(ITEM_UNCHANGED); }