~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/field.cc

  • Committer: Jay Pipes
  • Date: 2008-07-17 18:48:58 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717184858-2mbouxl8xi41gcge
Removed DBUG from CSV and Blackhole storage engines

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2010 Brian Aker
5
 
 *  Copyright (C) 2008 MySQL
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2 of the License, or
10
 
 *  (at your option) any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 
 */
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
21
16
 
22
17
/**
23
 
 * @file This file implements the Field class and API
24
 
 */
25
 
 
26
 
#include <config.h>
27
 
#include <cstdio>
 
18
  @file
 
19
 
 
20
  @brief
 
21
  This file implements classes defined in field.h
 
22
*/
 
23
 
 
24
#ifdef USE_PRAGMA_IMPLEMENTATION
 
25
#pragma implementation                          // gcc: Class implementation
 
26
#endif
 
27
 
 
28
#include "mysql_priv.h"
 
29
#include "sql_select.h"
 
30
#include <m_ctype.h>
28
31
#include <errno.h>
29
 
#include <float.h>
30
 
#include <drizzled/sql_select.h>
31
 
#include <drizzled/error.h>
32
 
#include <drizzled/field/str.h>
33
 
#include <drizzled/field/num.h>
34
 
#include <drizzled/field/blob.h>
35
 
#include <drizzled/field/boolean.h>
36
 
#include <drizzled/field/enum.h>
37
 
#include <drizzled/field/null.h>
38
 
#include <drizzled/field/date.h>
39
 
#include <drizzled/field/decimal.h>
40
 
#include <drizzled/field/real.h>
41
 
#include <drizzled/field/double.h>
42
 
#include <drizzled/field/int32.h>
43
 
#include <drizzled/field/int64.h>
44
 
#include <drizzled/field/num.h>
45
 
#include <drizzled/field/time.h>
46
 
#include <drizzled/field/epoch.h>
47
 
#include <drizzled/field/datetime.h>
48
 
#include <drizzled/field/microtime.h>
49
 
#include <drizzled/field/varstring.h>
50
 
#include <drizzled/field/uuid.h>
51
 
#include <drizzled/time_functions.h>
52
 
#include <drizzled/internal/m_string.h>
53
 
#include <drizzled/table.h>
54
 
#include <drizzled/util/test.h>
55
 
#include <drizzled/session.h>
56
 
#include <drizzled/current_session.h>
57
 
#include <drizzled/display.h>
58
 
#include <drizzled/typelib.h>
59
32
 
60
 
namespace drizzled
61
 
