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
|
#include "module.h"
#include "../extra/sql.h"
class SQLCache : public Timer
{
typedef std::map<Anope::string, time_t, ci::less> cache_map;
cache_map cache;
public:
SQLCache() : Timer(300, Anope::CurTime, true) { }
bool Check(const Anope::string &item)
{
cache_map::iterator it = this->cache.find(item);
if (it != this->cache.end() && Anope::CurTime - it->second < 5)
return true;
this->cache[item] = Anope::CurTime;
return false;
}
void Tick(time_t)
{
for (cache_map::iterator it = this->cache.begin(), next_it; it != this->cache.end(); it = next_it)
{
next_it = it;
++next_it;
if (Anope::CurTime - it->second > 5)
this->cache.erase(it);
}
}
};
class MySQLLiveModule : public Module
{
service_reference<SQLProvider, Base> SQL;
SQLCache chan_cache, nick_cache, core_cache;
SQLResult RunQuery(const SQLQuery &query)
{
if (!this->SQL)
throw SQLException("Unable to locate SQL reference, is db_sql loaded and configured correctly?");
SQLResult res = SQL->RunQuery(query);
if (!res.GetError().empty())
throw SQLException(res.GetError());
return res;
}
public:
MySQLLiveModule(const Anope::string &modname, const Anope::string &creator) :
Module(modname, creator, DATABASE), SQL("")
{
this->OnReload();
Implementation i[] = { I_OnReload, I_OnFindChan, I_OnFindNick, I_OnFindCore, I_OnShutdown };
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
}
void OnReload()
{
ConfigReader config;
Anope::string engine = config.ReadValue("db_sql", "engine", "", 0);
this->SQL = engine;
}
void OnShutdown()
{
Implementation i[] = { I_OnFindChan, I_OnFindNick, I_OnFindCore };
for (size_t j = 0; j < 3; ++j)
ModuleManager::Detach(i[j], this);
}
void OnFindChan(const Anope::string &chname)
{
if (chan_cache.Check(chname))
return;
try
{
SQLQuery query("SELECT * FROM `ChannelInfo` WHERE `name` = @name@");
query.setValue("name", chname);
SQLResult res = this->RunQuery(query);
if (res.Rows() == 0)
{
delete cs_findchan(chname);
return;
}
SerializeType *sb = SerializeType::Find("ChannelInfo");
if (sb == NULL)
return;
Serializable::serialized_data data;
const std::map<Anope::string, Anope::string> &row = res.Row(0);
for (std::map<Anope::string, Anope::string>::const_iterator rit = row.begin(), rit_end = row.end(); rit != rit_end; ++rit)
data[rit->first] << rit->second;
sb->Create(data);
}
catch (const SQLException &ex)
{
Log(LOG_DEBUG) << "OnFindChan: " << ex.GetReason();
}
}
void OnFindNick(const Anope::string &nick)
{
if (nick_cache.Check(nick))
return;
try
{
SQLQuery query("SELECT * FROM `NickAlias` WHERE `nick` = @nick@");
query.setValue("nick", nick);
SQLResult res = this->RunQuery(query);
if (res.Rows() == 0)
{
delete findnick(nick);
return;
}
SerializeType *sb = SerializeType::Find("NickAlias");
if (sb == NULL)
return;
Serializable::serialized_data data;
const std::map<Anope::string, Anope::string> &row = res.Row(0);
for (std::map<Anope::string, Anope::string>::const_iterator rit = row.begin(), rit_end = row.end(); rit != rit_end; ++rit)
data[rit->first] << rit->second;
sb->Create(data);
}
catch (const SQLException &ex)
{
Log(LOG_DEBUG) << "OnFindNick: " << ex.GetReason();
}
}
void OnFindCore(const Anope::string &nick)
{
if (core_cache.Check(nick))
return;
try
{
SQLQuery query("SELECT * FROM `NickCore` WHERE `display` = @display@");
query.setValue("display", nick);
SQLResult res = this->RunQuery(query);
if (res.Rows() == 0)
{
delete findcore(nick);
return;
}
SerializeType *sb = SerializeType::Find("NickCore");
if (sb == NULL)
return;
Serializable::serialized_data data;
const std::map<Anope::string, Anope::string> &row = res.Row(0);
for (std::map<Anope::string, Anope::string>::const_iterator rit = row.begin(), rit_end = row.end(); rit != rit_end; ++rit)
data[rit->first] << rit->second;
sb->Create(data);
}
catch (const SQLException &ex)
{
Log(LOG_DEBUG) << "OnFindCore: " << ex.GetReason();
}
}
};
MODULE_INIT(MySQLLiveModule)
|