diff options
author | randomdan <randomdan@localhost> | 2014-03-03 20:57:27 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-03-03 20:57:27 +0000 |
commit | 04901adee3373a158c5326d0b597d8131513ce9a (patch) | |
tree | b2bbfd5bf670ec04c1ac2d8ffebfe2a0181d8e63 | |
parent | Don't create the app engine instance before forking (diff) | |
download | project2-04901adee3373a158c5326d0b597d8131513ce9a.tar.bz2 project2-04901adee3373a158c5326d0b597d8131513ce9a.tar.xz project2-04901adee3373a158c5326d0b597d8131513ce9a.zip |
Adds native support for time_duration as a variable type
Pass/retrieve boost::posix_time ptime and time_duration into/out of the db tier
-rw-r--r-- | project2/common/fileStrmVarWriter.cpp | 3 | ||||
-rw-r--r-- | project2/common/fileStrmVarWriter.h | 1 | ||||
-rw-r--r-- | project2/common/variableConvert.cpp | 40 | ||||
-rw-r--r-- | project2/common/variableType.cpp | 21 | ||||
-rw-r--r-- | project2/common/variableType.h | 4 | ||||
-rw-r--r-- | project2/json/conversion.cpp | 3 | ||||
-rw-r--r-- | project2/json/conversion.h | 1 | ||||
-rw-r--r-- | project2/sql/sqlCache.cpp | 7 | ||||
-rw-r--r-- | project2/sql/sqlHandleAsVariableType.cpp | 7 | ||||
-rw-r--r-- | project2/sql/sqlHandleAsVariableType.h | 3 | ||||
-rw-r--r-- | project2/sql/sqlTest.cpp | 7 | ||||
-rw-r--r-- | project2/sql/sqlVariableBinder.cpp | 8 | ||||
-rw-r--r-- | project2/sql/sqlVariableBinder.h | 1 | ||||
-rw-r--r-- | project2/xml/xmlPresenter.cpp | 7 | ||||
-rw-r--r-- | project2/xml/xmlPresenter.h | 1 |
15 files changed, 98 insertions, 16 deletions
diff --git a/project2/common/fileStrmVarWriter.cpp b/project2/common/fileStrmVarWriter.cpp index caaf908..cdd7818 100644 --- a/project2/common/fileStrmVarWriter.cpp +++ b/project2/common/fileStrmVarWriter.cpp @@ -38,6 +38,9 @@ void FileStreamVariableWriter::operator()(const Glib::ustring & i) const { } fputc('\'', out); } +void FileStreamVariableWriter::operator()(const boost::posix_time::time_duration & i) const { + fprintf(out, "[%s]", boost::posix_time::to_simple_string(i).c_str()); +} void FileStreamVariableWriter::operator()(const boost::posix_time::ptime & i) const { fprintf(out, "[%s]", boost::posix_time::to_iso_extended_string(i).c_str()); } diff --git a/project2/common/fileStrmVarWriter.h b/project2/common/fileStrmVarWriter.h index d0fb76f..dd43473 100644 --- a/project2/common/fileStrmVarWriter.h +++ b/project2/common/fileStrmVarWriter.h @@ -13,6 +13,7 @@ class FileStreamVariableWriter : public boost::static_visitor<> { void operator()(const double & i) const; void operator()(const Boolean & i) const; void operator()(const Glib::ustring & i) const; + void operator()(const boost::posix_time::time_duration & i) const; void operator()(const boost::posix_time::ptime & i) const; private: diff --git a/project2/common/variableConvert.cpp b/project2/common/variableConvert.cpp index 737a856..2c6e49a 100644 --- a/project2/common/variableConvert.cpp +++ b/project2/common/variableConvert.cpp @@ -29,6 +29,9 @@ class ConvertVisitorGlibUstring : public boost::static_visitor<const Glib::ustri const Glib::ustring & operator()(const Glib::ustring & r) const { return r; } + const Glib::ustring & operator()(const boost::posix_time::time_duration & r) const { + CONVERT(const Glib::ustring, boost::posix_time::to_simple_string); + } const Glib::ustring & operator()(const boost::posix_time::ptime & r) const { CONVERT(const Glib::ustring, boost::posix_time::to_iso_extended_string); } @@ -50,6 +53,9 @@ class ConvertVisitorStdString : public boost::static_visitor<const std::string & const std::string & operator()(const Glib::ustring & r) const { return r.raw(); } + const std::string & operator()(const boost::posix_time::time_duration & r) const { + CONVERTF(const Glib::ustring, boost::posix_time::to_simple_string, raw); + } const std::string & operator()(const boost::posix_time::ptime & r) const { CONVERTF(const Glib::ustring, boost::posix_time::to_iso_extended_string, raw); } @@ -71,6 +77,9 @@ class ConvertVisitorCharStar : public boost::static_visitor<const char *> { const char * operator()(const Glib::ustring & r) const { return r.c_str(); } + const char * operator()(const boost::posix_time::time_duration & r) const { + CONVERTF(const Glib::ustring, boost::posix_time::to_simple_string, c_str); + } const char * operator()(const boost::posix_time::ptime & r) const { CONVERTF(const Glib::ustring, boost::posix_time::to_iso_extended_string, c_str); } @@ -93,6 +102,9 @@ class ConvertVisitor : public boost::static_visitor<DestType> { DestType operator()(const Glib::ustring & r) const { CONVERT(DestType, boost::lexical_cast<DestType>); } + DestType operator()(const boost::posix_time::time_duration &) const { + throw InvalidConversionTo(typeid(DestType).name(), "<Time Duration>"); + } DestType operator()(const boost::posix_time::ptime &) const { throw InvalidConversionTo(typeid(DestType).name(), "<Date Time>"); } @@ -109,6 +121,27 @@ class ConvertVisitor : public boost::static_visitor<DestType> { private: const VariableType * var; }; +// Convert to time duration +class ConvertVisitorTimeDuration : public boost::static_visitor<const boost::posix_time::time_duration &> { + public: + ConvertVisitorTimeDuration(const VariableType * v) : var(v) { + } + const boost::posix_time::time_duration & operator()(const Glib::ustring & r) const { + CONVERT(const boost::posix_time::time_duration, boost::posix_time::duration_from_string); + } + const boost::posix_time::time_duration & operator()(const boost::posix_time::time_duration & t) const { + return t; + } + const boost::posix_time::time_duration & operator()(const Null &) const { + throw NullVariable(); + } + template <typename T> + const boost::posix_time::time_duration & operator()(const T &) const { + throw InvalidConversionTo("TimeDuration", typeid(T).name()); + } + private: + const VariableType * var; +}; // Convert to ptime class ConvertVisitorDateTime : public boost::static_visitor<const boost::posix_time::ptime &> { public: @@ -138,6 +171,9 @@ class ConvertVisitorBool : public boost::static_visitor<bool> { bool operator()(const Glib::ustring & r) const { CONVERT(bool, fromStr); } + bool operator()(const boost::posix_time::time_duration & t) const { + return (t.ticks() != 0); + } bool operator()(const boost::posix_time::ptime &) const { throw InvalidConversionTo("bool", "<Date time>"); } @@ -189,6 +225,10 @@ VariableType::operator double() const { return boost::apply_visitor(ConvertVisitor<double>(this), *this); } +VariableType::operator const boost::posix_time::time_duration &() const +{ + return boost::apply_visitor(ConvertVisitorTimeDuration(this), *this); +} VariableType::operator const boost::posix_time::ptime &() const { return boost::apply_visitor(ConvertVisitorDateTime(this), *this); diff --git a/project2/common/variableType.cpp b/project2/common/variableType.cpp index 775b2fd..b19d2fd 100644 --- a/project2/common/variableType.cpp +++ b/project2/common/variableType.cpp @@ -43,6 +43,7 @@ VariableType::getTypeFromName(const std::string & src) if (src == "bool") return Bool; if (src == "int") return Int; if (src == "float") return Float; + if (src == "interval") return Interval; if (src == "datetime") return DateTime; if (src == "null") return Nul; throw UnknownVariableType(src); @@ -65,6 +66,8 @@ VariableType::make(const Glib::ustring & src, const VT_typeID format) return vt.as<int64_t>(); case Float: return vt.as<double>(); + case Interval: + return vt.as<boost::posix_time::time_duration>(); case DateTime: return vt.as<boost::posix_time::ptime>(); } @@ -87,13 +90,18 @@ VariableType::make(const Glib::ustring & src) return VariableType(src).as<bool>(); } catch (...) { - try { - return VariableType(src).as<boost::posix_time::ptime>(); - } - catch (...) { - return src; - } } + try { + return VariableType(src).as<boost::posix_time::time_duration>(); + } + catch (...) { + } + try { + return VariableType(src).as<boost::posix_time::ptime>(); + } + catch (...) { + } + return src; } } @@ -123,6 +131,7 @@ VariableType::VariableType(const Source & t) : \ } VTCONS(Null); +VTCONS(boost::posix_time::time_duration); VTCONS(boost::posix_time::ptime); VTCONS(Glib::ustring); VTCONSD(bool, Boolean); diff --git a/project2/common/variableType.h b/project2/common/variableType.h index d3ac5a0..5bf4c53 100644 --- a/project2/common/variableType.h +++ b/project2/common/variableType.h @@ -14,6 +14,7 @@ enum VT_typeID { Bool, Int, Float, + Interval, DateTime, }; @@ -48,6 +49,7 @@ typedef boost::variant< int64_t, double, // Date and Times + boost::posix_time::time_duration, boost::posix_time::ptime > _VT; @@ -58,6 +60,7 @@ class VariableType : public _VT { VariableType(const std::string &); VariableType(const Glib::ustring &); VariableType(const struct tm &); + VariableType(const boost::posix_time::time_duration &); VariableType(const boost::posix_time::ptime &); VariableType(const Null &); VariableType(const bool &); @@ -91,6 +94,7 @@ class VariableType : public _VT { operator long int() const; operator int() const; operator double() const; + operator const boost::posix_time::time_duration &() const; operator const boost::posix_time::ptime &() const; template <typename T> operator boost::optional<T>() const { return isNull() ? boost::optional<T>() : as<T>(); } operator bool() const; diff --git a/project2/json/conversion.cpp b/project2/json/conversion.cpp index 1fae0d3..8f7fc55 100644 --- a/project2/json/conversion.cpp +++ b/project2/json/conversion.cpp @@ -2,6 +2,9 @@ #include "conversion.h" #include <boost/date_time/posix_time/posix_time.hpp> +json::Value Project2ToJson::operator()(const boost::posix_time::time_duration & i) const { + return boost::posix_time::to_simple_string(i); +} json::Value Project2ToJson::operator()(const boost::posix_time::ptime & i) const { return boost::posix_time::to_iso_extended_string(i); } diff --git a/project2/json/conversion.h b/project2/json/conversion.h index d875462..51a8938 100644 --- a/project2/json/conversion.h +++ b/project2/json/conversion.h @@ -8,6 +8,7 @@ class ConversionNotSupported { }; class Project2ToJson : public boost::static_visitor<json::Value> { public: + json::Value operator()(const boost::posix_time::time_duration & i) const; json::Value operator()(const boost::posix_time::ptime & i) const; json::Value operator()(const Null & c) const; json::Value operator()(const Glib::ustring & c) const; diff --git a/project2/sql/sqlCache.cpp b/project2/sql/sqlCache.cpp index af59ee5..2d40bdd 100644 --- a/project2/sql/sqlCache.cpp +++ b/project2/sql/sqlCache.cpp @@ -15,6 +15,7 @@ #include <boost/foreach.hpp> #include "options.h" #include <boost/algorithm/string/predicate.hpp> +#include <boost/date_time/posix_time/posix_time.hpp> typedef boost::shared_ptr<DB::SelectCommand> SelectPtr; typedef boost::shared_ptr<DB::ModifyCommand> ModifyPtr; @@ -141,7 +142,7 @@ class SqlCache : public Cache { auto con = db->getReadonly(); SelectPtr gh(con->newSelectCommand(sql)); unsigned int offset = 0; - gh->bindParamT(offset++, time(NULL) - CacheLife); + gh->bindParamT(offset++, boost::posix_time::microsec_clock::universal_time() - boost::posix_time::seconds(CacheLife)); applyKeys(ec, boost::bind(bindKeyValues, gh.get(), &offset, _2), ps); if (gh->fetch()) { return new SqlCacheRowSet(gh); @@ -215,7 +216,7 @@ class SqlCache : public Cache { Buffer del; del.appendf("INSERT INTO %s(p2_time) VALUES(?)", HeaderTable.c_str()); ModifyPtr h = ModifyPtr(con->newModifyCommand(del)); - h->bindParamT(0, time(NULL)); + h->bindParamT(0, boost::posix_time::microsec_clock::universal_time()); h->execute(); // Record set header Buffer sql; @@ -275,7 +276,7 @@ class CustomSqlCacheLoader : public ElementLoader::For<SqlCache> { del.appendf("DELETE FROM %s WHERE p2_time < ?", SqlCache::HeaderTable.c_str()); auto con = db->getWritable(); ModifyPtr m(con->newModifyCommand(del)); - m->bindParamT(0, time(NULL) - SqlCache::CacheLife); + m->bindParamT(0, boost::posix_time::microsec_clock::universal_time() - boost::posix_time::seconds(SqlCache::CacheLife)); m->execute(); db->commit(); } diff --git a/project2/sql/sqlHandleAsVariableType.cpp b/project2/sql/sqlHandleAsVariableType.cpp index e628c29..2583df5 100644 --- a/project2/sql/sqlHandleAsVariableType.cpp +++ b/project2/sql/sqlHandleAsVariableType.cpp @@ -14,7 +14,10 @@ void HandleAsVariableType::integer(int64_t i) { void HandleAsVariableType::floatingpoint(double d) { variable = d; } -void HandleAsVariableType::timestamp(const struct tm & t) { - variable = boost::posix_time::ptime(boost::posix_time::ptime_from_tm(t)); +void HandleAsVariableType::interval(const boost::posix_time::time_duration & t) { + variable = t; +} +void HandleAsVariableType::timestamp(const boost::posix_time::ptime & t) { + variable = t; } diff --git a/project2/sql/sqlHandleAsVariableType.h b/project2/sql/sqlHandleAsVariableType.h index c874b7c..2aa6ccb 100644 --- a/project2/sql/sqlHandleAsVariableType.h +++ b/project2/sql/sqlHandleAsVariableType.h @@ -10,7 +10,8 @@ class HandleAsVariableType : public DB::HandleField { void string(const char * c, size_t l); void integer(int64_t i); void floatingpoint(double d); - void timestamp(const struct tm & t); + void interval(const boost::posix_time::time_duration & t); + void timestamp(const boost::posix_time::ptime & t); VariableType variable; }; diff --git a/project2/sql/sqlTest.cpp b/project2/sql/sqlTest.cpp index 45178ef..30b43b8 100644 --- a/project2/sql/sqlTest.cpp +++ b/project2/sql/sqlTest.cpp @@ -54,8 +54,11 @@ class HandleDoCompare : public DB::HandleField { void floatingpoint(double val) { doTest(val); } - void timestamp(const struct tm & val) { - doTest(boost::posix_time::ptime_from_tm(val)); + void interval(const boost::posix_time::time_duration & val) { + doTest(val); + } + void timestamp(const boost::posix_time::ptime & val) { + doTest(val); } bool operator()() const { return retVal; diff --git a/project2/sql/sqlVariableBinder.cpp b/project2/sql/sqlVariableBinder.cpp index c56047c..7d6645a 100644 --- a/project2/sql/sqlVariableBinder.cpp +++ b/project2/sql/sqlVariableBinder.cpp @@ -35,8 +35,12 @@ SqlVariableBinder::operator()(const Boolean & i) const cmd->bindParamI(idx, i.value ? 1 : 0); } void +SqlVariableBinder::operator()(const boost::posix_time::time_duration & i) const +{ + cmd->bindParamT(idx, i); +} +void SqlVariableBinder::operator()(const boost::posix_time::ptime & i) const { - struct tm tm(boost::posix_time::to_tm(i)); - cmd->bindParamT(idx, &tm); + cmd->bindParamT(idx, i); } diff --git a/project2/sql/sqlVariableBinder.h b/project2/sql/sqlVariableBinder.h index 4bbee9f..9f351e4 100644 --- a/project2/sql/sqlVariableBinder.h +++ b/project2/sql/sqlVariableBinder.h @@ -14,6 +14,7 @@ class SqlVariableBinder : public boost::static_visitor<> { void operator()(const Glib::ustring & i) const; void operator()(const int64_t & i) const; void operator()(const double & i) const; + void operator()(const boost::posix_time::time_duration & i) const; void operator()(const boost::posix_time::ptime & i) const; private: diff --git a/project2/xml/xmlPresenter.cpp b/project2/xml/xmlPresenter.cpp index cf31f21..10ff3e6 100644 --- a/project2/xml/xmlPresenter.cpp +++ b/project2/xml/xmlPresenter.cpp @@ -21,6 +21,8 @@ DECLARE_OPTIONS(XmlPresenter, "XML Presenter options") "Indicate the type of integers on output") ("presenter.xml.typeid.float", Options::value(&typeidFloat, true), "Indicate the type of floating point numbers on output") +("presenter.xml.typeid.interval", Options::value(&typeidInterval, true), + "Indicate the type of intervals on output") ("presenter.xml.typeid.datetime", Options::value(&typeidDateTime, true), "Indicate the type of datetimes on output") ("presenter.xml.datetime.dateattr", Options::value(&dateAttr, true), @@ -36,6 +38,7 @@ bool XmlPresenter::typeidBoolean; bool XmlPresenter::typeidString; bool XmlPresenter::typeidInt; bool XmlPresenter::typeidFloat; +bool XmlPresenter::typeidInterval; bool XmlPresenter::typeidDateTime; bool XmlPresenter::dateAttr; bool XmlPresenter::timeAttr; @@ -182,6 +185,10 @@ class XmlNodeWriter : public boost::static_visitor<bool> { addType(XmlPresenter::typeidFloat, "float"); return true; } + bool operator()(const boost::posix_time::time_duration &) const { + addType(XmlPresenter::typeidInterval, "interval"); + return true; + } bool operator()(const boost::posix_time::ptime & i) const { addType(XmlPresenter::typeidDateTime, "datetime"); if (XmlPresenter::timeAttr) { diff --git a/project2/xml/xmlPresenter.h b/project2/xml/xmlPresenter.h index fe4af60..c94e29a 100644 --- a/project2/xml/xmlPresenter.h +++ b/project2/xml/xmlPresenter.h @@ -67,6 +67,7 @@ class XmlPresenter : public Presenter, public ContentPresenter, public SourceOf< static bool typeidString; static bool typeidInt; static bool typeidFloat; + static bool typeidInterval; static bool typeidDateTime; static bool dateAttr; static bool timeAttr; |