{
 
33
// Maximum allowed exponent value for converting string to decimal
 
34
#define MAX_EXPONENT 1024
62
35
 
63
36
/*****************************************************************************
64
37
  Instansiate templates and static variables
65
38
*****************************************************************************/
66
39
 
67
 
static enum_field_types
68
 
field_types_merge_rules [enum_field_types_size][enum_field_types_size]=
69
 
{
70
 
  /* DRIZZLE_TYPE_LONG -> */
71
 
  {
72
 
    //DRIZZLE_TYPE_LONG
73
 
    DRIZZLE_TYPE_LONG,
74
 
    //DRIZZLE_TYPE_DOUBLE
75
 
    DRIZZLE_TYPE_DOUBLE,
76
 
    //DRIZZLE_TYPE_NULL
77
 
    DRIZZLE_TYPE_LONG,
78
 
    //DRIZZLE_TYPE_TIMESTAMP
79
 
    DRIZZLE_TYPE_VARCHAR,
80
 
    //DRIZZLE_TYPE_LONGLONG
81
 
    DRIZZLE_TYPE_LONGLONG,
82
 
    //DRIZZLE_TYPE_DATETIME
83
 
    DRIZZLE_TYPE_VARCHAR,
84
 
    //DRIZZLE_TYPE_DATE
85
 
    DRIZZLE_TYPE_VARCHAR,
86
 
    //DRIZZLE_TYPE_VARCHAR
87
 
    DRIZZLE_TYPE_VARCHAR,
88
 
    //DRIZZLE_TYPE_DECIMAL
89
 
    DRIZZLE_TYPE_DECIMAL,
90
 
    //DRIZZLE_TYPE_ENUM
91
 
    DRIZZLE_TYPE_VARCHAR,
92
 
    //DRIZZLE_TYPE_BLOB
93
 
    DRIZZLE_TYPE_BLOB,
94
 
    //DRIZZLE_TYPE_TIME
95
 
    DRIZZLE_TYPE_TIME,
96
 
    //DRIZZLE_TYPE_BOOLEAN
97
 
    DRIZZLE_TYPE_VARCHAR,
98
 
    //DRIZZLE_TYPE_UUID
99
 
    DRIZZLE_TYPE_VARCHAR,
100
 
    //DRIZZLE_TYPE_MICROTIME
101
 
    DRIZZLE_TYPE_VARCHAR,
102
 
  },
103
 
  /* DRIZZLE_TYPE_DOUBLE -> */
104
 
  {
105
 
    //DRIZZLE_TYPE_LONG
106
 
    DRIZZLE_TYPE_DOUBLE,
107
 
    //DRIZZLE_TYPE_DOUBLE
108
 
    DRIZZLE_TYPE_DOUBLE,
109
 
    //DRIZZLE_TYPE_NULL
110
 
    DRIZZLE_TYPE_DOUBLE,
111
 
    //DRIZZLE_TYPE_TIMESTAMP
112
 
    DRIZZLE_TYPE_VARCHAR,
113
 
    //DRIZZLE_TYPE_LONGLONG
114
 
    DRIZZLE_TYPE_DOUBLE,
115
 
    //DRIZZLE_TYPE_DATETIME
116
 
    DRIZZLE_TYPE_VARCHAR,
117
 
    //DRIZZLE_TYPE_DATE
118
 
    DRIZZLE_TYPE_VARCHAR,
119
 
    //DRIZZLE_TYPE_VARCHAR
120
 
    DRIZZLE_TYPE_VARCHAR,
121
 
    //DRIZZLE_TYPE_DECIMAL
122
 
    DRIZZLE_TYPE_DOUBLE,
123
 
    //DRIZZLE_TYPE_ENUM
124
 
    DRIZZLE_TYPE_VARCHAR,
125
 
    //DRIZZLE_TYPE_BLOB
126
 
    DRIZZLE_TYPE_BLOB,
127
 
    //DRIZZLE_TYPE_TIME
128
 
    DRIZZLE_TYPE_TIME,
129
 
    //DRIZZLE_TYPE_BOOLEAN
130
 
    DRIZZLE_TYPE_VARCHAR,
131
 
    //DRIZZLE_TYPE_UUID
132
 
    DRIZZLE_TYPE_VARCHAR,
133
 
    //DRIZZLE_TYPE_MICROTIME
134
 
    DRIZZLE_TYPE_VARCHAR,
135
 
  },
136
 
  /* DRIZZLE_TYPE_NULL -> */
137
 
  {
138
 
    //DRIZZLE_TYPE_LONG
139
 
    DRIZZLE_TYPE_LONG,
140
 
    //DRIZZLE_TYPE_DOUBLE
141
 
    DRIZZLE_TYPE_DOUBLE,
142
 
    //DRIZZLE_TYPE_NULL
143
 
    DRIZZLE_TYPE_NULL,
144
 
    //DRIZZLE_TYPE_TIMESTAMP
145
 
    DRIZZLE_TYPE_TIMESTAMP,
146
 
    //DRIZZLE_TYPE_LONGLONG
147
 
    DRIZZLE_TYPE_LONGLONG,
148
 
    //DRIZZLE_TYPE_DATETIME
149
 
    DRIZZLE_TYPE_DATETIME,
150
 
    //DRIZZLE_TYPE_DATE
151
 
    DRIZZLE_TYPE_DATE,
152
 
    //DRIZZLE_TYPE_VARCHAR
153
 
    DRIZZLE_TYPE_VARCHAR,
154
 
    //DRIZZLE_TYPE_DECIMAL
155
 
    DRIZZLE_TYPE_DECIMAL,
156
 
    //DRIZZLE_TYPE_ENUM
157
 
    DRIZZLE_TYPE_ENUM,
158
 
    //DRIZZLE_TYPE_BLOB
159
 
    DRIZZLE_TYPE_BLOB,
160
 
    //DRIZZLE_TYPE_TIME
161
 
    DRIZZLE_TYPE_TIME,
162
 
    //DRIZZLE_TYPE_BOOLEAN
163
 
    DRIZZLE_TYPE_BOOLEAN,
164
 
    //DRIZZLE_TYPE_UUID
165
 
    DRIZZLE_TYPE_UUID,
166
 
    //DRIZZLE_TYPE_MICROTIME
167
 
    DRIZZLE_TYPE_MICROTIME,
168
 
  },
169
 
  /* DRIZZLE_TYPE_TIMESTAMP -> */
170
 
  {
171
 
    //DRIZZLE_TYPE_LONG
172
 
    DRIZZLE_TYPE_VARCHAR,
173
 
    //DRIZZLE_TYPE_DOUBLE
174
 
    DRIZZLE_TYPE_VARCHAR,
175
 
    //DRIZZLE_TYPE_NULL
176
 
    DRIZZLE_TYPE_TIMESTAMP,
177
 
    //DRIZZLE_TYPE_TIMESTAMP
178
 
    DRIZZLE_TYPE_TIMESTAMP,
179
 
    //DRIZZLE_TYPE_LONGLONG
180
 
    DRIZZLE_TYPE_VARCHAR,
181
 
    //DRIZZLE_TYPE_DATETIME
182
 
    DRIZZLE_TYPE_DATETIME,
183
 
    //DRIZZLE_TYPE_DATE
184
 
    DRIZZLE_TYPE_DATE,
185
 
    //DRIZZLE_TYPE_VARCHAR
186
 
    DRIZZLE_TYPE_VARCHAR,
187
 
    //DRIZZLE_TYPE_DECIMAL
188
 
    DRIZZLE_TYPE_VARCHAR,
189
 
    //DRIZZLE_TYPE_ENUM
190
 
    DRIZZLE_TYPE_VARCHAR,
191
 
    //DRIZZLE_TYPE_BLOB
192
 
    DRIZZLE_TYPE_BLOB,
193
 
    //DRIZZLE_TYPE_TIME
194
 
    DRIZZLE_TYPE_TIME,
195
 
    //DRIZZLE_TYPE_BOOLEAN
196
 
    DRIZZLE_TYPE_VARCHAR,
197
 
    //DRIZZLE_TYPE_UUID
198
 
    DRIZZLE_TYPE_VARCHAR,
199
 
    //DRIZZLE_TYPE_MICROTIME
200
 
    DRIZZLE_TYPE_VARCHAR,
201
 
  },
202
 
  /* DRIZZLE_TYPE_LONGLONG -> */
203
 
  {
204
 
    //DRIZZLE_TYPE_LONG
205
 
    DRIZZLE_TYPE_LONGLONG,
206
 
    //DRIZZLE_TYPE_DOUBLE
207
 
    DRIZZLE_TYPE_DOUBLE,
208
 
    //DRIZZLE_TYPE_NULL
209
 
    DRIZZLE_TYPE_LONGLONG,
210
 
    //DRIZZLE_TYPE_TIMESTAMP
211
 
    DRIZZLE_TYPE_VARCHAR,
212
 
    //DRIZZLE_TYPE_LONGLONG
213
 
    DRIZZLE_TYPE_LONGLONG,
214
 
    //DRIZZLE_TYPE_DATETIME
215
 
    DRIZZLE_TYPE_VARCHAR,
216
 
    //DRIZZLE_TYPE_DATE
217
 
    DRIZZLE_TYPE_DATE,
218
 
    //DRIZZLE_TYPE_VARCHAR
219
 
    DRIZZLE_TYPE_VARCHAR,
220
 
    //DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_ENUM
221
 
    DRIZZLE_TYPE_DECIMAL,
222
 
    DRIZZLE_TYPE_VARCHAR,
223
 
    //DRIZZLE_TYPE_BLOB
224
 
    DRIZZLE_TYPE_BLOB,
225
 
    //DRIZZLE_TYPE_TIME
226
 
    DRIZZLE_TYPE_TIME,
227
 
    //DRIZZLE_TYPE_BOOLEAN
228
 
    DRIZZLE_TYPE_VARCHAR,
229
 
    //DRIZZLE_TYPE_UUID
230
 
    DRIZZLE_TYPE_VARCHAR,
231
 
    //DRIZZLE_TYPE_MICROTIME
232
 
    DRIZZLE_TYPE_VARCHAR,
233
 
  },
234
 
  /* DRIZZLE_TYPE_DATETIME -> */
235
 
  {
236
 
    //DRIZZLE_TYPE_LONG
237
 
    DRIZZLE_TYPE_VARCHAR,
238
 
    //DRIZZLE_TYPE_DOUBLE
239
 
    DRIZZLE_TYPE_VARCHAR,
240
 
    //DRIZZLE_TYPE_NULL
241
 
    DRIZZLE_TYPE_DATETIME,
242
 
    //DRIZZLE_TYPE_TIMESTAMP
243
 
    DRIZZLE_TYPE_DATETIME,
244
 
    //DRIZZLE_TYPE_LONGLONG
245
 
    DRIZZLE_TYPE_VARCHAR,
246
 
    //DRIZZLE_TYPE_DATETIME
247
 
    DRIZZLE_TYPE_DATETIME,
248
 
    //DRIZZLE_TYPE_DATE
249
 
    DRIZZLE_TYPE_DATE,
250
 
    //DRIZZLE_TYPE_VARCHAR
251
 
    DRIZZLE_TYPE_VARCHAR,
252
 
    //DRIZZLE_TYPE_DECIMAL
253
 
    DRIZZLE_TYPE_VARCHAR,
254
 
    //DRIZZLE_TYPE_ENUM
255
 
    DRIZZLE_TYPE_VARCHAR,
256
 
    //DRIZZLE_TYPE_BLOB
257
 
    DRIZZLE_TYPE_BLOB,
258
 
    //DRIZZLE_TYPE_TIME
259
 
    DRIZZLE_TYPE_TIME,
260
 
    //DRIZZLE_TYPE_BOOLEAN
261
 
    DRIZZLE_TYPE_VARCHAR,
262
 
    //DRIZZLE_TYPE_UUID
263
 
    DRIZZLE_TYPE_VARCHAR,
264
 
    //DRIZZLE_TYPE_MICROTIME
265
 
    DRIZZLE_TYPE_VARCHAR,
266
 
  },
267
 
  /* DRIZZLE_TYPE_DATE -> */
268
 
  {
269
 
    //DRIZZLE_TYPE_LONG
270
 
    DRIZZLE_TYPE_VARCHAR,
271
 
    //DRIZZLE_TYPE_DOUBLE
272
 
    DRIZZLE_TYPE_VARCHAR,
273
 
    //DRIZZLE_TYPE_NULL
274
 
    DRIZZLE_TYPE_DATE,
275
 
    //DRIZZLE_TYPE_TIMESTAMP
276
 
    DRIZZLE_TYPE_DATETIME,
277
 
    //DRIZZLE_TYPE_LONGLONG
278
 
    DRIZZLE_TYPE_VARCHAR,
279
 
    //DRIZZLE_TYPE_DATETIME
280
 
    DRIZZLE_TYPE_DATETIME,
281
 
    //DRIZZLE_TYPE_DATE
282
 
    DRIZZLE_TYPE_DATE,
283
 
    //DRIZZLE_TYPE_VARCHAR
284
 
    DRIZZLE_TYPE_VARCHAR,
285
 
    //DRIZZLE_TYPE_DECIMAL
286
 
    DRIZZLE_TYPE_VARCHAR,
287
 
    //DRIZZLE_TYPE_ENUM
288
 
    DRIZZLE_TYPE_VARCHAR,
289
 
    //DRIZZLE_TYPE_BLOB
290
 
    DRIZZLE_TYPE_BLOB,
291
 
    //DRIZZLE_TYPE_TIME
292
 
    DRIZZLE_TYPE_TIME,
293
 
    //DRIZZLE_TYPE_BOOLEAN
294
 
    DRIZZLE_TYPE_VARCHAR,
295
 
    //DRIZZLE_TYPE_UUID
296
 
    DRIZZLE_TYPE_VARCHAR,
297
 
    //DRIZZLE_TYPE_MICROTIME
298
 
    DRIZZLE_TYPE_VARCHAR,
299
 
  },
300
 
  /* DRIZZLE_TYPE_VARCHAR -> */
301
 
  {
302
 
    //DRIZZLE_TYPE_LONG
303
 
    DRIZZLE_TYPE_VARCHAR,
304
 
    //DRIZZLE_TYPE_DOUBLE
305
 
    DRIZZLE_TYPE_VARCHAR,
306
 
    //DRIZZLE_TYPE_NULL
307
 
    DRIZZLE_TYPE_VARCHAR,
308
 
    //DRIZZLE_TYPE_TIMESTAMP
309
 
    DRIZZLE_TYPE_VARCHAR,
310
 
    //DRIZZLE_TYPE_LONGLONG
311
 
    DRIZZLE_TYPE_VARCHAR,
312
 
    //DRIZZLE_TYPE_DATETIME
313
 
    DRIZZLE_TYPE_VARCHAR,
314
 
    //DRIZZLE_TYPE_DATE
315
 
    DRIZZLE_TYPE_VARCHAR,
316
 
    //DRIZZLE_TYPE_VARCHAR
317
 
    DRIZZLE_TYPE_VARCHAR,
318
 
    //DRIZZLE_TYPE_DECIMAL
319
 
    DRIZZLE_TYPE_VARCHAR,
320
 
    //DRIZZLE_TYPE_ENUM
321
 
    DRIZZLE_TYPE_VARCHAR,
322
 
    //DRIZZLE_TYPE_BLOB
323
 
    DRIZZLE_TYPE_BLOB,
324
 
    //DRIZZLE_TYPE_TIME
325
 
    DRIZZLE_TYPE_TIME,
326
 
    //DRIZZLE_TYPE_BOOLEAN
327
 
    DRIZZLE_TYPE_VARCHAR,
328
 
    //DRIZZLE_TYPE_UUID
329
 
    DRIZZLE_TYPE_VARCHAR,
330
 
    //DRIZZLE_TYPE_MICROTIME
331
 
    DRIZZLE_TYPE_VARCHAR,
332
 
  },
333
 
  /* DRIZZLE_TYPE_DECIMAL -> */
334
 
  {
335
 
    //DRIZZLE_TYPE_LONG
336
 
    DRIZZLE_TYPE_DECIMAL,
337
 
    //DRIZZLE_TYPE_DOUBLE
338
 
    DRIZZLE_TYPE_DOUBLE,
339
 
    //DRIZZLE_TYPE_NULL
340
 
    DRIZZLE_TYPE_DECIMAL,
341
 
    //DRIZZLE_TYPE_TIMESTAMP
342
 
    DRIZZLE_TYPE_VARCHAR,
343
 
    //DRIZZLE_TYPE_LONGLONG
344
 
    DRIZZLE_TYPE_DECIMAL,
345
 
    //DRIZZLE_TYPE_DATETIME
346
 
    DRIZZLE_TYPE_VARCHAR,
347
 
    //DRIZZLE_TYPE_DATE
348
 
    DRIZZLE_TYPE_VARCHAR,
349
 
    //DRIZZLE_TYPE_VARCHAR
350
 
    DRIZZLE_TYPE_VARCHAR,
351
 
    //DRIZZLE_TYPE_DECIMAL
352
 
    DRIZZLE_TYPE_DECIMAL,
353
 
    //DRIZZLE_TYPE_ENUM
354
 
    DRIZZLE_TYPE_VARCHAR,
355
 
    //DRIZZLE_TYPE_BLOB
356
 
    DRIZZLE_TYPE_BLOB,
357
 
    //DRIZZLE_TYPE_TIME
358
 
    DRIZZLE_TYPE_TIME,
359
 
    //DRIZZLE_TYPE_BOOLEAN
360
 
    DRIZZLE_TYPE_VARCHAR,
361
 
    //DRIZZLE_TYPE_UUID
362
 
    DRIZZLE_TYPE_VARCHAR,
363
 
    //DRIZZLE_TYPE_MICROTIME
364
 
    DRIZZLE_TYPE_VARCHAR,
365
 
  },
366
 
  /* DRIZZLE_TYPE_ENUM -> */
367
 
  {
368
 
    //DRIZZLE_TYPE_LONG
369
 
    DRIZZLE_TYPE_VARCHAR,
370
 
    //DRIZZLE_TYPE_DOUBLE
371
 
    DRIZZLE_TYPE_VARCHAR,
372
 
    //DRIZZLE_TYPE_NULL
373
 
    DRIZZLE_TYPE_ENUM,
374
 
    //DRIZZLE_TYPE_TIMESTAMP
375
 
    DRIZZLE_TYPE_VARCHAR,
376
 
    //DRIZZLE_TYPE_LONGLONG
377
 
    DRIZZLE_TYPE_VARCHAR,
378
 
    //DRIZZLE_TYPE_DATETIME
379
 
    DRIZZLE_TYPE_VARCHAR,
380
 
    //DRIZZLE_TYPE_DATE
381
 
    DRIZZLE_TYPE_VARCHAR,
382
 
    //DRIZZLE_TYPE_VARCHAR
383
 
    DRIZZLE_TYPE_VARCHAR,
384
 
    //DRIZZLE_TYPE_DECIMAL
385
 
    DRIZZLE_TYPE_VARCHAR,
386
 
    //DRIZZLE_TYPE_ENUM
387
 
    DRIZZLE_TYPE_VARCHAR,
388
 
    //DRIZZLE_TYPE_BLOB
389
 
    DRIZZLE_TYPE_BLOB,
390
 
    //DRIZZLE_TYPE_TIME
391
 
    DRIZZLE_TYPE_TIME,
392
 
    //DRIZZLE_TYPE_BOOLEAN
393
 
    DRIZZLE_TYPE_VARCHAR,
394
 
    //DRIZZLE_TYPE_UUID
395
 
    DRIZZLE_TYPE_VARCHAR,
396
 
    //DRIZZLE_TYPE_MICROTIME
397
 
    DRIZZLE_TYPE_VARCHAR,
398
 
   },
399
 
  /* DRIZZLE_TYPE_BLOB -> */
400
 
  {
401
 
    //DRIZZLE_TYPE_LONG
402
 
    DRIZZLE_TYPE_BLOB,
403
 
    //DRIZZLE_TYPE_DOUBLE
404
 
    DRIZZLE_TYPE_BLOB,
405
 
    //DRIZZLE_TYPE_NULL
406
 
    DRIZZLE_TYPE_BLOB,
407
 
    //DRIZZLE_TYPE_TIMESTAMP
408
 
    DRIZZLE_TYPE_BLOB,
409
 
    //DRIZZLE_TYPE_LONGLONG
410
 
    DRIZZLE_TYPE_BLOB,
411
 
    //DRIZZLE_TYPE_DATETIME
412
 
    DRIZZLE_TYPE_BLOB,
413
 
    //DRIZZLE_TYPE_DATE
414
 
    DRIZZLE_TYPE_BLOB,
415
 
    //DRIZZLE_TYPE_VARCHAR
416
 
    DRIZZLE_TYPE_BLOB,
417
 
    //DRIZZLE_TYPE_DECIMAL
418
 
    DRIZZLE_TYPE_BLOB,
419
 
    //DRIZZLE_TYPE_ENUM
420
 
    DRIZZLE_TYPE_BLOB,
421
 
    //DRIZZLE_TYPE_BLOB
422
 
    DRIZZLE_TYPE_BLOB,
423
 
    //DRIZZLE_TYPE_TIME
424
 
    DRIZZLE_TYPE_TIME,
425
 
    //DRIZZLE_TYPE_BOOLEAN
426
 
    DRIZZLE_TYPE_VARCHAR,
427
 
    //DRIZZLE_TYPE_UUID
428
 
    DRIZZLE_TYPE_VARCHAR,
429
 
    //DRIZZLE_TYPE_MICROTIME
430
 
    DRIZZLE_TYPE_VARCHAR,
431
 
  },
432
 
  /* DRIZZLE_TYPE_TIME -> */
433
 
  {
434
 
    //DRIZZLE_TYPE_LONG
435
 
    DRIZZLE_TYPE_VARCHAR,
436
 
    //DRIZZLE_TYPE_DOUBLE
437
 
    DRIZZLE_TYPE_VARCHAR,
438
 
    //DRIZZLE_TYPE_NULL
439
 
    DRIZZLE_TYPE_TIME,
440
 
    //DRIZZLE_TYPE_TIMESTAMP
441
 
    DRIZZLE_TYPE_VARCHAR,
442
 
    //DRIZZLE_TYPE_LONGLONG
443
 
    DRIZZLE_TYPE_VARCHAR,
444
 
    //DRIZZLE_TYPE_DATETIME
445
 
    DRIZZLE_TYPE_VARCHAR,
446
 
    //DRIZZLE_TYPE_DATE
447
 
    DRIZZLE_TYPE_VARCHAR,
448
 
    //DRIZZLE_TYPE_VARCHAR
449
 
    DRIZZLE_TYPE_VARCHAR,
450
 
    //DRIZZLE_TYPE_DECIMAL
451
 
    DRIZZLE_TYPE_VARCHAR,
452
 
    //DRIZZLE_TYPE_VARCHAR,
453
 
    DRIZZLE_TYPE_VARCHAR,
454
 
    //DRIZZLE_TYPE_BLOB
455
 
    DRIZZLE_TYPE_BLOB,
456
 
    //DRIZZLE_TYPE_TIME
457
 
    DRIZZLE_TYPE_TIME,
458
 
    //DRIZZLE_TYPE_BOOLEAN
459
 
    DRIZZLE_TYPE_VARCHAR,
460
 
    //DRIZZLE_TYPE_UUID
461
 
    DRIZZLE_TYPE_UUID,
462
 
    //DRIZZLE_TYPE_MICROTIME
463
 
    DRIZZLE_TYPE_VARCHAR,
464
 
  },
465
 
  /* DRIZZLE_TYPE_BOOLEAN -> */
466
 
  {
467
 
    //DRIZZLE_TYPE_LONG
468
 
    DRIZZLE_TYPE_VARCHAR,
469
 
    //DRIZZLE_TYPE_DOUBLE
470
 
    DRIZZLE_TYPE_VARCHAR,
471
 
    //DRIZZLE_TYPE_NULL
472
 
    DRIZZLE_TYPE_BOOLEAN,
473
 
    //DRIZZLE_TYPE_TIMESTAMP
474
 
    DRIZZLE_TYPE_VARCHAR,
475
 
    //DRIZZLE_TYPE_LONGLONG
476
 
    DRIZZLE_TYPE_VARCHAR,
477
 
    //DRIZZLE_TYPE_DATETIME
478
 
    DRIZZLE_TYPE_VARCHAR,
479
 
    //DRIZZLE_TYPE_DATE
480
 
    DRIZZLE_TYPE_VARCHAR,
481
 
    //DRIZZLE_TYPE_VARCHAR
482
 
    DRIZZLE_TYPE_VARCHAR,
483
 
    //DRIZZLE_TYPE_DECIMAL
484
 
    DRIZZLE_TYPE_VARCHAR,
485
 
    //DRIZZLE_TYPE_VARCHAR,
486
 
    DRIZZLE_TYPE_VARCHAR,
487
 
    //DRIZZLE_TYPE_BLOB
488
 
    DRIZZLE_TYPE_BLOB,
489
 
    //DRIZZLE_TYPE_TIME
490
 
    DRIZZLE_TYPE_TIME,
491
 
    //DRIZZLE_TYPE_BOOLEAN
492
 
    DRIZZLE_TYPE_BOOLEAN,
493
 
    //DRIZZLE_TYPE_UUID
494
 
    DRIZZLE_TYPE_VARCHAR,
495
 
    //DRIZZLE_TYPE_MICROTIME
496
 
    DRIZZLE_TYPE_VARCHAR,
497
 
  },
498
 
  /* DRIZZLE_TYPE_UUID -> */
499
 
  {
500
 
    //DRIZZLE_TYPE_LONG
501
 
    DRIZZLE_TYPE_VARCHAR,
502
 
    //DRIZZLE_TYPE_DOUBLE
503
 
    DRIZZLE_TYPE_VARCHAR,
504
 
    //DRIZZLE_TYPE_NULL
505
 
    DRIZZLE_TYPE_UUID,
506
 
    //DRIZZLE_TYPE_TIMESTAMP
507
 
    DRIZZLE_TYPE_VARCHAR,
508
 
    //DRIZZLE_TYPE_LONGLONG
509
 
    DRIZZLE_TYPE_VARCHAR,
510
 
    //DRIZZLE_TYPE_DATETIME
511
 
    DRIZZLE_TYPE_VARCHAR,
512
 
    //DRIZZLE_TYPE_DATE
513
 
    DRIZZLE_TYPE_VARCHAR,
514
 
    //DRIZZLE_TYPE_VARCHAR
515
 
    DRIZZLE_TYPE_VARCHAR,
516
 
    //DRIZZLE_TYPE_DECIMAL
517
 
    DRIZZLE_TYPE_VARCHAR,
518
 
    //DRIZZLE_TYPE_VARCHAR,
519
 
    DRIZZLE_TYPE_VARCHAR,
520
 
    //DRIZZLE_TYPE_BLOB
521
 
    DRIZZLE_TYPE_BLOB,
522
 
    //DRIZZLE_TYPE_TIME
523
 
    DRIZZLE_TYPE_TIME,
524
 
    //DRIZZLE_TYPE_BOOLEAN
525
 
    DRIZZLE_TYPE_VARCHAR,
526
 
    //DRIZZLE_TYPE_UUID
527
 
    DRIZZLE_TYPE_UUID,
528
 
    //DRIZZLE_TYPE_MICROTIME
529
 
    DRIZZLE_TYPE_VARCHAR,
530
 
  },
531
 
  /* DRIZZLE_TYPE_MICROTIME -> */
532
 
  {
533
 
    //DRIZZLE_TYPE_LONG
534
 
    DRIZZLE_TYPE_VARCHAR,
535
 
    //DRIZZLE_TYPE_DOUBLE
536
 
    DRIZZLE_TYPE_VARCHAR,
537
 
    //DRIZZLE_TYPE_NULL
538
 
    DRIZZLE_TYPE_MICROTIME,
539
 
    //DRIZZLE_TYPE_TIMESTAMP
540
 
    DRIZZLE_TYPE_VARCHAR,
541
 
    //DRIZZLE_TYPE_LONGLONG
542
 
    DRIZZLE_TYPE_VARCHAR,
543
 
    //DRIZZLE_TYPE_DATETIME
544
 
    DRIZZLE_TYPE_VARCHAR,
545
 
    //DRIZZLE_TYPE_DATE
546
 
    DRIZZLE_TYPE_VARCHAR,
547
 
    //DRIZZLE_TYPE_VARCHAR
548
 
    DRIZZLE_TYPE_VARCHAR,
549
 
    //DRIZZLE_TYPE_DECIMAL
550
 
    DRIZZLE_TYPE_VARCHAR,
551
 
    //DRIZZLE_TYPE_VARCHAR,
552
 
    DRIZZLE_TYPE_VARCHAR,
553
 
    //DRIZZLE_TYPE_BLOB
554
 
    DRIZZLE_TYPE_BLOB,
555
 
    //DRIZZLE_TYPE_TIME
556
 
    DRIZZLE_TYPE_TIME,
557
 
    //DRIZZLE_TYPE_BOOLEAN
558
 
    DRIZZLE_TYPE_VARCHAR,
559
 
    //DRIZZLE_TYPE_UUID
560
 
    DRIZZLE_TYPE_UUID,
561
 
    //DRIZZLE_TYPE_MICROTIME
562
 
    DRIZZLE_TYPE_MICROTIME,
563
 
  },
 
40
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
41
template class List<Create_field>;
 
42
template class List_iterator<Create_field>;
 
43
#endif
 
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))
 
