diff options
Diffstat (limited to 'modules/database/db_sql_live.cpp')
| -rw-r--r-- | modules/database/db_sql_live.cpp | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp index 8d8836055..62f1c0ca4 100644 --- a/modules/database/db_sql_live.cpp +++ b/modules/database/db_sql_live.cpp @@ -13,7 +13,7 @@ class DBMySQL : public Module, public Pipe time_t lastwarn; bool ro; bool init; - std::set<Reference<Serializable> > updated_items; + std::set<Serializable *> updated_items; bool CheckSQL() { @@ -83,20 +83,17 @@ class DBMySQL : public Module, public Pipe if (!this->CheckInit()) return; - for (std::set<Reference<Serializable> >::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it) + for (std::set<Serializable *>::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it) { - Reference<Serializable> obj = *it; + Serializable *obj = *it; if (obj && this->SQL) { - Data *data = new Data(); - obj->Serialize(*data); + Data data; + obj->Serialize(data); if (obj->IsCached(data)) - { - delete data; continue; - } obj->UpdateCache(data); @@ -104,12 +101,12 @@ class DBMySQL : public Module, public Pipe if (!s_type) continue; - std::vector<Query> create = this->SQL->CreateTable(this->prefix + s_type->GetName(), *data); + 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]); - Result res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + s_type->GetName(), obj->id, *data)); - if (obj->id != res.GetID()) + Result res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + s_type->GetName(), obj->id, data)); + if (res.GetID() && obj->id != res.GetID()) { /* In this case obj is new, so place it into the object map */ obj->id = res.GetID(); @@ -144,6 +141,7 @@ class DBMySQL : public Module, public Pipe { if (!this->CheckInit()) return; + obj->UpdateTS(); this->updated_items.insert(obj); this->Notify(); } @@ -153,10 +151,13 @@ class DBMySQL : public Module, public Pipe if (!this->CheckInit()) return; Serialize::Type *s_type = obj->GetSerializableType(); - if (!s_type) - return; - this->RunQuery("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + stringify(obj->id)); - s_type->objects.erase(obj->id); + if (s_type) + { + if (obj->id > 0) + this->RunQuery("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + stringify(obj->id)); + s_type->objects.erase(obj->id); + } + this->updated_items.erase(obj); } void OnSerializeCheck(Serialize::Type *obj) anope_override @@ -191,24 +192,21 @@ class DBMySQL : public Module, public Pipe clear_null = true; std::map<unsigned int, Serializable *>::iterator it = obj->objects.find(id); if (it != obj->objects.end()) - { - it->second->Destroy(); - obj->objects.erase(it); - } + delete it->second; // This also removes this object from the map } else { - Data *data = new Data(); + 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; + data[it->first] << it->second; Serializable *s = NULL; std::map<unsigned int, Serializable *>::iterator it = obj->objects.find(id); if (it != obj->objects.end()) s = it->second; - Serializable *new_s = obj->Unserialize(s, *data); + Serializable *new_s = obj->Unserialize(s, data); if (new_s) { // If s == new_s then s->id == new_s->id @@ -216,15 +214,18 @@ class DBMySQL : public Module, public Pipe { new_s->id = id; obj->objects[id] = new_s; - new_s->UpdateCache(data); /* We know this is the most up to date copy */ + + Data data2; + /* The Unserialize operation is destructive so rebuild the data for UpdateCache */ + for (std::map<Anope::string, Anope::string>::const_iterator rit = row.begin(), rit_end = row.end(); rit != rit_end; ++rit) + if (rit->first != "id" && rit->first != "timestamp") + data2[rit->first] << rit->second; + new_s->UpdateCache(data2); /* We know this is the most up to date copy */ } - else - delete data; } else { - delete data; - s->Destroy(); + delete s; } } } @@ -238,7 +239,7 @@ class DBMySQL : public Module, public Pipe void OnSerializableUpdate(Serializable *obj) anope_override { - if (obj->IsTSCached()) + if (!this->CheckInit() || obj->IsTSCached()) return; obj->UpdateTS(); this->updated_items.insert(obj); |
