From 60a649d4c71dfd02db589f95ed4b04b3b40641b1 Mon Sep 17 00:00:00 2001 From: Daniel De Graaf Date: Fri, 27 Aug 2010 22:12:25 -0400 Subject: Add FormatSubstitute utility class --- src/modules/extra/m_pgsql.cpp | 48 ++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 26 deletions(-) (limited to 'src/modules/extra/m_pgsql.cpp') diff --git a/src/modules/extra/m_pgsql.cpp b/src/modules/extra/m_pgsql.cpp index a697518ae..27e12cf25 100644 --- a/src/modules/extra/m_pgsql.cpp +++ b/src/modules/extra/m_pgsql.cpp @@ -418,38 +418,34 @@ restart: submit(req, res); } - void submit(SQLQuery *req, const std::string& q, const ParamM& p) + class PGSubstFn : public FormatSubstitute { - std::string res; - for(std::string::size_type i = 0; i < q.length(); i++) + public: + PGconn* sql; + const ParamM& map; + PGSubstFn(PGconn* SQL, const ParamM& Map) : sql(SQL), map(Map) {} + std::string lookup(const std::string& key) { - if (q[i] != '$') - res.push_back(q[i]); - else - { - std::string field; - i++; - while (i < q.length() && isalnum(q[i])) - field.push_back(q[i++]); - i--; - - ParamM::const_iterator it = p.find(field); - if (it != p.end()) - { - std::string parm = it->second; - char buffer[MAXBUF]; + char buffer[MAXBUF]; + ParamM::const_iterator it = map.find(key); + if (it == map.end()) + return ""; #ifdef PGSQL_HAS_ESCAPECONN - int error; - PQescapeStringConn(sql, buffer, parm.c_str(), parm.length(), &error); - if (error) - ServerInstance->Logs->Log("m_pgsql", DEBUG, "BUG: Apparently PQescapeStringConn() failed"); + int error; + PQescapeStringConn(sql, buffer, it->second.c_str(), it->second.length(), &error); + if (error) + ServerInstance->Logs->Log("m_pgsql", DEBUG, "BUG: Apparently PQescapeStringConn() failed"); #else - PQescapeString (buffer, parm.c_str(), parm.length()); + PQescapeString(buffer, it->second.c_str(), it->second.length()); #endif - res.append(buffer); - } - } + return buffer; } + }; + + void submit(SQLQuery *req, const std::string& q, const ParamM& p) + { + PGSubstFn subst(sql, p); + std::string res = subst.format(q); submit(req, res); } -- cgit v1.3.1-10-gc9f91