53
 
 
54
/*
 
55
  Rules for merging different types of fields in UNION
 
56
 
 
57
  NOTE: to avoid 256*256 table, gap in table types numeration is skiped
 
58
  following #defines describe that gap and how to canculate number of fields
 
59
  and index of field in thia array.
 
60
*/
 
61
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_VARCHAR + 1)
 
62
#define FIELDTYPE_TEAR_TO   (MYSQL_TYPE_NEWDECIMAL - 1)
 
63
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
 
64
inline int field_type2index (enum_field_types field_type)
 
65
{
 
66
  return (field_type < FIELDTYPE_TEAR_FROM ?
 
67
          field_type :
 
68
          ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
 
69
}
 
70
 
 
71
 
 
72
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
 
73
{
 
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
  }
564
614
};
565
615
 
566
 
static Item_result field_types_result_type [enum_field_types_size]=
567
 
{
568
 
  //DRIZZLE_TYPE_LONG
569
 
  INT_RESULT,
570
 
  //DRIZZLE_TYPE_DOUBLE
 
616
/**
 
617
  Return type of which can carry value of both given types in UNION result.
 
618
 
 
619
  @param a  type for merging
 
620
  @param b  type for merging
 
621
 
 
622
  @return
 
623
    type of field
 
624
*/
 
625
 
 
626
enum_field_types Field::field_type_merge(enum_field_types a,
 
627
                                         enum_field_types b)
 
628
{
 
629
  assert(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
 
630
  assert(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
 
631
  return field_types_merge_rules[field_type2index(a)]
 
632
                                [field_type2index(b)];
 
633
}
 
634
 
 
635
 
 
636
static Item_result field_types_result_type [FIELDTYPE_NUM]=
 
637
{
 
638
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
 
639
  DECIMAL_RESULT,           INT_RESULT,
 
640
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
 
641
  INT_RESULT,               INT_RESULT,
 
642
  //MYSQL_TYPE_DOUBLE
571
643
  REAL_RESULT,
572
 
  //DRIZZLE_TYPE_NULL
573
 
  STRING_RESULT,
574
 
  //DRIZZLE_TYPE_TIMESTAMP
575
 
  STRING_RESULT,
576
 
  //DRIZZLE_TYPE_LONGLONG
 
644
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
 
645
  STRING_RESULT,            STRING_RESULT,
 
646
  //MYSQL_TYPE_LONGLONG
577
647
  INT_RESULT,
578
 
  //DRIZZLE_TYPE_DATETIME
579
 
  STRING_RESULT,
580
 
  //DRIZZLE_TYPE_DATE
581
 
  STRING_RESULT,
582
 
  //DRIZZLE_TYPE_VARCHAR
583
 
  STRING_RESULT,
584
 
  //DRIZZLE_TYPE_DECIMAL   
585
 
  DECIMAL_RESULT,           
586
 
  //DRIZZLE_TYPE_ENUM
587
 
  STRING_RESULT,
588
 
  //DRIZZLE_TYPE_BLOB
589
 
  STRING_RESULT,
590
 
  //DRIZZLE_TYPE_TIME
591
 
  STRING_RESULT,
592
 
  //DRIZZLE_TYPE_BOOLEAN
593
 
  STRING_RESULT,
594
 
  //DRIZZLE_TYPE_UUID
595
 
  STRING_RESULT,
596
 
  //DRIZZLE_TYPE_MICROTIME
597
 
  STRING_RESULT,
 
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
 
655
  DECIMAL_RESULT,           STRING_RESULT,
 
656
  //MYSQL_TYPE_SET
 
657
  STRING_RESULT,
 
658
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
 
659
  STRING_RESULT,            STRING_RESULT,
 
660
  //MYSQL_TYPE_STRING
 
661
  STRING_RESULT
598
662
};
599
663
 
600
 
bool test_if_important_data(const CHARSET_INFO * const cs, 
601
 
                            const char *str,
602
 
                            const char *strend)
 
664
 
 
665
/*
 
666
  Test if the given string contains important data:
 
667
  not spaces for character string,
 
668
  or any data for binary string.
 
669
 
 
670
  SYNOPSIS
 
671
    test_if_important_data()
 
672
    cs          Character set
 
673
    str         String to test
 
674
    strend      String end
 
675
 
 
676
  RETURN
 
677
    false - If string does not have important data
 
678
    true  - If string has some important data
 
679
*/
 
680
 
 
681
static bool
 
682
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
603
683
{
604
684
  if (cs != &my_charset_bin)
605
685
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
606
686
  return (str < strend);
607
687
}
608
688
 
609
 
void *Field::operator new(size_t size)
610
 
{
611
 
  return memory::sql_alloc(size);
612
 
}
613
 
 
614
 
void *Field::operator new(size_t size, memory::Root *mem_root)
615
 
{
616
 
  return mem_root->alloc_root(static_cast<uint32_t>(size));
617
 
}
618
 
 
619
 
enum_field_types Field::field_type_merge(enum_field_types a,
620
 
                                         enum_field_types b)
621
 
{
622
 
  assert(a < enum_field_types_size);
623
 
  assert(b < enum_field_types_size);
624
 
  return field_types_merge_rules[a][b];
625
 
}
 
689
 
 
690
/**
 
691
  Detect Item_result by given field type of UNION merge result.
 
692
 
 
693
  @param field_type  given field type
 
694
 
 
695
  @return
 
696
    Item_result (type of internal MySQL expression result)
 
697
*/
626
698
 
627
699
Item_result Field::result_merge_type(enum_field_types field_type)
628
700
{
629
 
  assert(field_type < enum_field_types_size);
630
 
  return field_types_result_type[field_type];
631
 
}
632
 
 
633
 
bool Field::eq(Field *field)
634
 
{
635
 
  return (ptr == field->ptr && null_ptr == field->null_ptr &&
636
 
          null_bit == field->null_bit);
637
 
}
638
 
 
639
 
uint32_t Field::pack_length() const
640
 
{
641
 
  return field_length;
642
 
}
643
 
 
644
 
uint32_t Field::pack_length_in_rec() const
645
 
{
646
 
  return pack_length();
647
 
}
648
 
 
649
 
uint32_t Field::data_length()
650
 
{
651
 
  return pack_length();
652
 
}
653
 
 
654
 
uint32_t Field::used_length()
655
 
{
656
 
  return pack_length();
657
 
}
658
 
 
659
 
uint32_t Field::sort_length() const
660
 
{
661
 
  return pack_length();
662
 
}
663
 
 
664
 
uint32_t Field::max_data_length() const
665
 
{
666
 
  return pack_length();
667
 
}
668
 
 
669
 
int Field::reset(void)
670
 
{
671
 
  memset(ptr, 0, pack_length());
672
 
  return 0;
673
 
}
674
 
 
675
 
void Field::reset_fields()
676
 
{}
677
 
 
678
 
void Field::set_default()
679
 
{
680
 
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
681
 
  memcpy(ptr, ptr + l_offset, pack_length());
682
 
  if (null_ptr)
683
 
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
684
 
 
685
 
  if (this == table->next_number_field)
686
 
    table->auto_increment_field_not_null= false;
687
 
}
688
 
 
689
 
bool Field::binary() const
690
 
{
691
 
  return true;
692
 
}
693
 
 
694
 
bool Field::zero_pack() const
695
 
{
696
 
  return true;
697
 
}
698
 
 
699
 
enum ha_base_keytype Field::key_type() const
700
 
{
701
 
  return HA_KEYTYPE_BINARY;
702
 
}
703
 
 
704
 
uint32_t Field::key_length() const
705
 
{
706
 
  return pack_length();
707
 
}
708
 
 
709
 
enum_field_types Field::real_type() const
710
 
{
711
 
  return type();
712
 
}
713
 
 
714
 
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
715
 
{
716
 
  return cmp(a, b);
717
 
}
718
 
 
719
 
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
720
 
{
721
 
  return memcmp(a,b,pack_length());
722
 
}
723
 
 
724
 
int Field::cmp_offset(uint32_t row_offset)
725
 
{
726
 
  return cmp(ptr,ptr+row_offset);
727
 
}
728
 
 
729
 
int Field::cmp_binary_offset(uint32_t row_offset)
730
 
{
731
 
  return cmp_binary(ptr, ptr+row_offset);
732
 
}
733
 
 
734
 
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
735
 
{
736
 
  return cmp(a, b);
737
 
}
738
 
 
739
 
int Field::key_cmp(const unsigned char *str, uint32_t)
740
 
{
741
 
  return cmp(ptr,str);
742
 
}
743
 
 
744
 
uint32_t Field::decimals() const
745
 
{
746
 
  return 0;
747
 
}
748
 
 
749
 
bool Field::is_null(ptrdiff_t row_offset) const
750
 
{
751
 
  return null_ptr ?
752
 
    (null_ptr[row_offset] & null_bit ? true : false) :
753
 
    table->null_row;
754
 
}
755
 
 
756
 
bool Field::is_real_null(ptrdiff_t row_offset) const
757
 
{
758
 
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
759
 
}
760
 
 
761
 
bool Field::is_null_in_record(const unsigned char *record) const
762
 
{
763
 
  if (! null_ptr)
764
 
    return false;
765
 
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
766
 
}
767
 
 
768
 
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
769
 
{
770
 
  if (! null_ptr)
771
 
    return false;
772
 
  return test(null_ptr[with_offset] & null_bit);
773
 
}
774
 
 
775
 
void Field::set_null(ptrdiff_t row_offset)
776
 
{
777
 
  if (null_ptr)
778
 
    null_ptr[row_offset]|= null_bit;
779
 
}
780
 
 
781
 
void Field::set_notnull(ptrdiff_t row_offset)
782
 
{
783
 
  if (null_ptr)
784
 
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
785
 
}
786
 
 
787
 
bool Field::maybe_null(void) const
788
 
{
789
 
  return null_ptr != 0 || table->maybe_null;
790
 
}
791
 
 
792
 
bool Field::real_maybe_null(void) const
793
 
{
794
 
  return null_ptr != 0;
795
 
}
 
701
  assert(field_type < FIELDTYPE_TEAR_FROM || field_type
 
702
              > FIELDTYPE_TEAR_TO);
 
703
  return field_types_result_type[field_type2index(field_type)];
 
704
}
 
705
 
 
706
/*****************************************************************************
 
707
  Static help functions
 
708
*****************************************************************************/
 
709
 
 
710
 
 
711
/**
 
712
  Check whether a field type can be partially indexed by a key.
 
713
 
 
714
  This is a static method, rather than a virtual function, because we need
 
715
  to check the type of a non-Field in mysql_alter_table().
 
716
 
 
717
  @param type  field type
 
718
 
 
719
  @retval
 
720
    true  Type can have a prefixed key
 
721
  @retval
 
722
    false Type can not have a prefixed key
 
723
*/
796
724
 
797
725
bool Field::type_can_have_key_part(enum enum_field_types type)
798
726
{
799
727
  switch (type) {
800
 
  case DRIZZLE_TYPE_VARCHAR:
801
 
  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:
802
732
    return true;
803
733
  default:
804
734
    return false;
805
735
  }
806
736
}
807
737
 
 
738
 
 
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
/**
 
885
  Process decimal library return codes and issue warnings for overflow and
 
886
  truncation.
 
887
 
 
888
  @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
 
889
 
 
890
  @retval
 
891
    1  there was overflow
 
892
  @retval
 
893
    0  no error or some other errors except overflow
 
894
*/
 
895
 
808
896
int Field::warn_if_overflow(int op_result)
809
897
{
810
898
  if (op_result == E_DEC_OVERFLOW)
811
899
  {
812
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
813
 
    return E_DEC_OVERFLOW;
 
900
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
901
    return 1;
814
902
  }
815
903
  if (op_result == E_DEC_TRUNCATED)
816
904
  {
817
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
818
 
    return E_DEC_TRUNCATED;
 
905
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
 
906
    /* We return 0 here as this is not a critical issue */
819
907
  }
820
908
  return 0;
821
909
}
822
910
 
823
 
void Field::init(Table *table_arg)
824
 
{
825
 
  orig_table= table= table_arg;
826
 
}
 
911
 
 
912
#ifdef NOT_USED
 
913
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
 
914
{
 
915
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
 
916
 
 
917
  while (length && my_isspace(cs,*str))
 
918
  {                                             // Allow start space
 
919
    length--; str++;
 
920
  }
 
921
  if (!length)
 
922
    return 0;
 
923
  if (*str == '+' || *str == '-')
 
924
  {
 
925
    length--; str++;
 
926
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
 
927
      return 0;
 
928
  }
 
929
  while (length && my_isdigit(cs,*str))
 
930
  {
 
931
    length--; str++;
 
932
  }
 
933
  if (!length)
 
934
    return 1;
 
935
  if (*str == '.')
 
936
  {
 
937
    length--; str++;
 
938
    while (length && my_isdigit(cs,*str))
 
939
    {
 
940
      length--; str++;
 
941
    }
 
942
  }
 
943
  if (!length)
 
944
    return 1;
 
945
  if (*str == 'E' || *str == 'e')
 
946
  {
 
947
    if (length < 3 || (str[1] != '+' && str[1] != '-') || 
 
948
        !my_isdigit(cs,str[2]))
 
949
      return 0;
 
950
    length-=3;
 
951
    str+=3;
 
952
    while (length && my_isdigit(cs,*str))
 
953
    {
 
954
      length--; str++;
 
955
    }
 
956
  }
 
957
  for (; length ; length--, str++)
 
958
  {                                             // Allow end space
 
959
    if (!my_isspace(cs,*str))
 
960
      return 0;
 
961
  }
 
962
  return 1;
 
963
}
 
964
#endif
 
965
 
 
966
 
 
967
/**
 
968
  Interpret field value as an integer but return the result as a string.
 
969
 
 
970
  This is used for printing bit_fields as numbers while debugging.
 
971
*/
 
972
 
 
973
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
 
974
{
 
975
  CHARSET_INFO *cs= &my_charset_bin;
 
976
  uint length;
 
977
  int64_t value= val_int();
 
978
 
 
979
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
 
980
    return 0;
 
981
  length= (uint) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
 
982
                                                MY_INT64_NUM_DECIMAL_DIGITS,
 
983
                                                unsigned_val ? 10 : -10,
 
984
                                                value);
 
985
  val_buffer->length(length);
 
986
  return val_buffer;
 
987
}
 
988
 
827
989
 
828
990
/// This is used as a table name when the table structure is not set up
829
 
Field::Field(unsigned char *ptr_arg,
830
 
             uint32_t length_arg,
831
 
             unsigned char *null_ptr_arg,
832
 
             unsigned char null_bit_arg,
833
 
             utype unireg_check_arg, 
834
 
             const char *field_name_arg) :
835
 
    ptr(ptr_arg),
836
 
    null_ptr(null_ptr_arg),
837
 
    table(NULL),
838
 
    orig_table(NULL),
839
 
    field_name(field_name_arg),
840
 
    comment(NULL_LEX_STRING),
841
 
    key_start(0),
842
 
    part_of_key(0),
843
 
    part_of_key_not_clustered(0),
844
 
    part_of_sortkey(0),
845
 
    unireg_check(unireg_check_arg),
846
 
    field_length(length_arg),
847
 
    flags(null_ptr ? 0: NOT_NULL_FLAG),
848
 
    field_index(0),
849
 
    null_bit(null_bit_arg),
850
 
    is_created_from_null_item(false)
 
991
Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
 
992
             uchar null_bit_arg,
 
993
             utype unireg_check_arg, const char *field_name_arg)
 
