X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fextra%2Fm_pgsql.cpp;h=ab6908d171d0e200986fb3ee5d37eec0c13cff92;hb=ac7defcd3e52695dcf5e5150e9fe3e1624205e64;hp=735ca2f5aedced6f1d0b5d5535f2964c4381504a;hpb=1efedf9743fefe8269c2bcd292306de06a1b615e;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/extra/m_pgsql.cpp b/src/modules/extra/m_pgsql.cpp index 735ca2f5a..ab6908d17 100644 --- a/src/modules/extra/m_pgsql.cpp +++ b/src/modules/extra/m_pgsql.cpp @@ -1,16 +1,27 @@ -/* +------------------------------------+ - * | 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) 2006-2007, 2009 Dennis Friis + * Copyright (C) 2006-2007, 2009 Craig Edwards + * Copyright (C) 2008 Robin Burchell + * Copyright (C) 2008 Thomas Stagner + * Copyright (C) 2006 Oliver Lupton * - * 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 . */ + #include "inspircd.h" #include #include @@ -20,7 +31,6 @@ /* $ModDesc: PostgreSQL Service Provider module for all other m_sql* modules, uses v2 of the SQL API */ /* $CompileFlags: -Iexec("pg_config --includedir") eval("my $s = `pg_config --version`;$s =~ /^.*?(\d+)\.(\d+)\.(\d+).*?$/;my $v = hex(sprintf("0x%02x%02x%02x", $1, $2, $3));print "-DPGSQL_HAS_ESCAPECONN" if(($v >= 0x080104) || ($v >= 0x07030F && $v < 0x070400) || ($v >= 0x07040D && $v < 0x080000) || ($v >= 0x080008 && $v < 0x080100));") */ /* $LinkerFlags: -Lexec("pg_config --libdir") -lpq */ -/* $ModDep: m_sqlv2.h */ /* SQLConn rewritten by peavey to * use EventHandler instead of @@ -55,6 +65,12 @@ class ReconnectTimer : public Timer virtual void Tick(time_t TIME); }; +struct QueueItem +{ + SQLQuery* c; + std::string q; + QueueItem(SQLQuery* C, const std::string& Q) : c(C), q(Q) {} +}; /** PgSQLresult is a subclass of the mostly-pure-virtual class SQLresult. * All SQL providers must create their own subclass and define it's methods using that @@ -126,17 +142,17 @@ class SQLConn : public SQLProvider, public EventHandler { public: reference conf; /* The entry */ - std::deque queue; + std::deque queue; PGconn* sql; /* PgSQL database connection handle */ SQLstatus status; /* PgSQL database connection status */ - SQLQuery* qinprog; /* If there is currently a query in progress */ + QueueItem qinprog; /* If there is currently a query in progress */ SQLConn(Module* Creator, ConfigTag* tag) - : SQLProvider(Creator, "SQL/" + tag->getString("id")), conf(tag), sql(NULL), status(CWRITE), qinprog(NULL) + : SQLProvider(Creator, "SQL/" + tag->getString("id")), conf(tag), sql(NULL), status(CWRITE), qinprog(NULL, "") { if (!DoConnect()) { - ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: Could not connect to database " + tag->getString("id")); + ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: Could not connect to database " + tag->getString("id")); DelayReconnect(); } } @@ -151,14 +167,14 @@ class SQLConn : public SQLProvider, public EventHandler ~SQLConn() { SQLerror err(SQL_BAD_DBID); - if (qinprog) + if (qinprog.c) { - qinprog->OnError(err); - delete qinprog; + qinprog.c->OnError(err); + delete qinprog.c; } - for(std::deque::iterator i = queue.begin(); i != queue.end(); i++) + for(std::deque::iterator i = queue.begin(); i != queue.end(); i++) { - SQLQuery* q = *i; + SQLQuery* q = i->c; q->OnError(err); delete q; } @@ -262,7 +278,7 @@ class SQLConn : public SQLProvider, public EventHandler void DoConnectedPoll() { restart: - while (!qinprog && !queue.empty()) + while (qinprog.q.empty() && !queue.empty()) { /* There's no query currently in progress, and there's queries in the queue. */ DoQuery(queue.front()); @@ -275,7 +291,7 @@ restart: { /* Nothing happens here */ } - else if (qinprog) + else if (qinprog.c) { /* Fetch the result.. */ PGresult* result = PQgetResult(sql); @@ -301,18 +317,22 @@ restart: case PGRES_FATAL_ERROR: { SQLerror err(SQL_QREPLY_FAIL, PQresultErrorMessage(result)); - qinprog->OnError(err); + qinprog.c->OnError(err); break; } default: /* Other values are not errors */ - qinprog->OnResult(reply); + qinprog.c->OnResult(reply); } - delete qinprog; - qinprog = NULL; + delete qinprog.c; + qinprog = QueueItem(NULL, ""); goto restart; } + else + { + qinprog.q.clear(); + } } else { @@ -366,7 +386,20 @@ restart: } } - virtual std::string FormatQuery(const std::string& q, const ParamL& p) + void submit(SQLQuery *req, const std::string& q) + { + if (qinprog.q.empty()) + { + DoQuery(QueueItem(req,q)); + } + else + { + // wait your turn. + queue.push_back(QueueItem(req,q)); + } + } + + void submit(SQLQuery *req, const std::string& q, const ParamL& p) { std::string res; unsigned int param = 0; @@ -376,7 +409,6 @@ restart: res.push_back(q[i]); else { - // TODO numbered parameter support ('?1') if (param < p.size()) { std::string parm = p[param++]; @@ -393,10 +425,10 @@ restart: } } } - return res; + submit(req, res); } - std::string FormatQuery(const std::string& q, const ParamM& p) + void submit(SQLQuery *req, const std::string& q, const ParamM& p) { std::string res; for(std::string::size_type i = 0; i < q.length(); i++) @@ -407,7 +439,7 @@ restart: { std::string field; i++; - while (i < q.length() && isalpha(q[i])) + while (i < q.length() && isalnum(q[i])) field.push_back(q[i++]); i--; @@ -428,42 +460,29 @@ restart: } } } - return res; - } - - virtual void submit(SQLQuery *req) - { - if (qinprog) - { - // wait your turn. - queue.push_back(req); - } - else - { - DoQuery(req); - } + submit(req, res); } - void DoQuery(SQLQuery* req) + void DoQuery(const QueueItem& req) { if (status != WREAD && status != WWRITE) { // whoops, not connected... SQLerror err(SQL_BAD_CONN); - req->OnError(err); - delete req; + req.c->OnError(err); + delete req.c; return; } - if(PQsendQuery(sql, req->query.c_str())) + if(PQsendQuery(sql, req.q.c_str())) { qinprog = req; } else { SQLerror err(SQL_QSEND_FAIL, PQerrorMessage(sql)); - req->OnError(err); - delete req; + req.c->OnError(err); + delete req.c; } } @@ -479,13 +498,6 @@ restart: } }; -class DummyQuery : public SQLQuery -{ - public: - DummyQuery(Module* me) : SQLQuery(me, "") {} - void OnResult(SQLResult& result) {} -}; - class ModulePgSQL : public Module { public: @@ -558,16 +570,16 @@ class ModulePgSQL : public Module for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++) { SQLConn* conn = i->second; - if (conn->qinprog && conn->qinprog->creator == mod) + if (conn->qinprog.c && conn->qinprog.c->creator == mod) { - conn->qinprog->OnError(err); - delete conn->qinprog; - conn->qinprog = new DummyQuery(this); + conn->qinprog.c->OnError(err); + delete conn->qinprog.c; + conn->qinprog.c = NULL; } - std::deque::iterator j = conn->queue.begin(); + std::deque::iterator j = conn->queue.begin(); while (j != conn->queue.end()) { - SQLQuery* q = *j; + SQLQuery* q = j->c; if (q->creator == mod) { q->OnError(err);