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
|
/** A SQL exception, can be thrown at various points
*/
class SQLException : public ModuleException
{
public:
SQLException(const Anope::string &reason) : ModuleException(reason) { }
virtual ~SQLException() throw() { }
};
/** A result from a SQL query
*/
class SQLResult
{
protected:
/* Rows, column, item */
std::vector<std::map<Anope::string, Anope::string> > entries;
Anope::string query;
Anope::string error;
public:
SQLResult(const Anope::string &q, const Anope::string &err = "") : query(q), error(err) { }
inline operator bool() const { return this->error.empty(); }
inline const Anope::string &GetQuery() const { return this->query; }
inline const Anope::string &GetError() const { return this->error; }
int Rows() const { return this->entries.size(); }
const std::map<Anope::string, Anope::string> &Row(size_t index) const
{
try
{
return this->entries.at(index);
}
catch (const std::out_of_range &)
{
throw SQLException("Out of bounds access to SQLResult");
}
}
const Anope::string Get(size_t index, const Anope::string &col) const
{
const std::map<Anope::string, Anope::string> rows = this->Row(index);
std::map<Anope::string, Anope::string>::const_iterator it = rows.find(col);
if (it == rows.end())
throw SQLException("Unknown column name in SQLResult: " + col);
return it->second;
}
};
/* An interface used by modules to retrieve the results
*/
class SQLInterface
{
public:
Module *owner;
SQLInterface(Module *m) : owner(m) { }
virtual void OnResult(const SQLResult &r) { }
virtual void OnError(const SQLResult &r) { }
};
/** Class providing the SQL service, modules call this to execute queries
*/
class SQLProvider : public Service
{
public:
SQLProvider(Module *c, const Anope::string &n) : Service(c, n) { }
virtual void Run(SQLInterface *i, const Anope::string &query) = 0;
virtual SQLResult RunQuery(const Anope::string &query) = 0;
virtual const Anope::string Escape(const Anope::string &buf) { return buf; }
};
|