994
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
 
995
   table(0), orig_table(0), table_name(0),
 
996
   field_name(field_name_arg),
 
997
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
 
998
   part_of_sortkey(0), unireg_check(unireg_check_arg),
 
999
   field_length(length_arg), null_bit(null_bit_arg), 
 
1000
   is_created_from_null_item(false)
851
1001
{
 
1002
  flags=null_ptr ? 0: NOT_NULL_FLAG;
 
1003
  comment.str= (char*) "";
 
1004
  comment.length=0;
 
1005
  field_index= 0;
852
1006
}
853
1007
 
854
 
void Field::hash(uint32_t *nr, uint32_t *nr2) const
 
1008
 
 
1009
void Field::hash(ulong *nr, ulong *nr2)
855
1010
{
856
1011
  if (is_null())
857
1012
  {
859
1014
  }
860
1015
  else
861
1016
  {
862
 
    uint32_t len= pack_length();
863
 
    const CHARSET_INFO * const cs= charset();
 
1017
    uint len= pack_length();
 
1018
    CHARSET_INFO *cs= charset();
864
1019
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
865
1020
  }
866
1021
}
867
1022
 
 
1023
size_t
 
1024
Field::do_last_null_byte() const
 
1025
{
 
1026
  assert(null_ptr == NULL || null_ptr >= table->record[0]);
 
1027
  if (null_ptr)
 
1028
    return (size_t) (null_ptr - table->record[0]) + 1;
 
1029
  return LAST_NULL_BYTE_UNDEF;
 
1030
}
 
1031
 
 
1032
 
868
1033
void Field::copy_from_tmp(int row_offset)
869
1034
{
870
1035
  memcpy(ptr,ptr+row_offset,pack_length());
871
1036
  if (null_ptr)
872
1037
  {
873
 
    *null_ptr= (unsigned char) ((null_ptr[0] &
874
 
                                 (unsigned char) ~(uint32_t) null_bit) |
875
 
                                (null_ptr[row_offset] &
876
 
                                 (unsigned char) null_bit));
 
1038
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | (null_ptr[row_offset] & (uchar) null_bit));
877
1039
  }
878
1040
}
879
1041
 
880
 
int Field::store_and_check(enum_check_fields check_level,
881
 
                           const char *to, 
882
 
                           uint32_t length,
883
 
                           const CHARSET_INFO * const cs)
884
 
 
 
1042
 
 
1043
bool Field::send_binary(Protocol *protocol)
 
1044
{
 
1045
  char buff[MAX_FIELD_WIDTH];
 
1046
  String tmp(buff,sizeof(buff),charset());
 
1047
  val_str(&tmp);
 
1048
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
 
1049
}
 
1050
 
 
1051
 
 
1052
/**
 
1053
   Check to see if field size is compatible with destination.
 
1054
 
 
1055
   This method is used in row-based replication to verify that the slave's
 
1056
   field size is less than or equal to the master's field size. The 
 
1057
   encoded field metadata (from the master or source) is decoded and compared
 
1058
   to the size of this field (the slave or destination). 
 
1059
 
 
1060
   @param   field_metadata   Encoded size in field metadata
 
1061
 
 
1062
   @retval 0 if this field's size is < the source field's size
 
1063
   @retval 1 if this field's size is >= the source field's size
 
1064
*/
 
1065
int Field::compatible_field_size(uint field_metadata)
 
1066
{
 
1067
  uint const source_size= pack_length_from_metadata(field_metadata);
 
1068
  uint const destination_size= row_pack_length();
 
1069
  return (source_size <= destination_size);
 
1070
}
 
1071
 
 
1072
 
 
1073
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
 
1074
                 enum_check_fields check_level)
885
1075
{
886
1076
  int res;
887
1077
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
891
1081
  return res;
892
1082
}
893
1083
 
894
 
unsigned char *Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length, bool)
 
