~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/field.cc

  • Committer: Brian Aker
  • Date: 2008-07-14 16:24:25 UTC
  • Revision ID: brian@tangent.org-20080714162425-juw3vw221gs9kysh
Cleanup around intptr_t

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  @brief
21
21
  This file implements classes defined in field.h
22
22
*/
23
 
#include <drizzled/server_includes.h>
 
23
 
 
24
#ifdef USE_PRAGMA_IMPLEMENTATION
 
25
#pragma implementation                          // gcc: Class implementation
 
26
#endif
 
27
 
 
28
#include "mysql_priv.h"
24
29
#include "sql_select.h"
 
30
#include <m_ctype.h>
25
31
#include <errno.h>
26
 
#include <drizzled/drizzled_error_messages.h>
27
32
 
28
33
// Maximum allowed exponent value for converting string to decimal
29
34
#define MAX_EXPONENT 1024
37
42
template class List_iterator<Create_field>;
38
43
#endif
39
44
 
 
45
uchar Field_null::null[1]={1};
 
46
const char field_separator=',';
 
47
 
 
48
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
 
49
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
 
50
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
 
51
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
 
52
((ulong) ((1LL << min(arg, 4) * 8) - 1LL))
40
53
 
41
54
/*
42
55
  Rules for merging different types of fields in UNION
45
58
  following #defines describe that gap and how to canculate number of fields
46
59
  and index of field in thia array.
47
60
*/
48
 
#define FIELDTYPE_TEAR_FROM (DRIZZLE_TYPE_VARCHAR + 1)
49
 
#define FIELDTYPE_TEAR_TO   (DRIZZLE_TYPE_NEWDECIMAL - 1)
 
61
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_VARCHAR + 1)
 
62
#define FIELDTYPE_TEAR_TO   (MYSQL_TYPE_NEWDECIMAL - 1)
50
63
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
51
64
inline int field_type2index (enum_field_types field_type)
52
65
{
58
71
 
59
72
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
60
73
{
61
 
  /* DRIZZLE_TYPE_DECIMAL -> */
62
 
  {
63
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
64
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
65
 
  //DRIZZLE_TYPE_LONG
66
 
    DRIZZLE_TYPE_NEWDECIMAL,
67
 
  //DRIZZLE_TYPE_DOUBLE
68
 
    DRIZZLE_TYPE_DOUBLE,
69
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
70
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
71
 
  //DRIZZLE_TYPE_LONGLONG
72
 
    DRIZZLE_TYPE_NEWDECIMAL,
73
 
  //DRIZZLE_TYPE_TIME
74
 
    DRIZZLE_TYPE_VARCHAR,
75
 
  //DRIZZLE_TYPE_DATETIME
76
 
    DRIZZLE_TYPE_VARCHAR,
77
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
78
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
79
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
80
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
81
 
  //DRIZZLE_TYPE_BLOB
82
 
    DRIZZLE_TYPE_BLOB,
83
 
  },
84
 
  /* DRIZZLE_TYPE_TINY -> */
85
 
  {
86
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
87
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
88
 
  //DRIZZLE_TYPE_LONG
89
 
    DRIZZLE_TYPE_LONG,
90
 
  //DRIZZLE_TYPE_DOUBLE
91
 
    DRIZZLE_TYPE_DOUBLE,
92
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
93
 
    DRIZZLE_TYPE_TINY,        DRIZZLE_TYPE_VARCHAR,
94
 
  //DRIZZLE_TYPE_LONGLONG
95
 
    DRIZZLE_TYPE_LONGLONG,
96
 
  //DRIZZLE_TYPE_TIME
97
 
    DRIZZLE_TYPE_VARCHAR,
98
 
  //DRIZZLE_TYPE_DATETIME
99
 
    DRIZZLE_TYPE_VARCHAR,
100
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
101
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
102
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
103
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
104
 
  //DRIZZLE_TYPE_BLOB
105
 
    DRIZZLE_TYPE_BLOB,
106
 
  },
107
 
  /* DRIZZLE_TYPE_LONG -> */
108
 
  {
109
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
110
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONG,
111
 
  //DRIZZLE_TYPE_LONG
112
 
    DRIZZLE_TYPE_LONG,
113
 
  //DRIZZLE_TYPE_DOUBLE
114
 
    DRIZZLE_TYPE_DOUBLE,
115
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
116
 
    DRIZZLE_TYPE_LONG,         DRIZZLE_TYPE_VARCHAR,
117
 
  //DRIZZLE_TYPE_LONGLONG
118
 
    DRIZZLE_TYPE_LONGLONG,
119
 
  //DRIZZLE_TYPE_TIME
120
 
    DRIZZLE_TYPE_VARCHAR,
121
 
  //DRIZZLE_TYPE_DATETIME
122
 
    DRIZZLE_TYPE_VARCHAR,
123
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
124
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
125
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
126
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
127
 
  //DRIZZLE_TYPE_BLOB
128
 
    DRIZZLE_TYPE_BLOB,
129
 
  },
130
 
  /* DRIZZLE_TYPE_DOUBLE -> */
131
 
  {
132
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
133
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_DOUBLE,
134
 
  //DRIZZLE_TYPE_LONG
135
 
    DRIZZLE_TYPE_DOUBLE,
136
 
  //DRIZZLE_TYPE_DOUBLE
137
 
    DRIZZLE_TYPE_DOUBLE,
138
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
139
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
140
 
  //DRIZZLE_TYPE_LONGLONG
141
 
    DRIZZLE_TYPE_DOUBLE,
142
 
  //DRIZZLE_TYPE_TIME
143
 
    DRIZZLE_TYPE_VARCHAR,
144
 
  //DRIZZLE_TYPE_DATETIME
145
 
    DRIZZLE_TYPE_VARCHAR,
146
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
147
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
148
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
149
 
    DRIZZLE_TYPE_DOUBLE,      DRIZZLE_TYPE_VARCHAR,
150
 
  //DRIZZLE_TYPE_BLOB
151
 
    DRIZZLE_TYPE_BLOB,
152
 
  },
153
 
  /* DRIZZLE_TYPE_NULL -> */
154
 
  {
155
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
156
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_TINY,
157
 
  //DRIZZLE_TYPE_LONG
158
 
    DRIZZLE_TYPE_LONG,
159
 
  //DRIZZLE_TYPE_DOUBLE
160
 
    DRIZZLE_TYPE_DOUBLE,
161
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
162
 
    DRIZZLE_TYPE_NULL,        DRIZZLE_TYPE_TIMESTAMP,
163
 
  //DRIZZLE_TYPE_LONGLONG
164
 
    DRIZZLE_TYPE_LONGLONG,
165
 
  //DRIZZLE_TYPE_TIME
166
 
    DRIZZLE_TYPE_TIME,
167
 
  //DRIZZLE_TYPE_DATETIME
168
 
    DRIZZLE_TYPE_DATETIME,
169
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
170
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
171
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
172
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_ENUM,
173
 
  //DRIZZLE_TYPE_BLOB
174
 
    DRIZZLE_TYPE_BLOB,
175
 
  },
176
 
  /* DRIZZLE_TYPE_TIMESTAMP -> */
177
 
  {
178
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
179
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
180
 
  //DRIZZLE_TYPE_LONG
181
 
    DRIZZLE_TYPE_VARCHAR,
182
 
  //DRIZZLE_TYPE_DOUBLE
183
 
    DRIZZLE_TYPE_VARCHAR,
184
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
185
 
    DRIZZLE_TYPE_TIMESTAMP,   DRIZZLE_TYPE_TIMESTAMP,
186
 
  //DRIZZLE_TYPE_LONGLONG
187
 
    DRIZZLE_TYPE_VARCHAR,
188
 
  //DRIZZLE_TYPE_TIME
189
 
    DRIZZLE_TYPE_DATETIME,
190
 
  //DRIZZLE_TYPE_DATETIME
191
 
    DRIZZLE_TYPE_DATETIME,
192
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
193
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
194
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
195
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
196
 
  //DRIZZLE_TYPE_BLOB
197
 
    DRIZZLE_TYPE_BLOB,
198
 
  },
199
 
  /* DRIZZLE_TYPE_LONGLONG -> */
200
 
  {
201
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
202
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_LONGLONG,
203
 
  //DRIZZLE_TYPE_LONG
204
 
    DRIZZLE_TYPE_LONGLONG,
205
 
  //DRIZZLE_TYPE_DOUBLE
206
 
    DRIZZLE_TYPE_DOUBLE,
207
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
208
 
    DRIZZLE_TYPE_LONGLONG,    DRIZZLE_TYPE_VARCHAR,
209
 
  //DRIZZLE_TYPE_LONGLONG
210
 
    DRIZZLE_TYPE_LONGLONG,
211
 
  //DRIZZLE_TYPE_TIME
212
 
    DRIZZLE_TYPE_VARCHAR,
213
 
  //DRIZZLE_TYPE_DATETIME
214
 
    DRIZZLE_TYPE_VARCHAR,
215
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
216
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
217
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
218
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
219
 
  //DRIZZLE_TYPE_BLOB
220
 
    DRIZZLE_TYPE_BLOB,
221
 
  },
222
 
  /* DRIZZLE_TYPE_TIME -> */
223
 
  {
224
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
225
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
226
 
  //DRIZZLE_TYPE_LONG
227
 
    DRIZZLE_TYPE_VARCHAR,
228
 
  //DRIZZLE_TYPE_DOUBLE
229
 
    DRIZZLE_TYPE_VARCHAR,
230
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
231
 
    DRIZZLE_TYPE_TIME,        DRIZZLE_TYPE_DATETIME,
232
 
  //DRIZZLE_TYPE_LONGLONG
233
 
    DRIZZLE_TYPE_VARCHAR,
234
 
  //DRIZZLE_TYPE_TIME
235
 
    DRIZZLE_TYPE_TIME,
236
 
  //DRIZZLE_TYPE_DATETIME
237
 
    DRIZZLE_TYPE_DATETIME,
238
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
239
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
240
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
241
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
242
 
  //DRIZZLE_TYPE_BLOB
243
 
    DRIZZLE_TYPE_BLOB,
244
 
  },
245
 
  /* DRIZZLE_TYPE_DATETIME -> */
246
 
  {
247
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
248
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
249
 
  //DRIZZLE_TYPE_LONG
250
 
    DRIZZLE_TYPE_VARCHAR,
251
 
  //DRIZZLE_TYPE_DOUBLE
252
 
    DRIZZLE_TYPE_VARCHAR,
253
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
254
 
    DRIZZLE_TYPE_DATETIME,    DRIZZLE_TYPE_DATETIME,
255
 
  //DRIZZLE_TYPE_LONGLONG
256
 
    DRIZZLE_TYPE_VARCHAR,
257
 
  //DRIZZLE_TYPE_TIME
258
 
    DRIZZLE_TYPE_DATETIME,
259
 
  //DRIZZLE_TYPE_DATETIME
260
 
    DRIZZLE_TYPE_DATETIME,
261
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
262
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
263
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
264
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
265
 
  //DRIZZLE_TYPE_BLOB
266
 
    DRIZZLE_TYPE_BLOB,
267
 
  },
268
 
  /* DRIZZLE_TYPE_NEWDATE -> */
269
 
  {
270
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
271
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
272
 
  //DRIZZLE_TYPE_LONG
273
 
    DRIZZLE_TYPE_VARCHAR,
274
 
  //DRIZZLE_TYPE_DOUBLE
275
 
    DRIZZLE_TYPE_VARCHAR,
276
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
277
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_DATETIME,
278
 
  //DRIZZLE_TYPE_LONGLONG
279
 
    DRIZZLE_TYPE_VARCHAR,
280
 
  //DRIZZLE_TYPE_TIME
281
 
    DRIZZLE_TYPE_DATETIME,
282
 
  //DRIZZLE_TYPE_DATETIME
283
 
    DRIZZLE_TYPE_DATETIME,
284
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
285
 
    DRIZZLE_TYPE_NEWDATE,     DRIZZLE_TYPE_VARCHAR,
286
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
287
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
288
 
  //DRIZZLE_TYPE_BLOB
289
 
    DRIZZLE_TYPE_BLOB,
290
 
  },
291
 
  /* DRIZZLE_TYPE_VARCHAR -> */
292
 
  {
293
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
294
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
295
 
  //DRIZZLE_TYPE_LONG
296
 
    DRIZZLE_TYPE_VARCHAR,
297
 
  //DRIZZLE_TYPE_DOUBLE
298
 
    DRIZZLE_TYPE_VARCHAR,
299
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
300
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
301
 
  //DRIZZLE_TYPE_LONGLONG
302
 
    DRIZZLE_TYPE_VARCHAR,
303
 
  //DRIZZLE_TYPE_TIME
304
 
    DRIZZLE_TYPE_VARCHAR,
305
 
  //DRIZZLE_TYPE_DATETIME
306
 
    DRIZZLE_TYPE_VARCHAR,
307
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
308
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
309
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
310
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
311
 
  //DRIZZLE_TYPE_BLOB
312
 
    DRIZZLE_TYPE_BLOB,
313
 
  },
314
 
  /* DRIZZLE_TYPE_NEWDECIMAL -> */
315
 
  {
316
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
317
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_NEWDECIMAL,
318
 
  //DRIZZLE_TYPE_LONG
319
 
    DRIZZLE_TYPE_NEWDECIMAL,
320
 
  //DRIZZLE_TYPE_DOUBLE
321
 
    DRIZZLE_TYPE_DOUBLE,
322
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
323
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
324
 
  //DRIZZLE_TYPE_LONGLONG
325
 
    DRIZZLE_TYPE_NEWDECIMAL,
326
 
  //DRIZZLE_TYPE_TIME
327
 
    DRIZZLE_TYPE_VARCHAR,
328
 
  //DRIZZLE_TYPE_DATETIME
329
 
    DRIZZLE_TYPE_VARCHAR,
330
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
331
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
332
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
333
 
    DRIZZLE_TYPE_NEWDECIMAL,  DRIZZLE_TYPE_VARCHAR,
334
 
  //DRIZZLE_TYPE_BLOB
335
 
    DRIZZLE_TYPE_BLOB,
336
 
  },
337
 
  /* DRIZZLE_TYPE_ENUM -> */
338
 
  {
339
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
340
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
341
 
  //DRIZZLE_TYPE_LONG
342
 
    DRIZZLE_TYPE_VARCHAR,
343
 
  //DRIZZLE_TYPE_DOUBLE
344
 
    DRIZZLE_TYPE_VARCHAR,
345
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
346
 
    DRIZZLE_TYPE_ENUM,        DRIZZLE_TYPE_VARCHAR,
347
 
  //DRIZZLE_TYPE_LONGLONG
348
 
    DRIZZLE_TYPE_VARCHAR,
349
 
  //DRIZZLE_TYPE_TIME
350
 
    DRIZZLE_TYPE_VARCHAR,
351
 
  //DRIZZLE_TYPE_DATETIME
352
 
    DRIZZLE_TYPE_VARCHAR,
353
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
354
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
355
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
356
 
    DRIZZLE_TYPE_VARCHAR,     DRIZZLE_TYPE_VARCHAR,
357
 
  //DRIZZLE_TYPE_BLOB
358
 
    DRIZZLE_TYPE_BLOB,
359
 
  },
360
 
  /* DRIZZLE_TYPE_BLOB -> */
361
 
  {
362
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
363
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
364
 
  //DRIZZLE_TYPE_LONG
365
 
    DRIZZLE_TYPE_BLOB,
366
 
  //DRIZZLE_TYPE_DOUBLE
367
 
    DRIZZLE_TYPE_BLOB,
368
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
369
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
370
 
  //DRIZZLE_TYPE_LONGLONG
371
 
    DRIZZLE_TYPE_BLOB,
372
 
  //DRIZZLE_TYPE_TIME
373
 
    DRIZZLE_TYPE_BLOB,
374
 
  //DRIZZLE_TYPE_DATETIME
375
 
    DRIZZLE_TYPE_BLOB,
376
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
377
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
378
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
379
 
    DRIZZLE_TYPE_BLOB,        DRIZZLE_TYPE_BLOB,
380
 
  //DRIZZLE_TYPE_BLOB
381
 
    DRIZZLE_TYPE_BLOB,
382
 
  },
 
74
  /* MYSQL_TYPE_DECIMAL -> */
 
75
  {
 
76
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
77
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
78
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
79
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
80
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
81
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
82
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
83
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
84
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
85
  //MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
 
86
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
87
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
88
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
89
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
90
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
91
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
92
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
93
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
94
  //MYSQL_TYPE_SET
 
95
    MYSQL_TYPE_VARCHAR,
 
96
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
97
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
98
  //MYSQL_TYPE_STRING       
 
99
    MYSQL_TYPE_STRING
 
100
  },
 
101
  /* MYSQL_TYPE_TINY -> */
 
102
  {
 
103
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
104
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
105
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
106
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
107
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
108
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
109
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
110
    MYSQL_TYPE_TINY,        MYSQL_TYPE_VARCHAR,
 
111
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
112
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
113
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
114
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
115
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
116
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY,
 
117
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
118
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
119
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
120
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
121
  //MYSQL_TYPE_SET
 
122
    MYSQL_TYPE_VARCHAR,
 
123
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
124
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
125
  //MYSQL_TYPE_STRING       
 
126
    MYSQL_TYPE_STRING     
 
127
  },
 
128
  /* MYSQL_TYPE_SHORT -> */
 
129
  {
 
130
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
131
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_SHORT,
 
132
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
133
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
134
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
135
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
136
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
137
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_VARCHAR,
 
138
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
139
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
140
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
141
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
142
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
143
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_SHORT,
 
144
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
145
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
146
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
147
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
148
  //MYSQL_TYPE_SET
 
149
    MYSQL_TYPE_VARCHAR,
 
150
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
151
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
152
  //MYSQL_TYPE_STRING
 
153
    MYSQL_TYPE_STRING
 
154
  },
 
155
  /* MYSQL_TYPE_LONG -> */
 
156
  {
 
157
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
158
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONG,
 
159
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
160
    MYSQL_TYPE_LONG,        MYSQL_TYPE_LONG,
 
161
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
162
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
163
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
164
    MYSQL_TYPE_LONG,         MYSQL_TYPE_VARCHAR,
 
165
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
166
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
167
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
168
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
169
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
170
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONG,
 
171
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
172
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
173
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
174
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
175
  //MYSQL_TYPE_SET
 
176
    MYSQL_TYPE_VARCHAR,
 
177
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
178
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
179
  //MYSQL_TYPE_STRING
 
180
    MYSQL_TYPE_STRING
 
181
  },
 
182
  /* MYSQL_TYPE_FLOAT -> */
 
183
  {
 
184
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
185
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_FLOAT,
 
186
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
187
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
188
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
189
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
190
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
191
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_VARCHAR,
 
192
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
193
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_INT24,
 
194
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
195
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
196
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
197
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_FLOAT,
 
198
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
199
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
200
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
201
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
202
  //MYSQL_TYPE_SET
 
203
    MYSQL_TYPE_VARCHAR,
 
204
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
205
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
206
  //MYSQL_TYPE_STRING
 
207
    MYSQL_TYPE_STRING
 
208
  },
 
209
  /* MYSQL_TYPE_DOUBLE -> */
 
210
  {
 
211
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
212
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
213
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
214
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
215
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
216
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
217
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
218
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
219
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
220
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_INT24,
 
221
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
222
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
223
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
224
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_DOUBLE,
 
225
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
226
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
227
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
228
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
 
229
  //MYSQL_TYPE_SET
 
230
    MYSQL_TYPE_VARCHAR,
 
231
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
232
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
233
  //MYSQL_TYPE_STRING
 
234
    MYSQL_TYPE_STRING
 
235
  },
 
236
  /* MYSQL_TYPE_NULL -> */
 
237
  {
 
238
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
239
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
240
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
241
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
242
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
243
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
244
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
245
    MYSQL_TYPE_NULL,        MYSQL_TYPE_TIMESTAMP,
 
246
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
247
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
248
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
249
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_TIME,
 
250
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
251
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_YEAR,
 
252
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
253
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
254
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
255
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_ENUM,
 
256
  //MYSQL_TYPE_SET
 
257
    MYSQL_TYPE_SET,
 
258
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
259
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
260
  //MYSQL_TYPE_STRING
 
261
    MYSQL_TYPE_STRING
 
262
  },
 
263
  /* MYSQL_TYPE_TIMESTAMP -> */
 
264
  {
 
265
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
266
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
267
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
268
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
269
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
270
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
271
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
272
    MYSQL_TYPE_TIMESTAMP,   MYSQL_TYPE_TIMESTAMP,
 
273
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
274
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
275
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
276
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
277
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
278
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
279
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
280
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
281
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
282
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
283
  //MYSQL_TYPE_SET
 
284
    MYSQL_TYPE_VARCHAR,
 
285
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
286
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
287
  //MYSQL_TYPE_STRING
 
288
    MYSQL_TYPE_STRING
 
289
  },
 
290
  /* MYSQL_TYPE_LONGLONG -> */
 
291
  {
 
292
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
293
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONGLONG,
 
294
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
295
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONGLONG,
 
296
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
297
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
298
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
299
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_VARCHAR,
 
300
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
301
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONG,
 
302
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
303
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
304
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
305
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONGLONG,
 
306
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
307
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
308
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
309
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
310
  //MYSQL_TYPE_SET
 
311
    MYSQL_TYPE_VARCHAR,
 
312
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
313
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
314
  //MYSQL_TYPE_STRING
 
315
    MYSQL_TYPE_STRING
 
316
  },
 
317
  /* MYSQL_TYPE_INT24 -> */
 
318
  {
 
319
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
320
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_INT24,
 
321
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
322
    MYSQL_TYPE_INT24,       MYSQL_TYPE_LONG,
 
323
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
324
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
325
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
326
    MYSQL_TYPE_INT24,       MYSQL_TYPE_VARCHAR,
 
327
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
328
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
329
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
330
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
331
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
332
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_INT24,
 
333
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
334
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
335
  //MYSQL_TYPE_NEWDECIMAL    MYSQL_TYPE_ENUM
 
336
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
337
  //MYSQL_TYPE_SET
 
338
    MYSQL_TYPE_VARCHAR,
 
339
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
340
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
341
  //MYSQL_TYPE_STRING
 
342
    MYSQL_TYPE_STRING
 
343
  },
 
344
  /* MYSQL_TYPE_DATE -> */
 
345
  {
 
346
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
347
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
348
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
349
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
350
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
351
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
352
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
353
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
354
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
355
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
356
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
357
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
358
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
359
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
360
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
361
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
362
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
363
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
364
  //MYSQL_TYPE_SET
 
365
    MYSQL_TYPE_VARCHAR,
 
366
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
367
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
368
  //MYSQL_TYPE_STRING
 
369
    MYSQL_TYPE_STRING
 
370
  },
 
371
  /* MYSQL_TYPE_TIME -> */
 
372
  {
 
373
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
374
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
375
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
376
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
377
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
378
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
379
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
380
    MYSQL_TYPE_TIME,        MYSQL_TYPE_DATETIME,
 
381
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
382
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
383
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
384
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_TIME,
 
385
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
386
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
387
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
388
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
389
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
390
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
391
  //MYSQL_TYPE_SET
 
392
    MYSQL_TYPE_VARCHAR,
 
393
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
394
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
395
  //MYSQL_TYPE_STRING
 
396
    MYSQL_TYPE_STRING
 
397
  },
 
398
  /* MYSQL_TYPE_DATETIME -> */
 
399
  {
 
400
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
401
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
402
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
403
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
404
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
405
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
406
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
407
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
408
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
409
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
410
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
411
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
 
412
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
413
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
414
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
415
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
416
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
417
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
418
  //MYSQL_TYPE_SET
 
419
    MYSQL_TYPE_VARCHAR,
 
420
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
421
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
422
  //MYSQL_TYPE_STRING
 
423
    MYSQL_TYPE_STRING
 
424
  },
 
425
  /* MYSQL_TYPE_YEAR -> */
 
426
  {
 
427
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
428
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
 
429
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
430
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
 
431
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
432
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
 
433
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
434
    MYSQL_TYPE_YEAR,        MYSQL_TYPE_VARCHAR,
 
435
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
436
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
 
437
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
438
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
439
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
440
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_YEAR,
 
441
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
442
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
443
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
444
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
445
  //MYSQL_TYPE_SET
 
446
    MYSQL_TYPE_VARCHAR,
 
447
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
448
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
449
  //MYSQL_TYPE_STRING
 
450
    MYSQL_TYPE_STRING
 
451
  },
 
452
  /* MYSQL_TYPE_NEWDATE -> */
 
453
  {
 
454
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
455
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
456
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
457
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
458
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
459
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
460
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
461
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
462
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
463
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
464
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
465
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
 
466
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
467
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
 
468
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
469
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
 
470
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
471
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
472
  //MYSQL_TYPE_SET
 
473
    MYSQL_TYPE_VARCHAR,
 
474
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
475
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
476
  //MYSQL_TYPE_STRING
 
477
    MYSQL_TYPE_STRING
 
478
  },
 
479
  /* MYSQL_TYPE_VARCHAR -> */
 
480
  {
 
481
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
482
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
483
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
484
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
485
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
486
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
487
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
488
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
489
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
490
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
491
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
492
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
493
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
494
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
495
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
496
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
497
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
498
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
499
  //MYSQL_TYPE_SET
 
500
    MYSQL_TYPE_VARCHAR,
 
501
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
502
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
503
  //MYSQL_TYPE_STRING
 
504
    MYSQL_TYPE_VARCHAR
 
505
  },
 
506
  /* MYSQL_TYPE_NEWDECIMAL -> */
 
507
  {
 
508
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
509
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
510
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
511
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
512
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
513
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
 
514
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
515
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
516
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
517
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
 
518
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
519
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
520
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
521
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_NEWDECIMAL,
 
522
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
523
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
524
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
525
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
 
526
  //MYSQL_TYPE_SET
 
527
    MYSQL_TYPE_VARCHAR,
 
528
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
529
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
530
  //MYSQL_TYPE_STRING
 
531
    MYSQL_TYPE_STRING
 
532
  },
 
533
  /* MYSQL_TYPE_ENUM -> */
 
534
  {
 
535
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
536
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
537
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
538
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
539
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
540
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
541
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
542
    MYSQL_TYPE_ENUM,        MYSQL_TYPE_VARCHAR,
 
543
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
544
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
545
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
546
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
547
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
548
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
549
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
550
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
551
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
552
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
553
  //MYSQL_TYPE_SET
 
554
    MYSQL_TYPE_VARCHAR,
 
555
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
556
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
557
  //MYSQL_TYPE_STRING
 
558
    MYSQL_TYPE_STRING
 
559
  },
 
560
  /* MYSQL_TYPE_SET -> */
 
561
  {
 
562
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
563
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
564
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
565
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
566
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
567
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
568
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
569
    MYSQL_TYPE_SET,         MYSQL_TYPE_VARCHAR,
 
570
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
571
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
572
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
573
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
574
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
575
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
576
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
577
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
578
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
579
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
580
  //MYSQL_TYPE_SET
 
581
    MYSQL_TYPE_VARCHAR,
 
582
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
583
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
584
  //MYSQL_TYPE_STRING
 
585
    MYSQL_TYPE_STRING
 
586
  },
 
587
  /* MYSQL_TYPE_BLOB -> */
 
588
  {
 
589
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
590
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
591
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
592
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
593
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
594
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
595
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
596
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
597
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
598
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
599
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
600
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
601
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
602
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
603
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
604
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
605
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
606
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
607
  //MYSQL_TYPE_SET
 
608
    MYSQL_TYPE_BLOB,
 
609
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
610
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
 
611
  //MYSQL_TYPE_STRING
 
612
    MYSQL_TYPE_BLOB
 
613
  },
 
614
  /* MYSQL_TYPE_VAR_STRING -> */
 
615
  {
 
616
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
617
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
618
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
619
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
620
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
621
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
622
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
623
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
624
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
625
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
626
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
627
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
628
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
629
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
630
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
631
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
632
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
633
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
 
634
  //MYSQL_TYPE_SET
 
635
    MYSQL_TYPE_VARCHAR,
 
636
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
637
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
638
  //MYSQL_TYPE_STRING
 
639
    MYSQL_TYPE_VARCHAR
 
640
  },
 
641
  /* MYSQL_TYPE_STRING -> */
 
642
  {
 
643
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
644
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
645
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
646
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
647
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
648
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
649
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
650
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
651
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
652
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
653
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
654
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
655
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
656
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
657
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
658
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR,
 
659
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
 
660
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
 
661
  //MYSQL_TYPE_SET
 
662
    MYSQL_TYPE_STRING,
 
663
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
664
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
 
665
  //MYSQL_TYPE_STRING
 
666
    MYSQL_TYPE_STRING
 
667
  }
383
668
};
384
669
 
385
670
/**
404
689
 
405
690
static Item_result field_types_result_type [FIELDTYPE_NUM]=
406
691
{
407
 
  //DRIZZLE_TYPE_DECIMAL      DRIZZLE_TYPE_TINY
 
692
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
408
693
  DECIMAL_RESULT,           INT_RESULT,
409
 
  //DRIZZLE_TYPE_LONG
410
 
  INT_RESULT,
411
 
  //DRIZZLE_TYPE_DOUBLE
412
 
  REAL_RESULT,
413
 
  //DRIZZLE_TYPE_NULL         DRIZZLE_TYPE_TIMESTAMP
414
 
  STRING_RESULT,            STRING_RESULT,
415
 
  //DRIZZLE_TYPE_LONGLONG
416
 
  INT_RESULT,
417
 
  //DRIZZLE_TYPE_TIME
418
 
  STRING_RESULT,
419
 
  //DRIZZLE_TYPE_DATETIME
420
 
  STRING_RESULT,
421
 
  //DRIZZLE_TYPE_NEWDATE      DRIZZLE_TYPE_VARCHAR
422
 
  STRING_RESULT,            STRING_RESULT,
423
 
  //DRIZZLE_TYPE_NEWDECIMAL   DRIZZLE_TYPE_ENUM
 
694
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
695
  INT_RESULT,               INT_RESULT,
 
696
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
 
697
  REAL_RESULT,              REAL_RESULT,
 
698
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
699
  STRING_RESULT,            STRING_RESULT,
 
700
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
 
701
  INT_RESULT,               INT_RESULT,
 
702
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
 
703
  STRING_RESULT,            STRING_RESULT,
 
704
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
 
705
  STRING_RESULT,            INT_RESULT,
 
706
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
 
707
  STRING_RESULT,            STRING_RESULT,
 
708
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
424
709
  DECIMAL_RESULT,           STRING_RESULT,
425
 
  //DRIZZLE_TYPE_BLOB
 
710
  //MYSQL_TYPE_SET
426
711
  STRING_RESULT,
 
712
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
713
  STRING_RESULT,            STRING_RESULT,
 
714
  //MYSQL_TYPE_STRING
 
715
  STRING_RESULT
427
716
};
428
717
 
429
718
 
444
733
*/
445
734
 
446
735
static bool
447
 
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
448
 
                       const char *strend)
 
