From 74fcbf827294bac907fffe57382e34359893f20f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 4 Jan 2017 02:00:04 +0000 Subject: C++ify the SQL preparer and skip it if no variables are being bound --- libpqpp/pq-command.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'libpqpp/pq-command.cpp') diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp index 84eaa43..af203cc 100644 --- a/libpqpp/pq-command.cpp +++ b/libpqpp/pq-command.cpp @@ -25,22 +25,26 @@ PQ::Command::~Command() } } +AdHocFormatter(PQCommandParamName, "$%?"); void -PQ::Command::prepareSql(std::string & psql, const std::string & sql) +PQ::Command::prepareSql(std::stringstream & psql, const std::string & sql) const { - char buf[10]; + if (values.empty()) { + psql << sql; + return; + } int p = 1; bool inquote = false; - for(std::string::const_iterator i = sql.begin(); i != sql.end(); ++i) { - if (*i == '?' && !inquote) { - psql.append(buf, snprintf(buf, 10, "$%d", p++)); + for (const auto & i : sql) { + if (i == '?' && !inquote) { + PQCommandParamName::write(psql, p++); } - else if (*i == '\'') { + else if (i == '\'') { inquote = !inquote; - psql += *i; + psql << i; } else { - psql += *i; + psql << i; } } } -- cgit v1.2.3