summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-02-02 10:49:10 +0000
committerrandomdan <randomdan@localhost>2011-02-02 10:49:10 +0000
commitcb20ea40cbd1a6678c5d87795f999215ba53c120 (patch)
tree7b0fdd4367fd76b462b159caaa106d91cd040923
parentAdd lots of funky stuff to make the system (internally) type safe (diff)
downloadproject2-cb20ea40cbd1a6678c5d87795f999215ba53c120.tar.bz2
project2-cb20ea40cbd1a6678c5d87795f999215ba53c120.tar.xz
project2-cb20ea40cbd1a6678c5d87795f999215ba53c120.zip
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
-rw-r--r--project2/Jamfile.jam2
-rw-r--r--project2/dumpTask.cpp7
-rw-r--r--project2/genericVisitor.h31
-rw-r--r--project2/sqlRows.cpp30
-rw-r--r--project2/sqlVariableBinder.cpp25
-rw-r--r--project2/sqlVariableBinder.h3
-rw-r--r--project2/variables.h7
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++ : : <name>fcgi++ ;
lib odbc : : <name>odbc ;
lib boost_regex : : <name>boost_regex ;
lib boost_filesystem : : <name>boost_filesystem ;
+lib boost_date_time : : <name>boost_date_time ;
lib cgicc : : <name>cgicc ;
lib esmtp : : <name>esmtp ;
lib curl : : <name>curl ;
@@ -37,6 +38,7 @@ lib p2common :
<library>../libmisc//misc
<library>libxmlpp
<library>boost_filesystem
+ <library>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 <boost/foreach.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
#include <stdio.h>
ElementLoaderImpl<DumpTask> dumptaskLoader("dumptask");
@@ -59,6 +60,12 @@ class Printer : public boost::static_visitor<> {
void operator()(const boost::shared_ptr<const Glib::ustring> & 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<const boost::posix_time::ptime> & 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 <boost/lexical_cast.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
namespace LexicalVisitorHelper {
template <typename T, typename S, typename R>
@@ -54,28 +55,36 @@ namespace LexicalVisitorHelper {
return func(i.c_str());
}
};
- template <typename T, typename R>
- class lexical_run<T, const T *, R> {
+ template <typename R>
+ class lexical_run<Glib::ustring, boost::shared_ptr<const boost::posix_time::ptime>, R> {
public:
- R operator()(const boost::function1<R, T> & func, const T * i) const
+ R operator()(const boost::function1<R, Glib::ustring> & func, const boost::shared_ptr<const boost::posix_time::ptime> & i) const
{
- return func(*i);
+ return func(boost::posix_time::to_iso_extended_string(*i));
}
};
template <typename R>
- class lexical_run<const unsigned char *, const Glib::ustring *, R> {
+ class lexical_run<Glib::ustring, const boost::posix_time::ptime, R> {
public:
- R operator()(const boost::function1<R, const unsigned char *> & func, const Glib::ustring * i) const
+ R operator()(const boost::function1<R, Glib::ustring> & func, const boost::posix_time::ptime & i) const
{
- return func(reinterpret_cast<const unsigned char *>(i->c_str()));
+ return func(boost::posix_time::to_iso_extended_string(i));
}
};
- template <typename R>
- class lexical_run<const char *, const Glib::ustring *, R> {
+ template <typename S, typename R>
+ class lexical_run<const char *, boost::shared_ptr<const S>, R> {
public:
- R operator()(const boost::function1<R, const char *> & func, const Glib::ustring * i) const
+ R operator()(const boost::function1<R, const char *> & func, const boost::shared_ptr<const S> & i) const
{
- return func(i->c_str());
+ return func(boost::lexical_cast<Glib::ustring>(*i).c_str());
+ }
+ };
+ template <typename S, typename R>
+ class lexical_run<const unsigned char *, boost::shared_ptr<const S>, R> {
+ public:
+ R operator()(const boost::function1<R, const unsigned char *> & func, const boost::shared_ptr<const S> & i) const
+ {
+ return func(reinterpret_cast<const unsigned char *>(boost::lexical_cast<Glib::ustring>(*i).c_str()));
}
};
template <typename T, typename R>
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 <boost/date_time/gregorian/gregorian_types.hpp>
ElementLoaderImpl<SqlRows> 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<char> & c) {
+ variable = boost::shared_ptr<Glib::ustring>(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<boost::posix_time::ptime>(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 <boost/date_time/posix_time/posix_time_duration.hpp>
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<const Glib::ustring> & 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<const boost::posix_time::ptime> & 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 <boost/variant.hpp>
#include <boost/shared_ptr.hpp>
+#include <boost/date_time/posix_time/ptime.hpp>
#include <glibmm/ustring.h>
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<const boost::posix_time::ptime> & 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 <boost/intrusive_ptr.hpp>
#include <boost/optional.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <stdint.h>
#include <glibmm/ustring.h>
#include <libxml++/nodes/element.h>
@@ -26,8 +27,10 @@ typedef boost::variant<
int,
short int,
double,
- float
- // DateTimes (todo)
+ float,
+ // DateTimes
+ boost::posix_time::ptime,
+ boost::shared_ptr<const boost::posix_time::ptime>
> VariableType;
class VariableImpl : public virtual IntrusivePtrBase {