X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fextra%2Fm_sqlite3.cpp;h=ac7146e386b3a3e980dff0992753b99e0e6e1555;hb=3b78613576364c4ac6a4e4af43a2eea056c8dd3f;hp=cc54b90bf195df44b2f5684725a7645b5857846d;hpb=4e0997924d4052dede401fa1c0a1a91ae81c9aa3;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/extra/m_sqlite3.cpp b/src/modules/extra/m_sqlite3.cpp index cc54b90bf..ac7146e38 100644 --- a/src/modules/extra/m_sqlite3.cpp +++ b/src/modules/extra/m_sqlite3.cpp @@ -1,27 +1,49 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd: (C) 2002-2010 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits + * Copyright (C) 2009-2010 Daniel De Graaf + * Copyright (C) 2007-2009 Dennis Friis + * Copyright (C) 2007, 2009 Craig Edwards + * Copyright (C) 2008 Pippijn van Steenhoven * - * This program is free but copyrighted software; see - * the file COPYING for details. + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. * - * --------------------------------------------------- + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ +/// $CompilerFlags: find_compiler_flags("sqlite3") +/// $LinkerFlags: find_linker_flags("sqlite3" "-lsqlite3") + +/// $PackageInfo: require_system("centos") pkgconfig sqlite-devel +/// $PackageInfo: require_system("darwin") pkg-config sqlite3 +/// $PackageInfo: require_system("ubuntu") libsqlite3-dev pkg-config + #include "inspircd.h" +#include "modules/sql.h" + +// Fix warnings about the use of `long long` on C++03. +#if defined __clang__ +# pragma clang diagnostic ignored "-Wc++11-long-long" +#elif defined __GNUC__ +# pragma GCC diagnostic ignored "-Wlong-long" +#endif + #include -#include "sql.h" -/* $ModDesc: sqlite3 provider */ -/* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */ -/* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */ -/* $NoPedantic */ +#ifdef _WIN32 +# pragma comment(lib, "sqlite3.lib") +#endif class SQLConn; -typedef std::map > ConnMap; +typedef insp::flat_map ConnMap; class SQLite3Result : public SQLResult { @@ -35,16 +57,12 @@ class SQLite3Result : public SQLResult { } - ~SQLite3Result() - { - } - - virtual int Rows() + int Rows() { return rows; } - virtual bool GetRow(SQLEntries& result) + bool GetRow(SQLEntries& result) { if (currentrow < rows) { @@ -58,40 +76,45 @@ class SQLite3Result : public SQLResult return false; } } - virtual void GetCols(std::vector& result) + + void GetCols(std::vector& result) { result.assign(columns.begin(), columns.end()); } }; -class SQLConn : public refcountbase +class SQLConn : public SQLProvider { - private: sqlite3* conn; reference config; public: - SQLConn(ConfigTag* tag) : config(tag) + SQLConn(Module* Parent, ConfigTag* tag) : SQLProvider(Parent, "SQL/" + tag->getString("id")), config(tag) { std::string host = tag->getString("hostname"); if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK) { - ServerInstance->Logs->Log("m_sqlite3",DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id")); + // Even in case of an error conn must be closed + sqlite3_close(conn); conn = NULL; + ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id")); } } ~SQLConn() { - sqlite3_interrupt(conn); - sqlite3_close(conn); + if (conn) + { + sqlite3_interrupt(conn); + sqlite3_close(conn); + } } - void Query(SQLQuery* query) + void Query(SQLQuery* query, const std::string& q) { SQLite3Result res; sqlite3_stmt *stmt; - int err = sqlite3_prepare_v2(conn, query->query.c_str(), query->query.length(), &stmt, NULL); + int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL); if (err != SQLITE_OK) { SQLerror error(SQL_QSEND_FAIL, sqlite3_errmsg(conn)); @@ -134,16 +157,14 @@ class SQLConn : public refcountbase } sqlite3_finalize(stmt); } -}; -class SQLiteProvider : public SQLProvider -{ - public: - ConnMap hosts; - - SQLiteProvider(Module* Parent) : SQLProvider(Parent, "SQL/SQLite") {} + void submit(SQLQuery* query, const std::string& q) + { + Query(query, q); + delete query; + } - std::string FormatQuery(const std::string& q, const ParamL& p) + void submit(SQLQuery* query, const std::string& q, const ParamL& p) { std::string res; unsigned int param = 0; @@ -153,7 +174,6 @@ class SQLiteProvider : public SQLProvider res.push_back(q[i]); else { - // TODO numbered parameter support ('?1') if (param < p.size()) { char* escaped = sqlite3_mprintf("%q", p[param++].c_str()); @@ -162,10 +182,10 @@ class SQLiteProvider : public SQLProvider } } } - return res; + submit(query, res); } - std::string FormatQuery(const std::string& q, const ParamM& p) + void submit(SQLQuery* query, const std::string& q, const ParamM& p) { std::string res; for(std::string::size_type i = 0; i < q.length(); i++) @@ -176,7 +196,7 @@ class SQLiteProvider : public SQLProvider { std::string field; i++; - while (i < q.length() && isalpha(q[i])) + while (i < q.length() && isalnum(q[i])) field.push_back(q[i++]); i--; @@ -189,66 +209,46 @@ class SQLiteProvider : public SQLProvider } } } - return res; - } - - void submit(SQLQuery* query) - { - ConnMap::iterator iter = hosts.find(query->dbid); - if (iter == hosts.end()) - { - SQLerror err(SQL_BAD_DBID); - query->OnError(err); - } - else - { - iter->second->Query(query); - } - delete query; + submit(query, res); } }; class ModuleSQLite3 : public Module { - private: - SQLiteProvider sqlserv; + ConnMap conns; public: - ModuleSQLite3() - : sqlserv(this) - { - } - - void init() + ~ModuleSQLite3() { - ServerInstance->Modules->AddService(sqlserv); - - ReadConf(); - - Implementation eventlist[] = { I_OnRehash }; - ServerInstance->Modules->Attach(eventlist, this, 1); + ClearConns(); } - virtual ~ModuleSQLite3() + void ClearConns() { + for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++) + { + SQLConn* conn = i->second; + ServerInstance->Modules->DelService(*conn); + delete conn; + } + conns.clear(); } - void ReadConf() + void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE { - sqlserv.hosts.clear(); + ClearConns(); ConfigTagList tags = ServerInstance->Config->ConfTags("database"); for(ConfigIter i = tags.first; i != tags.second; i++) { - sqlserv.hosts.insert(std::make_pair(i->second->getString("id"), new SQLConn(i->second))); + if (i->second->getString("module", "sqlite") != "sqlite") + continue; + SQLConn* conn = new SQLConn(this, i->second); + conns.insert(std::make_pair(i->second->getString("id"), conn)); + ServerInstance->Modules->AddService(*conn); } } - void OnRehash(User* user) - { - ReadConf(); - } - - Version GetVersion() + Version GetVersion() CXX11_OVERRIDE { return Version("sqlite3 provider", VF_VENDOR); }