From 5234eb6fc8aeccf666f61bcf62c6b7712673f138 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 17:50:03 -0700 Subject: [PATCH 1/5] LoadNPCByNPCID converted to QueryDatabase --- zone/QGlobals.cpp | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/zone/QGlobals.cpp b/zone/QGlobals.cpp index 73faa8b2c..b14713c81 100644 --- a/zone/QGlobals.cpp +++ b/zone/QGlobals.cpp @@ -51,7 +51,7 @@ void QGlobalCache::Combine(std::list &cacheA, std::list cacheB void QGlobalCache::GetQGlobals(std::list &globals, NPC *n, Client *c, Zone *z) { globals.clear(); - + QGlobalCache *npc_c = nullptr; QGlobalCache *char_c = nullptr; QGlobalCache *zone_c = nullptr; @@ -139,21 +139,15 @@ void QGlobalCache::PurgeExpiredGlobals() void QGlobalCache::LoadByNPCID(uint32 npcID) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - MYSQL_RES *result; - MYSQL_ROW row; + std::string query = StringFormat("SELECT name, charid, npcid, zoneid, value, expdate " + "FROM quest_globals WHERE npcid = %d", npcID); + auto results = database.QueryDatabase(query); + if (!results.Success()) + return; + + for (auto row = results.begin(); row != results.end(); ++row) + AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); - if (database.RunQuery(query, MakeAnyLenString(&query, "select name, charid, npcid, zoneid, value, expdate" - " from quest_globals where npcid = %d", npcID), errbuf, &result)) - { - while((row = mysql_fetch_row(result))) - { - AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]?atoi(row[5]):0xFFFFFFFF)); - } - mysql_free_result(result); - } - safe_delete_array(query); } void QGlobalCache::LoadByCharID(uint32 charID) From c6b2b1da4e353323c8d6cceb2391c345a5d1e20c Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 17:53:05 -0700 Subject: [PATCH 2/5] LoadByCharID converted to QueryDatabase --- zone/QGlobals.cpp | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/zone/QGlobals.cpp b/zone/QGlobals.cpp index b14713c81..9146340a3 100644 --- a/zone/QGlobals.cpp +++ b/zone/QGlobals.cpp @@ -152,21 +152,15 @@ void QGlobalCache::LoadByNPCID(uint32 npcID) void QGlobalCache::LoadByCharID(uint32 charID) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - MYSQL_RES *result; - MYSQL_ROW row; + std::string query = StringFormat("SELECT name, charid, npcid, zoneid, value, expdate " + "FROM quest_globals WHERE charid = %d && npcid = 0", charID); + auto results = database.QueryDatabase(query); + if (!results.Success()) + return; + + for (auto row = results.begin(); row != results.end(); ++row) + AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); - if (database.RunQuery(query, MakeAnyLenString(&query, "select name, charid, npcid, zoneid, value, expdate from" - " quest_globals where charid = %d && npcid = 0", charID), errbuf, &result)) - { - while((row = mysql_fetch_row(result))) - { - AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]?atoi(row[5]):0xFFFFFFFF)); - } - mysql_free_result(result); - } - safe_delete_array(query); } void QGlobalCache::LoadByZoneID(uint32 zoneID) From 810553de2af02a4c47ba32ff21a19db62e11d051 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 17:55:35 -0700 Subject: [PATCH 3/5] LoadByZoneID converted to QueryDatabase --- zone/QGlobals.cpp | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/zone/QGlobals.cpp b/zone/QGlobals.cpp index 9146340a3..24fd4119c 100644 --- a/zone/QGlobals.cpp +++ b/zone/QGlobals.cpp @@ -165,22 +165,16 @@ void QGlobalCache::LoadByCharID(uint32 charID) void QGlobalCache::LoadByZoneID(uint32 zoneID) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - MYSQL_RES *result; - MYSQL_ROW row; + std::string query = StringFormat("SELECT name, charid, npcid, zoneid, value, expdate " + "FROM quest_globals WHERE zoneid = %d && npcid = 0 && charid = 0", zoneID); + auto results = database.QueryDatabase(query); + if (!results.Success()) + return; - if (database.RunQuery(query, MakeAnyLenString(&query, "select name, charid, npcid, zoneid, value, expdate from quest_globals" - " where zoneid = %d && npcid = 0 && charid = 0", zoneID), errbuf, &result)) - { - while((row = mysql_fetch_row(result))) - { - AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]?atoi(row[5]):0xFFFFFFFF)); - } - mysql_free_result(result); - } - safe_delete_array(query); + for (auto row = results.begin(); row != results.end(); ++row) + AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); } + void QGlobalCache::LoadByGlobalContext() { char errbuf[MYSQL_ERRMSG_SIZE]; From 8d909ea8e279faa5abd652a3f049daac0a2e3ebb Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 17:58:51 -0700 Subject: [PATCH 4/5] LoadByGlobalContext converted to QueryDatabase --- zone/QGlobals.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/zone/QGlobals.cpp b/zone/QGlobals.cpp index 24fd4119c..7bd09d8f7 100644 --- a/zone/QGlobals.cpp +++ b/zone/QGlobals.cpp @@ -177,19 +177,12 @@ void QGlobalCache::LoadByZoneID(uint32 zoneID) void QGlobalCache::LoadByGlobalContext() { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - MYSQL_RES *result; - MYSQL_ROW row; + std::string query = "SELECT name, charid, npcid, zoneid, value, expdate " + "FROM quest_globals WHERE zoneid = 0 && npcid = 0 && charid = 0"; + auto results = database.QueryDatabase(query); + if (!results.Success()) + return; - if (database.RunQuery(query, MakeAnyLenString(&query, "select name, charid, npcid, zoneid, value, expdate from quest_globals" - " where zoneid = 0 && npcid = 0 && charid = 0"), errbuf, &result)) - { - while((row = mysql_fetch_row(result))) - { - AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]?atoi(row[5]):0xFFFFFFFF)); - } - mysql_free_result(result); - } - safe_delete_array(query); + for (auto row = results.begin(); row != results.end(); ++row) + AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); } From 7692793289f8bd3bc03aa8696edbe81d16e74f73 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 18:10:28 -0700 Subject: [PATCH 5/5] Consolidated LoadBy code into new LoadBy method --- zone/QGlobals.cpp | 29 +++++++++-------------------- zone/QGlobals.h | 1 + 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/zone/QGlobals.cpp b/zone/QGlobals.cpp index 7bd09d8f7..d21bebf2d 100644 --- a/zone/QGlobals.cpp +++ b/zone/QGlobals.cpp @@ -141,48 +141,37 @@ void QGlobalCache::LoadByNPCID(uint32 npcID) { std::string query = StringFormat("SELECT name, charid, npcid, zoneid, value, expdate " "FROM quest_globals WHERE npcid = %d", npcID); - auto results = database.QueryDatabase(query); - if (!results.Success()) - return; - - for (auto row = results.begin(); row != results.end(); ++row) - AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); - + LoadBy(query); } void QGlobalCache::LoadByCharID(uint32 charID) { std::string query = StringFormat("SELECT name, charid, npcid, zoneid, value, expdate " "FROM quest_globals WHERE charid = %d && npcid = 0", charID); - auto results = database.QueryDatabase(query); - if (!results.Success()) - return; - - for (auto row = results.begin(); row != results.end(); ++row) - AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); - + LoadBy(query); } void QGlobalCache::LoadByZoneID(uint32 zoneID) { std::string query = StringFormat("SELECT name, charid, npcid, zoneid, value, expdate " "FROM quest_globals WHERE zoneid = %d && npcid = 0 && charid = 0", zoneID); - auto results = database.QueryDatabase(query); - if (!results.Success()) - return; - - for (auto row = results.begin(); row != results.end(); ++row) - AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); + LoadBy(query); } void QGlobalCache::LoadByGlobalContext() { std::string query = "SELECT name, charid, npcid, zoneid, value, expdate " "FROM quest_globals WHERE zoneid = 0 && npcid = 0 && charid = 0"; + LoadBy(query); + } + +void QGlobalCache::LoadBy(const std::string query) +{ auto results = database.QueryDatabase(query); if (!results.Success()) return; for (auto row = results.begin(); row != results.end(); ++row) AddGlobal(0, QGlobal(std::string(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), row[4], row[5]? atoi(row[5]): 0xFFFFFFFF)); + } diff --git a/zone/QGlobals.h b/zone/QGlobals.h index 73a795186..b879cba97 100644 --- a/zone/QGlobals.h +++ b/zone/QGlobals.h @@ -42,6 +42,7 @@ public: void LoadByZoneID(uint32 zoneID); //zone void LoadByGlobalContext(); //zone protected: + void LoadBy(const std::string query); std::list qGlobalBucket; };