From 42e652cae79c0387e690f18e00712963c2dfec22 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 29 Apr 2012 19:24:37 -0400 Subject: Pull table schemas from SQL on startup so we can alter the schemas if we need to, fixed sqlite to work again --- modules/database/db_sql.cpp | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) (limited to 'modules/database/db_sql.cpp') diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp index da891e587..9702bd400 100644 --- a/modules/database/db_sql.cpp +++ b/modules/database/db_sql.cpp @@ -80,33 +80,6 @@ class DBSQL : public Module, public Pipe this->sql->RunQuery(q); } - SQLQuery BuildInsert(const Anope::string &table, unsigned int id, const Serialize::Data &data) - { - if (this->sql) - { - std::vector create_queries = this->sql->CreateTable(table, data); - for (unsigned i = 0; i < create_queries.size(); ++i) - this->RunBackground(create_queries[i]); - } - - Anope::string query_text = "INSERT INTO `" + table + "` (`id`"; - for (Serialize::Data::const_iterator it = data.begin(), it_end = data.end(); it != it_end; ++it) - query_text += ",`" + it->first + "`"; - query_text += ") VALUES (" + stringify(id); - for (Serialize::Data::const_iterator it = data.begin(), it_end = data.end(); it != it_end; ++it) - query_text += ",@" + it->first + "@"; - query_text += ") ON DUPLICATE KEY UPDATE "; - for (Serialize::Data::const_iterator it = data.begin(), it_end = data.end(); it != it_end; ++it) - query_text += "`" + it->first + "`=VALUES(`" + it->first + "`),"; - query_text.erase(query_text.end() - 1); - - SQLQuery query(query_text); - for (Serialize::Data::const_iterator it = data.begin(), it_end = data.end(); it != it_end; ++it) - query.setValue(it->first, it->second.astr()); - - return query; - } - public: DBSQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE), sql("", ""), sqlinterface(this) { @@ -124,13 +97,20 @@ class DBSQL : public Module, public Pipe { dynamic_reference obj = *it; - if (obj) + if (obj && this->sql) { if (obj->IsCached()) continue; obj->UpdateCache(); - SQLQuery insert = this->BuildInsert(this->prefix + obj->serialize_name(), obj->id, obj->serialize()); + const Serialize::Data &data = obj->serialize(); + + std::vector create = this->sql->CreateTable(this->prefix + obj->serialize_name(), 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); this->RunBackground(insert, new ResultSQLSQLInterface(this, obj)); } } -- cgit