1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
* (C) 2003-2012 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
#include "module.h"
#include "../extra/sql.h"
class SQLSQLInterface : public SQLInterface
{
public:
SQLSQLInterface(Module *o) : SQLInterface(o) { }
void OnResult(const SQLResult &r)
{
Log(LOG_DEBUG) << "SQL successfully executed query: " << r.finished_query;
}
void OnError(const SQLResult &r)
{
if (!r.GetQuery().query.empty())
Log(LOG_DEBUG) << "Error executing query " << r.finished_query << ": " << r.GetError();
else
Log(LOG_DEBUG) << "Error executing query: " << r.GetError();
}
};
class DBSQL : public Module
{
service_reference<SQLProvider> sql;
SQLSQLInterface sqlinterface;
void RunBackground(const SQLQuery &q)
{
if (!quitting)
this->sql->Run(&sqlinterface, q);
else
this->sql->RunQuery(q);
}
void DropTable(const Anope::string &table)
{
SQLQuery query("DROP TABLE `" + table + "`");
this->RunBackground(query);
}
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 Serializable::serialized_data &newd)
{
for (Serializable::serialized_data::const_iterator it = newd.begin(), it_end = newd.end(); it != it_end; ++it)
{
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)";
else if (it->second.getMax() > 0)
query_text += "varchar(" + stringify(it->second.getMax()) + ")";
else
query_text += "text";
this->RunBackground(SQLQuery(query_text));
}
}
void Insert(const Anope::string &table, const Serializable::serialized_data &data)
{
Anope::string query_text = "INSERT INTO `" + table + "` (";
for (Serializable::serialized_data::const_iterator it = data.begin(), it_end = data.end(); it != it_end; ++it)
query_text += "`" + it->first + "`,";
query_text.erase(query_text.end() - 1);
query_text += ") VALUES (";
for (Serializable::serialized_data::const_iterator it = data.begin(), it_end = data.end(); it != it_end; ++it)
query_text += "@" + it->first + "@,";
query_text.erase(query_text.end() - 1);
query_text += ")";
SQLQuery query(query_text);
for (Serializable::serialized_data::const_iterator it = data.begin(), it_end = data.end(); it != it_end; ++it)
query.setValue(it->first, it->second.astr());
this->RunBackground(query);
}
public:
DBSQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE), sql("", ""), sqlinterface(this)
{
this->SetAuthor("Anope");
Implementation i[] = { I_OnReload, I_OnSaveDatabase, I_OnLoadDatabase };
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
this->OnReload();
}
void OnReload()
{
ConfigReader config;
Anope::string engine = config.ReadValue("db_sql", "engine", "", 0);
this->sql = service_reference<SQLProvider>("SQLProvider", engine);
}
EventReturn OnSaveDatabase()
{
if (!this->sql)
{
Log() << "db_sql: Unable to save databases, is SQL configured correctly?";
return EVENT_CONTINUE;
}
const std::list<Serializable *> &items = Serializable::GetItems();
if (items.empty())
return EVENT_CONTINUE;
this->DropAll();
std::map<Anope::string, std::set<Anope::string> > table_layout;
for (std::list<Serializable *>::const_iterator it = items.begin(), it_end = items.end(); it != it_end; ++it)
{
Serializable *base = *it;
Serializable::serialized_data data = base->serialize();
if (data.empty())
continue;
std::set<Anope::string> &layout = table_layout[base->serialize_name()];
if (layout.empty())
{
this->RunBackground(this->sql->CreateTable(base->serialize_name(), data));
for (Serializable::serialized_data::iterator it2 = data.begin(), it2_end = data.end(); it2 != it2_end; ++it2)
layout.insert(it2->first);
}
else
this->AlterTable(base->serialize_name(), layout, data);
this->Insert(base->serialize_name(), data);
}
return EVENT_CONTINUE;
}
EventReturn OnLoadDatabase()
{
if (!this->sql)
{
Log() << "db_sql: Unable to load databases, is SQL configured correctly?";
return EVENT_CONTINUE;
}
const std::vector<Anope::string> type_order = SerializeType::GetTypeOrder();
for (unsigned i = 0; i < type_order.size(); ++i)
{
SerializeType *sb = SerializeType::Find(type_order[i]);
SQLQuery query("SELECT * FROM `" + sb->GetName() + "`");
SQLResult res = this->sql->RunQuery(query);
for (int j = 0; j < res.Rows(); ++j)
{
Serializable::serialized_data data;
const std::map<Anope::string, Anope::string> &row = res.Row(j);
for (std::map<Anope::string, Anope::string>::const_iterator rit = row.begin(), rit_end = row.end(); rit != rit_end; ++rit)
data[rit->first] << rit->second;
sb->Create(data);
}
}
return EVENT_STOP;
}
};
MODULE_INIT(DBSQL)
|