1084
 
 
1085
/**
 
1086
   Pack the field into a format suitable for storage and transfer.
 
1087
 
 
1088
   To implement packing functionality, only the virtual function
 
1089
   should be overridden. The other functions are just convenience
 
1090
   functions and hence should not be overridden.
 
1091
 
 
1092
   The value of <code>low_byte_first</code> is dependent on how the
 
1093
   packed data is going to be used: for local use, e.g., temporary
 
1094
   store on disk or in memory, use the native format since that is
 
1095
   faster. For data that is going to be transfered to other machines
 
1096
   (e.g., when writing data to the binary log), data should always be
 
1097
   stored in little-endian format.
 
1098
 
 
1099
   @note The default method for packing fields just copy the raw bytes
 
1100
   of the record into the destination, but never more than
 
1101
   <code>max_length</code> characters.
 
1102
 
 
1103
   @param to
 
1104
   Pointer to memory area where representation of field should be put.
 
1105
 
 
1106
   @param from
 
1107
   Pointer to memory area where record representation of field is
 
1108
   stored.
 
1109
 
 
1110
   @param max_length
 
1111
   Maximum length of the field, as given in the column definition. For
 
1112
   example, for <code>CHAR(1000)</code>, the <code>max_length</code>
 
1113
   is 1000. This information is sometimes needed to decide how to pack
 
1114
   the data.
 
1115
 
 
1116
   @param low_byte_first
 
1117
   @c true if integers should be stored little-endian, @c false if
 
1118
   native format should be used. Note that for little-endian machines,
 
1119
   the value of this flag is a moot point since the native format is
 
1120
   little-endian.
 
1121
*/
 
1122
uchar *
 
1123
Field::pack(uchar *to, const uchar *from, uint max_length,
 
1124
            bool low_byte_first __attribute__((unused)))
895
1125
{
896
 
  uint32_t length= pack_length();
 
1126
  uint32 length= pack_length();
897
1127
  set_if_smaller(length, max_length);
898
1128
  memcpy(to, from, length);
899
1129
  return to+length;
900
1130
}
901
1131
 
902
 
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
903
 
{
904
 
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
905
 
  return(result);
906
 
}
907
 
 
908
 
const unsigned char *Field::unpack(unsigned char* to,
909
 
                                   const unsigned char *from, 
910
 
                                   uint32_t param_data,
911
 
                                   bool)
912
 
{
913
 
  uint32_t length=pack_length();
 
1132
/**
 
1133
   Unpack a field from row data.
 
1134
 
 
1135
   This method is used to unpack a field from a master whose size of
 
1136
   the field is less than that of the slave.
 
1137
 
 
1138
   The <code>param_data</code> parameter is a two-byte integer (stored
 
1139
   in the least significant 16 bits of the unsigned integer) usually
 
1140
   consisting of two parts: the real type in the most significant byte
 
1141
   and a original pack length in the least significant byte.
 
1142
 
 
1143
   The exact layout of the <code>param_data</code> field is given by
 
1144
   the <code>Table_map_log_event::save_field_metadata()</code>.
 
1145
 
 
1146
   This is the default method for unpacking a field. It just copies
 
1147
   the memory block in byte order (of original pack length bytes or
 
1148
   length of field, whichever is smaller).
 
1149
 
 
1150
   @param   to         Destination of the data
 
1151
   @param   from       Source of the data
 
1152
   @param   param_data Real type and original pack length of the field
 
1153
                       data
 
1154
 
 
1155
   @param low_byte_first
 
1156
   If this flag is @c true, all composite entities (e.g., lengths)
 
1157
   should be unpacked in little-endian format; otherwise, the entities
 
1158
   are unpacked in native order.
 
1159
 
 
1160
   @return  New pointer into memory based on from + length of the data
 
1161
*/
 
1162
const uchar *
 
1163
Field::unpack(uchar* to, const uchar *from, uint param_data,
 
1164
              bool low_byte_first __attribute__((unused)))
 
1165
{
 
1166
  uint length=pack_length();
914
1167
  int from_type= 0;
915
1168
  /*
916
1169
    If from length is > 255, it has encoded data in the upper bits. Need
930
1183
    return from+length;
931
1184
  }
932
1185
 
933
 
  uint32_t len= (param_data && (param_data < length)) ?
 
1186
  uint len= (param_data && (param_data < length)) ?
934
1187
            param_data : length;
935
1188
 
936
1189
  memcpy(to, from, param_data > length ? length : len);
937
 
  return (from + len);
938
 
}
939
 
 
940
 
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
941
 
{
942
 
  const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
943
 
  return(result);
944
 
}
945
 
 
946
 
type::Decimal *Field::val_decimal(type::Decimal *) const
 
1190
  return from+len;
 
1191
}
 
1192
 
 
1193
 
 
1194
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((__unused__)))
947
1195
{
948
1196
  /* This never have to be called */
949
1197
  assert(0);
951
1199
}
952
1200
 
953
1201
 
954
 
void Field::make_field(SendField *field)
955
 
{
956
 
  if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
 
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
 
 
1211
void Field::make_field(Send_field *field)
 
1212
{
 
1213
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
957
1214
  {
958
 
    field->db_name= orig_table->getShare()->getSchemaName();
959
 
    field->org_table_name= orig_table->getShare()->getTableName();
 
1215
    field->db_name= orig_table->s->db.str;
 
1216
    field->org_table_name= orig_table->s->table_name.str;
960
1217
  }
961
1218
  else
962
1219
    field->org_table_name= field->db_name= "";
963
1220
  if (orig_table)
964
1221
  {
965
 
    field->table_name= orig_table->getAlias();
 
1222
    field->table_name= orig_table->alias;
966
1223
    field->org_col_name= field_name;
967
1224
  }
968
1225
  else
972
1229
  }
973
1230
  field->col_name= field_name;
974
1231
  field->charsetnr= charset()->number;
975
 
  field->length= field_length;
976
 
  field->type= type();
977
 
  field->flags= table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
 
1232
  field->length=field_length;
 
1233
  field->type=type();
 
1234
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
978
1235
  field->decimals= 0;
979
1236
}
980
1237
 
981
 
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
 
1238
 
 
1239
/**
 
1240
  Conversion from decimal to int64_t with checking overflow and
 
1241
  setting correct value (min/max) in case of overflow.
 
1242
 
 
1243
  @param val             value which have to be converted
 
1244
  @param unsigned_flag   type of integer in which we convert val
 
1245
  @param err             variable to pass error code
 
1246
 
 
1247
  @return
 
1248
    value converted from val
 
1249
*/
 
1250
int64_t Field::convert_decimal2int64_t(const my_decimal *val,
 
1251
                                         bool unsigned_flag, int *err)
982
1252
{
983
1253
  int64_t i;
984
 
  if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
985
 
                                      ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
986
 
                                      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)))
987
1273
  {
988
1274
    i= (val->sign() ? INT64_MIN : INT64_MAX);
989
1275
    *err= 1;
991
1277
  return i;
992
1278
}
993
1279
 
994
 
uint32_t Field::fill_cache_field(CacheField *copy)
995
 
{
996
 
  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;
997
1386
  copy->str=ptr;
998
1387
  copy->length=pack_length();
999
1388
  copy->blob_field=0;
1001
1390
  {
1002
1391
    copy->blob_field=(Field_blob*) this;
1003
1392
    copy->strip=0;
1004
 
    copy->length-= table->getShare()->sizeBlobPtr();
 
1393
    copy->length-= table->s->blob_ptr_size;
1005
1394
    return copy->length;
1006
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
  }
1007
1403
  else
1008
1404
  {
1009
1405
    copy->strip=0;
1012
1408
  return copy->length+ store_length;
1013
1409
}
1014
1410
 
1015
 
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate) const
1016
 
{
1017
 
  char buff[type::Time::MAX_STRING_LENGTH];
1018
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1019
 
 
1020
 
  assert(getTable() and getTable()->getSession());
1021
 
 
1022
 
  if (not (res= val_str_internal(&tmp)) or
1023
 
      str_to_datetime_with_warn(getTable()->getSession(),
1024
 
                                res->ptr(), res->length(),
1025
 
                                &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1026
 
  {
1027
 
    return true;
1028
 
  }
1029
 
 
1030
 
  return false;
1031
 
}
1032
 
 
1033
 
bool Field::get_time(type::Time &ltime) const
1034
 
{
1035
 
  char buff[type::Time::MAX_STRING_LENGTH];
1036
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1037
 
 
1038
 
  if (not (res= val_str_internal(&tmp)) or
1039
 
      str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime))
1040
 
  {
1041
 
    return true;
1042
 
  }
1043
 
 
1044
 
  return false;
1045
 
}
1046
 
 
1047
 
int Field::store_time(type::Time &ltime, type::timestamp_t)
1048
 
{
1049
 
  String tmp;
1050
 
 
1051
 
  ltime.convert(tmp);
1052
 
 
1053
 
  return store(tmp.ptr(), tmp.length(), &my_charset_bin);
1054
 
}
1055
 
 
1056
 
bool Field::optimize_range(uint32_t idx, uint32_t)
1057
 
{
1058
 
  return test(table->index_flags(idx) & HA_READ_RANGE);
1059
 
}
1060
 
 
1061
 
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
 
1411
 
 
1412
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
 
1413
{
 
1414
  char buff[40];
 
1415
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1416
  if (!(res=val_str(&tmp)) ||
 
1417
      str_to_datetime_with_warn(res->ptr(), res->length(),
 
1418
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
 
1419
    return 1;
 
1420
  return 0;
 
1421
}
 
1422
 
 
1423
bool Field::get_time(MYSQL_TIME *ltime)
 
1424
{
 
1425
  char buff[40];
 
1426
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1427
  if (!(res=val_str(&tmp)) ||
 
1428
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
1429
    return 1;
 
1430
  return 0;
 
1431
}
 
1432
 
 
1433
/**
 
1434
  This is called when storing a date in a string.
 
1435
 
 
1436
  @note
 
1437
    Needs to be changed if/when we want to support different time formats.
 
1438
*/
 
1439
 
 
1440
int Field::store_time(MYSQL_TIME *ltime,
 
1441
                      timestamp_type type_arg __attribute__((__unused__)))
 
1442
{
 
1443
  char buff[MAX_DATE_STRING_REP_LENGTH];
 
1444
  uint length= (uint) my_TIME_to_str(ltime, buff);
 
1445
  return store(buff, length, &my_charset_bin);
 
1446
}
 
1447
 
 
1448
 
 
1449
bool Field::optimize_range(uint idx, uint part)
 
1450
{
 
1451
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
 
1452
}
 
1453
 
 
1454
 
 
1455
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
 
1456
                        bool keep_type __attribute__((unused)))
1062
1457
{
1063
1458
  Field *tmp;
1064
 
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
1459
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1065
1460
    return 0;
1066
1461
 
1067
1462
  if (tmp->table->maybe_null)
1068
1463
    tmp->flags&= ~NOT_NULL_FLAG;
1069
1464
  tmp->table= new_table;
1070
 
  tmp->key_start.reset();
1071
 
  tmp->part_of_key.reset();
1072
 
  tmp->part_of_sortkey.reset();
 
1465
  tmp->key_start.init(0);
 
1466
  tmp->part_of_key.init(0);
 
1467
  tmp->part_of_sortkey.init(0);
1073
1468
  tmp->unireg_check= Field::NONE;
1074
 
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG);
 
1469
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
 
1470
                ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1075
1471
  tmp->reset_fields();
1076
1472
  return tmp;
1077
1473
}
1078
1474
 
1079
 
Field *Field::new_key_field(memory::Root *root, Table *new_table,
1080
 
                            unsigned char *new_ptr,
1081
 
                            unsigned char *new_null_ptr,
1082
 
                            uint32_t new_null_bit)
 
1475
 
 
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)
1083
1479
{
1084
1480
  Field *tmp;
1085
1481
  if ((tmp= new_field(root, new_table, table == new_table)))
1086
1482
  {
1087
 
    tmp->ptr= new_ptr;
 
1483
    tmp->ptr=      new_ptr;
1088
1484
    tmp->null_ptr= new_null_ptr;
1089
1485
    tmp->null_bit= new_null_bit;
1090
1486
  }
1091
1487
  return tmp;
1092
1488
}
1093
1489
 
1094
 
Field *Field::clone(memory::Root *root, Table *new_table)
 
1490
 
 
1491
/* This is used to generate a field in TABLE from TABLE_SHARE */
 
1492
 
 
1493
Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
1095
1494
{
1096
1495
  Field *tmp;
1097
 
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
1496
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1098
1497
  {
1099
1498
    tmp->init(new_table);
1100
 
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1101
 
                                           new_table->getDefaultValues()));
 
1499
    tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
 
1500
                                           new_table->s->default_values));
1102
1501
  }
1103
1502
  return tmp;
1104
1503
}
1105
1504
 
1106
1505
 
1107
 
uint32_t Field::is_equal(CreateField *new_field_ptr)
1108
 
{
1109
 
  return (new_field_ptr->sql_type == real_type());
1110
 
}
 
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
/****************************************************************************
 
1937
** tiny int
 
1938
****************************************************************************/
 
1939
 
 
1940
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
 
1941
{
 
1942
  int error;
 
1943
  int64_t rnd;
 
1944
  
 
1945
  error= get_int(cs, from, len, &rnd, 255, -128, 127);
 
1946
  ptr[0]= unsigned_flag ? (char) (uint64_t) rnd : (char) rnd;
 
1947
  return error;
 
1948
}
 
