From 55e4104bb02ff89aa6d613896bbab88c6a4fe418 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 5 Jun 2017 18:48:07 +0100 Subject: Add wrappers for PQgetvalue and PQgetlength in column to simplify access --- libpqpp/pq-column.cpp | 32 ++++++++++++++++++++++---------- libpqpp/pq-column.h | 7 +++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/libpqpp/pq-column.cpp b/libpqpp/pq-column.cpp index 92107e8..6dbbfd2 100644 --- a/libpqpp/pq-column.cpp +++ b/libpqpp/pq-column.cpp @@ -25,6 +25,18 @@ PQ::Column::isNull() const return PQgetisnull(sc->execRes, sc->tuple, colNo); } +int +PQ::Column::length() const +{ + return PQgetlength(sc->execRes, sc->tuple, colNo); +} + +const char * +PQ::Column::value() const +{ + return PQgetvalue(sc->execRes, sc->tuple, colNo); +} + void PQ::Column::apply(DB::HandleField & h) const { @@ -37,43 +49,43 @@ PQ::Column::apply(DB::HandleField & h) const case 1043: //VARCHAROID: case 25: //TEXTOID: case 142: //XMLOID: - h.string(PQgetvalue(sc->execRes, sc->tuple, colNo), PQgetlength(sc->execRes, sc->tuple, colNo)); + h.string(value(), length()); break; case 16: //BOOLOID: - h.boolean(PQgetvalue(sc->execRes, sc->tuple, colNo)[0] == 't' ? 1 : 0); + h.boolean(value()[0] == 't'); break; case 21: //INT2OID: case 23: //INT4OID: case 20: //INT8OID: - h.integer(atol(PQgetvalue(sc->execRes, sc->tuple, colNo))); + h.integer(atol(value())); break; case 1700: //NUMERICOID: case 700: //FLOAT4OID: case 701: //FLOAT8OID: - h.floatingpoint(atof(PQgetvalue(sc->execRes, sc->tuple, colNo))); + h.floatingpoint(atof(value())); break; case 704: //TINTERVALOID case 1083: //TIMEOID: case 1186: //INTERVALOID { int days = 0, hours = 0, minutes = 0, seconds = 0, fractions = 0, flen1 = 0, flen2 = 0; - const char * val = PQgetvalue(sc->execRes, sc->tuple, colNo); + const char * val = value(); if (sscanf(val, "%d %*[days] %d:%d:%d.%n%d%n", &days, &hours, &minutes, &seconds, &flen1, &fractions, &flen2) >= 4) { h.interval(boost::posix_time::time_duration((24 * days) + hours, minutes, seconds, fractions * pow(10, boost::posix_time::time_res_traits::num_fractional_digits() + flen1 - flen2))); } else { - h.interval(boost::posix_time::duration_from_string(PQgetvalue(sc->execRes, sc->tuple, colNo))); + h.interval(boost::posix_time::duration_from_string(value())); } break; } case 1082: //DATEOID: h.timestamp(boost::posix_time::ptime( - boost::gregorian::from_string(PQgetvalue(sc->execRes, sc->tuple, colNo)))); + boost::gregorian::from_string(value()))); break; case 702: //ABSTIMEOID: case 1114: //TIMESTAMPOID: case 1184: //TIMESTAMPTZOID: - h.timestamp(boost::posix_time::time_from_string(PQgetvalue(sc->execRes, sc->tuple, colNo))); + h.timestamp(boost::posix_time::time_from_string(value())); break; case 17: //BYTEAOID { @@ -81,12 +93,12 @@ PQ::Column::apply(DB::HandleField & h) const PQfreemem(buf); } size_t len; - buf = PQunescapeBytea((unsigned char *)PQgetvalue(sc->execRes, sc->tuple, colNo), &len); + buf = PQunescapeBytea(valueAsPtr(), &len); h.blob(DB::Blob(buf, len)); break; } default: - h.string(PQgetvalue(sc->execRes, sc->tuple, colNo), PQgetlength(sc->execRes, sc->tuple, colNo)); + h.string(value(), length()); } } diff --git a/libpqpp/pq-column.h b/libpqpp/pq-column.h index 08e66a6..f6050ea 100644 --- a/libpqpp/pq-column.h +++ b/libpqpp/pq-column.h @@ -15,6 +15,13 @@ namespace PQ { void apply(DB::HandleField &) const override; protected: + template + inline T valueAs() const { return *(T*)(value()); } + template + inline T * valueAsPtr() const { return (T*)(value()); } + const char * value() const; + int length() const; + const SelectBase * sc; const Oid oid; // Buffer for PQunescapeBytea -- cgit v1.2.3