aboutsummaryrefslogtreecommitdiff
path: root/oracle/types/test.hxx
blob: 70c1fc308e688c2a7867f8778355003c8e5e8320 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// file      : oracle/types/test.hxx
// copyright : Copyright (c) 2009-2017 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef TEST_HXX
#define TEST_HXX

#include <common/config.hxx> // HAVE_CXX11

#include <string>
#include <vector>
#include <memory>   // std::auto_ptr
#include <cstring> // std::memcpy, std::str[n]cmp, std::strlen

#include <odb/core.hxx>

struct date_time
{
  date_time ()
  {
  }

  date_time (unsigned short y,
             unsigned char m,
             unsigned char d,
             unsigned char h,
             unsigned char min,
             unsigned char sec,
             unsigned int nsec)
      : year (y),
        month (m),
        day (d),
        hour (h),
        minute (min),
        second (sec),
        nanosecond (nsec)
  {
  }

  bool
  operator== (const date_time& y) const
  {
    return
      year == y.year &&
      month == y.month &&
      day == y.day &&
      hour == y.hour &&
      minute == y.minute &&
      second == y.second &&
      nanosecond == y.nanosecond;
  }

  unsigned short year;
  unsigned char month;
  unsigned char day;
  unsigned char hour;
  unsigned char minute;
  unsigned char second;
  unsigned int nanosecond;
};

struct time_interval
{
  time_interval ()
  {
  }

  time_interval (int y, int m, int d, int h, int min, int sec, int nsec)
      : year (y),
        month (m),
        day (d),
        hour (h),
        minute (min),
        second (sec),
        nanosecond (nsec)
  {
  }

  bool
  operator== (const time_interval& y) const
  {
    return
      year == y.year &&
      month == y.month &&
      day == y.day &&
      hour == y.hour &&
      minute == y.minute &&
      second == y.second &&
      nanosecond == y.nanosecond;
  }

  int year;
  int month;
  int day;
  int hour;
  int minute;
  int second;
  int nanosecond;
};

#ifdef HAVE_CXX11
typedef std::unique_ptr<std::string> string_ptr;
#else
typedef std::auto_ptr<std::string> string_ptr;
#endif

typedef std::vector<std::string> strings;

#pragma db object
struct object
{
  object () {}
  object (unsigned long id): id_ (id) {}

  #pragma db id
  unsigned int id_;

  // Integral types.
  //
  #pragma db type ("NUMBER(10)")
  int int_;

  #pragma db type ("NUMBER(10)")
  unsigned uint_;

  #pragma db type ("NUMBER(19)")
  long long long_long_;

  #pragma db type ("NUMBER(20)")
  unsigned long long ulong_long_;

  // Float types.
  //
  #pragma db type ("FLOAT(24)")
  float float_;

  #pragma db type ("FLOAT(53)")
  double double_;

  #pragma db type ("NUMBER(7,3)")
  float num_float_;

  #pragma db type ("NUMBER(15,5)")
  double num_double_;

  #pragma db type ("BINARY_FLOAT")
  float binary_float_;

  #pragma db type ("BINARY_DOUBLE")
  double binary_double_;

  // Data-time types.
  //
  #pragma db type ("DATE")
  date_time date_;

  #pragma db type ("TIMESTAMP(6)")
  date_time timestamp_;

  #pragma db type ("INTERVAL DAY TO SECOND")
  time_interval interval_ds_;

  #pragma db type ("INTERVAL YEAR TO MONTH")
  time_interval interval_ym_;

  // String and binary types.
  //
  #pragma db type ("CHAR(13)")
  std::string char_;

  #pragma db type ("VARCHAR2(512)") null
  std::string varchar2_;

  #pragma db type ("NCHAR(8)")
  std::string nchar_;

  #pragma db type ("NVARCHAR2(512)") null
  std::string nvarchar2_;

  // Oracle treats empty and NULL VARCHAR2 the same. Test that we
  // handle this.
  //
  std::string empty_;
  std::vector<std::string> empty_c_;

