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