736
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
449
737
{
450
738
  if (cs != &my_charset_bin)
451
739
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
491
779
bool Field::type_can_have_key_part(enum enum_field_types type)
492
780
{
493
781
  switch (type) {
494
 
  case DRIZZLE_TYPE_VARCHAR:
495
 
  case DRIZZLE_TYPE_BLOB:
 
782
  case MYSQL_TYPE_VARCHAR:
 
783
  case MYSQL_TYPE_BLOB:
 
784
  case MYSQL_TYPE_VAR_STRING:
 
785
  case MYSQL_TYPE_STRING:
496
786
    return true;
497
787
  default:
498
788
    return false;
503
793
/**
504
794
  Numeric fields base class constructor.
505
795
*/
506
 
Field_num::Field_num(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
507
 
                     unsigned char null_bit_arg, utype unireg_check_arg,
 
796
Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
797
                     uchar null_bit_arg, utype unireg_check_arg,
508
798
                     const char *field_name_arg,
509
 
                     uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
 
799
                     uint8 dec_arg, bool zero_arg, bool unsigned_arg)
510
800
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
511
801
         unireg_check_arg, field_name_arg),
512
 
  dec(dec_arg),decimal_precision(zero_arg),unsigned_flag(unsigned_arg)
 
802
  dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
513
803
{
 
804
  if (zerofill)
 
805
    flags|=ZEROFILL_FLAG;
514
806
  if (unsigned_flag)
515
807
    flags|=UNSIGNED_FLAG;
516
808
}
517
809
 
518
810
 
 
811
void Field_num::prepend_zeros(String *value)
 
812
{
 
813
  int diff;
 
814
  if ((diff= (int) (field_length - value->length())) > 0)
 
815
  {
 
816
    bmove_upp((uchar*) value->ptr()+field_length,
 
817
              (uchar*) value->ptr()+value->length(),
 
818
              value->length());
 
819
    bfill((uchar*) value->ptr(),diff,'0');
 
820
    value->length(field_length);
 
821
    (void) value->c_ptr_quick();                // Avoid warnings in purify
 
822
  }
 
823
}
 
824
 
519
825
/**
520
826
  Test if given number is a int.
521
827
 
538
844
    2   error: garbage at the end of string.
539
845
*/
540
846
 
541
 
int Field_num::check_int(const CHARSET_INFO * const cs, const char *str, int length, 
 
847
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
542
848
                         const char *int_end, int error)
543
849
{
544
850
  /* Test if we get an empty string or wrong integer */
545
851
  if (str == int_end || error == MY_ERRNO_EDOM)
546
852
  {
547
853
    char buff[128];
548
 
    String tmp(buff, (uint32_t) sizeof(buff), system_charset_info);
 
854
    String tmp(buff, (uint32) sizeof(buff), system_charset_info);
549
855
    tmp.copy(str, length, system_charset_info);
550
 
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
856
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
551
857
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
552
858
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
553
859
                        "integer", tmp.c_ptr(), field_name,
554
 
                        (uint32_t) table->in_use->row_count);
 
860
                        (ulong) table->in_use->row_count);
555
861
    return 1;
556
862
  }
557
863
  /* Test if we have garbage at the end of the given string. */
558
864
  if (test_if_important_data(cs, int_end, str + length))
559
865
  {
560
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
866
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
561
867
    return 2;
562
868
  }
563
869
  return 0;
587
893
    1   error
588
894
*/
589
895
 
590
 
bool Field_num::get_int(const CHARSET_INFO * const cs, const char *from, uint32_t len,
 
896
bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len,
591
897
                        int64_t *rnd, uint64_t unsigned_max, 
592
898
                        int64_t signed_min, int64_t signed_max)
593
899
{
625
931
  return 0;
626
932
 
627
933
out_of_range:
628
 
  set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
934
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
629
935
  return 1;
630
936
}
631
937
 
645
951
{
646
952
  if (op_result == E_DEC_OVERFLOW)
647
953
  {
648
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
954
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
649
955
    return 1;
650
956
  }
651
957
  if (op_result == E_DEC_TRUNCATED)
652
958
  {
653
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
959
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
654
960
    /* We return 0 here as this is not a critical issue */
655
961
  }
656
962
  return 0;
658
964
 
659
965
 
660
966
#ifdef NOT_USED
661
 
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
 
967
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
662
968
{
663
969
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
664
970
 
718
1024
  This is used for printing bit_fields as numbers while debugging.
719
1025
*/
720
1026
 
721
 
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
 
1027
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
722
1028
{
723
 
  const CHARSET_INFO * const cs= &my_charset_bin;
724
 
  uint32_t length;
 
1029
  CHARSET_INFO *cs= &my_charset_bin;
 
1030
  uint length;
725
1031
  int64_t value= val_int();
726
1032
 
727
1033
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
728
1034
    return 0;
729
 
  length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
 
1035
  length= (uint) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
730
1036
                                                MY_INT64_NUM_DECIMAL_DIGITS,
731
1037
                                                unsigned_val ? 10 : -10,
732
1038
                                                value);
736
1042
 
737
1043
 
738
1044
/// This is used as a table name when the table structure is not set up
739
 
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
740
 
             unsigned char null_bit_arg,
 
1045
Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
 
1046
             uchar null_bit_arg,
741
1047
             utype unireg_check_arg, const char *field_name_arg)
742
1048
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
743
1049
   table(0), orig_table(0), table_name(0),
754
1060
}
755
1061
 
756
1062
 
757
 
void Field::hash(uint32_t *nr, uint32_t *nr2)
 
1063
void Field::hash(ulong *nr, ulong *nr2)
758
1064
{
759
1065
  if (is_null())
760
1066
  {
762
1068
  }
763
1069
  else
764
1070
  {
765
 
    uint32_t len= pack_length();
766
 
    const CHARSET_INFO * const cs= charset();
 
1071
    uint len= pack_length();
 
1072
    CHARSET_INFO *cs= charset();
767
1073
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
768
1074
  }
769
1075
}
783
1089
  memcpy(ptr,ptr+row_offset,pack_length());
784
1090
  if (null_ptr)
785
1091
  {
786
 
    *null_ptr= (unsigned char) ((null_ptr[0] & (unsigned char) ~(uint32_t) null_bit) | (null_ptr[row_offset] & (unsigned char) null_bit));
 
1092
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | (null_ptr[row_offset] & (uchar) null_bit));
787
1093
  }
788
1094
}
789
1095
 
810
1116
   @retval 0 if this field's size is < the source field's size
811
1117
   @retval 1 if this field's size is >= the source field's size
812
1118
*/
813
 
int Field::compatible_field_size(uint32_t field_metadata)
 
1119
int Field::compatible_field_size(uint field_metadata)
814
1120
{
815
 
  uint32_t const source_size= pack_length_from_metadata(field_metadata);
816
 
  uint32_t const destination_size= row_pack_length();
 
1121
  uint const source_size= pack_length_from_metadata(field_metadata);
 
1122
  uint const destination_size= row_pack_length();
817
1123
  return (source_size <= destination_size);
818
1124
}
819
1125
 
820
1126
 
821
 
int Field::store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
 
1127
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
822
1128
                 enum_check_fields check_level)
823
1129
{
824
1130
  int res;
867
1173
   the value of this flag is a moot point since the native format is
868
1174
   little-endian.
869
1175
*/
870
 
unsigned char *
871
 
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
 
1176
uchar *
 
1177
Field::pack(uchar *to, const uchar *from, uint max_length,
872
1178
            bool low_byte_first __attribute__((unused)))
873
1179
{
874
 
  uint32_t length= pack_length();
 
1180
  uint32 length= pack_length();
875
1181
  set_if_smaller(length, max_length);
876
1182
  memcpy(to, from, length);
877
1183
  return to+length;
907
1213
 
908
1214
   @return  New pointer into memory based on from + length of the data
909
1215
*/
910
 
const unsigned char *
911
 
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
 
1216
const uchar *
 
1217
Field::unpack(uchar* to, const uchar *from, uint param_data,
912
1218
              bool low_byte_first __attribute__((unused)))
913
1219
{
914
 
  uint32_t length=pack_length();
 
1220
  uint length=pack_length();
915
1221
  int from_type= 0;
916
1222
  /*
917
1223
    If from length is > 255, it has encoded data in the upper bits. Need
931
1237
    return from+length;
932
1238
  }
933
1239
 
934
 
  uint32_t len= (param_data && (param_data < length)) ?
 
1240
  uint len= (param_data && (param_data < length)) ?
935
1241
            param_data : length;
936
1242
 
937
1243
  memcpy(to, from, param_data > length ? length : len);
939
1245
}
940
1246
 
941
1247
 
942
 
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((unused)))
 
1248
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((__unused__)))
943
1249
{
944
1250
  /* This never have to be called */
945
1251
  assert(0);
947
1253
}
948
1254
 
949
1255
 
950
 
void Field_num::add_unsigned(String &res) const
 
1256
void Field_num::add_zerofill_and_unsigned(String &res) const
951
1257
{
952
1258
  if (unsigned_flag)
953
1259
    res.append(STRING_WITH_LEN(" unsigned"));
 
1260
  if (zerofill)
 
1261
    res.append(STRING_WITH_LEN(" zerofill"));
954
1262
}
955
1263
 
956
1264
 
1001
1309
  {
1002
1310
    if (val->sign())
1003
1311
    {
1004
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1312
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1005
1313
      i= 0;
1006
1314
      *err= 1;
1007
1315
    }
1017
1325
                                         ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1018
1326
                                         val, false, &i)))
1019
1327
  {
1020
 
    i= (val->sign() ? INT64_MIN : INT64_MAX);
 
1328
    i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
1021
1329
    *err= 1;
1022
1330
  }
1023
1331
  return i;
1069
1377
}
1070
1378
 
1071
1379
 
1072
 
Field_str::Field_str(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
1073
 
                     unsigned char null_bit_arg, utype unireg_check_arg,
1074
 
                     const char *field_name_arg, const CHARSET_INFO * const charset_arg)
 
1380
Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
1381
                     uchar null_bit_arg, utype unireg_check_arg,
 
1382
                     const char *field_name_arg, CHARSET_INFO *charset_arg)
1075
1383
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1076
1384
         unireg_check_arg, field_name_arg)
1077
1385
{
1126
1434
}
1127
1435
 
1128
1436
 
1129
 
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
 
1437
uint Field::fill_cache_field(CACHE_FIELD *copy)
1130
1438
{
1131
 
  uint32_t store_length;
 
1439
  uint store_length;
1132
1440
  copy->str=ptr;
1133
1441
  copy->length=pack_length();
1134
1442
  copy->blob_field=0;
1139
1447
    copy->length-= table->s->blob_ptr_size;
1140
1448
    return copy->length;
1141
1449
  }
 
1450
  else if (!zero_pack() &&
 
1451
           (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
 
1452
            copy->length < 256))
 
1453
  {
 
1454
    copy->strip=1;                              /* Remove end space */
 
1455
    store_length= 2;
 
1456
  }
1142
1457
  else
1143
1458
  {
1144
1459
    copy->strip=0;
1148
1463
}
1149
1464
 
1150
1465
 
1151
 
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1466
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1152
1467
{
1153
1468
  char buff[40];
1154
1469
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1155
1470
  if (!(res=val_str(&tmp)) ||
1156
1471
      str_to_datetime_with_warn(res->ptr(), res->length(),
1157
 
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
1472
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
1158
1473
    return 1;
1159
1474
  return 0;
1160
1475
}
1161
1476
 
1162
 
bool Field::get_time(DRIZZLE_TIME *ltime)
 
1477
bool Field::get_time(MYSQL_TIME *ltime)
1163
1478
{
1164
1479
  char buff[40];
1165
1480
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1176
1491
    Needs to be changed if/when we want to support different time formats.
1177
1492
*/
1178
1493
 
1179
 
int Field::store_time(DRIZZLE_TIME *ltime,
1180
 
                      enum enum_drizzle_timestamp_type type_arg __attribute__((unused)))
 
1494
int Field::store_time(MYSQL_TIME *ltime,
 
1495
                      timestamp_type type_arg __attribute__((__unused__)))
1181
1496
{
1182
1497
  char buff[MAX_DATE_STRING_REP_LENGTH];
1183
 
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
 
1498
  uint length= (uint) my_TIME_to_str(ltime, buff);
1184
1499
  return store(buff, length, &my_charset_bin);
1185
1500
}
1186
1501
 
1187
1502
 
1188
 
bool Field::optimize_range(uint32_t idx, uint32_t part)
 
1503
bool Field::optimize_range(uint idx, uint part)
1189
1504
{
1190
1505
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1191
1506
}
1192
1507
 
1193
1508
 
1194
 
Field *Field::new_field(MEM_ROOT *root, Table *new_table,
 
1509
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
1195
1510
                        bool keep_type __attribute__((unused)))
1196
1511
{
1197
1512
  Field *tmp;
1205
1520
  tmp->part_of_key.init(0);
1206
1521
  tmp->part_of_sortkey.init(0);
1207
1522
  tmp->unireg_check= Field::NONE;
1208
 
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
 
1523
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
 
1524
                ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1209
1525
  tmp->reset_fields();
1210
1526
  return tmp;
1211
1527
}
1212
1528
 
1213
1529
 
1214
 
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
1215
 
                            unsigned char *new_ptr, unsigned char *new_null_ptr,
1216
 
                            uint32_t new_null_bit)
 
1530
Field *Field::new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
1531
                            uchar *new_ptr, uchar *new_null_ptr,
 
1532
                            uint new_null_bit)
1217
1533
{
1218
1534
  Field *tmp;
1219
1535
  if ((tmp= new_field(root, new_table, table == new_table)))
1226
1542
}
1227
1543
 
1228
1544
 
1229
 
/* This is used to generate a field in Table from TABLE_SHARE */
 
1545
/* This is used to generate a field in TABLE from TABLE_SHARE */
1230
1546
 
1231
 
Field *Field::clone(MEM_ROOT *root, Table *new_table)
 
1547
Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
1232
1548
{
1233
1549
  Field *tmp;
1234
1550
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1242
1558
 
1243
1559
 
1244
1560
/****************************************************************************
 
1561
  Field_null, a field that always return NULL
 
1562
****************************************************************************/
 
1563
 
 
1564
void Field_null::sql_type(String &res) const
 
1565
{
 
1566
  res.set_ascii(STRING_WITH_LEN("null"));
 
1567
}
 
1568
 
 
1569
 
 
1570
/****************************************************************************
 
1571
** Field_new_decimal
 
1572
****************************************************************************/
 
1573
 
 
1574
Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
 
1575
                                     uint32 len_arg, uchar *null_ptr_arg,
 
1576
                                     uchar null_bit_arg,
 
1577
                                     enum utype unireg_check_arg,
 
1578
                                     const char *field_name_arg,
 
1579
                                     uint8 dec_arg,bool zero_arg,
 
1580
                                     bool unsigned_arg)
 
1581
  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1582
             unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
 
1583
{
 
1584
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
1585
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
 
1586
  assert((precision <= DECIMAL_MAX_PRECISION) &&
 
1587
              (dec <= DECIMAL_MAX_SCALE));
 
1588
  bin_size= my_decimal_get_binary_size(precision, dec);
 
1589
}
 
1590
 
 
1591
 
 
1592
Field_new_decimal::Field_new_decimal(uint32 len_arg,
 
1593
                                     bool maybe_null_arg,
 
1594
                                     const char *name,
 
1595
                                     uint8 dec_arg,
 
1596
                                     bool unsigned_arg)
 
1597
  :Field_num((uchar*) 0, len_arg,
 
1598
             maybe_null_arg ? (uchar*) "": 0, 0,
 
1599
             NONE, name, dec_arg, 0, unsigned_arg)
 
1600
{
 
1601
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
1602
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
 
1603
  assert((precision <= DECIMAL_MAX_PRECISION) &&
 
1604
              (dec <= DECIMAL_MAX_SCALE));
 
1605
  bin_size= my_decimal_get_binary_size(precision, dec);
 
1606
}
 
1607
 
 
1608
 
 
1609
int Field_new_decimal::reset(void)
 
1610
{
 
1611
  store_value(&decimal_zero);
 
1612
  return 0;
 
1613
}
 
1614
 
 
1615
 
 
1616
/**
 
1617
  Generate max/min decimal value in case of overflow.
 
1618
 
 
1619
  @param decimal_value     buffer for value
 
1620
  @param sign              sign of value which caused overflow
 
1621
*/
 
1622
 
 
1623
void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
 
1624
                                              bool sign)
 
1625
{
 
1626
  max_my_decimal(decimal_value, precision, decimals());
 
1627
  if (sign)
 
1628
  {
 
1629
    if (unsigned_flag)
 
1630
      my_decimal_set_zero(decimal_value);
 
1631
    else
 
1632
      decimal_value->sign(true);
 
1633
  }
 
1634
  return;
 
1635
}
 
1636
 
 
1637
 
 
1638
/**
 
1639
  Store decimal value in the binary buffer.
 
1640
 
 
1641
  Checks if decimal_value fits into field size.
 
1642
  If it does, stores the decimal in the buffer using binary format.
 
1643
  Otherwise sets maximal number that can be stored in the field.
 
1644
 
 
1645
  @param decimal_value   my_decimal
 
1646
 
 
1647
  @retval
 
1648
    0 ok
 
1649
  @retval
 
1650
    1 error
 
1651
*/
 
1652
 
 
1653
bool Field_new_decimal::store_value(const my_decimal *decimal_value)
 
1654
{
 
1655
  int error= 0;
 
1656
 
 
1657
  /* check that we do not try to write negative value in unsigned field */
 
1658
  if (unsigned_flag && decimal_value->sign())
 
1659
  {
 
1660
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1661
    error= 1;
 
1662
    decimal_value= &decimal_zero;
 
1663
  }
 
1664
 
 
1665
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
1666
                                         decimal_value, ptr, precision, dec)))
 
1667
  {
 
1668
    my_decimal buff;
 
1669
    set_value_on_overflow(&buff, decimal_value->sign());
 
1670
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
 
1671
    error= 1;
 
1672
  }
 
1673
  return(error);
 
1674
}
 
1675
 
 
1676
 
 
1677
int Field_new_decimal::store(const char *from, uint length,
 
1678
                             CHARSET_INFO *charset_arg)
 
1679
{
 
1680
  int err;
 
1681
  my_decimal decimal_value;
 
1682
 
 
1683
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
 
1684
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
 
1685
                           from, length, charset_arg,
 
1686
                           &decimal_value)) &&
 
1687
      table->in_use->abort_on_warning)
 
1688
  {
 
1689
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
 
1690
    String from_as_str;
 
1691
    from_as_str.copy(from, length, &my_charset_bin);
 
1692
 
 
1693
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_ERROR,
 
1694
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
1695
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
1696
                        "decimal", from_as_str.c_ptr(), field_name,
 
1697
                        (ulong) table->in_use->row_count);
 
1698
 
 
1699
    return(err);
 
1700
  }
 
1701
 
 
1702
  switch (err) {
 
1703
  case E_DEC_TRUNCATED:
 
1704
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
 
1705
    break;
 
1706
  case E_DEC_OVERFLOW:
 
1707
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1708
    set_value_on_overflow(&decimal_value, decimal_value.sign());
 
1709
    break;
 
1710
  case E_DEC_BAD_NUM:
 
1711
    {
 
1712
      /* Because "from" is not NUL-terminated and we use %s in the ER() */
 
1713
      String from_as_str;
 
1714
      from_as_str.copy(from, length, &my_charset_bin);
 
1715
 
 
1716
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
 
1717
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
1718
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
1719
                          "decimal", from_as_str.c_ptr(), field_name,
 
1720
                        (ulong) table->in_use->row_count);
 
1721
    my_decimal_set_zero(&decimal_value);
 
1722
 
 
1723
    break;
 
1724
    }
 
1725
  }
 
1726
 
 
1727
  store_value(&decimal_value);
 
1728
  return(err);
 
1729
}
 
1730
 
 
1731
 
 
1732
/**
 
1733
  @todo
 
1734
  Fix following when double2my_decimal when double2decimal
 
1735
  will return E_DEC_TRUNCATED always correctly
 
1736
*/
 
1737
 
 
1738
int Field_new_decimal::store(double nr)
 
1739
{
 
1740
  my_decimal decimal_value;
 
1741
  int err;
 
1742
 
 
1743
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
 
1744
                         &decimal_value);
 
1745
  if (err)
 
1746
  {
 
1747
    if (check_overflow(err))
 
1748
      set_value_on_overflow(&decimal_value, decimal_value.sign());
 
1749
    /* Only issue a warning if store_value doesn't issue an warning */
 
1750
    table->in_use->got_warning= 0;
 
1751
  }
 
1752
  if (store_value(&decimal_value))
 
1753
    err= 1;
 
1754
  else if (err && !table->in_use->got_warning)
 
1755
    err= warn_if_overflow(err);
 
1756
  return(err);
 
1757
}
 
1758
 
 
1759
 
 
1760
int Field_new_decimal::store(int64_t nr, bool unsigned_val)
 
1761
{
 
1762
  my_decimal decimal_value;
 
1763
  int err;
 
1764
 
 
1765
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
1766
                           nr, unsigned_val, &decimal_value)))
 
1767
  {
 
1768
    if (check_overflow(err))
 
1769
      set_value_on_overflow(&decimal_value, decimal_value.sign());
 
1770
    /* Only issue a warning if store_value doesn't issue an warning */
 
1771
    table->in_use->got_warning= 0;
 
1772
  }
 
1773
  if (store_value(&decimal_value))
 
1774
    err= 1;
 
1775
  else if (err && !table->in_use->got_warning)
 
1776
    err= warn_if_overflow(err);
 
1777
  return err;
 
1778
}
 
1779
 
 
1780
 
 
1781
int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
 
1782
{
 
1783
  return store_value(decimal_value);
 
1784
}
 
1785
 
 
1786
 
 
1787
int Field_new_decimal::store_time(MYSQL_TIME *ltime,
 
1788
                                  timestamp_type t_type __attribute__((__unused__)))
 
1789
{
 
1790
    my_decimal decimal_value;
 
1791
    return store_value(date2my_decimal(ltime, &decimal_value));
 
1792
}
 
1793
 
 
1794
 
 
1795
double Field_new_decimal::val_real(void)
 
1796
{
 
1797
  double dbl;
 
1798
  my_decimal decimal_value;
 
1799
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
1800
  return dbl;
 
1801
}
 
1802
 
 
1803
 
 
1804
int64_t Field_new_decimal::val_int(void)
 
1805
{
 
1806
  int64_t i;
 
1807
  my_decimal decimal_value;
 
1808
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
1809
                 unsigned_flag, &i);
 
1810
  return i;
 
1811
}
 
1812
 
 
1813
 
 
1814
my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
 
1815
{
 
1816
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
 
1817
                    precision, dec);
 
1818
  return(decimal_value);
 
1819
}
 
1820
 
 
1821
 
 
1822
String *Field_new_decimal::val_str(String *val_buffer,
 
1823
                                   String *val_ptr __attribute__((unused)))
 
1824
{
 
1825
  my_decimal decimal_value;
 
1826
  uint fixed_precision= zerofill ? precision : 0;
 
1827
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
1828
                    fixed_precision, dec, '0', val_buffer);
 
1829
  return val_buffer;
 
1830
}
 
1831
 
 
1832
 
 
1833
int Field_new_decimal::cmp(const uchar *a,const uchar*b)
 
1834
{
 
1835
  return memcmp(a, b, bin_size);
 
1836
}
 
1837
 
 
1838
 
 
1839
void Field_new_decimal::sort_string(uchar *buff,
 
1840
                                    uint length __attribute__((unused)))
 
1841
{
 
1842
  memcpy(buff, ptr, bin_size);
 
1843
}
 
1844
 
 
1845
 
 
1846
void Field_new_decimal::sql_type(String &str) const
 
1847
{
 
1848
  CHARSET_INFO *cs= str.charset();
 
1849
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
 
1850
                                "decimal(%d,%d)", precision, (int)dec));
 
1851
  add_zerofill_and_unsigned(str);
 
1852
}
 
1853
 
 
1854
 
 
1855
/**
 
1856
   Save the field metadata for new decimal fields.
 
1857
 
 
1858
   Saves the precision in the first byte and decimals() in the second
 
1859
   byte of the field metadata array at index of *metadata_ptr and 
 
1860
   *(metadata_ptr + 1).
 
1861
 
 
1862
   @param   metadata_ptr   First byte of field metadata
 
1863
 
 
1864
   @returns number of bytes written to metadata_ptr
 
1865
*/
 
1866
int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
 
1867
{
 
1868
  *metadata_ptr= precision;
 
1869
  *(metadata_ptr + 1)= decimals();
 
1870
  return 2;
 
1871
}
 
1872
 
 
1873
 
 
1874
/**
 
1875
   Returns the number of bytes field uses in row-based replication 
 
1876
   row packed size.
 
1877
 
 
1878
   This method is used in row-based replication to determine the number
 
1879
   of bytes that the field consumes in the row record format. This is
 
1880
   used to skip fields in the master that do not exist on the slave.
 
1881
 
 
1882
   @param   field_metadata   Encoded size in field metadata
 
1883
 
 
1884
   @returns The size of the field based on the field metadata.
 
1885
*/
 
1886
uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
 
1887
{
 
1888
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
 
1889
  uint const source_decimal= field_metadata & 0x00ff; 
 
1890
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
1891
                                                     source_decimal);
 
1892
  return (source_size);
 
1893
}
 
1894
 
 
1895
 
 
1896
/**
 
1897
   Check to see if field size is compatible with destination.
 
1898
 
 
1899
   This method is used in row-based replication to verify that the slave's
 
1900
   field size is less than or equal to the master's field size. The 
 
1901
   encoded field metadata (from the master or source) is decoded and compared
 
1902
   to the size of this field (the slave or destination). 
 
1903
 
 
1904
   @param   field_metadata   Encoded size in field metadata
 
1905
 
 
1906
   @retval 0 if this field's size is < the source field's size
 
1907
   @retval 1 if this field's size is >= the source field's size
 
1908
*/
 
1909
int Field_new_decimal::compatible_field_size(uint field_metadata)
 
1910
{
 
1911
  int compatible= 0;
 
1912
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
 
1913
  uint const source_decimal= field_metadata & 0x00ff; 
 
1914
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
1915
                                                     source_decimal);
 
1916
  uint const destination_size= row_pack_length();
 
1917
  compatible= (source_size <= destination_size);
 
1918
  if (compatible)
 
1919
    compatible= (source_precision <= precision) &&
 
1920
                (source_decimal <= decimals());
 
1921
  return (compatible);
 
1922
}
 
1923
 
 
1924
 
 
1925
uint Field_new_decimal::is_equal(Create_field *new_field)
 
1926
{
 
1927
  return ((new_field->sql_type == real_type()) &&
 
1928
          ((new_field->flags & UNSIGNED_FLAG) == 
 
1929
           (uint) (flags & UNSIGNED_FLAG)) &&
 
1930
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
 
1931
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
 
1932
          (new_field->length == max_display_length()) &&
 
1933
          (new_field->decimals == dec));
 
1934
}
 
1935
 
 
1936
 
 
1937
/**
 
1938
   Unpack a decimal field from row data.
 
1939
 
 
1940
   This method is used to unpack a decimal or numeric field from a master
 
1941
   whose size of the field is less than that of the slave.
 
1942
  
 
1943
   @param   to         Destination of the data
 
1944
   @param   from       Source of the data
 
1945
   @param   param_data Precision (upper) and decimal (lower) values
 
1946
 
 
1947
   @return  New pointer into memory based on from + length of the data
 
1948
*/
 
1949
const uchar *
 
1950
Field_new_decimal::unpack(uchar* to,
 
1951
                          const uchar *from,
 
1952
                          uint param_data,
 
1953
                          bool low_byte_first)
 
1954
{
 
1955
  if (param_data == 0)
 
1956
    return Field::unpack(to, from, param_data, low_byte_first);
 
1957
 
 
1958
  uint from_precision= (param_data & 0xff00) >> 8U;
 
1959
  uint from_decimal= param_data & 0x00ff;
 
1960
  uint length=pack_length();
 
1961
  uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
 
1962
  uint len= (param_data && (from_pack_len < length)) ?
 
1963
            from_pack_len : length;
 
1964
  if ((from_pack_len && (from_pack_len < length)) ||
 
1965
      (from_precision < precision) ||
 
1966
      (from_decimal < decimals()))
 
1967
  {
 
1968
    /*
 
1969
      If the master's data is smaller than the slave, we need to convert
 
1970
      the binary to decimal then resize the decimal converting it back to
 
1971
      a decimal and write that to the raw data buffer.
 
1972
    */
 
1973
    decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
 
1974
    decimal_t dec;
 
1975
    dec.len= from_precision;
 
1976
    dec.buf= dec_buf;
 
1977
    /*
 
1978
      Note: bin2decimal does not change the length of the field. So it is
 
1979
      just the first step the resizing operation. The second step does the
 
1980
      resizing using the precision and decimals from the slave.
 
1981
    */
 
1982
    bin2decimal((uchar *)from, &dec, from_precision, from_decimal);
 
1983
    decimal2bin(&dec, to, precision, decimals());
 
1984
  }
 
1985
  else
 
1986
    memcpy(to, from, len); // Sizes are the same, just copy the data.
 
1987
  return from+len;
 
1988
}
 
1989
 
 
1990
/****************************************************************************
1245
1991
** tiny int
1246
1992
****************************************************************************/
1247
1993
 
1248
 
int Field_tiny::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
1994
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
1249
1995
{
1250
1996
  int error;
1251
1997
  int64_t rnd;
1265
2011
    if (nr < 0.0)
1266
2012
    {
1267
2013
      *ptr=0;
1268
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2014
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1269
2015
      error= 1;
1270
2016
    }
1271
2017
    else if (nr > 255.0)
1272
2018
    {
1273
2019
      *ptr=(char) 255;
1274
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2020
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1275
2021
      error= 1;
1276
2022
    }
1277
2023
    else
1282
2028
    if (nr < -128.0)
1283
2029
    {
1284
2030
      *ptr= (char) -128;
1285
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2031
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1286
2032
      error= 1;
1287
2033
    }
1288
2034
    else if (nr > 127.0)
1289
2035
    {
1290
2036
      *ptr=127;
1291
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2037
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1292
2038
      error= 1;
1293
2039
    }
1294
2040
    else
1307
2053
    if (nr < 0 && !unsigned_val)
1308
2054
    {
1309
2055
      *ptr= 0;
1310
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2056
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1311
2057
      error= 1;
1312
2058
    }
1313
2059
    else if ((uint64_t) nr > (uint64_t) 255)
1314
2060
    {
1315
2061
      *ptr= (char) 255;
1316
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2062
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1317
2063
      error= 1;
1318
2064
    }
1319
2065
    else
1326
2072
    if (nr < -128)
1327
2073
    {
1328
2074
      *ptr= (char) -128;
1329
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2075
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1330
2076
      error= 1;
1331
2077
    }
1332
2078
    else if (nr > 127)
1333
2079
    {
1334
2080
      *ptr=127;
1335
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2081
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1336
2082
      error= 1;
1337
2083
    }
1338
2084
    else
1361
2107
String *Field_tiny::val_str(String *val_buffer,
1362
2108
                            String *val_ptr __attribute__((unused)))
1363
2109
{
1364
 
  const CHARSET_INFO * const cs= &my_charset_bin;
1365
 
  uint32_t length;
1366
 
  uint32_t mlength=cmax(field_length+1,5*cs->mbmaxlen);
 
2110
  CHARSET_INFO *cs= &my_charset_bin;
 
2111
  uint length;
 
2112
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
1367
2113
  val_buffer->alloc(mlength);
1368
2114
  char *to=(char*) val_buffer->ptr();
1369
2115
 
1370
2116
  if (unsigned_flag)
1371
 
    length= (uint32_t) cs->cset->long10_to_str(cs,to,mlength, 10,
 
2117
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
1372
2118
                                           (long) *ptr);
1373
2119
  else
1374
 
    length= (uint32_t) cs->cset->long10_to_str(cs,to,mlength,-10,
 
2120
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
1375
2121
                                           (long) *((signed char*) ptr));
1376
2122
  
1377
2123
  val_buffer->length(length);
1378
 
 
 
2124
  if (zerofill)
 
2125
    prepend_zeros(val_buffer);
1379
2126
  return val_buffer;
1380
2127
}
1381
2128
 
1382
2129
bool Field_tiny::send_binary(Protocol *protocol)
1383
2130
{
1384
 
  return protocol->store_tiny((int64_t) (int8_t) ptr[0]);
 
2131
  return protocol->store_tiny((int64_t) (int8) ptr[0]);
1385
2132
}
1386
2133
 
1387
 
int Field_tiny::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
2134
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
1388
2135
{
1389
2136
  signed char a,b;
1390
2137
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
1391
2138
  if (unsigned_flag)
1392
 
    return ((unsigned char) a < (unsigned char) b) ? -1 : ((unsigned char) a > (unsigned char) b) ? 1 : 0;
 
2139
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
1393
2140
  return (a < b) ? -1 : (a > b) ? 1 : 0;
1394
2141
}
1395
2142
 
1396
 
void Field_tiny::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
 
2143
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
1397
2144
{
1398
2145
  if (unsigned_flag)
1399
2146
    *to= *ptr;
1400
2147
  else
1401
 
    to[0] = (char) (ptr[0] ^ (unsigned char) 128);      /* Revers signbit */
 
2148
    to[0] = (char) (ptr[0] ^ (uchar) 128);      /* Revers signbit */
1402
2149
}
1403
2150
 
1404
2151
void Field_tiny::sql_type(String &res) const
1405
2152
{
1406
 
  const CHARSET_INFO * const cs=res.charset();
 
2153
  CHARSET_INFO *cs=res.charset();
1407
2154
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1408
2155
                          "tinyint(%d)",(int) field_length));
1409
 
  add_unsigned(res);
1410
 
}
1411
 
 
 
2156
  add_zerofill_and_unsigned(res);
 
2157
}
 
2158
 
 
2159
/****************************************************************************
 
2160
 Field type short int (2 byte)
 
2161
****************************************************************************/
 
2162
 
 
2163
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
 
