diff options
-rw-r--r-- | libdbpp/column.cpp | 44 | ||||
-rw-r--r-- | libdbpp/column.h | 7 |
2 files changed, 51 insertions, 0 deletions
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 <boost/utility/enable_if.hpp> +#include <exception> DB::Column::Column(const Glib::ustring & n, unsigned int i) : colNo(i), @@ -10,4 +12,46 @@ DB::Column::~Column() { } +template<typename T> +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 <typename D, typename dummy = int> + void operator()(const D &, + typename boost::disable_if<std::is_convertible<D, T>, dummy>::type = 0) { + throw std::runtime_error("Invalid type conversion"); + } + + template <typename D, typename dummy = int> + void operator()(const D & v, + typename boost::enable_if<std::is_convertible<D, T>, dummy>::type = 0) { + target = (T)v; + } + + T & target; +}; + +#define COLUMNINTO(Type) \ +void \ +DB::Column::operator>>(Type & v) const \ +{ \ + Extract<Type> 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; }; |