summaryrefslogtreecommitdiff
path: root/libpqpp/pq-bulkselectcommand.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-04-24 15:47:33 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2016-04-24 15:47:33 +0100
commit6340b06110c35b92bfcc780e85537ca5a752a58c (patch)
tree90e66849ef16ee7fac3ae0d00900ae684996002c /libpqpp/pq-bulkselectcommand.cpp
parentIntroduce select base for different kinds of select (diff)
downloadlibdbpp-postgresql-6340b06110c35b92bfcc780e85537ca5a752a58c.tar.bz2
libdbpp-postgresql-6340b06110c35b92bfcc780e85537ca5a752a58c.tar.xz
libdbpp-postgresql-6340b06110c35b92bfcc780e85537ca5a752a58c.zip
Support bulk selects which don't use cursors, always use of RETURNING
Diffstat (limited to 'libpqpp/pq-bulkselectcommand.cpp')
-rw-r--r--libpqpp/pq-bulkselectcommand.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/libpqpp/pq-bulkselectcommand.cpp b/libpqpp/pq-bulkselectcommand.cpp
new file mode 100644
index 0000000..27674f5
--- /dev/null
+++ b/libpqpp/pq-bulkselectcommand.cpp
@@ -0,0 +1,53 @@
+#include "pq-bulkselectcommand.h"
+#include "pq-connection.h"
+#include "pq-column.h"
+#include "pq-error.h"
+
+PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, unsigned int no) :
+ DB::Command(sql),
+ DB::SelectCommand(sql),
+ PQ::Command(conn, sql, no),
+ executed(false)
+{
+ prepareSql(preparedSql, sql);
+}
+
+PQ::BulkSelectCommand::~BulkSelectCommand()
+{
+ if (execRes) {
+ PQclear(execRes);
+ }
+}
+
+void
+PQ::BulkSelectCommand::execute()
+{
+ if (!executed) {
+ execRes = c->checkResult(
+ PQexecParams(c->conn, preparedSql.c_str(), values.size(), NULL, &values.front(), &lengths.front(), NULL, 0),
+ 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)));
+ }
+ executed = true;
+ }
+}
+
+bool
+PQ::BulkSelectCommand::fetch()
+{
+ execute();
+ if (++tuple < nTuples) {
+ return true;
+ }
+ else {
+ PQclear(execRes);
+ execRes = NULL;
+ executed = false;
+ return false;
+ }
+}
+