2164
{
 
2165
  int store_tmp;
 
2166
  int error;
 
2167
  int64_t rnd;
 
2168
  
 
2169
  error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
 
2170
  store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
 
2171
#ifdef WORDS_BIGENDIAN
 
2172
  if (table->s->db_low_byte_first)
 
2173
  {
 
2174
    int2store(ptr, store_tmp);
 
2175
  }
 
2176
  else
 
2177
#endif
 
2178
    shortstore(ptr, (short) store_tmp);
 
2179
  return error;
 
2180
}
 
2181
 
 
2182
 
 
2183
int Field_short::store(double nr)
 
2184
{
 
2185
  int error= 0;
 
2186
  int16 res;
 
2187
  nr=rint(nr);
 
2188
  if (unsigned_flag)
 
2189
  {
 
2190
    if (nr < 0)
 
2191
    {
 
2192
      res=0;
 
2193
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2194
      error= 1;
 
2195
    }
 
2196
    else if (nr > (double) UINT_MAX16)
 
2197
    {
 
2198
      res=(int16) UINT_MAX16;
 
2199
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2200
      error= 1;
 
2201
    }
 
2202
    else
 
2203
      res=(int16) (uint16) nr;
 
2204
  }
 
2205
  else
 
2206
  {
 
2207
    if (nr < (double) INT_MIN16)
 
2208
    {
 
2209
      res=INT_MIN16;
 
2210
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2211
      error= 1;
 
2212
    }
 
2213
    else if (nr > (double) INT_MAX16)
 
2214
    {
 
2215
      res=INT_MAX16;
 
2216
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2217
      error= 1;
 
2218
    }
 
2219
    else
 
2220
      res=(int16) (int) nr;
 
2221
  }
 
2222
#ifdef WORDS_BIGENDIAN
 
2223
  if (table->s->db_low_byte_first)
 
2224
  {
 
2225
    int2store(ptr,res);
 
2226
  }
 
2227
  else
 
2228
#endif
 
2229
    shortstore(ptr,res);
 
2230
  return error;
 
2231
}
 
2232
 
 
2233
 
 
2234
int Field_short::store(int64_t nr, bool unsigned_val)
 
2235
{
 
2236
  int error= 0;
 
2237
  int16 res;
 
2238
 
 
2239
  if (unsigned_flag)
 
2240
  {
 
2241
    if (nr < 0L && !unsigned_val)
 
2242
    {
 
2243
      res=0;
 
2244
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2245
      error= 1;
 
2246
    }
 
2247
    else if ((uint64_t) nr > (uint64_t) UINT_MAX16)
 
2248
    {
 
2249
      res=(int16) UINT_MAX16;
 
2250
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2251
      error= 1;
 
2252
    }
 
2253
    else
 
2254
      res=(int16) (uint16) nr;
 
2255
  }
 
2256
  else
 
2257
  {
 
2258
    if (nr < 0 && unsigned_val)
 
2259
      nr= UINT_MAX16+1;                         // Generate overflow
 
2260
 
 
2261
    if (nr < INT_MIN16)
 
2262
    {
 
2263
      res=INT_MIN16;
 
2264
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2265
      error= 1;
 
2266
    }
 
2267
    else if (nr > (int64_t) INT_MAX16)
 
2268
    {
 
2269
      res=INT_MAX16;
 
2270
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2271
      error= 1;
 
2272
    }
 
2273
    else
 
2274
      res=(int16) nr;
 
2275
  }
 
2276
#ifdef WORDS_BIGENDIAN
 
2277
  if (table->s->db_low_byte_first)
 
2278
  {
 
2279
    int2store(ptr,res);
 
2280
  }
 
2281
  else
 
2282
#endif
 
2283
    shortstore(ptr,res);
 
2284
  return error;
 
2285
}
 
2286
 
 
2287
 
 
2288
double Field_short::val_real(void)
 
2289
{
 
2290
  short j;
 
2291
#ifdef WORDS_BIGENDIAN
 
2292
  if (table->s->db_low_byte_first)
 
2293
    j=sint2korr(ptr);
 
2294
  else
 
2295
#endif
 
2296
    shortget(j,ptr);
 
2297
  return unsigned_flag ? (double) (unsigned short) j : (double) j;
 
2298
}
 
2299
 
 
2300
int64_t Field_short::val_int(void)
 
2301
{
 
2302
  short j;
 
2303
#ifdef WORDS_BIGENDIAN
 
2304
  if (table->s->db_low_byte_first)
 
2305
    j=sint2korr(ptr);
 
2306
  else
 
2307
#endif
 
2308
    shortget(j,ptr);
 
2309
  return unsigned_flag ? (int64_t) (unsigned short) j : (int64_t) j;
 
2310
}
 
2311
 
 
2312
 
 
2313
String *Field_short::val_str(String *val_buffer,
 
2314
                             String *val_ptr __attribute__((unused)))
 
2315
{
 
2316
  CHARSET_INFO *cs= &my_charset_bin;
 
2317
  uint length;
 
2318
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
 
2319
  val_buffer->alloc(mlength);
 
2320
  char *to=(char*) val_buffer->ptr();
 
2321
  short j;
 
2322
#ifdef WORDS_BIGENDIAN
 
2323
  if (table->s->db_low_byte_first)
 
2324
    j=sint2korr(ptr);
 
2325
  else
 
2326
#endif
 
2327
    shortget(j,ptr);
 
2328
 
 
2329
  if (unsigned_flag)
 
2330
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
 
2331
                                          (long) (uint16) j);
 
2332
  else
 
2333
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
 
2334
  val_buffer->length(length);
 
2335
  if (zerofill)
 
2336
    prepend_zeros(val_buffer);
 
2337
  return val_buffer;
 
2338
}
 
2339
 
 
2340
 
 
2341
bool Field_short::send_binary(Protocol *protocol)
 
2342
{
 
2343
  return protocol->store_short(Field_short::val_int());
 
2344
}
 
2345
 
 
2346
 
 
2347
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
2348
{
 
2349
  short a,b;
 
2350
#ifdef WORDS_BIGENDIAN
 
2351
  if (table->s->db_low_byte_first)
 
2352
  {
 
2353
    a=sint2korr(a_ptr);
 
2354
    b=sint2korr(b_ptr);
 
2355
  }
 
2356
  else
 
2357
#endif
 
2358
  {
 
2359
    shortget(a,a_ptr);
 
2360
    shortget(b,b_ptr);
 
2361
  }
 
2362
 
 
2363
  if (unsigned_flag)
 
2364
    return ((unsigned short) a < (unsigned short) b) ? -1 :
 
2365
    ((unsigned short) a > (unsigned short) b) ? 1 : 0;
 
2366
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
2367
}
 
2368
 
 
2369
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
 
2370
{
 
2371
#ifdef WORDS_BIGENDIAN
 
2372
  if (!table->s->db_low_byte_first)
 
2373
  {
 
2374
    if (unsigned_flag)
 
2375
      to[0] = ptr[0];
 
2376
    else
 
2377
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
2378
    to[1]   = ptr[1];
 
2379
  }
 
2380
  else
 
2381
#endif
 
2382
  {
 
2383
    if (unsigned_flag)
 
2384
      to[0] = ptr[1];
 
2385
    else
 
2386
      to[0] = (char) (ptr[1] ^ 128);            /* Revers signbit */
 
2387
    to[1]   = ptr[0];
 
2388
  }
 
2389
}
 
2390
 
 
2391
void Field_short::sql_type(String &res) const
 
2392
{
 
2393
  CHARSET_INFO *cs=res.charset();
 
2394
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2395
                          "smallint(%d)",(int) field_length));
 
2396
  add_zerofill_and_unsigned(res);
 
2397
}
 
2398
 
 
2399
 
 
2400
/****************************************************************************
 
2401
** long int
 
2402
****************************************************************************/
 
2403
 
 
2404
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
 
2405
{
 
2406
  long store_tmp;
 
2407
  int error;
 
2408
  int64_t rnd;
 
2409
  
 
2410
  error= get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
 
2411
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
 
2412
#ifdef WORDS_BIGENDIAN
 
2413
  if (table->s->db_low_byte_first)
 
2414
  {
 
2415
    int4store(ptr, store_tmp);
 
2416
  }
 
2417
  else
 
2418
#endif
 
2419
    longstore(ptr, store_tmp);
 
2420
  return error;
 
2421
}
 
2422
 
 
2423
 
 
2424
int Field_long::store(double nr)
 
2425
{
 
2426
  int error= 0;
 
2427
  int32 res;
 
2428
  nr=rint(nr);
 
2429
  if (unsigned_flag)
 
2430
  {
 
2431
    if (nr < 0)
 
2432
    {
 
2433
      res=0;
 
2434
      error= 1;
 
2435
    }
 
2436
    else if (nr > (double) UINT_MAX32)
 
2437
    {
 
2438
      res= INT_MAX32;
 
2439
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2440
      error= 1;
 
2441
    }
 
2442
    else
 
2443
      res=(int32) (ulong) nr;
 
2444
  }
 
2445
  else
 
2446
  {
 
2447
    if (nr < (double) INT_MIN32)
 
2448
    {
 
2449
      res=(int32) INT_MIN32;
 
2450
      error= 1;
 
2451
    }
 
2452
    else if (nr > (double) INT_MAX32)
 
2453
    {
 
2454
      res=(int32) INT_MAX32;
 
2455
      error= 1;
 
2456
    }
 
2457
    else
 
2458
      res=(int32) (int64_t) nr;
 
2459
  }
 
2460
  if (error)
 
2461
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2462
 
 
2463
#ifdef WORDS_BIGENDIAN
 
2464
  if (table->s->db_low_byte_first)
 
2465
  {
 
2466
    int4store(ptr,res);
 
2467
  }
 
2468
  else
 
2469
#endif
 
2470
    longstore(ptr,res);
 
2471
  return error;
 
2472
}
 
2473
 
 
2474
 
 
2475
int Field_long::store(int64_t nr, bool unsigned_val)
 
2476
{
 
2477
  int error= 0;
 
2478
  int32 res;
 
2479
 
 
2480
  if (unsigned_flag)
 
2481
  {
 
2482
    if (nr < 0 && !unsigned_val)
 
2483
    {
 
2484
      res=0;
 
2485
      error= 1;
 
2486
    }
 
2487
    else if ((uint64_t) nr >= (1LL << 32))
 
2488
    {
 
2489
      res=(int32) (uint32) ~0L;
 
2490
      error= 1;
 
2491
    }
 
2492
    else
 
2493
      res=(int32) (uint32) nr;
 
2494
  }
 
2495
  else
 
2496
  {
 
2497
    if (nr < 0 && unsigned_val)
 
2498
      nr= ((int64_t) INT_MAX32) + 1;           // Generate overflow
 
2499
    if (nr < (int64_t) INT_MIN32) 
 
2500
    {
 
2501
      res=(int32) INT_MIN32;
 
2502
      error= 1;
 
2503
    }
 
2504
    else if (nr > (int64_t) INT_MAX32)
 
2505
    {
 
2506
      res=(int32) INT_MAX32;
 
2507
      error= 1;
 
2508
    }
 
2509
    else
 
2510
      res=(int32) nr;
 
2511
  }
 
2512
  if (error)
 
2513
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2514
 
 
2515
#ifdef WORDS_BIGENDIAN
 
2516
  if (table->s->db_low_byte_first)
 
2517
  {
 
2518
    int4store(ptr,res);
 
2519
  }
 
2520
  else
 
2521
#endif
 
2522
    longstore(ptr,res);
 
2523
  return error;
 
2524
}
 
2525
 
 
2526
 
 
2527
double Field_long::val_real(void)
 
2528
{
 
2529
  int32 j;
 
2530
#ifdef WORDS_BIGENDIAN
 
2531
  if (table->s->db_low_byte_first)
 
2532
    j=sint4korr(ptr);
 
2533
  else
 
2534
#endif
 
2535
    longget(j,ptr);
 
2536
  return unsigned_flag ? (double) (uint32) j : (double) j;
 
2537
}
 
2538
 
 
2539
int64_t Field_long::val_int(void)
 
2540
{
 
2541
  int32 j;
 
2542
  /* See the comment in Field_long::store(long long) */
 
2543
  assert(table->in_use == current_thd);
 
2544
#ifdef WORDS_BIGENDIAN
 
2545
  if (table->s->db_low_byte_first)
 
2546
    j=sint4korr(ptr);
 
2547
  else
 
2548
#endif
 
2549
    longget(j,ptr);
 
2550
  return unsigned_flag ? (int64_t) (uint32) j : (int64_t) j;
 
2551
}
 
2552
 
 
2553
String *Field_long::val_str(String *val_buffer,
 
2554
                            String *val_ptr __attribute__((unused)))
 
2555
{
 
2556
  CHARSET_INFO *cs= &my_charset_bin;
 
2557
  uint length;
 
2558
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
 
2559
  val_buffer->alloc(mlength);
 
2560
  char *to=(char*) val_buffer->ptr();
 
2561
  int32 j;
 
2562
#ifdef WORDS_BIGENDIAN
 
2563
  if (table->s->db_low_byte_first)
 
2564
    j=sint4korr(ptr);
 
2565
  else
 
2566
#endif
 
2567
    longget(j,ptr);
 
2568
 
 
2569
  if (unsigned_flag)
 
2570
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
 
2571
  else
 
2572
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
 
2573
  val_buffer->length(length);
 
2574
  if (zerofill)
 
2575
    prepend_zeros(val_buffer);
 
2576
  return val_buffer;
 
2577
}
 
2578
 
 
2579
 
 
2580
bool Field_long::send_binary(Protocol *protocol)
 
2581
{
 
2582
  return protocol->store_long(Field_long::val_int());
 
2583
}
 
2584
 
 
2585
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
2586
{
 
2587
  int32 a,b;
 
2588
#ifdef WORDS_BIGENDIAN
 
2589
  if (table->s->db_low_byte_first)
 
2590
  {
 
2591
    a=sint4korr(a_ptr);
 
2592
    b=sint4korr(b_ptr);
 
2593
  }
 
2594
  else
 
2595
#endif
 
2596
  {
 
2597
    longget(a,a_ptr);
 
2598
    longget(b,b_ptr);
 
2599
  }
 
2600
  if (unsigned_flag)
 
2601
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
 
2602
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
2603
}
 
2604
 
 
2605
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
 
2606
{
 
2607
#ifdef WORDS_BIGENDIAN
 
2608
  if (!table->s->db_low_byte_first)
 
2609
  {
 
2610
    if (unsigned_flag)
 
2611
      to[0] = ptr[0];
 
2612
    else
 
2613
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
2614
    to[1]   = ptr[1];
 
2615
    to[2]   = ptr[2];
 
2616
    to[3]   = ptr[3];
 
2617
  }
 
2618
  else
 
2619
#endif
 
2620
  {
 
2621
    if (unsigned_flag)
 
2622
      to[0] = ptr[3];
 
2623
    else
 
2624
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
 
2625
    to[1]   = ptr[2];
 
2626
    to[2]   = ptr[1];
 
2627
    to[3]   = ptr[0];
 
2628
  }
 
2629
}
 
2630
 
 
2631
 
 
2632
void Field_long::sql_type(String &res) const
 
2633
{
 
2634
  CHARSET_INFO *cs=res.charset();
 
2635
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2636
                          "int(%d)",(int) field_length));
 
2637
  add_zerofill_and_unsigned(res);
 
2638
}
 
2639
 
 
2640
/****************************************************************************
 
2641
 Field type int64_t int (8 bytes)
 
2642
****************************************************************************/
 
2643
 
 
2644
int Field_int64_t::store(const char *from,uint len,CHARSET_INFO *cs)
 
2645
{
 
2646
  int error= 0;
 
2647
  char *end;
 
2648
  uint64_t tmp;
 
2649
 
 
2650
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
 
2651
  if (error == MY_ERRNO_ERANGE)
 
2652
  {
 
2653
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2654
    error= 1;
 
2655
  }
 
2656
  else if (table->in_use->count_cuted_fields && 
 
2657
           check_int(cs, from, len, end, error))
 
2658
    error= 1;
 
2659
  else
 
2660
    error= 0;
 
2661
#ifdef WORDS_BIGENDIAN
 
2662
  if (table->s->db_low_byte_first)
 
2663
  {
 
2664
    int8store(ptr,tmp);
 
2665
  }
 
2666
  else
 
2667
#endif
 
2668
    int64_tstore(ptr,tmp);
 
2669
  return error;
 
2670
}
 
2671
 
 
2672
 
 
2673
int Field_int64_t::store(double nr)
 
2674
{
 
2675
  int error= 0;
 
2676
  int64_t res;
 
2677
 
 
2678
  nr= rint(nr);
 
2679
  if (unsigned_flag)
 
2680
  {
 
2681
    if (nr < 0)
 
2682
    {
 
2683
      res=0;
 
2684
      error= 1;
 
2685
    }
 
2686
    else if (nr >= (double) ULONGLONG_MAX)
 
2687
    {
 
2688
      res= ~(int64_t) 0;
 
2689
      error= 1;
 
2690
    }
 
2691
    else
 
2692
      res=(int64_t) (uint64_t) nr;
 
2693
  }
 
2694
  else
 
2695
  {
 
2696
    if (nr <= (double) LONGLONG_MIN)
 
2697
    {
 
2698
      res= LONGLONG_MIN;
 
2699
      error= (nr < (double) LONGLONG_MIN);
 
2700
    }
 
2701
    else if (nr >= (double) (uint64_t) LONGLONG_MAX)
 
2702
    {
 
2703
      res= LONGLONG_MAX;
 
2704
      error= (nr > (double) LONGLONG_MAX);
 
2705
    }
 
2706
    else
 
2707
      res=(int64_t) nr;
 
2708
  }
 
2709
  if (error)
 
2710
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2711
 
 
2712
#ifdef WORDS_BIGENDIAN
 
2713
  if (table->s->db_low_byte_first)
 
2714
  {
 
2715
    int8store(ptr,res);
 
2716
  }
 
2717
  else
 
2718
#endif
 
2719
    int64_tstore(ptr,res);
 
2720
  return error;
 
2721
}
 
2722
 
 
2723
 
 
2724
int Field_int64_t::store(int64_t nr, bool unsigned_val)
 
2725
{
 
2726
  int error= 0;
 
2727
 
 
2728
  if (nr < 0)                                   // Only possible error
 
2729
  {
 
2730
    /*
 
2731
      if field is unsigned and value is signed (< 0) or
 
2732
      if field is signed and value is unsigned we have an overflow
 
2733
    */
 
2734
    if (unsigned_flag != unsigned_val)
 
2735
    {
 
2736
      nr= unsigned_flag ? (uint64_t) 0 : (uint64_t) LONGLONG_MAX;
 
2737
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2738
      error= 1;
 
2739
    }
 
2740
  }
 
2741
 
 
2742
#ifdef WORDS_BIGENDIAN
 
2743
  if (table->s->db_low_byte_first)
 
2744
  {
 
2745
    int8store(ptr,nr);
 
2746
  }
 
2747
  else
 
2748
#endif
 
2749
    int64_tstore(ptr,nr);
 
2750
  return error;
 
2751
}
 
2752
 
 
2753
 
 
2754
double Field_int64_t::val_real(void)
 
2755
{
 
2756
  int64_t j;
 
2757
#ifdef WORDS_BIGENDIAN
 
2758
  if (table->s->db_low_byte_first)
 
2759
  {
 
2760
    j=sint8korr(ptr);
 
2761
  }
 
2762
  else
 
2763
#endif
 
2764
    int64_tget(j,ptr);
 
2765
  /* The following is open coded to avoid a bug in gcc 3.3 */
 
2766
  if (unsigned_flag)
 
2767
  {
 
2768
    uint64_t tmp= (uint64_t) j;
 
2769
    return uint64_t2double(tmp);
 
2770
  }
 
2771
  return (double) j;
 
2772
}
 
2773
 
 
2774
 
 
2775
int64_t Field_int64_t::val_int(void)
 
2776
{
 
2777
  int64_t j;
 
2778
#ifdef WORDS_BIGENDIAN
 
2779
  if (table->s->db_low_byte_first)
 
2780
    j=sint8korr(ptr);
 
2781
  else
 
2782
#endif
 
2783
    int64_tget(j,ptr);
 
2784
  return j;
 
2785
}
 
2786
 
 
2787
 
 
2788
String *Field_int64_t::val_str(String *val_buffer,
 
2789
                                String *val_ptr __attribute__((unused)))
 
2790
{
 
2791
  CHARSET_INFO *cs= &my_charset_bin;
 
2792
  uint length;
 
2793
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
 
2794
  val_buffer->alloc(mlength);
 
2795
  char *to=(char*) val_buffer->ptr();
 
2796
  int64_t j;
 
2797
#ifdef WORDS_BIGENDIAN
 
2798
  if (table->s->db_low_byte_first)
 
2799
    j=sint8korr(ptr);
 
2800
  else
 
2801
#endif
 
2802
    int64_tget(j,ptr);
 
2803
 
 
2804
  length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength,
 
2805
                                        unsigned_flag ? 10 : -10, j);
 
2806
  val_buffer->length(length);
 
2807
  if (zerofill)
 
2808
    prepend_zeros(val_buffer);
 
2809
  return val_buffer;
 
2810
}
 
2811
 
 
2812
 
 
2813
bool Field_int64_t::send_binary(Protocol *protocol)
 
2814
{
 
2815
  return protocol->store_int64_t(Field_int64_t::val_int(), unsigned_flag);
 
2816
}
 
2817
 
 
2818
 
 
2819
int Field_int64_t::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
2820
{
 
2821
  int64_t a,b;
 
2822
#ifdef WORDS_BIGENDIAN
 
2823
  if (table->s->db_low_byte_first)
 
2824
  {
 
2825
    a=sint8korr(a_ptr);
 
2826
    b=sint8korr(b_ptr);
 
2827
  }
 
2828
  else
 
2829
#endif
 
2830
  {
 
2831
    int64_tget(a,a_ptr);
 
2832
    int64_tget(b,b_ptr);
 
2833
  }
 
2834
  if (unsigned_flag)
 
2835
    return ((uint64_t) a < (uint64_t) b) ? -1 :
 
2836
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
2837
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
2838
}
 
2839
 
 
2840
void Field_int64_t::sort_string(uchar *to,uint length __attribute__((unused)))
 
2841
{
 
2842
#ifdef WORDS_BIGENDIAN
 
2843
  if (!table->s->db_low_byte_first)
 
2844
  {
 
2845
    if (unsigned_flag)
 
2846
      to[0] = ptr[0];
 
2847
    else
 
2848
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
2849
    to[1]   = ptr[1];
 
2850
    to[2]   = ptr[2];
 
2851
    to[3]   = ptr[3];
 
2852
    to[4]   = ptr[4];
 
2853
    to[5]   = ptr[5];
 
2854
    to[6]   = ptr[6];
 
2855
    to[7]   = ptr[7];
 
2856
  }
 
2857
  else
 
2858
#endif
 
2859
  {
 
2860
    if (unsigned_flag)
 
2861
      to[0] = ptr[7];
 
2862
    else
 
2863
      to[0] = (char) (ptr[7] ^ 128);            /* Revers signbit */
 
2864
    to[1]   = ptr[6];
 
2865
    to[2]   = ptr[5];
 
2866
    to[3]   = ptr[4];
 
2867
    to[4]   = ptr[3];
 
2868
    to[5]   = ptr[2];
 
2869
    to[6]   = ptr[1];
 
2870
    to[7]   = ptr[0];
 
2871
  }
 
2872
}
 
2873
 
 
2874
 
 
2875
void Field_int64_t::sql_type(String &res) const
 
2876
{
 
2877
  CHARSET_INFO *cs=res.charset();
 
2878
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2879
                          "bigint(%d)",(int) field_length));
 
2880
  add_zerofill_and_unsigned(res);
 
2881
}
 
2882
 
 
2883
 
 
2884
/*
 
2885
  Floating-point numbers
 
2886
 */
 
2887
 
 
2888
uchar *
 
2889
Field_real::pack(uchar *to, const uchar *from,
 
2890
                 uint max_length, bool low_byte_first)
 
2891
{
 
2892
  assert(max_length >= pack_length());
 
2893
#ifdef WORDS_BIGENDIAN
 
2894
  if (low_byte_first != table->s->db_low_byte_first)
 
2895
  {
 
2896
    const uchar *dptr= from + pack_length();
 
2897
    while (dptr-- > from)
 
2898
      *to++ = *dptr;
 
2899
    return(to);
 
2900
  }
 
2901
  else
 
2902
#endif
 
2903
    return(Field::pack(to, from, max_length, low_byte_first));
 
2904
}
 
2905
 
 
2906
const uchar *
 
2907
Field_real::unpack(uchar *to, const uchar *from,
 
2908
                   uint param_data, bool low_byte_first)
 
2909
{
 
2910
#ifdef WORDS_BIGENDIAN
 
2911
  if (low_byte_first != table->s->db_low_byte_first)
 
2912
  {
 
2913
    const uchar *dptr= from + pack_length();
 
2914
    while (dptr-- > from)
 
2915
      *to++ = *dptr;
 
2916
    return(from + pack_length());
 
2917
  }
 
2918
  else
 
2919
#endif
 
2920
    return(Field::unpack(to, from, param_data, low_byte_first));
 
2921
}
 
2922
 
 
2923
/****************************************************************************
 
2924
  single precision float
 
2925
****************************************************************************/
 
2926
 
 
2927
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
 
2928
{
 
2929
  int error;
 
2930
  char *end;
 
2931
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
 
2932
  if (error || (!len || (((uint)(end-from) != len) && table->in_use->count_cuted_fields)))
 
2933
  {
 
2934
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
2935
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
 
2936
    error= error ? 1 : 2;
 
2937
  }
 
2938
  Field_float::store(nr);
 
2939
  return error;
 
2940
}
 
2941
 
 
2942
 
 
2943
int Field_float::store(double nr)
 
2944
{
 
2945
  int error= truncate(&nr, FLT_MAX);
 
2946
  float j= (float)nr;
 
2947
 
 
2948
#ifdef WORDS_BIGENDIAN
 
2949
  if (table->s->db_low_byte_first)
 
2950
  {
 
2951
    float4store(ptr,j);
 
2952
  }
 
2953
  else
 
2954
#endif
 
2955
    memcpy_fixed(ptr,(uchar*) &j,sizeof(j));
 
2956
  return error;
 
2957
}
 
2958
 
 
2959
 
 
2960
int Field_float::store(int64_t nr, bool unsigned_val)
 
2961
{
 
2962
  return Field_float::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
 
2963
                            (double) nr);
 
2964
}
 
2965
 
 
2966
 
 
2967
double Field_float::val_real(void)
 
2968
{
 
2969
  float j;
 
2970
#ifdef WORDS_BIGENDIAN
 
2971
  if (table->s->db_low_byte_first)
 
2972
  {
 
2973
    float4get(j,ptr);
 
2974
  }
 
2975
  else
 
2976
#endif
 
2977
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
 
2978
  return ((double) j);
 
2979
}
 
2980
 
 
2981
int64_t Field_float::val_int(void)
 
2982
{
 
2983
  float j;
 
2984
#ifdef WORDS_BIGENDIAN
 
2985
  if (table->s->db_low_byte_first)
 
2986
  {
 
2987
    float4get(j,ptr);
 
2988
  }
 
2989
  else
 
2990
#endif
 
2991
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
 
2992
  return (int64_t) rint(j);
 
2993
}
 
2994
 
 
2995
 
 
2996
String *Field_float::val_str(String *val_buffer,
 
2997
                             String *val_ptr __attribute__((unused)))
 
2998
{
 
2999
  float nr;
 
3000
#ifdef WORDS_BIGENDIAN
 
3001
  if (table->s->db_low_byte_first)
 
3002
  {
 
3003
    float4get(nr,ptr);
 
3004
  }
 
3005
  else
 
3006
#endif
 
3007
    memcpy_fixed((uchar*) &nr,ptr,sizeof(nr));
 
3008
 
 
3009
  uint to_length=max(field_length,70);
 
3010
  val_buffer->alloc(to_length);
 
3011
  char *to=(char*) val_buffer->ptr();
 
3012
  size_t len;
 
3013
 
 
3014
  if (dec >= NOT_FIXED_DEC)
 
3015
    len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
 
3016
  else
 
3017
  {
 
3018
    /*
 
3019
      We are safe here because the buffer length is >= 70, and
 
3020
      fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
 
3021
      will be not longer than 69 chars + terminating '\0'.
 
3022
    */
 
3023
    len= my_fcvt(nr, dec, to, NULL);
 
3024
  }
 
3025
  val_buffer->length((uint) len);
 
3026
  if (zerofill)
 
3027
    prepend_zeros(val_buffer);
 
3028
  return val_buffer;
 
3029
}
 
3030
 
 
3031
 
 
3032
int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3033
{
 
3034
  float a,b;
 
3035
#ifdef WORDS_BIGENDIAN
 
3036
  if (table->s->db_low_byte_first)
 
3037
  {
 
3038
    float4get(a,a_ptr);
 
3039
    float4get(b,b_ptr);
 
3040
  }
 
3041
  else
 
3042
#endif
 
3043
  {
 
3044
    memcpy_fixed(&a,a_ptr,sizeof(float));
 
3045
    memcpy_fixed(&b,b_ptr,sizeof(float));
 
3046
  }
 
3047
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3048
}
 
3049
 
 
3050
#define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
 
3051
 
 
3052
void Field_float::sort_string(uchar *to,uint length __attribute__((unused)))
 
3053
{
 
3054
  float nr;
 
3055
#ifdef WORDS_BIGENDIAN
 
3056
  if (table->s->db_low_byte_first)
 
3057
  {
 
3058
    float4get(nr,ptr);
 
3059
  }
 
3060
  else
 
3061
#endif
 
3062
    memcpy_fixed(&nr,ptr,sizeof(float));
 
3063
 
 
3064
  uchar *tmp= to;
 
3065
  if (nr == (float) 0.0)
 
3066
  {                                             /* Change to zero string */
 
3067
    tmp[0]=(uchar) 128;
 
3068
    bzero((char*) tmp+1,sizeof(nr)-1);
 
3069
  }
 
3070
  else
 
3071
  {
 
3072
#ifdef WORDS_BIGENDIAN
 
3073
    memcpy_fixed(tmp,&nr,sizeof(nr));
 
3074
#else
 
3075
    tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
 
3076
#endif
 
3077
    if (tmp[0] & 128)                           /* Negative */
 
3078
    {                                           /* make complement */
 
3079
      uint i;
 
3080
      for (i=0 ; i < sizeof(nr); i++)
 
3081
        tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
 
3082
    }
 
3083
    else
 
3084
    {
 
3085
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
 
3086
                       (ushort) 32768);
 
3087
      exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
 
3088
      tmp[0]= (uchar) (exp_part >> 8);
 
3089
      tmp[1]= (uchar) exp_part;
 
3090
    }
 
3091
  }
 
3092
}
 
3093
 
 
3094
 
 
3095
bool Field_float::send_binary(Protocol *protocol)
 
3096
{
 
3097
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
 
3098
}
 
3099
 
 
3100
 
 
3101
/**
 
3102
   Save the field metadata for float fields.
 
3103
 
 
3104
   Saves the pack length in the first byte.
 
3105
 
 
3106
   @param   metadata_ptr   First byte of field metadata
 
3107
 
 
3108
   @returns number of bytes written to metadata_ptr
 
3109
*/
 
3110
int Field_float::do_save_field_metadata(uchar *metadata_ptr)
 
3111
{
 
3112
  *metadata_ptr= pack_length();
 
3113
  return 1;
 
3114
}
 
3115
 
 
3116
 
 
3117
void Field_float::sql_type(String &res) const
 
3118
{
 
3119
  if (dec == NOT_FIXED_DEC)
 
3120
  {
 
3121
    res.set_ascii(STRING_WITH_LEN("float"));
 
3122
  }
 
3123
  else
 
3124
  {
 
3125
    CHARSET_INFO *cs= res.charset();
 
3126
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
3127
                            "float(%d,%d)",(int) field_length,dec));
 
3128
  }
 
3129
  add_zerofill_and_unsigned(res);
 
3130
}
 
3131
 
 
3132
 
 
3133
/****************************************************************************
 
3134
  double precision floating point numbers
 
3135
****************************************************************************/
 
3136
 
 
3137
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
 
3138
{
 
3139
  int error;
 
3140
  char *end;
 
3141
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
 
3142
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
 
3143
  {
 
3144
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3145
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
 
3146
    error= error ? 1 : 2;
 
3147
  }
 
3148
  Field_double::store(nr);
 
3149
  return error;
 
3150
}
 
3151
 
 
3152
 
 
3153
int Field_double::store(double nr)
 
