| 1 | // file : odb/boost/date-time/oracle/posix-time-traits.hxx |
| 2 | // copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC |
| 3 | // license : GNU GPL v2; see accompanying LICENSE file |
| 4 | |
| 5 | #ifndef ODB_BOOST_DATE_TIME_ORACLE_POSIX_TIME_TRAITS_HXX |
| 6 | #define ODB_BOOST_DATE_TIME_ORACLE_POSIX_TIME_TRAITS_HXX |
| 7 | |
| 8 | #include <odb/pre.hxx> |
| 9 | |
| 10 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
| 11 | |
| 12 | #include <odb/core.hxx> |
| 13 | |
| 14 | #include <odb/oracle/traits.hxx> |
| 15 | #include <odb/oracle/oracle-fwd.hxx> // ub1, sb2, ub4 |
| 16 | #include <odb/oracle/oracle-types.hxx> // odb::oracle::{datetime interval_ds} |
| 17 | #include <odb/oracle/details/date.hxx> |
| 18 | |
| 19 | #include <odb/boost/date-time/exceptions.hxx> |
| 20 | |
| 21 | namespace odb |
| 22 | { |
| 23 | namespace oracle |
| 24 | { |
| 25 | template <> |
| 26 | struct default_value_traits< ::boost::posix_time::ptime, id_timestamp> |
| 27 | { |
| 28 | typedef ::boost::posix_time::ptime ptime; |
| 29 | typedef ::boost::posix_time::time_duration time_duration; |
| 30 | typedef ::boost::gregorian::date date; |
| 31 | |
| 32 | typedef ptime value_type; |
| 33 | typedef ptime query_type; |
| 34 | typedef datetime image_type; |
| 35 | |
| 36 | static void |
| 37 | set_value (ptime& v, const datetime& i, bool is_null) |
| 38 | { |
| 39 | if (is_null) |
| 40 | v = ptime (::boost::date_time::not_a_date_time); |
| 41 | else |
| 42 | { |
| 43 | sb2 y; |
| 44 | ub1 m, d, h, minute, s; |
| 45 | ub4 ns; |
| 46 | |
| 47 | i.get (y, m, d, h, minute, s, ns); |
| 48 | |
| 49 | unsigned long long fract_s (ns); |
| 50 | fract_s = fract_s * time_duration::ticks_per_second () / |
| 51 | 1000000000ULL; |
| 52 | |
| 53 | v = ptime ( |
| 54 | date (static_cast<date::year_type> (y), |
| 55 | static_cast<date::month_type> (m), |
| 56 | static_cast<date::day_type> (d)), |
| 57 | time_duration ( |
| 58 | static_cast<time_duration::hour_type> (h), |
| 59 | static_cast<time_duration::min_type> (minute), |
| 60 | static_cast<time_duration::sec_type> (s), |
| 61 | static_cast<time_duration::fractional_seconds_type> (fract_s))); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static void |
| 66 | set_image (datetime& i, bool& is_null, const ptime& v) |
| 67 | { |
| 68 | if (v.is_special ()) |
| 69 | { |
| 70 | if (v.is_not_a_date_time ()) |
| 71 | is_null = true; |
| 72 | else |
| 73 | throw odb::boost::date_time::special_value (); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | is_null = false; |
| 78 | |
| 79 | const date& d (v.date ()); |
| 80 | const time_duration& t (v.time_of_day ()); |
| 81 | |
| 82 | unsigned long long ns (t.fractional_seconds ()); |
| 83 | ns = ns * 1000000000ULL / time_duration::ticks_per_second (); |
| 84 | |
| 85 | i.set (static_cast<sb2> (d.year ()), |
| 86 | static_cast<ub1> (d.month ()), |
| 87 | static_cast<ub1> (d.day ()), |
| 88 | static_cast<ub1> (t.hours ()), |
| 89 | static_cast<ub1> (t.minutes ()), |
| 90 | static_cast<ub1> (t.seconds ()), |
| 91 | static_cast<ub4> (ns)); |
| 92 | } |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | template <> |
| 97 | struct default_value_traits< ::boost::posix_time::ptime, id_date> |
| 98 | { |
| 99 | typedef ::boost::posix_time::ptime ptime; |
| 100 | typedef ::boost::posix_time::time_duration time_duration; |
| 101 | typedef ::boost::gregorian::date date; |
| 102 | |
| 103 | typedef ptime value_type; |
| 104 | typedef ptime query_type; |
| 105 | typedef char* image_type; |
| 106 | |
| 107 | static void |
| 108 | set_value (ptime& v, const char* b, bool is_null) |
| 109 | { |
| 110 | if (is_null) |
| 111 | v = ptime (::boost::date_time::not_a_date_time); |
| 112 | else |
| 113 | { |
| 114 | short y; |
| 115 | unsigned char m, d, h, minute, s; |
| 116 | |
| 117 | details::get_date (b, y, m, d, h, minute, s); |
| 118 | |
| 119 | v = ptime ( |
| 120 | date (static_cast<date::year_type> (y), |
| 121 | static_cast<date::month_type> (m), |
| 122 | static_cast<date::day_type> (d)), |
| 123 | time_duration ( |
| 124 | static_cast<time_duration::hour_type> (h), |
| 125 | static_cast<time_duration::min_type> (minute), |
| 126 | static_cast<time_duration::sec_type> (s), |
| 127 | 0)); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | set_image (char* b, bool& is_null, const ptime& v) |
| 133 | { |
| 134 | if (v.is_special ()) |
| 135 | { |
| 136 | if (v.is_not_a_date_time ()) |
| 137 | is_null = true; |
| 138 | else |
| 139 | throw odb::boost::date_time::special_value (); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | is_null = false; |
| 144 | |
| 145 | const date& d (v.date ()); |
| 146 | const time_duration& t (v.time_of_day ()); |
| 147 | |
| 148 | details::set_date (b, |
| 149 | static_cast<short> (d.year ()), |
| 150 | static_cast<unsigned char> (d.month ()), |
| 151 | static_cast<unsigned char> (d.day ()), |
| 152 | static_cast<unsigned char> (t.hours ()), |
| 153 | static_cast<unsigned char> (t.minutes ()), |
| 154 | static_cast<unsigned char> (t.seconds ())); |
| 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | template <> |
| 160 | struct default_value_traits< ::boost::posix_time::time_duration, |
| 161 | id_interval_ds> |
| 162 | { |
| 163 | typedef ::boost::posix_time::time_duration time_duration; |
| 164 | |
| 165 | typedef time_duration value_type; |
| 166 | typedef time_duration query_type; |
| 167 | typedef interval_ds image_type; |
| 168 | |
| 169 | static void |
| 170 | set_value (time_duration& v, |
| 171 | const interval_ds& i, |
| 172 | bool is_null) |
| 173 | { |
| 174 | if (is_null) |
| 175 | v = time_duration (::boost::date_time::not_a_date_time); |
| 176 | else |
| 177 | { |
| 178 | sb4 d, h, m, s, ns; |
| 179 | i.get (d, h, m, s, ns); |
| 180 | |
| 181 | unsigned long long fract_s (ns); |
| 182 | fract_s = fract_s * time_duration::ticks_per_second () / |
| 183 | 1000000000ULL; |
| 184 | |
| 185 | v = time_duration ( |
| 186 | static_cast<time_duration::hour_type> ( |
| 187 | static_cast<unsigned long long> (d) * 24 + h), |
| 188 | static_cast<time_duration::min_type> (m), |
| 189 | static_cast<time_duration::sec_type> (s), |
| 190 | static_cast<time_duration::fractional_seconds_type> (fract_s)); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | set_image (interval_ds& i, |
| 196 | bool& is_null, |
| 197 | const time_duration& v) |
| 198 | { |
| 199 | if (v.is_special ()) |
| 200 | { |
| 201 | if (v.is_not_a_date_time ()) |
| 202 | is_null = true; |
| 203 | else |
| 204 | throw odb::boost::date_time::special_value (); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | is_null = false; |
| 209 | |
| 210 | unsigned long long ns (v.fractional_seconds ()); |
| 211 | ns = ns * 1000000000ULL / time_duration::ticks_per_second (); |
| 212 | |
| 213 | i.set (0, |
| 214 | static_cast<sb4> (v.hours ()), |
| 215 | static_cast<sb4> (v.minutes ()), |
| 216 | static_cast<sb4> (v.seconds ()), |
| 217 | static_cast<sb4> (ns)); |
| 218 | } |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | template <> |
| 223 | struct default_type_traits< ::boost::posix_time::ptime> |
| 224 | { |
| 225 | static const database_type_id db_type_id = id_timestamp; |
| 226 | }; |
| 227 | |
| 228 | template <> |
| 229 | struct default_type_traits< ::boost::posix_time::time_duration> |
| 230 | { |
| 231 | static const database_type_id db_type_id = id_interval_ds; |
| 232 | }; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | #include <odb/post.hxx> |
| 237 | |
| 238 | #endif // ODB_BOOST_DATE_TIME_ORACLE_POSIX_TIME_TRAITS_HXX |