]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_pgsql.cpp
Add GetId() to the SQL::Provider class.
[user/henk/code/inspircd.git] / src / modules / extra / m_pgsql.cpp
index 5f6f6e30faa0615c5d9c80eb37c306a59109571b..b0f0b4597eba45367a256e7d7801158ebd0eb3cf 100644 (file)
 /// $CompilerFlags: -Iexecute("pg_config --includedir" "POSTGRESQL_INCLUDE_DIR")
 /// $LinkerFlags: -Lexecute("pg_config --libdir" "POSTGRESQL_LIBRARY_DIR") -lpq
 
+/// $PackageInfo: require_system("arch") postgresql-libs
 /// $PackageInfo: require_system("centos") postgresql-devel
 /// $PackageInfo: require_system("darwin") postgresql
+/// $PackageInfo: require_system("debian") libpq-dev
 /// $PackageInfo: require_system("ubuntu") libpq-dev
 
 
@@ -64,14 +66,14 @@ class ReconnectTimer : public Timer
        ReconnectTimer(ModulePgSQL* m) : Timer(5, false), mod(m)
        {
        }
-       bool Tick(time_t TIME);
+       bool Tick(time_t TIME) CXX11_OVERRIDE;
 };
 
 struct QueueItem
 {
-       SQLQuery* c;
+       SQL::Query* c;
        std::string q;
-       QueueItem(SQLQuery* C, const std::string& Q) : c(C), q(Q) {}
+       QueueItem(SQL::Query* C, const std::string& Q) : c(C), q(Q) {}
 };
 
 /** PgSQLresult is a subclass of the mostly-pure-virtual class SQLresult.
@@ -81,17 +83,27 @@ struct QueueItem
  * data is passes to the module nearly as directly as if it was using the API directly itself.
  */
 
-class PgSQLresult : public SQLResult
+class PgSQLresult : public SQL::Result
 {
        PGresult* res;
        int currentrow;
        int rows;
+       std::vector<std::string> colnames;
+
+       void getColNames()
+       {
+               colnames.resize(PQnfields(res));
+               for(unsigned int i=0; i < colnames.size(); i++)
+               {
+                       colnames[i] = PQfname(res, i);
+               }
+       }
  public:
        PgSQLresult(PGresult* result) : res(result), currentrow(0)
        {
                rows = PQntuples(res);
                if (!rows)
-                       rows = atoi(PQcmdTuples(res));
+                       rows = ConvToNum<int>(PQcmdTuples(res));
        }
 
        ~PgSQLresult()
@@ -99,30 +111,44 @@ class PgSQLresult : public SQLResult
                PQclear(res);
        }
 
-       int Rows()
+       int Rows() CXX11_OVERRIDE
        {
                return rows;
        }
 
-       void GetCols(std::vector<std::string>& result)
+       void GetCols(std::vector<std::string>& result) CXX11_OVERRIDE
+       {
+               if (colnames.empty())
+                       getColNames();
+               result = colnames;
+       }
+
+       bool HasColumn(const std::string& column, size_t& index) CXX11_OVERRIDE
        {
-               result.resize(PQnfields(res));
-               for(unsigned int i=0; i < result.size(); i++)
+               if (colnames.empty())
+                       getColNames();
+
+               for (size_t i = 0; i < colnames.size(); ++i)
                {
-                       result[i] = PQfname(res, i);
+                       if (colnames[i] == column)
+                       {
+                               index = i;
+                               return true;
+                       }
                }
+               return false;
        }
 
-       SQLEntry GetValue(int row, int column)
+       SQL::Field GetValue(int row, int column)
        {
                char* v = PQgetvalue(res, row, column);
                if (!v || PQgetisnull(res, row, column))
-                       return SQLEntry();
+                       return SQL::Field();
 
-               return SQLEntry(std::string(v, PQgetlength(res, row, column)));
+               return SQL::Field(std::string(v, PQgetlength(res, row, column)));
        }
 