1949
 
 
1950
 
 
1951
int Field_tiny::store(double nr)
 
1952
{
 
1953
  int error= 0;
 
1954
  nr=rint(nr);
 
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
 
1973
  {
 
1974
    if (nr < -128.0)
 
1975
    {
 
1976
      *ptr= (char) -128;
 
1977
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1978
      error= 1;
 
1979
    }
 
1980
    else if (nr > 127.0)
 
1981
    {
 
1982
      *ptr=127;
 
1983
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
1984
      error= 1;
 
1985
    }
 
1986
    else
 
1987
      *ptr=(char) (int) nr;
 
1988
  }
 
1989
  return error;
 
1990
}
 
1991
 
 
1992
 
 
1993
int Field_tiny::store(int64_t nr, bool unsigned_val)
 
1994
{
 
1995
  int error= 0;
 
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
 
2015
  {
 
2016
    if (nr < 0 && unsigned_val)
 
2017
      nr= 256;                                    // Generate overflow
 
2018
    if (nr < -128)
 
2019
    {
 
2020
      *ptr= (char) -128;
 
2021
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2022
      error= 1;
 
2023
    }
 
2024
    else if (nr > 127)
 
2025
    {
 
2026
      *ptr=127;
 
2027
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
2028
      error= 1;
 
2029
    }
 
2030
    else
 
2031
      *ptr=(char) nr;
 
2032
  }
 
2033
  return error;
 
2034
}
 
2035
 
 
2036
 
 
2037
double Field_tiny::val_real(void)
 
2038
{
 
2039
  int tmp= unsigned_flag ? (int) ptr[0] :
 
2040
    (int) ((signed char*) ptr)[0];
 
2041
  return (double) tmp;
 
2042
}
 
2043
 
 
2044
 
 
2045
int64_t Field_tiny::val_int(void)
 
2046
{
 
2047
  int tmp= unsigned_flag ? (int) ptr[0] :
 
2048
    (int) ((signed char*) ptr)[0];
 
2049
  return (int64_t) tmp;
 
2050
}
 
2051
 
 
2052
 
 
2053
String *Field_tiny::val_str(String *val_buffer,
 
2054
                            String *val_ptr __attribute__((unused)))
 
2055
{
 
2056
  CHARSET_INFO *cs= &my_charset_bin;
 
2057
  uint length;
 
2058
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
 
2059
  val_buffer->alloc(mlength);
 
2060
  char *to=(char*) val_buffer->ptr();
 
2061
 
 
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
  
 
2069
  val_buffer->length(length);
 
2070
  if (zerofill)
 
2071
    prepend_zeros(val_buffer);
 
2072
  return val_buffer;
 
2073
}
 
2074
 
 
2075
bool Field_tiny::send_binary(Protocol *protocol)
 
2076
{
 
2077
  return protocol->store_tiny((int64_t) (int8) ptr[0]);
 
2078
}
 
2079
 
 
2080
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
2081
{
 
2082
  signed char a,b;
 
2083
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
 
2084
  if (unsigned_flag)
 
2085
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
 
2086
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
2087
}
 
2088
 
 
2089
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
 
2090
{
 
2091
  if (unsigned_flag)
 
2092
    *to= *ptr;
 
2093
  else
 
2094
    to[0] = (char) (ptr[0] ^ (uchar) 128);      /* Revers signbit */
 
2095
}
 
2096
 
 
2097
void Field_tiny::sql_type(String &res) const
 
2098
{
 
2099
  CHARSET_INFO *cs=res.charset();
 
2100
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
 
2101
                          "tinyint(%d)",(int) field_length));
 
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
****************************************************************************/
 
4481
 
 
4482
/*
 
4483
  Report "not well formed" or "cannot convert" error
 
4484
  after storing a character string info a field.
 
4485
 
 
4486
  SYNOPSIS
 
4487
    check_string_copy_error()
 
4488
    field                    - Field
 
4489
    well_formed_error_pos    - where not well formed data was first met
 
4490
    cannot_convert_error_pos - where a not-convertable character was first met
 
4491
    end                      - end of the string
 
4492
    cs                       - character set of the string
 
4493
 
 
4494
  NOTES
 
4495
    As of version 5.0 both cases return the same error:
 
4496
  
 
4497
      "Invalid string value: 'xxx' for column 't' at row 1"
 
4498
  
 
4499
  Future versions will possibly introduce a new error message:
 
4500
 
 
4501
      "Cannot convert character string: 'xxx' for column 't' at row 1"
 
4502
 
 
4503
  RETURN
 
4504
    false - If errors didn't happen
 
4505
    true  - If an error happened
 
4506
*/
 
4507
 
 
4508
static bool
 
4509
check_string_copy_error(Field_str *field,
 
4510
                        const char *well_formed_error_pos,
 
4511
                        const char *cannot_convert_error_pos,
 
4512
                        const char *end,
 
4513
                        CHARSET_INFO *cs)
 
4514
{
 
4515
  const char *pos, *end_orig;
 
4516
  char tmp[64], *t;
 
4517
  
 
4518
  if (!(pos= well_formed_error_pos) &&
 
4519
      !(pos= cannot_convert_error_pos))
 
4520
    return false;
 
4521
 
 
4522
  end_orig= end;
 
4523
  set_if_smaller(end, pos + 6);
 
4524
 
 
4525
  for (t= tmp; pos < end; pos++)
 
4526
  {
 
4527
    /*
 
4528
      If the source string is ASCII compatible (mbminlen==1)
 
4529
      and the source character is in ASCII printable range (0x20..0x7F),
 
4530
      then display the character as is.
 
4531
      
 
4532
      Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
 
4533
      or the source character is not in the printable range,
 
4534
      then print the character using HEX notation.
 
4535
    */
 
4536
    if (((unsigned char) *pos) >= 0x20 &&
 
4537
        ((unsigned char) *pos) <= 0x7F &&
 
4538
        cs->mbminlen == 1)
 
4539
    {
 
4540
      *t++= *pos;
 
4541
    }
 
4542
    else
 
4543
    {
 
4544
      *t++= '\\';
 
4545
      *t++= 'x';
 
4546
      *t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
 
4547
      *t++= _dig_vec_upper[((unsigned char) *pos) & 15];
 
4548
    }
 
4549
  }
 
4550
  if (end_orig > end)
 
4551
  {
 
4552
    *t++= '.';
 
4553
    *t++= '.';
 
4554
    *t++= '.';
 
4555
  }
 
4556
  *t= '\0';
 
4557
  push_warning_printf(field->table->in_use, 
 
4558
                      field->table->in_use->abort_on_warning ?
 
4559
                      MYSQL_ERROR::WARN_LEVEL_ERROR :
 
4560
                      MYSQL_ERROR::WARN_LEVEL_WARN,
 
4561
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
 
4562
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
4563
                      "string", tmp, field->field_name,
 
4564
                      (ulong) field->table->in_use->row_count);
 
4565
  return true;
 
4566
}
 
4567
 
 
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)
 
4669
{
 
4670
  return (new_field->sql_type == real_type());
 
4671
}
 
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
 
 
6719
/**
 
6720
  @retval
 
6721
    1  if the fields are equally defined
 
6722
  @retval
 
6723
    0  if the fields are unequally defined
 
6724
*/
1111
6725
 
1112
6726
bool Field::eq_def(Field *field)
1113
6727
{
1117
6731
  return 1;
1118
6732
}
1119
6733
 
 
6734
/**
 
6735
  @return
 
6736
  returns 1 if the fields are equally defined
 
6737
*/
1120
6738
bool Field_enum::eq_def(Field *field)
1121
6739
{
1122
6740
  if (!Field::eq_def(field))
1123
6741
    return 0;
1124
 
 
1125
6742
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
1126
6743
 
1127
6744
  if (typelib->count < from_lib->count)
1128
6745
    return 0;
1129
 
 
1130
 
  for (uint32_t i=0 ; i < from_lib->count ; i++)
1131
 
  {
 
6746
  for (uint i=0 ; i < from_lib->count ; i++)
1132
6747
    if (my_strnncoll(field_charset,
1133
 
                     (const unsigned char*)typelib->type_names[i],
 
6748
                     (const uchar*)typelib->type_names[i],
1134
6749
                     strlen(typelib->type_names[i]),
1135
 
                     (const unsigned char*)from_lib->type_names[i],
 
6750
                     (const uchar*)from_lib->type_names[i],
1136
6751
                     strlen(from_lib->type_names[i])))
1137
6752
      return 0;
1138
 
  }
1139
 
 
1140
 
  return 1;
1141
 
}
1142
 
 
1143
 
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1144
 
{
1145
 
  switch (type) {
1146
 
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1147
 
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1148
 
  case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1149
 
  case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1150
 
  case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1151
 
  case DRIZZLE_TYPE_DATE:
1152
 
  case DRIZZLE_TYPE_ENUM:
1153
 
  case DRIZZLE_TYPE_LONG: return 4;
1154
 
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1155
 
  case DRIZZLE_TYPE_TIME:
1156
 
  case DRIZZLE_TYPE_DATETIME:
1157
 
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1158
 
  case DRIZZLE_TYPE_NULL: return 0;
1159
 
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1160
 
  case DRIZZLE_TYPE_DECIMAL:
1161
 
                          break;
1162
 
  }
1163
 
 
1164
 
  assert(0);
1165
 
  abort();
1166
 
}
1167
 
 
1168
 
uint32_t pack_length_to_packflag(uint32_t type)
1169
 
{
1170
 
  switch (type) {
1171
 
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1172
 
    case 2: assert(1);
 
6753
  return 1;
 
6754
}
 
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
 
 
6785
/*****************************************************************************
 
6786
  Handling of field and Create_field
 
6787
*****************************************************************************/
 
6788
 
 
6789
/**
 
6790
  Convert create_field::length from number of characters to number of bytes.
 
6791
*/
 
6792
 
 
6793
void Create_field::create_length_to_internal_length(void)
 
6794
{
 
6795
  switch (sql_type) {
 
6796
  case MYSQL_TYPE_BLOB:
 
6797
  case MYSQL_TYPE_VAR_STRING:
 
6798
  case MYSQL_TYPE_STRING:
 
6799
  case MYSQL_TYPE_VARCHAR:
 
6800
    length*= charset->mbmaxlen;
 
6801
    key_length= length;
 
6802
    pack_length= calc_pack_length(sql_type, length);
 
6803
    break;
 
6804
  case MYSQL_TYPE_ENUM:
 
6805
  case MYSQL_TYPE_SET:
 
6806
    /* Pack_length already calculated in sql_parse.cc */
 
6807
    length*= charset->mbmaxlen;
 
6808
    key_length= pack_length;
 
6809
    break;
 
6810
  case MYSQL_TYPE_NEWDECIMAL:
 
6811
    key_length= pack_length=
 
6812
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
 
6813
                                                                decimals,
 
6814
                                                                flags &
 
6815
                                                                UNSIGNED_FLAG),
 
6816
                                 decimals);
 
6817
    break;
 
6818
  default:
 
6819
    key_length= pack_length= calc_pack_length(sql_type, length);
 
6820
    break;
 
6821
  }
 
6822
}
 
6823
 
 
6824
 
 
6825
/**
 
6826
  Init for a tmp table field. To be extended if need be.
 
6827
*/
 
6828
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
 
6829
                                      uint32 length_arg, uint32 decimals_arg,
 
6830
                                      bool maybe_null, bool is_unsigned)
 
6831
{
 
6832
  field_name= "";
 
6833
  sql_type= sql_type_arg;
 
6834
  char_length= length= length_arg;;
 
6835
  unireg_check= Field::NONE;
 
6836
  interval= 0;
 
6837
  charset= &my_charset_bin;
 
6838
  pack_flag= (FIELDFLAG_NUMBER |
 
6839
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
 
6840
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
 
6841
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
 
6842
}
 
6843
 
 
6844
 
 
6845
/**
 
6846
  Initialize field definition for create.
 
6847
 
 
6848
  @param thd                   Thread handle
 
6849
  @param fld_name              Field name
 
6850
  @param fld_type              Field type
 
6851
  @param fld_length            Field length
 
6852
  @param fld_decimals          Decimal (if any)
 
6853
  @param fld_type_modifier     Additional type information
 
6854
  @param fld_default_value     Field default value (if any)
 
6855
  @param fld_on_update_value   The value of ON UPDATE clause
 
6856
  @param fld_comment           Field comment
 
6857
  @param fld_change            Field change
 
6858
  @param fld_interval_list     Interval list (if any)
 
6859
  @param fld_charset           Field charset
 
6860
 
 
6861
  @retval
 
6862
    false on success
 
6863
  @retval
 
6864
    true  on error
 
6865
*/
 
6866
 
 
6867
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
 
6868
                        char *fld_length, char *fld_decimals,
 
6869
                        uint fld_type_modifier, Item *fld_default_value,
 
6870
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
 
6871
                        char *fld_change, List<String> *fld_interval_list,
 
6872
                        CHARSET_INFO *fld_charset,
 
6873
                        uint fld_geom_type __attribute__((__unused__)),
 
6874
                        enum column_format_type column_format)
 
