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-cursorselectcommand.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'libpqpp/pq-cursorselectcommand.cpp') diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp index 4ee6e80..ac4b825 100644 --- a/libpqpp/pq-cursorselectcommand.cpp +++ b/libpqpp/pq-cursorselectcommand.cpp @@ -3,7 +3,7 @@ #include "pq-error.h" #include -AdHocFormatter(PQCursorSelectDeclare, "DECLARE %? CURSOR FOR %?"); +AdHocFormatter(PQCursorSelectDeclare, "DECLARE %? CURSOR FOR "); AdHocFormatter(PQCursorSelectFetch, "FETCH %? IN %?"); AdHocFormatter(PQCursorSelectClose, "CLOSE %?"); @@ -14,7 +14,6 @@ PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::strin executed(false), txOpened(false), fTuples(35), - s_declare(mkdeclare()), s_fetch(PQCursorSelectFetch::get(fTuples, stmntName)), s_close(PQCursorSelectClose::get(stmntName)) { @@ -33,9 +32,10 @@ PQ::CursorSelectCommand::~CursorSelectCommand() std::string PQ::CursorSelectCommand::mkdeclare() const { - std::string psql; + std::stringstream psql; + PQCursorSelectDeclare::write(psql, stmntName); prepareSql(psql, sql); - return PQCursorSelectDeclare::get(stmntName, psql); + return psql.str(); } void @@ -46,6 +46,9 @@ PQ::CursorSelectCommand::execute() c->beginTx(); txOpened = true; } + if (s_declare.empty()) { + s_declare = mkdeclare(); + } c->checkResultFree( PQexecParams(c->conn, s_declare.c_str(), values.size(), NULL, &values.front(), &lengths.front(), NULL, 0), PGRES_COMMAND_OK); -- cgit v1.2.3