From fd8dc1214c0f0c46362cba3fd403b1138a202fcb Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 12:28:21 -0700 Subject: [PATCH 1/6] Load converted to QueryDatabase --- common/ptimer.cpp | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/common/ptimer.cpp b/common/ptimer.cpp index 8e9f03dcf..bfca79452 100644 --- a/common/ptimer.cpp +++ b/common/ptimer.cpp @@ -127,41 +127,31 @@ PersistentTimer::PersistentTimer(uint32 char_id, pTimerType type, uint32 in_star } bool PersistentTimer::Load(Database *db) { - char errbuf[MYSQL_ERRMSG_SIZE]; - MYSQL_RES *result; - MYSQL_ROW row; - char *query = 0; - uint32 qlen = 0; - uint32 qcount = 0; - - qlen = MakeAnyLenString(&query, "SELECT start,duration,enable " - " FROM timers WHERE char_id=%lu AND type=%u", (unsigned long)_char_id, _type); #ifdef DEBUG_PTIMERS printf("Loading timer: char %lu of type %u\n", (unsigned long)_char_id, _type); #endif - - if (!db->RunQuery(query, qlen, errbuf, &result)) { - safe_delete_array(query); + std::string query = StringFormat("SELECT start, duration, enable " + "FROM timers WHERE char_id=%lu AND type=%u", + (unsigned long)_char_id, _type); + auto results = db->QueryDatabase(query); + if (!results.Success()) { #if EQDEBUG > 5 - LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Load, error: %s", errbuf); + LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Load, error: %s", results.ErrorMessage().c_str()); #endif - return(false); + return false; } - safe_delete_array(query); - bool res = false; - qcount = mysql_num_rows(result); - if(qcount == 1 && (row = mysql_fetch_row(result)) ) { - start_time = strtoul(row[0], nullptr, 10); - timer_time = strtoul(row[1], nullptr, 10); - enabled = (row[2][0] == '1'); + if (results.RowCount() != 1) + return false; - res = true; - } - mysql_free_result(result); + auto row = results.begin(); - return(res); + start_time = strtoul(row[0], nullptr, 10); + timer_time = strtoul(row[1], nullptr, 10); + enabled = (row[2][0] == '1'); + + return true; } bool PersistentTimer::Store(Database *db) { From e360ba520931a2e44428b427459cf615c9630267 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 12:35:16 -0700 Subject: [PATCH 2/6] Store converted to QueryDatabase --- common/ptimer.cpp | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/common/ptimer.cpp b/common/ptimer.cpp index bfca79452..c099832ed 100644 --- a/common/ptimer.cpp +++ b/common/ptimer.cpp @@ -156,32 +156,26 @@ bool PersistentTimer::Load(Database *db) { bool PersistentTimer::Store(Database *db) { if(Expired(db, false)) //dont need to store expired timers. - return(true); - - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - uint32 qlen = 0; - - qlen = MakeAnyLenString(&query, "REPLACE INTO timers " - " (char_id,type,start,duration,enable) " - " VALUES(%lu,%u,%lu,%lu,%d)", - (unsigned long)_char_id, _type, (unsigned long)start_time, (unsigned long)timer_time, enabled?1:0); + return true; + std::string query = StringFormat("REPLACE INTO timers " + " (char_id, type, start, duration, enable) " + " VALUES (%lu, %u, %lu, %lu, %d)", + (unsigned long)_char_id, _type, (unsigned long)start_time, + (unsigned long)timer_time, enabled ? 1: 0); #ifdef DEBUG_PTIMERS - printf("Storing timer: char %lu of type %u: '%s'\n", (unsigned long)_char_id, _type, query); + printf("Storing timer: char %lu of type %u: '%s'\n", (unsigned long)_char_id, _type, query.c_str()); #endif - - if (!db->RunQuery(query, qlen, errbuf)) { - safe_delete_array(query); + auto results = db->QueryDatabase(query); + if (!results.Success()) { #if EQDEBUG > 5 - LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Store, error: %s", errbuf); + LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Store, error: %s", results.ErrorMessage().c_str()); #endif - return(false); + return false; } - safe_delete_array(query); - return(true); + return true; } bool PersistentTimer::Clear(Database *db) { From cbed61db7e61e57b912edcbd4000e05b4d528755 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 12:37:33 -0700 Subject: [PATCH 3/6] Clear converted to QueryDatabase --- common/ptimer.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/common/ptimer.cpp b/common/ptimer.cpp index c099832ed..d8231c4ca 100644 --- a/common/ptimer.cpp +++ b/common/ptimer.cpp @@ -179,28 +179,23 @@ bool PersistentTimer::Store(Database *db) { } bool PersistentTimer::Clear(Database *db) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - uint32 qlen = 0; - - qlen = MakeAnyLenString(&query, "DELETE FROM timers " - " WHERE char_id=%lu AND type=%u ", - (unsigned long)_char_id, _type); + std::string query = StringFormat("DELETE FROM timers " + "WHERE char_id = %lu AND type = %u ", + (unsigned long)_char_id, _type); #ifdef DEBUG_PTIMERS - printf("Clearing timer: char %lu of type %u: '%s'\n", (unsigned long)_char_id, _type, query); + printf("Clearing timer: char %lu of type %u: '%s'\n", (unsigned long)_char_id, _type, query.c_str()); #endif - if (!db->RunQuery(query, qlen, errbuf)) { - safe_delete_array(query); + auto results = db->QueryDatabase(query); + if (!results.Success()) { #if EQDEBUG > 5 - LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Clear, error: %s", errbuf); + LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Clear, error: %s", results.ErrorMessage().c_str()); #endif - return(false); + return false; } - safe_delete_array(query); - return(true); + return true; } From 3cde8d9af86125a768675463f7627c964d19006a Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 12:54:20 -0700 Subject: [PATCH 4/6] Load converted to QueryDatabase --- common/ptimer.cpp | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/common/ptimer.cpp b/common/ptimer.cpp index d8231c4ca..699dba729 100644 --- a/common/ptimer.cpp +++ b/common/ptimer.cpp @@ -279,61 +279,47 @@ PTimerList::~PTimerList() { bool PTimerList::Load(Database *db) { - std::map::iterator s; - s = _list.begin(); - while(s != _list.end()) { - if(s->second != nullptr) - delete s->second; - ++s; - } + + for (auto timerIterator = _list.begin(); timerIterator != _list.end(); ++timerIterator) + if(timerIterator->second != nullptr) + delete timerIterator->second; _list.clear(); - char errbuf[MYSQL_ERRMSG_SIZE]; - MYSQL_RES *result; - MYSQL_ROW row; - char *query = 0; - uint32 qlen = 0; - uint32 qcount = 0; - - qlen = MakeAnyLenString(&query, "SELECT type,start,duration,enable " - " FROM timers WHERE char_id=%lu", (unsigned long)_char_id); - #ifdef DEBUG_PTIMERS printf("Loading all timers for char %lu\n", (unsigned long)_char_id); #endif - - if (!db->RunQuery(query, qlen, errbuf, &result)) { - safe_delete_array(query); + std::string query = StringFormat("SELECT type, start, duration, enable " + "FROM timers WHERE char_id = %lu", + (unsigned long)_char_id); + auto results = db->QueryDatabase(query); + if (!results.Success()) { #if EQDEBUG > 5 - LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Load, error: %s", errbuf); + LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Load, error: %s", results.ErrorMessage().c_str()); #endif - return(false); + return false; } - safe_delete_array(query); pTimerType type; uint32 start_time, timer_time; bool enabled; PersistentTimer *cur; - qcount = mysql_num_rows(result); - while((row = mysql_fetch_row(result)) ) { + + for (auto row = results.begin(); row != results.end(); ++row) { type = atoi(row[0]); start_time = strtoul(row[1], nullptr, 10); timer_time = strtoul(row[2], nullptr, 10); enabled = (row[3][0] == '1'); //if it expired allready, dont bother. - cur = new PersistentTimer(_char_id, type, start_time, timer_time, enabled); if(!cur->Expired(nullptr)) _list[type] = cur; else delete cur; } - mysql_free_result(result); - return(true); + return true; } bool PTimerList::Store(Database *db) { From f052c6004a92ca39d13b5d2cec1e63fe08c66ab7 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 13:12:52 -0700 Subject: [PATCH 5/6] Clear converted to QueryDatabase --- common/ptimer.cpp | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/common/ptimer.cpp b/common/ptimer.cpp index 699dba729..4f6996715 100644 --- a/common/ptimer.cpp +++ b/common/ptimer.cpp @@ -346,27 +346,19 @@ bool PTimerList::Store(Database *db) { bool PTimerList::Clear(Database *db) { _list.clear(); - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - uint32 qlen = 0; - - qlen = MakeAnyLenString(&query, "DELETE FROM timers " - " WHERE char_id=%lu ", (unsigned long)_char_id); - + std::string query = StringFormat("DELETE FROM timers WHERE char_id=%lu ", (unsigned long)_char_id); #ifdef DEBUG_PTIMERS - printf("Storing all timers for char %lu: '%s'\n", (unsigned long)_char_id, query); + printf("Storing all timers for char %lu: '%s'\n", (unsigned long)_char_id, query.c_str()); #endif - - if (!db->RunQuery(query, qlen, errbuf)) { - safe_delete_array(query); + auto results = db->QueryDatabase(query); + if (!results.Success()) { #if EQDEBUG > 5 - LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Clear, error: %s", errbuf); + LogFile->write(EQEMuLog::Error, "Error in PersistentTimer::Clear, error: %s", results.ErrorMessage().c_str()); #endif - return(false); + return false; } - safe_delete_array(query); - return(true); + return true; } void PTimerList::Start(pTimerType type, uint32 duration) { From 1253fa2a25dd8b21e3b8427fb96ba3ea43e25a72 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Mon, 18 Aug 2014 13:14:46 -0700 Subject: [PATCH 6/6] ClearOffline converted to QueryDatabase --- common/ptimer.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/common/ptimer.cpp b/common/ptimer.cpp index 4f6996715..1e1aa7349 100644 --- a/common/ptimer.cpp +++ b/common/ptimer.cpp @@ -436,26 +436,19 @@ void PTimerList::ToVector(std::vector< std::pair } bool PTimerList::ClearOffline(Database *db, uint32 char_id, pTimerType type) { - char errbuf[MYSQL_ERRMSG_SIZE]; - char *query = 0; - uint32 qlen = 0; - qlen = MakeAnyLenString(&query, "DELETE FROM timers WHERE char_id=%lu AND type=%u ",(unsigned long)char_id, type); + std::string query = StringFormat("DELETE FROM timers WHERE char_id=%lu AND type=%u ",(unsigned long)char_id, type); #ifdef DEBUG_PTIMERS - printf("Clearing timer (offline): char %lu of type %u: '%s'\n", (unsigned long)char_id, type, query); + printf("Clearing timer (offline): char %lu of type %u: '%s'\n", (unsigned long)char_id, type, query.c_str()); #endif - - if (!db->RunQuery(query, qlen, errbuf)) { - safe_delete_array(query); + auto results = db->QueryDatabase(query); + if (!results.Success()) { #if EQDEBUG > 5 - LogFile->write(EQEMuLog::Error, "Error in PTimerList::ClearOffline, error: %s", errbuf); + LogFile->write(EQEMuLog::Error, "Error in PTimerList::ClearOffline, error: %s", results.ErrorMessage().c_str()); #endif - return(false); + return false; } - safe_delete_array(query); - - return(true); - + return true; }