summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-02-26 20:47:35 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2016-02-26 20:47:35 +0000
commit203936297e449f0215641037736cc4d142fd4a60 (patch)
treee2b8ea54713c563cb545a91f791e929b3e19a704
parentReuse prepared statements (diff)
downloadlibdbpp-postgresql-203936297e449f0215641037736cc4d142fd4a60.tar.bz2
libdbpp-postgresql-203936297e449f0215641037736cc4d142fd4a60.tar.xz
libdbpp-postgresql-203936297e449f0215641037736cc4d142fd4a60.zip
Prepare statement upfront during construction
-rw-r--r--libpqpp/pq-modifycommand.cpp5
-rw-r--r--libpqpp/pq-modifycommand.h2
-rw-r--r--libpqpp/unittests/testpq.cpp10
3 files changed, 13 insertions, 4 deletions
diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp
index e59908e..e140f0a 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),
- hash(std::hash<std::string>()(sql))
+ pstmt(prepare()->second)
{
}
@@ -18,6 +18,7 @@ PQ::ModifyCommand::~ModifyCommand()
PQ::Connection::PreparedStatements::const_iterator
PQ::ModifyCommand::prepare() const
{
+ auto hash(std::hash<std::string>()(sql));
auto i = c->preparedStatements.find(hash);
if (i != c->preparedStatements.end()) {
return i;
@@ -33,7 +34,7 @@ PQ::ModifyCommand::prepare() const
unsigned int
PQ::ModifyCommand::execute(bool anc)
{
- PGresult * res = PQexecPrepared(c->conn, prepare()->second.c_str(), values.size(), &values.front(), &lengths.front(), NULL, 0);
+ PGresult * res = PQexecPrepared(c->conn, pstmt.c_str(), 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 33fd333..ed8b622 100644
--- a/libpqpp/pq-modifycommand.h
+++ b/libpqpp/pq-modifycommand.h
@@ -15,7 +15,7 @@ namespace PQ {
private:
Connection::PreparedStatements::const_iterator prepare() const;
- const Connection::StatementHash hash;
+ const std::string pstmt;
};
}
diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp
index 3d8c804..088910d 100644
--- a/libpqpp/unittests/testpq.cpp
+++ b/libpqpp/unittests/testpq.cpp
@@ -267,14 +267,22 @@ BOOST_AUTO_TEST_CASE( statementReuse )
auto ro = DB::MockDatabase::openConnectionTo("pqmock");
auto pqconn = dynamic_cast<PQ::Connection *>(ro);
BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 0);
+ ro->modify("DELETE FROM test")->execute();
+ 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);
for (int x = 0; x < 4; x += 1) {
m1->bindParamI(0, x);
m1->execute();
}
}
- BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 1);
+ BOOST_REQUIRE_EQUAL(pqconn->preparedStatements.size(), 2);
+ auto select = ro->newSelectCommand("SELECT COUNT(id), SUM(id) FROM test");
+ while (select->fetch()) {
+ assertColumnValueHelper(*select, 0, 16);
+ assertColumnValueHelper(*select, 1, 24);
+ }
delete ro;
}