diff options
Diffstat (limited to 'modules/database/db_sql_live.cpp')
-rw-r--r-- | modules/database/db_sql_live.cpp | 72 |
1 files changed, 35 insertions, 37 deletions
diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp index e8fa9fa2f..3a02bd0c5 100644 --- a/modules/database/db_sql_live.cpp +++ b/modules/database/db_sql_live.cpp @@ -11,9 +11,11 @@ using namespace SQL; -class DBMySQL : public Module, public Pipe +class DBMySQL final + : public Module + , public Pipe { - private: +private: Anope::string prefix; ServiceReference<Provider> SQL; time_t lastwarn; @@ -35,7 +37,7 @@ class DBMySQL : public Module, public Pipe } else { - if (Anope::CurTime - Config->GetBlock("options")->Get<time_t>("updatetimeout", "5m") > lastwarn) + if (Anope::CurTime - Config->GetBlock("options")->Get<time_t>("updatetimeout", "2m") > lastwarn) { Log() << "Unable to locate SQL reference, going to readonly..."; Anope::ReadOnly = this->ro = true; @@ -51,13 +53,12 @@ class DBMySQL : public Module, public Pipe return init && SQL; } - void RunQuery(const Query &query) + Anope::string GetTableName(Serialize::Type *s_type) { - /* Can this be threaded? */ - this->RunQueryResult(query); + return this->prefix + s_type->GetName(); } - Result RunQueryResult(const Query &query) + Result RunQuery(const Query &query) { if (this->CheckSQL()) { @@ -71,7 +72,7 @@ class DBMySQL : public Module, public Pipe throw SQL::Exception("No SQL!"); } - public: +public: DBMySQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE | VENDOR), SQL("", "") { this->lastwarn = 0; @@ -83,15 +84,13 @@ class DBMySQL : public Module, public Pipe throw ModuleException("If db_sql_live is loaded it must be the first database module loaded."); } - void OnNotify() anope_override + void OnNotify() override { if (!this->CheckInit()) return; - for (std::set<Serializable *>::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it) + for (auto *obj : this->updated_items) { - Serializable *obj = *it; - if (obj && this->SQL) { Data data; @@ -106,11 +105,11 @@ class DBMySQL : public Module, public Pipe if (!s_type) continue; - std::vector<Query> create = this->SQL->CreateTable(this->prefix + s_type->GetName(), data); - for (unsigned i = 0; i < create.size(); ++i) - this->RunQueryResult(create[i]); + auto create = this->SQL->CreateTable(GetTableName(s_type), data); + for (const auto &query : create) + this->RunQuery(query); - Result res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + s_type->GetName(), obj->id, data)); + auto res = this->RunQuery(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 */ @@ -123,30 +122,30 @@ class DBMySQL : public Module, public Pipe this->updated_items.clear(); } - EventReturn OnLoadDatabase() anope_override + EventReturn OnLoadDatabase() override { init = true; return EVENT_STOP; } - void OnShutdown() anope_override + void OnShutdown() override { init = false; } - void OnRestart() anope_override + void OnRestart() override { init = false; } - void OnReload(Configuration::Conf *conf) anope_override + void OnReload(Configuration::Conf *conf) override { Configuration::Block *block = conf->GetModule(this); this->SQL = ServiceReference<Provider>("SQL::Provider", block->Get<const Anope::string>("engine")); this->prefix = block->Get<const Anope::string>("prefix", "anope_db_"); } - void OnSerializableConstruct(Serializable *obj) anope_override + void OnSerializableConstruct(Serializable *obj) override { if (!this->CheckInit()) return; @@ -155,7 +154,7 @@ class DBMySQL : public Module, public Pipe this->Notify(); } - void OnSerializableDestruct(Serializable *obj) anope_override + void OnSerializableDestruct(Serializable *obj) override { if (!this->CheckInit()) return; @@ -163,39 +162,38 @@ class DBMySQL : public Module, public Pipe if (s_type) { if (obj->id > 0) - this->RunQuery("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + stringify(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); } - void OnSerializeCheck(Serialize::Type *obj) anope_override + void OnSerializeCheck(Serialize::Type *obj) override { 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(); - Result res = this->RunQueryResult(query); + Result res = this->RunQuery(query); bool clear_null = false; for (int i = 0; i < res.Rows(); ++i) { const std::map<Anope::string, Anope::string> &row = res.Row(i); - unsigned int id; - try - { - id = convertTo<unsigned int>(res.Get(i, "id")); - } - catch (const ConvertException &) + + + auto oid = Anope::TryConvert<unsigned int>(res.Get(i, "id")); + if (!oid.has_value()) { Log(LOG_DEBUG) << "Unable to convert id from " << obj->GetName(); continue; } + auto id = oid.value(); if (res.Get(i, "timestamp").empty()) { clear_null = true; @@ -207,8 +205,8 @@ class DBMySQL : public Module, public Pipe { Data data; - for (std::map<Anope::string, Anope::string>::const_iterator it = row.begin(), it_end = row.end(); it != it_end; ++it) - data[it->first] << it->second; + for (const auto &[key, value] : row) + data[key] << value; Serializable *s = NULL; std::map<uint64_t, Serializable *>::iterator it = obj->objects.find(id); @@ -237,7 +235,7 @@ class DBMySQL : public Module, public Pipe else { if (!s) - this->RunQuery("UPDATE `" + prefix + obj->GetName() + "` SET `timestamp` = " + this->SQL->FromUnixtime(obj->GetTimestamp()) + " WHERE `id` = " + stringify(id)); + this->RunQuery("UPDATE `" + GetTableName(obj) + "` SET `timestamp` = " + this->SQL->FromUnixtime(obj->GetTimestamp()) + " WHERE `id` = " + Anope::ToString(id)); else delete s; } @@ -246,12 +244,12 @@ class DBMySQL : public Module, public Pipe 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); } } - void OnSerializableUpdate(Serializable *obj) anope_override + void OnSerializableUpdate(Serializable *obj) override { if (!this->CheckInit() || obj->IsTSCached()) return; |