  #pragma db type ("RAW(1024)")
  std::vector<char> raw_;

  // LOB types.
  //
  #pragma db type ("BLOB")
  std::vector<char> blob_;

  #pragma db type ("CLOB")
  std::string clob_;

  #pragma db type ("NCLOB")
  std::string nclob_;

  // Test containers of LOBs
  //
  #pragma db value_type ("CLOB")
  strings strs_;

  // Test NULL value.
  //
  #pragma db type ("VARCHAR2(32)") null
  string_ptr null_;

  bool
  operator== (const object& y) const
  {
    return
      id_ == y.id_ &&
      int_ == y.int_ &&
      uint_ == y.uint_ &&
      long_long_ == y.long_long_ &&
      ulong_long_ == y.ulong_long_ &&
      float_ == y.float_ &&
      double_ == y.double_ &&
      num_float_ == y.num_float_ &&
      num_double_ == y.num_double_ &&
      binary_float_ == y.binary_float_ &&
      binary_double_ == y.binary_double_ &&
      date_ == y.date_ &&
      timestamp_ == y.timestamp_ &&
      interval_ds_ == y.interval_ds_ &&
      interval_ym_ == y.interval_ym_ &&
      char_ == y.char_ &&
      varchar2_ == y.varchar2_ &&
      nchar_ == y.nchar_ &&
      nvarchar2_ == y.nvarchar2_ &&
      empty_ == y.empty_ &&
      empty_c_ == y.empty_c_ &&
      raw_ == y.raw_ &&
      blob_ == y.blob_ &&
      clob_ == y.clob_ &&
      nclob_ == y.nclob_ &&
      strs_ == y.strs_ &&
      ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_);
  }
};

#pragma db object
struct big_uint
{
  big_uint (unsigned int id = 0, unsigned long long v = 0)
      : id_ (id), value (v)
  {
  }

  #pragma db id
  unsigned int id_;

  unsigned long long value;

  bool
  operator== (const big_uint& y) const
  {
    return id_ == y.id_ && value == y.value;
  }
};

#pragma db object
struct big_int
{
  big_int (unsigned int id = 0, long long v = 0)
      : id_ (id), value (v)
  {
  }

  #pragma db id
  unsigned int id_;

  long long value;

  bool
  operator== (const big_int& y) const
  {
    return id_ == y.id_ && value == y.value;
  }
};

#pragma db object
struct descriptor
{
  descriptor (unsigned int id = 0): id_ (id) {}

  #pragma db id
  unsigned int id_;

  #pragma db type ("BLOB")
  std::vector<char> blob;

  #pragma db type ("TIMESTAMP(6)")
  date_time timestamp;

  #pragma db type ("INTERVAL DAY TO SECOND")
  time_interval interval_ds;

  #pragma db type ("INTERVAL YEAR TO MONTH")
  time_interval interval_ym;

  bool
  operator== (const descriptor& y) const
  {
    return id_ == y.id_ &&
      blob == y.blob &&
      timestamp == y.timestamp &&
      interval_ds == y.interval_ds &&
      interval_ym == y.interval_ym;
  }
};

// Test char array.
//
#pragma db object
struct char_array
{
  char_array () {}
  char_array (unsigned long id, const char* s)
      : id_ (id)
  {
    std::memcpy (s1, s, std::strlen (s) + 1); // VC++ strncpy deprecation.
    std::memcpy (s2, s, std::strlen (s) + 1);
    s3[0] = c1 = *s;
  }

  #pragma db id
  unsigned long id_;

  char s1[17];

  #pragma db type("CHAR(16)") null
  char s2[16];

  #pragma db null
  char s3[1];

  #pragma db null
  char c1;

  bool
  operator== (const char_array& y) const
  {
    return id_ == y.id_ &&
      std::strcmp (s1, y.s1) == 0 &&
      std::strncmp (s2, y.s2, sizeof (s2)) == 0 &&
      s3[0] == y.s3[0] &&
      c1 == y.c1;
  }
};

#endif // TEST_HXX