aboutsummaryrefslogtreecommitdiff
path: root/oracle/types/traits.hxx
blob: ad747d865d04c48e1d1d224ee81bc9782c685921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// file      : oracle/types/traits.hxx
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef TRAITS_HXX
#define TRAITS_HXX

#include <odb/oracle/oracle-types.hxx> // datetime, interval_ym, interval_ds
#include <odb/oracle/traits.hxx>

#include <odb/oracle/details/date.hxx>

#include "test.hxx" // date_time, time_interval

namespace odb
{
  namespace oracle
  {
    template <>
    class value_traits<date_time, id_date>
    {
    public:
      typedef date_time value_type;
      typedef date_time query_type;
      typedef char* image_type;

      static void
      set_value (date_time& v, const char* i, bool is_null)
      {
        if (!is_null)
        {
          short y (0);
          unsigned char m (0), d (0), h (0), mins (0), s (0);

          details::get_date (i, y, m, d, h, mins, s);

          v.year = y;
          v.month = m;
          v.day = d;
          v.hour = h;
          v.minute = mins;
          v.second = s;

          // Oracle DATE does not support fractional seconds.
          //
          v.nanosecond = 0;
        }
      }

      static void
      set_image (char* i, bool& is_null, const date_time& v)
      {
        is_null = false;
        details::set_date (i,
                           static_cast<unsigned short> (v.year),
                           v.month,
                           v.day,
                           v.hour,
                           v.minute,
                           v.second);
      }
    };

    template <>
    class value_traits<date_time, id_timestamp>
    {
    public:
      typedef date_time value_type;
      typedef date_time query_type;
      typedef datetime image_type;

      static void
      set_value (date_time& v, const datetime& i, bool is_null)
      {
        if (!is_null)
        {
          sb2 y (0);
          ub1 m (0), d (0), h (0), mins (0), s (0);
          ub4 ns (0);

          i.get (y, m, d, h, mins, s, ns);

          v.year = y;
          v.month = m;
          v.day = d;
          v.hour = h;
          v.minute = mins;
          v.second = s;
          v.nanosecond = ns;
        }
      }

      static void
      set_image (datetime& i,
                 bool& is_null,
                 const date_time& v)
      {
        is_null = false;

        i.set (static_cast<sb2> (v.year),
               v.month,
               v.day,
               v.hour,
               v.minute,
               v.second,
               v.nanosecond);
      }
    };

    template <>
    class value_traits<time_interval, id_interval_ds>
    {
    public:
      typedef time_interval value_type;
      typedef time_interval query_type;
      typedef interval_ds image_type;

      static void
      set_value (time_interval& v,
                 const interval_ds& i,
                 bool is_null)
      {
        if (!is_null)
        {
          sb4 d (0), h (0), m (0), s (0), ns (0);
          i.get (d, h, m, s, ns);

          v.year = 0;
          v.month = 0;
          v.day = static_cast<unsigned char> (d);
          v.hour = static_cast<unsigned char> (h);
          v.minute = static_cast<unsigned char> (m);
          v.second = static_cast<unsigned char> (s);
          v.nanosecond = static_cast<unsigned int> (ns);
        }
      }

      static void
      set_image (interval_ds& i,
                 bool& is_null,
                 const time_interval& v)
      {
        is_null = false;

        i.set (v.day,
               v.hour,
               v.minute,
               v.second,
               static_cast<sb4> (v.nanosecond));
      }
    };

    template <>
    class value_traits<time_interval, id_interval_ym>
    {
    public:
      typedef time_interval value_type;
      typedef time_interval query_type;
      typedef interval_ym image_type;

      static void
      set_value (time_interval& v,
                 const interval_ym& i,
                 bool is_null)
      {
        if (!is_null)
        {
          sb4 y (0), m (0);
          i.get (y, m);

          v.year = static_cast<unsigned short> (y);
          v.month = static_cast<unsigned char> (m);
          v.day = 0;
          v.hour = 0;
          v.minute = 0;
          v.second = 0;
          v.nanosecond = 0;
        }
      }

      static void
      set_image (interval_ym& i,
                 bool& is_null,
                 const time_interval& v)
      {
        is_null = false;
        i.set (v.year, v.month);
      }
    };
  }
}

#endif // TRAITS_HXX