From 56c29154f00fa0afc68636e24a7c643a2589f245 Mon Sep 17 00:00:00 2001 From: Alex King <89047260+Kinglykrab@users.noreply.github.com> Date: Thu, 7 Dec 2023 23:12:01 -0500 Subject: [PATCH] [Database] Add primary key to keyring table (#3746) * [Database] Add primary key to keyring table # Notes - Adds a primary key of `id` to `keyring` table so we can use it with repositories. * Update version.h * Update client.cpp * Update client.cpp --- common/database/database_update_manifest.cpp | 12 + .../base/base_keyring_repository.h | 344 ++++++++++++++++++ common/repositories/keyring_repository.h | 50 +++ common/ruletypes.h | 1 + common/version.h | 2 +- zone/client.cpp | 82 +++-- 6 files changed, 466 insertions(+), 25 deletions(-) create mode 100644 common/repositories/base/base_keyring_repository.h create mode 100644 common/repositories/keyring_repository.h diff --git a/common/database/database_update_manifest.cpp b/common/database/database_update_manifest.cpp index 0dd02d76e..bb9979c3d 100644 --- a/common/database/database_update_manifest.cpp +++ b/common/database/database_update_manifest.cpp @@ -5104,6 +5104,18 @@ ALTER TABLE `items` MODIFY COLUMN `updated` datetime NULL DEFAULT NULL; ALTER TABLE `object` CHANGE COLUMN `unknown08` `size_percentage` float NOT NULL DEFAULT 0 AFTER `icon`; ALTER TABLE `object` CHANGE COLUMN `unknown10` `solid_type` mediumint(5) NOT NULL DEFAULT 0 AFTER `size`; ALTER TABLE `object` CHANGE COLUMN `unknown20` `incline` int(11) NOT NULL DEFAULT 0 AFTER `solid_type`; +)" + }, + ManifestEntry{ + .version = 9246, + .description = "2023_12_07_keyring_id.sql", + .check = "SHOW COLUMNS FROM `keyring` LIKE 'id'", + .condition = "empty", + .match = "", + .sql = R"( +ALTER TABLE `keyring` +ADD COLUMN `id` int UNSIGNED NOT NULL AUTO_INCREMENT FIRST, +ADD PRIMARY KEY (`id`); )" } diff --git a/common/repositories/base/base_keyring_repository.h b/common/repositories/base/base_keyring_repository.h new file mode 100644 index 000000000..fa12e90d2 --- /dev/null +++ b/common/repositories/base/base_keyring_repository.h @@ -0,0 +1,344 @@ +/** + * DO NOT MODIFY THIS FILE + * + * This repository was automatically generated and is NOT to be modified directly. + * Any repository modifications are meant to be made to the repository extending the base. + * Any modifications to base repositories are to be made by the generator only + * + * @generator ./utils/scripts/generators/repository-generator.pl + * @docs https://eqemu.gitbook.io/server/in-development/developer-area/repositories + */ + +#ifndef EQEMU_BASE_KEYRING_REPOSITORY_H +#define EQEMU_BASE_KEYRING_REPOSITORY_H + +#include "../../database.h" +#include "../../strings.h" +#include + + +class BaseKeyringRepository { +public: + struct Keyring { + uint32_t id; + int32_t char_id; + int32_t item_id; + }; + + static std::string PrimaryKey() + { + return std::string("id"); + } + + static std::vector Columns() + { + return { + "id", + "char_id", + "item_id", + }; + } + + static std::vector SelectColumns() + { + return { + "id", + "char_id", + "item_id", + }; + } + + static std::string ColumnsRaw() + { + return std::string(Strings::Implode(", ", Columns())); + } + + static std::string SelectColumnsRaw() + { + return std::string(Strings::Implode(", ", SelectColumns())); + } + + static std::string TableName() + { + return std::string("keyring"); + } + + static std::string BaseSelect() + { + return fmt::format( + "SELECT {} FROM {}", + SelectColumnsRaw(), + TableName() + ); + } + + static std::string BaseInsert() + { + return fmt::format( + "INSERT INTO {} ({}) ", + TableName(), + ColumnsRaw() + ); + } + + static Keyring NewEntity() + { + Keyring e{}; + + e.id = 0; + e.char_id = 0; + e.item_id = 0; + + return e; + } + + static Keyring GetKeyring( + const std::vector &keyrings, + int keyring_id + ) + { + for (auto &keyring : keyrings) { + if (keyring.id == keyring_id) { + return keyring; + } + } + + return NewEntity(); + } + + static Keyring FindOne( + Database& db, + int keyring_id + ) + { + auto results = db.QueryDatabase( + fmt::format( + "{} WHERE {} = {} LIMIT 1", + BaseSelect(), + PrimaryKey(), + keyring_id + ) + ); + + auto row = results.begin(); + if (results.RowCount() == 1) { + Keyring e{}; + + e.id = static_cast(strtoul(row[0], nullptr, 10)); + e.char_id = static_cast(atoi(row[1])); + e.item_id = static_cast(atoi(row[2])); + + return e; + } + + return NewEntity(); + } + + static int DeleteOne( + Database& db, + int keyring_id + ) + { + auto results = db.QueryDatabase( + fmt::format( + "DELETE FROM {} WHERE {} = {}", + TableName(), + PrimaryKey(), + keyring_id + ) + ); + + return (results.Success() ? results.RowsAffected() : 0); + } + + static int UpdateOne( + Database& db, + const Keyring &e + ) + { + std::vector v; + + auto columns = Columns(); + + v.push_back(columns[1] + " = " + std::to_string(e.char_id)); + v.push_back(columns[2] + " = " + std::to_string(e.item_id)); + + auto results = db.QueryDatabase( + fmt::format( + "UPDATE {} SET {} WHERE {} = {}", + TableName(), + Strings::Implode(", ", v), + PrimaryKey(), + e.id + ) + ); + + return (results.Success() ? results.RowsAffected() : 0); + } + + static Keyring InsertOne( + Database& db, + Keyring e + ) + { + std::vector v; + + v.push_back(std::to_string(e.id)); + v.push_back(std::to_string(e.char_id)); + v.push_back(std::to_string(e.item_id)); + + auto results = db.QueryDatabase( + fmt::format( + "{} VALUES ({})", + BaseInsert(), + Strings::Implode(",", v) + ) + ); + + if (results.Success()) { + e.id = results.LastInsertedID(); + return e; + } + + e = NewEntity(); + + return e; + } + + static int InsertMany( + Database& db, + const std::vector &entries + ) + { + std::vector insert_chunks; + + for (auto &e: entries) { + std::vector v; + + v.push_back(std::to_string(e.id)); + v.push_back(std::to_string(e.char_id)); + v.push_back(std::to_string(e.item_id)); + + insert_chunks.push_back("(" + Strings::Implode(",", v) + ")"); + } + + std::vector v; + + auto results = db.QueryDatabase( + fmt::format( + "{} VALUES {}", + BaseInsert(), + Strings::Implode(",", insert_chunks) + ) + ); + + return (results.Success() ? results.RowsAffected() : 0); + } + + static std::vector All(Database& db) + { + std::vector all_entries; + + auto results = db.QueryDatabase( + fmt::format( + "{}", + BaseSelect() + ) + ); + + all_entries.reserve(results.RowCount()); + + for (auto row = results.begin(); row != results.end(); ++row) { + Keyring e{}; + + e.id = static_cast(strtoul(row[0], nullptr, 10)); + e.char_id = static_cast(atoi(row[1])); + e.item_id = static_cast(atoi(row[2])); + + all_entries.push_back(e); + } + + return all_entries; + } + + static std::vector GetWhere(Database& db, const std::string &where_filter) + { + std::vector all_entries; + + auto results = db.QueryDatabase( + fmt::format( + "{} WHERE {}", + BaseSelect(), + where_filter + ) + ); + + all_entries.reserve(results.RowCount()); + + for (auto row = results.begin(); row != results.end(); ++row) { + Keyring e{}; + + e.id = static_cast(strtoul(row[0], nullptr, 10)); + e.char_id = static_cast(atoi(row[1])); + e.item_id = static_cast(atoi(row[2])); + + all_entries.push_back(e); + } + + return all_entries; + } + + static int DeleteWhere(Database& db, const std::string &where_filter) + { + auto results = db.QueryDatabase( + fmt::format( + "DELETE FROM {} WHERE {}", + TableName(), + where_filter + ) + ); + + return (results.Success() ? results.RowsAffected() : 0); + } + + static int Truncate(Database& db) + { + auto results = db.QueryDatabase( + fmt::format( + "TRUNCATE TABLE {}", + TableName() + ) + ); + + return (results.Success() ? results.RowsAffected() : 0); + } + + static int64 GetMaxId(Database& db) + { + auto results = db.QueryDatabase( + fmt::format( + "SELECT COALESCE(MAX({}), 0) FROM {}", + PrimaryKey(), + TableName() + ) + ); + + return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0); + } + + static int64 Count(Database& db, const std::string &where_filter = "") + { + auto results = db.QueryDatabase( + fmt::format( + "SELECT COUNT(*) FROM {} {}", + TableName(), + (where_filter.empty() ? "" : "WHERE " + where_filter) + ) + ); + + return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0); + } + +}; + +#endif //EQEMU_BASE_KEYRING_REPOSITORY_H diff --git a/common/repositories/keyring_repository.h b/common/repositories/keyring_repository.h new file mode 100644 index 000000000..c4367eaf4 --- /dev/null +++ b/common/repositories/keyring_repository.h @@ -0,0 +1,50 @@ +#ifndef EQEMU_KEYRING_REPOSITORY_H +#define EQEMU_KEYRING_REPOSITORY_H + +#include "../database.h" +#include "../strings.h" +#include "base/base_keyring_repository.h" + +class KeyringRepository: public BaseKeyringRepository { +public: + + /** + * This file was auto generated and can be modified and extended upon + * + * Base repository methods are automatically + * generated in the "base" version of this repository. The base repository + * is immutable and to be left untouched, while methods in this class + * are used as extension methods for more specific persistence-layer + * accessors or mutators. + * + * Base Methods (Subject to be expanded upon in time) + * + * Note: Not all tables are designed appropriately to fit functionality with all base methods + * + * InsertOne + * UpdateOne + * DeleteOne + * FindOne + * GetWhere(std::string where_filter) + * DeleteWhere(std::string where_filter) + * InsertMany + * All + * + * Example custom methods in a repository + * + * KeyringRepository::GetByZoneAndVersion(int zone_id, int zone_version) + * KeyringRepository::GetWhereNeverExpires() + * KeyringRepository::GetWhereXAndY() + * KeyringRepository::DeleteWhereXAndY() + * + * Most of the above could be covered by base methods, but if you as a developer + * find yourself re-using logic for other parts of the code, its best to just make a + * method that can be re-used easily elsewhere especially if it can use a base repository + * method and encapsulate filters there + */ + + // Custom extended repository methods here + +}; + +#endif //EQEMU_KEYRING_REPOSITORY_H diff --git a/common/ruletypes.h b/common/ruletypes.h index bc91057ca..b36a3f741 100644 --- a/common/ruletypes.h +++ b/common/ruletypes.h @@ -304,6 +304,7 @@ RULE_BOOL(World, EnableDevTools, true, "Enable or Disable the Developer Tools gl RULE_BOOL(World, EnableChecksumVerification, false, "Enable or Disable the Checksum Verification for eqgame.exe and spells_us.txt") RULE_INT(World, MaximumQuestErrors, 30, "Changes the maximum number of quest errors that can be displayed in #questerrors, default is 30") RULE_INT(World, BootHour, 0, "Sets the in-game hour world will set when it first boots. 0-24 are valid options, where 0 disables this rule") +RULE_BOOL(World, UseItemLinksForKeyRing, false, "Uses item links for Key Ring Listing instead of item name") RULE_CATEGORY_END() RULE_CATEGORY(Zone) diff --git a/common/version.h b/common/version.h index be5294acb..74b2b345f 100644 --- a/common/version.h +++ b/common/version.h @@ -42,7 +42,7 @@ * Manifest: https://github.com/EQEmu/Server/blob/master/utils/sql/db_update_manifest.txt */ -#define CURRENT_BINARY_DATABASE_VERSION 9245 +#define CURRENT_BINARY_DATABASE_VERSION 9246 #define CURRENT_BINARY_BOTS_DATABASE_VERSION 9040 diff --git a/zone/client.cpp b/zone/client.cpp index 7ee10f543..0039334c5 100644 --- a/zone/client.cpp +++ b/zone/client.cpp @@ -65,6 +65,7 @@ extern volatile bool RunLoops; #include "../common/repositories/character_disciplines_repository.h" #include "../common/repositories/character_data_repository.h" #include "../common/repositories/discovered_items_repository.h" +#include "../common/repositories/keyring_repository.h" #include "../common/events/player_events.h" #include "../common/events/player_event_logs.h" #include "dialogue_window.h" @@ -4087,54 +4088,87 @@ void Client::SendWindow( void Client::KeyRingLoad() { - std::string query = StringFormat("SELECT item_id FROM keyring " - "WHERE char_id = '%i' ORDER BY item_id", character_id); - auto results = database.QueryDatabase(query); - if (!results.Success()) { + const auto &l = KeyringRepository::GetWhere( + database, + fmt::format( + "`char_id` = {} ORDER BY `item_id`", + character_id + ) + ); + + if (l.empty()) { return; } - for (auto row = results.begin(); row != results.end(); ++row) - keyring.push_back(Strings::ToInt(row[0])); + for (const auto &e : l) { + keyring.emplace_back(e.item_id); + } } void Client::KeyRingAdd(uint32 item_id) { - if(0==item_id) - return; - - bool found = KeyRingCheck(item_id); - if (found) - return; - - std::string query = StringFormat("INSERT INTO keyring(char_id, item_id) VALUES(%i, %i)", character_id, item_id); - auto results = database.QueryDatabase(query); - if (!results.Success()) { + if (!item_id) { return; } - Message(Chat::LightBlue,"Added to keyring."); + const bool found = KeyRingCheck(item_id); + if (found) { + return; + } - keyring.push_back(item_id); + auto e = KeyringRepository::NewEntity(); + + e.char_id = CharacterID(); + e.item_id = item_id; + + e = KeyringRepository::InsertOne(database, e); + + if (!e.id) { + return; + } + + keyring.emplace_back(item_id); + + if (!RuleB(World, UseItemLinksForKeyRing)) { + Message(Chat::LightBlue, "Added to keyring."); + return; + } + + const std::string &item_link = database.CreateItemLink(item_id); + + Message( + Chat::LightBlue, + fmt::format( + "Added {} to keyring.", + item_link + ).c_str() + ); } bool Client::KeyRingCheck(uint32 item_id) { - for (auto iter = keyring.begin(); iter != keyring.end(); ++iter) { - if(*iter == item_id) + for (const auto &e : keyring) { + if (e == item_id) { return true; + } } + return false; } void Client::KeyRingList() { - Message(Chat::LightBlue,"Keys on Keyring:"); + Message(Chat::LightBlue, "Keys on Keyring:"); + const EQ::ItemData *item = nullptr; - for (auto iter = keyring.begin(); iter != keyring.end(); ++iter) { - if ((item = database.GetItem(*iter))!=nullptr) { - Message(Chat::LightBlue,item->Name); + + for (const auto &e : keyring) { + item = database.GetItem(e); + if (item) { + const std::string &item_string = RuleB(World, UseItemLinksForKeyRing) ? database.CreateItemLink(e) : item->Name; + + Message(Chat::LightBlue, item_string.c_str()); } } }