3154
{
 
3155
  int error= truncate(&nr, DBL_MAX);
 
3156
 
 
3157
#ifdef WORDS_BIGENDIAN
 
3158
  if (table->s->db_low_byte_first)
 
3159
  {
 
3160
    float8store(ptr,nr);
 
3161
  }
 
3162
  else
 
3163
#endif
 
3164
    doublestore(ptr,nr);
 
3165
  return error;
 
3166
}
 
3167
 
 
3168
 
 
3169
int Field_double::store(int64_t nr, bool unsigned_val)
 
3170
{
 
3171
  return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
 
3172
                             (double) nr);
 
3173
}
 
3174
 
 
3175
/*
 
3176
  If a field has fixed length, truncate the double argument pointed to by 'nr'
 
3177
  appropriately.
 
3178
  Also ensure that the argument is within [-max_value; max_value] range.
 
3179
*/
 
3180
 
 
3181
int Field_real::truncate(double *nr, double max_value)
 
3182
{
 
3183
  int error= 1;
 
3184
  double res= *nr;
 
3185
  
 
3186
  if (isnan(res))
 
3187
  {
 
3188
    res= 0;
 
3189
    set_null();
 
3190
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3191
    goto end;
 
3192
  }
 
3193
  else if (unsigned_flag && res < 0)
 
3194
  {
 
3195
    res= 0;
 
3196
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3197
    goto end;
 
3198
  }
 
3199
 
 
3200
  if (!not_fixed)
 
3201
  {
 
3202
    uint order= field_length - dec;
 
3203
    uint step= array_elements(log_10) - 1;
 
3204
    max_value= 1.0;
 
3205
    for (; order > step; order-= step)
 
3206
      max_value*= log_10[step];
 
3207
    max_value*= log_10[order];
 
3208
    max_value-= 1.0 / log_10[dec];
 
3209
 
 
3210
    /* Check for infinity so we don't get NaN in calculations */
 
3211
    if (!my_isinf(res))
 
3212
    {
 
3213
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
 
3214
      res= floor(res) + tmp;
 
3215
    }
 
3216
  }
 
3217
  
 
3218
  if (res < -max_value)
 
3219
  {
 
3220
   res= -max_value;
 
3221
   set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3222
  }
 
3223
  else if (res > max_value)
 
3224
  {
 
3225
    res= max_value;
 
3226
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
3227
  }
 
3228
  else
 
3229
    error= 0;
 
3230
 
 
3231
end:
 
3232
  *nr= res;
 
3233
  return error;
 
3234
}
 
3235
 
 
3236
 
 
3237
int Field_real::store_decimal(const my_decimal *dm)
 
3238
{
 
3239
  double dbl;
 
3240
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
 
3241
  return store(dbl);
 
3242
}
 
3243
 
 
3244
double Field_double::val_real(void)
 
3245
{
 
3246
  double j;
 
3247
#ifdef WORDS_BIGENDIAN
 
3248
  if (table->s->db_low_byte_first)
 
3249
  {
 
3250
    float8get(j,ptr);
 
3251
  }
 
3252
  else
 
3253
#endif
 
3254
    doubleget(j,ptr);
 
3255
  return j;
 
3256
}
 
3257
 
 
3258
int64_t Field_double::val_int(void)
 
3259
{
 
3260
  double j;
 
3261
  int64_t res;
 
3262
#ifdef WORDS_BIGENDIAN
 
3263
  if (table->s->db_low_byte_first)
 
3264
  {
 
3265
    float8get(j,ptr);
 
3266
  }
 
3267
  else
 
3268
#endif
 
3269
    doubleget(j,ptr);
 
3270
  /* Check whether we fit into int64_t range */
 
3271
  if (j <= (double) LONGLONG_MIN)
 
3272
  {
 
3273
    res= (int64_t) LONGLONG_MIN;
 
3274
    goto warn;
 
3275
  }
 
3276
  if (j >= (double) (uint64_t) LONGLONG_MAX)
 
3277
  {
 
3278
    res= (int64_t) LONGLONG_MAX;
 
3279
    goto warn;
 
3280
  }
 
3281
  return (int64_t) rint(j);
 
3282
 
 
3283
warn:
 
3284
  {
 
3285
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
3286
    String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
 
3287
    str= val_str(&tmp, 0);
 
3288
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
3289
                        ER_TRUNCATED_WRONG_VALUE,
 
3290
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
 
3291
                        str->c_ptr());
 
3292
  }
 
3293
  return res;
 
3294
}
 
3295
 
 
3296
 
 
3297
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
 
3298
{
 
3299
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
 
3300
  return decimal_value;
 
3301
}
 
3302
 
 
3303
 
 
3304
String *Field_double::val_str(String *val_buffer,
 
3305
                              String *val_ptr __attribute__((unused)))
 
3306
{
 
3307
  double nr;
 
3308
#ifdef WORDS_BIGENDIAN
 
3309
  if (table->s->db_low_byte_first)
 
3310
  {
 
3311
    float8get(nr,ptr);
 
3312
  }
 
3313
  else
 
3314
#endif
 
3315
    doubleget(nr,ptr);
 
3316
 
 
3317
  uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
 
3318
  val_buffer->alloc(to_length);
 
3319
  char *to=(char*) val_buffer->ptr();
 
3320
  size_t len;
 
3321
 
 
3322
  if (dec >= NOT_FIXED_DEC)
 
3323
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
 
3324
  else
 
3325
    len= my_fcvt(nr, dec, to, NULL);
 
3326
 
 
3327
  val_buffer->length((uint) len);
 
3328
  if (zerofill)
 
3329
    prepend_zeros(val_buffer);
 
3330
  return val_buffer;
 
3331
}
 
3332
 
 
3333
bool Field_double::send_binary(Protocol *protocol)
 
3334
{
 
3335
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
 
3336
}
 
3337
 
 
3338
 
 
3339
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3340
{
 
3341
  double a,b;
 
3342
#ifdef WORDS_BIGENDIAN
 
3343
  if (table->s->db_low_byte_first)
 
3344
  {
 
3345
    float8get(a,a_ptr);
 
3346
    float8get(b,b_ptr);
 
3347
  }
 
3348
  else
 
3349
#endif
 
3350
  {
 
3351
    doubleget(a, a_ptr);
 
3352
    doubleget(b, b_ptr);
 
3353
  }
 
3354
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
3355
}
 
3356
 
 
3357
 
 
3358
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
 
3359
 
 
3360
/* The following should work for IEEE */
 
3361
 
 
3362
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
 
3363
{
 
3364
  double nr;
 
3365
#ifdef WORDS_BIGENDIAN
 
3366
  if (table->s->db_low_byte_first)
 
3367
  {
 
3368
    float8get(nr,ptr);
 
3369
  }
 
3370
  else
 
3371
#endif
 
3372
    doubleget(nr,ptr);
 
3373
  change_double_for_sort(nr, to);
 
3374
}
 
3375
 
 
3376
 
 
3377
/**
 
3378
   Save the field metadata for double fields.
 
3379
 
 
3380
   Saves the pack length in the first byte of the field metadata array
 
3381
   at index of *metadata_ptr.
 
3382
 
 
3383
   @param   metadata_ptr   First byte of field metadata
 
3384
 
 
3385
   @returns number of bytes written to metadata_ptr
 
3386
*/
 
3387
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
 
3388
{
 
3389
  *metadata_ptr= pack_length();
 
3390
  return 1;
 
3391
}
 
3392
 
 
3393
 
 
3394
void Field_double::sql_type(String &res) const
 
3395
{
 
3396
  CHARSET_INFO *cs=res.charset();
 
3397
  if (dec == NOT_FIXED_DEC)
 
3398
  {
 
3399
    res.set_ascii(STRING_WITH_LEN("double"));
 
3400
  }
 
3401
  else
 
3402
  {
 
3403
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
3404
                            "double(%d,%d)",(int) field_length,dec));
 
3405
  }
 
3406
  add_zerofill_and_unsigned(res);
 
3407
}
 
3408
 
 
3409
 
 
3410
/**
 
3411
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to 
 
3412
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix 
 
3413
  Epoch in UTC.
 
3414
  
 
3415
  Up to one of timestamps columns in the table can be automatically 
 
3416
  set on row update and/or have NOW() as default value.
 
3417
  TABLE::timestamp_field points to Field object for such timestamp with 
 
3418
  auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
 
3419
  field, and is used by handler code which performs updates required.
 
3420
  
 
3421
  Actually SQL-99 says that we should allow niladic functions (like NOW())
 
3422
  as defaults for any field. Current limitations (only NOW() and only 
 
3423
  for one TIMESTAMP field) are because of restricted binary .frm format 
 
3424
  and should go away in the future.
 
3425
  
 
3426
  Also because of this limitation of binary .frm format we use 5 different
 
3427
  unireg_check values with TIMESTAMP field to distinguish various cases of
 
3428
  DEFAULT or ON UPDATE values. These values are:
 
3429
  
 
3430
  TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
 
3431
    auto-set-on-update (or now() as default) in this table before, then this 
 
3432
    field has NOW() as default and is updated when row changes, else it is 
 
3433
    field which has 0 as default value and is not automatically updated.
 
3434
  TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
 
3435
    automatically (TIMESTAMP DEFAULT NOW())
 
3436
  TIMESTAMP_UN_FIELD - field which is set on update automatically but has not 
 
3437
    NOW() as default (but it may has 0 or some other const timestamp as 
 
3438
    default) (TIMESTAMP ON UPDATE NOW()).
 
3439
  TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on 
 
3440
    update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
 
3441
  NONE - field which is not auto-set on update with some other than NOW() 
 
3442
    default value (TIMESTAMP DEFAULT 0).
 
3443
 
 
3444
  Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are 
 
3445
  left only for preserving ability to read old tables. Such fields replaced 
 
3446
  with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is 
 
3447
  because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for 
 
3448
  "TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such 
 
3449
  specification too but ignored default value for first timestamp, which of 
 
3450
  course is non-standard.) In most cases user won't notice any change, only
 
3451
  exception is different behavior of old/new timestamps during ALTER TABLE.
 
3452
 */
 
3453
 
 
3454
Field_timestamp::Field_timestamp(uchar *ptr_arg,
 
3455
                                 uint32 len_arg __attribute__((__unused__)),
 
3456
                                 uchar *null_ptr_arg, uchar null_bit_arg,
 
3457
                                 enum utype unireg_check_arg,
 
3458
                                 const char *field_name_arg,
 
3459
                                 TABLE_SHARE *share,
 
3460
                                 CHARSET_INFO *cs)
 
3461
  :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
 
3462
             unireg_check_arg, field_name_arg, cs)
 
3463
{
 
3464
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
3465
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
3466
  if (!share->timestamp_field && unireg_check != NONE)
 
3467
  {
 
3468
    /* This timestamp has auto-update */
 
3469
    share->timestamp_field= this;
 
3470
    flags|= TIMESTAMP_FLAG;
 
3471
    if (unireg_check != TIMESTAMP_DN_FIELD)
 
3472
      flags|= ON_UPDATE_NOW_FLAG;
 
3473
  }
 
3474
}
 
3475
 
 
3476
 
 
3477
Field_timestamp::Field_timestamp(bool maybe_null_arg,
 
3478
                                 const char *field_name_arg,
 
3479
                                 CHARSET_INFO *cs)
 
3480
  :Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
 
3481
             maybe_null_arg ? (uchar*) "": 0, 0,
 
3482
             NONE, field_name_arg, cs)
 
3483
{
 
3484
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
3485
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
3486
    if (unireg_check != TIMESTAMP_DN_FIELD)
 
3487
      flags|= ON_UPDATE_NOW_FLAG;
 
3488
}
 
3489
 
 
3490
 
 
3491
/**
 
3492
  Get auto-set type for TIMESTAMP field.
 
3493
 
 
3494
  Returns value indicating during which operations this TIMESTAMP field
 
3495
  should be auto-set to current timestamp.
 
3496
*/
 
3497
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
 
3498
{
 
3499
  switch (unireg_check)
 
3500
  {
 
3501
  case TIMESTAMP_DN_FIELD:
 
3502
    return TIMESTAMP_AUTO_SET_ON_INSERT;
 
3503
  case TIMESTAMP_UN_FIELD:
 
3504
    return TIMESTAMP_AUTO_SET_ON_UPDATE;
 
3505
  case TIMESTAMP_OLD_FIELD:
 
3506
    /*
 
3507
      Although we can have several such columns in legacy tables this
 
3508
      function should be called only for first of them (i.e. the one
 
3509
      having auto-set property).
 
3510
    */
 
3511
    assert(table->timestamp_field == this);
 
3512
    /* Fall-through */
 
3513
  case TIMESTAMP_DNUN_FIELD:
 
3514
    return TIMESTAMP_AUTO_SET_ON_BOTH;
 
3515
  default:
 
3516
    /*
 
3517
      Normally this function should not be called for TIMESTAMPs without
 
3518
      auto-set property.
 
3519
    */
 
3520
    assert(0);
 
3521
    return TIMESTAMP_NO_AUTO_SET;
 
3522
  }
 
3523
}
 
3524
 
 
3525
 
 
3526
int Field_timestamp::store(const char *from,
 
3527
                           uint len,
 
3528
                           CHARSET_INFO *cs __attribute__((__unused__)))
 
3529
{
 
3530
  MYSQL_TIME l_time;
 
3531
  my_time_t tmp= 0;
 
3532
  int error;
 
3533
  bool have_smth_to_conv;
 
3534
  bool in_dst_time_gap;
 
3535
  THD *thd= table ? table->in_use : current_thd;
 
3536
 
 
3537
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
 
3538
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 1, &error) >
 
3539
                      MYSQL_TIMESTAMP_ERROR);
 
3540
 
 
3541
  if (error || !have_smth_to_conv)
 
3542
  {
 
3543
    error= 1;
 
3544
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
3545
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
 
3546
  }
 
3547
 
 
3548
  /* Only convert a correct date (not a zero date) */
 
3549
  if (have_smth_to_conv && l_time.month)
 
3550
  {
 
3551
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
 
3552
    {
 
3553
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3554
                           ER_WARN_DATA_OUT_OF_RANGE,
 
3555
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
 
3556
      error= 1;
 
3557
    }
 
3558
    else if (in_dst_time_gap)
 
3559
    {
 
3560
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3561
                           ER_WARN_INVALID_TIMESTAMP,
 
3562
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
 
3563
      error= 1;
 
3564
    }
 
3565
  }
 
3566
  store_timestamp(tmp);
 
3567
  return error;
 
3568
}
 
3569
 
 
3570
 
 
3571
int Field_timestamp::store(double nr)
 
3572
{
 
3573
  int error= 0;
 
3574
  if (nr < 0 || nr > 99991231235959.0)
 
3575
  {
 
3576
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3577
                         ER_WARN_DATA_OUT_OF_RANGE,
 
3578
                         nr, MYSQL_TIMESTAMP_DATETIME);
 
3579
    nr= 0;                                      // Avoid overflow on buff
 
3580
    error= 1;
 
3581
  }
 
3582
  error|= Field_timestamp::store((int64_t) rint(nr), false);
 
3583
  return error;
 
3584
}
 
3585
 
 
3586
 
 
3587
int Field_timestamp::store(int64_t nr,
 
3588
                           bool unsigned_val __attribute__((__unused__)))
 
3589
{
 
3590
  MYSQL_TIME l_time;
 
3591
  my_time_t timestamp= 0;
 
3592
  int error;
 
3593
  bool in_dst_time_gap;
 
3594
  THD *thd= table ? table->in_use : current_thd;
 
3595
 
 
3596
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
 
3597
  int64_t tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
 
3598
                                                 MODE_NO_ZERO_DATE) |
 
3599
                                   MODE_NO_ZERO_IN_DATE, &error);
 
3600
  if (tmp == -1LL)
 
3601
  {
 
3602
    error= 2;
 
3603
  }
 
3604
 
 
3605
  if (!error && tmp)
 
3606
  {
 
3607
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
 
3608
    {
 
3609
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3610
                           ER_WARN_DATA_OUT_OF_RANGE,
 
3611
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
3612
      error= 1;
 
3613
    }
 
3614
    if (in_dst_time_gap)
 
3615
    {
 
3616
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3617
                           ER_WARN_INVALID_TIMESTAMP,
 
3618
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
3619
      error= 1;
 
3620
    }
 
3621
  } else if (error)
 
3622
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3623
                         WARN_DATA_TRUNCATED,
 
3624
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
 
3625
 
 
3626
  store_timestamp(timestamp);
 
3627
  return error;
 
3628
}
 
3629
 
 
3630
double Field_timestamp::val_real(void)
 
3631
{
 
3632
  return (double) Field_timestamp::val_int();
 
3633
}
 
3634
 
 
3635
int64_t Field_timestamp::val_int(void)
 
3636
{
 
3637
  uint32 temp;
 
3638
  MYSQL_TIME time_tmp;
 
3639
  THD  *thd= table ? table->in_use : current_thd;
 
3640
 
 
3641
  thd->time_zone_used= 1;
 
3642
#ifdef WORDS_BIGENDIAN
 
3643
  if (table && table->s->db_low_byte_first)
 
3644
    temp=uint4korr(ptr);
 
3645
  else
 
3646
#endif
 
3647
    longget(temp,ptr);
 
3648
 
 
3649
  if (temp == 0L)                               // No time
 
3650
    return(0);                                  /* purecov: inspected */
 
3651
  
 
3652
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
 
3653
  
 
3654
  return time_tmp.year * 10000000000LL + time_tmp.month * 100000000LL +
 
3655
         time_tmp.day * 1000000L + time_tmp.hour * 10000L +
 
3656
         time_tmp.minute * 100 + time_tmp.second;
 
3657
}
 
3658
 
 
3659
 
 
3660
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
 
3661
{
 
3662
  uint32 temp, temp2;
 
3663
  MYSQL_TIME time_tmp;
 
3664
  THD *thd= table ? table->in_use : current_thd;
 
3665
  char *to;
 
3666
 
 
3667
  val_buffer->alloc(field_length+1);
 
3668
  to= (char*) val_buffer->ptr();
 
3669
  val_buffer->length(field_length);
 
3670
 
 
3671
  thd->time_zone_used= 1;
 
3672
#ifdef WORDS_BIGENDIAN
 
3673
  if (table && table->s->db_low_byte_first)
 
3674
    temp=uint4korr(ptr);
 
3675
  else
 
3676
#endif
 
3677
    longget(temp,ptr);
 
3678
 
 
3679
  if (temp == 0L)
 
3680
  {                                   /* Zero time is "000000" */
 
3681
    val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_bin);
 
3682
    return val_ptr;
 
3683
  }
 
3684
  val_buffer->set_charset(&my_charset_bin);     // Safety
 
3685
  
 
3686
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
 
3687
 
 
3688
  temp= time_tmp.year % 100;
 
3689
  if (temp < YY_PART_YEAR - 1)
 
3690
  {
 
3691
    *to++= '2';
 
3692
    *to++= '0';
 
3693
  }
 
3694
  else
 
3695
  {
 
3696
    *to++= '1';
 
3697
    *to++= '9';
 
3698
  }
 
3699
  temp2=temp/10; temp=temp-temp2*10;
 
3700
  *to++= (char) ('0'+(char) (temp2));
 
3701
  *to++= (char) ('0'+(char) (temp));
 
3702
  *to++= '-';
 
3703
  temp=time_tmp.month;
 
3704
  temp2=temp/10; temp=temp-temp2*10;
 
3705
  *to++= (char) ('0'+(char) (temp2));
 
3706
  *to++= (char) ('0'+(char) (temp));
 
3707
  *to++= '-';
 
3708
  temp=time_tmp.day;
 
3709
  temp2=temp/10; temp=temp-temp2*10;
 
3710
  *to++= (char) ('0'+(char) (temp2));
 
3711
  *to++= (char) ('0'+(char) (temp));
 
3712
  *to++= ' ';
 
3713
  temp=time_tmp.hour;
 
3714
  temp2=temp/10; temp=temp-temp2*10;
 
3715
  *to++= (char) ('0'+(char) (temp2));
 
3716
  *to++= (char) ('0'+(char) (temp));
 
3717
  *to++= ':';
 
3718
  temp=time_tmp.minute;
 
3719
  temp2=temp/10; temp=temp-temp2*10;
 
3720
  *to++= (char) ('0'+(char) (temp2));
 
3721
  *to++= (char) ('0'+(char) (temp));
 
3722
  *to++= ':';
 
3723
  temp=time_tmp.second;
 
3724
  temp2=temp/10; temp=temp-temp2*10;
 
3725
  *to++= (char) ('0'+(char) (temp2));
 
3726
  *to++= (char) ('0'+(char) (temp));
 
3727
  *to= 0;
 
3728
  return val_buffer;
 
3729
}
 
3730
 
 
3731
 
 
3732
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
3733
{
 
3734
  long temp;
 
3735
  THD *thd= table ? table->in_use : current_thd;
 
3736
  thd->time_zone_used= 1;
 
3737
#ifdef WORDS_BIGENDIAN
 
3738
  if (table && table->s->db_low_byte_first)
 
3739
    temp=uint4korr(ptr);
 
3740
  else
 
3741
#endif
 
3742
    longget(temp,ptr);
 
3743
  if (temp == 0L)
 
3744
  {                                   /* Zero time is "000000" */
 
3745
    if (fuzzydate & TIME_NO_ZERO_DATE)
 
3746
      return 1;
 
3747
    bzero((char*) ltime,sizeof(*ltime));
 
3748
  }
 
3749
  else
 
3750
  {
 
3751
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
 
3752
  }
 
3753
  return 0;
 
3754
}
 
3755
 
 
3756
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
 
3757
{
 
3758
  return Field_timestamp::get_date(ltime,0);
 
3759
}
 
3760
 
 
3761
 
 
3762
bool Field_timestamp::send_binary(Protocol *protocol)
 
3763
{
 
3764
  MYSQL_TIME tm;
 
3765
  Field_timestamp::get_date(&tm, 0);
 
3766
  return protocol->store(&tm);
 
3767
}
 
3768
 
 
3769
 
 
3770
int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
3771
{
 
3772
  int32 a,b;
 
3773
#ifdef WORDS_BIGENDIAN
 
3774
  if (table && table->s->db_low_byte_first)
 
3775
  {
 
3776
    a=sint4korr(a_ptr);
 
3777
    b=sint4korr(b_ptr);
 
3778
  }
 
3779
  else
 
3780
#endif
 
3781
  {
 
3782
  longget(a,a_ptr);
 
3783
  longget(b,b_ptr);
 
3784
  }
 
3785
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
 
3786
}
 
3787
 
 
3788
 
 
3789
void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
 
3790
{
 
3791
#ifdef WORDS_BIGENDIAN
 
3792
  if (!table || !table->s->db_low_byte_first)
 
3793
  {
 
3794
    to[0] = ptr[0];
 
3795
    to[1] = ptr[1];
 
3796
    to[2] = ptr[2];
 
3797
    to[3] = ptr[3];
 
3798
  }
 
3799
  else
 
3800
#endif
 
3801
  {
 
3802
    to[0] = ptr[3];
 
3803
    to[1] = ptr[2];
 
3804
    to[2] = ptr[1];
 
3805
    to[3] = ptr[0];
 
3806
  }
 
3807
}
 
3808
 
 
3809
 
 
3810
void Field_timestamp::sql_type(String &res) const
 
3811
{
 
3812
  res.set_ascii(STRING_WITH_LEN("timestamp"));
 
3813
}
 
3814
 
 
3815
 
 
3816
void Field_timestamp::set_time()
 
3817
{
 
3818
  THD *thd= table ? table->in_use : current_thd;
 
3819
  long tmp= (long) thd->query_start();
 
3820
  set_notnull();
 
3821
  store_timestamp(tmp);
 
3822
}
 
3823
 
 
3824
/****************************************************************************
 
3825
** time type
 
3826
** In string context: HH:MM:SS
 
3827
** In number context: HHMMSS
 
3828
** Stored as a 3 byte unsigned int
 
3829
****************************************************************************/
 
3830
 
 
3831
int Field_time::store(const char *from,
 
3832
                      uint len,
 
3833
                      CHARSET_INFO *cs __attribute__((__unused__)))
 
3834
{
 
3835
  MYSQL_TIME ltime;
 
3836
  long tmp;
 
3837
  int error= 0;
 
3838
  int warning;
 
3839
 
 
3840
  if (str_to_time(from, len, &ltime, &warning))
 
3841
  {
 
3842
    tmp=0L;
 
3843
    error= 2;
 
3844
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
3845
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
 
3846
  }
 
3847
  else
 
3848
  {
 
3849
    if (warning & MYSQL_TIME_WARN_TRUNCATED)
 
3850
    {
 
3851
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3852
                           WARN_DATA_TRUNCATED,
 
3853
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
 
3854
      error= 1;
 
3855
    }
 
3856
    if (warning & MYSQL_TIME_WARN_OUT_OF_RANGE)
 
3857
    {
 
3858
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3859
                           ER_WARN_DATA_OUT_OF_RANGE,
 
3860
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
 
3861
      error= 1;
 
3862
    }
 
3863
    if (ltime.month)
 
3864
      ltime.day=0;
 
3865
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
 
3866
  }
 
3867
  
 
3868
  if (ltime.neg)
 
3869
    tmp= -tmp;
 
3870
  int3store(ptr,tmp);
 
3871
  return error;
 
3872
}
 
3873
 
 
3874
 
 
3875
int Field_time::store_time(MYSQL_TIME *ltime,
 
3876
                           timestamp_type time_type __attribute__((__unused__)))
 
3877
{
 
3878
  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
 
3879
            (ltime->minute * 100 + ltime->second);
 
3880
  if (ltime->neg)
 
3881
    tmp= -tmp;
 
3882
  return Field_time::store((int64_t) tmp, false);
 
3883
}
 
3884
 
 
3885
 
 
3886
int Field_time::store(double nr)
 
3887
{
 
3888
  long tmp;
 
3889
  int error= 0;
 
3890
  if (nr > (double)TIME_MAX_VALUE)
 
3891
  {
 
3892
    tmp= TIME_MAX_VALUE;
 
3893
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
3894
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
 
3895
    error= 1;
 
3896
  }
 
3897
  else if (nr < (double)-TIME_MAX_VALUE)
 
3898
  {
 
3899
    tmp= -TIME_MAX_VALUE;
 
3900
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3901
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
 
3902
    error= 1;
 
3903
  }
 
3904
  else
 
3905
  {
 
3906
    tmp=(long) floor(fabs(nr));                 // Remove fractions
 
3907
    if (nr < 0)
 
3908
      tmp= -tmp;
 
3909
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
3910
    {
 
3911
      tmp=0;
 
3912
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3913
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
 
3914
                           MYSQL_TIMESTAMP_TIME);
 
3915
      error= 1;
 
3916
    }
 
3917
  }
 
3918
  int3store(ptr,tmp);
 
3919
  return error;
 
3920
}
 
3921
 
 
3922
 
 
3923
int Field_time::store(int64_t nr, bool unsigned_val)
 
3924
{
 
3925
  long tmp;
 
3926
  int error= 0;
 
3927
  if (nr < (int64_t) -TIME_MAX_VALUE && !unsigned_val)
 
3928
  {
 
3929
    tmp= -TIME_MAX_VALUE;
 
3930
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3931
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
 
3932
                         MYSQL_TIMESTAMP_TIME, 1);
 
3933
    error= 1;
 
3934
  }
 
3935
  else if (nr > (int64_t) TIME_MAX_VALUE || (nr < 0 && unsigned_val))
 
3936
  {
 
3937
    tmp= TIME_MAX_VALUE;
 
3938
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3939
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
 
3940
                         MYSQL_TIMESTAMP_TIME, 1);
 
3941
    error= 1;
 
3942
  }
 
3943
  else
 
3944
  {
 
3945
    tmp=(long) nr;
 
3946
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
3947
    {
 
3948
      tmp=0;
 
3949
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
3950
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
 
3951
                           MYSQL_TIMESTAMP_TIME, 1);
 
3952
      error= 1;
 
3953
    }
 
3954
  }
 
3955
  int3store(ptr,tmp);
 
3956
  return error;
 
3957
}
 
3958
 
 
3959
 
 
3960
double Field_time::val_real(void)
 
3961
{
 
3962
  uint32 j= (uint32) uint3korr(ptr);
 
3963
  return (double) j;
 
3964
}
 
3965
 
 
3966
int64_t Field_time::val_int(void)
 
3967
{
 
3968
  return (int64_t) sint3korr(ptr);
 
3969
}
 
3970
 
 
3971
 
 
3972
/**
 
3973
  @note
 
3974
  This function is multi-byte safe as the result string is always of type
 
3975
  my_charset_bin
 
3976
*/
 
3977
 
 
3978
String *Field_time::val_str(String *val_buffer,
 
3979
                            String *val_ptr __attribute__((unused)))
 
3980
{
 
3981
  MYSQL_TIME ltime;
 
3982
  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
 
3983
  long tmp=(long) sint3korr(ptr);
 
3984
  ltime.neg= 0;
 
3985
  if (tmp < 0)
 
3986
  {
 
3987
    tmp= -tmp;
 
3988
    ltime.neg= 1;
 
3989
  }
 
3990
  ltime.day= (uint) 0;
 
3991
  ltime.hour= (uint) (tmp/10000);
 
3992
  ltime.minute= (uint) (tmp/100 % 100);
 
3993
  ltime.second= (uint) (tmp % 100);
 
3994
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
 
3995
  return val_buffer;
 
3996
}
 
3997
 
 
3998
 
 
3999
/**
 
4000
  @note
 
4001
  Normally we would not consider 'time' as a valid date, but we allow
 
4002
  get_date() here to be able to do things like
 
4003
  DATE_FORMAT(time, "%l.%i %p")
 
4004
*/
 
4005
 
 
4006
bool Field_time::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4007
{
 
4008
  long tmp;
 
4009
  THD *thd= table ? table->in_use : current_thd;
 
4010
  if (!(fuzzydate & TIME_FUZZY_DATE))
 
4011
  {
 
4012
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
4013
                        ER_WARN_DATA_OUT_OF_RANGE,
 
4014
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
 
4015
                        thd->row_count);
 
4016
    return 1;
 
4017
  }
 
4018
  tmp=(long) sint3korr(ptr);
 
4019
  ltime->neg=0;
 
4020
  if (tmp < 0)
 
4021
  {
 
4022
    ltime->neg= 1;
 
4023
    tmp=-tmp;
 
4024
  }
 
4025
  ltime->hour=tmp/10000;
 
4026
  tmp-=ltime->hour*10000;
 
4027
  ltime->minute=   tmp/100;
 
4028
  ltime->second= tmp % 100;
 
4029
  ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
 
4030
  return 0;
 
4031
}
 
4032
 
 
4033
 
 
4034
bool Field_time::get_time(MYSQL_TIME *ltime)
 
4035
{
 
4036
  long tmp=(long) sint3korr(ptr);
 
4037
  ltime->neg=0;
 
4038
  if (tmp < 0)
 
4039
  {
 
4040
    ltime->neg= 1;
 
4041
    tmp=-tmp;
 
4042
  }
 
4043
  ltime->day= 0;
 
4044
  ltime->hour=   (int) (tmp/10000);
 
4045
  tmp-=ltime->hour*10000;
 
4046
  ltime->minute= (int) tmp/100;
 
4047
  ltime->second= (int) tmp % 100;
 
4048
  ltime->second_part=0;
 
4049
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
 
4050
  return 0;
 
4051
}
 
4052
 
 
4053
 
 
4054
bool Field_time::send_binary(Protocol *protocol)
 
4055
{
 
4056
  MYSQL_TIME tm;
 
4057
  Field_time::get_time(&tm);
 
4058
  tm.day= tm.hour/24;                           // Move hours to days
 
4059
  tm.hour-= tm.day*24;
 
4060
  return protocol->store_time(&tm);
 
4061
}
 
4062
 
 
4063
 
 
4064
int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4065
{
 
4066
  int32 a,b;
 
4067
  a=(int32) sint3korr(a_ptr);
 
4068
  b=(int32) sint3korr(b_ptr);
 
4069
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
4070
}
 
4071
 
 
4072
void Field_time::sort_string(uchar *to,uint length __attribute__((unused)))
 
4073
{
 
4074
  to[0] = (uchar) (ptr[2] ^ 128);
 
4075
  to[1] = ptr[1];
 
4076
  to[2] = ptr[0];
 
4077
}
 
4078
 
 
4079
void Field_time::sql_type(String &res) const
 
4080
{
 
4081
  res.set_ascii(STRING_WITH_LEN("time"));
 
4082
}
 
4083
 
 
4084
/****************************************************************************
 
4085
** year type
 
4086
** Save in a byte the year 0, 1901->2155
 
4087
** Can handle 2 byte or 4 byte years!
 
4088
****************************************************************************/
 
