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/>.
28 # pragma comment(lib, "sqlite3.lib")
31 /* $ModDesc: sqlite3 provider */
32 /* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */
33 /* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */
37 typedef std::map<std::string, SQLConn*> ConnMap;
39 class SQLite3Result : public SQLResult
44 std::vector<std::string> columns;
45 std::vector<SQLEntries> fieldlists;
47 SQLite3Result() : currentrow(0), rows(0)
60 virtual bool GetRow(SQLEntries& result)
62 if (currentrow < rows)
64 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
75 virtual void GetCols(std::vector<std::string>& result)
77 result.assign(columns.begin(), columns.end());
81 class SQLConn : public SQLProvider
85 reference<ConfigTag> config;
88 SQLConn(Module* Parent, ConfigTag* tag) : SQLProvider(Parent, "SQL/" + tag->getString("id")), config(tag)
90 std::string host = tag->getString("hostname");
91 if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
93 ServerInstance->Logs->Log("m_sqlite3",DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
102 sqlite3_interrupt(conn);
107 void Query(SQLQuery* query, const std::string& q)
111 int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL);
112 if (err != SQLITE_OK)
114 SQLerror error(SQL_QSEND_FAIL, sqlite3_errmsg(conn));
115 query->OnError(error);
118 int cols = sqlite3_column_count(stmt);
119 res.columns.resize(cols);
120 for(int i=0; i < cols; i++)
122 res.columns[i] = sqlite3_column_name(stmt, i);
126 err = sqlite3_step(stmt);
127 if (err == SQLITE_ROW)
130 res.fieldlists.resize(res.rows + 1);
131 res.fieldlists[res.rows].resize(cols);
132 for(int i=0; i < cols; i++)
134 const char* txt = (const char*)sqlite3_column_text(stmt, i);
136 res.fieldlists[res.rows][i] = SQLEntry(txt);
140 else if (err == SQLITE_DONE)
142 query->OnResult(res);
147 SQLerror error(SQL_QREPLY_FAIL, sqlite3_errmsg(conn));
148 query->OnError(error);
152 sqlite3_finalize(stmt);
155 virtual void submit(SQLQuery* query, const std::string& q)
161 virtual void submit(SQLQuery* query, const std::string& q, const ParamL& p)
164 unsigned int param = 0;
165 for(std::string::size_type i = 0; i < q.length(); i++)
171 if (param < p.size())
173 char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
175 sqlite3_free(escaped);
182 virtual void submit(SQLQuery* query, const std::string& q, const ParamM& p)
185 for(std::string::size_type i = 0; i < q.length(); i++)
193 while (i < q.length() && isalnum(q[i]))
194 field.push_back(q[i++]);
197 ParamM::const_iterator it = p.find(field);
200 char* escaped = sqlite3_mprintf("%q", it->second.c_str());
202 sqlite3_free(escaped);
210 class ModuleSQLite3 : public Module
224 Implementation eventlist[] = { I_OnRehash };
225 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
228 virtual ~ModuleSQLite3()
235 for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++)
237 SQLConn* conn = i->second;
238 ServerInstance->Modules->DelService(*conn);
247 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
248 for(ConfigIter i = tags.first; i != tags.second; i++)
250 if (i->second->getString("module", "sqlite") != "sqlite")
252 SQLConn* conn = new SQLConn(this, i->second);
253 conns.insert(std::make_pair(i->second->getString("id"), conn));
254 ServerInstance->Modules->AddService(*conn);
258 void OnRehash(User* user)
265 return Version("sqlite3 provider", VF_VENDOR);
269 MODULE_INIT(ModuleSQLite3)