2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2009 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2007, 2009 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /// $CompilerFlags: find_compiler_flags("sqlite3")
23 /// $LinkerFlags: find_linker_flags("sqlite3" "-lsqlite3")
25 /// $PackageInfo: require_system("arch") pkgconf sqlite
26 /// $PackageInfo: require_system("centos") pkgconfig sqlite-devel
27 /// $PackageInfo: require_system("darwin") pkg-config sqlite3
28 /// $PackageInfo: require_system("debian") libsqlite3-dev pkg-config
29 /// $PackageInfo: require_system("ubuntu") libsqlite3-dev pkg-config
32 #include "modules/sql.h"
35 # pragma GCC diagnostic push
38 // Fix warnings about the use of `long long` on C++03.
40 # pragma clang diagnostic ignored "-Wc++11-long-long"
41 #elif defined __GNUC__
42 # pragma GCC diagnostic ignored "-Wlong-long"
48 # pragma GCC diagnostic pop
52 # pragma comment(lib, "sqlite3.lib")
56 typedef insp::flat_map<std::string, SQLConn*> ConnMap;
58 class SQLite3Result : public SQL::Result
63 std::vector<std::string> columns;
64 std::vector<SQL::Row> fieldlists;
66 SQLite3Result() : currentrow(0), rows(0)
70 int Rows() CXX11_OVERRIDE
75 bool GetRow(SQL::Row& result) CXX11_OVERRIDE
77 if (currentrow < rows)
79 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
90 void GetCols(std::vector<std::string>& result) CXX11_OVERRIDE
92 result.assign(columns.begin(), columns.end());
95 bool HasColumn(const std::string& column, size_t& index) CXX11_OVERRIDE
97 for (size_t i = 0; i < columns.size(); ++i)
99 if (columns[i] == column)
109 class SQLConn : public SQL::Provider
112 reference<ConfigTag> config;
115 SQLConn(Module* Parent, ConfigTag* tag) : SQL::Provider(Parent, "SQL/" + tag->getString("id")), config(tag)
117 std::string host = tag->getString("hostname");
118 if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
120 // Even in case of an error conn must be closed
123 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
131 sqlite3_interrupt(conn);
136 void Query(SQL::Query* query, const std::string& q)
140 int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL);
141 if (err != SQLITE_OK)
143 SQL::Error error(SQL::QSEND_FAIL, sqlite3_errmsg(conn));
144 query->OnError(error);
147 int cols = sqlite3_column_count(stmt);
148 res.columns.resize(cols);
149 for(int i=0; i < cols; i++)
151 res.columns[i] = sqlite3_column_name(stmt, i);
155 err = sqlite3_step(stmt);
156 if (err == SQLITE_ROW)
159 res.fieldlists.resize(res.rows + 1);
160 res.fieldlists[res.rows].resize(cols);
161 for(int i=0; i < cols; i++)
163 const char* txt = (const char*)sqlite3_column_text(stmt, i);
165 res.fieldlists[res.rows][i] = SQL::Field(txt);
169 else if (err == SQLITE_DONE)
171 query->OnResult(res);
176 SQL::Error error(SQL::QREPLY_FAIL, sqlite3_errmsg(conn));
177 query->OnError(error);
181 sqlite3_finalize(stmt);
184 void Submit(SQL::Query* query, const std::string& q) CXX11_OVERRIDE
186 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Executing SQLite3 query: " + q);
191 void Submit(SQL::Query* query, const std::string& q, const SQL::ParamList& p) CXX11_OVERRIDE
194 unsigned int param = 0;
195 for(std::string::size_type i = 0; i < q.length(); i++)
201 if (param < p.size())
203 char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
205 sqlite3_free(escaped);
212 void Submit(SQL::Query* query, const std::string& q, const SQL::ParamMap& p) CXX11_OVERRIDE
215 for(std::string::size_type i = 0; i < q.length(); i++)
223 while (i < q.length() && isalnum(q[i]))
224 field.push_back(q[i++]);
227 SQL::ParamMap::const_iterator it = p.find(field);
230 char* escaped = sqlite3_mprintf("%q", it->second.c_str());
232 sqlite3_free(escaped);
240 class ModuleSQLite3 : public Module
252 for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++)
254 SQLConn* conn = i->second;
255 ServerInstance->Modules->DelService(*conn);
261 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
264 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
265 for(ConfigIter i = tags.first; i != tags.second; i++)
267 if (!stdalgo::string::equalsci(i->second->getString("module"), "sqlite"))
269 SQLConn* conn = new SQLConn(this, i->second);
270 conns.insert(std::make_pair(i->second->getString("id"), conn));
271 ServerInstance->Modules->AddService(*conn);
275 Version GetVersion() CXX11_OVERRIDE
277 return Version("Provides SQLite3 support", VF_VENDOR);
281 MODULE_INIT(ModuleSQLite3)