summaryrefslogtreecommitdiff
path: root/libpqpp/pq-prepared.cpp
diff options
context:
space:
mode:
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());
+}
+