]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_pgsql.cpp
Make a ./configure --system to support system-wide installation of inspircd
[user/henk/code/inspircd.git] / src / modules / extra / m_pgsql.cpp
index b417fc0192b039185240dfcb263725789e0ed933..629193ea1613e1b845ae074822bd45c8a39816a7 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
  * See: http://wiki.inspircd.org/Credits
  *
  * This program is free but copyrighted software; see
@@ -44,10 +44,10 @@ typedef std::map<std::string, SQLConn*> ConnMap;
  */
 enum SQLstatus { CREAD, CWRITE, WREAD, WWRITE, RREAD, RWRITE };
 
-unsigned long count(const char * const str, char a)
+static unsigned long count(const char * const str, char a)
 {
        unsigned long n = 0;
-       for (const char *p = reinterpret_cast<const char *>(str); *p; ++p)
+       for (const char *p = str; *p; ++p)
        {
                if (*p == '?')
                        ++n;
@@ -91,10 +91,9 @@ std::string SQLhost::GetDSN()
 class ReconnectTimer : public Timer
 {
  private:
-       Module* mod;
+       Module* const mod;
  public:
-       ReconnectTimer(InspIRCd* SI, Module* m)
-       : Timer(5, SI->Time(), false), mod(m)
+       ReconnectTimer(Module* m) : Timer(5, ServerInstance->Time(), false), mod(m)
        {
        }
        virtual void Tick(time_t TIME);
@@ -301,7 +300,6 @@ public:
 class SQLConn : public EventHandler
 {
  private:
-       InspIRCd*               ServerInstance;
        SQLhost                 confhost;       /* The <database> entry */
        Module*                 us;                     /* Pointer to the SQL provider itself */
        PGconn*                 sql;            /* PgSQL database connection handle */
@@ -311,10 +309,10 @@ class SQLConn : public EventHandler
        time_t                  idle;           /* Time we last heard from the database */
 
  public:
-       SQLConn(InspIRCd* SI, Module* self, const SQLhost& hi)
-       : EventHandler(), ServerInstance(SI), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
+       SQLConn(Module* self, const SQLhost& hi)
+       : EventHandler(), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
        {
-               idle = this->ServerInstance->Time();
+               idle = ServerInstance->Time();
                if(!DoConnect())
                {
                        ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: Could not connect to database with id: " + ConvToStr(hi.id));
@@ -367,7 +365,7 @@ class SQLConn : public EventHandler
                if(this->fd <= -1)
                        return false;
 
-               if (!this->ServerInstance->SE->AddFd(this))
+               if (!ServerInstance->SE->AddFd(this, FD_WANT_NO_WRITE | FD_WANT_NO_READ))
                {
                        ServerInstance->Logs->Log("m_pgsql",DEBUG, "BUG: Couldn't add pgsql socket to socket engine");
                        return false;
@@ -382,15 +380,17 @@ class SQLConn : public EventHandler
                switch(PQconnectPoll(sql))
                {
                        case PGRES_POLLING_WRITING:
-                               ServerInstance->SE->WantWrite(this);
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_WRITE | FD_WANT_NO_READ);
                                status = CWRITE;
                                return true;
                        case PGRES_POLLING_READING:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = CREAD;
                                return true;
                        case PGRES_POLLING_FAILED:
                                return false;
                        case PGRES_POLLING_OK:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = WWRITE;
                                return DoConnectedPoll();
                        default:
@@ -403,8 +403,8 @@ class SQLConn : public EventHandler
                if(!qinprog && queue.totalsize())
                {
                        /* There's no query currently in progress, and there's queries in the queue. */
-                       SQLrequest& query = queue.front();
-                       DoQuery(query);
+                       SQLrequest* query = queue.front();
+                       DoQuery(*query);
                }
 
                if(PQconsumeInput(sql))
@@ -412,7 +412,7 @@ class SQLConn : public EventHandler
                        /* We just read stuff from the server, that counts as it being alive
                         * so update the idle-since time :p
                         */
-                       idle = this->ServerInstance->Time();
+                       idle = ServerInstance->Time();
 
                        if (PQisBusy(sql))
                        {
@@ -421,10 +421,10 @@ class SQLConn : public EventHandler
                        else if (qinprog)
                        {
                                /* Grab the request we're processing */
-                               SQLrequest& query = queue.front();
+                               SQLrequest* query = queue.front();
 
                                /* Get a pointer to the module we're about to return the result to */
-                               Module* to = query.GetSource();
+                               Module* to = query->source;
 
                                /* Fetch the result.. */
                                PGresult* result = PQgetResult(sql);
@@ -444,10 +444,10 @@ class SQLConn : public EventHandler
                                if(to)
                                {
                                        /* ..and the result */
-                                       PgSQLresult reply(us, to, query.id, result);
+                                       PgSQLresult reply(us, to, query->id, result);
 
                                        /* Fix by brain, make sure the original query gets sent back in the reply */
-                                       reply.query = query.query.q;
+                                       reply.query = query->query.q;
 
                                        switch(PQresultStatus(result))
                                        {
@@ -496,15 +496,17 @@ class SQLConn : public EventHandler
                switch(PQresetPoll(sql))
                {
                        case PGRES_POLLING_WRITING:
-                               ServerInstance->SE->WantWrite(this);
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_WRITE | FD_WANT_NO_READ);
                                status = CWRITE;
                                return DoPoll();
                        case PGRES_POLLING_READING:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = CREAD;
                                return true;
                        case PGRES_POLLING_FAILED:
                                return false;
                        case PGRES_POLLING_OK:
+                               ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = WWRITE;
                                return DoConnectedPoll();
                        default:
@@ -707,13 +709,13 @@ class SQLConn : public EventHandler
 
        SQLerror Query(const SQLrequest &req)
        {
-               queue.push(req);
+               queue.push(new SQLrequest(req));
 
                if(!qinprog && queue.totalsize())
                {
                        /* There's no query currently in progress, and there's queries in the queue. */
-                       SQLrequest& query = queue.front();
-                       return DoQuery(query);
+                       SQLrequest* query = queue.front();
+                       return DoQuery(*query);
                }
                else
                {
@@ -733,11 +735,11 @@ class SQLConn : public EventHandler
 
        void Close()
        {
-               if (!this->ServerInstance->SE->DelFd(this))
+               if (!ServerInstance->SE->DelFd(this))
                {
                        if (sql && PQstatus(sql) == CONNECTION_BAD)
                        {
-                               this->ServerInstance->SE->DelFd(this, true);
+                               ServerInstance->SE->DelFd(this, true);
                        }
                        else
                        {
@@ -761,27 +763,20 @@ class ModulePgSQL : public Module
        unsigned long currid;
        char* sqlsuccess;
        ReconnectTimer* retimer;
-
+       ServiceProvider sqlserv;
  public:
-       ModulePgSQL(InspIRCd* Me)
-       : Module(Me), currid(0)
+       ModulePgSQL()
+       : currid(0), sqlserv(this, "SQL/pgsql", SERVICE_DATA)
        {
-               ServerInstance->Modules->UseInterface("SQLutils");
-
                sqlsuccess = new char[strlen(SQLSUCCESS)+1];
 
                strlcpy(sqlsuccess, SQLSUCCESS, strlen(SQLSUCCESS));
 
-               if (!ServerInstance->Modules->PublishFeature("SQL", this))
-               {
-                       throw ModuleException("BUG: PgSQL Unable to publish feature 'SQL'");
-               }
-
                ReadConf();
 
-               ServerInstance->Modules->PublishInterface("SQL", this);
-               Implementation eventlist[] = { I_OnUnloadModule, I_OnRequest, I_OnRehash };
-               ServerInstance->Modules->Attach(eventlist, this, 3);
+               ServerInstance->Modules->AddService(sqlserv);
+               Implementation eventlist[] = { I_OnUnloadModule, I_OnRehash };
+               ServerInstance->Modules->Attach(eventlist, this, 2);
        }
 
        virtual ~ModulePgSQL()
@@ -790,9 +785,6 @@ class ModulePgSQL : public Module
                        ServerInstance->Timers->DelTimer(retimer);
                ClearAllConnections();
                delete[] sqlsuccess;
-               ServerInstance->Modules->UnpublishInterface("SQL", this);
-               ServerInstance->Modules->UnpublishFeature("SQL");
-               ServerInstance->Modules->DoneWithInterface("SQLutils");
        }
 
 
@@ -813,7 +805,7 @@ class ModulePgSQL : public Module
 
        bool HostInConf(const SQLhost &h)
        {
-               ConfigReader conf(ServerInstance);
+               ConfigReader conf;
                for(int i = 0; i < conf.Enumerate("database"); i++)
                {
                        SQLhost host;
@@ -834,7 +826,7 @@ class ModulePgSQL : public Module
        {
                ClearOldConnections();
 
-               ConfigReader conf(ServerInstance);
+               ConfigReader conf;
                for(int i = 0; i < conf.Enumerate("database"); i++)
                {
                        SQLhost host;
@@ -889,7 +881,7 @@ class ModulePgSQL : public Module
 
                SQLConn* newconn;
 
-               newconn = new SQLConn(ServerInstance, this, hi);
+               newconn = new SQLConn(this, hi);
 
                connections.insert(std::make_pair(hi.id, newconn));
        }
@@ -905,34 +897,30 @@ class ModulePgSQL : public Module
                                break;
                        }
                }
-               retimer = new ReconnectTimer(ServerInstance, this);
+               retimer = new ReconnectTimer(this);
                ServerInstance->Timers->AddTimer(retimer);
        }
 
-       virtual const char* OnRequest(Request* request)
+       void OnRequest(Request& request)
        {
-               if(strcmp(SQLREQID, request->GetId()) == 0)
+               if(strcmp(SQLREQID, request.id) == 0)
                {
-                       SQLrequest* req = (SQLrequest*)request;
+                       SQLrequest* req = (SQLrequest*)&request;
                        ConnMap::iterator iter;
                        if((iter = connections.find(req->dbid)) != connections.end())
                        {
                                /* Execute query */
                                req->id = NewID();
                                req->error = iter->second->Query(*req);
-
-                               return (req->error.Id() == SQL_NO_ERROR) ? sqlsuccess : NULL;
                        }
                        else
                        {
                                req->error.Id(SQL_BAD_DBID);
-                               return NULL;
                        }
                }
-               return NULL;
        }
 
-       virtual void OnUnloadModule(Module* mod, const std::string&     name)
+       virtual void OnUnloadModule(Module* mod)
        {
                /* When a module unloads we have to check all the pending queries for all our connections
                 * and set the Module* specifying where the query came from to NULL. If the query has already
@@ -956,7 +944,7 @@ class ModulePgSQL : public Module
 
        virtual Version GetVersion()
        {
-               return Version("$Id$", VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
+               return Version("PostgreSQL Service Provider module for all other m_sql* modules, uses v2 of the SQL API", VF_VENDOR);
        }
 };