From 1eb9007542b1cbfad3e7089d0273ac32b4756d4e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 24 Sep 2015 02:22:49 +0100 Subject: Add operator>> support to columns --- libdbpp/column.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ libdbpp/column.h | 7 +++++++ 2 files changed, 51 insertions(+) diff --git a/libdbpp/column.cpp b/libdbpp/column.cpp index 91d26f5..81150a5 100644 --- a/libdbpp/column.cpp +++ b/libdbpp/column.cpp @@ -1,4 +1,6 @@ #include "column.h" +#include +#include DB::Column::Column(const Glib::ustring & n, unsigned int i) : colNo(i), @@ -10,4 +12,46 @@ DB::Column::~Column() { } +template +class Extract : public DB::HandleField { + public: + Extract(T & t) : target(t) { } + + void floatingpoint(double v) override { (*this)(v); } + void integer(int64_t v) override { (*this)(v); } + void boolean(bool v) override { (*this)(v); } + void string(const char * v, size_t len) override { (*this)(std::string(v, len)); } + void timestamp(const boost::posix_time::ptime & v) override { (*this)(v); } + void interval(const boost::posix_time::time_duration & v) override { (*this)(v); } + void null() override { } + + template + void operator()(const D &, + typename boost::disable_if, dummy>::type = 0) { + throw std::runtime_error("Invalid type conversion"); + } + + template + void operator()(const D & v, + typename boost::enable_if, dummy>::type = 0) { + target = (T)v; + } + + T & target; +}; + +#define COLUMNINTO(Type) \ +void \ +DB::Column::operator>>(Type & v) const \ +{ \ + Extract e(v); \ + apply(e); \ +} + +COLUMNINTO(bool); +COLUMNINTO(int64_t); +COLUMNINTO(double); +COLUMNINTO(std::string); +COLUMNINTO(boost::posix_time::ptime); +COLUMNINTO(boost::posix_time::time_duration); diff --git a/libdbpp/column.h b/libdbpp/column.h index 47492d1..c94cbe3 100644 --- a/libdbpp/column.h +++ b/libdbpp/column.h @@ -27,6 +27,13 @@ namespace DB { virtual void apply(HandleField &) const = 0; virtual void rebind(Command *, unsigned int) const = 0; + void operator>>(std::string &) const; + void operator>>(bool &) const; + void operator>>(int64_t &) const; + void operator>>(double &) const; + void operator>>(boost::posix_time::time_duration &) const; + void operator>>(boost::posix_time::ptime &) const; + const unsigned int colNo; const Glib::ustring name; }; -- cgit v1.2.3