aboutsummaryrefslogtreecommitdiff
path: root/mssql/types/test.hxx
blob: 8f0c521496f4a97ad532acdcc9a9251973a0a16a (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// file      : mssql/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

#ifdef _WIN32
#  ifndef WIN32_LEAN_AND_MEAN
#    define WIN32_LEAN_AND_MEAN
#  endif
#  include <windows.h> // GUID
#elif defined(HOST_WIN32)
typedef struct _GUID
{
   unsigned int Data1;
   unsigned short Data2;
   unsigned short Data3;
   unsigned char Data4[8];
} GUID;
#endif

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

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

#include <odb/core.hxx>

struct date_time
{
  date_time ()
  {
  }

  date_time (short y,
             unsigned short m,
             unsigned short d,
             unsigned short h,
             unsigned short min,
             unsigned short sec,
             unsigned int f,
             short tzh,
             short tzm)
      : year (y),
        month (m),
        day (d),
        hour (h),
        minute (min),
        second (sec),
        fraction (f),
        timezone_hour (tzh),
        timezone_minute (tzm)
  {
  }

  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 &&
      fraction == y.fraction &&
      timezone_hour == y.timezone_hour &&
      timezone_minute == y.timezone_minute;
  }

  short year;
  unsigned short month;
  unsigned short day;
  unsigned short hour;
  unsigned short minute;
  unsigned short second;
  unsigned int fraction;
  short timezone_hour;
  short timezone_minute;
};

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

  #pragma db id
  unsigned int id_;

  // Integer types.
  //
  #pragma db type ("BIT")
  unsigned char bit_;

  #pragma db type ("TINYINT")
  unsigned char utint_;

  #pragma db type ("TINYINT")
  unsigned char stint_;

  #pragma db type ("SMALLINT")
  unsigned short usint_;

  #pragma db type ("SMALLINT")
  short ssint_;

  #pragma db type ("INT")
  unsigned int uint_;

  #pragma db type ("INTEGER")
  int sint_;

  #pragma db type ("BIGINT")
  unsigned long long ubint_;

  #pragma db type ("BIGINT")
  long long sbint_;

  // Floating/fixed point types.
  //
  #pragma db type ("SMALLMONEY")
  float fsm_;

  #pragma db type ("SMALLMONEY")
  double dsm_;

  #pragma db type ("SMALLMONEY")
  int ism_;

  #pragma db type ("MONEY")
  double dm1_;

  #pragma db type ("MONEY")
  double dm2_;

  #pragma db type ("MONEY")
  long long im_;

  #pragma db type ("REAL")
  float f4_;

  #pragma db type ("FLOAT")
  double f8_;

  // Strings.
  //
  #pragma db type ("CHAR(20)")
  std::string schar_;

  #pragma db type ("VARCHAR(128)")
  std::string svchar_;

  #pragma db type ("CHAR(1025)")
  std::string lchar_;

  #pragma db type ("CHARACTER VARYING(8000)")
  std::string lvchar_;

  #pragma db type ("VARCHAR(max)")
  std::string mvchar_;

  #pragma db type ("TEXT")
  std::string text_;

  // National strings.
  //
  #pragma db type ("NCHAR(20)")
  std::wstring snchar_;

  #pragma db type ("NVARCHAR(128)")
  std::wstring snvchar_;

  #pragma db type ("NCHAR(513)")
  std::wstring lnchar_;

  #pragma db type ("NATIONAL CHARACTER VARYING(4000)")
  std::wstring lnvchar_;

  #pragma db type ("NVARCHAR(max)")
  std::wstring mnvchar_;

  #pragma db type ("NTEXT")
  std::wstring ntext_;

  // Binary.
  //
  #pragma db type ("BINARY(9)")
  unsigned char sbin_[9];

  #pragma db type ("VARBINARY(256)")
  std::vector<char> svbin_;

  #pragma db type ("BINARY(1025)")
  char lbin_[1025];

  #pragma db type ("BINARY VARYING(8000)")
  std::vector<char> lvbin_;

  #pragma db type ("VARBINARY(max)")
  std::vector<unsigned char> mvbin_;

  #pragma db type ("IMAGE")
  std::vector<char> image_;

  // Date-time. SQL Server 2005 (9.0) only has DATETIME and SMALLDATETIME.
  //
