X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fextra%2Fm_pgsql.cpp;h=bb727b623ea504dd4d3dbd4a874fd59e61531702;hb=77730fd5f09f8fc193205654c8bba84d34365670;hp=6835cb558dde29a7365c6da739bb1d75c1345cd5;hpb=834c94679b7df475e50d87ccb11311e297a78718;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/extra/m_pgsql.cpp b/src/modules/extra/m_pgsql.cpp index 6835cb558..bb727b623 100644 --- a/src/modules/extra/m_pgsql.cpp +++ b/src/modules/extra/m_pgsql.cpp @@ -26,6 +26,7 @@ /// $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 +65,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,11 +82,21 @@ 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 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) { @@ -99,30 +110,44 @@ class PgSQLresult : public SQLResult PQclear(res); } - int Rows() + int Rows() CXX11_OVERRIDE { return rows; } - void GetCols(std::vector& result) + void GetCols(std::vector& 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 +165,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 conf; /* The entry */ @@ -150,7 +175,7 @@ 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, "SQL/" + tag->getString("id")), conf(tag), sql(NULL), status(CWRITE), qinprog(NULL, "") { if (!DoConnect()) { @@ -161,14 +186,14 @@ class SQLConn : public SQLProvider, public EventHandler 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 +201,7 @@ class SQLConn : public SQLProvider, public EventHandler } for(std::deque::iterator i = queue.begin(); i != queue.end(); i++) { - SQLQuery* q = i->c; + SQL::Query* q = i->c; q->OnError(err); delete q; } @@ -319,7 +344,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; } @@ -389,7 +414,7 @@ restart: } } - void submit(SQLQuery *req, const std::string& q) CXX11_OVERRIDE + void Submit(SQL::Query *req, const std::string& q) CXX11_OVERRIDE { if (qinprog.q.empty()) { @@ -402,7 +427,7 @@ restart: } } - void submit(SQLQuery *req, const std::string& q, const ParamL& p) CXX11_OVERRIDE + void Submit(SQL::Query *req, const std::string& q, const SQL::ParamList& p) CXX11_OVERRIDE { std::string res; unsigned int param = 0; @@ -424,10 +449,10 @@ restart: } } } - submit(req, res); + Submit(req, res); } - void submit(SQLQuery *req, const std::string& q, const ParamM& p) CXX11_OVERRIDE + 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 +467,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 +480,7 @@ restart: } } } - submit(req, res); + Submit(req, res); } void DoQuery(const QueueItem& req) @@ -463,7 +488,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 +500,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 +546,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 +578,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 +591,7 @@ class ModulePgSQL : public Module std::deque::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);