summaryrefslogtreecommitdiff
path: root/libpqpp
diff options
context:
space:
mode:
authorDan Goodliffe <daniel.goodliffe@pressassociation.com>2017-01-03 15:03:05 +0000
committerDan Goodliffe <daniel.goodliffe@pressassociation.com>2017-01-03 15:16:54 +0000
commitbac96d1b62dde949412a998cb0c3231ad179d870 (patch)
treeea7a72996c508d2c62104fe3ce523453b8f57d6b /libpqpp
parentOnly set unlogged for mocked tables on server versions 9.5 and above (diff)
downloadlibdbpp-postgresql-bac96d1b62dde949412a998cb0c3231ad179d870.tar.bz2
libdbpp-postgresql-bac96d1b62dde949412a998cb0c3231ad179d870.tar.xz
libdbpp-postgresql-bac96d1b62dde949412a998cb0c3231ad179d870.zip
Refactor to remove some duplication
Diffstat (limited to 'libpqpp')
-rw-r--r--libpqpp/pq-bulkselectcommand.cpp14
-rw-r--r--libpqpp/pq-bulkselectcommand.h4
-rw-r--r--libpqpp/pq-cursorselectcommand.cpp11
-rw-r--r--libpqpp/pq-cursorselectcommand.h3
-rw-r--r--libpqpp/pq-selectbase.cpp21
-rw-r--r--libpqpp/pq-selectbase.h9
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 <selectcommand.h>
#include "pq-selectbase.h"
#include "pq-prepared.h"
#include <vector>
@@ -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 <compileTimeFormatter.h>
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 <selectcommand.h>
#include "pq-selectbase.h"
#include "pq-command.h"
#include <vector>
@@ -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 <libpq-fe.h>
+#include <selectcommand.h>
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;