diff options
| author | 2010-08-27 22:12:25 -0400 | |
|---|---|---|
| committer | 2010-08-27 22:43:59 -0400 | |
| commit | 60a649d4c71dfd02db589f95ed4b04b3b40641b1 (patch) | |
| tree | 4e2a200ee4be50fdef590f7ebfec5316313251af /src/modules/extra/m_pgsql.cpp | |
| parent | Promote PopulateInfoMap to core, it is moderately useful (diff) | |
Add FormatSubstitute utility class
Diffstat (limited to 'src/modules/extra/m_pgsql.cpp')
| -rw-r--r-- | src/modules/extra/m_pgsql.cpp | 48 |
1 files changed, 22 insertions, 26 deletions
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); } |
