Anope IRC Services  Version 2.0
db_sql.cpp
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  * Based on the original code of Epona by Lara.
8  * Based on the original code of Services by Andy Church.
9  */
10 
11 #include "module.h"
12 #include "modules/sql.h"
13 
14 using namespace SQL;
15 
16 class SQLSQLInterface : public Interface
17 {
18  public:
20 
22  {
23  Log(LOG_DEBUG) << "SQL successfully executed query: " << r.finished_query;
24  }
25 
26  void OnError(const Result &r) anope_override
27  {
28  if (!r.GetQuery().query.empty())
29  Log(LOG_DEBUG) << "Error executing query " << r.finished_query << ": " << r.GetError();
30  else
31  Log(LOG_DEBUG) << "Error executing query: " << r.GetError();
32  }
33 };
34 
36 {
38 
39 public:
41 
43  {
45  if (r.GetID() > 0 && this->obj)
46  this->obj->id = r.GetID();
47  delete this;
48  }
49 
50  void OnError(const Result &r) anope_override
51  {
53  delete this;
54  }
55 };
56 
57 class DBSQL : public Module, public Pipe
58 {
62  bool import;
63 
64  std::set<Serializable *> updated_items;
67  bool loaded;
68  bool imported;
69 
70  void RunBackground(const Query &q, Interface *iface = NULL)
71  {
72  if (!this->sql)
73  {
74  static time_t last_warn = 0;
75  if (last_warn + 300 < Anope::CurTime)
76  {
77  last_warn = Anope::CurTime;
78  Log(this) << "db_sql: Unable to execute query, is SQL configured correctly?";
79  }
80  }
81  else if (!Anope::Quitting)
82  {
83  if (iface == NULL)
84  iface = &this->sqlinterface;
85  this->sql->Run(iface, q);
86  }
87  else
88  this->sql->RunQuery(q);
89  }
90 
91  public:
92  DBSQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE | VENDOR), sql("", ""), sqlinterface(this), shutting_down(false), loading_databases(false), loaded(false), imported(false)
93  {
94 
95 
96  if (ModuleManager::FindModule("db_sql_live") != NULL)
97  throw ModuleException("db_sql can not be loaded after db_sql_live");
98  }
99 
101  {
102  for (std::set<Serializable *>::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it)
103  {
104  Serializable *obj = *it;
105 
106  if (this->sql)
107  {
108  Data data;
109  obj->Serialize(data);
110 
111  if (obj->IsCached(data))
112  continue;
113 
114  obj->UpdateCache(data);
115 
116  /* If we didn't load these objects and we don't want to import just update the cache and continue */
117  if (!this->loaded && !this->imported && !this->import)
118  continue;
119 
120  Serialize::Type *s_type = obj->GetSerializableType();
121  if (!s_type)
122  continue;
123 
124  std::vector<Query> create = this->sql->CreateTable(this->prefix + s_type->GetName(), data);
125  for (unsigned i = 0; i < create.size(); ++i)
126  this->RunBackground(create[i]);
127 
128  Query insert = this->sql->BuildInsert(this->prefix + s_type->GetName(), obj->id, data);
129  if (this->imported)
130  this->RunBackground(insert, new ResultSQLSQLInterface(this, obj));
131  else
132  {
133  /* We are importing objects from another database module, so don't do asynchronous
134  * queries in case the core has to shut down, it will cut short the import
135  */
136  Result r = this->sql->RunQuery(insert);
137  if (r.GetID() > 0)
138  obj->id = r.GetID();
139  }
140  }
141  }
142 
143  this->updated_items.clear();
144  this->imported = true;
145  }
146 
148  {
149  Configuration::Block *block = conf->GetModule(this);
150  this->sql = ServiceReference<Provider>("SQL::Provider", block->Get<const Anope::string>("engine"));
151  this->prefix = block->Get<const Anope::string>("prefix", "anope_db_");
152  this->import = block->Get<bool>("import");
153  }
154 
156  {
157  this->shutting_down = true;
158  this->OnNotify();
159  }
160 
162  {
163  this->OnShutdown();
164  }
165 
167  {
168  if (!this->sql)
169  {
170  Log(this) << "Unable to load databases, is SQL configured correctly?";
171  return EVENT_CONTINUE;
172  }
173 
174  this->loading_databases = true;
175 
176  const std::vector<Anope::string> type_order = Serialize::Type::GetTypeOrder();
177  for (unsigned i = 0; i < type_order.size(); ++i)
178  {
179  Serialize::Type *sb = Serialize::Type::Find(type_order[i]);
180  this->OnSerializeTypeCreate(sb);
181  }
182 
183  this->loading_databases = false;
184  this->loaded = true;
185 
186  return EVENT_STOP;
187  }
188 
190  {
191  if (this->shutting_down || this->loading_databases)
192  return;
193  obj->UpdateTS();
194  this->updated_items.insert(obj);
195  this->Notify();
196  }
197 
199  {
200  if (this->shutting_down)
201  return;
202  Serialize::Type *s_type = obj->GetSerializableType();
203  if (s_type && obj->id > 0)
204  this->RunBackground("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + stringify(obj->id));
205  this->updated_items.erase(obj);
206  }
207 
209  {
210  if (this->shutting_down || obj->IsTSCached())
211  return;
212  obj->UpdateTS();
213  this->updated_items.insert(obj);
214  this->Notify();
215  }
216 
218  {
219  if (!this->loading_databases && !this->loaded)
220  return;
221 
222  Query query("SELECT * FROM `" + this->prefix + sb->GetName() + "`");
223  Result res = this->sql->RunQuery(query);
224 
225  for (int j = 0; j < res.Rows(); ++j)
226  {
227  Data data;
228 
229  const std::map<Anope::string, Anope::string> &row = res.Row(j);
230  for (std::map<Anope::string, Anope::string>::const_iterator rit = row.begin(), rit_end = row.end(); rit != rit_end; ++rit)
231  data[rit->first] << rit->second;
232 
233  Serializable *obj = sb->Unserialize(NULL, data);
234  try
235  {
236  if (obj)
237  obj->id = convertTo<unsigned int>(res.Get(j, "id"));
238  }
239  catch (const ConvertException &)
240  {
241  Log(this) << "Unable to convert id for object #" << j << " of type " << sb->GetName();
242  }
243 
244  if (obj)
245  {
246  /* The Unserialize operation is destructive so rebuild the data for UpdateCache.
247  * Also the old data may contain columns that we don't use, so we reserialize the
248  * object to know for sure our cache is consistent
249  */
250 
251  Data data2;
252  obj->Serialize(data2);
253  obj->UpdateCache(data2); /* We know this is the most up to date copy */
254  }
255  }
256  }
257 };
258 
260 
void OnRestart() anope_override
Definition: db_sql.cpp:161
static Module * FindModule(const Anope::string &name)
void OnSerializableDestruct(Serializable *obj) anope_override
Definition: db_sql.cpp:198
void UpdateCache(Serialize::Data &)
Definition: serialize.cpp:97
ResultSQLSQLInterface(Module *o, Serializable *ob)
Definition: db_sql.cpp:40
void OnResult(const Result &r) anope_override
Definition: db_sql.cpp:21
bool loaded
Definition: db_sql.cpp:67
bool shutting_down
Definition: db_sql.cpp:65
void OnNotify() anope_override
Definition: db_sql.cpp:100
Anope::string prefix
Definition: db_sql.cpp:61
void OnSerializableUpdate(Serializable *obj) anope_override
Definition: db_sql.cpp:208
CoreExport time_t CurTime
Definition: main.cpp:41
uint64_t id
Definition: serialize.h:83
Definition: sockets.h:454
std::set< Serializable * > updated_items
Definition: db_sql.cpp:64
ServiceReference< Provider > sql
Definition: db_sql.cpp:59
EventReturn OnLoadDatabase() anope_override
Definition: db_sql.cpp:166
CoreExport bool Quitting
Definition: main.cpp:34
Definition: sql.h:96
DBSQL(const Anope::string &modname, const Anope::string &creator)
Definition: db_sql.cpp:92
void OnSerializeTypeCreate(Serialize::Type *sb) anope_override
Definition: db_sql.cpp:217
virtual void Serialize(Serialize::Data &data) const =0
bool imported
Definition: db_sql.cpp:68
#define anope_override
Definition: services.h:56
Definition: sql.h:11
const unsigned int GetID() const
Definition: sql.h:151
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
Definition: sql.h:8
void OnReload(Configuration::Conf *conf) anope_override
Definition: db_sql.cpp:147
SQLSQLInterface(Module *o)
Definition: db_sql.cpp:19
Anope::string stringify(const T &x)
Definition: anope.h:710
void OnResult(const Result &r) anope_override
Definition: db_sql.cpp:42
bool IsCached(Serialize::Data &)
Definition: serialize.cpp:92
void OnError(const Result &r) anope_override
Definition: db_sql.cpp:50
void OnSerializableConstruct(Serializable *obj) anope_override
Definition: db_sql.cpp:189
Definition: logger.h:53
T Get(const Anope::string &tag)
Definition: config.h:44
void OnShutdown() anope_override
Definition: db_sql.cpp:155
void OnError(const Result &r) anope_override
Definition: db_sql.cpp:26
SQLSQLInterface sqlinterface
Definition: db_sql.cpp:60
Definition: db_sql.cpp:57
void RunBackground(const Query &q, Interface *iface=NULL)
Definition: db_sql.cpp:70
Serialize::Type * GetSerializableType() const
Definition: serialize.h:101
Type(const Anope::string &n, unserialize_func f, Module *owner=NULL)
bool loading_databases
Definition: db_sql.cpp:66
Reference< Serializable > obj
Definition: db_sql.cpp:37