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/>.
25 #include "modules/sql.h"
28 # pragma comment(lib, "sqlite3.lib")
31 /* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */
32 /* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */
36 typedef std::map<std::string, SQLConn*> ConnMap;
38 class SQLite3Result : public SQLResult
43 std::vector<std::string> columns;
44 std::vector<SQLEntries> fieldlists;
46 SQLite3Result() : currentrow(0), rows(0)
55 bool GetRow(SQLEntries& result)
57 if (currentrow < rows)
59 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
70 void GetCols(std::vector<std::string>& result)
72 result.assign(columns.begin(), columns.end());
76 class SQLConn : public SQLProvider
79 reference<ConfigTag> config;
82 SQLConn(Module* Parent, ConfigTag* tag) : SQLProvider(Parent, "SQL/" + tag->getString("id")), config(tag)
84 std::string host = tag->getString("hostname");
85 if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
87 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
94 sqlite3_interrupt(conn);
98 void Query(SQLQuery* query, const std::string& q)
102 int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL);
103 if (err != SQLITE_OK)
105 SQLerror error(SQL_QSEND_FAIL, sqlite3_errmsg(conn));
106 query->OnError(error);
109 int cols = sqlite3_column_count(stmt);
110 res.columns.resize(cols);
111 for(int i=0; i < cols; i++)
113 res.columns[i] = sqlite3_column_name(stmt, i);
117 err = sqlite3_step(stmt);
118 if (err == SQLITE_ROW)
121 res.fieldlists.resize(res.rows + 1);
122 res.fieldlists[res.rows].resize(cols);
123 for(int i=0; i < cols; i++)
125 const char* txt = (const char*)sqlite3_column_text(stmt, i);
127 res.fieldlists[res.rows][i] = SQLEntry(txt);
131 else if (err == SQLITE_DONE)
133 query->OnResult(res);
138 SQLerror error(SQL_QREPLY_FAIL, sqlite3_errmsg(conn));
139 query->OnError(error);
143 sqlite3_finalize(stmt);
146 void submit(SQLQuery* query, const std::string& q)
152 void submit(SQLQuery* query, const std::string& q, const ParamL& p)
155 unsigned int param = 0;
156 for(std::string::size_type i = 0; i < q.length(); i++)
162 if (param < p.size())
164 char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
166 sqlite3_free(escaped);
173 void submit(SQLQuery* query, const std::string& q, const ParamM& p)
176 for(std::string::size_type i = 0; i < q.length(); i++)
184 while (i < q.length() && isalnum(q[i]))
185 field.push_back(q[i++]);
188 ParamM::const_iterator it = p.find(field);
191 char* escaped = sqlite3_mprintf("%q", it->second.c_str());
193 sqlite3_free(escaped);
201 class ModuleSQLite3 : public Module
206 void init() CXX11_OVERRIDE
218 for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++)
220 SQLConn* conn = i->second;
221 ServerInstance->Modules->DelService(*conn);
230 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
231 for(ConfigIter i = tags.first; i != tags.second; i++)
233 if (i->second->getString("module", "sqlite") != "sqlite")
235 SQLConn* conn = new SQLConn(this, i->second);
236 conns.insert(std::make_pair(i->second->getString("id"), conn));
237 ServerInstance->Modules->AddService(*conn);
241 void OnRehash(User* user) CXX11_OVERRIDE
246 Version GetVersion() CXX11_OVERRIDE
248 return Version("sqlite3 provider", VF_VENDOR);
252 MODULE_INIT(ModuleSQLite3)