summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2013-11-29 23:44:42 +0000
committerrandomdan <randomdan@localhost>2013-11-29 23:44:42 +0000
commit6bdb34223db0dd71dac329b67a5cd985efb8ebe8 (patch)
treef078a41f099b56d6f17dd81463335d9b45cd6989
parentUse CXX from the environment (diff)
downloadlibdbpp-postgresql-6bdb34223db0dd71dac329b67a5cd985efb8ebe8.tar.bz2
libdbpp-postgresql-6bdb34223db0dd71dac329b67a5cd985efb8ebe8.tar.xz
libdbpp-postgresql-6bdb34223db0dd71dac329b67a5cd985efb8ebe8.zip
Fetch first rows in execute, allows population of fields before fetch()
-rw-r--r--libpqpp/selectcommand.cpp39
-rw-r--r--libpqpp/selectcommand.h1
2 files changed, 23 insertions, 17 deletions
diff --git a/libpqpp/selectcommand.cpp b/libpqpp/selectcommand.cpp
index 3798406..23be577 100644
--- a/libpqpp/selectcommand.cpp
+++ b/libpqpp/selectcommand.cpp
@@ -58,34 +58,39 @@ PQ::SelectCommand::execute()
}
c->beginTx();
txOpened = true;
- c->checkResultFree(
+ c->checkResult(
PQexecParams(c->conn, psql.c_str(), values.size(), NULL, &values.front(), &lengths.front(), &formats.front(), 0),
PGRES_COMMAND_OK);
+ fetchTuples();
+ unsigned int nFields = PQnfields(execRes);
+ fields.resize(nFields);
+ for (unsigned int f = 0; f < nFields; f += 1) {
+ Column * c = new Column(this, f);
+ fields[f] = c;
+ fieldsName[c->name] = c;
+ }
executed = true;
}
}
+void
+PQ::SelectCommand::fetchTuples()
+{
+ if (execRes) {
+ PQclear(execRes);
+ }
+ execRes = NULL;
+ execRes = c->checkResult(PQexec(c->conn, ("FETCH 35 IN " + stmntName).c_str()), PGRES_TUPLES_OK);
+ nTuples = PQntuples(execRes);
+ tuple = -1;
+}
+
bool
PQ::SelectCommand::fetch()
{
execute();
if (tuple >= (nTuples - 1)) {
- if (execRes) {
- PQclear(execRes);
- }
- execRes = NULL;
- execRes = c->checkResult(PQexec(c->conn, ("FETCH 35 IN " + stmntName).c_str()), PGRES_TUPLES_OK);
- nTuples = PQntuples(execRes);
- tuple = -1;
- }
- if (fields.empty()) {
- unsigned int nFields = PQnfields(execRes);
- fields.resize(nFields);
- for (unsigned int f = 0; f < nFields; f += 1) {
- Column * c = new Column(this, f);
- fields[f] = c;
- fieldsName[c->name] = c;
- }
+ fetchTuples();
}
if (tuple++ < (nTuples - 1)) {
return true;
diff --git a/libpqpp/selectcommand.h b/libpqpp/selectcommand.h
index 507bf43..f4774d7 100644
--- a/libpqpp/selectcommand.h
+++ b/libpqpp/selectcommand.h
@@ -21,6 +21,7 @@ namespace PQ {
unsigned int columnCount() const;
unsigned int getOrdinal(const Glib::ustring&) const;
private:
+ void fetchTuples();
mutable bool executed;
mutable bool txOpened;
std::vector<Column *> fields;