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/>.
24 #include "modules/sql.h"
26 // Fix warnings about the use of `long long` on C++03.
28 # pragma clang diagnostic ignored "-Wc++11-long-long"
29 #elif defined __GNUC__
30 # pragma GCC diagnostic ignored "-Wlong-long"
36 # pragma comment(lib, "sqlite3.lib")
39 /* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */
40 /* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */
43 typedef std::map<std::string, SQLConn*> ConnMap;
45 class SQLite3Result : public SQLResult
50 std::vector<std::string> columns;
51 std::vector<SQLEntries> fieldlists;
53 SQLite3Result() : currentrow(0), rows(0)
62 bool GetRow(SQLEntries& result)
64 if (currentrow < rows)
66 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
77 void GetCols(std::vector<std::string>& result)
79 result.assign(columns.begin(), columns.end());
83 class SQLConn : public SQLProvider
86 reference<ConfigTag> config;
89 SQLConn(Module* Parent, ConfigTag* tag) : SQLProvider(Parent, "SQL/" + tag->getString("id")), config(tag)
91 std::string host = tag->getString("hostname");
92 if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
94 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
101 sqlite3_interrupt(conn);
105 void Query(SQLQuery* query, const std::string& q)
109 int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL);
110 if (err != SQLITE_OK)
112 SQLerror error(SQL_QSEND_FAIL, sqlite3_errmsg(conn));
113 query->OnError(error);
116 int cols = sqlite3_column_count(stmt);
117 res.columns.resize(cols);
118 for(int i=0; i < cols; i++)
120 res.columns[i] = sqlite3_column_name(stmt, i);
124 err = sqlite3_step(stmt);
125 if (err == SQLITE_ROW)
128 res.fieldlists.resize(res.rows + 1);
129 res.fieldlists[res.rows].resize(cols);
130 for(int i=0; i < cols; i++)
132 const char* txt = (const char*)sqlite3_column_text(stmt, i);
134 res.fieldlists[res.rows][i] = SQLEntry(txt);
138 else if (err == SQLITE_DONE)
140 query->OnResult(res);
145 SQLerror error(SQL_QREPLY_FAIL, sqlite3_errmsg(conn));
146 query->OnError(error);
150 sqlite3_finalize(stmt);
153 void submit(SQLQuery* query, const std::string& q)
159 void submit(SQLQuery* query, const std::string& q, const ParamL& p)
162 unsigned int param = 0;
163 for(std::string::size_type i = 0; i < q.length(); i++)
169 if (param < p.size())
171 char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
173 sqlite3_free(escaped);
180 void submit(SQLQuery* query, const std::string& q, const ParamM& p)
183 for(std::string::size_type i = 0; i < q.length(); i++)
191 while (i < q.length() && isalnum(q[i]))
192 field.push_back(q[i++]);
195 ParamM::const_iterator it = p.find(field);
198 char* escaped = sqlite3_mprintf("%q", it->second.c_str());
200 sqlite3_free(escaped);
208 class ModuleSQLite3 : public Module
220 for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++)
222 SQLConn* conn = i->second;
223 ServerInstance->Modules->DelService(*conn);
229 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
232 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
233 for(ConfigIter i = tags.first; i != tags.second; i++)
235 if (i->second->getString("module", "sqlite") != "sqlite")
237 SQLConn* conn = new SQLConn(this, i->second);
238 conns.insert(std::make_pair(i->second->getString("id"), conn));
239 ServerInstance->Modules->AddService(*conn);
243 Version GetVersion() CXX11_OVERRIDE
245 return Version("sqlite3 provider", VF_VENDOR);
249 MODULE_INIT(ModuleSQLite3)