4089
 
 
4090
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
 
4091
{
 
4092
  char *end;
 
4093
  int error;
 
4094
  int64_t nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
 
4095
 
 
4096
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 || 
 
4097
      error == MY_ERRNO_ERANGE)
 
4098
  {
 
4099
    *ptr=0;
 
4100
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4101
    return 1;
 
4102
  }
 
4103
  if (table->in_use->count_cuted_fields && 
 
4104
      (error= check_int(cs, from, len, end, error)))
 
4105
  {
 
4106
    if (error == 1)  /* empty or incorrect string */
 
4107
    {
 
4108
      *ptr= 0;
 
4109
      return 1;
 
4110
    }
 
4111
    error= 1;
 
4112
  }
 
4113
 
 
4114
  if (nr != 0 || len != 4)
 
4115
  {
 
4116
    if (nr < YY_PART_YEAR)
 
4117
      nr+=100;                                  // 2000 - 2069
 
4118
    else if (nr > 1900)
 
4119
      nr-= 1900;
 
4120
  }
 
4121
  *ptr= (char) (uchar) nr;
 
4122
  return error;
 
4123
}
 
4124
 
 
4125
 
 
4126
int Field_year::store(double nr)
 
4127
{
 
4128
  if (nr < 0.0 || nr >= 2155.0)
 
4129
  {
 
4130
    (void) Field_year::store((int64_t) -1, false);
 
4131
    return 1;
 
4132
  }
 
4133
  return Field_year::store((int64_t) nr, false);
 
4134
}
 
4135
 
 
4136
 
 
4137
int Field_year::store(int64_t nr,
 
4138
                      bool unsigned_val __attribute__((__unused__)))
 
4139
{
 
4140
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
 
4141
  {
 
4142
    *ptr= 0;
 
4143
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
4144
    return 1;
 
4145
  }
 
4146
  if (nr != 0 || field_length != 4)             // 0000 -> 0; 00 -> 2000
 
4147
  {
 
4148
    if (nr < YY_PART_YEAR)
 
4149
      nr+=100;                                  // 2000 - 2069
 
4150
    else if (nr > 1900)
 
4151
      nr-= 1900;
 
4152
  }
 
4153
  *ptr= (char) (uchar) nr;
 
4154
  return 0;
 
4155
}
 
4156
 
 
4157
 
 
4158
bool Field_year::send_binary(Protocol *protocol)
 
4159
{
 
4160
  uint64_t tmp= Field_year::val_int();
 
4161
  return protocol->store_short(tmp);
 
4162
}
 
4163
 
 
4164
 
 
4165
double Field_year::val_real(void)
 
4166
{
 
4167
  return (double) Field_year::val_int();
 
4168
}
 
4169
 
 
4170
 
 
4171
int64_t Field_year::val_int(void)
 
4172
{
 
4173
  int tmp= (int) ptr[0];
 
4174
  if (field_length != 4)
 
4175
    tmp%=100;                                   // Return last 2 char
 
4176
  else if (tmp)
 
4177
    tmp+=1900;
 
4178
  return (int64_t) tmp;
 
4179
}
 
4180
 
 
4181
 
 
4182
String *Field_year::val_str(String *val_buffer,
 
4183
                            String *val_ptr __attribute__((unused)))
 
4184
{
 
4185
  val_buffer->alloc(5);
 
4186
  val_buffer->length(field_length);
 
4187
  char *to=(char*) val_buffer->ptr();
 
4188
  sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
 
4189
  return val_buffer;
 
4190
}
 
4191
 
 
4192
 
 
4193
void Field_year::sql_type(String &res) const
 
4194
{
 
4195
  CHARSET_INFO *cs=res.charset();
 
4196
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
 
4197
                          "year(%d)",(int) field_length));
 
4198
}
 
4199
 
 
4200
 
 
4201
/****************************************************************************
 
4202
** The new date type
 
4203
** This is identical to the old date type, but stored on 3 bytes instead of 4
 
4204
** In number context: YYYYMMDD
 
4205
****************************************************************************/
 
4206
 
 
4207
/*
 
4208
  Store string into a date field
 
4209
 
 
4210
  SYNOPSIS
 
4211
    Field_newdate::store()
 
4212
    from                Date string
 
4213
    len                 Length of date field
 
4214
    cs                  Character set (not used)
 
4215
 
 
4216
  RETURN
 
4217
    0  ok
 
4218
    1  Value was cut during conversion
 
4219
    2  Wrong date string
 
4220
    3  Datetime value that was cut (warning level NOTE)
 
4221
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
 
4222
       nearly-identical class Field_date doesn't ever return 3 from its
 
4223
       store function.
 
4224
*/
 
4225
 
 
4226
int Field_newdate::store(const char *from,
 
4227
                         uint len,
 
4228
                         CHARSET_INFO *cs __attribute__((__unused__)))
 
4229
{
 
4230
  long tmp;
 
4231
  MYSQL_TIME l_time;
 
4232
  int error;
 
4233
  THD *thd= table ? table->in_use : current_thd;
 
4234
  enum enum_mysql_timestamp_type ret;
 
4235
  if ((ret= str_to_datetime(from, len, &l_time,
 
4236
                            (TIME_FUZZY_DATE |
 
4237
                             (thd->variables.sql_mode &
 
4238
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4239
                               MODE_INVALID_DATES))),
 
4240
                            &error)) <= MYSQL_TIMESTAMP_ERROR)
 
4241
  {
 
4242
    tmp= 0;
 
4243
    error= 2;
 
4244
  }
 
4245
  else
 
4246
  {
 
4247
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
4248
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
 
4249
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
4250
      error= 3;                                 // Datetime was cut (note)
 
4251
  }
 
4252
 
 
4253
  if (error)
 
4254
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
4255
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
4256
                         WARN_DATA_TRUNCATED,
 
4257
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
 
4258
 
 
4259
  int3store(ptr, tmp);
 
4260
  return error;
 
4261
}
 
4262
 
 
4263
 
 
4264
int Field_newdate::store(double nr)
 
4265
{
 
4266
  if (nr < 0.0 || nr > 99991231235959.0)
 
4267
  {
 
4268
    int3store(ptr,(int32) 0);
 
4269
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4270
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
 
4271
    return 1;
 
4272
  }
 
4273
  return Field_newdate::store((int64_t) rint(nr), false);
 
4274
}
 
4275
 
 
4276
 
 
4277
int Field_newdate::store(int64_t nr,
 
4278
                         bool unsigned_val __attribute__((__unused__)))
 
4279
{
 
4280
  MYSQL_TIME l_time;
 
4281
  int64_t tmp;
 
4282
  int error;
 
4283
  THD *thd= table ? table->in_use : current_thd;
 
4284
  if (number_to_datetime(nr, &l_time,
 
4285
                         (TIME_FUZZY_DATE |
 
4286
                          (thd->variables.sql_mode &
 
4287
                           (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4288
                            MODE_INVALID_DATES))),
 
4289
                         &error) == -1LL)
 
4290
  {
 
4291
    tmp= 0L;
 
4292
    error= 2;
 
4293
  }
 
4294
  else
 
4295
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
4296
 
 
4297
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
 
4298
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
4299
    error= 3;
 
4300
 
 
4301
  if (error)
 
4302
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
4303
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
4304
                         error == 2 ? 
 
4305
                         ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED,
 
4306
                         nr,MYSQL_TIMESTAMP_DATE, 1);
 
4307
 
 
4308
  int3store(ptr,tmp);
 
4309
  return error;
 
4310
}
 
4311
 
 
4312
 
 
4313
int Field_newdate::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
 
4314
{
 
4315
  long tmp;
 
4316
  int error= 0;
 
4317
  if (time_type == MYSQL_TIMESTAMP_DATE ||
 
4318
      time_type == MYSQL_TIMESTAMP_DATETIME)
 
4319
  {
 
4320
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
 
4321
    if (check_date(ltime, tmp != 0,
 
4322
                   (TIME_FUZZY_DATE |
 
4323
                    (current_thd->variables.sql_mode &
 
4324
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4325
                      MODE_INVALID_DATES))), &error))
 
4326
    {
 
4327
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
4328
      String str(buff, sizeof(buff), &my_charset_latin1);
 
4329
      make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
 
4330
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
4331
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
 
4332
    }
 
4333
    if (!error && ltime->time_type != MYSQL_TIMESTAMP_DATE &&
 
4334
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
 
4335
    {
 
4336
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
4337
      String str(buff, sizeof(buff), &my_charset_latin1);
 
4338
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
4339
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_NOTE,
 
4340
                           WARN_DATA_TRUNCATED,
 
4341
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
 
4342
      error= 3;
 
4343
    }
 
4344
  }
 
4345
  else
 
4346
  {
 
4347
    tmp=0;
 
4348
    error= 1;
 
4349
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
4350
  }
 
4351
  int3store(ptr,tmp);
 
4352
  return error;
 
4353
}
 
4354
 
 
4355
 
 
4356
bool Field_newdate::send_binary(Protocol *protocol)
 
4357
{
 
4358
  MYSQL_TIME tm;
 
4359
  Field_newdate::get_date(&tm,0);
 
4360
  return protocol->store_date(&tm);
 
4361
}
 
4362
 
 
4363
 
 
4364
double Field_newdate::val_real(void)
 
4365
{
 
4366
  return (double) Field_newdate::val_int();
 
4367
}
 
4368
 
 
4369
 
 
4370
int64_t Field_newdate::val_int(void)
 
4371
{
 
4372
  ulong j= uint3korr(ptr);
 
4373
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
 
4374
  return (int64_t) j;
 
4375
}
 
4376
 
 
4377
 
 
4378
String *Field_newdate::val_str(String *val_buffer,
 
4379
                               String *val_ptr __attribute__((unused)))
 
4380
{
 
4381
  val_buffer->alloc(field_length);
 
4382
  val_buffer->length(field_length);
 
4383
  uint32 tmp=(uint32) uint3korr(ptr);
 
4384
  int part;
 
4385
  char *pos=(char*) val_buffer->ptr()+10;
 
4386
 
 
4387
  /* Open coded to get more speed */
 
4388
  *pos--=0;                                     // End NULL
 
4389
  part=(int) (tmp & 31);
 
4390
  *pos--= (char) ('0'+part%10);
 
4391
  *pos--= (char) ('0'+part/10);
 
4392
  *pos--= '-';
 
4393
  part=(int) (tmp >> 5 & 15);
 
4394
  *pos--= (char) ('0'+part%10);
 
4395
  *pos--= (char) ('0'+part/10);
 
4396
  *pos--= '-';
 
4397
  part=(int) (tmp >> 9);
 
4398
  *pos--= (char) ('0'+part%10); part/=10;
 
4399
  *pos--= (char) ('0'+part%10); part/=10;
 
4400
  *pos--= (char) ('0'+part%10); part/=10;
 
4401
  *pos=   (char) ('0'+part);
 
4402
  return val_buffer;
 
4403
}
 
4404
 
 
4405
 
 
4406
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
 
4407
{
 
4408
  uint32 tmp=(uint32) uint3korr(ptr);
 
4409
  ltime->day=   tmp & 31;
 
4410
  ltime->month= (tmp >> 5) & 15;
 
4411
  ltime->year=  (tmp >> 9);
 
4412
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
 
4413
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
 
4414
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
 
4415
          1 : 0);
 
4416
}
 
4417
 
 
4418
 
 
4419
bool Field_newdate::get_time(MYSQL_TIME *ltime)
 
4420
{
 
4421
  return Field_newdate::get_date(ltime,0);
 
4422
}
 
4423
 
 
4424
 
 
4425
int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4426
{
 
4427
  uint32 a,b;
 
4428
  a=(uint32) uint3korr(a_ptr);
 
4429
  b=(uint32) uint3korr(b_ptr);
 
4430
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
4431
}
 
4432
 
 
4433
 
 
4434
void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
 
4435
{
 
4436
  to[0] = ptr[2];
 
4437
  to[1] = ptr[1];
 
4438
  to[2] = ptr[0];
 
4439
}
 
4440
 
 
4441
 
 
4442
void Field_newdate::sql_type(String &res) const
 
4443
{
 
4444
  res.set_ascii(STRING_WITH_LEN("date"));
 
4445
}
 
4446
 
 
4447
 
 
4448
/****************************************************************************
 
4449
** datetime type
 
4450
** In string context: YYYY-MM-DD HH:MM:DD
 
4451
** In number context: YYYYMMDDHHMMDD
 
4452
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
 
4453
****************************************************************************/
 
4454
 
 
4455
int Field_datetime::store(const char *from,
 
4456
                          uint len,
 
4457
                          CHARSET_INFO *cs __attribute__((__unused__)))
 
4458
{
 
4459
  MYSQL_TIME time_tmp;
 
4460
  int error;
 
4461
  uint64_t tmp= 0;
 
4462
  enum enum_mysql_timestamp_type func_res;
 
4463
  THD *thd= table ? table->in_use : current_thd;
 
4464
 
 
4465
  func_res= str_to_datetime(from, len, &time_tmp,
 
4466
                            (TIME_FUZZY_DATE |
 
4467
                             (thd->variables.sql_mode &
 
4468
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4469
                               MODE_INVALID_DATES))),
 
4470
                            &error);
 
4471
  if ((int) func_res > (int) MYSQL_TIMESTAMP_ERROR)
 
4472
    tmp= TIME_to_uint64_t_datetime(&time_tmp);
 
4473
  else
 
4474
    error= 1;                                 // Fix if invalid zero date
 
4475
 
 
4476
  if (error)
 
4477
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4478
                         ER_WARN_DATA_OUT_OF_RANGE,
 
4479
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
 
4480
 
 
4481
#ifdef WORDS_BIGENDIAN
 
4482
  if (table && table->s->db_low_byte_first)
 
4483
  {
 
4484
    int8store(ptr,tmp);
 
4485
  }
 
4486
  else
 
4487
#endif
 
4488
    int64_tstore(ptr,tmp);
 
4489
  return error;
 
4490
}
 
4491
 
 
4492
 
 
4493
int Field_datetime::store(double nr)
 
4494
{
 
4495
  int error= 0;
 
4496
  if (nr < 0.0 || nr > 99991231235959.0)
 
4497
  {
 
4498
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
 
4499
                         ER_WARN_DATA_OUT_OF_RANGE,
 
4500
                         nr, MYSQL_TIMESTAMP_DATETIME);
 
4501
    nr= 0.0;
 
4502
    error= 1;
 
4503
  }
 
4504
  error|= Field_datetime::store((int64_t) rint(nr), false);
 
4505
  return error;
 
4506
}
 
4507
 
 
4508
 
 
4509
int Field_datetime::store(int64_t nr,
 
4510
                          bool unsigned_val __attribute__((__unused__)))
 
4511
{
 
4512
  MYSQL_TIME not_used;
 
4513
  int error;
 
4514
  int64_t initial_nr= nr;
 
4515
  THD *thd= table ? table->in_use : current_thd;
 
4516
 
 
4517
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
 
4518
                                         (thd->variables.sql_mode &
 
4519
                                          (MODE_NO_ZERO_IN_DATE |
 
4520
                                           MODE_NO_ZERO_DATE |
 
4521
                                           MODE_INVALID_DATES))), &error);
 
4522
 
 
4523
  if (nr == -1LL)
 
4524
  {
 
4525
    nr= 0;
 
4526
    error= 2;
 
4527
  }
 
4528
 
 
4529
  if (error)
 
4530
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
4531
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
 
4532
                         WARN_DATA_TRUNCATED, initial_nr,
 
4533
                         MYSQL_TIMESTAMP_DATETIME, 1);
 
4534
 
 
4535
#ifdef WORDS_BIGENDIAN
 
4536
  if (table && table->s->db_low_byte_first)
 
4537
  {
 
4538
    int8store(ptr,nr);
 
4539
  }
 
4540
  else
 
4541
#endif
 
4542
    int64_tstore(ptr,nr);
 
4543
  return error;
 
4544
}
 
4545
 
 
4546
 
 
4547
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
 
4548
{
 
4549
  int64_t tmp;
 
4550
  int error= 0;
 
4551
  /*
 
4552
    We don't perform range checking here since values stored in TIME
 
4553
    structure always fit into DATETIME range.
 
4554
  */
 
4555
  if (time_type == MYSQL_TIMESTAMP_DATE ||
 
4556
      time_type == MYSQL_TIMESTAMP_DATETIME)
 
4557
  {
 
4558
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*1000000LL+
 
4559
         (ltime->hour*10000L+ltime->minute*100+ltime->second));
 
4560
    if (check_date(ltime, tmp != 0,
 
4561
                   (TIME_FUZZY_DATE |
 
4562
                    (current_thd->variables.sql_mode &
 
4563
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
4564
                      MODE_INVALID_DATES))), &error))
 
4565
    {
 
4566
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
4567
      String str(buff, sizeof(buff), &my_charset_latin1);
 
4568
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
4569
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
4570
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATETIME,1);
 
4571
    }
 
4572
  }
 
4573
  else
 
4574
  {
 
4575
    tmp=0;
 
4576
    error= 1;
 
4577
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
4578
  }
 
4579
#ifdef WORDS_BIGENDIAN
 
4580
  if (table && table->s->db_low_byte_first)
 
4581
  {
 
4582
    int8store(ptr,tmp);
 
4583
  }
 
4584
  else
 
4585
#endif
 
4586
    int64_tstore(ptr,tmp);
 
4587
  return error;
 
4588
}
 
4589
 
 
4590
bool Field_datetime::send_binary(Protocol *protocol)
 
4591
{
 
4592
  MYSQL_TIME tm;
 
4593
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
 
4594
  return protocol->store(&tm);
 
4595
}
 
4596
 
 
4597
 
 
4598
double Field_datetime::val_real(void)
 
4599
{
 
4600
  return (double) Field_datetime::val_int();
 
4601
}
 
4602
 
 
4603
int64_t Field_datetime::val_int(void)
 
4604
{
 
4605
  int64_t j;
 
4606
#ifdef WORDS_BIGENDIAN
 
4607
  if (table && table->s->db_low_byte_first)
 
4608
    j=sint8korr(ptr);
 
4609
  else
 
4610
#endif
 
4611
    int64_tget(j,ptr);
 
4612
  return j;
 
4613
}
 
4614
 
 
4615
 
 
4616
String *Field_datetime::val_str(String *val_buffer,
 
4617
                                String *val_ptr __attribute__((unused)))
 
4618
{
 
4619
  val_buffer->alloc(field_length);
 
4620
  val_buffer->length(field_length);
 
4621
  uint64_t tmp;
 
4622
  long part1,part2;
 
4623
  char *pos;
 
4624
  int part3;
 
4625
 
 
4626
#ifdef WORDS_BIGENDIAN
 
4627
  if (table && table->s->db_low_byte_first)
 
4628
    tmp=sint8korr(ptr);
 
4629
  else
 
4630
#endif
 
4631
    int64_tget(tmp,ptr);
 
4632
 
 
4633
  /*
 
4634
    Avoid problem with slow int64_t arithmetic and sprintf
 
4635
  */
 
4636
 
 
4637
  part1=(long) (tmp/1000000LL);
 
4638
  part2=(long) (tmp - (uint64_t) part1*1000000LL);
 
4639
 
 
4640
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
 
4641
  *pos--=0;
 
4642
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
 
4643
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
 
4644
  *pos--= ':';
 
4645
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4646
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4647
  *pos--= ':';
 
4648
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4649
  *pos--= (char) ('0'+(char) part3);
 
4650
  *pos--= ' ';
 
4651
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
4652
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
4653
  *pos--= '-';
 
4654
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
4655
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
 
4656
  *pos--= '-';
 
4657
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4658
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4659
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
4660
  *pos=(char) ('0'+(char) part3);
 
4661
  return val_buffer;
 
4662
}
 
4663
 
 
4664
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
 
4665
{
 
4666
  int64_t tmp=Field_datetime::val_int();
 
4667
  uint32 part1,part2;
 
4668
  part1=(uint32) (tmp/1000000LL);
 
4669
  part2=(uint32) (tmp - (uint64_t) part1*1000000LL);
 
4670
 
 
4671
  ltime->time_type=     MYSQL_TIMESTAMP_DATETIME;
 
4672
  ltime->neg=           0;
 
4673
  ltime->second_part=   0;
 
4674
  ltime->second=        (int) (part2%100);
 
4675
  ltime->minute=        (int) (part2/100%100);
 
4676
  ltime->hour=          (int) (part2/10000);
 
4677
  ltime->day=           (int) (part1%100);
 
4678
  ltime->month=         (int) (part1/100%100);
 
4679
  ltime->year=          (int) (part1/10000);
 
4680
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
 
4681
}
 
4682
 
 
4683
bool Field_datetime::get_time(MYSQL_TIME *ltime)
 
4684
{
 
4685
  return Field_datetime::get_date(ltime,0);
 
4686
}
 
4687
 
 
4688
int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
4689
{
 
4690
  int64_t a,b;
 
4691
#ifdef WORDS_BIGENDIAN
 
4692
  if (table && table->s->db_low_byte_first)
 
4693
  {
 
4694
    a=sint8korr(a_ptr);
 
4695
    b=sint8korr(b_ptr);
 
4696
  }
 
4697
  else
 
4698
#endif
 
4699
  {
 
4700
    int64_tget(a,a_ptr);
 
4701
    int64_tget(b,b_ptr);
 
4702
  }
 
4703
  return ((uint64_t) a < (uint64_t) b) ? -1 :
 
4704
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
4705
}
 
4706
 
 
4707
void Field_datetime::sort_string(uchar *to,uint length __attribute__((unused)))
 
4708
{
 
4709
#ifdef WORDS_BIGENDIAN
 
4710
  if (!table || !table->s->db_low_byte_first)
 
4711
  {
 
4712
    to[0] = ptr[0];
 
4713
    to[1] = ptr[1];
 
4714
    to[2] = ptr[2];
 
4715
    to[3] = ptr[3];
 
4716
    to[4] = ptr[4];
 
4717
    to[5] = ptr[5];
 
4718
    to[6] = ptr[6];
 
4719
    to[7] = ptr[7];
 
4720
  }
 
4721
  else
 
4722
#endif
 
4723
  {
 
4724
    to[0] = ptr[7];
 
4725
    to[1] = ptr[6];
 
4726
    to[2] = ptr[5];
 
4727
    to[3] = ptr[4];
 
4728
    to[4] = ptr[3];
 
4729
    to[5] = ptr[2];
 
4730
    to[6] = ptr[1];
 
4731
    to[7] = ptr[0];
 
4732
  }
 
4733
}
 
4734
 
 
4735
 
 
4736
void Field_datetime::sql_type(String &res) const
 
4737
{
 
4738
  res.set_ascii(STRING_WITH_LEN("datetime"));
 
4739
}
 
4740
 
 
4741
/****************************************************************************
 
4742
** string type
 
4743
** A string may be varchar or binary
 
4744
****************************************************************************/
1412
4745
 
1413
4746
/*
1414
4747
  Report "not well formed" or "cannot convert" error
1436
4769
    true  - If an error happened
1437
4770
*/
1438
4771
 
1439
 
bool
 
4772
static bool
1440
4773
check_string_copy_error(Field_str *field,
1441
4774
                        const char *well_formed_error_pos,
1442
4775
                        const char *cannot_convert_error_pos,
1443
4776
                        const char *end,
1444
 
                        const CHARSET_INFO * const cs)
 
4777
                        CHARSET_INFO *cs)
1445
4778
{
1446
4779
  const char *pos, *end_orig;
1447
4780
  char tmp[64], *t;
1487
4820
  *t= '\0';
1488
4821
  push_warning_printf(field->table->in_use, 
1489
4822
                      field->table->in_use->abort_on_warning ?
1490
 
                      DRIZZLE_ERROR::WARN_LEVEL_ERROR :
1491
 
                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
4823
                      MYSQL_ERROR::WARN_LEVEL_ERROR :
 
4824
                      MYSQL_ERROR::WARN_LEVEL_WARN,
1492
4825
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
1493
4826
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1494
4827
                      "string", tmp, field->field_name,
1495
 
                      (uint32_t) field->table->in_use->row_count);
 
4828
                      (ulong) field->table->in_use->row_count);
1496
4829
  return true;
1497
4830
}
1498
4831
 
1523
4856
    if (test_if_important_data(field_charset, ptr, end))
1524
4857
    {
1525
4858
      if (table->in_use->abort_on_warning)
1526
 
        set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
4859
        set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
1527
4860
      else
1528
 
        set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
4861
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1529
4862
    }
1530
4863
    else /* If we lost only spaces then produce a NOTE, not a WARNING */
1531
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
4864
      set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1532
4865
    return 2;
1533
4866
  }
1534
4867
  return 0;
1535
4868
}
1536
4869
 
1537
4870
 
 
4871
        /* Copy a string and fill with space */
 
4872
 
 
4873
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
 
4874
{
 
4875
  uint copy_length;
 
4876
  const char *well_formed_error_pos;
 
4877
  const char *cannot_convert_error_pos;
 
4878
  const char *from_end_pos;
 
4879
 
 
4880
  /* See the comment for Field_long::store(long long) */
 
4881
  assert(table->in_use == current_thd);
 
4882
 
 
4883
  copy_length= well_formed_copy_nchars(field_charset,
 
4884
                                       (char*) ptr, field_length,
 
4885
                                       cs, from, length,
 
4886
                                       field_length / field_charset->mbmaxlen,
 
4887
                                       &well_formed_error_pos,
 
4888
                                       &cannot_convert_error_pos,
 
4889
                                       &from_end_pos);
 
4890
 
 
4891
  /* Append spaces if the string was shorter than the field. */
 
4892
  if (copy_length < field_length)
 
4893
    field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
 
4894
                              field_length-copy_length,
 
4895
                              field_charset->pad_char);
 
4896
 
 
4897
  if (check_string_copy_error(this, well_formed_error_pos,
 
4898
                              cannot_convert_error_pos, from + length, cs))
 
4899
    return 2;
 
4900
 
 
4901
  return report_if_important_data(from_end_pos, from + length);
 
4902
}
 
4903
 
 
4904
 
1538
4905
/**
1539
 
  Store double value in Field_varstring.
 
4906
  Store double value in Field_string or Field_varstring.
1540
4907
 
1541
4908
  Pretty prints double number into field_length characters buffer.
1542
4909
 
1546
4913
int Field_str::store(double nr)
1547
4914
{
1548
4915
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
1549
 
  uint32_t local_char_length= field_length / charset()->mbmaxlen;
 
4916
  uint local_char_length= field_length / charset()->mbmaxlen;
1550
4917
  size_t length;
1551
 
  bool error;
 
4918
  my_bool error;
1552
4919
 
1553
4920
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
1554
4921
  if (error)
1555
4922
  {
1556
4923
    if (table->in_use->abort_on_warning)
1557
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
4924
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
1558
4925
    else
1559
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
4926
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1560
4927
  }
1561
4928
  return store(buff, length, charset());
1562
4929
}
1563
4930
 
1564
4931
 
1565
 
uint32_t Field::is_equal(Create_field *new_field)
 
4932
uint Field::is_equal(Create_field *new_field)
1566
4933
{
1567
4934
  return (new_field->sql_type == real_type());
1568
4935
}
1570
4937
 
1571
4938
/* If one of the fields is binary and the other one isn't return 1 else 0 */
1572
4939
 
1573
 
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32_t flag_arg)
 
4940
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32 flag_arg)
1574
4941
{
1575
4942
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
1576
4943
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
1579
4946
}
1580
4947
 
1581
4948
 
1582
 
uint32_t Field_str::is_equal(Create_field *new_field)
 
4949
uint Field_str::is_equal(Create_field *new_field)
1583
4950
{
1584
4951
  if (compare_str_field_flags(new_field, flags))
1585
4952
    return 0;
1590
4957
}
1591
4958
 
1592
4959
 
 
4960
int Field_string::store(int64_t nr, bool unsigned_val)
 
4961
{
 
4962
  char buff[64];
 
4963
  int  l;
 
4964
  CHARSET_INFO *cs=charset();
 
4965
  l= (cs->cset->int64_t10_to_str)(cs,buff,sizeof(buff),
 
4966
                                   unsigned_val ? 10 : -10, nr);
 
4967
  return Field_string::store(buff,(uint)l,cs);
 
4968
}
 
4969
 
 
4970
 
1593
4971
int Field_longstr::store_decimal(const my_decimal *d)
1594
4972
{
1595
4973
  char buff[DECIMAL_MAX_STR_LENGTH+1];
1598
4976
  return store(str.ptr(), str.length(), str.charset());
1599
4977
}
1600
4978
 
1601
 
uint32_t Field_longstr::max_data_length() const
 
4979
uint32 Field_longstr::max_data_length() const
1602
4980
{
1603
4981
  return field_length + (field_length > 255 ? 2 : 1);
1604
4982
}
1605
4983
 
1606
4984
 
 
4985
double Field_string::val_real(void)
 
4986
{
 
4987
  int error;
 
4988
  char *end;
 
4989
  CHARSET_INFO *cs= charset();
 
4990
  double result;
 
4991
  
 
4992
  result=  my_strntod(cs,(char*) ptr,field_length,&end,&error);
 
4993
  if (!table->in_use->no_errors &&
 
4994
      (error || (field_length != (uint32)(end - (char*) ptr) && 
 
4995
                 !check_if_only_end_space(cs, end,
 
4996
                                          (char*) ptr + field_length))))
 
4997
  {
 
4998
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
4999
    String tmp(buf, sizeof(buf), cs);
 
5000
    tmp.copy((char*) ptr, field_length, cs);
 
5001
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
5002
                        ER_TRUNCATED_WRONG_VALUE, 
 
5003
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
5004
                        "DOUBLE", tmp.c_ptr());
 
5005
  }
 
5006
  return result;
 
5007
}
 
5008
 
 
5009
 
 
5010
int64_t Field_string::val_int(void)
 
5011
{
 
5012
  int error;
 
5013
  char *end;
 
5014
  CHARSET_INFO *cs= charset();
 
5015
  int64_t result;
 
5016
 
 
5017
  result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
 
5018
  if (!table->in_use->no_errors &&
 
5019
      (error || (field_length != (uint32)(end - (char*) ptr) && 
 
5020
                 !check_if_only_end_space(cs, end,
 
5021
                                          (char*) ptr + field_length))))
 
5022
  {
 
5023
    char buf[LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE];
 
5024
    String tmp(buf, sizeof(buf), cs);
 
5025
    tmp.copy((char*) ptr, field_length, cs);
 
5026
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
5027
                        ER_TRUNCATED_WRONG_VALUE, 
 
5028
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
5029
                        "INTEGER", tmp.c_ptr());
 
5030
  }
 
5031
  return result;
 
5032
}
 
5033
 
 
5034
 
 
5035
String *Field_string::val_str(String *val_buffer __attribute__((unused)),
 
5036
                              String *val_ptr)
 
5037
{
 
5038
  /* See the comment for Field_long::store(long long) */
 
5039
  assert(table->in_use == current_thd);
 
5040
  uint length;
 
5041
  if (table->in_use->variables.sql_mode &
 
5042
      MODE_PAD_CHAR_TO_FULL_LENGTH)
 
5043
    length= my_charpos(field_charset, ptr, ptr + field_length, field_length);
 
5044
  else
 
5045
    length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
 
5046
                                          field_length);
 
5047
  val_ptr->set((const char*) ptr, length, field_charset);
 
5048
  return val_ptr;
 
5049
}
 
5050
 
 
5051
 
 
5052
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
 
5053
{
 
5054
  int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
 
5055
                          charset(), decimal_value);
 
5056
  if (!table->in_use->no_errors && err)
 
5057
  {
 
5058
    char buf[DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE];
 
5059
    CHARSET_INFO *cs= charset();
 
5060
    String tmp(buf, sizeof(buf), cs);
 
5061
    tmp.copy((char*) ptr, field_length, cs);
 
5062
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
5063
                        ER_TRUNCATED_WRONG_VALUE, 
 
5064
                        ER(ER_TRUNCATED_WRONG_VALUE),
 
5065
                        "DECIMAL", tmp.c_ptr());
 
5066
  }
 
5067
 
 
5068
  return decimal_value;
 
5069
}
 
5070
 
 
5071
 
 
5072
int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
5073
{
 
5074
  uint a_len, b_len;
 
5075
 
 
5076
  if (field_charset->mbmaxlen != 1)
 
5077
  {
 
5078
    uint char_len= field_length/field_charset->mbmaxlen;
 
5079
    a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
 
5080
    b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
 
5081
  }
 
5082
  else
 
5083
    a_len= b_len= field_length;
 
5084
  /*
 
5085
    We have to remove end space to be able to compare multi-byte-characters
 
5086
    like in latin_de 'ae' and 0xe4
 
5087
  */
 
5088
  return field_charset->coll->strnncollsp(field_charset,
 
5089
                                          a_ptr, a_len,
 
5090
                                          b_ptr, b_len,
 
5091
                                          0);
 
5092
}
 
