summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/database/db_sql.cpp31
-rw-r--r--modules/extra/m_mysql.cpp7
-rw-r--r--modules/extra/m_sqlite.cpp7
-rw-r--r--modules/extra/sql.h2
4 files changed, 40 insertions, 7 deletions
diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp
index d411d8b6d..8b2b0e26d 100644
--- a/modules/database/db_sql.cpp
+++ b/modules/database/db_sql.cpp
@@ -49,13 +49,26 @@ class DBSQL : public Module
this->RunBackground(query);
}
- void AlterTable(const Anope::string &table, const SerializableBase::serialized_data &oldd, const SerializableBase::serialized_data &newd)
+ void DropAll()
+ {
+ SQLResult r = this->sql->RunQuery(this->sql->GetTables());
+ for (int i = 0; i < r.Rows(); ++i)
+ {
+ const std::map<Anope::string, Anope::string> &map = r.Row(i);
+ for (std::map<Anope::string, Anope::string>::const_iterator it = map.begin(); it != map.end(); ++it)
+ this->DropTable(it->second);
+ }
+ }
+
+ void AlterTable(const Anope::string &table, std::set<Anope::string> &data, const SerializableBase::serialized_data &newd)
{
for (SerializableBase::serialized_data::const_iterator it = newd.begin(), it_end = newd.end(); it != it_end; ++it)
{
- if (oldd.count(it->first) > 0)
+ if (data.count(it->first) > 0)
continue;
+ data.insert(it->first);
+
Anope::string query_text = "ALTER TABLE `" + table + "` ADD `" + it->first + "` ";
if (it->second.getType() == Serialize::DT_INT)
query_text += "int(11)";
@@ -116,7 +129,9 @@ class DBSQL : public Module
if (serialized_items == NULL)
return EVENT_CONTINUE;
- std::map<Anope::string, SerializableBase::serialized_data> table_layout;
+ this->DropAll();
+
+ std::map<Anope::string, std::set<Anope::string> > table_layout;
for (std::list<SerializableBase *>::iterator it = serialized_items->begin(), it_end = serialized_items->end(); it != it_end; ++it)
{
SerializableBase *base = *it;
@@ -125,14 +140,16 @@ class DBSQL : public Module
if (data.empty())
continue;
- if (table_layout.count(base->serialize_name()) == 0)
+ std::set<Anope::string> &layout = table_layout[base->serialize_name()];
+ if (layout.empty())
{
- this->DropTable(base->serialize_name());
this->RunBackground(this->sql->CreateTable(base->serialize_name(), data));
+
+ for (SerializableBase::serialized_data::iterator it2 = data.begin(), it2_end = data.end(); it2 != it2_end; ++it2)
+ layout.insert(it2->first);
}
else
- this->AlterTable(base->serialize_name(), table_layout[base->serialize_name()], data);
- table_layout[base->serialize_name()] = data;
+ this->AlterTable(base->serialize_name(), layout, data);
this->Insert(base->serialize_name(), data);
}
diff --git a/modules/extra/m_mysql.cpp b/modules/extra/m_mysql.cpp
index 876d2ae4e..61e6c39c9 100644
--- a/modules/extra/m_mysql.cpp
+++ b/modules/extra/m_mysql.cpp
@@ -125,6 +125,8 @@ class MySQLService : public SQLProvider
SQLQuery CreateTable(const Anope::string &table, const SerializableBase::serialized_data &data);
+ SQLQuery GetTables();
+
void Connect();
bool CheckConnection();
@@ -374,6 +376,11 @@ SQLQuery MySQLService::CreateTable(const Anope::string &table, const Serializabl
return SQLQuery(query_text);
}
+SQLQuery MySQLService::GetTables()
+{
+ return SQLQuery("SHOW TABLES");
+}
+
void MySQLService::Connect()
{
this->sql = mysql_init(this->sql);
diff --git a/modules/extra/m_sqlite.cpp b/modules/extra/m_sqlite.cpp
index 58eab1c32..2411f843f 100644
--- a/modules/extra/m_sqlite.cpp
+++ b/modules/extra/m_sqlite.cpp
@@ -46,6 +46,8 @@ class SQLiteService : public SQLProvider
SQLQuery CreateTable(const Anope::string &table, const SerializableBase::serialized_data &data);
+ SQLQuery GetTables();
+
Anope::string BuildQuery(const SQLQuery &q);
};
@@ -213,6 +215,11 @@ SQLQuery SQLiteService::CreateTable(const Anope::string &table, const Serializab
return SQLQuery(query_text);
}
+SQLQuery SQLiteService::GetTables()
+{
+ return SQLQuery("SELECT name FROM sqlite_master WHERE type='table'");
+}
+
Anope::string SQLiteService::Escape(const Anope::string &query)
{
char *e = sqlite3_mprintf("%q", query.c_str());
diff --git a/modules/extra/sql.h b/modules/extra/sql.h
index 85b3c291a..21b311014 100644
--- a/modules/extra/sql.h
+++ b/modules/extra/sql.h
@@ -118,5 +118,7 @@ class SQLProvider : public Service<Base>
virtual SQLResult RunQuery(const SQLQuery &query) = 0;
virtual SQLQuery CreateTable(const Anope::string &table, const SerializableBase::serialized_data &data) = 0;
+
+ virtual SQLQuery GetTables() = 0;
};