~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/field.cc

  • Committer: Jay Pipes
  • Date: 2008-07-21 17:52:33 UTC
  • mto: (201.2.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 204.
  • Revision ID: jay@mysql.com-20080721175233-mtyz298j8xl3v63y
cleanup of FAQ file

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