From 27860c3316b7354c6bf17cac82992085d2905934 Mon Sep 17 00:00:00 2001 From: Shauren Date: Fri, 3 Jan 2025 13:04:19 +0100 Subject: Core/Database: Added std::span based functions to Field and PreparedStatement --- src/server/bnetserver/REST/LoginRESTService.cpp | 2 +- src/server/database/Database/Field.cpp | 27 +++++++++++----------- src/server/database/Database/Field.h | 2 ++ src/server/database/Database/PreparedStatement.cpp | 16 +++++++++---- src/server/database/Database/PreparedStatement.h | 15 ++++-------- src/server/game/Accounts/AccountMgr.cpp | 8 +++---- src/server/game/Accounts/BattlenetAccountMgr.cpp | 6 ++--- src/server/game/Entities/Item/Item.cpp | 6 +++-- src/server/game/Entities/Player/Player.cpp | 12 ++++++---- src/server/game/Globals/ObjectMgr.cpp | 6 +++-- src/server/game/Tools/PlayerDump.cpp | 2 +- src/server/game/World/World.cpp | 2 +- src/server/scripts/Commands/cs_account.cpp | 6 +++-- .../scripts/Commands/cs_battlenet_account.cpp | 6 +++-- src/server/scripts/Commands/cs_disable.cpp | 2 +- src/server/scripts/Commands/cs_misc.cpp | 6 +++-- src/server/shared/DataStores/DB2DatabaseLoader.cpp | 2 +- src/server/shared/Secrets/SecretMgr.cpp | 2 +- 18 files changed, 71 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/server/bnetserver/REST/LoginRESTService.cpp b/src/server/bnetserver/REST/LoginRESTService.cpp index b2ce7ddab56..12f6cf63712 100644 --- a/src/server/bnetserver/REST/LoginRESTService.cpp +++ b/src/server/bnetserver/REST/LoginRESTService.cpp @@ -600,7 +600,7 @@ void LoginRESTService::MigrateLegacyPasswordHashes() const LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_LOGON); stmt->setInt8(0, AsUnderlyingType(SrpVersion::v1)); stmt->setBinary(1, salt); - stmt->setBinary(2, verifier); + stmt->setBinary(2, std::move(verifier)); stmt->setUInt32(3, id); tx->Append(stmt); diff --git a/src/server/database/Database/Field.cpp b/src/server/database/Database/Field.cpp index 664090839c0..6045e8dee81 100644 --- a/src/server/database/Database/Field.cpp +++ b/src/server/database/Database/Field.cpp @@ -124,26 +124,20 @@ char const* Field::GetCString() const std::string Field::GetString() const { - if (!_value) - return ""; - - char const* string = GetCString(); - if (!string) - return ""; + std::string result; + if (char const* string = GetCString()) + result.assign(string, _length); - return std::string(string, _length); + return result; } std::string_view Field::GetStringView() const { - if (!_value) - return {}; - - char const* const string = GetCString(); - if (!string) - return {}; + std::string_view result; + if (char const* const string = GetCString()) + result = { string, _length }; - return { string, _length }; + return result; } std::vector Field::GetBinary() const @@ -157,6 +151,11 @@ std::vector Field::GetBinary() const return result; } +std::span Field::GetBinaryView() const +{ + return { reinterpret_cast(_value), _length }; +} + void Field::GetBinarySizeChecked(uint8* buf, size_t length) const { ASSERT(_value && (_length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, _value ? "" : "no ", _length); diff --git a/src/server/database/Database/Field.h b/src/server/database/Database/Field.h index 1d28d8cf713..b1cbb2a8da7 100644 --- a/src/server/database/Database/Field.h +++ b/src/server/database/Database/Field.h @@ -21,6 +21,7 @@ #include "Define.h" #include "Duration.h" #include +#include #include #include #include @@ -117,6 +118,7 @@ class TC_DATABASE_API Field std::string GetString() const; std::string_view GetStringView() const; std::vector GetBinary() const; + std::span GetBinaryView() const; template std::array GetBinary() const { diff --git a/src/server/database/Database/PreparedStatement.cpp b/src/server/database/Database/PreparedStatement.cpp index 684d3ee4eb6..c0c14beec7b 100644 --- a/src/server/database/Database/PreparedStatement.cpp +++ b/src/server/database/Database/PreparedStatement.cpp @@ -100,22 +100,28 @@ void PreparedStatementBase::setDate(uint8 index, SystemTimePoint value) statement_data[index].data = value; } -void PreparedStatementBase::setString(uint8 index, std::string const& value) +void PreparedStatementBase::setString(uint8 index, std::string&& value) { ASSERT(index < statement_data.size()); - statement_data[index].data = value; + statement_data[index].data = std::move(value); } -void PreparedStatementBase::setStringView(uint8 index, std::string_view value) +void PreparedStatementBase::setString(uint8 index, std::string_view value) { ASSERT(index < statement_data.size()); statement_data[index].data.emplace(value); } -void PreparedStatementBase::setBinary(uint8 index, std::vector const& value) +void PreparedStatementBase::setBinary(uint8 index, std::vector&& value) { ASSERT(index < statement_data.size()); - statement_data[index].data = value; + statement_data[index].data = std::move(value); +} + +void PreparedStatementBase::setBinary(uint8 index, std::span value) +{ + ASSERT(index < statement_data.size()); + statement_data[index].data.emplace>(value.begin(), value.end()); } void PreparedStatementBase::setNull(uint8 index) diff --git a/src/server/database/Database/PreparedStatement.h b/src/server/database/Database/PreparedStatement.h index 88151ef193b..d34eaa9d23f 100644 --- a/src/server/database/Database/PreparedStatement.h +++ b/src/server/database/Database/PreparedStatement.h @@ -21,7 +21,7 @@ #include "DatabaseEnvFwd.h" #include "Define.h" #include "Duration.h" -#include +#include #include #include #include @@ -82,15 +82,10 @@ class TC_DATABASE_API PreparedStatementBase void setFloat(uint8 index, float value); void setDouble(uint8 index, double value); void setDate(uint8 index, SystemTimePoint value); - void setString(uint8 index, std::string const& value); - void setStringView(uint8 index, std::string_view value); - void setBinary(uint8 index, std::vector const& value); - template - void setBinary(const uint8 index, std::array const& value) - { - std::vector vec(value.begin(), value.end()); - setBinary(index, vec); - } + void setString(uint8 index, std::string&& value); + void setString(uint8 index, std::string_view value); + void setBinary(uint8 index, std::vector&& value); + void setBinary(uint8 index, std::span value); uint32 GetIndex() const { return m_index; } std::vector const& GetParameters() const { return statement_data; } diff --git a/src/server/game/Accounts/AccountMgr.cpp b/src/server/game/Accounts/AccountMgr.cpp index 2055bf437c6..c0297cafb7e 100644 --- a/src/server/game/Accounts/AccountMgr.cpp +++ b/src/server/game/Accounts/AccountMgr.cpp @@ -63,7 +63,7 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass stmt->setString(0, username); auto [salt, verifier] = Trinity::Crypto::SRP6::MakeRegistrationData(username, password); stmt->setBinary(1, salt); - stmt->setBinary(2, verifier); + stmt->setBinary(2, std::move(verifier)); stmt->setString(3, email); stmt->setString(4, email); @@ -188,7 +188,7 @@ AccountOpResult AccountMgr::ChangeUsername(uint32 accountId, std::string newUser auto [salt, verifier] = Trinity::Crypto::SRP6::MakeRegistrationData(newUsername, newPassword); stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGON); stmt->setBinary(0, salt); - stmt->setBinary(1, verifier); + stmt->setBinary(1, std::move(verifier)); stmt->setUInt32(2, accountId); LoginDatabase.Execute(stmt); @@ -217,7 +217,7 @@ AccountOpResult AccountMgr::ChangePassword(uint32 accountId, std::string newPass LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGON); stmt->setBinary(0, salt); - stmt->setBinary(1, verifier); + stmt->setBinary(1, std::move(verifier)); stmt->setUInt32(2, accountId); LoginDatabase.Execute(stmt); @@ -288,7 +288,7 @@ AccountOpResult AccountMgr::ChangeRegEmail(uint32 accountId, std::string newEmai uint32 AccountMgr::GetId(std::string_view username) { LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_GET_ACCOUNT_ID_BY_USERNAME); - stmt->setStringView(0, username); + stmt->setString(0, username); PreparedQueryResult result = LoginDatabase.Query(stmt); return (result) ? (*result)[0].GetUInt32() : 0; diff --git a/src/server/game/Accounts/BattlenetAccountMgr.cpp b/src/server/game/Accounts/BattlenetAccountMgr.cpp index 8b57ac71093..0ec58b95344 100644 --- a/src/server/game/Accounts/BattlenetAccountMgr.cpp +++ b/src/server/game/Accounts/BattlenetAccountMgr.cpp @@ -51,7 +51,7 @@ AccountOpResult Battlenet::AccountMgr::CreateBattlenetAccount(std::string email, stmt->setString(0, email); stmt->setInt8(1, AsUnderlyingType(SrpVersion::v2)); stmt->setBinary(2, salt); - stmt->setBinary(3, verifier); + stmt->setBinary(3, std::move(verifier)); LoginDatabase.DirectExecute(stmt); uint32 newAccountId = GetId(email); @@ -83,7 +83,7 @@ AccountOpResult Battlenet::AccountMgr::ChangePassword(uint32 accountId, std::str LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_LOGON); stmt->setInt8(0, AsUnderlyingType(SrpVersion::v2)); stmt->setBinary(1, salt); - stmt->setBinary(2, verifier); + stmt->setBinary(2, std::move(verifier)); stmt->setUInt32(3, accountId); LoginDatabase.Execute(stmt); @@ -160,7 +160,7 @@ AccountOpResult Battlenet::AccountMgr::UnlinkGameAccount(std::string_view gameAc uint32 Battlenet::AccountMgr::GetId(std::string_view username) { LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_ACCOUNT_ID_BY_EMAIL); - stmt->setStringView(0, username); + stmt->setString(0, username); if (PreparedQueryResult result = LoginDatabase.Query(stmt)) return (*result)[0].GetUInt32(); diff --git a/src/server/game/Entities/Item/Item.cpp b/src/server/game/Entities/Item/Item.cpp index e8d7cc2582e..c3e55c70b01 100644 --- a/src/server/game/Entities/Item/Item.cpp +++ b/src/server/game/Entities/Item/Item.cpp @@ -637,6 +637,8 @@ void Item::SaveToDB(CharacterDatabaseTransaction trans) if (m_itemData->Gems.size()) { + using namespace std::string_view_literals; + stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_ITEM_INSTANCE_GEMS); stmt->setUInt64(0, GetGUID().GetCounter()); uint32 i = 0; @@ -657,7 +659,7 @@ void Item::SaveToDB(CharacterDatabaseTransaction trans) else { stmt->setUInt32(1 + i * gemFields, 0); - stmt->setString(2 + i * gemFields, ""); + stmt->setString(2 + i * gemFields, ""sv); stmt->setUInt8(3 + i * gemFields, 0); stmt->setUInt32(4 + i * gemFields, 0); } @@ -666,7 +668,7 @@ void Item::SaveToDB(CharacterDatabaseTransaction trans) for (; i < MAX_GEM_SOCKETS; ++i) { stmt->setUInt32(1 + i * gemFields, 0); - stmt->setString(2 + i * gemFields, ""); + stmt->setString(2 + i * gemFields, ""sv); stmt->setUInt8(3 + i * gemFields, 0); stmt->setUInt32(4 + i * gemFields, 0); } diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index 6f29b300fb4..4a0d9ec36bb 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -980,11 +980,13 @@ void Player::Update(uint32 p_time) // If mute expired, remove it from the DB if (GetSession()->m_muteTime && GetSession()->m_muteTime < now) { + using namespace std::string_view_literals; + GetSession()->m_muteTime = 0; LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_MUTE_TIME); stmt->setInt64(0, 0); // Set the mute time to 0 - stmt->setString(1, ""); - stmt->setString(2, ""); + stmt->setString(1, ""sv); + stmt->setString(2, ""sv); stmt->setUInt32(3, GetSession()->GetAccountId()); LoginDatabase.Execute(stmt); } @@ -14955,8 +14957,8 @@ void Player::AddQuest(Quest const* quest, Object* questGiver) CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_QUEST_TRACK); stmt->setUInt32(0, quest_id); stmt->setUInt64(1, GetGUID().GetCounter()); - stmt->setString(2, GitRevision::GetHash()); - stmt->setString(3, GitRevision::GetDate()); + stmt->setString(2, std::string_view(GitRevision::GetHash())); + stmt->setString(3, std::string_view(GitRevision::GetDate())); // add to Quest Tracker CharacterDatabase.Execute(stmt); @@ -28158,7 +28160,7 @@ void Player::_SaveTraits(CharacterDatabaseTransaction trans) break; } - stmt->setString(8, traitConfig->Name); + stmt->setString(8, *traitConfig->Name); trans->Append(stmt); for (UF::TraitEntry const& traitEntry : traitConfig->Entries) diff --git a/src/server/game/Globals/ObjectMgr.cpp b/src/server/game/Globals/ObjectMgr.cpp index 8d9ffe1f835..9bf75f1ff18 100644 --- a/src/server/game/Globals/ObjectMgr.cpp +++ b/src/server/game/Globals/ObjectMgr.cpp @@ -7115,6 +7115,8 @@ bool ObjectMgr::AddGraveyardLink(uint32 id, uint32 zoneId, uint32 team, bool per // Store graveyard condition if team is set if (team != 0) { + using namespace std::string_view_literals; + WorldDatabasePreparedStatement* conditionStmt = WorldDatabase.GetPreparedStatement(WORLD_INS_CONDITION); conditionStmt->setUInt32(0, CONDITION_SOURCE_TYPE_GRAVEYARD); // SourceTypeOrReferenceId conditionStmt->setUInt32(1, zoneId); // SourceGroup @@ -7129,8 +7131,8 @@ bool ObjectMgr::AddGraveyardLink(uint32 id, uint32 zoneId, uint32 team, bool per conditionStmt->setUInt8(10, 0); // NegativeCondition conditionStmt->setUInt32(11, 0); // ErrorType conditionStmt->setUInt32(12, 0); // ErrorTextId - conditionStmt->setString(13, ""); // ScriptName - conditionStmt->setString(14, ""); // Comment + conditionStmt->setString(13, ""sv); // ScriptName + conditionStmt->setString(14, ""sv); // Comment WorldDatabase.Execute(conditionStmt); diff --git a/src/server/game/Tools/PlayerDump.cpp b/src/server/game/Tools/PlayerDump.cpp index 5fe16403faa..a5547652c60 100644 --- a/src/server/game/Tools/PlayerDump.cpp +++ b/src/server/game/Tools/PlayerDump.cpp @@ -640,7 +640,7 @@ inline void AppendTableDump(StringTransaction& trans, TableStruct const& tableSt } else { - std::vector b(fields[i].GetBinary()); + std::span b = fields[i].GetBinaryView(); if (!b.empty()) ss << "0x" << ByteArrayToHexStr(b); diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp index 899c279c839..00de03a22cb 100644 --- a/src/server/game/World/World.cpp +++ b/src/server/game/World/World.cpp @@ -3908,7 +3908,7 @@ void World::SetPersistentWorldVariable(PersistentWorldVariable const& var, int32 m_worldVariables[var.Id] = value; CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_REP_WORLD_VARIABLE); - stmt->setStringView(0, var.Id); + stmt->setString(0, var.Id); stmt->setInt32(1, value); CharacterDatabase.Execute(stmt); } diff --git a/src/server/scripts/Commands/cs_account.cpp b/src/server/scripts/Commands/cs_account.cpp index 873d777a3e0..2fd6e7dd557 100644 --- a/src/server/scripts/Commands/cs_account.cpp +++ b/src/server/scripts/Commands/cs_account.cpp @@ -449,8 +449,10 @@ public: } else { + using namespace std::string_view_literals; + LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_ACCOUNT_LOCK_COUNTRY); - stmt->setString(0, "00"); + stmt->setString(0, "00"sv); stmt->setUInt32(1, handler->GetSession()->GetAccountId()); LoginDatabase.Execute(stmt); handler->PSendSysMessage(LANG_COMMAND_ACCLOCKUNLOCKED); @@ -902,7 +904,7 @@ public: Trinity::Crypto::AEEncryptWithRandomIV(*decoded, *masterKey); LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_ACCOUNT_TOTP_SECRET); - stmt->setBinary(0, *decoded); + stmt->setBinary(0, std::move(*decoded)); stmt->setUInt32(1, targetAccountId); LoginDatabase.Execute(stmt); handler->PSendSysMessage(LANG_2FA_SECRET_SET_COMPLETE, accountName.c_str()); diff --git a/src/server/scripts/Commands/cs_battlenet_account.cpp b/src/server/scripts/Commands/cs_battlenet_account.cpp index 78c4860ae3b..6f38ac768ac 100644 --- a/src/server/scripts/Commands/cs_battlenet_account.cpp +++ b/src/server/scripts/Commands/cs_battlenet_account.cpp @@ -136,8 +136,10 @@ public: } else { + using namespace std::string_view_literals; + LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_ACCOUNT_LOCK_CONTRY); - stmt->setString(0, "00"); + stmt->setString(0, "00"sv); stmt->setUInt32(1, handler->GetSession()->GetBattlenetAccountId()); LoginDatabase.Execute(stmt); handler->PSendSysMessage(LANG_COMMAND_ACCLOCKUNLOCKED); @@ -356,7 +358,7 @@ public: static bool HandleListGameAccountsCommand(ChatHandler* handler, std::string const& battlenetAccountName) { LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_GAME_ACCOUNT_LIST_SMALL); - stmt->setStringView(0, battlenetAccountName); + stmt->setString(0, battlenetAccountName); if (PreparedQueryResult accountList = LoginDatabase.Query(stmt)) { auto formatDisplayName = [](char const* name) -> std::string diff --git a/src/server/scripts/Commands/cs_disable.cpp b/src/server/scripts/Commands/cs_disable.cpp index 5ded723950b..1cfed781ecd 100644 --- a/src/server/scripts/Commands/cs_disable.cpp +++ b/src/server/scripts/Commands/cs_disable.cpp @@ -203,7 +203,7 @@ public: stmt->setUInt32(0, entry); stmt->setUInt8(1, disableType); stmt->setUInt16(2, flags.value_or(0)); - stmt->setStringView(3, disableComment); + stmt->setString(3, disableComment); WorldDatabase.Execute(stmt); handler->PSendSysMessage("Add Disabled %s (Id: %u) for reason " STRING_VIEW_FMT, disableTypeStr, entry, STRING_VIEW_FMT_ARG(disableComment)); diff --git a/src/server/scripts/Commands/cs_misc.cpp b/src/server/scripts/Commands/cs_misc.cpp index bafc1980807..7320cc35550 100644 --- a/src/server/scripts/Commands/cs_misc.cpp +++ b/src/server/scripts/Commands/cs_misc.cpp @@ -1982,10 +1982,12 @@ public: target->GetSession()->m_muteTime = 0; } + using namespace std::string_view_literals; + LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_MUTE_TIME); stmt->setInt64(0, 0); - stmt->setString(1, ""); - stmt->setString(2, ""); + stmt->setString(1, ""sv); + stmt->setString(2, ""sv); stmt->setUInt32(3, accountId); LoginDatabase.Execute(stmt); diff --git a/src/server/shared/DataStores/DB2DatabaseLoader.cpp b/src/server/shared/DataStores/DB2DatabaseLoader.cpp index 5b8cea5f0c3..32595995060 100644 --- a/src/server/shared/DataStores/DB2DatabaseLoader.cpp +++ b/src/server/shared/DataStores/DB2DatabaseLoader.cpp @@ -188,7 +188,7 @@ void DB2DatabaseLoader::LoadStrings(bool custom, LocaleConstant locale, uint32 r { HotfixDatabasePreparedStatement* stmt = HotfixDatabase.GetPreparedStatement(HotfixDatabaseStatements(_loadInfo->Statement + HOTFIX_LOCALE_STMT_OFFSET)); stmt->setBool(0, !custom); - stmt->setString(1, localeNames[locale]); + stmt->setString(1, std::string_view(localeNames[locale])); PreparedQueryResult result = HotfixDatabase.Query(stmt); if (!result) return; diff --git a/src/server/shared/Secrets/SecretMgr.cpp b/src/server/shared/Secrets/SecretMgr.cpp index a4d487b3401..f084bd10bfb 100644 --- a/src/server/shared/Secrets/SecretMgr.cpp +++ b/src/server/shared/Secrets/SecretMgr.cpp @@ -200,7 +200,7 @@ Optional SecretMgr::AttemptTransition(Secrets i, Optional(totpSecret, newSecret->ToByteArray()); LoginDatabasePreparedStatement* updateStmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_ACCOUNT_TOTP_SECRET); - updateStmt->setBinary(0, totpSecret); + updateStmt->setBinary(0, std::move(totpSecret)); updateStmt->setUInt32(1, id); trans->Append(updateStmt); } while (result->NextRow()); -- cgit v1.2.3