2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2015 Daniel Vassdal <shutter@canternet.org>
6 * Copyright (C) 2013-2014, 2016-2019 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2013-2014, 2016 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2012, 2016 Adam <Adam@anope.org>
10 * Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
11 * Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
12 * Copyright (C) 2007, 2009 Craig Edwards <brain@inspircd.org>
13 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /// $CompilerFlags: find_compiler_flags("sqlite3")
29 /// $LinkerFlags: find_linker_flags("sqlite3" "-lsqlite3")
31 /// $PackageInfo: require_system("arch") pkgconf sqlite
32 /// $PackageInfo: require_system("centos") pkgconfig sqlite-devel
33 /// $PackageInfo: require_system("darwin") pkg-config sqlite3
34 /// $PackageInfo: require_system("debian") libsqlite3-dev pkg-config
35 /// $PackageInfo: require_system("ubuntu") libsqlite3-dev pkg-config
38 #include "modules/sql.h"
41 # pragma GCC diagnostic push
44 // Fix warnings about the use of `long long` on C++03.
46 # pragma clang diagnostic ignored "-Wc++11-long-long"
47 #elif defined __GNUC__
48 # pragma GCC diagnostic ignored "-Wlong-long"
54 # pragma GCC diagnostic pop
58 # pragma comment(lib, "sqlite3.lib")
62 typedef insp::flat_map<std::string, SQLConn*> ConnMap;
64 class SQLite3Result : public SQL::Result
69 std::vector<std::string> columns;
70 std::vector<SQL::Row> fieldlists;
72 SQLite3Result() : currentrow(0), rows(0)
76 int Rows() CXX11_OVERRIDE
81 bool GetRow(SQL::Row& result) CXX11_OVERRIDE
83 if (currentrow < rows)
85 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
96 void GetCols(std::vector<std::string>& result) CXX11_OVERRIDE
98 result.assign(columns.begin(), columns.end());
101 bool HasColumn(const std::string& column, size_t& index) CXX11_OVERRIDE
103 for (size_t i = 0; i < columns.size(); ++i)
105 if (columns[i] == column)
115 class SQLConn : public SQL::Provider
118 reference<ConfigTag> config;
121 SQLConn(Module* Parent, ConfigTag* tag)
122 : SQL::Provider(Parent, tag->getString("id"))
125 std::string host = tag->getString("hostname");
126 if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
128 // Even in case of an error conn must be closed
131 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
139 sqlite3_interrupt(conn);
144 void Query(SQL::Query* query, const std::string& q)
148 int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL);
149 if (err != SQLITE_OK)
151 SQL::Error error(SQL::QSEND_FAIL, sqlite3_errmsg(conn));
152 query->OnError(error);
155 int cols = sqlite3_column_count(stmt);
156 res.columns.resize(cols);
157 for(int i=0; i < cols; i++)
159 res.columns[i] = sqlite3_column_name(stmt, i);
163 err = sqlite3_step(stmt);
164 if (err == SQLITE_ROW)
167 res.fieldlists.resize(res.rows + 1);
168 res.fieldlists[res.rows].resize(cols);
169 for(int i=0; i < cols; i++)
171 const char* txt = (const char*)sqlite3_column_text(stmt, i);
173 res.fieldlists[res.rows][i] = SQL::Field(txt);
177 else if (err == SQLITE_DONE)
179 query->OnResult(res);
184 SQL::Error error(SQL::QREPLY_FAIL, sqlite3_errmsg(conn));
185 query->OnError(error);
189 sqlite3_finalize(stmt);
192 void Submit(SQL::Query* query, const std::string& q) CXX11_OVERRIDE
194 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Executing SQLite3 query: " + q);
199 void Submit(SQL::Query* query, const std::string& q, const SQL::ParamList& p) CXX11_OVERRIDE
202 unsigned int param = 0;
203 for(std::string::size_type i = 0; i < q.length(); i++)
209 if (param < p.size())
211 char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
213 sqlite3_free(escaped);
220 void Submit(SQL::Query* query, const std::string& q, const SQL::ParamMap& p) CXX11_OVERRIDE
223 for(std::string::size_type i = 0; i < q.length(); i++)
231 while (i < q.length() && isalnum(q[i]))
232 field.push_back(q[i++]);
235 SQL::ParamMap::const_iterator it = p.find(field);
238 char* escaped = sqlite3_mprintf("%q", it->second.c_str());
240 sqlite3_free(escaped);
248 class ModuleSQLite3 : public Module
260 for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++)
262 SQLConn* conn = i->second;
263 ServerInstance->Modules->DelService(*conn);
269 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
272 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
273 for(ConfigIter i = tags.first; i != tags.second; i++)
275 if (!stdalgo::string::equalsci(i->second->getString("module"), "sqlite"))
277 SQLConn* conn = new SQLConn(this, i->second);
278 conns.insert(std::make_pair(i->second->getString("id"), conn));
279 ServerInstance->Modules->AddService(*conn);
283 Version GetVersion() CXX11_OVERRIDE
285 return Version("Provides the ability for SQL modules to query a SQLite 3 database.", VF_VENDOR);
289 MODULE_INIT(ModuleSQLite3)