diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp index d8fe89972..22b617d9f 100644 --- a/modules/database/db_sql.cpp +++ b/modules/database/db_sql.cpp @@ -72,6 +72,11 @@ class DBSQL final bool loaded = false; bool imported = false; + Anope::string GetTableName(Serialize::Type *s_type) + { + return this->prefix + s_type->GetName(); + } + void RunBackground(const Query &q, Interface *iface = NULL) { if (!this->sql) @@ -124,8 +129,8 @@ public: if (!s_type) continue; - std::vector create = this->sql->CreateTable(this->prefix + s_type->GetName(), data); - Query insert = this->sql->BuildInsert(this->prefix + s_type->GetName(), obj->id, data); + auto create = this->sql->CreateTable(GetTableName(s_type), data); + auto insert = this->sql->BuildInsert(GetTableName(s_type), obj->id, data); if (this->imported) { @@ -209,7 +214,7 @@ public: return; Serialize::Type *s_type = obj->GetSerializableType(); if (s_type && obj->id > 0) - this->RunBackground("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + Anope::ToString(obj->id)); + this->RunBackground("DELETE FROM `" + GetTableName(s_type) + "` WHERE `id` = " + Anope::ToString(obj->id)); this->updated_items.erase(obj); } @@ -229,7 +234,7 @@ public: if (!this->loading_databases && !this->loaded) return; - Query query("SELECT * FROM `" + this->prefix + sb->GetName() + "`"); + Query query("SELECT * FROM `" + GetTableName(sb) + "`"); Result res = this->sql->RunQuery(query); for (int j = 0; j < res.Rows(); ++j) diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp index ea1fb1a27..b9f4477cf 100644 --- a/modules/database/db_sql_live.cpp +++ b/modules/database/db_sql_live.cpp @@ -53,6 +53,11 @@ private: return init && SQL; } + Anope::string GetTableName(Serialize::Type *s_type) + { + return this->prefix + s_type->GetName(); + } + void RunQuery(const Query &query) { /* Can this be threaded? */ @@ -106,11 +111,11 @@ public: if (!s_type) continue; - std::vector create = this->SQL->CreateTable(this->prefix + s_type->GetName(), data); + auto create = this->SQL->CreateTable(GetTableName(s_type), data); for (const auto &query : create) this->RunQueryResult(query); - Result res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + s_type->GetName(), obj->id, data)); + auto res = this->RunQueryResult(this->SQL->BuildInsert(GetTableName(s_type), obj->id, data)); if (res.GetID() && obj->id != res.GetID()) { /* In this case obj is new, so place it into the object map */ @@ -163,7 +168,7 @@ public: if (s_type) { if (obj->id > 0) - this->RunQuery("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + Anope::ToString(obj->id)); + this->RunQuery("DELETE FROM `" + GetTableName(s_type) + "` WHERE `id` = " + Anope::ToString(obj->id)); s_type->objects.erase(obj->id); } this->updated_items.erase(obj); @@ -174,7 +179,7 @@ public: if (!this->CheckInit() || obj->GetTimestamp() == Anope::CurTime) return; - Query query("SELECT * FROM `" + this->prefix + obj->GetName() + "` WHERE (`timestamp` >= " + this->SQL->FromUnixtime(obj->GetTimestamp()) + " OR `timestamp` IS NULL)"); + Query query("SELECT * FROM `" + GetTableName(obj) + "` WHERE (`timestamp` >= " + this->SQL->FromUnixtime(obj->GetTimestamp()) + " OR `timestamp` IS NULL)"); obj->UpdateTimestamp(); @@ -236,7 +241,7 @@ public: else { if (!s) - this->RunQuery("UPDATE `" + prefix + obj->GetName() + "` SET `timestamp` = " + this->SQL->FromUnixtime(obj->GetTimestamp()) + " WHERE `id` = " + Anope::ToString(id)); + this->RunQuery("UPDATE `" + GetTableName(obj) + "` SET `timestamp` = " + this->SQL->FromUnixtime(obj->GetTimestamp()) + " WHERE `id` = " + Anope::ToString(id)); else delete s; } @@ -245,7 +250,7 @@ public: if (clear_null) { - query = "DELETE FROM `" + this->prefix + obj->GetName() + "` WHERE `timestamp` IS NULL"; + query = "DELETE FROM `" + GetTableName(obj) + "` WHERE `timestamp` IS NULL"; this->RunQuery(query); } }