6875
{
 
6876
  uint sign_len, allowed_type_modifier= 0;
 
6877
  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
 
6878
 
 
6879
  field= 0;
 
6880
  field_name= fld_name;
 
6881
  def= fld_default_value;
 
6882
  flags= fld_type_modifier;
 
6883
  flags|= (((uint)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
 
6884
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
 
6885
                 Field::NEXT_NUMBER : Field::NONE);
 
6886
  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
 
6887
  if (decimals >= NOT_FIXED_DEC)
 
6888
  {
 
6889
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
 
6890
             NOT_FIXED_DEC-1);
 
6891
    return(true);
 
6892
  }
 
6893
 
 
6894
  sql_type= fld_type;
 
6895
  length= 0;
 
6896
  change= fld_change;
 
6897
  interval= 0;
 
6898
  pack_length= key_length= 0;
 
6899
  charset= fld_charset;
 
6900
  interval_list.empty();
 
6901
 
 
6902
  comment= *fld_comment;
 
6903
  /*
 
6904
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
 
6905
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
 
6906
  */
 
6907
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
 
6908
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != MYSQL_TYPE_TIMESTAMP)
 
6909
    flags|= NO_DEFAULT_VALUE_FLAG;
 
6910
 
 
6911
  if (fld_length && !(length= (uint) atoi(fld_length)))
 
6912
    fld_length= 0; /* purecov: inspected */
 
6913
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
 
6914
 
 
6915
  switch (fld_type) {
 
6916
  case MYSQL_TYPE_TINY:
 
6917
    if (!fld_length)
 
6918
      length= MAX_TINYINT_WIDTH+sign_len;
 
6919
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
6920
    break;
 
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:
 
6927
    if (!fld_length)
 
6928
      length= MAX_INT_WIDTH+sign_len;
 
6929
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
6930
    break;
 
6931
  case MYSQL_TYPE_LONGLONG:
 
6932
    if (!fld_length)
 
6933
      length= MAX_BIGINT_WIDTH;
 
6934
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
6935
    break;
 
6936
  case MYSQL_TYPE_NULL:
 
6937
    break;
 
6938
  case MYSQL_TYPE_NEWDECIMAL:
 
6939
    my_decimal_trim(&length, &decimals);
 
6940
    if (length > DECIMAL_MAX_PRECISION)
 
6941
    {
 
6942
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
 
6943
               DECIMAL_MAX_PRECISION);
 
6944
      return(true);
 
6945
    }
 
6946
    if (length < decimals)
 
6947
    {
 
6948
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
 
6949
      return(true);
 
6950
    }
 
6951
    length=
 
6952
      my_decimal_precision_to_length(length, decimals,
 
6953
                                     fld_type_modifier & UNSIGNED_FLAG);
 
6954
    pack_length=
 
6955
      my_decimal_get_binary_size(length, decimals);
 
6956
    break;
 
6957
  case MYSQL_TYPE_VARCHAR:
 
6958
    /*
 
6959
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
 
6960
      if they don't have a default value
 
6961
    */
 
6962
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
 
6963
    break;
 
6964
  case MYSQL_TYPE_STRING:
 
6965
    break;
 
6966
  case MYSQL_TYPE_BLOB:
 
6967
    if (fld_default_value)
 
6968
    {
 
6969
      /* Allow empty as default value. */
 
6970
      String str,*res;
 
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;
 
6995
    }
 
6996
    flags|= BLOB_FLAG;
 
6997
    break;
 
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:
 
7004
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
7005
    if (!fld_length && !fld_decimals)
 
7006
    {
 
7007
      length= DBL_DIG+7;
 
7008
      decimals= NOT_FIXED_DEC;
 
7009
    }
 
7010
    if (length < decimals &&
 
7011
        decimals != NOT_FIXED_DEC)
 
7012
    {
 
7013
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
 
7014
      return(true);
 
7015
    }
 
7016
    break;
 
7017
  case MYSQL_TYPE_TIMESTAMP:
 
7018
    if (!fld_length)
 
7019
    {
 
7020
      /* Compressed date YYYYMMDDHHMMSS */
 
7021
      length= MAX_DATETIME_COMPRESSED_WIDTH;
 
7022
    }
 
7023
    else if (length != MAX_DATETIME_WIDTH)
 
7024
    {
 
7025
      /*
 
7026
        We support only even TIMESTAMP lengths less or equal than 14
 
7027
        and 19 as length of 4.1 compatible representation.
 
7028
      */
 
7029
      length= ((length+1)/2)*2; /* purecov: inspected */
 
7030
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
 
7031
    }
 
7032
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
 
7033
    if (fld_default_value)
 
7034
    {
 
7035
      /* Grammar allows only NOW() value for ON UPDATE clause */
 
7036
      if (fld_default_value->type() == Item::FUNC_ITEM && 
 
7037
          ((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
 
7038
      {
 
7039
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
 
7040
                                             Field::TIMESTAMP_DN_FIELD);
 
7041
        /*
 
7042
          We don't need default value any longer moreover it is dangerous.
 
7043
          Everything handled by unireg_check further.
 
7044
        */
 
7045
        def= 0;
 
7046
      }
 
7047
      else
 
7048
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
 
7049
                                             Field::NONE);
 
7050
    }
 
7051
    else
 
7052
    {
 
7053
      /*
 
7054
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
 
7055
        or ON UPDATE values then for the sake of compatiblity we should treat
 
7056
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
 
7057
        have another TIMESTAMP column with auto-set option before this one)
 
7058
        or DEFAULT 0 (in other cases).
 
7059
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
 
7060
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
 
7061
        information about all TIMESTAMP fields in table will be availiable.
 
7062
 
 
7063
        If we have TIMESTAMP NULL column without explicit DEFAULT value
 
7064
        we treat it as having DEFAULT NULL attribute.
 
7065
      */
 
7066
      unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
 
7067
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
 
7068
                                              Field::NONE));
 
7069
    }
 
7070
    break;
 
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:
 
7082
    length= MAX_DATETIME_WIDTH;
 
7083
    break;
 
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:
 
7101
    {
 
7102
      /* Should be safe. */
 
7103
      pack_length= get_enum_pack_length(fld_interval_list->elements);
 
7104
 
 
7105
      List_iterator<String> it(*fld_interval_list);
 
7106
      String *tmp;
 
7107
      while ((tmp= it++))
 
7108
        interval_list.push_back(tmp);
 
7109
      length= 1; /* See comment for MYSQL_TYPE_SET above. */
 
7110
      break;
 
7111
   }
 
7112
  case MYSQL_TYPE_VAR_STRING:
 
7113
    assert(0);  /* Impossible, we killed it */
 
7114
    break;
 
7115
  }
 
7116
  /* Remember the value of length */
 
7117
  char_length= length;
 
7118
 
 
7119
  if (!(flags & BLOB_FLAG) &&
 
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)))
 
7126
  {
 
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,
 
7131
              MYF(0),
 
7132
              fld_name, max_field_charlength); /* purecov: inspected */
 
7133
    return(true);
 
7134
  }
 
7135
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
 
7136
  if ((~allowed_type_modifier) & fld_type_modifier)
 
7137
  {
 
7138
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
 
7139
    return(true);
 
7140
  }
 
7141
 
 
7142
  return(false); /* success */
 
7143
}
 
7144
 
 
7145
 
 
7146
enum_field_types get_blob_type_from_length(ulong length __attribute__((__unused__)))
 
7147
{
 
7148
  enum_field_types type;
 
7149
 
 
7150
  type= MYSQL_TYPE_BLOB;
 
7151
 
 
7152
  return type;
 
7153
}
 
7154
 
 
7155
 
 
7156
/*
 
7157
  Make a field from the .frm file info
 
7158
*/
 
7159
 
 
7160
uint32 calc_pack_length(enum_field_types type,uint32 length)
 
7161
{
 
7162
  switch (type) {
 
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:
 
7182
    abort(); return 0;                          // This shouldn't happen
 
7183
  default:
 
7184
    return 0;
 
7185
  }
 
7186
}
 
7187
 
 
7188
 
 
7189
uint pack_length_to_packflag(uint type)
 
7190
{
 
7191
  switch (type) {
 
7192
    case 1: return f_settype((uint) MYSQL_TYPE_TINY);
 
7193
    case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
1173
7194
    case 3: assert(1);
1174
 
    case 4: return f_settype(DRIZZLE_TYPE_LONG);
1175
 
    case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
 
7195
    case 4: return f_settype((uint) MYSQL_TYPE_LONG);
 
7196
    case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
1176
7197
  }
1177
7198
  return 0;                                     // This shouldn't happen
1178
7199
}
1179
7200
 
 
7201
 
 
7202
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
 
7203
                  uchar *null_pos, uchar null_bit,
 
7204
                  uint pack_flag,
 
7205
                  enum_field_types field_type,
 
7206
                  CHARSET_INFO *field_charset,
 
7207
                  Field::utype unireg_check,
 
7208
                  TYPELIB *interval,
 
7209
                  const char *field_name)
 
7210
{
 
7211
  if (!f_maybe_null(pack_flag))
 
7212
  {
 
7213
    null_pos=0;
 
7214
    null_bit=0;
 
7215
  }
 
7216
  else
 
7217
  {
 
7218
    null_bit= ((uchar) 1) << null_bit;
 
7219
  }
 
7220
 
 
7221
  switch (field_type) {
 
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:
 
7227
    field_charset= &my_charset_bin;
 
7228
  default: break;
 
7229
  }
 
7230
 
 
7231
  if (f_is_alpha(pack_flag))
 
7232
  {
 
7233
    if (!f_is_packed(pack_flag))
 
7234
    {
 
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)
 
7241
        return new Field_varstring(ptr,field_length,
 
7242
                                   HA_VARCHAR_PACKLENGTH(field_length),
 
7243
                                   null_pos,null_bit,
 
7244
                                   unireg_check, field_name,
 
7245
                                   share,
 
7246
                                   field_charset);
 
7247
      return 0;                                 // Error
 
7248
    }
 
7249
 
 
7250
    uint pack_length=calc_pack_length((enum_field_types)
 
7251
                                      f_packtype(pack_flag),
 
7252
                                      field_length);
 
7253
 
 
7254
    if (f_is_blob(pack_flag))
 
7255
      return new Field_blob(ptr,null_pos,null_bit,
 
7256
                            unireg_check, field_name, share,
 
7257
                            pack_length, field_charset);
 
7258
    if (interval)
 
7259
    {
 
7260
      if (f_is_enum(pack_flag))
 
7261
        return new Field_enum(ptr,field_length,null_pos,null_bit,
 
7262
                                  unireg_check, field_name,
 
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);
 
7268
    }
 
7269
  }
 
7270
 
 
7271
  switch (field_type) {
 
7272
  case MYSQL_TYPE_NEWDECIMAL:
 
7273
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
 
7274
                                 unireg_check, field_name,
 
7275
                                 f_decimals(pack_flag),
 
7276
                                 f_is_zerofill(pack_flag) != 0,
 
7277
                                 f_is_dec(pack_flag) == 0);
 
7278
  case MYSQL_TYPE_DOUBLE:
 
7279
    return new Field_double(ptr,field_length,null_pos,null_bit,
 
7280
                            unireg_check, field_name,
 
7281
                            f_decimals(pack_flag),
 
7282
                            f_is_zerofill(pack_flag) != 0,
 
7283
                            f_is_dec(pack_flag)== 0);
 
7284
  case MYSQL_TYPE_TINY:
 
7285
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
 
7286
                          unireg_check, field_name,
 
7287
                          f_is_zerofill(pack_flag) != 0,
 
7288
                          f_is_dec(pack_flag) == 0);
 
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:
 
7295
    return new Field_long(ptr,field_length,null_pos,null_bit,
 
7296
                           unireg_check, field_name,
 
7297
                           f_is_zerofill(pack_flag) != 0,
 
7298
                           f_is_dec(pack_flag) == 0);
 
7299
  case MYSQL_TYPE_LONGLONG:
 
7300
    return new Field_int64_t(ptr,field_length,null_pos,null_bit,
 
7301
                              unireg_check, field_name,
 
7302
                              f_is_zerofill(pack_flag) != 0,
 
7303
                              f_is_dec(pack_flag) == 0);
 
7304
  case MYSQL_TYPE_TIMESTAMP:
 
7305
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
 
7306
                               unireg_check, field_name, share,
 
7307
                               field_charset);
 
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:
 
7313
    return new Field_newdate(ptr,null_pos,null_bit,
 
7314
                             unireg_check, field_name, field_charset);
 
7315
  case MYSQL_TYPE_TIME:
 
7316
    return new Field_time(ptr,null_pos,null_bit,
 
7317
                          unireg_check, field_name, field_charset);
 
7318
  case MYSQL_TYPE_DATETIME:
 
