/*
* InspIRCd -- Internet Relay Chat Daemon
*
- * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
- * Copyright (C) 2007-2009 Dennis Friis <peavey@inspircd.org>
- * Copyright (C) 2007, 2009 Craig Edwards <craigedwards@brainbox.cc>
- * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
+ * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
+ * Copyright (C) 2015 Daniel Vassdal <shutter@canternet.org>
+ * Copyright (C) 2013-2014, 2016-2019 Sadie Powell <sadie@witchery.services>
+ * Copyright (C) 2013-2014, 2016 Attila Molnar <attilamolnar@hush.com>
+ * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ * Copyright (C) 2012, 2016 Adam <Adam@anope.org>
+ * Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
+ * Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
+ * Copyright (C) 2007, 2009 Craig Edwards <brain@inspircd.org>
+ * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+/// $CompilerFlags: find_compiler_flags("sqlite3")
+/// $LinkerFlags: find_linker_flags("sqlite3" "-lsqlite3")
+
+/// $PackageInfo: require_system("arch") pkgconf sqlite
+/// $PackageInfo: require_system("centos") pkgconfig sqlite-devel
+/// $PackageInfo: require_system("darwin") pkg-config sqlite3
+/// $PackageInfo: require_system("debian") libsqlite3-dev pkg-config
+/// $PackageInfo: require_system("ubuntu") libsqlite3-dev pkg-config
#include "inspircd.h"
#include "modules/sql.h"
+#ifdef __GNUC__
+# pragma GCC diagnostic push
+#endif
+
// Fix warnings about the use of `long long` on C++03.
#if defined __clang__
# pragma clang diagnostic ignored "-Wc++11-long-long"
#include <sqlite3.h>
+#ifdef __GNUC__
+# pragma GCC diagnostic pop
+#endif
+
#ifdef _WIN32
# pragma comment(lib, "sqlite3.lib")
#endif
-/* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */
-/* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */
-
class SQLConn;
-typedef std::map<std::string, SQLConn*> ConnMap;
+typedef insp::flat_map<std::string, SQLConn*> ConnMap;
-class SQLite3Result : public SQLResult
+class SQLite3Result : public SQL::Result
{
public:
int currentrow;
int rows;
std::vector<std::string> columns;
- std::vector<SQLEntries> fieldlists;
+ std::vector<SQL::Row> fieldlists;
SQLite3Result() : currentrow(0), rows(0)
{
}
- int Rows()
+ int Rows() CXX11_OVERRIDE
{
return rows;
}
- bool GetRow(SQLEntries& result)
+ bool GetRow(SQL::Row& result) CXX11_OVERRIDE
{
if (currentrow < rows)
{
}
}
- void GetCols(std::vector<std::string>& result)
+ void GetCols(std::vector<std::string>& result) CXX11_OVERRIDE
{
result.assign(columns.begin(), columns.end());
}
+
+ bool HasColumn(const std::string& column, size_t& index) CXX11_OVERRIDE
+ {
+ for (size_t i = 0; i < columns.size(); ++i)
+ {
+ if (columns[i] == column)
+ {
+ index = i;
+ return true;
+ }
+ }
+ return false;
+ }
};
-class SQLConn : public SQLProvider
+class SQLConn : public SQL::Provider
{
sqlite3* conn;
reference<ConfigTag> config;
public:
- SQLConn(Module* Parent, ConfigTag* tag) : SQLProvider(Parent, "SQL/" + tag->getString("id")), config(tag)
+ SQLConn(Module* Parent, ConfigTag* tag)
+ : SQL::Provider(Parent, 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(MODNAME, LOG_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"));
}
}
}
}
- void Query(SQLQuery* query, const std::string& q)
+ void Query(SQL::Query* query, const std::string& q)
{
SQLite3Result res;
sqlite3_stmt *stmt;
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));
+ SQL::Error error(SQL::QSEND_FAIL, sqlite3_errmsg(conn));
query->OnError(error);
return;
}
{
const char* txt = (const char*)sqlite3_column_text(stmt, i);
if (txt)
- res.fieldlists[res.rows][i] = SQLEntry(txt);
+ res.fieldlists[res.rows][i] = SQL::Field(txt);
}
res.rows++;
}
}
else
{
- SQLerror error(SQL_QREPLY_FAIL, sqlite3_errmsg(conn));
+ SQL::Error error(SQL::QREPLY_FAIL, sqlite3_errmsg(conn));
query->OnError(error);
break;
}
sqlite3_finalize(stmt);
}
- void submit(SQLQuery* query, const std::string& q)
+ void Submit(SQL::Query* query, const std::string& q) CXX11_OVERRIDE
{
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Executing SQLite3 query: " + q);
Query(query, q);
delete query;
}
- void submit(SQLQuery* query, const std::string& q, const ParamL& p)
+ void Submit(SQL::Query* query, const std::string& q, const SQL::ParamList& p) CXX11_OVERRIDE
{
std::string res;
unsigned int param = 0;
}
}
}
- submit(query, res);
+ Submit(query, res);
}
- void submit(SQLQuery* query, const std::string& q, const ParamM& p)
+ void Submit(SQL::Query* query, const std::string& q, const SQL::ParamMap& p) CXX11_OVERRIDE
{
std::string res;
for(std::string::size_type i = 0; i < q.length(); i++)
field.push_back(q[i++]);
i--;
- ParamM::const_iterator it = p.find(field);
+ SQL::ParamMap::const_iterator it = p.find(field);
if (it != p.end())
{
char* escaped = sqlite3_mprintf("%q", it->second.c_str());
}
}
}
- submit(query, res);
+ Submit(query, res);
}
};
ConfigTagList tags = ServerInstance->Config->ConfTags("database");
for(ConfigIter i = tags.first; i != tags.second; i++)
{
- if (i->second->getString("module", "sqlite") != "sqlite")
+ if (!stdalgo::string::equalsci(i->second->getString("module"), "sqlite"))
continue;
SQLConn* conn = new SQLConn(this, i->second);
conns.insert(std::make_pair(i->second->getString("id"), conn));
Version GetVersion() CXX11_OVERRIDE
{
- return Version("sqlite3 provider", VF_VENDOR);
+ return Version("Provides the ability for SQL modules to query a SQLite 3 database.", VF_VENDOR);
}
};