From cb20ea40cbd1a6678c5d87795f999215ba53c120 Mon Sep 17 00:00:00 2001 From: randomdan Date: Wed, 2 Feb 2011 10:49:10 +0000 Subject: Remove compose functions on column data and add a handle function for type safe data passing Use new handle function to get type safe data from ODBC Add a datetime option to variables --- project2/Jamfile.jam | 2 ++ project2/dumpTask.cpp | 7 +++++++ project2/genericVisitor.h | 31 ++++++++++++++++++++----------- project2/sqlRows.cpp | 30 ++++++++++++++++++++++++++++-- project2/sqlVariableBinder.cpp | 25 +++++++++++++++++++++++-- project2/sqlVariableBinder.h | 3 +++ project2/variables.h | 7 +++++-- 7 files changed, 88 insertions(+), 17 deletions(-) diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index 0bec35a..e2eaf42 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -13,6 +13,7 @@ lib fcgi++ : : fcgi++ ; lib odbc : : odbc ; lib boost_regex : : boost_regex ; lib boost_filesystem : : boost_filesystem ; +lib boost_date_time : : boost_date_time ; lib cgicc : : cgicc ; lib esmtp : : esmtp ; lib curl : : curl ; @@ -37,6 +38,7 @@ lib p2common : ../libmisc//misc libxmlpp boost_filesystem + boost_date_time ; lib p2xml : diff --git a/project2/dumpTask.cpp b/project2/dumpTask.cpp index e424dfc..5dfeba6 100644 --- a/project2/dumpTask.cpp +++ b/project2/dumpTask.cpp @@ -2,6 +2,7 @@ #include "rowSet.h" #include "xmlObjectLoader.h" #include +#include #include ElementLoaderImpl dumptaskLoader("dumptask"); @@ -59,6 +60,12 @@ class Printer : public boost::static_visitor<> { void operator()(const boost::shared_ptr & i) const { fprintf(stderr, "'%s'", i->c_str()); } + void operator()(const boost::posix_time::ptime & i) const { + fprintf(stderr, "[%s]", boost::posix_time::to_iso_extended_string(i).c_str()); + } + void operator()(const boost::shared_ptr & i) const { + fprintf(stderr, "[%s]", boost::posix_time::to_iso_extended_string(*i).c_str()); + } }; void diff --git a/project2/genericVisitor.h b/project2/genericVisitor.h index be9b462..2e66e19 100644 --- a/project2/genericVisitor.h +++ b/project2/genericVisitor.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace LexicalVisitorHelper { template @@ -54,28 +55,36 @@ namespace LexicalVisitorHelper { return func(i.c_str()); } }; - template - class lexical_run { + template + class lexical_run, R> { public: - R operator()(const boost::function1 & func, const T * i) const + R operator()(const boost::function1 & func, const boost::shared_ptr & i) const { - return func(*i); + return func(boost::posix_time::to_iso_extended_string(*i)); } }; template - class lexical_run { + class lexical_run { public: - R operator()(const boost::function1 & func, const Glib::ustring * i) const + R operator()(const boost::function1 & func, const boost::posix_time::ptime & i) const { - return func(reinterpret_cast(i->c_str())); + return func(boost::posix_time::to_iso_extended_string(i)); } }; - template - class lexical_run { + template + class lexical_run, R> { public: - R operator()(const boost::function1 & func, const Glib::ustring * i) const + R operator()(const boost::function1 & func, const boost::shared_ptr & i) const { - return func(i->c_str()); + return func(boost::lexical_cast(*i).c_str()); + } + }; + template + class lexical_run, R> { + public: + R operator()(const boost::function1 & func, const boost::shared_ptr & i) const + { + return func(reinterpret_cast(boost::lexical_cast(*i).c_str())); } }; template diff --git a/project2/sqlRows.cpp b/project2/sqlRows.cpp index 0017352..d4c6cd8 100644 --- a/project2/sqlRows.cpp +++ b/project2/sqlRows.cpp @@ -9,6 +9,7 @@ #include "xmlObjectLoader.h" #include "commonObjects.h" #include "sqlVariableBinder.h" +#include ElementLoaderImpl sqlviewLoader("sqlrows"); @@ -41,16 +42,41 @@ SqlRows::setFilter(const Glib::ustring & name) } } +class HandleAsVariableType : public ODBC::HandleField { + public: + void null() { } + void string(const std::vector & c) { + variable = boost::shared_ptr(new Glib::ustring(&c[0], &c[c.size()])); + } + void integer(SQLINTEGER i) { + variable = i; + } + void floatingpoint(SQLDOUBLE d) { + variable = d; + } + void timestamp(const SQL_TIMESTAMP_STRUCT & t) + { + variable = boost::shared_ptr(new boost::posix_time::ptime( + boost::gregorian::date(t.year, t.month, t.day), + boost::posix_time::time_duration(t.hour, t.minute, t.second, t.fraction))); + } + VariableType variable; +}; + VariableType SqlRows::getCurrentValue(const Glib::ustring & id) const { - return (*query)[id].compose(); + HandleAsVariableType h; + (*query)[id].apply(h); + return h.variable; } VariableType SqlRows::getCurrentValue(unsigned int col) const { - return (*query)[col].compose(); + HandleAsVariableType h; + (*query)[col].apply(h); + return h.variable; } bool diff --git a/project2/sqlVariableBinder.cpp b/project2/sqlVariableBinder.cpp index f1e70da..13912f3 100644 --- a/project2/sqlVariableBinder.cpp +++ b/project2/sqlVariableBinder.cpp @@ -1,5 +1,6 @@ #include "sqlVariableBinder.h" #include "command.h" +#include OdbcVariableBinder::OdbcVariableBinder(ODBC::Command * c, unsigned int i) : cmd(c), @@ -14,7 +15,8 @@ OdbcVariableBinder::operator()(const Glib::ustring & i) const void OdbcVariableBinder::operator()(const boost::shared_ptr & i) const { - cmd->bindParamS(idx, *i); + if (!i) return cmd->bindNull(idx); + (*this)(*i); } void OdbcVariableBinder::operator()(const long long unsigned int & i) const @@ -66,5 +68,24 @@ OdbcVariableBinder::operator()(const float & i) const { cmd->bindParamF(idx, i); } - +void +OdbcVariableBinder::operator()(const boost::posix_time::ptime & i) const +{ + SQL_TIMESTAMP_STRUCT t; + boost::gregorian::date::ymd_type ymd(i.date().year_month_day()); + t.year = ymd.year; + t.month = ymd.month; + t.day = ymd.day; + t.hour = i.time_of_day().hours(); + t.minute = i.time_of_day().minutes(); + t.second = i.time_of_day().seconds(); + t.fraction = i.time_of_day().fractional_seconds(); + cmd->bindParamT(idx, t); +} +void +OdbcVariableBinder::operator()(const boost::shared_ptr & i) const +{ + if (!i) return cmd->bindNull(idx); + (*this)(*i); +} diff --git a/project2/sqlVariableBinder.h b/project2/sqlVariableBinder.h index ace2028..f175be1 100644 --- a/project2/sqlVariableBinder.h +++ b/project2/sqlVariableBinder.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace ODBC { @@ -23,6 +24,8 @@ class OdbcVariableBinder : public boost::static_visitor<> { void operator()(const short int & i) const; void operator()(const double & i) const; void operator()(const float & i) const; + void operator()(const boost::posix_time::ptime & i) const; + void operator()(const boost::shared_ptr & i) const; private: ODBC::Command * cmd; diff --git a/project2/variables.h b/project2/variables.h index 469baa5..0b27419 100644 --- a/project2/variables.h +++ b/project2/variables.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -26,8 +27,10 @@ typedef boost::variant< int, short int, double, - float - // DateTimes (todo) + float, + // DateTimes + boost::posix_time::ptime, + boost::shared_ptr > VariableType; class VariableImpl : public virtual IntrusivePtrBase { -- cgit v1.2.3