5093
 
 
5094
 
 
5095
void Field_string::sort_string(uchar *to,uint length)
 
5096
{
 
5097
  uint tmp= my_strnxfrm(field_charset,
 
5098
                                 to, length,
 
5099
                                 ptr, field_length);
 
5100
  assert(tmp == length);
 
5101
}
 
5102
 
 
5103
 
 
5104
void Field_string::sql_type(String &res) const
 
5105
{
 
5106
  THD *thd= table->in_use;
 
5107
  CHARSET_INFO *cs=res.charset();
 
5108
  ulong length;
 
5109
 
 
5110
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
 
5111
                             res.alloced_length(), "%s(%d)",
 
5112
                             ((type() == MYSQL_TYPE_VAR_STRING &&
 
5113
                               !thd->variables.new_mode) ?
 
5114
                              (has_charset() ? "varchar" : "varbinary") :
 
5115
                              (has_charset() ? "char" : "binary")),
 
5116
                             (int) field_length / charset()->mbmaxlen);
 
5117
  res.length(length);
 
5118
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
 
5119
      has_charset() && (charset()->state & MY_CS_BINSORT))
 
5120
    res.append(STRING_WITH_LEN(" binary"));
 
5121
}
 
5122
 
 
5123
 
 
5124
uchar *Field_string::pack(uchar *to, const uchar *from,
 
5125
                          uint max_length,
 
5126
                          bool low_byte_first __attribute__((unused)))
 
5127
{
 
5128
  uint length=      min(field_length,max_length);
 
5129
  uint local_char_length= max_length/field_charset->mbmaxlen;
 
5130
  if (length > local_char_length)
 
5131
    local_char_length= my_charpos(field_charset, from, from+length,
 
5132
                                  local_char_length);
 
5133
  set_if_smaller(length, local_char_length);
 
5134
  while (length && from[length-1] == field_charset->pad_char)
 
5135
    length--;
 
5136
 
 
5137
  // Length always stored little-endian
 
5138
  *to++= (uchar) length;
 
5139
  if (field_length > 255)
 
5140
    *to++= (uchar) (length >> 8);
 
5141
 
 
5142
  // Store the actual bytes of the string
 
5143
  memcpy(to, from, length);
 
5144
  return to+length;
 
5145
}
 
5146
 
 
5147
 
 
5148
/**
 
5149
   Unpack a string field from row data.
 
5150
 
 
5151
   This method is used to unpack a string field from a master whose size 
 
5152
   of the field is less than that of the slave. Note that there can be a
 
5153
   variety of field types represented with this class. Certain types like
 
5154
   ENUM or SET are processed differently. Hence, the upper byte of the 
 
5155
   @c param_data argument contains the result of field->real_type() from
 
5156
   the master.
 
5157
 
 
5158
   @param   to         Destination of the data
 
5159
   @param   from       Source of the data
 
5160
   @param   param_data Real type (upper) and length (lower) values
 
5161
 
 
5162
   @return  New pointer into memory based on from + length of the data
 
5163
*/
 
5164
const uchar *
 
5165
Field_string::unpack(uchar *to,
 
5166
                     const uchar *from,
 
5167
                     uint param_data,
 
5168
                     bool low_byte_first __attribute__((unused)))
 
5169
{
 
5170
  uint from_length=
 
5171
    param_data ? min(param_data & 0x00ff, field_length) : field_length;
 
5172
  uint length;
 
5173
 
 
5174
  if (from_length > 255)
 
5175
  {
 
5176
    length= uint2korr(from);
 
5177
    from+= 2;
 
5178
  }
 
5179
  else
 
5180
    length= (uint) *from++;
 
5181
 
 
5182
  memcpy(to, from, length);
 
5183
  // Pad the string with the pad character of the fields charset
 
5184
  bfill(to + length, field_length - length, field_charset->pad_char);
 
5185
  return from+length;
 
5186
}
 
5187
 
 
5188
 
 
5189
/**
 
5190
   Save the field metadata for string fields.
 
5191
 
 
5192
   Saves the real type in the first byte and the field length in the 
 
5193
   second byte of the field metadata array at index of *metadata_ptr and
 
5194
   *(metadata_ptr + 1).
 
5195
 
 
5196
   @param   metadata_ptr   First byte of field metadata
 
5197
 
 
5198
   @returns number of bytes written to metadata_ptr
 
5199
*/
 
5200
int Field_string::do_save_field_metadata(uchar *metadata_ptr)
 
5201
{
 
5202
  *metadata_ptr= real_type();
 
5203
  *(metadata_ptr + 1)= field_length;
 
5204
  return 2;
 
5205
}
 
5206
 
 
5207
 
 
5208
/*
 
5209
  Compare two packed keys
 
5210
 
 
5211
  SYNOPSIS
 
5212
    pack_cmp()
 
5213
     a                  New key
 
5214
     b                  Original key
 
5215
     length             Key length
 
5216
     insert_or_update   1 if this is an insert or update
 
5217
 
 
5218
  RETURN
 
5219
    < 0   a < b
 
5220
    0     a = b
 
5221
    > 0   a > b
 
5222
*/
 
5223
 
 
5224
int Field_string::pack_cmp(const uchar *a, const uchar *b, uint length,
 
5225
                           my_bool insert_or_update)
 
5226
{
 
5227
  uint a_length, b_length;
 
5228
  if (length > 255)
 
5229
  {
 
5230
    a_length= uint2korr(a);
 
5231
    b_length= uint2korr(b);
 
5232
    a+= 2;
 
5233
    b+= 2;
 
5234
  }
 
5235
  else
 
5236
  {
 
5237
    a_length= (uint) *a++;
 
5238
    b_length= (uint) *b++;
 
5239
  }
 
5240
  return field_charset->coll->strnncollsp(field_charset,
 
5241
                                          a, a_length,
 
5242
                                          b, b_length,
 
5243
                                          insert_or_update);
 
5244
}
 
5245
 
 
5246
 
 
5247
/**
 
5248
  Compare a packed key against row.
 
5249
 
 
5250
  @param key                    Original key
 
5251
  @param length         Key length. (May be less than field length)
 
5252
  @param insert_or_update       1 if this is an insert or update
 
5253
 
 
5254
  @return
 
5255
    < 0   row < key
 
5256
  @return
 
5257
    0     row = key
 
5258
  @return
 
5259
    > 0   row > key
 
5260
*/
 
5261
 
 
5262
int Field_string::pack_cmp(const uchar *key, uint length,
 
5263
                           my_bool insert_or_update)
 
5264
{
 
5265
  uint row_length, local_key_length;
 
5266
  uchar *end;
 
5267
  if (length > 255)
 
5268
  {
 
5269
    local_key_length= uint2korr(key);
 
5270
    key+= 2;
 
5271
  }
 
5272
  else
 
5273
    local_key_length= (uint) *key++;
 
5274
  
 
5275
  /* Only use 'length' of key, not field_length */
 
5276
  end= ptr + length;
 
5277
  while (end > ptr && end[-1] == ' ')
 
5278
    end--;
 
5279
  row_length= (uint) (end - ptr);
 
5280
 
 
5281
  return field_charset->coll->strnncollsp(field_charset,
 
5282
                                          ptr, row_length,
 
5283
                                          key, local_key_length,
 
5284
                                          insert_or_update);
 
5285
}
 
5286
 
 
5287
 
 
5288
uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
 
5289
{
 
5290
  if (length > 255)
 
5291
    return uint2korr(data_ptr)+2;
 
5292
  return (uint) *data_ptr + 1;
 
5293
}
 
5294
 
 
5295
 
 
5296
uint Field_string::max_packed_col_length(uint max_length)
 
5297
{
 
5298
  return (max_length > 255 ? 2 : 1)+max_length;
 
5299
}
 
5300
 
 
5301
 
 
5302
uint Field_string::get_key_image(uchar *buff,
 
5303
                                 uint length,
 
5304
                                 imagetype type_arg __attribute__((__unused__)))
 
5305
{
 
5306
  uint bytes = my_charpos(field_charset, (char*) ptr,
 
5307
                          (char*) ptr + field_length,
 
5308
                          length / field_charset->mbmaxlen);
 
5309
  memcpy(buff, ptr, bytes);
 
5310
  if (bytes < length)
 
5311
    field_charset->cset->fill(field_charset, (char*) buff + bytes,
 
5312
                              length - bytes, field_charset->pad_char);
 
5313
  return bytes;
 
5314
}
 
5315
 
 
5316
 
 
5317
Field *Field_string::new_field(MEM_ROOT *root, struct st_table *new_table,
 
5318
                               bool keep_type)
 
5319
{
 
5320
  Field *field;
 
5321
  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
 
5322
    field= Field::new_field(root, new_table, keep_type);
 
5323
  else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
 
5324
                                       new_table->s, charset())))
 
5325
  {
 
5326
    /*
 
5327
      Old VARCHAR field which should be modified to a VARCHAR on copy
 
5328
      This is done to ensure that ALTER TABLE will convert old VARCHAR fields
 
5329
      to now VARCHAR fields.
 
5330
    */
 
5331
    field->init(new_table);
 
5332
    /*
 
5333
      Normally orig_table is different from table only if field was created
 
5334
      via ::new_field.  Here we alter the type of field, so ::new_field is
 
5335
      not applicable. But we still need to preserve the original field
 
5336
      metadata for the client-server protocol.
 
5337
    */
 
5338
    field->orig_table= orig_table;
 
5339
  }
 
5340
  return field;
 
5341
}
 
5342
 
 
5343
 
 
5344
/****************************************************************************
 
5345
  VARCHAR type
 
5346
  Data in field->ptr is stored as:
 
5347
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
 
5348
    data
 
5349
 
 
5350
  NOTE:
 
5351
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
 
5352
  stored with a 2 byte prefix. (Just like blob keys).
 
5353
 
 
5354
  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
 
5355
  The exception is if there is a prefix key field that is part of a long
 
5356
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
 
5357
  is 2.
 
5358
****************************************************************************/
 
5359
 
 
5360
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
 
5361
 
 
5362
/**
 
5363
   Save the field metadata for varstring fields.
 
5364
 
 
5365
   Saves the field length in the first byte. Note: may consume
 
5366
   2 bytes. Caller must ensure second byte is contiguous with
 
5367
   first byte (e.g. array index 0,1).
 
5368
 
 
5369
   @param   metadata_ptr   First byte of field metadata
 
5370
 
 
5371
   @returns number of bytes written to metadata_ptr
 
5372
*/
 
5373
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
 
5374
{
 
5375
  char *ptr= (char *)metadata_ptr;
 
5376
  assert(field_length <= 65535);
 
5377
  int2store(ptr, field_length);
 
5378
  return 2;
 
5379
}
 
5380
 
 
5381
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
 
5382
{
 
5383
  uint copy_length;
 
5384
  const char *well_formed_error_pos;
 
5385
  const char *cannot_convert_error_pos;
 
5386
  const char *from_end_pos;
 
5387
 
 
5388
  copy_length= well_formed_copy_nchars(field_charset,
 
5389
                                       (char*) ptr + length_bytes,
 
5390
                                       field_length,
 
5391
                                       cs, from, length,
 
5392
                                       field_length / field_charset->mbmaxlen,
 
5393
                                       &well_formed_error_pos,
 
5394
                                       &cannot_convert_error_pos,
 
5395
                                       &from_end_pos);
 
5396
 
 
5397
  if (length_bytes == 1)
 
5398
    *ptr= (uchar) copy_length;
 
5399
  else
 
5400
    int2store(ptr, copy_length);
 
5401
 
 
5402
  if (check_string_copy_error(this, well_formed_error_pos,
 
5403
                              cannot_convert_error_pos, from + length, cs))
 
5404
    return 2;
 
5405
 
 
5406
  return report_if_important_data(from_end_pos, from + length);
 
5407
}
 
5408
 
 
5409
 
 
5410
int Field_varstring::store(int64_t nr, bool unsigned_val)
 
5411
{
 
5412
  char buff[64];
 
5413
  uint  length;
 
5414
  length= (uint) (field_charset->cset->int64_t10_to_str)(field_charset,
 
5415
                                                          buff,
 
5416
                                                          sizeof(buff),
 
5417
                                                          (unsigned_val ? 10:
 
5418
                                                           -10),
 
5419
                                                           nr);
 
5420
  return Field_varstring::store(buff, length, field_charset);
 
5421
}
 
5422
 
 
5423
 
 
5424
double Field_varstring::val_real(void)
 
5425
{
 
5426
  int not_used;
 
5427
  char *end_not_used;
 
5428
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5429
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
 
5430
                    &end_not_used, &not_used);
 
5431
}
 
5432
 
 
5433
 
 
5434
int64_t Field_varstring::val_int(void)
 
5435
{
 
5436
  int not_used;
 
5437
  char *end_not_used;
 
5438
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5439
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
 
5440
                     &end_not_used, &not_used);
 
5441
}
 
5442
 
 
5443
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
 
5444
                                 String *val_ptr)
 
5445
{
 
5446
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5447
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
 
5448
  return val_ptr;
 
5449
}
 
5450
 
 
5451
 
 
5452
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
 
5453
{
 
5454
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5455
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
 
5456
                 charset(), decimal_value);
 
5457
  return decimal_value;
 
5458
}
 
5459
 
 
5460
 
 
5461
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
 
5462
                             uint max_len)
 
5463
{
 
5464
  uint a_length, b_length;
 
5465
  int diff;
 
5466
 
 
5467
  if (length_bytes == 1)
 
5468
  {
 
5469
    a_length= (uint) *a_ptr;
 
5470
    b_length= (uint) *b_ptr;
 
5471
  }
 
5472
  else
 
5473
  {
 
5474
    a_length= uint2korr(a_ptr);
 
5475
    b_length= uint2korr(b_ptr);
 
5476
  }
 
5477
  set_if_smaller(a_length, max_len);
 
5478
  set_if_smaller(b_length, max_len);
 
5479
  diff= field_charset->coll->strnncollsp(field_charset,
 
5480
                                         a_ptr+
 
5481
                                         length_bytes,
 
5482
                                         a_length,
 
5483
                                         b_ptr+
 
5484
                                         length_bytes,
 
5485
                                         b_length,0);
 
5486
  return diff;
 
5487
}
 
5488
 
 
5489
 
 
5490
/**
 
5491
  @note
 
5492
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
 
5493
*/
 
5494
 
 
5495
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
 
5496
{
 
5497
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5498
  uint local_char_length= max_key_length / field_charset->mbmaxlen;
 
5499
 
 
5500
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
 
5501
                          ptr + length_bytes + length, local_char_length);
 
5502
  set_if_smaller(length, local_char_length);
 
5503
  return field_charset->coll->strnncollsp(field_charset, 
 
5504
                                          ptr + length_bytes,
 
5505
                                          length,
 
5506
                                          key_ptr+
 
5507
                                          HA_KEY_BLOB_LENGTH,
 
5508
                                          uint2korr(key_ptr), 0);
 
5509
}
 
5510
 
 
5511
 
 
5512
/**
 
5513
  Compare to key segments (always 2 byte length prefix).
 
5514
 
 
5515
  @note
 
5516
    This is used only to compare key segments created for index_read().
 
5517
    (keys are created and compared in key.cc)
 
5518
*/
 
5519
 
 
5520
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
 
5521
{
 
5522
  return field_charset->coll->strnncollsp(field_charset,
 
5523
                                          a + HA_KEY_BLOB_LENGTH,
 
5524
                                          uint2korr(a),
 
5525
                                          b + HA_KEY_BLOB_LENGTH,
 
5526
                                          uint2korr(b),
 
5527
                                          0);
 
5528
}
 
5529
 
 
5530
 
 
5531
void Field_varstring::sort_string(uchar *to,uint length)
 
5532
{
 
5533
  uint tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5534
 
 
5535
  if (field_charset == &my_charset_bin)
 
5536
  {
 
5537
    /* Store length last in high-byte order to sort longer strings first */
 
5538
    if (length_bytes == 1)
 
5539
      to[length-1]= tot_length;
 
5540
    else
 
5541
      mi_int2store(to+length-2, tot_length);
 
5542
    length-= length_bytes;
 
5543
  }
 
5544
 
 
5545
  tot_length= my_strnxfrm(field_charset,
 
5546
                          to, length, ptr + length_bytes,
 
5547
                          tot_length);
 
5548
  assert(tot_length == length);
 
5549
}
 
5550
 
 
5551
 
 
5552
enum ha_base_keytype Field_varstring::key_type() const
 
5553
{
 
5554
  enum ha_base_keytype res;
 
5555
 
 
5556
  if (binary())
 
5557
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
 
5558
  else
 
5559
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
 
5560
  return res;
 
5561
}
 
5562
 
 
5563
 
 
5564
void Field_varstring::sql_type(String &res) const
 
5565
{
 
5566
  THD *thd= table->in_use;
 
5567
  CHARSET_INFO *cs=res.charset();
 
5568
  ulong length;
 
5569
 
 
5570
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
 
5571
                             res.alloced_length(), "%s(%d)",
 
5572
                              (has_charset() ? "varchar" : "varbinary"),
 
5573
                             (int) field_length / charset()->mbmaxlen);
 
5574
  res.length(length);
 
5575
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
 
5576
      has_charset() && (charset()->state & MY_CS_BINSORT))
 
5577
    res.append(STRING_WITH_LEN(" binary"));
 
5578
}
 
5579
 
 
5580
 
 
5581
uint32 Field_varstring::data_length()
 
5582
{
 
5583
  return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
 
5584
}
 
5585
 
 
5586
uint32 Field_varstring::used_length()
 
5587
{
 
5588
  return length_bytes == 1 ? 1 + (uint32) (uchar) *ptr : 2 + uint2korr(ptr);
 
5589
}
 
5590
 
 
5591
/*
 
5592
  Functions to create a packed row.
 
5593
  Here the number of length bytes are depending on the given max_length
 
5594
*/
 
5595
 
 
5596
uchar *Field_varstring::pack(uchar *to, const uchar *from,
 
5597
                             uint max_length,
 
5598
                             bool low_byte_first __attribute__((unused)))
 
5599
{
 
5600
  uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
 
5601
  set_if_smaller(max_length, field_length);
 
5602
  if (length > max_length)
 
5603
    length=max_length;
 
5604
 
 
5605
  /* Length always stored little-endian */
 
5606
  *to++= length & 0xFF;
 
5607
  if (max_length > 255)
 
5608
    *to++= (length >> 8) & 0xFF;
 
5609
 
 
5610
  /* Store bytes of string */
 
5611
  if (length > 0)
 
5612
    memcpy(to, from+length_bytes, length);
 
5613
  return to+length;
 
5614
}
 
5615
 
 
5616
 
 
5617
uchar *
 
5618
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
 
5619
                          bool low_byte_first __attribute__((unused)))
 
5620
{
 
5621
  uint length=  length_bytes == 1 ? (uint) *key : uint2korr(key);
 
5622
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
5623
                     max_length/field_charset->mbmaxlen : max_length);
 
5624
  key+= length_bytes;
 
5625
  if (length > local_char_length)
 
5626
  {
 
5627
    local_char_length= my_charpos(field_charset, key, key+length,
 
5628
                                  local_char_length);
 
5629
    set_if_smaller(length, local_char_length);
 
5630
  }
 
5631
  *to++= (char) (length & 255);
 
5632
  if (max_length > 255)
 
5633
    *to++= (char) (length >> 8);
 
5634
  if (length)
 
5635
    memcpy(to, key, length);
 
5636
  return to+length;
 
5637
}
 
5638
 
 
5639
 
 
5640
/**
 
5641
  Unpack a key into a record buffer.
 
5642
 
 
5643
  A VARCHAR key has a maximum size of 64K-1.
 
5644
  In its packed form, the length field is one or two bytes long,
 
5645
  depending on 'max_length'.
 
5646
 
 
5647
  @param to                          Pointer into the record buffer.
 
5648
  @param key                         Pointer to the packed key.
 
5649
  @param max_length                  Key length limit from key description.
 
5650
 
 
5651
  @return
 
5652
    Pointer to end of 'key' (To the next key part if multi-segment key)
 
5653
*/
 
5654
 
 
5655
const uchar *
 
5656
Field_varstring::unpack_key(uchar *to __attribute__((__unused__)),
 
5657
                            const uchar *key, uint max_length,
 
5658
                            bool low_byte_first __attribute__((unused)))
 
5659
{
 
5660
  /* get length of the blob key */
 
5661
  uint32 length= *key++;
 
5662
  if (max_length > 255)
 
5663
    length+= (*key++) << 8;
 
5664
 
 
5665
  /* put the length into the record buffer */
 
5666
  if (length_bytes == 1)
 
5667
    *ptr= (uchar) length;
 
5668
  else
 
5669
    int2store(ptr, length);
 
5670
  memcpy(ptr + length_bytes, key, length);
 
5671
  return key + length;
 
5672
}
 
5673
 
 
5674
/**
 
5675
  Create a packed key that will be used for storage in the index tree.
 
5676
 
 
5677
  @param to             Store packed key segment here
 
5678
  @param from           Key segment (as given to index_read())
 
5679
  @param max_length     Max length of key
 
5680
 
 
5681
  @return
 
5682
    end of key storage
 
5683
*/
 
5684
 
 
5685
uchar *
 
5686
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
 
5687
                                         bool low_byte_first __attribute__((unused)))
 
5688
{
 
5689
  /* Key length is always stored as 2 bytes */
 
5690
  uint length= uint2korr(from);
 
5691
  if (length > max_length)
 
5692
    length= max_length;
 
5693
  *to++= (char) (length & 255);
 
5694
  if (max_length > 255)
 
5695
    *to++= (char) (length >> 8);
 
5696
  if (length)
 
5697
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
 
5698
  return to+length;
 
5699
}
 
5700
 
 
5701
 
 
5702
/**
 
5703
   Unpack a varstring field from row data.
 
5704
 
 
5705
   This method is used to unpack a varstring field from a master
 
5706
   whose size of the field is less than that of the slave.
 
5707
 
 
5708
   @note
 
5709
   The string length is always packed little-endian.
 
5710
  
 
5711
   @param   to         Destination of the data
 
5712
   @param   from       Source of the data
 
5713
   @param   param_data Length bytes from the master's field data
 
5714
 
 
5715
   @return  New pointer into memory based on from + length of the data
 
5716
*/
 
5717
const uchar *
 
5718
Field_varstring::unpack(uchar *to, const uchar *from,
 
5719
                        uint param_data,
 
5720
                        bool low_byte_first __attribute__((unused)))
 
5721
{
 
5722
  uint length;
 
5723
  uint l_bytes= (param_data && (param_data < field_length)) ? 
 
5724
                (param_data <= 255) ? 1 : 2 : length_bytes;
 
5725
  if (l_bytes == 1)
 
5726
  {
 
5727
    to[0]= *from++;
 
5728
    length= to[0];
 
5729
    if (length_bytes == 2)
 
5730
      to[1]= 0;
 
5731
  }
 
5732
  else /* l_bytes == 2 */
 
5733
  {
 
5734
    length= uint2korr(from);
 
5735
    to[0]= *from++;
 
5736
    to[1]= *from++;
 
5737
  }
 
5738
  if (length)
 
5739
    memcpy(to+ length_bytes, from, length);
 
5740
  return from+length;
 
5741
}
 
5742
 
 
5743
 
 
5744
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
 
5745
                              uint key_length_arg,
 
5746
                              my_bool insert_or_update)
 
5747
{
 
5748
  uint a_length, b_length;
 
5749
  if (key_length_arg > 255)
 
5750
  {
 
5751
    a_length=uint2korr(a); a+= 2;
 
5752
    b_length=uint2korr(b); b+= 2;
 
5753
  }
 
5754
  else
 
5755
  {
 
5756
    a_length= (uint) *a++;
 
5757
    b_length= (uint) *b++;
 
5758
  }
 
5759
  return field_charset->coll->strnncollsp(field_charset,
 
5760
                                          a, a_length,
 
5761
                                          b, b_length,
 
5762
                                          insert_or_update);
 
5763
}
 
5764
 
 
5765
 
 
5766
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
 
5767
                              my_bool insert_or_update)
 
5768
{
 
5769
  uchar *a= ptr+ length_bytes;
 
5770
  uint a_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5771
  uint b_length;
 
5772
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
5773
                           key_length_arg / field_charset->mbmaxlen :
 
5774
                           key_length_arg);
 
5775
 
 
5776
  if (key_length_arg > 255)
 
5777
  {
 
5778
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
 
5779
  }
 
5780
  else
 
5781
    b_length= (uint) *b++;
 
5782
 
 
5783
  if (a_length > local_char_length)
 
5784
  {
 
5785
    local_char_length= my_charpos(field_charset, a, a+a_length,
 
5786
                                  local_char_length);
 
5787
    set_if_smaller(a_length, local_char_length);
 
5788
  }
 
5789
 
 
5790
  return field_charset->coll->strnncollsp(field_charset,
 
5791
                                          a, a_length,
 
5792
                                          b, b_length,
 
5793
                                          insert_or_update);
 
5794
}
 
5795
 
 
5796
 
 
5797
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
 
5798
{
 
5799
  if (length > 255)
 
5800
    return uint2korr(data_ptr)+2;
 
5801
  return (uint) *data_ptr + 1;
 
5802
}
 
5803
 
 
5804
 
 
5805
uint Field_varstring::max_packed_col_length(uint max_length)
 
5806
{
 
5807
  return (max_length > 255 ? 2 : 1)+max_length;
 
5808
}
 
5809
 
 
5810
uint Field_varstring::get_key_image(uchar *buff,
 
5811
                                    uint length,
 
5812
                                    imagetype type __attribute__((__unused__)))
 
5813
{
 
5814
  uint f_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5815
  uint local_char_length= length / field_charset->mbmaxlen;
 
5816
  uchar *pos= ptr+length_bytes;
 
5817
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
 
5818
                                local_char_length);
 
5819
  set_if_smaller(f_length, local_char_length);
 
5820
  /* Key is always stored with 2 bytes */
 
5821
  int2store(buff,f_length);
 
5822
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
 
5823
  if (f_length < length)
 
5824
  {
 
5825
    /*
 
5826
      Must clear this as we do a memcmp in opt_range.cc to detect
 
5827
      identical keys
 
5828
    */
 
5829
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
 
5830
  }
 
5831
  return HA_KEY_BLOB_LENGTH+f_length;
 
5832
}
 
5833
 
 
5834
 
 
5835
void Field_varstring::set_key_image(const uchar *buff,uint length)
 
5836
{
 
5837
  length= uint2korr(buff);                      // Real length is here
 
5838
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
5839
                                field_charset);
 
5840
}
 
5841
 
 
5842
 
 
5843
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
 
5844
                                uint32 max_length)
 
5845
{
 
5846
  uint32 a_length,b_length;
 
5847
 
 
5848
  if (length_bytes == 1)
 
5849
  {
 
5850
    a_length= (uint) *a_ptr;
 
5851
    b_length= (uint) *b_ptr;
 
5852
  }
 
5853
  else
 
5854
  {
 
5855
    a_length= uint2korr(a_ptr);
 
5856
    b_length= uint2korr(b_ptr);
 
5857
  }
 
5858
  set_if_smaller(a_length, max_length);
 
5859
  set_if_smaller(b_length, max_length);
 
5860
  if (a_length != b_length)
 
5861
    return 1;
 
5862
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
 
5863
}
 
5864
 
 
5865
 
 
5866
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
 
5867
                                  bool keep_type)
 
5868
{
 
5869
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
 
5870
                                                            keep_type);
 
5871
  if (res)
 
5872
    res->length_bytes= length_bytes;
 
5873
  return res;
 
5874
}
 
5875
 
 
5876
 
 
5877
Field *Field_varstring::new_key_field(MEM_ROOT *root,
 
5878
                                      struct st_table *new_table,
 
5879
                                      uchar *new_ptr, uchar *new_null_ptr,
 
5880
                                      uint new_null_bit)
 
5881
{
 
5882
  Field_varstring *res;
 
5883
  if ((res= (Field_varstring*) Field::new_key_field(root,
 
5884
                                                    new_table,
 
5885
                                                    new_ptr,
 
5886
                                                    new_null_ptr,
 
5887
                                                    new_null_bit)))
 
5888
  {
 
5889
    /* Keys length prefixes are always packed with 2 bytes */
 
5890
    res->length_bytes= 2;
 
5891
  }
 
5892
  return res;
 
5893
}
 
5894
 
 
5895
 
 
5896
uint Field_varstring::is_equal(Create_field *new_field)
 
5897
{
 
5898
  if (new_field->sql_type == real_type() &&
 
5899
      new_field->charset == field_charset)
 
5900
  {
 
5901
    if (new_field->length == max_display_length())
 
5902
      return IS_EQUAL_YES;
 
5903
    if (new_field->length > max_display_length() &&
 
5904
        ((new_field->length <= 255 && max_display_length() <= 255) ||
 
5905
         (new_field->length > 255 && max_display_length() > 255)))
 
5906
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
 
5907
  }
 
5908
  return IS_EQUAL_NO;
 
5909
}
 
5910
 
 
5911
 
 
5912
void Field_varstring::hash(ulong *nr, ulong *nr2)
 
5913
{
 
5914
  if (is_null())
 
5915
  {
 
5916
    *nr^= (*nr << 1) | 1;
 
5917
  }
 
5918
  else
 
5919
  {
 
5920
    uint len=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
 
5921
    CHARSET_INFO *cs= charset();
 
5922
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
 
5923
  }
 
5924
}
 
5925
 
 
5926
 
 
5927
/****************************************************************************
 
5928
** blob type
 
5929
** A blob is saved as a length and a pointer. The length is stored in the
 
5930
** packlength slot and may be from 1-4.
 
5931
****************************************************************************/
 
5932
 
 
5933
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
5934
                       enum utype unireg_check_arg, const char *field_name_arg,
 
5935
                       TABLE_SHARE *share, uint blob_pack_length,
 
5936
                       CHARSET_INFO *cs)
 
5937
  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
 
5938
                 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
 
5939
                 cs),
 
5940
   packlength(blob_pack_length)
 
5941
{
 
5942
  flags|= BLOB_FLAG;
 
5943
  share->blob_fields++;
 
5944
  /* TODO: why do not fill table->s->blob_field array here? */
 
5945
}
 
5946
 
 
5947
 
 
5948
void Field_blob::store_length(uchar *i_ptr,
 
5949
                              uint i_packlength,
 
5950
                              uint32 i_number,
 
5951
                              bool low_byte_first __attribute__((__unused__)))
 
5952
{
 
5953
  switch (i_packlength) {
 
5954
  case 1:
 
5955
    i_ptr[0]= (uchar) i_number;
 
5956
    break;
 
5957
  case 2:
 
5958
#ifdef WORDS_BIGENDIAN
 
5959
    if (low_byte_first)
 
5960
    {
 
5961
      int2store(i_ptr,(unsigned short) i_number);
 
5962
    }
 
5963
    else
 
5964
#endif
 
5965
      shortstore(i_ptr,(unsigned short) i_number);
 
5966
    break;
 
5967
  case 3:
 
5968
    int3store(i_ptr,i_number);
 
5969
    break;
 
5970
  case 4:
 
5971
#ifdef WORDS_BIGENDIAN
 
5972
    if (low_byte_first)
 
5973
    {
 
5974
      int4store(i_ptr,i_number);
 
5975
    }
 
5976
    else
 
5977
#endif
 
5978
      longstore(i_ptr,i_number);
 
5979
  }
 
5980
}
 
5981
 
 
5982
 
 
5983
uint32 Field_blob::get_length(const uchar *pos,
 
5984
                              uint packlength_arg,
 
5985
                              bool low_byte_first __attribute__((__unused__)))
 
5986
{
 
5987
  switch (packlength_arg) {
 
5988
  case 1:
 
5989
    return (uint32) pos[0];
 
5990
  case 2:
 
5991
    {
 
5992
      uint16 tmp;
 
5993
#ifdef WORDS_BIGENDIAN
 
5994
      if (low_byte_first)
 
5995
        tmp=sint2korr(pos);
 
5996
      else
 
5997
#endif
 
5998
        shortget(tmp,pos);
 
5999
      return (uint32) tmp;
 
6000
    }
 
6001
  case 3:
 
6002
    return (uint32) uint3korr(pos);
 
6003
  case 4:
 
6004
    {
 
6005
      uint32 tmp;
 
6006
#ifdef WORDS_BIGENDIAN
 
6007
      if (low_byte_first)
 
6008
        tmp=uint4korr(pos);
 
6009
      else
 
6010
#endif
 
6011
        longget(tmp,pos);
 
6012
      return (uint32) tmp;
 
6013
    }
 
6014
  }
 
6015
  return 0;                                     // Impossible
 
6016
}
 
