diff options
author | randomdan <randomdan@localhost> | 2011-02-09 01:33:33 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-02-09 01:33:33 +0000 |
commit | cfa36aa5466d4c342c7de0f6cea7b8386984fd36 (patch) | |
tree | 17a13c13b3588101a146705839d5da6f280bddb9 /libpqpp/selectcommand.cpp | |
parent | Centralise the ICE splicer (diff) | |
download | libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.tar.bz2 libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.tar.xz libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.zip |
Fix the build system to do dependencies properly
Break down libodbcpp into a set of base classes; libdbpp
Add a native PostgreSQL implementation of libdbpp; libpqpp
Extend project2 rdbms stuff to work with generic connectors
Update datasources to specify connector type
Build libmisc as .so
Diffstat (limited to 'libpqpp/selectcommand.cpp')
-rw-r--r-- | libpqpp/selectcommand.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/libpqpp/selectcommand.cpp b/libpqpp/selectcommand.cpp new file mode 100644 index 0000000..603cffd --- /dev/null +++ b/libpqpp/selectcommand.cpp @@ -0,0 +1,94 @@ +#include "selectcommand.h" +#include "connection.h" +#include "column.h" +#include "error.h" + +PQ::SelectCommand::SelectCommand(const Connection * conn, const std::string & sql, unsigned int no) : + DB::Command(sql), + DB::SelectCommand(sql), + PQ::Command(conn, sql, no), + executed(false), + nTuples(0), + tuple(0), + execRes(NULL) +{ +} + +PQ::SelectCommand::~SelectCommand() +{ + if (execRes) { + PQclear(execRes); + } + for (unsigned int f = 0; f < fields.size(); f += 1) { + delete fields[f]; + } +} + +void +PQ::SelectCommand::execute() +{ + if (!executed) { + prepare(); + execRes = PQexecPrepared(c->conn, stmntName.c_str(), values.size(), &values.front(), &lengths.front(), &formats.front(), 0); + c->checkResult(execRes, PGRES_TUPLES_OK, __PRETTY_FUNCTION__); + 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; + } + nTuples = PQntuples(execRes); + tuple = -1; + executed = true; + } +} + +bool +PQ::SelectCommand::fetch() +{ + execute(); + if (tuple++ < (nTuples - 1)) { + return true; + } + else { + executed = false; + return false; + } +} + +const DB::Column& +PQ::SelectCommand::operator[](unsigned int n) const +{ + if (n < fields.size()) { + return *fields[n]; + } + throw Error(); +} + +const DB::Column& +PQ::SelectCommand::operator[](const Glib::ustring & n) const +{ + std::map<Glib::ustring, Column *>::const_iterator i = fieldsName.find(n); + if (i != fieldsName.end()) { + return *i->second; + } + throw Error(); +} + +unsigned int +PQ::SelectCommand::getOrdinal(const Glib::ustring & n) const +{ + std::map<Glib::ustring, Column *>::const_iterator i = fieldsName.find(n); + if (i != fieldsName.end()) { + return i->second->colNo; + } + throw Error(); +} + +unsigned int +PQ::SelectCommand::columnCount() const +{ + return fields.size(); +} + |