aboutsummaryrefslogtreecommitdiff
path: root/mysql/types/traits.hxx
blob: 1aea63799c3153371cdbdff4d060f91391f9d66a (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// file      : mysql/types/traits.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef TRAITS_HXX
#define TRAITS_HXX

#include <cstring> // std::memcpy, std::memset

#include <odb/mysql/traits.hxx>

#include "test.hxx" // date_time

namespace odb
{
  namespace mysql
  {
    template <>
    class value_traits<date_time>
    {
    public:
      typedef date_time type;
      typedef date_time value_type;

      static void
      set_value (date_time& v, const MYSQL_TIME& i, bool is_null)
      {
        if (!is_null)
        {
          v.negative = i.neg;
          v.year = i.year;
          v.month = i.month;
          v.day = i.day;
          v.hour = i.hour;
          v.minute = i.minute;
          v.second = i.second;
        }
        else
          v = date_time ();
      }

      static void
      set_image (MYSQL_TIME& i, bool& is_null, const date_time& v)
      {
        is_null = false;
        i.neg = v.negative;
        i.year = v.year;
        i.month = v.month;
        i.day = v.day;
        i.hour = v.hour;
        i.minute = v.minute;
        i.second = v.second;
        i.second_part = 0;
      }
    };

    template <>
    class value_traits< ::buffer >
    {
    public:
      typedef ::buffer type;
      typedef ::buffer value_type;

      static void
      set_value (type& v, const char* s, std::size_t n, bool is_null)
      {
        if (!is_null)
          v.assign (s, n);
        else
          v.assign (0, 0);
      }

      static void
      set_image (char* s,
                 std::size_t c,
                 std::size_t& n,
                 bool& is_null,
                 const type& v)
      {
        is_null = false;
        n = v.size ();

        if (n > c)
          n = c;

        if (n != 0)
          std::memcpy (s, v.data (), n);
      }

      static void
      set_image (odb::buffer& b,
                 std::size_t& n,
                 bool& is_null,
                 const type& v)
      {
        is_null = false;
        n = v.size ();

        if (n > b.capacity ())
          b.capacity (n);

        if (n != 0)
          std::memcpy (b.data (), v.data (), n);
      }
    };

    template <>
    class value_traits<bitfield>
    {
    public:
      typedef bitfield type;
      typedef bitfield value_type;

      static void
      set_value (bitfield& v,
                 const unsigned char* s,
                 std::size_t,
                 bool is_null)
      {
        if (!is_null)
          std::memcpy (&v, s, 1);
        else
          std::memset (&v, 0, sizeof (bitfield));
      }

      static void
      set_image (unsigned char* s,
                 std::size_t,
                 std::size_t& n,
                 bool& is_null,
                 bitfield v)
      {
        is_null = false;
        n = 1;
        std::memcpy (s, &v, 1);
        s[0] &= 0x0F; // Clear unused bits -- MySQL is sensitive about that.
      }
    };

    template <>
    class value_traits<set>
    {
    public:
      typedef set type;
      typedef set value_type;

      static void
      set_value (set& v, const char* s, std::size_t n, bool is_null)
      {
        v.clear ();

        if (!is_null)
        {
          const char* e (s + n);

          while (s < e)
          {
            const char* p (s);

            while (p < e && *p != ',')
              ++p;

            v.insert (std::string (s, p - s));
            s = p;

            if (p != e)
              ++s;
          }
        }
      }

      static void
      set_image (odb::buffer& buf,
                 std::size_t& n,
                 bool& is_null,
                 const set& v)
      {
        is_null = false;
        n = 0;

        for (set::const_iterator b (v.begin ()), i (b); i != v.end (); ++i)
        {
          std::size_t m (i->size () + (i != b ? 1 : 0));

          if (n + m > buf.capacity ())
            buf.capacity (n + m, n);

          if (i != b)
            buf.data ()[n++] = ',';

          std::memcpy (buf.data () + n, i->c_str (), i->size ());
          n += i->size ();
        }
      }
    };

    template <>
    class value_traits<std::auto_ptr<std::string> >
    {
    public:
      typedef std::auto_ptr<std::string> type;
      typedef std::string value_type;

      static void
      set_value (std::auto_ptr<std::string>& v,
                 const char* s,
                 std::size_t n,
                 bool is_null)
      {
        v.reset (is_null ? 0 : new std::string (s, n));
      }

      static void
      set_image (odb::buffer& b,
                 std::size_t& n,
                 bool& is_null,
                 const std::auto_ptr<std::string>& v)
      {
        is_null = v.get () == 0;

        if (!is_null)
        {
          n = v->size ();

          if (n > b.capacity ())
            b.capacity (n);

          if (n != 0)
            std::memcpy (b.data (), v->c_str (), n);
        }
      }
    };
  }
}

#endif // TRAITS_HXX