summaryrefslogtreecommitdiff
path: root/libpqpp/pq-prepared.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-04-24 18:48:23 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2016-04-24 18:48:23 +0100
commit56de3f5f9768396525669af327fd00d36e621ada (patch)
tree385bc028607b5d573a87ab2da861c06596fb61cf /libpqpp/pq-prepared.cpp
parentSupport bulk selects which don't use cursors, always use of RETURNING (diff)
downloadlibdbpp-postgresql-56de3f5f9768396525669af327fd00d36e621ada.tar.bz2
libdbpp-postgresql-56de3f5f9768396525669af327fd00d36e621ada.tar.xz
libdbpp-postgresql-56de3f5f9768396525669af327fd00d36e621ada.zip
Use prepared statements for bulk selects
Diffstat (limited to 'libpqpp/pq-prepared.cpp')
-rw-r--r--libpqpp/pq-prepared.cpp29
1 files changed, 29 insertions, 0 deletions
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<std::string>()(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());
+}
+