From 409aeefb61795aa8f524d4371800a5bebb66a2e4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 1 Jan 2017 20:08:22 +0000 Subject: Use command options statement hash when it's available --- libpqpp/pq-bulkselectcommand.cpp | 4 ++-- libpqpp/pq-bulkselectcommand.h | 2 +- libpqpp/pq-connection.cpp | 8 ++++---- libpqpp/pq-modifycommand.cpp | 4 ++-- libpqpp/pq-modifycommand.h | 2 +- libpqpp/pq-prepared.cpp | 4 ++-- libpqpp/pq-prepared.h | 4 +++- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/libpqpp/pq-bulkselectcommand.cpp b/libpqpp/pq-bulkselectcommand.cpp index 8b669f4..466eaaf 100644 --- a/libpqpp/pq-bulkselectcommand.cpp +++ b/libpqpp/pq-bulkselectcommand.cpp @@ -3,10 +3,10 @@ #include "pq-column.h" #include "pq-error.h" -PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, unsigned int no) : +PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, unsigned int no, const DB::CommandOptions * opts) : DB::Command(sql), DB::SelectCommand(sql), - PQ::PreparedStatement(conn, sql, no), + PQ::PreparedStatement(conn, sql, no, opts), executed(false) { } diff --git a/libpqpp/pq-bulkselectcommand.h b/libpqpp/pq-bulkselectcommand.h index 58b01aa..e4775d4 100644 --- a/libpqpp/pq-bulkselectcommand.h +++ b/libpqpp/pq-bulkselectcommand.h @@ -12,7 +12,7 @@ namespace PQ { class Column; class BulkSelectCommand : public DB::SelectCommand, public SelectBase, public PreparedStatement { public: - BulkSelectCommand(Connection *, const std::string & sql, unsigned int no); + BulkSelectCommand(Connection *, const std::string & sql, unsigned int no, const DB::CommandOptions *); virtual ~BulkSelectCommand(); bool fetch() override; diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp index ec53981..590435e 100644 --- a/libpqpp/pq-connection.cpp +++ b/libpqpp/pq-connection.cpp @@ -97,19 +97,19 @@ PQ::Connection::ping() const DB::SelectCommand * -PQ::Connection::newSelectCommand(const std::string & sql, const DB::CommandOptions *) +PQ::Connection::newSelectCommand(const std::string & sql, const DB::CommandOptions * opts) { // Yes, this is a hack if (sql.find("libdbpp:no-cursor") != (std::string::size_type)-1) { - return new BulkSelectCommand(this, sql, pstmntNo++); + return new BulkSelectCommand(this, sql, pstmntNo++, opts); } return new CursorSelectCommand(this, sql, pstmntNo++); } DB::ModifyCommand * -PQ::Connection::newModifyCommand(const std::string & sql, const DB::CommandOptions *) +PQ::Connection::newModifyCommand(const std::string & sql, const DB::CommandOptions * opts) { - return new ModifyCommand(this, sql, pstmntNo++); + return new ModifyCommand(this, sql, pstmntNo++, opts); } bool diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp index 4cc31d2..f8e4d63 100644 --- a/libpqpp/pq-modifycommand.cpp +++ b/libpqpp/pq-modifycommand.cpp @@ -3,10 +3,10 @@ #include #include "pq-connection.h" -PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, unsigned int no) : +PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, unsigned int no, const DB::CommandOptions * opts) : DB::Command(sql), DB::ModifyCommand(sql), - PQ::PreparedStatement(conn, sql, no) + PQ::PreparedStatement(conn, sql, no, opts) { } diff --git a/libpqpp/pq-modifycommand.h b/libpqpp/pq-modifycommand.h index ef50261..4f0a481 100644 --- a/libpqpp/pq-modifycommand.h +++ b/libpqpp/pq-modifycommand.h @@ -8,7 +8,7 @@ namespace PQ { class ModifyCommand : public DB::ModifyCommand, public PreparedStatement { public: - ModifyCommand(Connection *, const std::string & sql, unsigned int no); + ModifyCommand(Connection *, const std::string & sql, unsigned int no, const DB::CommandOptions *); virtual ~ModifyCommand(); unsigned int execute(bool) override; diff --git a/libpqpp/pq-prepared.cpp b/libpqpp/pq-prepared.cpp index 4cd4540..bdf2a7d 100644 --- a/libpqpp/pq-prepared.cpp +++ b/libpqpp/pq-prepared.cpp @@ -1,9 +1,10 @@ #include "pq-prepared.h" #include "pq-connection.h" -PQ::PreparedStatement::PreparedStatement(Connection * c, const std::string & sql, unsigned int no) : +PQ::PreparedStatement::PreparedStatement(Connection * c, const std::string & sql, unsigned int no, const DB::CommandOptions * opts) : DB::Command(sql), Command(c, sql, no), + hash(opts && opts->hash ? *opts->hash : std::hash()(sql)), pstmt(nullptr) { } @@ -14,7 +15,6 @@ 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()); diff --git a/libpqpp/pq-prepared.h b/libpqpp/pq-prepared.h index 1f6e028..0f745c9 100644 --- a/libpqpp/pq-prepared.h +++ b/libpqpp/pq-prepared.h @@ -2,14 +2,16 @@ #define stuff #include "pq-command.h" +#include "pq-connection.h" namespace PQ { class PreparedStatement : public Command { protected: - PreparedStatement(Connection *, const std::string &, unsigned int); + PreparedStatement(Connection *, const std::string &, unsigned int, const DB::CommandOptions *); virtual ~PreparedStatement() = default; const char * prepare() const; + Connection::StatementHash hash; mutable const char * pstmt; }; } -- cgit v1.2.3