From bac96d1b62dde949412a998cb0c3231ad179d870 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 3 Jan 2017 15:03:05 +0000 Subject: Refactor to remove some duplication --- libpqpp/pq-bulkselectcommand.cpp | 14 ++------------ libpqpp/pq-bulkselectcommand.h | 4 +--- libpqpp/pq-cursorselectcommand.cpp | 11 ++--------- libpqpp/pq-cursorselectcommand.h | 3 +-- libpqpp/pq-selectbase.cpp | 21 ++++++++++++++++++++- libpqpp/pq-selectbase.h | 9 ++++++--- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/libpqpp/pq-bulkselectcommand.cpp b/libpqpp/pq-bulkselectcommand.cpp index 466eaaf..d745e64 100644 --- a/libpqpp/pq-bulkselectcommand.cpp +++ b/libpqpp/pq-bulkselectcommand.cpp @@ -5,19 +5,12 @@ PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, unsigned int no, const DB::CommandOptions * opts) : DB::Command(sql), - DB::SelectCommand(sql), + PQ::SelectBase(sql), PQ::PreparedStatement(conn, sql, no, opts), executed(false) { } -PQ::BulkSelectCommand::~BulkSelectCommand() -{ - if (execRes) { - PQclear(execRes); - } -} - void PQ::BulkSelectCommand::execute() { @@ -27,10 +20,7 @@ PQ::BulkSelectCommand::execute() PGRES_TUPLES_OK); nTuples = PQntuples(execRes); tuple = -1; - unsigned int nFields = PQnfields(execRes); - for (unsigned int f = 0; f < nFields; f += 1) { - insertColumn(DB::ColumnPtr(new Column(this, f))); - } + createColumns(execRes); executed = true; } } diff --git a/libpqpp/pq-bulkselectcommand.h b/libpqpp/pq-bulkselectcommand.h index e4775d4..a54a5a4 100644 --- a/libpqpp/pq-bulkselectcommand.h +++ b/libpqpp/pq-bulkselectcommand.h @@ -1,7 +1,6 @@ #ifndef PQ_BULKSELECTCOMMAND_H #define PQ_BULKSELECTCOMMAND_H -#include #include "pq-selectbase.h" #include "pq-prepared.h" #include @@ -10,10 +9,9 @@ namespace PQ { class Connection; class Column; - class BulkSelectCommand : public DB::SelectCommand, public SelectBase, public PreparedStatement { + class BulkSelectCommand : public SelectBase, public PreparedStatement { public: BulkSelectCommand(Connection *, const std::string & sql, unsigned int no, const DB::CommandOptions *); - virtual ~BulkSelectCommand(); bool fetch() override; void execute() override; diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp index 29e29f5..fbc14eb 100644 --- a/libpqpp/pq-cursorselectcommand.cpp +++ b/libpqpp/pq-cursorselectcommand.cpp @@ -1,12 +1,11 @@ #include "pq-cursorselectcommand.h" #include "pq-connection.h" -#include "pq-column.h" #include "pq-error.h" #include PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::string & sql, unsigned int no) : DB::Command(sql), - DB::SelectCommand(sql), + PQ::SelectBase(sql), PQ::Command(conn, sql, no), executed(false), txOpened(false), @@ -19,9 +18,6 @@ PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::strin PQ::CursorSelectCommand::~CursorSelectCommand() { - if (execRes) { - PQclear(execRes); - } if (executed && PQtransactionStatus(c->conn) != PQTRANS_INERROR) { c->checkResultFree((PQexec(c->conn, s_close.c_str())), PGRES_COMMAND_OK); } @@ -65,10 +61,7 @@ PQ::CursorSelectCommand::execute() PQexecParams(c->conn, s_declare.c_str(), values.size(), NULL, &values.front(), &lengths.front(), NULL, 0), PGRES_COMMAND_OK); fetchTuples(); - unsigned int nFields = PQnfields(execRes); - for (unsigned int f = 0; f < nFields; f += 1) { - insertColumn(DB::ColumnPtr(new Column(this, f))); - } + createColumns(execRes); executed = true; } } diff --git a/libpqpp/pq-cursorselectcommand.h b/libpqpp/pq-cursorselectcommand.h index 6182d3f..9766f86 100644 --- a/libpqpp/pq-cursorselectcommand.h +++ b/libpqpp/pq-cursorselectcommand.h @@ -1,7 +1,6 @@ #ifndef PQ_CURSORSELECTCOMMAND_H #define PQ_CURSORSELECTCOMMAND_H -#include #include "pq-selectbase.h" #include "pq-command.h" #include @@ -10,7 +9,7 @@ namespace PQ { class Connection; class Column; - class CursorSelectCommand : public DB::SelectCommand, public SelectBase, public Command { + class CursorSelectCommand : public SelectBase, public Command { public: CursorSelectCommand(Connection *, const std::string & sql, unsigned int no); virtual ~CursorSelectCommand(); diff --git a/libpqpp/pq-selectbase.cpp b/libpqpp/pq-selectbase.cpp index 1ede6b8..7dbdb4f 100644 --- a/libpqpp/pq-selectbase.cpp +++ b/libpqpp/pq-selectbase.cpp @@ -1,9 +1,28 @@ #include "pq-selectbase.h" +#include "pq-column.h" -PQ::SelectBase::SelectBase() : +PQ::SelectBase::SelectBase(const std::string & sql) : + DB::Command(sql), + DB::SelectCommand(sql), nTuples(0), tuple(0), execRes(NULL) { } +PQ::SelectBase::~SelectBase() +{ + if (execRes) { + PQclear(execRes); + } +} + +void +PQ::SelectBase::createColumns(PGresult * execRes) +{ + unsigned int nFields = PQnfields(execRes); + for (unsigned int f = 0; f < nFields; f += 1) { + insertColumn(DB::ColumnPtr(new Column(this, f))); + } +} + diff --git a/libpqpp/pq-selectbase.h b/libpqpp/pq-selectbase.h index 217a9e7..a6b199a 100644 --- a/libpqpp/pq-selectbase.h +++ b/libpqpp/pq-selectbase.h @@ -2,14 +2,17 @@ #define PQ_SELECTBASE_H #include +#include namespace PQ { - class SelectBase { + class SelectBase : public DB::SelectCommand { friend class Column; protected: - SelectBase(); - ~SelectBase() = default; + SelectBase(const std::string & sql); + ~SelectBase(); + + void createColumns(PGresult *); int nTuples, tuple; PGresult * execRes; -- cgit v1.2.3