From 9e1fda2a44dee120e26acc6e51fbe779eea97712 Mon Sep 17 00:00:00 2001 From: DukePyrolator Date: Sun, 8 Apr 2012 12:30:48 +0200 Subject: [PATCH] Modified the SQL API to allow unescaped parameters (useful for passing row names and NULL values) --- modules/extra/m_mysql.cpp | 4 ++-- modules/extra/sql.h | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/extra/m_mysql.cpp b/modules/extra/m_mysql.cpp index 138c726d8..e83e78c7c 100644 --- a/modules/extra/m_mysql.cpp +++ b/modules/extra/m_mysql.cpp @@ -423,8 +423,8 @@ Anope::string MySQLService::BuildQuery(const SQLQuery &q) { Anope::string real_query = q.query; - for (std::map::const_iterator it = q.parameters.begin(), it_end = q.parameters.end(); it != it_end; ++it) - real_query = real_query.replace_all_cs("@" + it->first + "@", "'" + this->Escape(it->second) + "'"); + for (std::map::const_iterator it = q.parameters.begin(), it_end = q.parameters.end(); it != it_end; ++it) + real_query = real_query.replace_all_cs("@" + it->first + "@", (it->second.escape ? ("'" + this->Escape(it->second.data) + "'") : it->second.data)); return real_query; } diff --git a/modules/extra/sql.h b/modules/extra/sql.h index 0b27fe744..b23466dee 100644 --- a/modules/extra/sql.h +++ b/modules/extra/sql.h @@ -11,10 +11,17 @@ class SQLException : public ModuleException /** A SQL query */ + +struct QueryData +{ + Anope::string data; + bool escape; +}; + struct SQLQuery { Anope::string query; - std::map parameters; + std::map parameters; SQLQuery() { } SQLQuery(const Anope::string &q) : query(q) { } @@ -36,12 +43,13 @@ struct SQLQuery return !(*this == other); } - template void setValue(const Anope::string &key, const T& value) + template void setValue(const Anope::string &key, const T& value, bool escape = true) { try { Anope::string string_value = stringify(value); - this->parameters[key] = string_value; + this->parameters[key].data = string_value; + this->parameters[key].escape = escape; } catch (const ConvertException &ex) { } }