6017
 
 
6018
 
 
6019
/**
 
6020
  Put a blob length field into a record buffer.
 
6021
 
 
6022
  Depending on the maximum length of a blob, its length field is
 
6023
  put into 1 to 4 bytes. This is a property of the blob object,
 
6024
  described by 'packlength'.
 
6025
 
 
6026
  @param pos                 Pointer into the record buffer.
 
6027
  @param length              The length value to put.
 
6028
*/
 
6029
 
 
6030
void Field_blob::put_length(uchar *pos, uint32 length)
 
6031
{
 
6032
  switch (packlength) {
 
6033
  case 1:
 
6034
    *pos= (char) length;
 
6035
    break;
 
6036
  case 2:
 
6037
    int2store(pos, length);
 
6038
    break;
 
6039
  case 3:
 
6040
    int3store(pos, length);
 
6041
    break;
 
6042
  case 4:
 
6043
    int4store(pos, length);
 
6044
    break;
 
6045
  }
 
6046
}
 
6047
 
 
6048
 
 
6049
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
 
6050
{
 
6051
  uint copy_length, new_length;
 
6052
  const char *well_formed_error_pos;
 
6053
  const char *cannot_convert_error_pos;
 
6054
  const char *from_end_pos, *tmp;
 
6055
  char buff[STRING_BUFFER_USUAL_SIZE];
 
6056
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
6057
 
 
6058
  if (!length)
 
6059
  {
 
6060
    bzero(ptr,Field_blob::pack_length());
 
6061
    return 0;
 
6062
  }
 
6063
 
 
6064
  if (from == value.ptr())
 
6065
  {
 
6066
    uint32 dummy_offset;
 
6067
    if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
 
6068
    {
 
6069
      Field_blob::store_length(length);
 
6070
      bmove(ptr+packlength,(char*) &from,sizeof(char*));
 
6071
      return 0;
 
6072
    }
 
6073
    if (tmpstr.copy(from, length, cs))
 
6074
      goto oom_error;
 
6075
    from= tmpstr.ptr();
 
6076
  }
 
6077
 
 
6078
  new_length= min(max_data_length(), field_charset->mbmaxlen * length);
 
6079
  if (value.alloc(new_length))
 
6080
    goto oom_error;
 
6081
 
 
6082
 
 
6083
  if (f_is_hex_escape(flags))
 
6084
  {
 
6085
    copy_length= my_copy_with_hex_escaping(field_charset,
 
6086
                                           (char*) value.ptr(), new_length,
 
6087
                                            from, length);
 
6088
    Field_blob::store_length(copy_length);
 
6089
    tmp= value.ptr();
 
6090
    bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
 
6091
    return 0;
 
6092
  }
 
6093
  /*
 
6094
    "length" is OK as "nchars" argument to well_formed_copy_nchars as this
 
6095
    is never used to limit the length of the data. The cut of long data
 
6096
    is done with the new_length value.
 
6097
  */
 
6098
  copy_length= well_formed_copy_nchars(field_charset,
 
6099
                                       (char*) value.ptr(), new_length,
 
6100
                                       cs, from, length,
 
6101
                                       length,
 
6102
                                       &well_formed_error_pos,
 
6103
                                       &cannot_convert_error_pos,
 
6104
                                       &from_end_pos);
 
6105
 
 
6106
  Field_blob::store_length(copy_length);
 
6107
  tmp= value.ptr();
 
6108
  bmove(ptr+packlength,(uchar*) &tmp,sizeof(char*));
 
6109
 
 
6110
  if (check_string_copy_error(this, well_formed_error_pos,
 
6111
                              cannot_convert_error_pos, from + length, cs))
 
6112
    return 2;
 
6113
 
 
6114
  return report_if_important_data(from_end_pos, from + length);
 
6115
 
 
6116
oom_error:
 
6117
  /* Fatal OOM error */
 
6118
  bzero(ptr,Field_blob::pack_length());
 
6119
  return -1; 
 
6120
}
 
6121
 
 
6122
 
 
6123
int Field_blob::store(double nr)
 
6124
{
 
6125
  CHARSET_INFO *cs=charset();
 
6126
  value.set_real(nr, NOT_FIXED_DEC, cs);
 
6127
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
 
6128
}
 
6129
 
 
6130
 
 
6131
int Field_blob::store(int64_t nr, bool unsigned_val)
 
6132
{
 
6133
  CHARSET_INFO *cs=charset();
 
6134
  value.set_int(nr, unsigned_val, cs);
 
6135
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
 
6136
}
 
6137
 
 
6138
 
 
6139
double Field_blob::val_real(void)
 
6140
{
 
6141
  int not_used;
 
6142
  char *end_not_used, *blob;
 
6143
  uint32 length;
 
6144
  CHARSET_INFO *cs;
 
6145
 
 
6146
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6147
  if (!blob)
 
6148
    return 0.0;
 
6149
  length= get_length(ptr);
 
6150
  cs= charset();
 
6151
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
 
6152
}
 
6153
 
 
6154
 
 
6155
int64_t Field_blob::val_int(void)
 
6156
{
 
6157
  int not_used;
 
6158
  char *blob;
 
6159
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6160
  if (!blob)
 
6161
    return 0;
 
6162
  uint32 length=get_length(ptr);
 
6163
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
 
6164
}
 
6165
 
 
6166
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
 
6167
                            String *val_ptr)
 
6168
{
 
6169
  char *blob;
 
6170
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6171
  if (!blob)
 
6172
    val_ptr->set("",0,charset());       // A bit safer than ->length(0)
 
6173
  else
 
6174
    val_ptr->set((const char*) blob,get_length(ptr),charset());
 
6175
  return val_ptr;
 
6176
}
 
6177
 
 
6178
 
 
6179
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
 
6180
{
 
6181
  const char *blob;
 
6182
  size_t length;
 
6183
  memcpy_fixed(&blob, ptr+packlength, sizeof(const uchar*));
 
6184
  if (!blob)
 
6185
  {
 
6186
    blob= "";
 
6187
    length= 0;
 
6188
  }
 
6189
  else
 
6190
    length= get_length(ptr);
 
6191
 
 
6192
  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
 
6193
                 decimal_value);
 
6194
  return decimal_value;
 
6195
}
 
6196
 
 
6197
 
 
6198
int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
 
6199
                    uint32 b_length)
 
6200
{
 
6201
  return field_charset->coll->strnncollsp(field_charset, 
 
6202
                                          a, a_length, b, b_length,
 
6203
                                          0);
 
6204
}
 
6205
 
 
6206
 
 
6207
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
 
6208
                        uint max_length)
 
6209
{
 
6210
  uchar *blob1,*blob2;
 
6211
  memcpy_fixed(&blob1,a_ptr+packlength,sizeof(char*));
 
6212
  memcpy_fixed(&blob2,b_ptr+packlength,sizeof(char*));
 
6213
  uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
 
6214
  set_if_smaller(a_len, max_length);
 
6215
  set_if_smaller(b_len, max_length);
 
6216
  return Field_blob::cmp(blob1,a_len,blob2,b_len);
 
6217
}
 
6218
 
 
6219
 
 
6220
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
 
6221
                           uint32 max_length)
 
6222
{
 
6223
  char *a,*b;
 
6224
  uint diff;
 
6225
  uint32 a_length,b_length;
 
6226
  memcpy_fixed(&a,a_ptr+packlength,sizeof(char*));
 
6227
  memcpy_fixed(&b,b_ptr+packlength,sizeof(char*));
 
6228
  a_length=get_length(a_ptr);
 
6229
  if (a_length > max_length)
 
6230
    a_length=max_length;
 
6231
  b_length=get_length(b_ptr);
 
6232
  if (b_length > max_length)
 
6233
    b_length=max_length;
 
6234
  diff=memcmp(a,b,min(a_length,b_length));
 
6235
  return diff ? diff : (int) (a_length - b_length);
 
6236
}
 
6237
 
 
6238
 
 
6239
/* The following is used only when comparing a key */
 
6240
 
 
6241
uint Field_blob::get_key_image(uchar *buff,
 
6242
                               uint length,
 
6243
                               imagetype type_arg __attribute__((__unused__)))
 
6244
{
 
6245
  uint32 blob_length= get_length(ptr);
 
6246
  uchar *blob;
 
6247
 
 
6248
  get_ptr(&blob);
 
6249
  uint local_char_length= length / field_charset->mbmaxlen;
 
6250
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
 
6251
                          local_char_length);
 
6252
  set_if_smaller(blob_length, local_char_length);
 
6253
 
 
6254
  if ((uint32) length > blob_length)
 
6255
  {
 
6256
    /*
 
6257
      Must clear this as we do a memcmp in opt_range.cc to detect
 
6258
      identical keys
 
6259
    */
 
6260
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
 
6261
    length=(uint) blob_length;
 
6262
  }
 
6263
  int2store(buff,length);
 
6264
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
 
6265
  return HA_KEY_BLOB_LENGTH+length;
 
6266
}
 
6267
 
 
6268
 
 
6269
void Field_blob::set_key_image(const uchar *buff,uint length)
 
6270
{
 
6271
  length= uint2korr(buff);
 
6272
  (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
6273
                           field_charset);
 
6274
}
 
6275
 
 
6276
 
 
6277
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
 
6278
{
 
6279
  uchar *blob1;
 
6280
  uint blob_length=get_length(ptr);
 
6281
  memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
 
6282
  CHARSET_INFO *cs= charset();
 
6283
  uint local_char_length= max_key_length / cs->mbmaxlen;
 
6284
  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
 
6285
                                local_char_length);
 
6286
  set_if_smaller(blob_length, local_char_length);
 
6287
  return Field_blob::cmp(blob1, blob_length,
 
6288
                         key_ptr+HA_KEY_BLOB_LENGTH,
 
6289
                         uint2korr(key_ptr));
 
6290
}
 
6291
 
 
6292
int Field_blob::key_cmp(const uchar *a,const uchar *b)
 
6293
{
 
6294
  return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
 
6295
                         b+HA_KEY_BLOB_LENGTH, uint2korr(b));
 
6296
}
 
6297
 
 
6298
 
 
6299
/**
 
6300
   Save the field metadata for blob fields.
 
6301
 
 
6302
   Saves the pack length in the first byte of the field metadata array
 
6303
   at index of *metadata_ptr.
 
6304
 
 
6305
   @param   metadata_ptr   First byte of field metadata
 
6306
 
 
6307
   @returns number of bytes written to metadata_ptr
 
6308
*/
 
6309
int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
 
6310
{
 
6311
  *metadata_ptr= pack_length_no_ptr();
 
6312
  return 1;
 
6313
}
 
6314
 
 
6315
 
 
6316
uint32 Field_blob::sort_length() const
 
6317
{
 
6318
  return (uint32) (current_thd->variables.max_sort_length + 
 
6319
                   (field_charset == &my_charset_bin ? 0 : packlength));
 
6320
}
 
6321
 
 
6322
 
 
6323
void Field_blob::sort_string(uchar *to,uint length)
 
6324
{
 
6325
  uchar *blob;
 
6326
  uint blob_length=get_length();
 
6327
 
 
6328
  if (!blob_length)
 
6329
    bzero(to,length);
 
6330
  else
 
6331
  {
 
6332
    if (field_charset == &my_charset_bin)
 
6333
    {
 
6334
      uchar *pos;
 
6335
 
 
6336
      /*
 
6337
        Store length of blob last in blob to shorter blobs before longer blobs
 
6338
      */
 
6339
      length-= packlength;
 
6340
      pos= to+length;
 
6341
 
 
6342
      switch (packlength) {
 
6343
      case 1:
 
6344
        *pos= (char) blob_length;
 
6345
        break;
 
6346
      case 2:
 
6347
        mi_int2store(pos, blob_length);
 
6348
        break;
 
6349
      case 3:
 
6350
        mi_int3store(pos, blob_length);
 
6351
        break;
 
6352
      case 4:
 
6353
        mi_int4store(pos, blob_length);
 
6354
        break;
 
6355
      }
 
6356
    }
 
6357
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
 
6358
    
 
6359
    blob_length=my_strnxfrm(field_charset,
 
6360
                            to, length, blob, blob_length);
 
6361
    assert(blob_length == length);
 
6362
  }
 
6363
}
 
6364
 
 
6365
 
 
6366
void Field_blob::sql_type(String &res) const
 
6367
{
 
6368
  const char *str;
 
6369
  uint length;
 
6370
  switch (packlength) {
 
6371
  default: str="tiny"; length=4; break;
 
6372
  case 2:  str="";     length=0; break;
 
6373
  case 3:  str="medium"; length= 6; break;
 
6374
  case 4:  str="long";  length=4; break;
 
6375
  }
 
6376
  res.set_ascii(str,length);
 
6377
  if (charset() == &my_charset_bin)
 
6378
    res.append(STRING_WITH_LEN("blob"));
 
6379
  else
 
6380
  {
 
6381
    res.append(STRING_WITH_LEN("text"));
 
6382
  }
 
6383
}
 
6384
 
 
6385
uchar *Field_blob::pack(uchar *to, const uchar *from,
 
6386
                        uint max_length, bool low_byte_first)
 
6387
{
 
6388
  uchar *save= ptr;
 
6389
  ptr= (uchar*) from;
 
6390
  uint32 length=get_length();                   // Length of from string
 
6391
 
 
6392
  /*
 
6393
    Store max length, which will occupy packlength bytes. If the max
 
6394
    length given is smaller than the actual length of the blob, we
 
6395
    just store the initial bytes of the blob.
 
6396
  */
 
6397
  store_length(to, packlength, min(length, max_length), low_byte_first);
 
6398
 
 
6399
  /*
 
6400
    Store the actual blob data, which will occupy 'length' bytes.
 
6401
   */
 
6402
  if (length > 0)
 
6403
  {
 
6404
    get_ptr((uchar**) &from);
 
6405
    memcpy(to+packlength, from,length);
 
6406
  }
 
6407
  ptr=save;                                     // Restore org row pointer
 
6408
  return(to+packlength+length);
 
6409
}
 
6410
 
 
6411
 
 
6412
/**
 
6413
   Unpack a blob field from row data.
 
6414
 
 
6415
   This method is used to unpack a blob field from a master whose size of 
 
6416
   the field is less than that of the slave. Note: This method is included
 
6417
   to satisfy inheritance rules, but is not needed for blob fields. It
 
6418
   simply is used as a pass-through to the original unpack() method for
 
6419
   blob fields.
 
6420
 
 
6421
   @param   to         Destination of the data
 
6422
   @param   from       Source of the data
 
6423
   @param   param_data @c true if base types should be stored in little-
 
6424
                       endian format, @c false if native format should
 
6425
                       be used.
 
6426
 
 
6427
   @return  New pointer into memory based on from + length of the data
 
6428
*/
 
6429
const uchar *Field_blob::unpack(uchar *to __attribute__((__unused__)),
 
6430
                                const uchar *from,
 
6431
                                uint param_data,
 
6432
                                bool low_byte_first)
 
6433
{
 
6434
  uint const master_packlength=
 
6435
    param_data > 0 ? param_data & 0xFF : packlength;
 
6436
  uint32 const length= get_length(from, master_packlength, low_byte_first);
 
6437
  bitmap_set_bit(table->write_set, field_index);
 
6438
  store(reinterpret_cast<const char*>(from) + master_packlength,
 
6439
        length, field_charset);
 
6440
  return(from + master_packlength + length);
 
6441
}
 
6442
 
 
6443
/* Keys for blobs are like keys on varchars */
 
6444
 
 
6445
int Field_blob::pack_cmp(const uchar *a, const uchar *b, uint key_length_arg,
 
6446
                         my_bool insert_or_update)
 
6447
{
 
6448
  uint a_length, b_length;
 
6449
  if (key_length_arg > 255)
 
6450
  {
 
6451
    a_length=uint2korr(a); a+=2;
 
6452
    b_length=uint2korr(b); b+=2;
 
6453
  }
 
6454
  else
 
6455
  {
 
6456
    a_length= (uint) *a++;
 
6457
    b_length= (uint) *b++;
 
6458
  }
 
6459
  return field_charset->coll->strnncollsp(field_charset,
 
6460
                                          a, a_length,
 
6461
                                          b, b_length,
 
6462
                                          insert_or_update);
 
6463
}
 
6464
 
 
6465
 
 
6466
int Field_blob::pack_cmp(const uchar *b, uint key_length_arg,
 
6467
                         my_bool insert_or_update)
 
6468
{
 
6469
  uchar *a;
 
6470
  uint a_length, b_length;
 
6471
  memcpy_fixed(&a,ptr+packlength,sizeof(char*));
 
6472
  if (!a)
 
6473
    return key_length_arg > 0 ? -1 : 0;
 
6474
 
 
6475
  a_length= get_length(ptr);
 
6476
  if (key_length_arg > 255)
 
6477
  {
 
6478
    b_length= uint2korr(b); b+=2;
 
6479
  }
 
6480
  else
 
6481
    b_length= (uint) *b++;
 
6482
  return field_charset->coll->strnncollsp(field_charset,
 
6483
                                          a, a_length,
 
6484
                                          b, b_length,
 
6485
                                          insert_or_update);
 
6486
}
 
6487
 
 
6488
/** Create a packed key that will be used for storage from a MySQL row. */
 
6489
 
 
6490
uchar *
 
6491
Field_blob::pack_key(uchar *to, const uchar *from, uint max_length,
 
6492
                     bool low_byte_first __attribute__((unused)))
 
6493
{
 
6494
  uchar *save= ptr;
 
6495
  ptr= (uchar*) from;
 
6496
  uint32 length=get_length();        // Length of from string
 
6497
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
 
6498
                           max_length/field_charset->mbmaxlen : max_length);
 
6499
  if (length)
 
6500
    get_ptr((uchar**) &from);
 
6501
  if (length > local_char_length)
 
6502
    local_char_length= my_charpos(field_charset, from, from+length,
 
6503
                                  local_char_length);
 
6504
  set_if_smaller(length, local_char_length);
 
6505
  *to++= (uchar) length;
 
6506
  if (max_length > 255)                         // 2 byte length
 
6507
    *to++= (uchar) (length >> 8);
 
6508
  memcpy(to, from, length);
 
6509
  ptr=save;                                     // Restore org row pointer
 
6510
  return to+length;
 
6511
}
 
6512
 
 
6513
 
 
6514
/**
 
6515
  Unpack a blob key into a record buffer.
 
6516
 
 
6517
  A blob key has a maximum size of 64K-1.
 
6518
  In its packed form, the length field is one or two bytes long,
 
6519
  depending on 'max_length'.
 
6520
  Depending on the maximum length of a blob, its length field is
 
6521
  put into 1 to 4 bytes. This is a property of the blob object,
 
6522
  described by 'packlength'.
 
6523
  Blobs are internally stored apart from the record buffer, which
 
6524
  contains a pointer to the blob buffer.
 
6525
 
 
6526
 
 
6527
  @param to                          Pointer into the record buffer.
 
6528
  @param from                        Pointer to the packed key.
 
6529
  @param max_length                  Key length limit from key description.
 
6530
 
 
6531
  @return
 
6532
    Pointer into 'from' past the last byte copied from packed key.
 
6533
*/
 
6534
 
 
6535
const uchar *
 
6536
Field_blob::unpack_key(uchar *to, const uchar *from, uint max_length,
 
6537
                       bool low_byte_first __attribute__((unused)))
 
6538
{
 
6539
  /* get length of the blob key */
 
6540
  uint32 length= *from++;
 
6541
  if (max_length > 255)
 
6542
    length+= *from++ << 8;
 
6543
 
 
6544
  /* put the length into the record buffer */
 
6545
  put_length(to, length);
 
6546
 
 
6547
  /* put the address of the blob buffer or NULL */
 
6548
  if (length)
 
6549
    memcpy_fixed(to + packlength, &from, sizeof(from));
 
6550
  else
 
6551
    bzero(to + packlength, sizeof(from));
 
6552
 
 
6553
  /* point to first byte of next field in 'from' */
 
6554
  return from + length;
 
6555
}
 
6556
 
 
6557
 
 
6558
/** Create a packed key that will be used for storage from a MySQL key. */
 
6559
 
 
6560
uchar *
 
6561
Field_blob::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
 
6562
                                    bool low_byte_first __attribute__((unused)))
 
6563
{
 
6564
  uint length=uint2korr(from);
 
6565
  if (length > max_length)
 
6566
    length=max_length;
 
6567
  *to++= (char) (length & 255);
 
6568
  if (max_length > 255)
 
6569
    *to++= (char) (length >> 8);
 
6570
  if (length)
 
6571
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
 
6572
  return to+length;
 
6573
}
 
6574
 
 
6575
 
 
6576
uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
 
6577
{
 
6578
  if (length > 255)
 
6579
    return uint2korr(data_ptr)+2;
 
6580
  return (uint) *data_ptr + 1;
 
6581
}
 
6582
 
 
6583
 
 
6584
uint Field_blob::max_packed_col_length(uint max_length)
 
6585
{
 
6586
  return (max_length > 255 ? 2 : 1)+max_length;
 
6587
}
 
6588
 
 
6589
 
 
6590
uint Field_blob::is_equal(Create_field *new_field)
 
6591
{
 
6592
  if (compare_str_field_flags(new_field, flags))
 
6593
    return 0;
 
6594
 
 
6595
  return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
 
6596
          && new_field->charset == field_charset &&
 
6597
          ((Field_blob *)new_field->field)->max_data_length() ==
 
6598
          max_data_length());
 
6599
}
 
6600
 
 
6601
 
 
6602
/****************************************************************************
 
6603
** enum type.
 
6604
** This is a string which only can have a selection of different values.
 
6605
** If one uses this string in a number context one gets the type number.
 
6606
****************************************************************************/
 
6607
 
 
6608
enum ha_base_keytype Field_enum::key_type() const
 
6609
{
 
6610
  switch (packlength) {
 
6611
  default: return HA_KEYTYPE_BINARY;
 
6612
  case 2: return HA_KEYTYPE_USHORT_INT;
 
6613
  case 3: return HA_KEYTYPE_UINT24;
 
6614
  case 4: return HA_KEYTYPE_ULONG_INT;
 
6615
  case 8: return HA_KEYTYPE_ULONGLONG;
 
6616
  }
 
6617
}
 
6618
 
 
6619
void Field_enum::store_type(uint64_t value)
 
6620
{
 
6621
  switch (packlength) {
 
6622
  case 1: ptr[0]= (uchar) value;  break;
 
6623
  case 2:
 
6624
#ifdef WORDS_BIGENDIAN
 
6625
  if (table->s->db_low_byte_first)
 
6626
  {
 
6627
    int2store(ptr,(unsigned short) value);
 
6628
  }
 
6629
  else
 
6630
#endif
 
6631
    shortstore(ptr,(unsigned short) value);
 
6632
  break;
 
6633
  case 3: int3store(ptr,(long) value); break;
 
6634
  case 4:
 
6635
#ifdef WORDS_BIGENDIAN
 
6636
  if (table->s->db_low_byte_first)
 
6637
  {
 
6638
    int4store(ptr,value);
 
6639
  }
 
6640
  else
 
6641
#endif
 
6642
    longstore(ptr,(long) value);
 
6643
  break;
 
6644
  case 8:
 
6645
#ifdef WORDS_BIGENDIAN
 
6646
  if (table->s->db_low_byte_first)
 
6647
  {
 
6648
    int8store(ptr,value);
 
6649
  }
 
6650
  else
 
6651
#endif
 
6652
    int64_tstore(ptr,value); break;
 
6653
  }
 
6654
}
 
6655
 
 
6656
 
 
6657
/**
 
6658
  @note
 
6659
    Storing a empty string in a enum field gives a warning
 
6660
    (if there isn't a empty value in the enum)
 
6661
*/
 
6662
 
 
6663
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
 
6664
{
 
6665
  int err= 0;
 
6666
  uint32 not_used;
 
6667
  char buff[STRING_BUFFER_USUAL_SIZE];
 
6668
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
6669
 
 
6670
  /* Convert character set if necessary */
 
6671
  if (String::needs_conversion(length, cs, field_charset, &not_used))
 
6672
  { 
 
6673
    uint dummy_errors;
 
6674
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
 
6675
    from= tmpstr.ptr();
 
6676
    length=  tmpstr.length();
 
6677
  }
 
6678
 
 
6679
  /* Remove end space */
 
6680
  length= field_charset->cset->lengthsp(field_charset, from, length);
 
6681
  uint tmp=find_type2(typelib, from, length, field_charset);
 
6682
  if (!tmp)
 
6683
  {
 
6684
    if (length < 6) // Can't be more than 99999 enums
 
6685
    {
 
6686
      /* This is for reading numbers with LOAD DATA INFILE */
 
6687
      char *end;
 
6688
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
 
6689
      if (err || end != from+length || tmp > typelib->count)
 
6690
      {
 
6691
        tmp=0;
 
6692
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6693
      }
 
6694
      if (!table->in_use->count_cuted_fields)
 
6695
        err= 0;
 
6696
    }
 
6697
    else
 
6698
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6699
  }
 
6700
  store_type((uint64_t) tmp);
 
6701
  return err;
 
6702
}
 
6703
 
 
6704
 
 
6705
int Field_enum::store(double nr)
 
6706
{
 
6707
  return Field_enum::store((int64_t) nr, false);
 
6708
}
 
6709
 
 
6710
 
 
6711
int Field_enum::store(int64_t nr,
 
6712
                      bool unsigned_val __attribute__((__unused__)))
 
6713
{
 
6714
  int error= 0;
 
6715
  if ((uint64_t) nr > typelib->count || nr == 0)
 
6716
  {
 
6717
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6718
    if (nr != 0 || table->in_use->count_cuted_fields)
 
6719
    {
 
6720
      nr= 0;
 
6721
      error= 1;
 
6722
    }
 
6723
  }
 
6724
  store_type((uint64_t) (uint) nr);
 
6725
  return error;
 
6726
}
 
6727
 
 
6728
 
 
6729
double Field_enum::val_real(void)
 
6730
{
 
6731
  return (double) Field_enum::val_int();
 
6732
}
 
6733
 
 
6734
 
 
6735
int64_t Field_enum::val_int(void)
 
6736
{
 
6737
  switch (packlength) {
 
6738
  case 1:
 
6739
    return (int64_t) ptr[0];
 
6740
  case 2:
 
6741
  {
 
6742
    uint16 tmp;
 
6743
#ifdef WORDS_BIGENDIAN
 
6744
    if (table->s->db_low_byte_first)
 
6745
      tmp=sint2korr(ptr);
 
6746
    else
 
6747
#endif
 
6748
      shortget(tmp,ptr);
 
6749
    return (int64_t) tmp;
 
6750
  }
 
6751
  case 3:
 
6752
    return (int64_t) uint3korr(ptr);
 
6753
  case 4:
 
6754
  {
 
6755
    uint32 tmp;
 
6756
#ifdef WORDS_BIGENDIAN
 
6757
    if (table->s->db_low_byte_first)
 
6758
      tmp=uint4korr(ptr);
 
6759
    else
 
6760
#endif
 
6761
      longget(tmp,ptr);
 
6762
    return (int64_t) tmp;
 
6763
  }
 
6764
  case 8:
 
6765
  {
 
6766
    int64_t tmp;
 
6767
#ifdef WORDS_BIGENDIAN
 
6768
    if (table->s->db_low_byte_first)
 
6769
      tmp=sint8korr(ptr);
 
6770
    else
 
6771
#endif
 
6772
      int64_tget(tmp,ptr);
 
6773
    return tmp;
 
6774
  }
 
6775
  }
 
6776
  return 0;                                     // impossible
 
6777
}
 
6778
 
 
6779
 
 
6780
/**
 
6781
   Save the field metadata for enum fields.
 
6782
 
 
6783
   Saves the real type in the first byte and the pack length in the 
 
6784
   second byte of the field metadata array at index of *metadata_ptr and
 
6785
   *(metadata_ptr + 1).
 
6786
 
 
6787
   @param   metadata_ptr   First byte of field metadata
 
6788
 
 
6789
   @returns number of bytes written to metadata_ptr
 
6790
*/
 
6791
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
 
6792
{
 
6793
  *metadata_ptr= real_type();
 
6794
  *(metadata_ptr + 1)= pack_length();
 
6795
  return 2;
 
6796
}
 
6797
 
 
6798
 
 
6799
String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
 
6800
                            String *val_ptr)
 
6801
{
 
6802
  uint tmp=(uint) Field_enum::val_int();
 
6803
  if (!tmp || tmp > typelib->count)
 
6804
    val_ptr->set("", 0, field_charset);
 
6805
  else
 
6806
    val_ptr->set((const char*) typelib->type_names[tmp-1],
 
6807
                 typelib->type_lengths[tmp-1],
 
6808
                 field_charset);
 
6809
  return val_ptr;
 
6810
}
 
6811
 
 
6812
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
6813
{
 
6814
  uchar *old= ptr;
 
6815
  ptr= (uchar*) a_ptr;
 
6816
  uint64_t a=Field_enum::val_int();
 
6817
  ptr= (uchar*) b_ptr;
 
6818
  uint64_t b=Field_enum::val_int();
 
6819
  ptr= old;
 
6820
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
6821
}
 
6822
 
 
6823
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
 
6824
{
 
6825
  uint64_t value=Field_enum::val_int();
 
6826
  to+=packlength-1;
 
6827
  for (uint i=0 ; i < packlength ; i++)
 
6828
  {
 
6829
    *to-- = (uchar) (value & 255);
 
6830
    value>>=8;
 
6831
  }
 
6832
}
 
6833
 
 
6834
 
 
6835
void Field_enum::sql_type(String &res) const
 
6836
{
 
6837
  char buffer[255];
 
6838
  String enum_item(buffer, sizeof(buffer), res.charset());
 
6839
 
 
6840
  res.length(0);
 
6841
  res.append(STRING_WITH_LEN("enum("));
 
6842
 
 
6843
  bool flag=0;
 
6844
  uint *len= typelib->type_lengths;
 
6845
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
 
6846
  {
 
6847
    uint dummy_errors;
 
6848
    if (flag)
 
6849
      res.append(',');
 
6850
    /* convert to res.charset() == utf8, then quote */
 
6851
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
 
6852
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
 
6853
    flag= 1;
 
6854
  }
 
6855
  res.append(')');
 
6856
}
 
6857
 
 
6858
 
 
6859
Field *Field_enum::new_field(MEM_ROOT *root, struct st_table *new_table,
 
6860
                             bool keep_type)
 
6861
{
 
6862
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
 
6863
  if (res)
 
6864
    res->typelib= copy_typelib(root, typelib);
 
6865
  return res;
 
6866
}
 
6867
 
 
6868
 
 
6869
/*
 
6870
   set type.
 
6871
   This is a string which can have a collection of different values.
 
6872
   Each string value is separated with a ','.
 
6873
   For example "One,two,five"
 
6874
   If one uses this string in a number context one gets the bits as a int64_t
 
6875
   number.
 
6876
*/
 
6877
 
 
6878
 
 
6879
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
 
6880
{
 
6881
  bool got_warning= 0;
 
6882
  int err= 0;
 
6883
  char *not_used;
 
6884
  uint not_used2;
 
6885
  uint32 not_used_offset;
 
6886
  char buff[STRING_BUFFER_USUAL_SIZE];
 
6887
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
 
6888
 
 
6889
  /* Convert character set if necessary */
 
6890
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
 
6891
  { 
 
6892
    uint dummy_errors;
 
6893
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
 
6894
    from= tmpstr.ptr();
 
6895
    length=  tmpstr.length();
 
6896
  }
 
6897
  uint64_t tmp= find_set(typelib, from, length, field_charset,
 
6898
                          &not_used, &not_used2, &got_warning);
 
6899
  if (!tmp && length && length < 22)
 
6900
  {
 
6901
    /* This is for reading numbers with LOAD DATA INFILE */
 
6902
    char *end;
 
6903
    tmp=my_strntoull(cs,from,length,10,&end,&err);
 
6904
    if (err || end != from+length ||
 
6905
        tmp > (uint64_t) (((int64_t) 1 << typelib->count) - (int64_t) 1))
 
6906
    {
 
6907
      tmp=0;      
 
6908
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6909
    }
 
6910
  }
 
6911
  else if (got_warning)
 
6912
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6913
  store_type(tmp);
 
6914
  return err;
 
6915
}
 
6916
 
 
6917
 
 
6918
int Field_set::store(int64_t nr,
 
6919
                     bool unsigned_val __attribute__((__unused__)))
 
