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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* Anope IRC Services
*
* Copyright (C) 2010-2016 Anope Team <team@anope.org>
*
* This file is part of Anope. Anope is free software; you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see see <http://www.gnu.org/licenses/>.
*/
namespace SQL
{
/** A SQL exception, can be thrown at various points
*/
class Exception : public ModuleException
{
public:
Exception(const Anope::string &reason) : ModuleException(reason) { }
virtual ~Exception() throw() { }
};
/** A SQL query
*/
struct QueryData
{
Anope::string data;
bool escape;
bool null;
};
struct Query
{
Anope::string query;
std::map<Anope::string, QueryData> parameters;
Query() { }
Query(const Anope::string &q) : query(q) { }
Query& operator=(const Anope::string &q)
{
this->query = q;
this->parameters.clear();
return *this;
}
bool operator==(const Query &other) const
{
return this->query == other.query;
}
inline bool operator!=(const Query &other) const
{
return !(*this == other);
}
template<typename T> void SetValue(const Anope::string &key, const T& value, bool escape = true)
{
try
{
Anope::string string_value = stringify(value);
this->parameters[key].data = string_value;
this->parameters[key].escape = escape;
}
catch (const ConvertException &ex) { }
}
void SetNull(const Anope::string &key)
{
QueryData &qd = this->parameters[key];
qd.data = "";
qd.escape = false;
qd.null = true;
}
Anope::string Unsafe() const
{
Anope::string q = query;
for (auto it = parameters.begin(); it != parameters.end(); ++it)
q = q.replace_all_cs("@" + it->first + "@", it->second.data);
return q;
}
};
/** A result from a SQL query
*/
class Result
{
public:
struct Value
{
bool null = false;
Anope::string value;
};
protected:
std::vector<Anope::string> columns;
// row, column
std::vector<std::vector<Value>> values;
Query query;
Anope::string error;
public:
unsigned int id;
Anope::string finished_query;
Result() : id(0) { }
Result(unsigned int i, const Query &q, const Anope::string &fq, const Anope::string &err = "") : query(q), error(err), id(i), finished_query(fq) { }
inline operator bool() const { return this->error.empty(); }
inline unsigned int GetID() const { return this->id; }
inline const Query &GetQuery() const { return this->query; }
inline const Anope::string &GetError() const { return this->error; }
int Rows() const
{
return this->values.size();
}
const std::vector<Value> &Row(size_t index) const
{
try
{
return this->values.at(index);
}
catch (const std::out_of_range &)
{
throw Exception("Out of bounds access to SQLResult");
}
}
const Value &GetValue(size_t index, const Anope::string &col) const
{
const std::vector<Value> &v = this->Row(index);
auto it = std::find(this->columns.begin(), this->columns.end(), col);
if (it == this->columns.end())
throw Exception("Unknown column name in SQLResult: " + col);
unsigned int col_idx = it - this->columns.begin();
try
{
return v[col_idx];
}
catch (const std::out_of_range &)
{
throw Exception("Out of bounds access to SQLResult");
}
}
const Anope::string &Get(size_t index, const Anope::string &col) const
{
const Value &value = GetValue(index, col);
return value.value;
}
bool IsNull(size_t index, const Anope::string &col) const
{
const Value &value = GetValue(index, col);
return value.null;
}
};
/* An interface used by modules to retrieve the results
*/
class Interface
{
public:
Module *owner;
Interface(Module *m) : owner(m) { }
virtual ~Interface() { }
virtual void OnResult(const Result &r) anope_abstract;
virtual void OnError(const Result &r) anope_abstract;
};
/** Class providing the SQL service, modules call this to execute queries
*/
class Provider : public Service
{
public:
static constexpr const char *NAME = "sql";
Provider(Module *c, const Anope::string &n) : Service(c, NAME, n) { }
virtual void Run(Interface *i, const Query &query) anope_abstract;
virtual Result RunQuery(const Query &query) anope_abstract;
virtual std::vector<Query> InitSchema(const Anope::string &prefix) anope_abstract;
virtual std::vector<Query> Replace(const Anope::string &table, const Query &, const std::set<Anope::string> &) anope_abstract;
virtual std::vector<Query> CreateTable(const Anope::string &prefix, Serialize::TypeBase *) anope_abstract;
virtual std::vector<Query> AlterTable(const Anope::string &, Serialize::TypeBase *, Serialize::FieldBase *) anope_abstract;
virtual std::vector<Query> CreateIndex(const Anope::string &table, const Anope::string &field) anope_abstract;
virtual Query SelectFind(const Anope::string &table, const Anope::string &field) anope_abstract;
virtual Query BeginTransaction() anope_abstract;
virtual Query Commit() anope_abstract;
virtual Serialize::ID GetID(const Anope::string &prefix, const Anope::string &type) anope_abstract;
virtual Query GetTables(const Anope::string &prefix) anope_abstract;
};
}
|