From 55b8c97ae06dd7902d3dc47eece04890ecaf30bc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 1 Apr 2018 13:52:39 +0100 Subject: Transactionless cursor selects Remove the need to open a transaction for a cursor select by specifying them as WITH HOLD. Also add NO SCROLL as it's a feature we don't actually use. Updates tests with loops to ensure we haven't broken the ability to re-use / re-create statements. --- libpqpp/pq-cursorselectcommand.cpp | 10 +--------- libpqpp/pq-cursorselectcommand.h | 1 - libpqpp/unittests/testpq.cpp | 13 +++++++++---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp index 2b639c8..53d951a 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 %? NO SCROLL CURSOR WITH HOLD FOR "); AdHocFormatter(PQCursorSelectFetch, "FETCH %? IN %?"); AdHocFormatter(PQCursorSelectClose, "CLOSE %?"); @@ -12,7 +12,6 @@ PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::strin PQ::SelectBase(sql, pqco), PQ::Command(conn, sql, opts), executed(false), - txOpened(false), fTuples(pqco ? pqco->fetchTuples : 35), s_fetch(PQCursorSelectFetch::get(fTuples, stmntName)), s_close(PQCursorSelectClose::get(stmntName)) @@ -24,9 +23,6 @@ PQ::CursorSelectCommand::~CursorSelectCommand() if (executed && PQtransactionStatus(c->conn) != PQTRANS_INERROR) { c->checkResultFree((PQexec(c->conn, s_close.c_str())), PGRES_COMMAND_OK); } - if (txOpened) { - c->commitTx(); - } } std::string @@ -42,10 +38,6 @@ void PQ::CursorSelectCommand::execute() { if (!executed) { - if (!c->inTx()) { - c->beginTx(); - txOpened = true; - } if (s_declare.empty()) { s_declare = mkdeclare(); } diff --git a/libpqpp/pq-cursorselectcommand.h b/libpqpp/pq-cursorselectcommand.h index 8182210..4d47229 100644 --- a/libpqpp/pq-cursorselectcommand.h +++ b/libpqpp/pq-cursorselectcommand.h @@ -22,7 +22,6 @@ namespace PQ { std::string mkdeclare() const; mutable bool executed; - mutable bool txOpened; int fTuples; std::string s_declare; std::string s_fetch; diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index 476c3a2..cfe7b11 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -112,13 +112,18 @@ BOOST_AUTO_TEST_CASE( selectInTx ) { auto db = DB::MockDatabase::openConnectionTo("PQmock"); - auto select = db->newSelectCommand("SELECT * FROM test"); - while (select->fetch()) { } - delete select; + // Loop to ensure we can create the same statement several times + for (int x = 0; x < 2; x++) { + auto select = db->select("SELECT * FROM test"); + // Loop to ensure we can use the same command several times + for (int y = 0; y < 2; y++) { + while (select->fetch()) { } + } + } db->finish(); db->beginTx(); - select = db->newSelectCommand("SELECT * FROM test"); + auto select = db->newSelectCommand("SELECT * FROM test"); while (select->fetch()) { } delete select; db->commitTx(); -- cgit v1.2.3