aboutsummaryrefslogtreecommitdiff
path: root/odb/qt/basic/oracle/qbyte-array-traits.hxx
blob: d19e02e9afc0c5aae0872266bfe9ba9fa3cd4c72 (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
// file      : odb/qt/basic/oracle/qbyte-array-traits.hxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_QT_BASIC_ORACLE_QBYTE_ARRAY_TRAITS_HXX
#define ODB_QT_BASIC_ORACLE_QBYTE_ARRAY_TRAITS_HXX

#include <odb/pre.hxx>

#include <cstring> // std::memcpy
#include <cstddef> // std::size_t

#include <QtCore/QByteArray>

#include <odb/oracle/traits.hxx>

namespace odb
{
  namespace oracle
  {
    template <>
    struct default_value_traits<QByteArray, id_raw>
    {
      typedef QByteArray value_type;
      typedef QByteArray query_type;
      typedef char* image_type;

      static void
      set_value (QByteArray& v,
                 const char* b,
                 std::size_t n,
                 bool is_null)
      {
        if (is_null)
          v = QByteArray ();
        else
        {
          // Note that we cannot use replace() here since a suitable
          // overload was only added in Qt 4.7.
          //
          v.resize (static_cast<int> (n));
          std::memcpy (v.data (), b, n);
        }
      }

      static void
      set_image (char* b,
                 std::size_t c,
                 std::size_t& n,
                 bool& is_null,
                 const QByteArray& v)
      {
        if (v.isNull ())
          is_null = true;
        else
        {
          is_null = false;

          n = static_cast<std::size_t> (v.size ());

          if (n > c)
            n = c;

          std::memcpy (b, v.constData (), n);
        }
      }
    };

    template <>
    struct default_value_traits<QByteArray, id_blob>
    {
      typedef QByteArray value_type;
      typedef QByteArray query_type;
      typedef lob_callback image_type;

      static void
      set_value (QByteArray& v,
                 result_callback_type& cb,
                 void*& context,
                 bool is_null)
      {
        if (is_null)
          v = QByteArray ();
        else
        {
          cb = &result_callback;
          context = &v;
        }
      }

      static void
      set_image (param_callback_type& cb,
                 const void*& context,
                 bool& is_null,
                 const QByteArray& v)
      {
        if (v.isNull ())
          is_null = true;
        else
        {
          is_null = false;
          cb = &param_callback;
          context = &v;
        }
      }

      static bool
      result_callback (void* context,
                       ub4*,
                       void* b,
                       ub4 s,
                       chunk_position p)
      {
        QByteArray& v (*static_cast<QByteArray*> (context));

        switch (p)
        {
        case chunk_one:
        case chunk_first:
          {
            v.clear ();

            // Falling through.
          }
        case chunk_next:
        case chunk_last:
          {
            v.append (static_cast<char*> (b), static_cast<int> (s));
            break;
          }
        }

        return true;
      }

      static bool
      param_callback (const void* context,
                      ub4*,
                      const void** b,
                      ub4* s,
                      chunk_position* p,
                      void*,
                      ub4)
      {
        const QByteArray& v (*static_cast<const QByteArray*> (context));

        *p = chunk_one;
        *s = static_cast<ub4> (v.size ());
        *b = v.constData ();

        return true;
      }
    };

    template <>
    struct default_type_traits<QByteArray>
    {
      // Allow use of QByteArray in query expressions by default by specifying
      // the default type id as RAW.
      //
      static const database_type_id db_type_id = id_raw;
    };
  }
}

#include <odb/post.hxx>

#endif // ODB_QT_BASIC_ORACLE_QBYTE_ARRAY_TRAITS_HXX