diff options
Diffstat (limited to 'modules/database')
-rw-r--r-- | modules/database/db_flatfile.cpp | 103 | ||||
-rw-r--r-- | modules/database/db_sql.cpp | 12 | ||||
-rw-r--r-- | modules/database/db_sql_live.cpp | 25 |
3 files changed, 94 insertions, 46 deletions
diff --git a/modules/database/db_flatfile.cpp b/modules/database/db_flatfile.cpp index a4ab5ce9a..4649a3666 100644 --- a/modules/database/db_flatfile.cpp +++ b/modules/database/db_flatfile.cpp @@ -82,42 +82,58 @@ class DBFlatFile : public Module EventReturn OnLoadDatabase() anope_override { - std::fstream db; - db.open(DatabaseFile.c_str(), std::ios_base::in); + std::map<Module *, std::fstream *> databases; + databases[NULL] = new std::fstream(DatabaseFile.c_str(), std::ios_base::in); - if (!db.is_open()) + if (!databases[NULL]->is_open()) { + delete databases[NULL]; Log() << "Unable to open " << DatabaseFile << " for reading!"; return EVENT_CONTINUE; } - SerializeType *st = NULL; - Serialize::Data data; - std::multimap<SerializeType *, Serialize::Data> objects; - for (Anope::string buf, token; std::getline(db, buf.str());) - { - spacesepstream sep(buf); - - if (!sep.GetToken(token)) - continue; + const std::vector<Anope::string> type_order = SerializeType::GetTypeOrder(); - if (token == "OBJECT" && sep.GetToken(token)) + for (unsigned i = 0; i < type_order.size(); ++i) + { + SerializeType *stype = SerializeType::Find(type_order[i]); + if (stype && !databases.count(stype->GetOwner())) { - st = SerializeType::Find(token); - data.clear(); + Anope::string db_name = db_dir + "/module_" + stype->GetOwner()->name + ".db"; + databases[stype->GetOwner()] = new std::fstream(db_name.c_str(), std::ios_base::in); } - else if (token == "DATA" && st != NULL && sep.GetToken(token)) - data[token] << sep.GetRemaining(); - else if (token == "END" && st != NULL) - { - objects.insert(std::make_pair(st, data)); + } - st = NULL; - data.clear(); + std::multimap<SerializeType *, Serialize::Data> objects; + for (std::map<Module *, std::fstream *>::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it) + { + std::fstream *db = it->second; + SerializeType *st = NULL; + Serialize::Data data; + for (Anope::string buf, token; std::getline(*db, buf.str());) + { + spacesepstream sep(buf); + + if (!sep.GetToken(token)) + continue; + + if (token == "OBJECT" && sep.GetToken(token)) + { + st = SerializeType::Find(token); + data.clear(); + } + else if (token == "DATA" && st != NULL && sep.GetToken(token)) + data[token] << sep.GetRemaining(); + else if (token == "END" && st != NULL) + { + objects.insert(std::make_pair(st, data)); + + st = NULL; + data.clear(); + } } } - const std::vector<Anope::string> type_order = SerializeType::GetTypeOrder(); for (unsigned i = 0; i < type_order.size(); ++i) { SerializeType *stype = SerializeType::Find(type_order[i]); @@ -129,7 +145,11 @@ class DBFlatFile : public Module it->first->Unserialize(NULL, it->second); } - db.close(); + for (std::map<Module *, std::fstream *>::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it) + { + it->second->close(); + delete it->second; + } return EVENT_STOP; } @@ -143,9 +163,11 @@ class DBFlatFile : public Module if (IsFile(DatabaseFile)) rename(DatabaseFile.c_str(), tmp_db.c_str()); - std::fstream db(DatabaseFile.c_str(), std::ios_base::out | std::ios_base::trunc); - if (!db.is_open()) + std::map<Module *, std::fstream *> databases; + databases[NULL] = new std::fstream(DatabaseFile.c_str(), std::ios_base::out | std::ios_base::trunc); + if (!databases[NULL]->is_open()) { + delete databases[NULL]; Log() << "Unable to open " << DatabaseFile << " for writing"; if (IsFile(tmp_db)) rename(tmp_db.c_str(), DatabaseFile.c_str()); @@ -156,19 +178,30 @@ class DBFlatFile : public Module for (std::list<Serializable *>::const_iterator it = items.begin(), it_end = items.end(); it != it_end; ++it) { Serializable *base = *it; + SerializeType *s_type = base->GetSerializableType(); + + if (!s_type) + continue; + Serialize::Data data = base->serialize(); + + if (!databases.count(s_type->GetOwner())) + { + Anope::string db_name = db_dir + "/module_" + s_type->GetOwner()->name + ".db"; + databases[s_type->GetOwner()] = new std::fstream(db_name.c_str(), std::ios_base::out | std::ios_base::trunc); + } + std::fstream *fd = databases[s_type->GetOwner()]; - db << "OBJECT " << base->serialize_name() << "\n"; + *fd << "OBJECT " << s_type->GetName() << "\n"; for (Serialize::Data::iterator dit = data.begin(), dit_end = data.end(); dit != dit_end; ++dit) - db << "DATA " << dit->first << " " << dit->second.astr() << "\n"; - db << "END\n"; + *fd << "DATA " << dit->first << " " << dit->second.astr() << "\n"; + *fd << "END\n"; } - db.close(); - - if (db.good() == false) + if (databases[NULL]->good() == false) { Log() << "Unable to write database"; + databases[NULL]->close(); if (!Config->NoBackupOkay) quitting = true; if (IsFile(tmp_db)) @@ -177,6 +210,12 @@ class DBFlatFile : public Module else unlink(tmp_db.c_str()); + for (std::map<Module *, std::fstream *>::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it) + { + it->second->close(); + delete it->second; + } + return EVENT_CONTINUE; } }; diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp index c07bdaa59..acc59b668 100644 --- a/modules/database/db_sql.cpp +++ b/modules/database/db_sql.cpp @@ -105,13 +105,17 @@ class DBSQL : public Module, public Pipe continue; obj->UpdateCache(); + SerializeType *s_type = obj->GetSerializableType(); + if (!s_type) + continue; + Serialize::Data data = obj->serialize(); - std::vector<SQLQuery> create = this->sql->CreateTable(this->prefix + obj->serialize_name(), data); + std::vector<SQLQuery> create = this->sql->CreateTable(this->prefix + s_type->GetName(), data); for (unsigned i = 0; i < create.size(); ++i) this->RunBackground(create[i]); - SQLQuery insert = this->sql->BuildInsert(this->prefix + obj->serialize_name(), obj->id, data); + SQLQuery insert = this->sql->BuildInsert(this->prefix + s_type->GetName(), obj->id, data); this->RunBackground(insert, new ResultSQLSQLInterface(this, obj)); } } @@ -188,7 +192,9 @@ class DBSQL : public Module, public Pipe void OnSerializableDestruct(Serializable *obj) anope_override { - this->RunBackground("DELETE FROM `" + this->prefix + obj->serialize_name() + "` WHERE `id` = " + stringify(obj->id)); + SerializeType *s_type = obj->GetSerializableType(); + if (s_type) + this->RunBackground("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + stringify(obj->id)); } void OnSerializableUpdate(Serializable *obj) anope_override diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp index 4dca16d26..2aa3b73e6 100644 --- a/modules/database/db_sql_live.cpp +++ b/modules/database/db_sql_live.cpp @@ -95,18 +95,20 @@ class DBMySQL : public Module, public Pipe continue; obj->UpdateCache(); + SerializeType *s_type = obj->GetSerializableType(); + if (!s_type) + continue; + Serialize::Data data = obj->serialize(); - std::vector<SQLQuery> create = this->SQL->CreateTable(this->prefix + obj->serialize_name(), data); + std::vector<SQLQuery> create = this->SQL->CreateTable(this->prefix + s_type->GetName(), data); for (unsigned i = 0; i < create.size(); ++i) this->RunQueryResult(create[i]); - SQLResult res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + obj->serialize_name(), obj->id, data)); + SQLResult res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + s_type->GetName(), obj->id, data)); if (res.GetID() > 0) obj->id = res.GetID(); - SerializeType *stype = SerializeType::Find(obj->serialize_name()); - if (stype) - stype->objects.erase(obj->id); + s_type->objects.erase(obj->id); } } @@ -144,15 +146,16 @@ class DBMySQL : public Module, public Pipe { if (!this->CheckInit()) return; - this->RunQuery("DELETE FROM `" + this->prefix + obj->serialize_name() + "` WHERE `id` = " + stringify(obj->id)); - SerializeType *stype = SerializeType::Find(obj->serialize_name()); - if (stype) - stype->objects.erase(obj->id); + SerializeType *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); } void OnSerializePtrAssign(Serializable *obj) anope_override { - SerializeType *stype = SerializeType::Find(obj->serialize_name()); + SerializeType *stype = obj->GetSerializableType(); if (stype == NULL || !this->CheckInit() || stype->GetTimestamp() == Anope::CurTime) return; @@ -160,7 +163,7 @@ class DBMySQL : public Module, public Pipe return; obj->UpdateCache(); - SQLResult res = this->RunQueryResult("SELECT * FROM `" + this->prefix + obj->serialize_name() + "` WHERE `id` = " + stringify(obj->id)); + SQLResult res = this->RunQueryResult("SELECT * FROM `" + this->prefix + stype->GetName() + "` WHERE `id` = " + stringify(obj->id)); if (res.Rows() == 0) obj->destroy(); |