#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000
  #pragma db type ("DATE")
  date_time date_;

  #pragma db type ("TIME")
  date_time time7_;

  #pragma db type ("TIME(4)")
  date_time time4_;
#endif

  #pragma db type ("SMALLDATETIME")
  date_time sdt_;

  #pragma db type ("DATETIME")
  date_time dt_;

#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000
  #pragma db type ("DATETIME2")
  date_time dt2_;

  #pragma db type ("DATETIMEOFFSET")
  date_time dto7_;

  #pragma db type ("DATETIMEOFFSET(0)")
  date_time dto0_;
#endif

  // Other types.
  //
#if defined(_WIN32) || defined(HOST_WIN32)
  //#pragma db type ("UNIQUEIDENTIFIER")
  GUID guid_;
#endif

  #pragma db type ("UNIQUEIDENTIFIER")
  char uuid_[16];

  bool
  operator== (const object& y) const
  {
    return
      id_ == y.id_ &&
      bit_ == y.bit_ &&
      utint_ == y.utint_ &&
      stint_ == y.stint_ &&
      usint_ == y.usint_ &&
      ssint_ == y.ssint_ &&
      uint_ == y.uint_ &&
      sint_ == y.sint_ &&
      ubint_ == y.ubint_ &&
      sbint_ == y.sbint_ &&
      fsm_ == y.fsm_ &&
      dsm_ == y.dsm_ &&
      ism_ == y.ism_ &&
      dm1_ == y.dm1_ &&
      dm2_ == y.dm2_ &&
      im_ == y.im_ &&
      f4_ == y.f4_ &&
      f8_ == y.f8_ &&

      schar_ == y.schar_ &&
      svchar_ == y.svchar_ &&
      lchar_ == y.lchar_ &&
      lvchar_ == y.lvchar_ &&
      mvchar_ == y.mvchar_ &&
      text_ == y.text_ &&

      snchar_ == y.snchar_ &&
      snvchar_ == y.snvchar_ &&
      lnchar_ == y.lnchar_ &&
      lnvchar_ == y.lnvchar_ &&
      mnvchar_ == y.mnvchar_ &&
      ntext_ == y.ntext_ &&

      std::memcmp (sbin_, y.sbin_, sizeof (sbin_)) == 0 &&
      svbin_ == y.svbin_ &&
      std::memcmp (lbin_, y.lbin_, sizeof (lbin_)) == 0 &&
      lvbin_ == y.lvbin_ &&
      mvbin_ == y.mvbin_ &&
      image_ == y.image_

#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000
      && date_ == y.date_
      && time7_ == y.time7_
      && time4_ == y.time4_
#endif
      && sdt_ == y.sdt_
      && dt_ == y.dt_
#if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000
      && dt2_ == y.dt2_
      && dto7_ == y.dto7_
      && dto0_ == y.dto0_
#endif

#ifdef _WIN32
      && std::memcmp (&guid_, &y.guid_, sizeof (guid_)) == 0
#endif
      && std::memcmp (uuid_, y.uuid_, sizeof (uuid_)) == 0;
  }
};

// Test long NULL data.
//
#pragma db object
struct long_null
{
  long_null () {}
  long_null (unsigned int id): id_ (id) {}

  #pragma db id
  unsigned int id_;

  #pragma db type ("VARCHAR(max)") null
#ifdef HAVE_CXX11
  std::unique_ptr<std::string> str_;
#else
  std::auto_ptr<std::string> str_;
#endif