-       bool GetRow(SQLEntries& result)
+       bool GetRow(SQL::Row& result) CXX11_OVERRIDE
        {
                if (currentrow >= PQntuples(res))
                        return false;
@@ -140,7 +166,7 @@ class PgSQLresult : public SQLResult
 
 /** SQLConn represents one SQL session.
  */
-class SQLConn : public SQLProvider, public EventHandler
+class SQLConn : public SQL::Provider, public EventHandler
 {
  public:
        reference<ConfigTag> conf;      /* The <database> entry */
@@ -150,7 +176,11 @@ class SQLConn : public SQLProvider, public EventHandler
        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, "")
+               : SQL::Provider(Creator, tag->getString("id"))
+               , conf(tag)
+               , sql(NULL)
+               , status(CWRITE)
+               , qinprog(NULL, "")
        {
                if (!DoConnect())
                {
@@ -159,16 +189,16 @@ class SQLConn : public SQLProvider, public EventHandler
                }
        }
 
-       CullResult cull()
+       CullResult cull() CXX11_OVERRIDE
        {
-               this->SQLProvider::cull();
+               this->SQL::Provider::cull();
                ServerInstance->Modules->DelService(*this);
                return this->EventHandler::cull();
        }
 
        ~SQLConn()
        {
-               SQLerror err(SQL_BAD_DBID);
+               SQL::Error err(SQL::BAD_DBID);
                if (qinprog.c)
                {
                        qinprog.c->OnError(err);
@@ -176,7 +206,7 @@ class SQLConn : public SQLProvider, public EventHandler
                }
                for(std::deque<QueueItem>::iterator i = queue.begin(); i != queue.end(); i++)
                {
-                       SQLQuery* q = i->c;
+                       SQL::Query* q = i->c;
                        q->OnError(err);
                        delete q;
                }
@@ -273,6 +303,7 @@ class SQLConn : public SQLProvider, public EventHandler
                                SocketEngine::ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = WWRITE;
                                DoConnectedPoll();
+                               return true;
                        default:
                                return true;
                }
@@ -319,7 +350,7 @@ restart:
                                        case PGRES_BAD_RESPONSE:
                                        case PGRES_FATAL_ERROR:
                                        {
-                                               SQLerror err(SQL_QREPLY_FAIL, PQresultErrorMessage(result));
+                                               SQL::Error err(SQL::QREPLY_FAIL, PQresultErrorMessage(result));
                                                qinprog.c->OnError(err);
                                                break;
                                        }
@@ -366,6 +397,7 @@ restart:
                                SocketEngine::ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                                status = WWRITE;
                                DoConnectedPoll();
+                               return true;
                        default:
                                return true;
                }
@@ -389,8 +421,9 @@ restart:
                }
        }
 
-       void submit(SQLQuery *req, const std::string& q)
+       void Submit(SQL::Query *req, const std::string& q) CXX11_OVERRIDE
        {
+               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Executing PostgreSQL query: " + q);
                if (qinprog.q.empty())
                {
                        DoQuery(QueueItem(req,q));
@@ -402,7 +435,7 @@ restart:
                }
        }
 
-       void submit(SQLQuery *req, const std::string& q, const ParamL& p)
+       void Submit(SQL::Query *req, const std::string& q, const SQL::ParamList& p) CXX11_OVERRIDE
        {
                std::string res;
                unsigned int param = 0;
@@ -424,10 +457,10 @@ restart:
                                }
                        }
                }
-               submit(req, res);
+               Submit(req, res);
        }
 
-       void submit(SQLQuery *req, const std::string& q, const ParamM& p)
+       void Submit(SQL::Query *req, const std::string& q, const SQL::ParamMap& p) CXX11_OVERRIDE
        {
                std::string res;
                for(std::string::size_type i = 0; i < q.length(); i++)
@@ -442,7 +475,7 @@ restart:
                                        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())
                                {
                                        std::string parm = it->second;
@@ -455,7 +488,7 @@ restart:
                                }
                        }
                }
-               submit(req, res);
+               Submit(req, res);
        }
 
        void DoQuery(const QueueItem& req)
@@ -463,7 +496,7 @@ restart:
                if (status != WREAD && status != WWRITE)
                {
                        // whoops, not connected...
-                       SQLerror err(SQL_BAD_CONN);
+                       SQL::Error err(SQL::BAD_CONN);
                        req.c->OnError(err);
                        delete req.c;
                        return;
@@ -475,7 +508,7 @@ restart:
                }
                else
                {
-                       SQLerror err(SQL_QSEND_FAIL, PQerrorMessage(sql));
+                       SQL::Error err(SQL::QSEND_FAIL, PQerrorMessage(sql));
                        req.c->OnError(err);
                        delete req.c;
                }
@@ -521,7 +554,7 @@ class ModulePgSQL : public Module
                ConfigTagList tags = ServerInstance->Config->ConfTags("database");
                for(ConfigIter i = tags.first; i != tags.second; i++)
                {
-                       if (i->second->getString("module", "pgsql") != "pgsql")
+                       if (!stdalgo::string::equalsci(i->second->getString("module"), "pgsql"))
                                continue;
                        std::string id = i->second->getString("id");
                        ConnMap::iterator curr = connections.find(id);
@@ -553,7 +586,7 @@ class ModulePgSQL : public Module
 
        void OnUnloadModule(Module* mod) CXX11_OVERRIDE
        {
-               SQLerror err(SQL_BAD_DBID);
+               SQL::Error err(SQL::BAD_DBID);
                for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
                {
                        SQLConn* conn = i->second;
@@ -566,7 +599,7 @@ class ModulePgSQL : public Module
                        std::deque<QueueItem>::iterator j = conn->queue.begin();
                        while (j != conn->queue.end())
                        {
-                               SQLQuery* q = j->c;
+                               SQL::Query* q = j->c;
                                if (q->creator == mod)
                                {
                                        q->OnError(err);