summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-07 14:26:09 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-07 14:26:09 +0000
commitb58f541a17dcc3eadb0502275bc90412ed9115a7 (patch)
treedb9175af481bc36eee961b9a7182e9a11dc58590
parentImprove connection failure detection (diff)
downloadlibdbpp-postgresql-b58f541a17dcc3eadb0502275bc90412ed9115a7.tar.bz2
libdbpp-postgresql-b58f541a17dcc3eadb0502275bc90412ed9115a7.tar.xz
libdbpp-postgresql-b58f541a17dcc3eadb0502275bc90412ed9115a7.zip
Merge select and modify command's SQL parser
-rw-r--r--libpqpp/command.cpp21
-rw-r--r--libpqpp/command.h1
-rw-r--r--libpqpp/modifycommand.cpp17
-rw-r--r--libpqpp/selectcommand.cpp17
4 files changed, 24 insertions, 32 deletions
diff --git a/libpqpp/command.cpp b/libpqpp/command.cpp
index ba12574..94079da 100644
--- a/libpqpp/command.cpp
+++ b/libpqpp/command.cpp
@@ -25,6 +25,27 @@ PQ::Command::~Command()
}
void
+PQ::Command::prepareSql(std::string & psql, const std::string & sql)
+{
+ char buf[4];
+ int p = 1;
+ bool inquote = false;
+ for(std::string::const_iterator i = sql.begin(); i != sql.end(); ++i) {
+ if (*i == '?' && !inquote) {
+ snprintf(buf, 4, "$%d", p++);
+ psql += buf;
+ }
+ else if (*i == '\'') {
+ inquote = !inquote;
+ psql += *i;
+ }
+ else {
+ psql += *i;
+ }
+ }
+}
+
+void
PQ::Command::paramsAtLeast(unsigned int n)
{
if (values.size() <= n) {
diff --git a/libpqpp/command.h b/libpqpp/command.h
index e6edac2..32faaa0 100644
--- a/libpqpp/command.h
+++ b/libpqpp/command.h
@@ -31,6 +31,7 @@ namespace PQ {
void bindNull(unsigned int);
protected:
+ static void prepareSql(std::string & psql, const std::string & sql);
const std::string stmntName;
const Connection * c;
diff --git a/libpqpp/modifycommand.cpp b/libpqpp/modifycommand.cpp
index 3eb41ec..460ec31 100644
--- a/libpqpp/modifycommand.cpp
+++ b/libpqpp/modifycommand.cpp
@@ -21,22 +21,7 @@ PQ::ModifyCommand::prepare() const
if (!prepared) {
std::string psql;
psql.reserve(sql.length() + 20);
- char buf[4];
- int p = 1;
- bool inquote = false;
- for(std::string::const_iterator i = sql.begin(); i != sql.end(); ++i) {
- if (*i == '?' && !inquote) {
- snprintf(buf, 4, "$%d", p++);
- psql += buf;
- }
- else if (*i == '\'') {
- inquote = !inquote;
- psql += *i;
- }
- else {
- psql += *i;
- }
- }
+ prepareSql(psql, sql);
c->checkResultFree(PQprepare(
c->conn, stmntName.c_str(), psql.c_str(), values.size(), NULL), PGRES_COMMAND_OK);
prepared = true;
diff --git a/libpqpp/selectcommand.cpp b/libpqpp/selectcommand.cpp
index 8e3ec36..2312dbb 100644
--- a/libpqpp/selectcommand.cpp
+++ b/libpqpp/selectcommand.cpp
@@ -37,25 +37,10 @@ PQ::SelectCommand::mkdeclare() const
{
std::string psql;
psql.reserve(sql.length() + 40);
- char buf[4];
- int p = 1;
- bool inquote = false;
psql += "DECLARE ";
psql += stmntName;
psql += " CURSOR FOR ";
- for(std::string::const_iterator i = sql.begin(); i != sql.end(); ++i) {
- if (*i == '?' && !inquote) {
- snprintf(buf, 4, "$%d", p++);
- psql += buf;
- }
- else if (*i == '\'') {
- inquote = !inquote;
- psql += *i;
- }
- else {
- psql += *i;
- }
- }
+ prepareSql(psql, sql);
return psql;
}