diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2017-01-04 02:00:04 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2017-01-04 02:00:04 +0000 |
commit | f9fc8240229cd1f059bfc56eb027aec11fec6b4c (patch) | |
tree | c276986ac80870167d594d5d8229824317d38582 /libpqpp/pq-command.cpp | |
parent | Tidy clearing of PGresults (diff) | |
download | libdbpp-postgresql-f9fc8240229cd1f059bfc56eb027aec11fec6b4c.tar.bz2 libdbpp-postgresql-f9fc8240229cd1f059bfc56eb027aec11fec6b4c.tar.xz libdbpp-postgresql-f9fc8240229cd1f059bfc56eb027aec11fec6b4c.zip |
C++ify the SQL preparer and skip it if no variables are being bound
Diffstat (limited to 'libpqpp/pq-command.cpp')
-rw-r--r-- | libpqpp/pq-command.cpp | 20 |
1 files changed, 12 insertions, 8 deletions
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; } } } |