6920
{
 
6921
  int error= 0;
 
6922
  uint64_t max_nr= set_bits(uint64_t, typelib->count);
 
6923
  if ((uint64_t) nr > max_nr)
 
6924
  {
 
6925
    nr&= max_nr;
 
6926
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
 
6927
    error=1;
 
6928
  }
 
6929
  store_type((uint64_t) nr);
 
6930
  return error;
 
6931
}
 
6932
 
 
6933
 
 
6934
String *Field_set::val_str(String *val_buffer,
 
6935
                           String *val_ptr __attribute__((unused)))
 
6936
{
 
6937
  uint64_t tmp=(uint64_t) Field_enum::val_int();
 
6938
  uint bitnr=0;
 
6939
 
 
6940
  val_buffer->length(0);
 
6941
  val_buffer->set_charset(field_charset);
 
6942
  while (tmp && bitnr < (uint) typelib->count)
 
6943
  {
 
6944
    if (tmp & 1)
 
6945
    {
 
6946
      if (val_buffer->length())
 
6947
        val_buffer->append(&field_separator, 1, &my_charset_latin1);
 
6948
      String str(typelib->type_names[bitnr],
 
6949
                 typelib->type_lengths[bitnr],
 
6950
                 field_charset);
 
6951
      val_buffer->append(str);
 
6952
    }
 
6953
    tmp>>=1;
 
6954
    bitnr++;
 
6955
  }
 
6956
  return val_buffer;
 
6957
}
 
6958
 
 
6959
 
 
6960
void Field_set::sql_type(String &res) const
 
6961
{
 
6962
  char buffer[255];
 
6963
  String set_item(buffer, sizeof(buffer), res.charset());
 
6964
 
 
6965
  res.length(0);
 
6966
  res.append(STRING_WITH_LEN("set("));
 
6967
 
 
6968
  bool flag=0;
 
6969
  uint *len= typelib->type_lengths;
 
6970
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
 
6971
  {
 
6972
    uint dummy_errors;
 
6973
    if (flag)
 
6974
      res.append(',');
 
6975
    /* convert to res.charset() == utf8, then quote */
 
6976
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
 
6977
    append_unescaped(&res, set_item.ptr(), set_item.length());
 
6978
    flag= 1;
 
6979
  }
 
6980
  res.append(')');
 
6981
}
 
6982
 
1607
6983
/**
1608
6984
  @retval
1609
6985
    1  if the fields are equally defined
1631
7007
 
1632
7008
  if (typelib->count < from_lib->count)
1633
7009
    return 0;
1634
 
  for (uint32_t i=0 ; i < from_lib->count ; i++)
 
7010
  for (uint i=0 ; i < from_lib->count ; i++)
1635
7011
    if (my_strnncoll(field_charset,
1636
 
                     (const unsigned char*)typelib->type_names[i],
 
7012
                     (const uchar*)typelib->type_names[i],
1637
7013
                     strlen(typelib->type_names[i]),
1638
 
                     (const unsigned char*)from_lib->type_names[i],
 
7014
                     (const uchar*)from_lib->type_names[i],
1639
7015
                     strlen(from_lib->type_names[i])))
1640
7016
      return 0;
1641
7017
  return 1;
1652
7028
  Field_num *from_num= (Field_num*) field;
1653
7029
 
1654
7030
  if (unsigned_flag != from_num->unsigned_flag ||
 
7031
      (zerofill && !from_num->zerofill && !zero_pack()) ||
1655
7032
      dec != from_num->dec)
1656
7033
    return 0;
1657
7034
  return 1;
1658
7035
}
1659
7036
 
1660
7037
 
1661
 
uint32_t Field_num::is_equal(Create_field *new_field)
 
7038
uint Field_num::is_equal(Create_field *new_field)
1662
7039
{
1663
7040
  return ((new_field->sql_type == real_type()) &&
1664
 
          ((new_field->flags & UNSIGNED_FLAG) == (uint32_t) (flags &
 
7041
          ((new_field->flags & UNSIGNED_FLAG) == (uint) (flags &
1665
7042
                                                         UNSIGNED_FLAG)) &&
1666
7043
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
1667
 
           (uint32_t) (flags & AUTO_INCREMENT_FLAG)) &&
 
7044
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
1668
7045
          (new_field->length <= max_display_length()));
1669
7046
}
1670
7047
 
1680
7057
void Create_field::create_length_to_internal_length(void)
1681
7058
{
1682
7059
  switch (sql_type) {
1683
 
  case DRIZZLE_TYPE_BLOB:
1684
 
  case DRIZZLE_TYPE_VARCHAR:
 
7060
  case MYSQL_TYPE_BLOB:
 
7061
  case MYSQL_TYPE_VAR_STRING:
 
7062
  case MYSQL_TYPE_STRING:
 
7063
  case MYSQL_TYPE_VARCHAR:
1685
7064
    length*= charset->mbmaxlen;
1686
7065
    key_length= length;
1687
7066
    pack_length= calc_pack_length(sql_type, length);
1688
7067
    break;
1689
 
  case DRIZZLE_TYPE_ENUM:
 
7068
  case MYSQL_TYPE_ENUM:
 
7069
  case MYSQL_TYPE_SET:
1690
7070
    /* Pack_length already calculated in sql_parse.cc */
1691
7071
    length*= charset->mbmaxlen;
1692
7072
    key_length= pack_length;
1693
7073
    break;
1694
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7074
  case MYSQL_TYPE_NEWDECIMAL:
1695
7075
    key_length= pack_length=
1696
7076
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1697
7077
                                                                decimals,
1710
7090
  Init for a tmp table field. To be extended if need be.
1711
7091
*/
1712
7092
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1713
 
                                      uint32_t length_arg, uint32_t decimals_arg,
 
7093
                                      uint32 length_arg, uint32 decimals_arg,
1714
7094
                                      bool maybe_null, bool is_unsigned)
1715
7095
{
1716
7096
  field_name= "";
1748
7128
    true  on error
1749
7129
*/
1750
7130
 
1751
 
bool Create_field::init(THD *thd __attribute__((unused)), char *fld_name, enum_field_types fld_type,
 
7131
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
1752
7132
                        char *fld_length, char *fld_decimals,
1753
 
                        uint32_t fld_type_modifier, Item *fld_default_value,
 
7133
                        uint fld_type_modifier, Item *fld_default_value,
1754
7134
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
1755
7135
                        char *fld_change, List<String> *fld_interval_list,
1756
 
                        const CHARSET_INFO * const fld_charset,
1757
 
                        uint32_t fld_geom_type __attribute__((unused)),
 
7136
                        CHARSET_INFO *fld_charset,
 
7137
                        uint fld_geom_type __attribute__((__unused__)),
1758
7138
                        enum column_format_type column_format)
1759
7139
{
1760
 
  uint32_t sign_len, allowed_type_modifier= 0;
1761
 
  uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
 
7140
  uint sign_len, allowed_type_modifier= 0;
 
7141
  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
1762
7142
 
1763
7143
  field= 0;
1764
7144
  field_name= fld_name;
1765
7145
  def= fld_default_value;
1766
7146
  flags= fld_type_modifier;
1767
 
  flags|= (((uint32_t)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
 
7147
  flags|= (((uint)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1768
7148
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1769
7149
                 Field::NEXT_NUMBER : Field::NONE);
1770
 
  decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
 
7150
  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
1771
7151
  if (decimals >= NOT_FIXED_DEC)
1772
7152
  {
1773
7153
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1789
7169
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1790
7170
  */
1791
7171
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1792
 
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
 
7172
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != MYSQL_TYPE_TIMESTAMP)
1793
7173
    flags|= NO_DEFAULT_VALUE_FLAG;
1794
7174
 
1795
 
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
 
7175
  if (fld_length && !(length= (uint) atoi(fld_length)))
1796
7176
    fld_length= 0; /* purecov: inspected */
1797
7177
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1798
7178
 
1799
7179
  switch (fld_type) {
1800
 
  case DRIZZLE_TYPE_TINY:
 
7180
  case MYSQL_TYPE_TINY:
1801
7181
    if (!fld_length)
1802
7182
      length= MAX_TINYINT_WIDTH+sign_len;
1803
7183
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1804
7184
    break;
1805
 
  case DRIZZLE_TYPE_LONG:
 
7185
  case MYSQL_TYPE_SHORT:
 
7186
    if (!fld_length)
 
7187
      length= MAX_SMALLINT_WIDTH+sign_len;
 
7188
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
7189
    break;
 
7190
  case MYSQL_TYPE_LONG:
1806
7191
    if (!fld_length)
1807
7192
      length= MAX_INT_WIDTH+sign_len;
1808
7193
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1809
7194
    break;
1810
 
  case DRIZZLE_TYPE_LONGLONG:
 
7195
  case MYSQL_TYPE_LONGLONG:
1811
7196
    if (!fld_length)
1812
7197
      length= MAX_BIGINT_WIDTH;
1813
7198
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1814
7199
    break;
1815
 
  case DRIZZLE_TYPE_NULL:
 
7200
  case MYSQL_TYPE_NULL:
1816
7201
    break;
1817
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7202
  case MYSQL_TYPE_NEWDECIMAL:
1818
7203
    my_decimal_trim(&length, &decimals);
1819
7204
    if (length > DECIMAL_MAX_PRECISION)
1820
7205
    {
1833
7218
    pack_length=
1834
7219
      my_decimal_get_binary_size(length, decimals);
1835
7220
    break;
1836
 
  case DRIZZLE_TYPE_VARCHAR:
 
7221
  case MYSQL_TYPE_VARCHAR:
1837
7222
    /*
1838
7223
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1839
7224
      if they don't have a default value
1840
7225
    */
1841
7226
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1842
7227
    break;
1843
 
  case DRIZZLE_TYPE_BLOB:
 
7228
  case MYSQL_TYPE_STRING:
 
7229
    break;
 
7230
  case MYSQL_TYPE_BLOB:
1844
7231
    if (fld_default_value)
1845
7232
    {
1846
7233
      /* Allow empty as default value. */
1847
7234
      String str,*res;
1848
7235
      res= fld_default_value->val_str(&str);
 
7236
      /*
 
7237
        A default other than '' is always an error, and any non-NULL
 
7238
        specified default is an error in strict mode.
 
7239
      */
 
7240
      if (res->length() || (thd->variables.sql_mode &
 
7241
                            (MODE_STRICT_TRANS_TABLES |
 
7242
                             MODE_STRICT_ALL_TABLES)))
 
7243
      {
 
7244
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
 
7245
                 fld_name); /* purecov: inspected */
 
7246
        return(true);
 
7247
      }
 
7248
      else
 
7249
      {
 
7250
        /*
 
7251
          Otherwise a default of '' is just a warning.
 
7252
        */
 
7253
        push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
7254
                            ER_BLOB_CANT_HAVE_DEFAULT,
 
7255
                            ER(ER_BLOB_CANT_HAVE_DEFAULT),
 
7256
                            fld_name);
 
7257
      }
 
7258
      def= 0;
1849
7259
    }
1850
7260
    flags|= BLOB_FLAG;
1851
7261
    break;
1852
 
  case DRIZZLE_TYPE_DOUBLE:
 
7262
  case MYSQL_TYPE_YEAR:
 
7263
    if (!fld_length || length != 2)
 
7264
      length= 4; /* Default length */
 
7265
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
7266
    break;
 
7267
  case MYSQL_TYPE_FLOAT:
 
7268
    /* change FLOAT(precision) to FLOAT or DOUBLE */
 
7269
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
7270
    if (fld_length && !fld_decimals)
 
7271
    {
 
7272
      uint tmp_length= length;
 
7273
      if (tmp_length > PRECISION_FOR_DOUBLE)
 
7274
      {
 
7275
        my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
 
7276
        return(true);
 
7277
      }
 
7278
      else if (tmp_length > PRECISION_FOR_FLOAT)
 
7279
      {
 
7280
        sql_type= MYSQL_TYPE_DOUBLE;
 
7281
        length= DBL_DIG+7; /* -[digits].E+### */
 
7282
      }
 
7283
      else
 
7284
        length= FLT_DIG+6; /* -[digits].E+## */
 
7285
      decimals= NOT_FIXED_DEC;
 
7286
      break;
 
7287
    }
 
7288
    if (!fld_length && !fld_decimals)
 
7289
    {
 
7290
      length=  FLT_DIG+6;
 
7291
      decimals= NOT_FIXED_DEC;
 
7292
    }
 
7293
    if (length < decimals &&
 
7294
        decimals != NOT_FIXED_DEC)
 
7295
    {
 
7296
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
 
7297
      return(true);
 
7298
    }
 
7299
    break;
 
7300
  case MYSQL_TYPE_DOUBLE:
1853
7301
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1854
7302
    if (!fld_length && !fld_decimals)
1855
7303
    {
1863
7311
      return(true);
1864
7312
    }
1865
7313
    break;
1866
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
7314
  case MYSQL_TYPE_TIMESTAMP:
1867
7315
    if (!fld_length)
1868
7316
    {
1869
7317
      /* Compressed date YYYYMMDDHHMMSS */
1876
7324
        and 19 as length of 4.1 compatible representation.
1877
7325
      */
1878
7326
      length= ((length+1)/2)*2; /* purecov: inspected */
1879
 
      length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
 
7327
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1880
7328
    }
1881
 
    flags|= UNSIGNED_FLAG;
 
7329
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
1882
7330
    if (fld_default_value)
1883
7331
    {
1884
7332
      /* Grammar allows only NOW() value for ON UPDATE clause */
1917
7365
                                              Field::NONE));
1918
7366
    }
1919
7367
    break;
1920
 
  case DRIZZLE_TYPE_NEWDATE:
1921
 
    length= 10;
1922
 
    break;
1923
 
  case DRIZZLE_TYPE_TIME:
1924
 
    length= 10;
1925
 
    break;
1926
 
  case DRIZZLE_TYPE_DATETIME:
 
7368
  case MYSQL_TYPE_DATE:
 
7369
    /* Old date type. */
 
7370
    sql_type= MYSQL_TYPE_NEWDATE;
 
7371
    /* fall trough */
 
7372
  case MYSQL_TYPE_NEWDATE:
 
7373
    length= 10;
 
7374
    break;
 
7375
  case MYSQL_TYPE_TIME:
 
7376
    length= 10;
 
7377
    break;
 
7378
  case MYSQL_TYPE_DATETIME:
1927
7379
    length= MAX_DATETIME_WIDTH;
1928
7380
    break;
1929
 
  case DRIZZLE_TYPE_ENUM:
 
7381
  case MYSQL_TYPE_SET:
 
7382
    {
 
7383
      pack_length= get_set_pack_length(fld_interval_list->elements);
 
7384
 
 
7385
      List_iterator<String> it(*fld_interval_list);
 
7386
      String *tmp;
 
7387
      while ((tmp= it++))
 
7388
        interval_list.push_back(tmp);
 
7389
      /*
 
7390
        Set fake length to 1 to pass the below conditions.
 
7391
        Real length will be set in mysql_prepare_table()
 
7392
        when we know the character set of the column
 
7393
      */
 
7394
      length= 1;
 
7395
      break;
 
7396
    }
 
7397
  case MYSQL_TYPE_ENUM:
1930
7398
    {
1931
7399
      /* Should be safe. */
1932
7400
      pack_length= get_enum_pack_length(fld_interval_list->elements);
1935
7403
      String *tmp;
1936
7404
      while ((tmp= it++))
1937
7405
        interval_list.push_back(tmp);
1938
 
      length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
 
7406
      length= 1; /* See comment for MYSQL_TYPE_SET above. */
1939
7407
      break;
1940
7408
   }
 
7409
  case MYSQL_TYPE_VAR_STRING:
 
7410
  case MYSQL_TYPE_INT24:
 
7411
    assert(0);  /* Impossible, we killed it */
 
7412
    break;
1941
7413
  }
1942
7414
  /* Remember the value of length */
1943
7415
  char_length= length;
1944
7416
 
1945
7417
  if (!(flags & BLOB_FLAG) &&
1946
 
      ((length > max_field_charlength &&
1947
 
        fld_type != DRIZZLE_TYPE_ENUM &&
1948
 
        (fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1949
 
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
 
7418
      ((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
 
7419
        fld_type != MYSQL_TYPE_ENUM &&
 
7420
        (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
 
7421
       (!length &&
 
7422
        fld_type != MYSQL_TYPE_STRING &&
 
7423
        fld_type != MYSQL_TYPE_VARCHAR)))
1950
7424
  {
1951
 
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
 
7425
    my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
 
7426
              fld_type == MYSQL_TYPE_VARCHAR ||
 
7427
              fld_type == MYSQL_TYPE_STRING) ?  ER_TOO_BIG_FIELDLENGTH :
 
7428
                                                ER_TOO_BIG_DISPLAYWIDTH,
1952
7429
              MYF(0),
1953
7430
              fld_name, max_field_charlength); /* purecov: inspected */
1954
7431
    return(true);
1964
7441
}
1965
7442
 
1966
7443
 
1967
 
enum_field_types get_blob_type_from_length(uint32_t length __attribute__((unused)))
 
7444
enum_field_types get_blob_type_from_length(ulong length __attribute__((__unused__)))
1968
7445
{
1969
7446
  enum_field_types type;
1970
7447
 
1971
 
  type= DRIZZLE_TYPE_BLOB;
 
7448
  type= MYSQL_TYPE_BLOB;
1972
7449
 
1973
7450
  return type;
1974
7451
}
1978
7455
  Make a field from the .frm file info
1979
7456
*/
1980
7457
 
1981
 
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
 
7458
uint32 calc_pack_length(enum_field_types type,uint32 length)
1982
7459
{
1983
7460
  switch (type) {
1984
 
  case DRIZZLE_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
1985
 
  case DRIZZLE_TYPE_TINY        : return 1;
1986
 
  case DRIZZLE_TYPE_NEWDATE:
1987
 
  case DRIZZLE_TYPE_TIME:   return 3;
1988
 
  case DRIZZLE_TYPE_TIMESTAMP:
1989
 
  case DRIZZLE_TYPE_LONG        : return 4;
1990
 
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1991
 
  case DRIZZLE_TYPE_DATETIME:
1992
 
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1993
 
  case DRIZZLE_TYPE_NULL        : return 0;
1994
 
  case DRIZZLE_TYPE_BLOB:               return 4+portable_sizeof_char_ptr;
1995
 
  case DRIZZLE_TYPE_ENUM:
1996
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7461
  case MYSQL_TYPE_VAR_STRING:
 
7462
  case MYSQL_TYPE_STRING:
 
7463
  case MYSQL_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
 
7464
  case MYSQL_TYPE_YEAR:
 
7465
  case MYSQL_TYPE_TINY  : return 1;
 
7466
  case MYSQL_TYPE_SHORT : return 2;
 
7467
  case MYSQL_TYPE_DATE:
 
7468
  case MYSQL_TYPE_NEWDATE:
 
7469
  case MYSQL_TYPE_TIME:   return 3;
 
7470
  case MYSQL_TYPE_TIMESTAMP:
 
7471
  case MYSQL_TYPE_LONG  : return 4;
 
7472
  case MYSQL_TYPE_FLOAT : return sizeof(float);
 
7473
  case MYSQL_TYPE_DOUBLE: return sizeof(double);
 
7474
  case MYSQL_TYPE_DATETIME:
 
7475
  case MYSQL_TYPE_LONGLONG: return 8;   /* Don't crash if no int64_t */
 
7476
  case MYSQL_TYPE_NULL  : return 0;
 
7477
  case MYSQL_TYPE_BLOB:         return 4+portable_sizeof_char_ptr;
 
7478
  case MYSQL_TYPE_SET:
 
7479
  case MYSQL_TYPE_ENUM:
 
7480
  case MYSQL_TYPE_NEWDECIMAL:
1997
7481
    abort(); return 0;                          // This shouldn't happen
1998
7482
  default:
1999
7483
    return 0;
2001
7485
}
2002
7486
 
2003
7487
 
2004
 
uint32_t pack_length_to_packflag(uint32_t type)
 
7488
uint pack_length_to_packflag(uint type)
2005
7489
{
2006
7490
  switch (type) {
2007
 
    case 1: return f_settype((uint32_t) DRIZZLE_TYPE_TINY);
2008
 
    case 2: assert(1);
 
7491
    case 1: return f_settype((uint) MYSQL_TYPE_TINY);
 
7492
    case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
2009
7493
    case 3: assert(1);
2010
 
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
2011
 
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
 
7494
    case 4: return f_settype((uint) MYSQL_TYPE_LONG);
 
7495
    case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
2012
7496
  }
2013
7497
  return 0;                                     // This shouldn't happen
2014
7498
}
2015
7499
 
2016
7500
 
2017
 
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
2018
 
                  unsigned char *null_pos, unsigned char null_bit,
2019
 
                  uint32_t pack_flag,
 
7501
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
 
7502
                  uchar *null_pos, uchar null_bit,
 
7503
                  uint pack_flag,
2020
7504
                  enum_field_types field_type,
2021
 
                  const CHARSET_INFO * field_charset,
 
7505
                  CHARSET_INFO *field_charset,
2022
7506
                  Field::utype unireg_check,
2023
7507
                  TYPELIB *interval,
2024
7508
                  const char *field_name)
2030
7514
  }
2031
7515
  else
2032
7516
  {
2033
 
    null_bit= ((unsigned char) 1) << null_bit;
 
7517
    null_bit= ((uchar) 1) << null_bit;
2034
7518
  }
2035
7519
 
2036
7520
  switch (field_type) {
2037
 
  case DRIZZLE_TYPE_NEWDATE:
2038
 
  case DRIZZLE_TYPE_TIME:
2039
 
  case DRIZZLE_TYPE_DATETIME:
2040
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
7521
  case MYSQL_TYPE_DATE:
 
7522
  case MYSQL_TYPE_NEWDATE:
 
7523
  case MYSQL_TYPE_TIME:
 
7524
  case MYSQL_TYPE_DATETIME:
 
7525
  case MYSQL_TYPE_TIMESTAMP:
2041
7526
    field_charset= &my_charset_bin;
2042
7527
  default: break;
2043
7528
  }
2046
7531
  {
2047
7532
    if (!f_is_packed(pack_flag))
2048
7533
    {
2049
 
      if (field_type == DRIZZLE_TYPE_VARCHAR)
 
7534
      if (field_type == MYSQL_TYPE_STRING ||
 
7535
          field_type == MYSQL_TYPE_VAR_STRING)
 
7536
        return new Field_string(ptr,field_length,null_pos,null_bit,
 
7537
                                unireg_check, field_name,
 
7538
                                field_charset);
 
7539
      if (field_type == MYSQL_TYPE_VARCHAR)
2050
7540
        return new Field_varstring(ptr,field_length,
2051
7541
                                   HA_VARCHAR_PACKLENGTH(field_length),
2052
7542
                                   null_pos,null_bit,
2056
7546
      return 0;                                 // Error
2057
7547
    }
2058
7548
 
2059
 
    uint32_t pack_length=calc_pack_length((enum_field_types)
 
7549
    uint pack_length=calc_pack_length((enum_field_types)
2060
7550
                                      f_packtype(pack_flag),
2061
7551
                                      field_length);
2062
7552
 
2070
7560
        return new Field_enum(ptr,field_length,null_pos,null_bit,
2071
7561
                                  unireg_check, field_name,
2072
7562
                                  pack_length, interval, field_charset);
 
7563
      else
 
7564
        return new Field_set(ptr,field_length,null_pos,null_bit,
 
7565
                             unireg_check, field_name,
 
7566
                             pack_length, interval, field_charset);
2073
7567
    }
2074
7568
  }
2075
7569
 
2076
7570
  switch (field_type) {
2077
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
7571
  case MYSQL_TYPE_NEWDECIMAL:
2078
7572
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
2079
7573
                                 unireg_check, field_name,
2080
7574
                                 f_decimals(pack_flag),
2081
 
                                 f_is_decimal_precision(pack_flag) != 0,
 
7575
                                 f_is_zerofill(pack_flag) != 0,
2082
7576
                                 f_is_dec(pack_flag) == 0);
2083
 
  case DRIZZLE_TYPE_DOUBLE:
 
7577
  case MYSQL_TYPE_FLOAT:
 
7578
    return new Field_float(ptr,field_length,null_pos,null_bit,
 
7579
                           unireg_check, field_name,
 
7580
                           f_decimals(pack_flag),
 
7581
                           f_is_zerofill(pack_flag) != 0,
 
7582
                           f_is_dec(pack_flag)== 0);
 
7583
  case MYSQL_TYPE_DOUBLE:
2084
7584
    return new Field_double(ptr,field_length,null_pos,null_bit,
2085
7585
                            unireg_check, field_name,
2086
7586
                            f_decimals(pack_flag),
2087
 
                            false,
 
7587
                            f_is_zerofill(pack_flag) != 0,
2088
7588
                            f_is_dec(pack_flag)== 0);
2089
 
  case DRIZZLE_TYPE_TINY:
 
7589
  case MYSQL_TYPE_TINY:
2090
7590
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
2091
7591
                          unireg_check, field_name,
2092
 
                          false,
 
7592
                          f_is_zerofill(pack_flag) != 0,
2093
7593
                          f_is_dec(pack_flag) == 0);
2094
 
  case DRIZZLE_TYPE_LONG:
 
7594
  case MYSQL_TYPE_SHORT:
 
7595
    return new Field_short(ptr,field_length,null_pos,null_bit,
 
7596
                           unireg_check, field_name,
 
7597
                           f_is_zerofill(pack_flag) != 0,
 
7598
                           f_is_dec(pack_flag) == 0);
 
7599
  case MYSQL_TYPE_LONG:
2095
7600
    return new Field_long(ptr,field_length,null_pos,null_bit,
2096
7601
                           unireg_check, field_name,
2097
 
                           false,
 
7602
                           f_is_zerofill(pack_flag) != 0,
2098
7603
                           f_is_dec(pack_flag) == 0);
2099
 
  case DRIZZLE_TYPE_LONGLONG:
 
7604
  case MYSQL_TYPE_LONGLONG:
2100
7605
    return new Field_int64_t(ptr,field_length,null_pos,null_bit,
2101
7606
                              unireg_check, field_name,
2102
 
                              false,
 
7607
                              f_is_zerofill(pack_flag) != 0,
2103
7608
                              f_is_dec(pack_flag) == 0);
2104
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
7609
  case MYSQL_TYPE_TIMESTAMP:
2105
7610
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
2106
7611
                               unireg_check, field_name, share,
2107
7612
                               field_charset);
2108
 
  case DRIZZLE_TYPE_NEWDATE:
 
7613
  case MYSQL_TYPE_YEAR:
 
7614
    return new Field_year(ptr,field_length,null_pos,null_bit,
 
7615
                          unireg_check, field_name);
 
7616
  case MYSQL_TYPE_DATE:
 
7617
  case MYSQL_TYPE_NEWDATE:
2109
7618
    return new Field_newdate(ptr,null_pos,null_bit,
2110
7619
                             unireg_check, field_name, field_charset);
2111
 
  case DRIZZLE_TYPE_TIME:
 
7620
  case MYSQL_TYPE_TIME:
2112
7621
    return new Field_time(ptr,null_pos,null_bit,
2113
7622
                          unireg_check, field_name, field_charset);
2114
 
  case DRIZZLE_TYPE_DATETIME:
 
7623
  case MYSQL_TYPE_DATETIME:
2115
7624
    return new Field_datetime(ptr,null_pos,null_bit,
2116
7625
                              unireg_check, field_name, field_charset);
2117
 
  case DRIZZLE_TYPE_NULL:
 
7626
  case MYSQL_TYPE_NULL:
2118
7627
    return new Field_null(ptr, field_length, unireg_check, field_name,
2119
7628
                          field_charset);
2120
7629
  default:                                      // Impossible (Wrong version)
2146
7655
                  portable_sizeof_char_ptr);
2147
7656
 
2148
7657
  switch (sql_type) {
2149
 
  case DRIZZLE_TYPE_BLOB:
2150
 
    sql_type= DRIZZLE_TYPE_BLOB;
 
7658
  case MYSQL_TYPE_BLOB:
 
7659
    sql_type= MYSQL_TYPE_BLOB;
2151
7660
    length/= charset->mbmaxlen;
2152
7661
    key_length/= charset->mbmaxlen;
2153
7662
    break;
 
7663
  case MYSQL_TYPE_STRING:
2154
7664
    /* Change CHAR -> VARCHAR if dynamic record length */
2155
 
  case DRIZZLE_TYPE_ENUM:
2156
 
  case DRIZZLE_TYPE_VARCHAR:
 
7665
    if (old_field->type() == MYSQL_TYPE_VAR_STRING)
 
7666
      sql_type= MYSQL_TYPE_VARCHAR;
 
7667
    /* fall through */
 
7668
 
 
7669
  case MYSQL_TYPE_ENUM:
 
7670
  case MYSQL_TYPE_SET:
 
7671
  case MYSQL_TYPE_VARCHAR:
 
7672
  case MYSQL_TYPE_VAR_STRING:
2157
7673
    /* This is corrected in create_length_to_internal_length */
2158
7674
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
2159
7675
    break;
2170
7686
 
2171
7687
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
2172
7688
      old_field->ptr && orig_field &&
2173
 
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
 
7689
      (sql_type != MYSQL_TYPE_TIMESTAMP ||                /* set def only if */
2174
7690
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
2175
7691
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
2176
7692
  {
2195
7711
}
2196
7712
 
2197
7713
 
 
7714
/**
 
7715
  maximum possible display length for blob.
 
7716
 
 
7717
  @return
 
7718
    length
 
7719
*/
 
7720
 
 
7721
uint32 Field_blob::max_display_length()
 
7722
{
 
7723
  switch (packlength)
 
7724
  {
 
7725
  case 1:
 
7726
    return 255 * field_charset->mbmaxlen;
 
7727
  case 2:
 
7728
    return 65535 * field_charset->mbmaxlen;
 
7729
  case 3:
 
7730
    return 16777215 * field_charset->mbmaxlen;
 
7731
  case 4:
 
7732
    return (uint32) 4294967295U;
 
7733
  default:
 
7734
    assert(0); // we should never go here
 
7735
    return 0;
 
7736
  }
 
7737
}
 
7738
 
 
7739
 
2198
7740
/*****************************************************************************
2199
7741
 Warning handling
2200
7742
*****************************************************************************/
2220
7762
*/
2221
7763
 
2222
7764
bool 
2223
 
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
 
7765
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
2224
7766
                   int cuted_increment)
2225
7767
{
2226
7768
  /*
2235
7777
                        thd->row_count);
2236
7778
    return 0;
2237
7779
  }
2238
 
  return level >= DRIZZLE_ERROR::WARN_LEVEL_WARN;
 
7780
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
2239
7781
}
2240
7782
 
2241
7783
 
2256
7798
*/
2257
7799
 
2258
7800
void 
2259
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
2260
 
                            unsigned int code, 
2261
 
                            const char *str, uint32_t str_length, 
2262
 
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
 
7801
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
7802
                            const char *str, uint str_length, 
 
7803
                            timestamp_type ts_type, int cuted_increment)
2263
7804
{
2264
7805
  THD *thd= table ? table->in_use : current_thd;
2265
7806
  if ((thd->really_abort_on_warning() &&
2266
 
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
 
7807
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
2267
7808
      set_warning(level, code, cuted_increment))
2268
7809
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
2269
7810
                                 field_name);
2286
7827
*/
2287
7828
 
2288
7829
void 
2289
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code, 
2290
 
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
 
7830
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
7831
                            int64_t nr, timestamp_type ts_type,
2291
7832
                            int cuted_increment)
2292
7833
{
2293
7834
  THD *thd= table ? table->in_use : current_thd;
2296
7837
  {
2297
7838
    char str_nr[22];
2298
7839
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
2299
 
    make_truncated_value_warning(thd, level, str_nr, (uint32_t) (str_end - str_nr), 
 
7840
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
2300
7841
                                 ts_type, field_name);
2301
7842
  }
2302
7843
}
2317
7858
*/
2318
7859
 
2319
7860
void 
2320
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code, 
2321
 
                            double nr, enum enum_drizzle_timestamp_type ts_type)
 
7861
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
7862
                            double nr, timestamp_type ts_type)
2322
7863
{
2323
7864
  THD *thd= table ? table->in_use : current_thd;
2324
7865
  if (thd->really_abort_on_warning() ||
2326
7867
  {
2327
7868
    /* DBL_DIG is enough to print '-[digits].E+###' */
2328
7869
    char str_nr[DBL_DIG + 8];
2329
 
    uint32_t str_len= sprintf(str_nr, "%g", nr);
 
7870
    uint str_len= my_sprintf(str_nr, (str_nr, "%g", nr));
2330
7871
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
2331
7872
                                 field_name);
2332
7873
  }