From eab3a1beba707e9f531a250a5c74a6ba462aa4fc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 24 Apr 2016 14:34:16 +0100 Subject: Rename SelectCommand to CursorSelectCommand --- libpqpp/pq-column.cpp | 4 +- libpqpp/pq-column.h | 6 +- libpqpp/pq-connection.cpp | 6 +- libpqpp/pq-cursorselectcommand.cpp | 113 +++++++++++++++++++++++++++++++++++++ libpqpp/pq-cursorselectcommand.h | 40 +++++++++++++ libpqpp/pq-selectcommand.cpp | 113 ------------------------------------- libpqpp/pq-selectcommand.h | 40 ------------- 7 files changed, 161 insertions(+), 161 deletions(-) create mode 100644 libpqpp/pq-cursorselectcommand.cpp create mode 100644 libpqpp/pq-cursorselectcommand.h delete mode 100644 libpqpp/pq-selectcommand.cpp delete mode 100644 libpqpp/pq-selectcommand.h diff --git a/libpqpp/pq-column.cpp b/libpqpp/pq-column.cpp index 6b949d4..31a3d9b 100644 --- a/libpqpp/pq-column.cpp +++ b/libpqpp/pq-column.cpp @@ -1,10 +1,10 @@ #include "pq-column.h" -#include "pq-selectcommand.h" +#include "pq-cursorselectcommand.h" #include "pq-error.h" #include #include -PQ::Column::Column(const SelectCommand * s, unsigned int i) : +PQ::Column::Column(const CursorSelectCommand * s, unsigned int i) : DB::Column(PQfname(s->execRes, i), i), sc(s), oid(PQftype(sc->execRes, colNo)) diff --git a/libpqpp/pq-column.h b/libpqpp/pq-column.h index a5b33ef..99a37fb 100644 --- a/libpqpp/pq-column.h +++ b/libpqpp/pq-column.h @@ -5,16 +5,16 @@ #include namespace PQ { - class SelectCommand; + class CursorSelectCommand; class Column : public DB::Column { public: - Column(const SelectCommand *, unsigned int field); + Column(const CursorSelectCommand *, unsigned int field); bool isNull() const override; void apply(DB::HandleField &) const override; protected: - const SelectCommand * sc; + const CursorSelectCommand * sc; const Oid oid; }; } diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp index e6340d5..49867a4 100644 --- a/libpqpp/pq-connection.cpp +++ b/libpqpp/pq-connection.cpp @@ -1,6 +1,6 @@ #include "pq-connection.h" #include "pq-error.h" -#include "pq-selectcommand.h" +#include "pq-cursorselectcommand.h" #include "pq-modifycommand.h" #include #include @@ -97,7 +97,7 @@ PQ::Connection::ping() const DB::SelectCommand * PQ::Connection::newSelectCommand(const std::string & sql) { - return new SelectCommand(this, sql, pstmntNo++); + return new CursorSelectCommand(this, sql, pstmntNo++); } DB::ModifyCommand * @@ -173,7 +173,7 @@ PQ::Connection::bulkUploadData(const char * data, size_t len) const int64_t PQ::Connection::insertId() { - SelectCommand getId(this, "SELECT lastval()", pstmntNo++); + CursorSelectCommand getId(this, "SELECT lastval()", pstmntNo++); int64_t id = -1; while (getId.fetch()) { getId[0] >> id; diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp new file mode 100644 index 0000000..fe097a5 --- /dev/null +++ b/libpqpp/pq-cursorselectcommand.cpp @@ -0,0 +1,113 @@ +#include "pq-cursorselectcommand.h" +#include "pq-connection.h" +#include "pq-column.h" +#include "pq-error.h" + +PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::string & sql, unsigned int no) : + DB::Command(sql), + DB::SelectCommand(sql), + PQ::Command(conn, sql, no), + executed(false), + txOpened(false), + nTuples(0), + tuple(0), + fTuples(35), + execRes(NULL), + s_declare(mkdeclare()), + s_fetch(mkfetch()), + s_close(mkclose()) +{ +} + +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); + } + if (txOpened) { + c->commitTx(); + } +} + +std::string +PQ::CursorSelectCommand::mkdeclare() const +{ + std::string psql; + psql.reserve(sql.length() + 40); + psql += "DECLARE "; + psql += stmntName; + psql += " CURSOR FOR "; + prepareSql(psql, sql); + return psql; +} + +std::string +PQ::CursorSelectCommand::mkfetch() const +{ + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf), "FETCH %d IN %s", fTuples, stmntName.c_str()); + return buf; +} + +std::string +PQ::CursorSelectCommand::mkclose() const +{ + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf), "CLOSE %s", stmntName.c_str()); + return buf; +} + +void +PQ::CursorSelectCommand::execute() +{ + if (!executed) { + if (!c->inTx()) { + c->beginTx(); + txOpened = true; + } + execRes = c->checkResult( + 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))); + } + executed = true; + } +} + +void +PQ::CursorSelectCommand::fetchTuples() +{ + if (execRes) { + PQclear(execRes); + } + execRes = NULL; + execRes = c->checkResult(PQexec(c->conn, s_fetch.c_str()), PGRES_TUPLES_OK); + nTuples = PQntuples(execRes); + tuple = -1; +} + +bool +PQ::CursorSelectCommand::fetch() +{ + execute(); + if ((tuple >= (nTuples - 1)) && (nTuples == fTuples)) { + fetchTuples(); + } + if (tuple++ < (nTuples - 1)) { + return true; + } + else { + PQclear(PQexec(c->conn, s_close.c_str())); + PQclear(execRes); + execRes = NULL; + executed = false; + return false; + } +} + diff --git a/libpqpp/pq-cursorselectcommand.h b/libpqpp/pq-cursorselectcommand.h new file mode 100644 index 0000000..aafe4e1 --- /dev/null +++ b/libpqpp/pq-cursorselectcommand.h @@ -0,0 +1,40 @@ +#ifndef PQ_CURSORSELECTCOMMAND_H +#define PQ_CURSORSELECTCOMMAND_H + +#include +#include "pq-command.h" +#include +#include + +namespace PQ { + class Connection; + class Column; + class CursorSelectCommand : public DB::SelectCommand, public Command { + public: + CursorSelectCommand(Connection *, const std::string & sql, unsigned int no); + virtual ~CursorSelectCommand(); + + bool fetch() override; + void execute() override; + + private: + void fetchTuples(); + std::string mkdeclare() const; + std::string mkfetch() const; + std::string mkclose() const; + + mutable bool executed; + mutable bool txOpened; + int nTuples, tuple, fTuples; + PGresult * execRes; + std::string s_declare; + std::string s_fetch; + std::string s_close; + + friend class Column; + }; +} + +#endif + + diff --git a/libpqpp/pq-selectcommand.cpp b/libpqpp/pq-selectcommand.cpp deleted file mode 100644 index b049eea..0000000 --- a/libpqpp/pq-selectcommand.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "pq-selectcommand.h" -#include "pq-connection.h" -#include "pq-column.h" -#include "pq-error.h" - -PQ::SelectCommand::SelectCommand(Connection * conn, const std::string & sql, unsigned int no) : - DB::Command(sql), - DB::SelectCommand(sql), - PQ::Command(conn, sql, no), - executed(false), - txOpened(false), - nTuples(0), - tuple(0), - fTuples(35), - execRes(NULL), - s_declare(mkdeclare()), - s_fetch(mkfetch()), - s_close(mkclose()) -{ -} - -PQ::SelectCommand::~SelectCommand() -{ - if (execRes) { - PQclear(execRes); - } - if (executed && PQtransactionStatus(c->conn) != PQTRANS_INERROR) { - c->checkResultFree((PQexec(c->conn, s_close.c_str())), PGRES_COMMAND_OK); - } - if (txOpened) { - c->commitTx(); - } -} - -std::string -PQ::SelectCommand::mkdeclare() const -{ - std::string psql; - psql.reserve(sql.length() + 40); - psql += "DECLARE "; - psql += stmntName; - psql += " CURSOR FOR "; - prepareSql(psql, sql); - return psql; -} - -std::string -PQ::SelectCommand::mkfetch() const -{ - char buf[BUFSIZ]; - snprintf(buf, sizeof(buf), "FETCH %d IN %s", fTuples, stmntName.c_str()); - return buf; -} - -std::string -PQ::SelectCommand::mkclose() const -{ - char buf[BUFSIZ]; - snprintf(buf, sizeof(buf), "CLOSE %s", stmntName.c_str()); - return buf; -} - -void -PQ::SelectCommand::execute() -{ - if (!executed) { - if (!c->inTx()) { - c->beginTx(); - txOpened = true; - } - execRes = c->checkResult( - 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))); - } - executed = true; - } -} - -void -PQ::SelectCommand::fetchTuples() -{ - if (execRes) { - PQclear(execRes); - } - execRes = NULL; - execRes = c->checkResult(PQexec(c->conn, s_fetch.c_str()), PGRES_TUPLES_OK); - nTuples = PQntuples(execRes); - tuple = -1; -} - -bool -PQ::SelectCommand::fetch() -{ - execute(); - if ((tuple >= (nTuples - 1)) && (nTuples == fTuples)) { - fetchTuples(); - } - if (tuple++ < (nTuples - 1)) { - return true; - } - else { - PQclear(PQexec(c->conn, s_close.c_str())); - PQclear(execRes); - execRes = NULL; - executed = false; - return false; - } -} - diff --git a/libpqpp/pq-selectcommand.h b/libpqpp/pq-selectcommand.h deleted file mode 100644 index f2e0c74..0000000 --- a/libpqpp/pq-selectcommand.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef PQ_SELECTCOMMAND_H -#define PQ_SELECTCOMMAND_H - -#include -#include "pq-command.h" -#include -#include - -namespace PQ { - class Connection; - class Column; - class SelectCommand : public DB::SelectCommand, public Command { - public: - SelectCommand(Connection *, const std::string & sql, unsigned int no); - virtual ~SelectCommand(); - - bool fetch() override; - void execute() override; - - private: - void fetchTuples(); - std::string mkdeclare() const; - std::string mkfetch() const; - std::string mkclose() const; - - mutable bool executed; - mutable bool txOpened; - int nTuples, tuple, fTuples; - PGresult * execRes; - std::string s_declare; - std::string s_fetch; - std::string s_close; - - friend class Column; - }; -} - -#endif - - -- cgit v1.2.3