aboutsummaryrefslogtreecommitdiff
path: root/odb/qt/date-time/oracle/qdate-time-traits.hxx
blob: 2c2bfe4c87cdf10a128f71f8c92f5315a7bed773 (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
// file      : odb/qt/date-time/oracle/qdate-time-traits.hxx
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_QT_DATE_TIME_ORACLE_QDATETIME_TRAITS_HXX
#define ODB_QT_DATE_TIME_ORACLE_QDATETIME_TRAITS_HXX

#include <odb/pre.hxx>

#include <QtCore/QDateTime>

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

namespace odb
{
  namespace oracle
  {
    template <>
    struct default_value_traits<QDateTime, id_timestamp>
    {
      typedef QDateTime value_type;
      typedef QDateTime query_type;
      typedef datetime image_type;

      static void
      set_value (QDateTime& v, const datetime& i, bool is_null)
      {
        if (is_null)
          // Default constructor creates a null QDateTime.
          //
          v = QDateTime ();
        else
        {
          sb2 y (0);
          ub1 m (0), d (0), h (0), minute (0), s (0);
          ub4 ns (0);
          i.get (y, m, d, h, minute, s, ns);

          v = QDateTime (QDate (static_cast<int> (y),
                                static_cast<int> (m),
                                static_cast<int> (d)),
                         QTime (static_cast<int> (h),
                                static_cast<int> (minute),
                                static_cast<int> (s),
                                static_cast<int> (ns / 1000000)));
        }
      }

      static void
      set_image (datetime& i, bool& is_null, const QDateTime& v)
      {
        if (v.isNull ())
          is_null = true;
        else
        {
          is_null = false;

          const QDate& d (v.date ());
          const QTime& t (v.time ());

          i.set (static_cast<sb2> (d.year ()),
                 static_cast<ub1> (d.month ()),
                 static_cast<ub1> (d.day ()),
                 static_cast<ub1> (t.hour ()),
                 static_cast<ub1> (t.minute ()),
                 static_cast<ub1> (t.second ()),
                 static_cast<ub4> (t.msec () * 1000000));
        }
      }
    };

    template <>
    struct default_value_traits<QDateTime, id_date>
    {
      typedef QDateTime value_type;
      typedef QDateTime query_type;
      typedef char* image_type;

      static void
      set_value (QDateTime& v, const char* b, bool is_null)
      {
        if (is_null)
          // Default constructor creates a null QDateTime.
          //
          v = QDateTime ();
        else
        {
          short y;
          unsigned char m, d, h, minute, s;

          details::get_date (b, y, m, d, h, minute, s);

          v = QDateTime (QDate (static_cast<int> (y),
                                static_cast<int> (m),
                                static_cast<int> (d)),
                         QTime (static_cast<int> (h),
                                static_cast<int> (minute),
                                static_cast<int> (s),
                                0));
        }
      }

      static void
      set_image (char* b, bool& is_null, const QDateTime& v)
      {
        if (v.isNull ())
          is_null = true;
        else
        {
          is_null = false;

          const QDate& d (v.date ());
          const QTime& t (v.time ());

          details::set_date (b,
                             static_cast<short> (d.year ()),
                             static_cast<unsigned char> (d.month ()),
                             static_cast<unsigned char> (d.day ()),
                             static_cast<unsigned char> (t.hour ()),
                             static_cast<unsigned char> (t.minute ()),
                             static_cast<unsigned char> (t.second ()));
        }
      }
    };

    template <>
    struct default_type_traits<QDateTime>
    {
      static const database_type_id db_type_od = id_timestamp;
    };
  }
}

#include <odb/post.hxx>

#endif // ODB_QT_DATE_TIME_ORACLE_QDATETIME_TRAITS_HXX