From 5f48c2a56ff8a318978d1f6af5bc1c8d82ee98d4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 24 Apr 2016 18:48:23 +0100 Subject: Use prepared statements for bulk selects --- libpqpp/pq-prepared.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 libpqpp/pq-prepared.cpp (limited to 'libpqpp/pq-prepared.cpp') diff --git a/libpqpp/pq-prepared.cpp b/libpqpp/pq-prepared.cpp new file mode 100644 index 0000000..4cd4540 --- /dev/null +++ b/libpqpp/pq-prepared.cpp @@ -0,0 +1,29 @@ +#include "pq-prepared.h" +#include "pq-connection.h" + +PQ::PreparedStatement::PreparedStatement(Connection * c, const std::string & sql, unsigned int no) : + DB::Command(sql), + Command(c, sql, no), + pstmt(nullptr) +{ +} + +const char * +PQ::PreparedStatement::prepare() const +{ + if (pstmt) { + return pstmt; + } + auto hash(std::hash()(sql)); + auto i = c->preparedStatements.find(hash); + if (i != c->preparedStatements.end()) { + 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 (pstmt = c->preparedStatements.insert({hash, stmntName}).first->second.c_str()); +} + -- cgit v1.2.3