diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-02-28 17:22:24 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-02-28 17:22:24 +0000 |
commit | 9228b13ad8b8a42277af00bbcd645aa65e0c9f56 (patch) | |
tree | 1800183cd6053d9c2703934c2a8a978ed33aa260 /libpqpp | |
parent | Prepare statement upfront during construction (diff) | |
download | libdbpp-postgresql-1.0.3.tar.bz2 libdbpp-postgresql-1.0.3.tar.xz libdbpp-postgresql-1.0.3.zip |
Actually, don't prepare statement upfront during construction as it might not be valid (e.g. uncreated temp tables)libdbpp-postgresql-1.0.3
Diffstat (limited to 'libpqpp')
-rw-r--r-- | libpqpp/pq-modifycommand.cpp | 13 | ||||
-rw-r--r-- | libpqpp/pq-modifycommand.h | 4 | ||||
-rw-r--r-- | libpqpp/unittests/testpq.cpp | 2 |
3 files changed, 11 insertions, 8 deletions
diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp index e140f0a..caad5c6 100644 --- a/libpqpp/pq-modifycommand.cpp +++ b/libpqpp/pq-modifycommand.cpp @@ -7,7 +7,7 @@ PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, uns DB::Command(sql), DB::ModifyCommand(sql), PQ::Command(conn, sql, no), - pstmt(prepare()->second) + pstmt(nullptr) { } @@ -15,26 +15,29 @@ PQ::ModifyCommand::~ModifyCommand() { } -PQ::Connection::PreparedStatements::const_iterator +const char * PQ::ModifyCommand::prepare() const { + if (pstmt) { + return pstmt; + } auto hash(std::hash<std::string>()(sql)); auto i = c->preparedStatements.find(hash); if (i != c->preparedStatements.end()) { - return i; + return (pstmt = i->second.c_str()); } std::string psql; psql.reserve(sql.length() + 20); prepareSql(psql, sql); c->checkResultFree(PQprepare( c->conn, stmntName.c_str(), psql.c_str(), values.size(), NULL), PGRES_COMMAND_OK); - return c->preparedStatements.insert({hash, stmntName}).first; + return (pstmt = c->preparedStatements.insert({hash, stmntName}).first->second.c_str()); } unsigned int PQ::ModifyCommand::execute(bool anc) { - PGresult * res = PQexecPrepared(c->conn, pstmt.c_str(), values.size(), &values.front(), &lengths.front(), NULL, 0); + PGresult * res = PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), NULL, 0); c->checkResult(res, PGRES_COMMAND_OK, PGRES_TUPLES_OK); unsigned int rows = atoi(PQcmdTuples(res)); PQclear(res); diff --git a/libpqpp/pq-modifycommand.h b/libpqpp/pq-modifycommand.h index ed8b622..79593a1 100644 --- a/libpqpp/pq-modifycommand.h +++ b/libpqpp/pq-modifycommand.h @@ -14,8 +14,8 @@ namespace PQ { unsigned int execute(bool) override; private: - Connection::PreparedStatements::const_iterator prepare() const; - const std::string pstmt; + const char * prepare() const; + mutable const char * pstmt; }; } diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index 088910d..1905192 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -271,7 +271,7 @@ BOOST_AUTO_TEST_CASE( statementReuse ) BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 1); for (int y = 0; y < 4; y += 1) { auto m1 = ro->modify("INSERT INTO test(id) VALUES(?)"); - BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 2); + BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), y == 0 ? 1 : 2); for (int x = 0; x < 4; x += 1) { m1->bindParamI(0, x); m1->execute(); |