  bool
  operator== (const long_null& y) const
  {
    return
      id_ == y.id_ &&
      ((str_.get () == 0 && y.str_.get () == 0) || *str_ == *y.str_);
  }
};

// Test long data in containers, in particular column re-arrangement.
//
#pragma db value
struct long_comp
{
  long_comp () {}
  long_comp (std::string s, unsigned int n): str (s), num (n) {}

  #pragma db type ("VARCHAR(max)")
  std::string str;

  unsigned int num;

  bool
  operator== (const long_comp& y) const
  {
    return str == y.str && num == y.num;
  }
};

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

  #pragma db id
  unsigned int id_;

  std::vector<long_comp> v;

  bool
  operator== (const long_cont& y) const
  {
    return id_ == y.id_ && v == y.v;
  }
};

// Test char/wchar_t arrays.
//
#pragma db object
struct char_array
{
  char_array () {}
  char_array (unsigned long id, const char* s, const wchar_t* ws)
      : 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;

    std::memcpy (ws1, ws, (std::wcslen (ws) + 1) * sizeof (wchar_t));
    std::memcpy (ws2, ws, (std::wcslen (ws) + 1) * sizeof (wchar_t));
    ws3[0] = wc1 = *ws;

    if (std::strlen (s) == sizeof (s2))
    {
      std::memset (ls1, '1', 1025);
      ls1[1025] = '\0';
      std::memset (ls2, '2', 1025);

      for (std::size_t i (0); i < 257; ++i)
      {
        lws1[i] = L'1';
        lws2[i] = L'2';
      }
      lws1[257] = L'\0';
    }
    else
    {
      std::memcpy (ls1, s, std::strlen (s) + 1); // VC++ strcpy deprecation.
      std::memcpy (ls2, s, std::strlen (s) + 1);

      std::memcpy (lws1, ws, (std::wcslen (ws) + 1) * sizeof (wchar_t));
      std::memcpy (lws2, ws, (std::wcslen (ws) + 1) * sizeof (wchar_t));
    }
  }

  #pragma db id
  unsigned long id_;

  //
  //
  char s1[17];

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

  char s3[1];
  char c1;

  // Long data.
  //
  char ls1[1026];

  #pragma db type("CHAR(1025)")
  char ls2[1025];

  //
  //
  wchar_t ws1[17];

  #pragma db type("NCHAR(16)")
  wchar_t ws2[16];

  wchar_t ws3[1];
  wchar_t wc1;

  // Long data.
  //
  wchar_t lws1[258];

  #pragma db type("NCHAR(257)")
  wchar_t lws2[257];

  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 &&

      std::strcmp (ls1, y.ls1) == 0 &&
      std::strncmp (ls2, y.ls2, sizeof (ls2)) == 0 &&

      std::wcscmp (ws1, y.ws1) == 0 &&
      std::wcsncmp (ws2, y.ws2, sizeof (ws2) / sizeof (wchar_t)) == 0 &&
      ws3[0] == y.ws3[0] &&
      wc1 == y.wc1 &&

      std::wcscmp (lws1, y.lws1) == 0 &&
      std::wcsncmp (lws2, y.lws2, sizeof (lws2) / sizeof (wchar_t)) == 0;
  }
};

// Test optimistic concurrency using ROWVERSION, both with auto and
// manually-assigned ids.
//
#pragma db object optimistic
struct rowversion
{
  rowversion (unsigned int id = 0): id_ (id), ver (0) {}

  #pragma db id
  unsigned int id_;

  #pragma db version type("ROWVERSION")
#ifdef _WIN32
  unsigned __int64 ver;
#else
  unsigned long long ver;
#endif

  std::string str;
};

#pragma db object optimistic
struct rowversion_auto
{
  rowversion_auto (): ver (0) {}

  #pragma db id auto
  unsigned int id_;

  #pragma db version type("ROWVERSION")
#ifdef _WIN32
  unsigned __int64 ver;
#else
  unsigned long long ver;
#endif

  std::string str;
};

#endif // TEST_HXX