]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_pgsql.cpp
Change empty string assignments to .clear() or remove them entirely
[user/henk/code/inspircd.git] / src / modules / extra / m_pgsql.cpp
index 85d092241d2074627e42d00f0ae9beb60684724b..71f494dd9012f1314a4f529dd4b6813e72a04614 100644 (file)
@@ -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 <danieldg@inspircd.org>
+ *   Copyright (C) 2006-2007, 2009 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006-2007, 2009 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
+ *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
  *
- * 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 <http://www.gnu.org/licenses/>.
  */
 
+
 #include "inspircd.h"
 #include <cstdlib>
 #include <sstream>
@@ -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
@@ -124,19 +140,16 @@ class PgSQLresult : public SQLResult
  */
 class SQLConn : public SQLProvider, public EventHandler
 {
- private:
+ public:
        reference<ConfigTag> conf;      /* The <database> entry */
-       std::deque<SQLQuery*> queue;
+       std::deque<QueueItem> queue;
        PGconn*                 sql;            /* PgSQL database connection handle */
        SQLstatus               status;         /* PgSQL database connection status */
-       SQLQuery*               qinprog;        /* If there is currently a query in progress */
-       time_t                  idle;           /* Time we last heard from the database */
+       QueueItem               qinprog;        /* If there is currently a query in progress */
 
- public:
        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, "")
        {
-               idle = ServerInstance->Time();
                if (!DoConnect())
                {
                        ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: Could not connect to database " + tag->getString("id")); 
@@ -154,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<SQLQuery*>::iterator i = queue.begin(); i != queue.end(); i++)
+               for(std::deque<QueueItem>::iterator i = queue.begin(); i != queue.end(); i++)
                {
-                       SQLQuery* q = *i;
+                       SQLQuery* q = i->c;
                        q->OnError(err);
                        delete q;
                }
@@ -265,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());
@@ -274,16 +287,11 @@ restart:
 
                if (PQconsumeInput(sql))
                {
-                       /* We just read stuff from the server, that counts as it being alive
-                        * so update the idle-since time :p
-                        */
-                       idle = ServerInstance->Time();
-
                        if (PQisBusy(sql))
                        {
                                /* Nothing happens here */
                        }
-                       else if (qinprog)
+                       else if (qinprog.c)
                        {
                                /* Fetch the result.. */
                                PGresult* result = PQgetResult(sql);
@@ -309,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
                {
@@ -374,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;
@@ -384,7 +409,6 @@ restart:
                                res.push_back(q[i]);
                        else
                        {
-                               // TODO numbered parameter support ('?1')
                                if (param < p.size())
                                {
                                        std::string parm = p[param++];
@@ -401,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++)
@@ -415,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--;
 
@@ -436,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;
                }
        }
 
@@ -555,7 +566,30 @@ class ModulePgSQL : public Module
 
        void OnUnloadModule(Module* mod)
        {
-               // TODO cancel queries that will have a bad vtable
+               SQLerror err(SQL_BAD_DBID);
+               for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
+               {
+                       SQLConn* conn = i->second;
+                       if (conn->qinprog.c && conn->qinprog.c->creator == mod)
+                       {
+                               conn->qinprog.c->OnError(err);
+                               delete conn->qinprog.c;
+                               conn->qinprog.c = NULL;
+                       }
+                       std::deque<QueueItem>::iterator j = conn->queue.begin();
+                       while (j != conn->queue.end())
+                       {
+                               SQLQuery* q = j->c;
+                               if (q->creator == mod)
+                               {
+                                       q->OnError(err);
+                                       delete q;
+                                       j = conn->queue.erase(j);
+                               }
+                               else
+                                       j++;
+                       }
+               }
        }
 
        Version GetVersion()