summaryrefslogtreecommitdiff
path: root/modules/database/db_sql.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/database/db_sql.cpp')
-rw-r--r--modules/database/db_sql.cpp38
1 files changed, 9 insertions, 29 deletions
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<SQLQuery> 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<Serializable> 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<SQLQuery> 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));
}
}