aboutsummaryrefslogtreecommitdiff
path: root/pgsql/types/test.hxx
blob: 8b42aa152663231c3948e453e0576a92c80d0cfc (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
// file      : pgsql/types/test.hxx
// author    : Constantin Michael <constantin@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef TEST_HXX
#define TEST_HXX

#include <set>
#include <string>
#include <memory>  // std::auto_ptr
#include <cstring> // std::memcmp
#include <cstddef> // std::size_t

#include <odb/core.hxx>

#include <common/buffer.hxx>

struct bitfield
{
  unsigned int a: 1;
  unsigned int b: 1;
  unsigned int c: 1;
  unsigned int d: 1;
};

inline bool
operator== (bitfield x, bitfield y)
{
  return
    x.a == y.a &&
    x.b == y.b &&
    x.c == y.c &&
    x.d == y.d;
}

struct varbit
{
  std::size_t size;
  ubuffer ubuffer_;

  bool
  compare (const varbit& x) const
  {
    if (size != x.size)
      return false;

    std::size_t byte_len = size / 8;

    if (std::memcmp (ubuffer_.data (), x.ubuffer_.data (), byte_len != 0))
      return false;

    std::size_t trailing_bits = size % 8;

    if (trailing_bits != 0)
    {
      unsigned char mask (0xFFU << (8 - trailing_bits));

      return (ubuffer_.data ()[byte_len] & mask) ==
        (x.ubuffer_.data ()[byte_len] & mask);
    }

    return true;
  }
};

inline bool
operator== (const varbit& x, const varbit& y)
{
  return x.compare (y);
}

#pragma db value(bitfield) type ("BIT(4) NOT NULL")

typedef std::auto_ptr<std::string> string_ptr;

enum color {red, green, blue};

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

  object ()
  {
  }

  #pragma db id
  unsigned long id_;

  // Integral types.
  //
  #pragma db type ("BOOL NOT NULL")
  bool bool_;

  #pragma db type ("SMALLINT NOT NULL")
  short short_;

  #pragma db type ("INT NOT NULL")
  int int_;

  #pragma db type ("BIGINT NOT NULL")
  long long long_long_;

  // Float types.
  //
  #pragma db type ("REAL NOT NULL")
  float float_;

  #pragma db type ("FLOAT(32) NOT NULL")
  double float8_;

  #pragma db type ("DOUBLE PRECISION NOT NULL")
  double double_;

  // #pragma db type ("NUMERIC(6,3) NOT NULL")
  // std::string numeric_;

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

  #pragma db type ("TIME NOT NULL")
  long long time_;

  #pragma db type ("TIMESTAMP NOT NULL")
  long long timestamp_;

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

  #pragma db type ("VARCHAR(256) NOT NULL")
  std::string varchar_;

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

  #pragma db type ("BYTEA NOT NULL")
  buffer bytea_;

  #pragma db type ("VARBIT(1024) NOT NULL")
  varbit varbit_;

  // #pragma db type ("BIT(4) NOT NULL") - assigned by #pragma db value
  bitfield bit_;

  // Other types.
  //
  #pragma db type ("UUID NOT NULL")
  unsigned char uuid_[16];

  // Test ENUM representation.
  //
  color enum_;

  // Test NULL value.
  //
  #pragma db type ("TEXT")
  string_ptr null_;

  bool
  operator== (const object& y) const
  {
    return
      id_ == y.id_ &&
      bool_ == y.bool_ &&
      short_ == y.short_ &&
      int_ == y.int_ &&
      long_long_ == y.long_long_ &&
      float_ == y.float_ &&
      float8_ == y.float8_ &&
      double_ == y.double_ &&
      // numeric__ == y.numeric_ &&
      date_ == y.date_ &&
      time_ == y.time_ &&
      timestamp_ == y.timestamp_ &&
      char_ == y.char_ &&
      varchar_ == y.varchar_ &&
      text_ == y.text_ &&
      bytea_ == y.bytea_ &&
      bit_ == y.bit_ &&
      varbit_ == y.varbit_ &&
      memcmp (uuid_, y.uuid_, 16) == 0 &&
      enum_ == y.enum_ &&
      ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_);
  }
};

#endif // TEST_HXX