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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
* Copyright (C) 2007-2009 Dennis Friis <peavey@inspircd.org>
* Copyright (C) 2007, 2009 Craig Edwards <craigedwards@brainbox.cc>
* Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// $CompilerFlags: find_compiler_flags("sqlite3")
/// $LinkerFlags: find_linker_flags("sqlite3" "-lsqlite3")
/// $PackageInfo: require_system("centos") pkgconfig sqlite-devel
/// $PackageInfo: require_system("darwin") pkg-config sqlite3
/// $PackageInfo: require_system("debian") libsqlite3-dev pkg-config
/// $PackageInfo: require_system("ubuntu") libsqlite3-dev pkg-config
#include "inspircd.h"
#include "modules/sql.h"
#ifdef __GNUC__
# pragma GCC diagnostic push
#endif
// Fix warnings about the use of `long long` on C++03.
#if defined __clang__
# pragma clang diagnostic ignored "-Wc++11-long-long"
#elif defined __GNUC__
# pragma GCC diagnostic ignored "-Wlong-long"
#endif
#include <sqlite3.h>
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif
#ifdef _WIN32
# pragma comment(lib, "sqlite3.lib")
#endif
class SQLConn;
typedef insp::flat_map<std::string, SQLConn*> ConnMap;
class SQLite3Result : public SQL::Result
{
public:
int currentrow;
int rows;
std::vector<std::string> columns;
std::vector<SQL::Row> fieldlists;
SQLite3Result() : currentrow(0), rows(0)
{
}
int Rows() CXX11_OVERRIDE
{
return rows;
}
bool GetRow(SQL::Row& result) CXX11_OVERRIDE
{
if (currentrow < rows)
{
result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
currentrow++;
return true;
}
else
{
result.clear();
return false;
}
}
void GetCols(std::vector<std::string>& result) CXX11_OVERRIDE
{
result.assign(columns.begin(), columns.end());
}
bool HasColumn(const std::string& column, size_t& index) CXX11_OVERRIDE
{
for (size_t i = 0; i < columns.size(); ++i)
{
if (columns[i] == column)
{
index = i;
return true;
}
}
return false;
}
};
class SQLConn : public SQL::Provider
{
sqlite3* conn;
reference<ConfigTag> config;
public:
SQLConn(Module* Parent, ConfigTag* tag) : SQL::Provider(Parent, "SQL/" + tag->getString("id")), config(tag)
{
std::string host = tag->getString("hostname");
if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
{
// Even in case of an error conn must be closed
sqlite3_close(conn);
conn = NULL;
ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
}
}
~SQLConn()
{
if (conn)
{
sqlite3_interrupt(conn);
sqlite3_close(conn);
}
}
void Query(SQL::Query* query, const std::string& q)
{
SQLite3Result res;
sqlite3_stmt *stmt;
int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL);
if (err != SQLITE_OK)
{
SQL::Error error(SQL::QSEND_FAIL, sqlite3_errmsg(conn));
query->OnError(error);
return;
}
int cols = sqlite3_column_count(stmt);
res.columns.resize(cols);
for(int i=0; i < cols; i++)
{
res.columns[i] = sqlite3_column_name(stmt, i);
}
while (1)
{
err = sqlite3_step(stmt);
if (err == SQLITE_ROW)
{
// Add the row
res.fieldlists.resize(res.rows + 1);
res.fieldlists[res.rows].resize(cols);
for(int i=0; i < cols; i++)
{
const char* txt = (const char*)sqlite3_column_text(stmt, i);
if (txt)
res.fieldlists[res.rows][i] = SQL::Field(txt);
}
res.rows++;
}
else if (err == SQLITE_DONE)
{
query->OnResult(res);
break;
}
else
{
SQL::Error error(SQL::QREPLY_FAIL, sqlite3_errmsg(conn));
query->OnError(error);
break;
}
}
sqlite3_finalize(stmt);
}
void Submit(SQL::Query* query, const std::string& q) CXX11_OVERRIDE
{
ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Executing SQLite3 query: " + q);
Query(query, q);
delete query;
}
void Submit(SQL::Query* query, const std::string& q, const SQL::ParamList& p) CXX11_OVERRIDE
{
std::string res;
unsigned int param = 0;
for(std::string::size_type i = 0; i < q.length(); i++)
{
if (q[i] != '?')
res.push_back(q[i]);
else
{
if (param < p.size())
{
char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
res.append(escaped);
sqlite3_free(escaped);
}
}
}
Submit(query, res);
}
void Submit(SQL::Query* query, const std::string& q, const SQL::ParamMap& p) CXX11_OVERRIDE
{
std::string res;
for(std::string::size_type i = 0; i < q.length(); i++)
{
if (q[i] != '$')
res.push_back(q[i]);
else
{
std::string field;
i++;
while (i < q.length() && isalnum(q[i]))
field.push_back(q[i++]);
i--;
SQL::ParamMap::const_iterator it = p.find(field);
if (it != p.end())
{
char* escaped = sqlite3_mprintf("%q", it->second.c_str());
res.append(escaped);
sqlite3_free(escaped);
}
}
}
Submit(query, res);
}
};
class ModuleSQLite3 : public Module
{
ConnMap conns;
public:
~ModuleSQLite3()
{
ClearConns();
}
void ClearConns()
{
for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++)
{
SQLConn* conn = i->second;
ServerInstance->Modules->DelService(*conn);
delete conn;
}
conns.clear();
}
void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
{
ClearConns();
ConfigTagList tags = ServerInstance->Config->ConfTags("database");
for(ConfigIter i = tags.first; i != tags.second; i++)
{
if (!stdalgo::string::equalsci(i->second->getString("module"), "sqlite"))
continue;
SQLConn* conn = new SQLConn(this, i->second);
conns.insert(std::make_pair(i->second->getString("id"), conn));
ServerInstance->Modules->AddService(*conn);
}
}
Version GetVersion() CXX11_OVERRIDE
{
return Version("Provides SQLite3 support", VF_VENDOR);
}
};
MODULE_INIT(ModuleSQLite3)
|