From 7ba4c3065f8df144b2643c8e21e142b4b89eeb65 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Fri, 25 Feb 2011 13:19:52 +0200 Subject: Add support for boost date, ptime, and time_duration --- odb/boost/date-time.options | 1 + odb/boost/date-time/exceptions.cxx | 6 + odb/boost/date-time/exceptions.hxx | 6 + odb/boost/date-time/mysql/gregorian-mapping.hxx | 4 +- odb/boost/date-time/mysql/posix-time-mapping.hxx | 21 +++ odb/boost/date-time/mysql/posix-time-traits.hxx | 177 +++++++++++++++++++++++ odb/boost/date-time/posix-time.options | 9 ++ 7 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 odb/boost/date-time/mysql/posix-time-mapping.hxx create mode 100644 odb/boost/date-time/mysql/posix-time-traits.hxx create mode 100644 odb/boost/date-time/posix-time.options diff --git a/odb/boost/date-time.options b/odb/boost/date-time.options index d545a62..4d245bf 100644 --- a/odb/boost/date-time.options +++ b/odb/boost/date-time.options @@ -4,3 +4,4 @@ # license : GNU GPL v2; see accompanying LICENSE file --profile boost/date-time/gregorian +--profile boost/date-time/posix-time diff --git a/odb/boost/date-time/exceptions.cxx b/odb/boost/date-time/exceptions.cxx index abde353..9a8ef3d 100644 --- a/odb/boost/date-time/exceptions.cxx +++ b/odb/boost/date-time/exceptions.cxx @@ -16,6 +16,12 @@ namespace odb { return "unrepresentable date/time special value"; } + + const char* value_out_of_range:: + what () const throw () + { + return "date/time value out of range"; + } } } } diff --git a/odb/boost/date-time/exceptions.hxx b/odb/boost/date-time/exceptions.hxx index 8fd58b8..4d064cb 100644 --- a/odb/boost/date-time/exceptions.hxx +++ b/odb/boost/date-time/exceptions.hxx @@ -22,6 +22,12 @@ namespace odb virtual const char* what () const throw (); }; + + struct LIBODB_BOOST_EXPORT value_out_of_range : odb::boost::exception + { + virtual const char* + what () const throw (); + }; } } } diff --git a/odb/boost/date-time/mysql/gregorian-mapping.hxx b/odb/boost/date-time/mysql/gregorian-mapping.hxx index add14ba..3ebe5e3 100644 --- a/odb/boost/date-time/mysql/gregorian-mapping.hxx +++ b/odb/boost/date-time/mysql/gregorian-mapping.hxx @@ -10,9 +10,7 @@ #include // By default map boost::gregorian::date to MySQL DATE. We use the -// NULL value to represent not_a_date_time. In MYSQL "zero" date -// plays the special value role but this value is not treated as -// special by gregorian::date. +// NULL value to represent not_a_date_time. // #pragma db value(boost::gregorian::date) type("DATE") diff --git a/odb/boost/date-time/mysql/posix-time-mapping.hxx b/odb/boost/date-time/mysql/posix-time-mapping.hxx new file mode 100644 index 0000000..257d391 --- /dev/null +++ b/odb/boost/date-time/mysql/posix-time-mapping.hxx @@ -0,0 +1,21 @@ +// file : odb/boost/date-time/mysql/posix-time-mapping.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_MAPPING_HXX +#define ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_MAPPING_HXX + +#include + +// By default map boost::posix_time::ptime to MySQL DATETIME. We use +// the NULL value to represent not_a_date_time. +// +#pragma db value(boost::posix_time::ptime) type("DATETIME") + +// By default map boost::posix_time::time_duration to MySQL TIME. We +// use the NULL value to represent not_a_date_time. +// +#pragma db value(boost::posix_time::time_duration) type("TIME") + +#endif // ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_MAPPING_HXX diff --git a/odb/boost/date-time/mysql/posix-time-traits.hxx b/odb/boost/date-time/mysql/posix-time-traits.hxx new file mode 100644 index 0000000..35f1413 --- /dev/null +++ b/odb/boost/date-time/mysql/posix-time-traits.hxx @@ -0,0 +1,177 @@ +// file : odb/boost/date-time/mysql/posix-time-traits.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_TRAITS_HXX +#define ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_TRAITS_HXX + +#include + +#include // std::abs + +#include + +#include +#include +#include + +namespace odb +{ + namespace mysql + { + template <> + class value_traits< ::boost::posix_time::ptime, MYSQL_TIME, id_datetime> + { + public: + typedef ::boost::posix_time::ptime ptime; + typedef ::boost::posix_time::time_duration time_duration; + typedef ::boost::gregorian::date date; + typedef ptime value_type; + typedef ptime query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (ptime& v, const MYSQL_TIME& i, bool is_null) + { + if (is_null) + v = ptime (::boost::date_time::not_a_date_time); + else + v = ptime (date (i.year, i.month, i.day), + time_duration (i.hour, i.minute, i.second)); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const ptime& v) + { + if (v.is_special ()) + { + if (v.is_not_a_date_time ()) + is_null = true; + else + throw odb::boost::date_time::special_value (); + } + else + { + is_null = false; + + const date& d (v.date ()); + i.year = d.year (); + i.month = d.month (); + i.day = d.day (); + + const time_duration& t (v.time_of_day ()); + i.hour = t.hours (); + i.minute = t.minutes (); + i.second = t.seconds (); + } + } + }; + + template <> + class value_traits< ::boost::posix_time::ptime, MYSQL_TIME, id_timestamp> + { + public: + typedef ::boost::posix_time::ptime ptime; + typedef ::boost::posix_time::time_duration time_duration; + typedef ::boost::gregorian::date date; + typedef ptime value_type; + typedef ptime query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (ptime& v, const MYSQL_TIME& i, bool is_null) + { + if (is_null) + v = ptime (::boost::date_time::not_a_date_time); + else + v = ptime (date (i.year, i.month, i.day), + time_duration (i.hour, i.minute, i.second)); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const ptime& v) + { + if (v.is_special ()) + { + if (v.is_not_a_date_time ()) + is_null = true; + else + throw odb::boost::date_time::special_value (); + } + else if (v < ptime (date (1970, ::boost::date_time::Jan, 1), + time_duration (0, 0, 1)) || + v > ptime (date (2038, ::boost::date_time::Jan, 19), + time_duration (3, 14, 7))) + throw odb::boost::date_time::value_out_of_range (); + else + { + is_null = false; + + const date& d (v.date ()); + i.year = d.year (); + i.month = d.month (); + i.day = d.day (); + + const time_duration& t (v.time_of_day ()); + i.hour = t.hours (); + i.minute = t.minutes (); + i.second = t.seconds (); + } + } + }; + + template <> + class value_traits< ::boost::posix_time::time_duration, + MYSQL_TIME, + id_time> + { + public: + typedef ::boost::posix_time::time_duration time_duration; + typedef time_duration value_type; + typedef time_duration query_type; + typedef MYSQL_TIME image_type; + + static const unsigned short max_hours = 838; + + static void + set_value (time_duration& v, const MYSQL_TIME& i, bool is_null) + { + if (is_null) + v = time_duration (::boost::date_time::not_a_date_time); + else + { + v = time_duration (i.hour, i.minute, i.second); + + if (i.neg) + v = v.invert_sign (); + } + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const time_duration& v) + { + if (v.is_special ()) + { + if (v.is_not_a_date_time ()) + is_null = true; + else + throw odb::boost::date_time::special_value (); + } + else if (std::abs (v.hours ()) > max_hours) + throw odb::boost::date_time::value_out_of_range (); + else + { + is_null = false; + i.day = 0; + i.hour = std::abs (v.hours ()); + i.minute = std::abs (v.minutes ()); + i.second = std::abs (v.seconds ()); + i.neg = v.is_negative (); + } + } + }; + } +} + +#endif // ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_TRAITS_HXX diff --git a/odb/boost/date-time/posix-time.options b/odb/boost/date-time/posix-time.options new file mode 100644 index 0000000..2576ac5 --- /dev/null +++ b/odb/boost/date-time/posix-time.options @@ -0,0 +1,9 @@ +# file : odb/boost/date-time/posix-time.options +# author : Constantin Michael +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +--profile boost/version + +--odb-epilogue '#include ' +--hxx-prologue '#include ' -- cgit v1.1