summaryrefslogtreecommitdiff
path: root/storage/ndb/src/old_files/client/odbc/common/DataType.hpp
blob: e03e445cf057e89da70a7ba1334bf0985f105e3c (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
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef ODBC_COMMON_DataType_hpp
#define ODBC_COMMON_DataType_hpp

#include <map>
#include <ndb_types.h>
#include <AttrType.hpp>
#include <NdbDictionary.hpp>
#include <common/common.hpp>

/**
 * Sql data exists in several formats:
 *
 * - as NDB data at the bottom
 * - as SQL data during intermediary processing
 * - as external data in user input and output buffers
 * - as lexical constants in SQL statement text
 *
 * Each data format has specific types (e.g. number) and each
 * type has specific attributes (e.g. precision).
 */
enum DataFormat {
    Undef_format = 0,
    Ndb_format = 1,	// not used in NDB version >= v2.10
    Sql_format = 2,
    Ext_format = 3,
    Lex_format = 4
};

#define UndefDataType	990
#define NullDataType	991
#define UnboundDataType	992

class SqlType;
class ExtType;
class LexType;

/**
 * @class SqlType
 * @brief Sql data type
 */
class SqlType {
public:
    enum Type {
	Undef = UndefDataType,
	Char = SQL_CHAR,
	Varchar = SQL_VARCHAR,
	Longvarchar = SQL_LONGVARCHAR,
	Binary = SQL_BINARY,
	Varbinary = SQL_VARBINARY,
	Longvarbinary = SQL_LONGVARBINARY,
	Decimal = SQL_DECIMAL,
	Tinyint = SQL_TINYINT,
	Smallint = SQL_SMALLINT,
	Integer = SQL_INTEGER,
	Bigint = SQL_BIGINT,
	Real = SQL_REAL,
	Double = SQL_DOUBLE,
	Date = SQL_DATE,
	Datetime = SQL_TYPE_TIMESTAMP,
	Blob = SQL_BLOB,
	Clob = SQL_CLOB,
	Null = NullDataType,		// not an ODBC SQL type
	Unbound = UnboundDataType	// special for placeholders
    };
    SqlType();
    SqlType(Type type, bool nullable = true);
    SqlType(Type type, unsigned length, bool nullable = true);
    SqlType(Type type, unsigned precision, unsigned scale, bool nullable = true);
    SqlType(Ctx& ctx, Type type, bool nullable = true);
    SqlType(Ctx& ctx, Type type, unsigned length, bool nullable = true);
    SqlType(Ctx& ctx, Type type, unsigned precision, unsigned scale, bool nullable = true);
    SqlType(Ctx& ctx, const NdbDictionary::Column* ndbColumn);
    Type type() const;
    void setType(Ctx& ctx, Type type, bool nullable = true);
    void setType(Ctx& ctx, Type type, unsigned length, bool nullable = true);
    void setType(Ctx& ctx, Type type, unsigned precision, unsigned scale, bool nullable = true);
    void setType(Ctx& ctx, const NdbDictionary::Column* ndbColumn);
    bool equal(const SqlType& sqlType) const;
    unsigned size() const;
    unsigned displaySize() const;
    const char* typeName() const;
    unsigned length() const;
    bool nullable() const;
    void nullable(bool value);
    bool unSigned() const;
    void unSigned(bool value);
    // forwards compatible
    void getType(Ctx& ctx, NdbDictionary::Column* ndbColumn) const;
    // type conversion
    SQLSMALLINT sqlcdefault(Ctx& ctx) const;
    // print for debugging
    void print(char* buf, unsigned size) const;
private:
    friend class LexType;
    Type m_type;
    unsigned m_precision;
    unsigned m_scale;
    unsigned m_length;
    bool m_nullable;
    bool m_unSigned;	// qualifier instead of separate types
};

inline SqlType::Type
SqlType::type() const
{
    return m_type;
}

inline unsigned
SqlType::length() const
{
    return m_length;
}

inline bool
SqlType::nullable() const
{
    return m_nullable;
}

inline void
SqlType::nullable(bool value)
{
    m_nullable = value;
}

inline bool
SqlType::unSigned() const
{
    return m_unSigned;
}

inline void
SqlType::unSigned(bool value)
{
    ctx_assert(m_type == Smallint || m_type == Integer || m_type == Bigint);
    m_unSigned = value;
}

/**
 * Actual SQL datatypes.
 */
typedef unsigned char SqlChar;		// Char and Varchar via pointer
typedef Int16 SqlSmallint;
typedef Int32 SqlInteger;
typedef Int64 SqlBigint;
typedef Uint16 SqlUsmallint;
typedef Uint32 SqlUinteger;
typedef Uint64 SqlUbigint;
typedef float SqlReal;
typedef double SqlDouble;

// datetime cc yy mm dd HH MM SS 00 ff ff ff ff stored as String(12)
struct SqlDatetime {
    int cc() const { return *(signed char*)&m_data[0]; }
    void cc(int x) { *(signed char*)&m_data[0] = x; }
    unsigned yy() const { return *(unsigned char*)&m_data[1]; }
    void yy(unsigned x) { *(unsigned char*)&m_data[1] = x; }
    unsigned mm() const { return *(unsigned char*)&m_data[2]; }
    void mm(unsigned x) { *(unsigned char*)&m_data[2] = x; }
    unsigned dd() const { return *(unsigned char*)&m_data[3]; }
    void dd(unsigned x) { *(unsigned char*)&m_data[3] = x; }
    unsigned HH() const { return *(unsigned char*)&m_data[4]; }
    void HH(unsigned x) { *(unsigned char*)&m_data[4] = x; }
    unsigned MM() const { return *(unsigned char*)&m_data[5]; }
    void MM(unsigned x) { *(unsigned char*)&m_data[5] = x; }
    unsigned SS() const { return *(unsigned char*)&m_data[6]; }
    void SS(unsigned x) { *(unsigned char*)&m_data[6] = x; }
    unsigned ff() const {
	const unsigned char* p = (unsigned char*)&m_data[8];
	unsigned x = 0;
	x += *p++ << 24;
	x += *p++ << 16;
	x += *p++ << 8;
	x += *p++;
	return x;
    }
    void ff(unsigned x) {
	unsigned char* p = (unsigned char*)&m_data[8];
	*p++ = (x >> 24) & 0xff;
	*p++ = (x >> 16) & 0xff;
	*p++ = (x >> 8) & 0xff;
	*p++ = x & 0xff;
    }
    bool valid() { return true; }	// XXX later
    bool less(const SqlDatetime t) const {
	if (cc() != t.cc())
	    return cc() < t.cc();
	if (yy() != t.yy())
	    return yy() < t.yy();
	if (mm() != t.mm())
	    return mm() < t.mm();
	if (dd() != t.dd())
	    return dd() < t.dd();
	if (HH() != t.HH())
	    return HH() < t.HH();
	if (MM() != t.MM())
	    return MM() < t.MM();
	if (SS() != t.SS())
	    return SS() < t.SS();
	if (ff() != t.ff())
	    return ff() < t.ff();
	return false;
    }
private:
    char m_data[12];	// use array to avoid gaps
};

/**
 * @class ExtType
 * @brief External data type
 */
class ExtType {
public:
    enum Type {
	Undef = UndefDataType,
	Char = SQL_C_CHAR,
	Short = SQL_C_SHORT,
	Sshort = SQL_C_SSHORT,
	Ushort = SQL_C_USHORT,
	Long = SQL_C_LONG,		// for sun.jdbc.odbc
	Slong = SQL_C_SLONG,
	Ulong = SQL_C_ULONG,
	Sbigint = SQL_C_SBIGINT,
	Ubigint = SQL_C_UBIGINT,
	Float = SQL_C_FLOAT,
	Double = SQL_C_DOUBLE,
	Timestamp = SQL_C_TYPE_TIMESTAMP,
	Binary = SQL_C_BINARY,		// XXX BLOB hack
	Unbound = UnboundDataType
    };
    ExtType();
    ExtType(Type type);
    ExtType(Ctx& ctx, Type type);
    Type type() const;
    void setType(Ctx& ctx, Type type);
    unsigned size() const;
private:
    Type m_type;
};

inline ExtType::Type
ExtType::type() const
{
    return m_type;
}

/**
 * @class LexType
 * @class Lexical data type
 */
class LexType {
public:
    enum Type {
	Undef = UndefDataType,
	Char = 1,
	Integer = 2,
	Float = 3,
	Null = 4
    };
    LexType();
    LexType(Type type);
    LexType(Ctx& ctx, Type type);
    Type type() const;
    void setType(Ctx& ctx, Type type);
    void convert(Ctx& ctx, SqlType& out, unsigned length = 0) const;
private:
    Type m_type;
};

inline LexType::Type
LexType::type() const
{
    return m_type;
}

#endif