Anope IRC Services  Version 2.0
sql.h
Go to the documentation of this file.
1 /*
2  * (C) 2003-2014 Anope Team
3  * Contact us at team@anope.org
4  *
5  * Please read COPYING and README for further details.
6  */
7 
8 namespace SQL
9 {
10 
11  class Data : public Serialize::Data
12  {
13  public:
14  typedef std::map<Anope::string, std::stringstream *> Map;
15  Map data;
16  std::map<Anope::string, Type> types;
17 
19  {
20  Clear();
21  }
22 
23  std::iostream& operator[](const Anope::string &key) anope_override
24  {
25  std::stringstream *&ss = data[key];
26  if (!ss)
27  ss = new std::stringstream();
28  return *ss;
29  }
30 
31  std::set<Anope::string> KeySet() const anope_override
32  {
33  std::set<Anope::string> keys;
34  for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it)
35  keys.insert(it->first);
36  return keys;
37  }
38 
39  size_t Hash() const anope_override
40  {
41  size_t hash = 0;
42  for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it)
43  if (!it->second->str().empty())
44  hash ^= Anope::hash_cs()(it->second->str());
45  return hash;
46  }
47 
48  std::map<Anope::string, std::iostream *> GetData() const
49  {
50  std::map<Anope::string, std::iostream *> d;
51  for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it)
52  d[it->first] = it->second;
53  return d;
54  }
55 
56  void Clear()
57  {
58  for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it)
59  delete it->second;
60  this->data.clear();
61  }
62 
64  {
65  this->types[key] = t;
66  }
67 
69  {
70  std::map<Anope::string, Type>::const_iterator it = this->types.find(key);
71  if (it != this->types.end())
72  return it->second;
73  return DT_TEXT;
74  }
75  };
76 
79  class Exception : public ModuleException
80  {
81  public:
82  Exception(const Anope::string &reason) : ModuleException(reason) { }
83 
84  virtual ~Exception() throw() { }
85  };
86 
90  struct QueryData
91  {
93  bool escape;
94  };
95 
96  struct Query
97  {
99  std::map<Anope::string, QueryData> parameters;
100 
101  Query() { }
102  Query(const Anope::string &q) : query(q) { }
103 
105  {
106  this->query = q;
107  this->parameters.clear();
108  return *this;
109  }
110 
111  bool operator==(const Query &other) const
112  {
113  return this->query == other.query;
114  }
115 
116  inline bool operator!=(const Query &other) const
117  {
118  return !(*this == other);
119  }
120 
121  template<typename T> void SetValue(const Anope::string &key, const T& value, bool escape = true)
122  {
123  try
124  {
125  Anope::string string_value = stringify(value);
126  this->parameters[key].data = string_value;
127  this->parameters[key].escape = escape;
128  }
129  catch (const ConvertException &ex) { }
130  }
131  };
132 
135  class Result
136  {
137  protected:
138  /* Rows, column, item */
139  std::vector<std::map<Anope::string, Anope::string> > entries;
142  public:
143  unsigned int id;
145 
146  Result() : id(0) { }
147  Result(unsigned int i, const Query &q, const Anope::string &fq, const Anope::string &err = "") : query(q), error(err), id(i), finished_query(fq) { }
148 
149  inline operator bool() const { return this->error.empty(); }
150 
151  inline const unsigned int GetID() const { return this->id; }
152  inline const Query &GetQuery() const { return this->query; }
153  inline const Anope::string &GetError() const { return this->error; }
154 
155  int Rows() const { return this->entries.size(); }
156 
157  const std::map<Anope::string, Anope::string> &Row(size_t index) const
158  {
159  try
160  {
161  return this->entries.at(index);
162  }
163  catch (const std::out_of_range &)
164  {
165  throw Exception("Out of bounds access to SQLResult");
166  }
167  }
168 
169  const Anope::string Get(size_t index, const Anope::string &col) const
170  {
171  const std::map<Anope::string, Anope::string> rows = this->Row(index);
172 
173  std::map<Anope::string, Anope::string>::const_iterator it = rows.find(col);
174  if (it == rows.end())
175  throw Exception("Unknown column name in SQLResult: " + col);
176 
177  return it->second;
178  }
179  };
180 
181  /* An interface used by modules to retrieve the results
182  */
183  class Interface
184  {
185  public:
187 
188  Interface(Module *m) : owner(m) { }
189  virtual ~Interface() { }
190 
191  virtual void OnResult(const Result &r) = 0;
192  virtual void OnError(const Result &r) = 0;
193  };
194 
197  class Provider : public Service
198  {
199  public:
200  Provider(Module *c, const Anope::string &n) : Service(c, "SQL::Provider", n) { }
201 
202  virtual void Run(Interface *i, const Query &query) = 0;
203 
204  virtual Result RunQuery(const Query &query) = 0;
205 
206  virtual std::vector<Query> CreateTable(const Anope::string &table, const Data &data) = 0;
207 
208  virtual Query BuildInsert(const Anope::string &table, unsigned int id, Data &data) = 0;
209 
210  virtual Query GetTables(const Anope::string &prefix) = 0;
211 
212  virtual Anope::string FromUnixtime(time_t) = 0;
213  };
214 
215 }
216 
virtual ~Interface()
Definition: sql.h:189
Module * owner
Definition: sql.h:186
Map data
Definition: sql.h:15
Provider(Module *c, const Anope::string &n)
Definition: sql.h:200
void Clear()
Definition: sql.h:56
virtual void OnError(const Result &r)=0
void clear()
Definition: anope.h:187
Result()
Definition: sql.h:146
const std::map< Anope::string, Anope::string > & Row(size_t index) const
Definition: sql.h:157
Anope::string query
Definition: sql.h:98
std::map< Anope::string, QueryData > parameters
Definition: sql.h:99
bool operator==(const Query &other) const
Definition: sql.h:111
Query query
Definition: sql.h:140
size_t Hash() const anope_override
Definition: sql.h:39
const Anope::string & GetError() const
Definition: sql.h:153
Query(const Anope::string &q)
Definition: sql.h:102
virtual std::vector< Query > CreateTable(const Anope::string &table, const Data &data)=0
virtual Query BuildInsert(const Anope::string &table, unsigned int id, Data &data)=0
std::iostream & operator[](const Anope::string &key) anope_override
Definition: sql.h:23
Anope::string finished_query
Definition: sql.h:144
void SetValue(const Anope::string &key, const T &value, bool escape=true)
Definition: sql.h:121
virtual ~Exception()
Definition: sql.h:84
~Data()
Definition: sql.h:18
Anope::string data
Definition: sql.h:92
std::vector< std::map< Anope::string, Anope::string > > entries
Definition: sql.h:139
std::map< Anope::string, std::stringstream * > Map
Definition: sql.h:14
Interface(Module *m)
Definition: sql.h:188
std::map< Anope::string, Type > types
Definition: sql.h:16
Definition: sql.h:96
virtual void OnResult(const Result &r)=0
unsigned int id
Definition: sql.h:143
std::map< Anope::string, std::iostream * > GetData() const
Definition: sql.h:48
virtual Query GetTables(const Anope::string &prefix)=0
virtual void Run(Interface *i, const Query &query)=0
#define anope_override
Definition: services.h:56
Definition: sql.h:11
bool empty() const
Definition: anope.h:126
Type GetType(const Anope::string &key) const anope_override
Definition: sql.h:68
const unsigned int GetID() const
Definition: sql.h:151
std::set< Anope::string > KeySet() const anope_override
Definition: sql.h:31
Definition: sql.h:8
Exception(const Anope::string &reason)
Definition: sql.h:82
Anope::string stringify(const T &x)
Definition: anope.h:710
bool operator!=(const Query &other) const
Definition: sql.h:116
virtual Result RunQuery(const Query &query)=0
bool escape
Definition: sql.h:93
const Query & GetQuery() const
Definition: sql.h:152
int Rows() const
Definition: sql.h:155
Anope::string error
Definition: sql.h:141
Query & operator=(const Anope::string &q)
Definition: sql.h:104
virtual Anope::string FromUnixtime(time_t)=0
void SetType(const Anope::string &key, Type t) anope_override
Definition: sql.h:63
Result(unsigned int i, const Query &q, const Anope::string &fq, const Anope::string &err="")
Definition: sql.h:147
const Anope::string Get(size_t index, const Anope::string &col) const
Definition: sql.h:169
Query()
Definition: sql.h:101