7319
    return new Field_datetime(ptr,null_pos,null_bit,
 
7320
                              unireg_check, field_name, field_charset);
 
7321
  case MYSQL_TYPE_NULL:
 
7322
    return new Field_null(ptr, field_length, unireg_check, field_name,
 
7323
                          field_charset);
 
7324
  default:                                      // Impossible (Wrong version)
 
7325
    break;
 
7326
  }
 
7327
  return 0;
 
7328
}
 
7329
 
 
7330
 
 
7331
/** Create a field suitable for create of table. */
 
7332
 
 
7333
Create_field::Create_field(Field *old_field,Field *orig_field)
 
7334
{
 
7335
  field=      old_field;
 
7336
  field_name=change=old_field->field_name;
 
7337
  length=     old_field->field_length;
 
7338
  flags=      old_field->flags;
 
7339
  unireg_check=old_field->unireg_check;
 
7340
  pack_length=old_field->pack_length();
 
7341
  key_length= old_field->key_length();
 
7342
  sql_type=   old_field->real_type();
 
7343
  charset=    old_field->charset();             // May be NULL ptr
 
7344
  comment=    old_field->comment;
 
7345
  decimals=   old_field->decimals();
 
7346
 
 
7347
  /* Fix if the original table had 4 byte pointer blobs */
 
7348
  if (flags & BLOB_FLAG)
 
7349
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
 
7350
                  portable_sizeof_char_ptr);
 
7351
 
 
7352
  switch (sql_type) {
 
7353
  case MYSQL_TYPE_BLOB:
 
7354
    sql_type= MYSQL_TYPE_BLOB;
 
7355
    length/= charset->mbmaxlen;
 
7356
    key_length/= charset->mbmaxlen;
 
7357
    break;
 
7358
  case MYSQL_TYPE_STRING:
 
7359
    /* Change CHAR -> VARCHAR if dynamic record length */
 
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:
 
7368
    /* This is corrected in create_length_to_internal_length */
 
7369
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
 
7370
    break;
 
7371
  default:
 
7372
    break;
 
7373
  }
 
7374
 
 
7375
  if (flags & (ENUM_FLAG | SET_FLAG))
 
7376
    interval= ((Field_enum*) old_field)->typelib;
 
7377
  else
 
7378
    interval=0;
 
7379
  def=0;
 
7380
  char_length= length;
 
7381
 
 
7382
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
 
7383
      old_field->ptr && orig_field &&
 
7384
      (sql_type != MYSQL_TYPE_TIMESTAMP ||                /* set def only if */
 
7385
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
 
7386
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
 
7387
  {
 
7388
    char buff[MAX_FIELD_WIDTH];
 
7389
    String tmp(buff,sizeof(buff), charset);
 
7390
    my_ptrdiff_t diff;
 
7391
 
 
7392
    /* Get the value from default_values */
 
7393
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
 
7394
                          orig_field->table->record[0]);
 
7395
    orig_field->move_field_offset(diff);        // Points now at default_values
 
7396
    if (!orig_field->is_real_null())
 
7397
    {
 
7398
      char buff[MAX_FIELD_WIDTH], *pos;
 
7399
      String tmp(buff, sizeof(buff), charset), *res;
 
7400
      res= orig_field->val_str(&tmp);
 
7401
      pos= (char*) sql_strmake(res->ptr(), res->length());
 
7402
      def= new Item_string(pos, res->length(), charset);
 
7403
    }
 
7404
    orig_field->move_field_offset(-diff);       // Back to record[0]
 
7405
  }
 
7406
}
 
7407
 
 
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
 
1180
7435
/*****************************************************************************
1181
7436
 Warning handling
1182
7437
*****************************************************************************/
1183
7438
 
1184
 
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1185
 
                        drizzled::error_t code,
1186
 
                        int cuted_increment)
 
7439
/**
 
7440
  Produce warning or note about data saved into field.
 
7441
 
 
7442
  @param level            - level of message (Note/Warning/Error)
 
7443
  @param code             - error code of message to be produced
 
7444
  @param cuted_increment  - whenever we should increase cut fields count or not
 
7445
 
 
7446
  @note
 
7447
    This function won't produce warning and increase cut fields counter
 
7448
    if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
 
7449
 
 
7450
    if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
 
7451
    This allows us to avoid notes in optimisation, like convert_constant_item().
 
7452
 
 
7453
  @retval
 
7454
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
 
7455
  @retval
 
7456
    0 otherwise
 
7457
*/
 
7458
 
 
7459
bool 
 
7460
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
 
7461
                   int cuted_increment)
1187
7462
{
1188
7463
  /*
1189
7464
    If this field was created only for type conversion purposes it
1190
7465
    will have table == NULL.
1191
7466
  */
1192
 
  Session *session= table ? table->in_use : current_session;
1193
 
  if (session->count_cuted_fields)
 
7467
  THD *thd= table ? table->in_use : current_thd;
 
7468
  if (thd->count_cuted_fields)
1194
7469
  {
1195
 
    session->cuted_fields+= cuted_increment;
1196
 
    push_warning_printf(session, level, code, ER(code), field_name,
1197
 
                        session->row_count);
 
7470
    thd->cuted_fields+= cuted_increment;
 
7471
    push_warning_printf(thd, level, code, ER(code), field_name,
 
7472
                        thd->row_count);
1198
7473
    return 0;
1199
7474
  }
1200
 
  return level >= DRIZZLE_ERROR::WARN_LEVEL_WARN;
 
7475
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
1201
7476
}
1202
7477
 
1203
7478
 
1204
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1205
 
                                 drizzled::error_t code,
1206
 
                                 const char *str, 
1207
 
                                 uint32_t str_length,
1208
 
                                 type::timestamp_t ts_type, 
1209
 
                                 int cuted_increment)
 
7479
/**
 
7480
  Produce warning or note about datetime string data saved into field.
 
7481
 
 
7482
  @param level            level of message (Note/Warning/Error)
 
7483
  @param code             error code of message to be produced
 
7484
  @param str              string value which we tried to save
 
7485
  @param str_length       length of string which we tried to save
 
7486
  @param ts_type          type of datetime value (datetime/date/time)
 
7487
  @param cuted_increment  whenever we should increase cut fields count or not
 
7488
 
 
7489
  @note
 
7490
    This function will always produce some warning but won't increase cut
 
7491
    fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
 
7492
    thread.
 
7493
*/
 
7494
 
 
7495
void 
 
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)
1210
7499
{
1211
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1212
 
 
1213
 
  if ((session->abortOnWarning() and
1214
 
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
 
7500
  THD *thd= table ? table->in_use : current_thd;
 
7501
  if ((thd->really_abort_on_warning() &&
 
7502
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
1215
7503
      set_warning(level, code, cuted_increment))
1216
 
    make_truncated_value_warning(session, level, str, str_length, ts_type,
 
7504
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
1217
7505
                                 field_name);
1218
7506
}
1219
7507
 
1220
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
1221
 
                                 drizzled::error_t code,
1222
 
                                 int64_t nr, 
1223
 
                                 type::timestamp_t ts_type,
1224
 
                                 int cuted_increment)
 
7508
 
 
7509
/**
 
7510
  Produce warning or note about integer datetime value saved into field.
 
7511
 
 
7512
  @param level            level of message (Note/Warning/Error)
 
7513
  @param code             error code of message to be produced
 
7514
  @param nr               numeric value which we tried to save
 
7515
  @param ts_type          type of datetime value (datetime/date/time)
 
7516
  @param cuted_increment  whenever we should increase cut fields count or not
 
7517
 
 
7518
  @note
 
7519
    This function will always produce some warning but won't increase cut
 
7520
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
 
7521
    thread.
 
7522
*/
 
7523
 
 
7524
void 
 
7525
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
7526
                            int64_t nr, timestamp_type ts_type,
 
7527
                            int cuted_increment)
1225
7528
{
1226
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1227
 
 
1228
 
  if (session->abortOnWarning() or
 
7529
  THD *thd= table ? table->in_use : current_thd;
 
7530
  if (thd->really_abort_on_warning() ||
1229
7531
      set_warning(level, code, cuted_increment))
1230
7532
  {
1231
 
    char str_nr[DECIMAL_LONGLONG_DIGITS];
1232
 
    char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1233
 
    make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
 
7533
    char str_nr[22];
 
7534
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
 
7535
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
1234
7536
                                 ts_type, field_name);
1235
7537
  }
1236
7538
}
1237
7539
 
1238
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1239
 
                                 const drizzled::error_t code,
1240
 
                                 double nr, 
1241
 
                                 type::timestamp_t ts_type)
 
7540
 
 
7541
/**
 
7542
  Produce warning or note about double datetime data saved into field.
 
7543
 
 
7544
  @param level            level of message (Note/Warning/Error)
 
7545
  @param code             error code of message to be produced
 
7546
  @param nr               double value which we tried to save
 
7547
  @param ts_type          type of datetime value (datetime/date/time)
 
7548
 
 
7549
  @note
 
7550
    This function will always produce some warning but won't increase cut
 
7551
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
 
7552
    thread.
 
7553
*/
 
7554
 
 
7555
void 
 
7556
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
 
7557
                            double nr, timestamp_type ts_type)
1242
7558
{
1243
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1244
 
 
1245
 
  if (session->abortOnWarning() or
 
7559
  THD *thd= table ? table->in_use : current_thd;
 
7560
  if (thd->really_abort_on_warning() ||
1246
7561
      set_warning(level, code, 1))
1247
7562
  {
1248
7563
    /* DBL_DIG is enough to print '-[digits].E+###' */
1249
7564
    char str_nr[DBL_DIG + 8];
1250
 
    uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
1251
 
    make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
 
7565
    uint str_len= sprintf(str_nr, "%g", nr);
 
7566
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
1252
7567
                                 field_name);
1253
7568
  }
1254
7569
}
1255
 
 
1256
 
bool Field::isReadSet() const 
1257
 
1258
 
  return table->isReadSet(field_index); 
1259
 
}
1260
 
 
1261
 
bool Field::isWriteSet()
1262
 
1263
 
  return table->isWriteSet(field_index); 
1264
 
}
1265
 
 
1266
 
void Field::setReadSet(bool arg)
1267
 
{
1268
 
  if (arg)
1269
 
    table->setReadSet(field_index);
1270
 
  else
1271
 
    table->clearReadSet(field_index);
1272
 
}
1273
 
 
1274
 
void Field::setWriteSet(bool arg)
1275
 
{
1276
 
  if (arg)
1277
 
    table->setWriteSet(field_index);
1278
 
  else
1279
 
    table->clearWriteSet(field_index);
1280
 
}
1281
 
 
1282
 
void Field::pack_num(uint64_t arg, unsigned char *destination)
1283
 
{
1284
 
  if (not destination)
1285
 
    destination= ptr;
1286
 
 
1287
 
  int64_tstore(destination, arg);
1288
 
}
1289
 
 
1290
 
void Field::pack_num(uint32_t arg, unsigned char *destination)
1291
 
{
1292
 
  if (not destination)
1293
 
    destination= ptr;
1294
 
 
1295
 
  longstore(destination, arg);
1296
 
}
1297
 
 
1298
 
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1299
 
{
1300
 
  if (not arg)
1301
 
    arg= ptr;
1302
 
 
1303
 
  int64_tget(destination, arg);
1304
 
 
1305
 
  return destination;
1306
 
}
1307
 
 
1308
 
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1309
 
{
1310
 
  if (not arg)
1311
 
    arg= ptr;
1312
 
 
1313
 
  longget(destination, arg);
1314
 
 
1315
 
  return destination;
1316
 
}
1317
 
 
1318
 
std::ostream& operator<<(std::ostream& output, const Field &field)
1319
 
{
1320
 
  output << "Field:(";
1321
 
  output <<  field.field_name;
1322
 
  output << ", ";
1323
 
  output << drizzled::display::type(field.real_type());
1324
 
  output << ", { ";
1325
 
 
1326
 
  if (field.flags & NOT_NULL_FLAG)
1327
 
    output << " NOT_NULL";
1328
 
 
1329
 
  if (field.flags & PRI_KEY_FLAG)
1330
 
    output << ", PRIMARY KEY";
1331
 
 
1332
 
  if (field.flags & UNIQUE_KEY_FLAG)
1333
 
    output << ", UNIQUE KEY";
1334
 
 
1335
 
  if (field.flags & MULTIPLE_KEY_FLAG)
1336
 
    output << ", MULTIPLE KEY";
1337
 
 
1338
 
  if (field.flags & BLOB_FLAG)
1339
 
    output << ", BLOB";
1340
 
 
1341
 
  if (field.flags & UNSIGNED_FLAG)
1342
 
    output << ", UNSIGNED";
1343
 
 
1344
 
  if (field.flags & BINARY_FLAG)
1345
 
    output << ", BINARY";
1346
 
  output << "}, ";
1347
 
  output << ")";
1348
 
 
1349
 
  return output;  // for multiple << operators.
1350
 
}
1351
 
 
1352
 
} /* namespace drizzled */