1
/* Copyright (C) 2000-2006 MySQL AB
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.
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.
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 */
21
This file implements classes defined in field.h
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation // gcc: Class implementation
28
#include "mysql_priv.h"
29
#include "sql_select.h"
33
#include <floatingpoint.h>
36
// Maximum allowed exponent value for converting string to decimal
37
#define MAX_EXPONENT 1024
39
/*****************************************************************************
40
Instansiate templates and static variables
41
*****************************************************************************/
43
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
44
template class List<Create_field>;
45
template class List_iterator<Create_field>;
48
uchar Field_null::null[1]={1};
49
const char field_separator=',';
51
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
52
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
53
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
54
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
55
((ulong) ((LL(1) << min(arg, 4) * 8) - LL(1)))
57
#define ASSERT_COLUMN_MARKED_FOR_READ DBUG_ASSERT(!table || (!table->read_set || bitmap_is_set(table->read_set, field_index)))
58
#define ASSERT_COLUMN_MARKED_FOR_WRITE DBUG_ASSERT(!table || (!table->write_set || bitmap_is_set(table->write_set, field_index)))
61
Rules for merging different types of fields in UNION
63
NOTE: to avoid 256*256 table, gap in table types numeration is skiped
64
following #defines describe that gap and how to canculate number of fields
65
and index of field in thia array.
67
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_BIT + 1)
68
#define FIELDTYPE_TEAR_TO (MYSQL_TYPE_NEWDECIMAL - 1)
69
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
70
inline int field_type2index (enum_field_types field_type)
72
return (field_type < FIELDTYPE_TEAR_FROM ?
74
((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
78
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
80
/* MYSQL_TYPE_DECIMAL -> */
82
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
83
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
84
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
85
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
86
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
87
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
88
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
89
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
90
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
91
MYSQL_TYPE_DECIMAL, MYSQL_TYPE_DECIMAL,
92
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
93
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
94
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
95
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
96
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
97
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
98
//MYSQL_TYPE_BIT <16>-<245>
100
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
101
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
102
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
103
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
104
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
105
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
106
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
107
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
108
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
109
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
111
/* MYSQL_TYPE_TINY -> */
113
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
114
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
115
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
116
MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
117
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
118
MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
119
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
120
MYSQL_TYPE_TINY, MYSQL_TYPE_VARCHAR,
121
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
122
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
123
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
124
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
125
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
126
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY,
127
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
128
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
129
//MYSQL_TYPE_BIT <16>-<245>
131
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
132
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
133
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
134
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
135
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
136
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
137
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
138
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
139
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
140
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
142
/* MYSQL_TYPE_SHORT -> */
144
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
145
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_SHORT,
146
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
147
MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
148
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
149
MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
150
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
151
MYSQL_TYPE_SHORT, MYSQL_TYPE_VARCHAR,
152
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
153
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
154
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
155
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
156
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
157
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_SHORT,
158
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
159
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
160
//MYSQL_TYPE_BIT <16>-<245>
162
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
163
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
164
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
165
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
166
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
167
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
168
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
169
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
170
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
171
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
173
/* MYSQL_TYPE_LONG -> */
175
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
176
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONG,
177
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
178
MYSQL_TYPE_LONG, MYSQL_TYPE_LONG,
179
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
180
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
181
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
182
MYSQL_TYPE_LONG, MYSQL_TYPE_VARCHAR,
183
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
184
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
185
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
186
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
187
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
188
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONG,
189
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
190
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
191
//MYSQL_TYPE_BIT <16>-<245>
193
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
194
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
195
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
196
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
197
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
198
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
199
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
200
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
201
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
202
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
204
/* MYSQL_TYPE_FLOAT -> */
206
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
207
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT,
208
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
209
MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
210
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
211
MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
212
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
213
MYSQL_TYPE_FLOAT, MYSQL_TYPE_VARCHAR,
214
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
215
MYSQL_TYPE_FLOAT, MYSQL_TYPE_INT24,
216
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
217
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
218
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
219
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_FLOAT,
220
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
221
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
222
//MYSQL_TYPE_BIT <16>-<245>
224
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
225
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
226
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
227
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
228
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
229
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
230
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
231
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
232
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
233
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
235
/* MYSQL_TYPE_DOUBLE -> */
237
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
238
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
239
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
240
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
241
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
242
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
243
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
244
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
245
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
246
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_INT24,
247
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
248
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
249
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
250
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_DOUBLE,
251
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
252
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
253
//MYSQL_TYPE_BIT <16>-<245>
255
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
256
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
257
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
258
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
259
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
260
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
261
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
262
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
263
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
264
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
266
/* MYSQL_TYPE_NULL -> */
268
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
269
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
270
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
271
MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
272
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
273
MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
274
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
275
MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP,
276
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
277
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
278
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
279
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_TIME,
280
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
281
MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR,
282
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
283
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
284
//MYSQL_TYPE_BIT <16>-<245>
286
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
287
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_ENUM,
288
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
289
MYSQL_TYPE_SET, MYSQL_TYPE_TINY_BLOB,
290
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
291
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
292
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
293
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
294
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
295
MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY
297
/* MYSQL_TYPE_TIMESTAMP -> */
299
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
300
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
301
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
302
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
303
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
304
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
305
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
306
MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_TIMESTAMP,
307
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
308
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
309
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
310
MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
311
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
312
MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
313
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
314
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
315
//MYSQL_TYPE_BIT <16>-<245>
317
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
318
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
319
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
320
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
321
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
322
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
323
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
324
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
325
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
326
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
328
/* MYSQL_TYPE_LONGLONG -> */
330
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
331
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONGLONG,
332
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
333
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
334
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
335
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
336
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
337
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_VARCHAR,
338
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
339
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG,
340
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
341
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
342
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
343
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONGLONG,
344
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
345
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
346
//MYSQL_TYPE_BIT <16>-<245>
348
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
349
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
350
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
351
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
352
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
353
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
354
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
355
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
356
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
357
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
359
/* MYSQL_TYPE_INT24 -> */
361
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
362
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_INT24,
363
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
364
MYSQL_TYPE_INT24, MYSQL_TYPE_LONG,
365
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
366
MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
367
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
368
MYSQL_TYPE_INT24, MYSQL_TYPE_VARCHAR,
369
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
370
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
371
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
372
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
373
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
374
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_INT24,
375
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
376
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
377
//MYSQL_TYPE_BIT <16>-<245>
379
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
380
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
381
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
382
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
383
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
384
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
385
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
386
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
387
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
388
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
390
/* MYSQL_TYPE_DATE -> */
392
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
393
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
394
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
395
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
396
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
397
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
398
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
399
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
400
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
401
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
402
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
403
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
404
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
405
MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
406
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
407
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
408
//MYSQL_TYPE_BIT <16>-<245>
410
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
411
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
412
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
413
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
414
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
415
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
416
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
417
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
418
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
419
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
421
/* MYSQL_TYPE_TIME -> */
423
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
424
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
425
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
426
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
427
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
428
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
429
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
430
MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME,
431
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
432
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
433
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
434
MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIME,
435
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
436
MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
437
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
438
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
439
//MYSQL_TYPE_BIT <16>-<245>
441
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
442
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
443
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
444
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
445
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
446
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
447
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
448
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
449
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
450
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
452
/* MYSQL_TYPE_DATETIME -> */
454
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
455
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
456
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
457
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
458
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
459
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
460
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
461
MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
462
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
463
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
464
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
465
MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
466
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
467
MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
468
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
469
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
470
//MYSQL_TYPE_BIT <16>-<245>
472
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
473
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
474
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
475
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
476
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
477
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
478
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
479
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
480
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
481
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
483
/* MYSQL_TYPE_YEAR -> */
485
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
486
MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,
487
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
488
MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
489
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
490
MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
491
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
492
MYSQL_TYPE_YEAR, MYSQL_TYPE_VARCHAR,
493
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
494
MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
495
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
496
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
497
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
498
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_YEAR,
499
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
500
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
501
//MYSQL_TYPE_BIT <16>-<245>
503
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
504
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
505
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
506
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
507
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
508
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
509
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
510
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
511
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
512
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
514
/* MYSQL_TYPE_NEWDATE -> */
516
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
517
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
518
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
519
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
520
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
521
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
522
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
523
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
524
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
525
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
526
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
527
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
528
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
529
MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
530
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
531
MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
532
//MYSQL_TYPE_BIT <16>-<245>
534
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
535
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
536
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
537
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
538
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
539
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
540
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
541
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
542
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
543
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
545
/* MYSQL_TYPE_VARCHAR -> */
547
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
548
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
549
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
550
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
551
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
552
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
553
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
554
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
555
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
556
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
557
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
558
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
559
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
560
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
561
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
562
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
563
//MYSQL_TYPE_BIT <16>-<245>
565
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
566
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
567
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
568
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
569
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
570
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
571
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
572
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
573
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
574
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR
576
/* MYSQL_TYPE_BIT -> */
578
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
579
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
580
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
581
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
582
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
583
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
584
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
585
MYSQL_TYPE_BIT, MYSQL_TYPE_VARCHAR,
586
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
587
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
588
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
589
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
590
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
591
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
592
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
593
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
594
//MYSQL_TYPE_BIT <16>-<245>
596
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
597
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
598
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
599
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
600
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
601
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
602
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
603
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
604
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
605
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
607
/* MYSQL_TYPE_NEWDECIMAL -> */
609
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
610
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
611
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
612
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
613
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
614
MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
615
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
616
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
617
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
618
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
619
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
620
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
621
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
622
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_NEWDECIMAL,
623
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
624
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
625
//MYSQL_TYPE_BIT <16>-<245>
627
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
628
MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
629
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
630
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
631
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
632
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
633
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
634
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
635
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
636
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
638
/* MYSQL_TYPE_ENUM -> */
640
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
641
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
642
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
643
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
644
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
645
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
646
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
647
MYSQL_TYPE_ENUM, MYSQL_TYPE_VARCHAR,
648
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
649
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
650
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
651
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
652
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
653
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
654
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
655
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
656
//MYSQL_TYPE_BIT <16>-<245>
658
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
659
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
660
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
661
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
662
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
663
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
664
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
665
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
666
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
667
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
669
/* MYSQL_TYPE_SET -> */
671
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
672
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
673
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
674
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
675
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
676
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
677
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
678
MYSQL_TYPE_SET, MYSQL_TYPE_VARCHAR,
679
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
680
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
681
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
682
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
683
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
684
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
685
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
686
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
687
//MYSQL_TYPE_BIT <16>-<245>
689
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
690
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
691
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
692
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
693
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
694
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
695
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
696
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
697
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
698
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
700
/* MYSQL_TYPE_TINY_BLOB -> */
702
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
703
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
704
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
705
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
706
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
707
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
708
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
709
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
710
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
711
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
712
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
713
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
714
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
715
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
716
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
717
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
718
//MYSQL_TYPE_BIT <16>-<245>
719
MYSQL_TYPE_TINY_BLOB,
720
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
721
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
722
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
723
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
724
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
725
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
726
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
727
MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
728
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
729
MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB
731
/* MYSQL_TYPE_MEDIUM_BLOB -> */
733
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
734
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
735
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
736
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
737
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
738
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
739
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
740
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
741
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
742
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
743
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
744
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
745
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
746
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
747
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
748
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
749
//MYSQL_TYPE_BIT <16>-<245>
750
MYSQL_TYPE_MEDIUM_BLOB,
751
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
752
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
753
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
754
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
755
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
756
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
757
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
758
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
759
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
760
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB
762
/* MYSQL_TYPE_LONG_BLOB -> */
764
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
765
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
766
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
767
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
768
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
769
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
770
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
771
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
772
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
773
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
774
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
775
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
776
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
777
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
778
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
779
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
780
//MYSQL_TYPE_BIT <16>-<245>
781
MYSQL_TYPE_LONG_BLOB,
782
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
783
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
784
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
785
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
786
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
787
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
788
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
789
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
790
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
791
MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB
793
/* MYSQL_TYPE_BLOB -> */
795
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
796
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
797
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
798
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
799
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
800
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
801
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
802
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
803
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
804
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
805
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
806
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
807
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
808
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
809
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
810
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
811
//MYSQL_TYPE_BIT <16>-<245>
813
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
814
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
815
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
816
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
817
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
818
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
819
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
820
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
821
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
822
MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB
824
/* MYSQL_TYPE_VAR_STRING -> */
826
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
827
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
828
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
829
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
830
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
831
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
832
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
833
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
834
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
835
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
836
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
837
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
838
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
839
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
840
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
841
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
842
//MYSQL_TYPE_BIT <16>-<245>
844
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
845
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
846
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
847
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
848
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
849
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
850
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
851
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
852
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
853
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR
855
/* MYSQL_TYPE_STRING -> */
857
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
858
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
859
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
860
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
861
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
862
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
863
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
864
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
865
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
866
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
867
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
868
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
869
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
870
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
871
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
872
MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR,
873
//MYSQL_TYPE_BIT <16>-<245>
875
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
876
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
877
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
878
MYSQL_TYPE_STRING, MYSQL_TYPE_TINY_BLOB,
879
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
880
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
881
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
882
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
883
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
884
MYSQL_TYPE_STRING, MYSQL_TYPE_STRING
886
/* MYSQL_TYPE_GEOMETRY -> */
888
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
889
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
890
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
891
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
892
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
893
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
894
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
895
MYSQL_TYPE_GEOMETRY, MYSQL_TYPE_VARCHAR,
896
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
897
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
898
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
899
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
900
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
901
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
902
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
903
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
904
//MYSQL_TYPE_BIT <16>-<245>
906
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
907
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
908
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
909
MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
910
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
911
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
912
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
913
MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
914
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
915
MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY
920
Return type of which can carry value of both given types in UNION result.
922
@param a type for merging
923
@param b type for merging
929
enum_field_types Field::field_type_merge(enum_field_types a,
932
DBUG_ASSERT(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
933
DBUG_ASSERT(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
934
return field_types_merge_rules[field_type2index(a)]
935
[field_type2index(b)];
939
static Item_result field_types_result_type [FIELDTYPE_NUM]=
941
//MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
942
DECIMAL_RESULT, INT_RESULT,
943
//MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
944
INT_RESULT, INT_RESULT,
945
//MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
946
REAL_RESULT, REAL_RESULT,
947
//MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
948
STRING_RESULT, STRING_RESULT,
949
//MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
950
INT_RESULT, INT_RESULT,
951
//MYSQL_TYPE_DATE MYSQL_TYPE_TIME
952
STRING_RESULT, STRING_RESULT,
953
//MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
954
STRING_RESULT, INT_RESULT,
955
//MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
956
STRING_RESULT, STRING_RESULT,
957
//MYSQL_TYPE_BIT <16>-<245>
959
//MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
960
DECIMAL_RESULT, STRING_RESULT,
961
//MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
962
STRING_RESULT, STRING_RESULT,
963
//MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
964
STRING_RESULT, STRING_RESULT,
965
//MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
966
STRING_RESULT, STRING_RESULT,
967
//MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
968
STRING_RESULT, STRING_RESULT
973
Test if the given string contains important data:
974
not spaces for character string,
975
or any data for binary string.
978
test_if_important_data()
984
FALSE - If string does not have important data
985
TRUE - If string has some important data
989
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
991
if (cs != &my_charset_bin)
992
str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
993
return (str < strend);
998
Detect Item_result by given field type of UNION merge result.
1000
@param field_type given field type
1003
Item_result (type of internal MySQL expression result)
1006
Item_result Field::result_merge_type(enum_field_types field_type)
1008
DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM || field_type
1009
> FIELDTYPE_TEAR_TO);
1010
return field_types_result_type[field_type2index(field_type)];
1013
/*****************************************************************************
1014
Static help functions
1015
*****************************************************************************/
1019
Check whether a field type can be partially indexed by a key.
1021
This is a static method, rather than a virtual function, because we need
1022
to check the type of a non-Field in mysql_alter_table().
1024
@param type field type
1027
TRUE Type can have a prefixed key
1029
FALSE Type can not have a prefixed key
1032
bool Field::type_can_have_key_part(enum enum_field_types type)
1035
case MYSQL_TYPE_VARCHAR:
1036
case MYSQL_TYPE_TINY_BLOB:
1037
case MYSQL_TYPE_MEDIUM_BLOB:
1038
case MYSQL_TYPE_LONG_BLOB:
1039
case MYSQL_TYPE_BLOB:
1040
case MYSQL_TYPE_VAR_STRING:
1041
case MYSQL_TYPE_STRING:
1050
Numeric fields base class constructor.
1052
Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1053
uchar null_bit_arg, utype unireg_check_arg,
1054
const char *field_name_arg,
1055
uint8 dec_arg, bool zero_arg, bool unsigned_arg)
1056
:Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1057
unireg_check_arg, field_name_arg),
1058
dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
1061
flags|=ZEROFILL_FLAG;
1063
flags|=UNSIGNED_FLAG;
1067
void Field_num::prepend_zeros(String *value)
1070
if ((diff= (int) (field_length - value->length())) > 0)
1072
bmove_upp((uchar*) value->ptr()+field_length,
1073
(uchar*) value->ptr()+value->length(),
1075
bfill((uchar*) value->ptr(),diff,'0');
1076
value->length(field_length);
1077
(void) value->c_ptr_quick(); // Avoid warnings in purify
1082
Test if given number is a int.
1085
Make this multi-byte-character safe
1087
@param str String to test
1088
@param length Length of 'str'
1089
@param int_end Pointer to char after last used digit
1090
@param cs Character set
1093
This is called after one has called strntoull10rnd() function.
1098
1 error: empty string or wrong integer.
1100
2 error: garbage at the end of string.
1103
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length,
1104
const char *int_end, int error)
1106
/* Test if we get an empty string or wrong integer */
1107
if (str == int_end || error == MY_ERRNO_EDOM)
1110
String tmp(buff, (uint32) sizeof(buff), system_charset_info);
1111
tmp.copy(str, length, system_charset_info);
1112
push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
1113
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1114
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1115
"integer", tmp.c_ptr(), field_name,
1116
(ulong) table->in_use->row_count);
1119
/* Test if we have garbage at the end of the given string. */
1120
if (test_if_important_data(cs, int_end, str + length))
1122
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1130
Conver a string to an integer then check bounds.
1135
from String to convert
1136
len Length of the string
1137
rnd OUT longlong value
1138
unsigned_max max unsigned value
1139
signed_min min signed value
1140
signed_max max signed value
1143
The function calls strntoull10rnd() to get an integer value then
1144
check bounds and errors returned. In case of any error a warning
1152
bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len,
1153
longlong *rnd, uint64_t unsigned_max,
1154
longlong signed_min, longlong signed_max)
1159
*rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
1160
unsigned_flag, &end,
1165
if ((((uint64_t) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max)) ||
1166
error == MY_ERRNO_ERANGE)
1173
if (*rnd < signed_min)
1178
else if (*rnd > signed_max)
1184
if (table->in_use->count_cuted_fields &&
1185
check_int(cs, from, len, end, error))
1190
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1195
Process decimal library return codes and issue warnings for overflow and
1198
@param op_result decimal library return code (E_DEC_* see include/decimal.h)
1201
1 there was overflow
1203
0 no error or some other errors except overflow
1206
int Field::warn_if_overflow(int op_result)
1208
if (op_result == E_DEC_OVERFLOW)
1210
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1213
if (op_result == E_DEC_TRUNCATED)
1215
set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1216
/* We return 0 here as this is not a critical issue */
1223
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
1225
cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
1227
while (length && my_isspace(cs,*str))
1228
{ // Allow start space
1233
if (*str == '+' || *str == '-')
1236
if (!length || !(my_isdigit(cs,*str) || *str == '.'))
1239
while (length && my_isdigit(cs,*str))
1248
while (length && my_isdigit(cs,*str))
1255
if (*str == 'E' || *str == 'e')
1257
if (length < 3 || (str[1] != '+' && str[1] != '-') ||
1258
!my_isdigit(cs,str[2]))
1262
while (length && my_isdigit(cs,*str))
1267
for (; length ; length--, str++)
1268
{ // Allow end space
1269
if (!my_isspace(cs,*str))
1278
Interpret field value as an integer but return the result as a string.
1280
This is used for printing bit_fields as numbers while debugging.
1283
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
1285
ASSERT_COLUMN_MARKED_FOR_READ;
1286
CHARSET_INFO *cs= &my_charset_bin;
1288
longlong value= val_int();
1290
if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
1292
length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
1293
MY_INT64_NUM_DECIMAL_DIGITS,
1294
unsigned_val ? 10 : -10,
1296
val_buffer->length(length);
1301
/// This is used as a table name when the table structure is not set up
1302
Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
1304
utype unireg_check_arg, const char *field_name_arg)
1305
:ptr(ptr_arg), null_ptr(null_ptr_arg),
1306
table(0), orig_table(0), table_name(0),
1307
field_name(field_name_arg),
1308
key_start(0), part_of_key(0), part_of_key_not_clustered(0),
1309
part_of_sortkey(0), unireg_check(unireg_check_arg),
1310
field_length(length_arg), null_bit(null_bit_arg),
1311
is_created_from_null_item(FALSE)
1313
flags=null_ptr ? 0: NOT_NULL_FLAG;
1314
comment.str= (char*) "";
1320
void Field::hash(ulong *nr, ulong *nr2)
1324
*nr^= (*nr << 1) | 1;
1328
uint len= pack_length();
1329
CHARSET_INFO *cs= charset();
1330
cs->coll->hash_sort(cs, ptr, len, nr, nr2);
1335
Field::do_last_null_byte() const
1337
DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
1339
return (size_t) (null_ptr - table->record[0]) + 1;
1340
return LAST_NULL_BYTE_UNDEF;
1344
void Field::copy_from_tmp(int row_offset)
1346
memcpy(ptr,ptr+row_offset,pack_length());
1349
*null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | (null_ptr[row_offset] & (uchar) null_bit));
1354
bool Field::send_binary(Protocol *protocol)
1356
char buff[MAX_FIELD_WIDTH];
1357
String tmp(buff,sizeof(buff),charset());
1359
return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
1364
Check to see if field size is compatible with destination.
1366
This method is used in row-based replication to verify that the slave's
1367
field size is less than or equal to the master's field size. The
1368
encoded field metadata (from the master or source) is decoded and compared
1369
to the size of this field (the slave or destination).
1371
@param field_metadata Encoded size in field metadata
1373
@retval 0 if this field's size is < the source field's size
1374
@retval 1 if this field's size is >= the source field's size
1376
int Field::compatible_field_size(uint field_metadata)
1378
uint const source_size= pack_length_from_metadata(field_metadata);
1379
uint const destination_size= row_pack_length();
1380
return (source_size <= destination_size);
1384
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
1385
enum_check_fields check_level)
1388
enum_check_fields old_check_level= table->in_use->count_cuted_fields;
1389
table->in_use->count_cuted_fields= check_level;
1390
res= store(to, length, cs);
1391
table->in_use->count_cuted_fields= old_check_level;
1397
Pack the field into a format suitable for storage and transfer.
1399
To implement packing functionality, only the virtual function
1400
should be overridden. The other functions are just convenience
1401
functions and hence should not be overridden.
1403
The value of <code>low_byte_first</code> is dependent on how the
1404
packed data is going to be used: for local use, e.g., temporary
1405
store on disk or in memory, use the native format since that is
1406
faster. For data that is going to be transfered to other machines
1407
(e.g., when writing data to the binary log), data should always be
1408
stored in little-endian format.
1410
@note The default method for packing fields just copy the raw bytes
1411
of the record into the destination, but never more than
1412
<code>max_length</code> characters.
1415
Pointer to memory area where representation of field should be put.
1418
Pointer to memory area where record representation of field is
1422
Maximum length of the field, as given in the column definition. For
1423
example, for <code>CHAR(1000)</code>, the <code>max_length</code>
1424
is 1000. This information is sometimes needed to decide how to pack
1427
@param low_byte_first
1428
@c TRUE if integers should be stored little-endian, @c FALSE if
1429
native format should be used. Note that for little-endian machines,
1430
the value of this flag is a moot point since the native format is
1434
Field::pack(uchar *to, const uchar *from, uint max_length,
1435
bool low_byte_first __attribute__((unused)))
1437
uint32 length= pack_length();
1438
set_if_smaller(length, max_length);
1439
memcpy(to, from, length);
1444
Unpack a field from row data.
1446
This method is used to unpack a field from a master whose size of
1447
the field is less than that of the slave.
1449
The <code>param_data</code> parameter is a two-byte integer (stored
1450
in the least significant 16 bits of the unsigned integer) usually
1451
consisting of two parts: the real type in the most significant byte
1452
and a original pack length in the least significant byte.
1454
The exact layout of the <code>param_data</code> field is given by
1455
the <code>Table_map_log_event::save_field_metadata()</code>.
1457
This is the default method for unpacking a field. It just copies
1458
the memory block in byte order (of original pack length bytes or
1459
length of field, whichever is smaller).
1461
@param to Destination of the data
1462
@param from Source of the data
1463
@param param_data Real type and original pack length of the field
1466
@param low_byte_first
1467
If this flag is @c true, all composite entities (e.g., lengths)
1468
should be unpacked in little-endian format; otherwise, the entities
1469
are unpacked in native order.
1471
@return New pointer into memory based on from + length of the data
1474
Field::unpack(uchar* to, const uchar *from, uint param_data,
1475
bool low_byte_first __attribute__((unused)))
1477
uint length=pack_length();
1480
If from length is > 255, it has encoded data in the upper bits. Need
1483
if (param_data > 255)
1485
from_type= (param_data & 0xff00) >> 8U; // real_type.
1486
param_data= param_data & 0x00ff; // length.
1489
if ((param_data == 0) ||
1490
(length == param_data) ||
1491
(from_type != real_type()))
1493
memcpy(to, from, length);
1497
uint len= (param_data && (param_data < length)) ?
1498
param_data : length;
1500
memcpy(to, from, param_data > length ? length : len);
1505
my_decimal *Field::val_decimal(my_decimal *decimal)
1507
/* This never have to be called */
1513
void Field_num::add_zerofill_and_unsigned(String &res) const
1516
res.append(STRING_WITH_LEN(" unsigned"));
1518
res.append(STRING_WITH_LEN(" zerofill"));
1522
void Field::make_field(Send_field *field)
1524
if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1526
field->db_name= orig_table->s->db.str;
1527
field->org_table_name= orig_table->s->table_name.str;
1530
field->org_table_name= field->db_name= "";
1533
field->table_name= orig_table->alias;
1534
field->org_col_name= field_name;
1538
field->table_name= "";
1539
field->org_col_name= "";
1541
field->col_name= field_name;
1542
field->charsetnr= charset()->number;
1543
field->length=field_length;
1545
field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1551
Conversion from decimal to longlong with checking overflow and
1552
setting correct value (min/max) in case of overflow.
1554
@param val value which have to be converted
1555
@param unsigned_flag type of integer in which we convert val
1556
@param err variable to pass error code
1559
value converted from val
1561
longlong Field::convert_decimal2longlong(const my_decimal *val,
1562
bool unsigned_flag, int *err)
1569
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1573
else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1574
~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1581
else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1582
~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1585
i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
1593
Storing decimal in integer fields.
1595
@param val value for storing
1598
This method is used by all integer fields, real/decimal redefine it
1606
int Field_num::store_decimal(const my_decimal *val)
1608
ASSERT_COLUMN_MARKED_FOR_WRITE;
1610
longlong i= convert_decimal2longlong(val, unsigned_flag, &err);
1611
return test(err | store(i, unsigned_flag));
1616
Return decimal value of integer field.
1618
@param decimal_value buffer for storing decimal value
1621
This method is used by all integer fields, real/decimal redefine it.
1622
All longlong values fit in our decimal buffer which cal store 8*9=72
1623
digits of integer number
1626
pointer to decimal buffer with value of field
1629
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
1631
ASSERT_COLUMN_MARKED_FOR_READ;
1632
DBUG_ASSERT(result_type() == INT_RESULT);
1633
longlong nr= val_int();
1634
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1635
return decimal_value;
1639
Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1640
uchar null_bit_arg, utype unireg_check_arg,
1641
const char *field_name_arg, CHARSET_INFO *charset_arg)
1642
:Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1643
unireg_check_arg, field_name_arg)
1645
field_charset= charset_arg;
1646
if (charset_arg->state & MY_CS_BINSORT)
1648
field_derivation= DERIVATION_IMPLICIT;
1652
void Field_num::make_field(Send_field *field)
1654
Field::make_field(field);
1655
field->decimals= dec;
1659
Decimal representation of Field_str.
1661
@param d value for storing
1664
Field_str is the base class for fields like Field_enum,
1665
Field_date and some similar. Some dates use fraction and also
1666
string value should be converted to floating point value according
1667
our rules, so we use double to store value of decimal in string.
1678
int Field_str::store_decimal(const my_decimal *d)
1680
ASSERT_COLUMN_MARKED_FOR_WRITE;
1682
/* TODO: use decimal2string? */
1683
int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
1684
~E_DEC_OVERFLOW, d, &val));
1685
return err | store(val);
1689
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
1691
ASSERT_COLUMN_MARKED_FOR_READ;
1692
longlong nr= val_int();
1693
int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
1694
return decimal_value;
1698
uint Field::fill_cache_field(CACHE_FIELD *copy)
1702
copy->length=pack_length();
1704
if (flags & BLOB_FLAG)
1706
copy->blob_field=(Field_blob*) this;
1708
copy->length-= table->s->blob_ptr_size;
1709
return copy->length;
1711
else if (!zero_pack() &&
1712
(type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
1713
copy->length < 256))
1715
copy->strip=1; /* Remove end space */
1723
return copy->length+ store_length;
1727
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1730
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1731
if (!(res=val_str(&tmp)) ||
1732
str_to_datetime_with_warn(res->ptr(), res->length(),
1733
ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
1738
bool Field::get_time(MYSQL_TIME *ltime)
1741
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1742
if (!(res=val_str(&tmp)) ||
1743
str_to_time_with_warn(res->ptr(), res->length(), ltime))
1749
This is called when storing a date in a string.
1752
Needs to be changed if/when we want to support different time formats.
1755
int Field::store_time(MYSQL_TIME *ltime, timestamp_type type_arg)
1757
ASSERT_COLUMN_MARKED_FOR_WRITE;
1758
char buff[MAX_DATE_STRING_REP_LENGTH];
1759
uint length= (uint) my_TIME_to_str(ltime, buff);
1760
return store(buff, length, &my_charset_bin);
1764
bool Field::optimize_range(uint idx, uint part)
1766
return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1770
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
1771
bool keep_type __attribute__((unused)))
1774
if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1777
if (tmp->table->maybe_null)
1778
tmp->flags&= ~NOT_NULL_FLAG;
1779
tmp->table= new_table;
1780
tmp->key_start.init(0);
1781
tmp->part_of_key.init(0);
1782
tmp->part_of_sortkey.init(0);
1783
tmp->unireg_check= Field::NONE;
1784
tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
1785
ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1786
tmp->reset_fields();
1791
Field *Field::new_key_field(MEM_ROOT *root, struct st_table *new_table,
1792
uchar *new_ptr, uchar *new_null_ptr,
1796
if ((tmp= new_field(root, new_table, table == new_table)))
1799
tmp->null_ptr= new_null_ptr;
1800
tmp->null_bit= new_null_bit;
1806
/* This is used to generate a field in TABLE from TABLE_SHARE */
1808
Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
1811
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1813
tmp->init(new_table);
1814
tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
1815
new_table->s->default_values));
1821
/****************************************************************************
1822
Field_null, a field that always return NULL
1823
****************************************************************************/
1825
void Field_null::sql_type(String &res) const
1827
res.set_ascii(STRING_WITH_LEN("null"));
1831
/****************************************************************************
1832
Functions for the Field_decimal class
1833
This is an number stored as a pre-space (or pre-zero) string
1834
****************************************************************************/
1837
Field_decimal::reset(void)
1839
Field_decimal::store(STRING_WITH_LEN("0"),&my_charset_bin);
1843
void Field_decimal::overflow(bool negative)
1845
uint len=field_length;
1846
uchar *to=ptr, filler= '9';
1848
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1853
/* Put - sign as a first digit so we'll have -999..999 or 999..999 */
1859
filler= '0'; // Fill up with 0
1863
Handle unsigned integer without zerofill, in which case
1864
the number should be of format ' 0' or ' 0.000'
1866
uint whole_part=field_length- (dec ? dec+2 : 1);
1867
// Fill with spaces up to the first digit
1868
bfill(to, whole_part, ' ');
1871
// The main code will also handle the 0 before the decimal point
1875
bfill(to, len, filler);
1877
ptr[field_length-dec-1]='.';
1882
int Field_decimal::store(const char *from_arg, uint len, CHARSET_INFO *cs)
1884
ASSERT_COLUMN_MARKED_FOR_WRITE;
1885
char buff[STRING_BUFFER_USUAL_SIZE];
1886
String tmp(buff,sizeof(buff), &my_charset_bin);
1887
const uchar *from= (uchar*) from_arg;
1889
/* Convert character set if the old one is multi uchar */
1890
if (cs->mbmaxlen > 1)
1893
tmp.copy((char*) from, len, cs, &my_charset_bin, &dummy_errors);
1894
from= (uchar*) tmp.ptr();
1898
const uchar *end= from+len;
1899
/* The pointer where the field value starts (i.e., "where to write") */
1901
uint tmp_dec, tmp_uint;
1903
The sign of the number : will be 0 (means positive but sign not
1904
specified), '+' or '-'
1907
/* The pointers where prezeros start and stop */
1908
const uchar *pre_zeros_from, *pre_zeros_end;
1909
/* The pointers where digits at the left of '.' start and stop */
1910
const uchar *int_digits_from, *int_digits_end;
1911
/* The pointers where digits at the right of '.' start and stop */
1912
const uchar *frac_digits_from, *frac_digits_end;
1913
/* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
1914
char expo_sign_char=0;
1915
uint exponent=0; // value of the exponent
1917
Pointers used when digits move from the left of the '.' to the
1918
right of the '.' (explained below)
1920
const uchar *int_digits_tail_from= NULL;
1921
/* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
1922
uint int_digits_added_zeros= 0;
1924
Pointer used when digits move from the right of the '.' to the left
1927
const uchar *frac_digits_head_end= NULL;
1928
/* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
1929
uint frac_digits_added_zeros= 0;
1930
uchar *pos, *tmp_left_pos, *tmp_right_pos;
1931
/* Pointers that are used as limits (begin and end of the field buffer) */
1932
uchar *left_wall, *right_wall;
1935
To remember if table->in_use->cuted_fields has already been incremented,
1936
to do that only once
1938
bool is_cuted_fields_incr=0;
1941
There are three steps in this function :
1942
- parse the input string
1943
- modify the position of digits around the decimal dot '.'
1944
according to the exponent value (if specified)
1945
- write the formatted number
1951
/* skip pre-space */
1952
while (from != end && my_isspace(&my_charset_bin,*from))
1956
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1957
is_cuted_fields_incr=1;
1959
else if (*from == '+' || *from == '-') // Found some sign ?
1963
We allow "+" for unsigned decimal unless defined different
1964
Both options allowed as one may wish not to have "+" for unsigned numbers
1965
because of data processing issues
1971
Field_decimal::overflow(1);
1975
Defining this will not store "+" for unsigned decimal type even if
1976
it is passed in numeric string. This will make some tests to fail
1978
#ifdef DONT_ALLOW_UNSIGNED_PLUS
1985
pre_zeros_from= from;
1986
for (; from!=end && *from == '0'; from++) ; // Read prezeros
1987
pre_zeros_end=int_digits_from=from;
1988
/* Read non zero digits at the left of '.'*/
1989
for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
1990
int_digits_end=from;
1991
if (from!=end && *from == '.') // Some '.' ?
1993
frac_digits_from= from;
1994
/* Read digits at the right of '.' */
1995
for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
1996
frac_digits_end=from;
1997
// Some exponentiation symbol ?
1998
if (from != end && (*from == 'e' || *from == 'E'))
2001
if (from != end && (*from == '+' || *from == '-')) // Some exponent sign ?
2002
expo_sign_char= *from++;
2004
expo_sign_char= '+';
2006
Read digits of the exponent and compute its value. We must care about
2007
'exponent' overflow, because as unsigned arithmetic is "modulo", big
2008
exponents will become small (e.g. 1e4294967296 will become 1e0, and the
2009
field will finally contain 1 instead of its max possible value).
2011
for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
2013
exponent=10*exponent+(*from-'0');
2014
if (exponent>MAX_EXPONENT)
2020
We only have to generate warnings if count_cuted_fields is set.
2021
This is to avoid extra checks of the number when they are not needed.
2022
Even if this flag is not set, it's OK to increment warnings, if
2023
it makes the code easer to read.
2026
if (table->in_use->count_cuted_fields)
2029
for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
2030
if (from != end) // If still something left, warn
2032
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
2033
is_cuted_fields_incr=1;
2038
Now "move" digits around the decimal dot according to the exponent value,
2039
and add necessary zeros.
2041
- 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
2042
- 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
2044
- 1234.5E-3 : '234' moves at the right of '.'
2045
These moves are implemented with pointers which point at the begin
2046
and end of each moved segment. Examples :
2047
- 1234.5E-3 : before the code below is executed, the int_digits part is
2048
from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2049
below, the int_digits part is from '1' to '1', the frac_digits_head
2050
part is from '2' to '4', and the frac_digits part from '5' to '5'.
2051
- 1234.5E3 : before the code below is executed, the int_digits part is
2052
from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2053
below, the int_digits part is from '1' to '4', the int_digits_tail
2054
part is from '5' to '5', the frac_digits part is empty, and
2055
int_digits_added_zeros=2 (to make 1234500).
2059
Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
2060
as int_digits_added_zeros<=exponent<4G and
2061
(int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
2062
(frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
2065
if (!expo_sign_char)
2066
tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2067
else if (expo_sign_char == '-')
2069
tmp_uint=min(exponent,(uint)(int_digits_end-int_digits_from));
2070
frac_digits_added_zeros=exponent-tmp_uint;
2071
int_digits_end -= tmp_uint;
2072
frac_digits_head_end=int_digits_end+tmp_uint;
2073
tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2075
else // (expo_sign_char=='+')
2077
tmp_uint=min(exponent,(uint)(frac_digits_end-frac_digits_from));
2078
int_digits_added_zeros=exponent-tmp_uint;
2079
int_digits_tail_from=frac_digits_from;
2080
frac_digits_from=frac_digits_from+tmp_uint;
2082
We "eat" the heading zeros of the
2083
int_digits.int_digits_tail.int_digits_added_zeros concatenation
2084
(for example 0.003e3 must become 3 and not 0003)
2086
if (int_digits_from == int_digits_end)
2089
There was nothing in the int_digits part, so continue
2090
eating int_digits_tail zeros
2092
for (; int_digits_tail_from != frac_digits_from &&
2093
*int_digits_tail_from == '0'; int_digits_tail_from++) ;
2094
if (int_digits_tail_from == frac_digits_from)
2096
// there were only zeros in int_digits_tail too
2097
int_digits_added_zeros=0;
2100
tmp_uint= (uint) (tmp_dec+(int_digits_end-int_digits_from)+
2101
(uint)(frac_digits_from-int_digits_tail_from)+
2102
int_digits_added_zeros);
2106
Now write the formated number
2108
First the digits of the int_% parts.
2109
Do we have enough room to write these digits ?
2110
If the sign is defined and '-', we need one position for it
2113
if (field_length < tmp_uint + (int) (sign_char == '-'))
2115
// too big number, change to max or min number
2116
Field_decimal::overflow(sign_char == '-');
2121
Tmp_left_pos is the position where the leftmost digit of
2122
the int_% parts will be written
2124
tmp_left_pos=pos=to+(uint)(field_length-tmp_uint);
2126
// Write all digits of the int_% parts
2127
while (int_digits_from != int_digits_end)
2128
*pos++ = *int_digits_from++ ;
2130
if (expo_sign_char == '+')
2132
while (int_digits_tail_from != frac_digits_from)
2133
*pos++= *int_digits_tail_from++;
2134
while (int_digits_added_zeros-- >0)
2138
Note the position where the rightmost digit of the int_% parts has been
2139
written (this is to later check if the int_% parts contained nothing,
2140
meaning an extra 0 is needed).
2145
Step back to the position of the leftmost digit of the int_% parts,
2146
to write sign and fill with zeros or blanks or prezeros.
2152
while (pos > left_wall) // Fill with zeros
2157
left_wall=to+(sign_char != 0)-1;
2158
if (!expo_sign_char) // If exponent was specified, ignore prezeros
2160
for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
2164
if (pos == tmp_right_pos-1)
2165
*pos--= '0'; // no 0 has ever been written, so write one
2167
if (sign_char && pos != left_wall)
2169
/* Write sign if possible (it is if sign is '-') */
2172
while (pos != left_wall)
2173
*pos--=' '; //fill with blanks
2177
Write digits of the frac_% parts ;
2178
Depending on table->in_use->count_cutted_fields, we may also want
2179
to know if some non-zero tail of these parts will
2180
be truncated (for example, 0.002->0.00 will generate a warning,
2181
while 0.000->0.00 will not)
2182
(and 0E1000000000 will not, while 1E-1000000000 will)
2185
pos=to+(uint)(field_length-tmp_dec); // Calculate post to '.'
2186
right_wall=to+field_length;
2187
if (pos != right_wall)
2190
if (expo_sign_char == '-')
2192
while (frac_digits_added_zeros-- > 0)
2194
if (pos == right_wall)
2196
if (table->in_use->count_cuted_fields && !is_cuted_fields_incr)
2197
break; // Go on below to see if we lose non zero digits
2202
while (int_digits_end != frac_digits_head_end)
2204
tmp_char= *int_digits_end++;
2205
if (pos == right_wall)
2207
if (tmp_char != '0') // Losing a non zero digit ?
2209
if (!is_cuted_fields_incr)
2210
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
2211
WARN_DATA_TRUNCATED, 1);
2220
for (;frac_digits_from!=frac_digits_end;)
2222
tmp_char= *frac_digits_from++;
2223
if (pos == right_wall)
2225
if (tmp_char != '0') // Losing a non zero digit ?
2227
if (!is_cuted_fields_incr)
2230
This is a note, not a warning, as we don't want to abort
2231
when we cut decimals in strict mode
2233
set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
2242
while (pos != right_wall)
2243
*pos++='0'; // Fill with zeros at right of '.'
2248
int Field_decimal::store(double nr)
2250
ASSERT_COLUMN_MARKED_FOR_WRITE;
2251
if (unsigned_flag && nr < 0)
2257
if (!isfinite(nr)) // Handle infinity as special case
2266
char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
2268
fyllchar = zerofill ? (char) '0' : (char) ' ';
2269
length= my_fcvt(nr, dec, buff, NULL);
2271
if (length > field_length)
2279
for (i=field_length-length ; i-- > 0 ;)
2281
memcpy(to,buff,length);
2287
int Field_decimal::store(longlong nr, bool unsigned_val)
2289
ASSERT_COLUMN_MARKED_FOR_WRITE;
2291
uint length, int_part;
2295
if (nr < 0 && unsigned_flag && !unsigned_val)
2300
length= (uint) (longlong10_to_str(nr,buff,unsigned_val ? 10 : -10) - buff);
2301
int_part= field_length- (dec ? dec+1 : 0);
2303
if (length > int_part)
2305
overflow(!unsigned_val && nr < 0L); /* purecov: inspected */
2309
fyllchar = zerofill ? (char) '0' : (char) ' ';
2311
for (uint i=int_part-length ; i-- > 0 ;)
2313
memcpy(to,buff,length);
2317
bfill(to+length+1,dec,'0');
2323
double Field_decimal::val_real(void)
2325
ASSERT_COLUMN_MARKED_FOR_READ;
2328
return my_strntod(&my_charset_bin, (char*) ptr, field_length, &end_not_used,
2332
longlong Field_decimal::val_int(void)
2334
ASSERT_COLUMN_MARKED_FOR_READ;
2337
return my_strntoull(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2339
return my_strntoll(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2344
String *Field_decimal::val_str(String *val_buffer __attribute__((unused)),
2347
ASSERT_COLUMN_MARKED_FOR_READ;
2351
for (str=ptr ; *str == ' ' ; str++) ;
2352
val_ptr->set_charset(&my_charset_bin);
2353
tmp_length= (size_t) (str-ptr);
2354
if (field_length < tmp_length) // Error in data
2357
val_ptr->set_ascii((const char*) str, field_length-tmp_length);
2362
Should be able to handle at least the following fixed decimal formats:
2363
5.00 , -1.0, 05, -05, +5 with optional pre/end space
2366
int Field_decimal::cmp(const uchar *a_ptr,const uchar *b_ptr)
2370
/* First remove prefixes '0', ' ', and '-' */
2371
for (end=a_ptr+field_length;
2373
(*a_ptr == *b_ptr ||
2374
((my_isspace(&my_charset_bin,*a_ptr) || *a_ptr == '+' ||
2376
(my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' ||
2380
if (*a_ptr == '-') // If both numbers are negative
2381
swap= -1 ^ 1; // Swap result
2390
while (a_ptr != end)
2392
if (*a_ptr++ != *b_ptr++)
2393
return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1); // compare digits
2399
void Field_decimal::sort_string(uchar *to,uint length)
2402
for (str=ptr,end=ptr+length;
2404
((my_isspace(&my_charset_bin,*str) || *str == '+' ||
2409
return; /* purecov: inspected */
2413
*to++=1; // Smaller than any number
2416
if (my_isdigit(&my_charset_bin,*str))
2417
*to++= (char) ('9' - *str++);
2421
else memcpy(to,str,(uint) (end-str));
2425
void Field_decimal::sql_type(String &res) const
2427
CHARSET_INFO *cs=res.charset();
2428
uint tmp=field_length;
2433
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2434
"decimal(%d,%d)",tmp,dec));
2435
add_zerofill_and_unsigned(res);
2439
/****************************************************************************
2440
** Field_new_decimal
2441
****************************************************************************/
2443
Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
2444
uint32 len_arg, uchar *null_ptr_arg,
2446
enum utype unireg_check_arg,
2447
const char *field_name_arg,
2448
uint8 dec_arg,bool zero_arg,
2450
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2451
unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
2453
precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
2454
set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2455
DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2456
(dec <= DECIMAL_MAX_SCALE));
2457
bin_size= my_decimal_get_binary_size(precision, dec);
2461
Field_new_decimal::Field_new_decimal(uint32 len_arg,
2462
bool maybe_null_arg,
2466
:Field_num((uchar*) 0, len_arg,
2467
maybe_null_arg ? (uchar*) "": 0, 0,
2468
NONE, name, dec_arg, 0, unsigned_arg)
2470
precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
2471
set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2472
DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2473
(dec <= DECIMAL_MAX_SCALE));
2474
bin_size= my_decimal_get_binary_size(precision, dec);
2478
int Field_new_decimal::reset(void)
2480
store_value(&decimal_zero);
2486
Generate max/min decimal value in case of overflow.
2488
@param decimal_value buffer for value
2489
@param sign sign of value which caused overflow
2492
void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
2495
DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
2496
max_my_decimal(decimal_value, precision, decimals());
2500
my_decimal_set_zero(decimal_value);
2502
decimal_value->sign(TRUE);
2509
Store decimal value in the binary buffer.
2511
Checks if decimal_value fits into field size.
2512
If it does, stores the decimal in the buffer using binary format.
2513
Otherwise sets maximal number that can be stored in the field.
2515
@param decimal_value my_decimal
2523
bool Field_new_decimal::store_value(const my_decimal *decimal_value)
2525
ASSERT_COLUMN_MARKED_FOR_WRITE;
2527
DBUG_ENTER("Field_new_decimal::store_value");
2530
char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
2531
DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
2535
/* check that we do not try to write negative value in unsigned field */
2536
if (unsigned_flag && decimal_value->sign())
2538
DBUG_PRINT("info", ("unsigned overflow"));
2539
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2541
decimal_value= &decimal_zero;
2545
char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
2546
DBUG_PRINT("info", ("saving with precision %d scale: %d value %s",
2547
(int)precision, (int)dec,
2548
dbug_decimal_as_string(dbug_buff, decimal_value)));
2552
if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2553
decimal_value, ptr, precision, dec)))
2556
DBUG_PRINT("info", ("overflow"));
2557
set_value_on_overflow(&buff, decimal_value->sign());
2558
my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
2561
DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2567
int Field_new_decimal::store(const char *from, uint length,
2568
CHARSET_INFO *charset_arg)
2570
ASSERT_COLUMN_MARKED_FOR_WRITE;
2572
my_decimal decimal_value;
2573
DBUG_ENTER("Field_new_decimal::store(char*)");
2575
if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
2576
~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
2577
from, length, charset_arg,
2579
table->in_use->abort_on_warning)
2581
/* Because "from" is not NUL-terminated and we use %s in the ER() */
2583
from_as_str.copy(from, length, &my_charset_bin);
2585
push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_ERROR,
2586
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
2587
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2588
"decimal", from_as_str.c_ptr(), field_name,
2589
(ulong) table->in_use->row_count);
2595
case E_DEC_TRUNCATED:
2596
set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
2598
case E_DEC_OVERFLOW:
2599
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2600
set_value_on_overflow(&decimal_value, decimal_value.sign());
2604
/* Because "from" is not NUL-terminated and we use %s in the ER() */
2606
from_as_str.copy(from, length, &my_charset_bin);
2608
push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
2609
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
2610
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2611
"decimal", from_as_str.c_ptr(), field_name,
2612
(ulong) table->in_use->row_count);
2613
my_decimal_set_zero(&decimal_value);
2620
char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
2621
DBUG_PRINT("enter", ("value: %s",
2622
dbug_decimal_as_string(dbug_buff, &decimal_value)));
2624
store_value(&decimal_value);
2631
Fix following when double2my_decimal when double2decimal
2632
will return E_DEC_TRUNCATED always correctly
2635
int Field_new_decimal::store(double nr)
2637
ASSERT_COLUMN_MARKED_FOR_WRITE;
2638
my_decimal decimal_value;
2640
DBUG_ENTER("Field_new_decimal::store(double)");
2642
err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
2646
if (check_overflow(err))
2647
set_value_on_overflow(&decimal_value, decimal_value.sign());
2648
/* Only issue a warning if store_value doesn't issue an warning */
2649
table->in_use->got_warning= 0;
2651
if (store_value(&decimal_value))
2653
else if (err && !table->in_use->got_warning)
2654
err= warn_if_overflow(err);
2659
int Field_new_decimal::store(longlong nr, bool unsigned_val)
2661
ASSERT_COLUMN_MARKED_FOR_WRITE;
2662
my_decimal decimal_value;
2665
if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2666
nr, unsigned_val, &decimal_value)))
2668
if (check_overflow(err))
2669
set_value_on_overflow(&decimal_value, decimal_value.sign());
2670
/* Only issue a warning if store_value doesn't issue an warning */
2671
table->in_use->got_warning= 0;
2673
if (store_value(&decimal_value))
2675
else if (err && !table->in_use->got_warning)
2676
err= warn_if_overflow(err);
2681
int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
2683
ASSERT_COLUMN_MARKED_FOR_WRITE;
2684
return store_value(decimal_value);
2688
int Field_new_decimal::store_time(MYSQL_TIME *ltime, timestamp_type t_type)
2690
my_decimal decimal_value;
2691
return store_value(date2my_decimal(ltime, &decimal_value));
2695
double Field_new_decimal::val_real(void)
2697
ASSERT_COLUMN_MARKED_FOR_READ;
2699
my_decimal decimal_value;
2700
my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
2705
longlong Field_new_decimal::val_int(void)
2707
ASSERT_COLUMN_MARKED_FOR_READ;
2709
my_decimal decimal_value;
2710
my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2716
my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
2718
ASSERT_COLUMN_MARKED_FOR_READ;
2719
DBUG_ENTER("Field_new_decimal::val_decimal");
2720
binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
2722
DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2724
DBUG_RETURN(decimal_value);
2728
String *Field_new_decimal::val_str(String *val_buffer,
2729
String *val_ptr __attribute__((unused)))
2731
ASSERT_COLUMN_MARKED_FOR_READ;
2732
my_decimal decimal_value;
2733
uint fixed_precision= zerofill ? precision : 0;
2734
my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2735
fixed_precision, dec, '0', val_buffer);
2740
int Field_new_decimal::cmp(const uchar *a,const uchar*b)
2742
return memcmp(a, b, bin_size);
2746
void Field_new_decimal::sort_string(uchar *buff,
2747
uint length __attribute__((unused)))
2749
memcpy(buff, ptr, bin_size);
2753
void Field_new_decimal::sql_type(String &str) const
2755
CHARSET_INFO *cs= str.charset();
2756
str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
2757
"decimal(%d,%d)", precision, (int)dec));
2758
add_zerofill_and_unsigned(str);
2763
Save the field metadata for new decimal fields.
2765
Saves the precision in the first byte and decimals() in the second
2766
byte of the field metadata array at index of *metadata_ptr and
2767
*(metadata_ptr + 1).
2769
@param metadata_ptr First byte of field metadata
2771
@returns number of bytes written to metadata_ptr
2773
int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
2775
*metadata_ptr= precision;
2776
*(metadata_ptr + 1)= decimals();
2782
Returns the number of bytes field uses in row-based replication
2785
This method is used in row-based replication to determine the number
2786
of bytes that the field consumes in the row record format. This is
2787
used to skip fields in the master that do not exist on the slave.
2789
@param field_metadata Encoded size in field metadata
2791
@returns The size of the field based on the field metadata.
2793
uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
2795
uint const source_precision= (field_metadata >> 8U) & 0x00ff;
2796
uint const source_decimal= field_metadata & 0x00ff;
2797
uint const source_size= my_decimal_get_binary_size(source_precision,
2799
return (source_size);
2804
Check to see if field size is compatible with destination.
2806
This method is used in row-based replication to verify that the slave's
2807
field size is less than or equal to the master's field size. The
2808
encoded field metadata (from the master or source) is decoded and compared
2809
to the size of this field (the slave or destination).
2811
@param field_metadata Encoded size in field metadata
2813
@retval 0 if this field's size is < the source field's size
2814
@retval 1 if this field's size is >= the source field's size
2816
int Field_new_decimal::compatible_field_size(uint field_metadata)
2819
uint const source_precision= (field_metadata >> 8U) & 0x00ff;
2820
uint const source_decimal= field_metadata & 0x00ff;
2821
uint const source_size= my_decimal_get_binary_size(source_precision,
2823
uint const destination_size= row_pack_length();
2824
compatible= (source_size <= destination_size);
2826
compatible= (source_precision <= precision) &&
2827
(source_decimal <= decimals());
2828
return (compatible);
2832
uint Field_new_decimal::is_equal(Create_field *new_field)
2834
return ((new_field->sql_type == real_type()) &&
2835
((new_field->flags & UNSIGNED_FLAG) ==
2836
(uint) (flags & UNSIGNED_FLAG)) &&
2837
((new_field->flags & AUTO_INCREMENT_FLAG) ==
2838
(uint) (flags & AUTO_INCREMENT_FLAG)) &&
2839
(new_field->length == max_display_length()) &&
2840
(new_field->decimals == dec));
2845
Unpack a decimal field from row data.
2847
This method is used to unpack a decimal or numeric field from a master
2848
whose size of the field is less than that of the slave.
2850
@param to Destination of the data
2851
@param from Source of the data
2852
@param param_data Precision (upper) and decimal (lower) values
2854
@return New pointer into memory based on from + length of the data
2857
Field_new_decimal::unpack(uchar* to,
2860
bool low_byte_first)
2862
if (param_data == 0)
2863
return Field::unpack(to, from, param_data, low_byte_first);
2865
uint from_precision= (param_data & 0xff00) >> 8U;
2866
uint from_decimal= param_data & 0x00ff;
2867
uint length=pack_length();
2868
uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
2869
uint len= (param_data && (from_pack_len < length)) ?
2870
from_pack_len : length;
2871
if ((from_pack_len && (from_pack_len < length)) ||
2872
(from_precision < precision) ||
2873
(from_decimal < decimals()))
2876
If the master's data is smaller than the slave, we need to convert
2877
the binary to decimal then resize the decimal converting it back to
2878
a decimal and write that to the raw data buffer.
2880
decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
2882
dec.len= from_precision;
2885
Note: bin2decimal does not change the length of the field. So it is
2886
just the first step the resizing operation. The second step does the
2887
resizing using the precision and decimals from the slave.
2889
bin2decimal((uchar *)from, &dec, from_precision, from_decimal);
2890
decimal2bin(&dec, to, precision, decimals());
2893
memcpy(to, from, len); // Sizes are the same, just copy the data.
2897
/****************************************************************************
2899
****************************************************************************/
2901
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
2903
ASSERT_COLUMN_MARKED_FOR_WRITE;
2907
error= get_int(cs, from, len, &rnd, 255, -128, 127);
2908
ptr[0]= unsigned_flag ? (char) (uint64_t) rnd : (char) rnd;
2913
int Field_tiny::store(double nr)
2915
ASSERT_COLUMN_MARKED_FOR_WRITE;
2923
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2926
else if (nr > 255.0)
2929
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2940
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2943
else if (nr > 127.0)
2946
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2950
*ptr=(char) (int) nr;
2956
int Field_tiny::store(longlong nr, bool unsigned_val)
2958
ASSERT_COLUMN_MARKED_FOR_WRITE;
2963
if (nr < 0 && !unsigned_val)
2966
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2969
else if ((uint64_t) nr > (uint64_t) 255)
2972
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2980
if (nr < 0 && unsigned_val)
2981
nr= 256; // Generate overflow
2985
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2991
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3001
double Field_tiny::val_real(void)
3003
ASSERT_COLUMN_MARKED_FOR_READ;
3004
int tmp= unsigned_flag ? (int) ptr[0] :
3005
(int) ((signed char*) ptr)[0];
3006
return (double) tmp;
3010
longlong Field_tiny::val_int(void)
3012
ASSERT_COLUMN_MARKED_FOR_READ;
3013
int tmp= unsigned_flag ? (int) ptr[0] :
3014
(int) ((signed char*) ptr)[0];
3015
return (longlong) tmp;
3019
String *Field_tiny::val_str(String *val_buffer,
3020
String *val_ptr __attribute__((unused)))
3022
ASSERT_COLUMN_MARKED_FOR_READ;
3023
CHARSET_INFO *cs= &my_charset_bin;
3025
uint mlength=max(field_length+1,5*cs->mbmaxlen);
3026
val_buffer->alloc(mlength);
3027
char *to=(char*) val_buffer->ptr();
3030
length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
3033
length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
3034
(long) *((signed char*) ptr));
3036
val_buffer->length(length);
3038
prepend_zeros(val_buffer);
3042
bool Field_tiny::send_binary(Protocol *protocol)
3044
return protocol->store_tiny((longlong) (int8) ptr[0]);
3047
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
3050
a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
3052
return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
3053
return (a < b) ? -1 : (a > b) ? 1 : 0;
3056
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
3061
to[0] = (char) (ptr[0] ^ (uchar) 128); /* Revers signbit */
3064
void Field_tiny::sql_type(String &res) const
3066
CHARSET_INFO *cs=res.charset();
3067
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3068
"tinyint(%d)",(int) field_length));
3069
add_zerofill_and_unsigned(res);
3072
/****************************************************************************
3073
Field type short int (2 byte)
3074
****************************************************************************/
3076
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
3078
ASSERT_COLUMN_MARKED_FOR_WRITE;
3083
error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
3084
store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
3085
#ifdef WORDS_BIGENDIAN
3086
if (table->s->db_low_byte_first)
3088
int2store(ptr, store_tmp);
3092
shortstore(ptr, (short) store_tmp);
3097
int Field_short::store(double nr)
3099
ASSERT_COLUMN_MARKED_FOR_WRITE;
3108
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3111
else if (nr > (double) UINT_MAX16)
3113
res=(int16) UINT_MAX16;
3114
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3118
res=(int16) (uint16) nr;
3122
if (nr < (double) INT_MIN16)
3125
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3128
else if (nr > (double) INT_MAX16)
3131
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3135
res=(int16) (int) nr;
3137
#ifdef WORDS_BIGENDIAN
3138
if (table->s->db_low_byte_first)
3144
shortstore(ptr,res);
3149
int Field_short::store(longlong nr, bool unsigned_val)
3151
ASSERT_COLUMN_MARKED_FOR_WRITE;
3157
if (nr < 0L && !unsigned_val)
3160
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3163
else if ((uint64_t) nr > (uint64_t) UINT_MAX16)
3165
res=(int16) UINT_MAX16;
3166
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3170
res=(int16) (uint16) nr;
3174
if (nr < 0 && unsigned_val)
3175
nr= UINT_MAX16+1; // Generate overflow
3180
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3183
else if (nr > (longlong) INT_MAX16)
3186
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3192
#ifdef WORDS_BIGENDIAN
3193
if (table->s->db_low_byte_first)
3199
shortstore(ptr,res);
3204
double Field_short::val_real(void)
3206
ASSERT_COLUMN_MARKED_FOR_READ;
3208
#ifdef WORDS_BIGENDIAN
3209
if (table->s->db_low_byte_first)
3214
return unsigned_flag ? (double) (unsigned short) j : (double) j;
3217
longlong Field_short::val_int(void)
3219
ASSERT_COLUMN_MARKED_FOR_READ;
3221
#ifdef WORDS_BIGENDIAN
3222
if (table->s->db_low_byte_first)
3227
return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
3231
String *Field_short::val_str(String *val_buffer,
3232
String *val_ptr __attribute__((unused)))
3234
ASSERT_COLUMN_MARKED_FOR_READ;
3235
CHARSET_INFO *cs= &my_charset_bin;
3237
uint mlength=max(field_length+1,7*cs->mbmaxlen);
3238
val_buffer->alloc(mlength);
3239
char *to=(char*) val_buffer->ptr();
3241
#ifdef WORDS_BIGENDIAN
3242
if (table->s->db_low_byte_first)
3249
length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10,
3252
length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
3253
val_buffer->length(length);
3255
prepend_zeros(val_buffer);
3260
bool Field_short::send_binary(Protocol *protocol)
3262
return protocol->store_short(Field_short::val_int());
3266
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
3269
#ifdef WORDS_BIGENDIAN
3270
if (table->s->db_low_byte_first)
3283
return ((unsigned short) a < (unsigned short) b) ? -1 :
3284
((unsigned short) a > (unsigned short) b) ? 1 : 0;
3285
return (a < b) ? -1 : (a > b) ? 1 : 0;
3288
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
3290
#ifdef WORDS_BIGENDIAN
3291
if (!table->s->db_low_byte_first)
3296
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
3305
to[0] = (char) (ptr[1] ^ 128); /* Revers signbit */
3310
void Field_short::sql_type(String &res) const
3312
CHARSET_INFO *cs=res.charset();
3313
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3314
"smallint(%d)",(int) field_length));
3315
add_zerofill_and_unsigned(res);
3319
/****************************************************************************
3320
Field type medium int (3 byte)
3321
****************************************************************************/
3323
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
3325
ASSERT_COLUMN_MARKED_FOR_WRITE;
3330
error= get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
3331
store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
3332
int3store(ptr, store_tmp);
3337
int Field_medium::store(double nr)
3339
ASSERT_COLUMN_MARKED_FOR_WRITE;
3347
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3350
else if (nr >= (double) (long) (1L << 24))
3352
uint32 tmp=(uint32) (1L << 24)-1L;
3354
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3358
int3store(ptr,(uint32) nr);
3362
if (nr < (double) INT_MIN24)
3364
long tmp=(long) INT_MIN24;
3366
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3369
else if (nr > (double) INT_MAX24)
3371
long tmp=(long) INT_MAX24;
3373
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3377
int3store(ptr,(long) nr);
3383
int Field_medium::store(longlong nr, bool unsigned_val)
3385
ASSERT_COLUMN_MARKED_FOR_WRITE;
3390
if (nr < 0 && !unsigned_val)
3393
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3396
else if ((uint64_t) nr >= (uint64_t) (long) (1L << 24))
3398
long tmp= (long) (1L << 24)-1L;
3400
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3404
int3store(ptr,(uint32) nr);
3408
if (nr < 0 && unsigned_val)
3409
nr= (uint64_t) (long) (1L << 24); // Generate overflow
3411
if (nr < (longlong) INT_MIN24)
3413
long tmp= (long) INT_MIN24;
3415
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3418
else if (nr > (longlong) INT_MAX24)
3420
long tmp=(long) INT_MAX24;
3422
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3426
int3store(ptr,(long) nr);
3432
double Field_medium::val_real(void)
3434
ASSERT_COLUMN_MARKED_FOR_READ;
3435
long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3440
longlong Field_medium::val_int(void)
3442
ASSERT_COLUMN_MARKED_FOR_READ;
3443
long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3444
return (longlong) j;
3448
String *Field_medium::val_str(String *val_buffer,
3449
String *val_ptr __attribute__((unused)))
3451
ASSERT_COLUMN_MARKED_FOR_READ;
3452
CHARSET_INFO *cs= &my_charset_bin;
3454
uint mlength=max(field_length+1,10*cs->mbmaxlen);
3455
val_buffer->alloc(mlength);
3456
char *to=(char*) val_buffer->ptr();
3457
long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3459
length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
3460
val_buffer->length(length);
3462
prepend_zeros(val_buffer); /* purecov: inspected */
3467
bool Field_medium::send_binary(Protocol *protocol)
3469
ASSERT_COLUMN_MARKED_FOR_READ;
3470
return protocol->store_long(Field_medium::val_int());
3474
int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr)
3487
return (a < b) ? -1 : (a > b) ? 1 : 0;
3490
void Field_medium::sort_string(uchar *to,uint length __attribute__((unused)))
3495
to[0] = (uchar) (ptr[2] ^ 128); /* Revers signbit */
3501
void Field_medium::sql_type(String &res) const
3503
CHARSET_INFO *cs=res.charset();
3504
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3505
"mediumint(%d)",(int) field_length));
3506
add_zerofill_and_unsigned(res);
3509
/****************************************************************************
3511
****************************************************************************/
3513
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
3515
ASSERT_COLUMN_MARKED_FOR_WRITE;
3520
error= get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
3521
store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
3522
#ifdef WORDS_BIGENDIAN
3523
if (table->s->db_low_byte_first)
3525
int4store(ptr, store_tmp);
3529
longstore(ptr, store_tmp);
3534
int Field_long::store(double nr)
3536
ASSERT_COLUMN_MARKED_FOR_WRITE;
3547
else if (nr > (double) UINT_MAX32)
3550
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3554
res=(int32) (ulong) nr;
3558
if (nr < (double) INT_MIN32)
3560
res=(int32) INT_MIN32;
3563
else if (nr > (double) INT_MAX32)
3565
res=(int32) INT_MAX32;
3569
res=(int32) (longlong) nr;
3572
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3574
#ifdef WORDS_BIGENDIAN
3575
if (table->s->db_low_byte_first)
3586
int Field_long::store(longlong nr, bool unsigned_val)
3588
ASSERT_COLUMN_MARKED_FOR_WRITE;
3594
if (nr < 0 && !unsigned_val)
3599
else if ((uint64_t) nr >= (LL(1) << 32))
3601
res=(int32) (uint32) ~0L;
3605
res=(int32) (uint32) nr;
3609
if (nr < 0 && unsigned_val)
3610
nr= ((longlong) INT_MAX32) + 1; // Generate overflow
3611
if (nr < (longlong) INT_MIN32)
3613
res=(int32) INT_MIN32;
3616
else if (nr > (longlong) INT_MAX32)
3618
res=(int32) INT_MAX32;
3625
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3627
#ifdef WORDS_BIGENDIAN
3628
if (table->s->db_low_byte_first)
3639
double Field_long::val_real(void)
3641
ASSERT_COLUMN_MARKED_FOR_READ;
3643
#ifdef WORDS_BIGENDIAN
3644
if (table->s->db_low_byte_first)
3649
return unsigned_flag ? (double) (uint32) j : (double) j;
3652
longlong Field_long::val_int(void)
3654
ASSERT_COLUMN_MARKED_FOR_READ;
3656
/* See the comment in Field_long::store(long long) */
3657
DBUG_ASSERT(table->in_use == current_thd);
3658
#ifdef WORDS_BIGENDIAN
3659
if (table->s->db_low_byte_first)
3664
return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
3667
String *Field_long::val_str(String *val_buffer,
3668
String *val_ptr __attribute__((unused)))
3670
ASSERT_COLUMN_MARKED_FOR_READ;
3671
CHARSET_INFO *cs= &my_charset_bin;
3673
uint mlength=max(field_length+1,12*cs->mbmaxlen);
3674
val_buffer->alloc(mlength);
3675
char *to=(char*) val_buffer->ptr();
3677
#ifdef WORDS_BIGENDIAN
3678
if (table->s->db_low_byte_first)
3685
length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
3687
length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
3688
val_buffer->length(length);
3690
prepend_zeros(val_buffer);
3695
bool Field_long::send_binary(Protocol *protocol)
3697
ASSERT_COLUMN_MARKED_FOR_READ;
3698
return protocol->store_long(Field_long::val_int());
3701
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
3704
#ifdef WORDS_BIGENDIAN
3705
if (table->s->db_low_byte_first)
3717
return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
3718
return (a < b) ? -1 : (a > b) ? 1 : 0;
3721
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
3723
#ifdef WORDS_BIGENDIAN
3724
if (!table->s->db_low_byte_first)
3729
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
3740
to[0] = (char) (ptr[3] ^ 128); /* Revers signbit */
3748
void Field_long::sql_type(String &res) const
3750
CHARSET_INFO *cs=res.charset();
3751
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3752
"int(%d)",(int) field_length));
3753
add_zerofill_and_unsigned(res);
3756
/****************************************************************************
3757
Field type longlong int (8 bytes)
3758
****************************************************************************/
3760
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
3762
ASSERT_COLUMN_MARKED_FOR_WRITE;
3767
tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
3768
if (error == MY_ERRNO_ERANGE)
3770
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3773
else if (table->in_use->count_cuted_fields &&
3774
check_int(cs, from, len, end, error))
3778
#ifdef WORDS_BIGENDIAN
3779
if (table->s->db_low_byte_first)
3785
longlongstore(ptr,tmp);
3790
int Field_longlong::store(double nr)
3792
ASSERT_COLUMN_MARKED_FOR_WRITE;
3804
else if (nr >= (double) ULONGLONG_MAX)
3810
res=(longlong) (uint64_t) nr;
3814
if (nr <= (double) LONGLONG_MIN)
3817
error= (nr < (double) LONGLONG_MIN);
3819
else if (nr >= (double) (uint64_t) LONGLONG_MAX)
3822
error= (nr > (double) LONGLONG_MAX);
3828
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3830
#ifdef WORDS_BIGENDIAN
3831
if (table->s->db_low_byte_first)
3837
longlongstore(ptr,res);
3842
int Field_longlong::store(longlong nr, bool unsigned_val)
3844
ASSERT_COLUMN_MARKED_FOR_WRITE;
3847
if (nr < 0) // Only possible error
3850
if field is unsigned and value is signed (< 0) or
3851
if field is signed and value is unsigned we have an overflow
3853
if (unsigned_flag != unsigned_val)
3855
nr= unsigned_flag ? (uint64_t) 0 : (uint64_t) LONGLONG_MAX;
3856
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3861
#ifdef WORDS_BIGENDIAN
3862
if (table->s->db_low_byte_first)
3868
longlongstore(ptr,nr);
3873
double Field_longlong::val_real(void)
3875
ASSERT_COLUMN_MARKED_FOR_READ;
3877
#ifdef WORDS_BIGENDIAN
3878
if (table->s->db_low_byte_first)
3885
/* The following is open coded to avoid a bug in gcc 3.3 */
3888
uint64_t tmp= (uint64_t) j;
3889
return ulonglong2double(tmp);
3895
longlong Field_longlong::val_int(void)
3897
ASSERT_COLUMN_MARKED_FOR_READ;
3899
#ifdef WORDS_BIGENDIAN
3900
if (table->s->db_low_byte_first)
3909
String *Field_longlong::val_str(String *val_buffer,
3910
String *val_ptr __attribute__((unused)))
3912
CHARSET_INFO *cs= &my_charset_bin;
3914
uint mlength=max(field_length+1,22*cs->mbmaxlen);
3915
val_buffer->alloc(mlength);
3916
char *to=(char*) val_buffer->ptr();
3918
#ifdef WORDS_BIGENDIAN
3919
if (table->s->db_low_byte_first)
3925
length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
3926
unsigned_flag ? 10 : -10, j);
3927
val_buffer->length(length);
3929
prepend_zeros(val_buffer);
3934
bool Field_longlong::send_binary(Protocol *protocol)
3936
ASSERT_COLUMN_MARKED_FOR_READ;
3937
return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
3941
int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
3944
#ifdef WORDS_BIGENDIAN
3945
if (table->s->db_low_byte_first)
3953
longlongget(a,a_ptr);
3954
longlongget(b,b_ptr);
3957
return ((uint64_t) a < (uint64_t) b) ? -1 :
3958
((uint64_t) a > (uint64_t) b) ? 1 : 0;
3959
return (a < b) ? -1 : (a > b) ? 1 : 0;
3962
void Field_longlong::sort_string(uchar *to,uint length __attribute__((unused)))
3964
#ifdef WORDS_BIGENDIAN
3965
if (!table->s->db_low_byte_first)
3970
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
3985
to[0] = (char) (ptr[7] ^ 128); /* Revers signbit */
3997
void Field_longlong::sql_type(String &res) const
3999
CHARSET_INFO *cs=res.charset();
4000
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4001
"bigint(%d)",(int) field_length));
4002
add_zerofill_and_unsigned(res);
4007
Floating-point numbers
4011
Field_real::pack(uchar *to, const uchar *from,
4012
uint max_length, bool low_byte_first)
4014
DBUG_ENTER("Field_real::pack");
4015
DBUG_ASSERT(max_length >= pack_length());
4016
DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
4017
#ifdef WORDS_BIGENDIAN
4018
if (low_byte_first != table->s->db_low_byte_first)
4020
const uchar *dptr= from + pack_length();
4021
while (dptr-- > from)
4027
DBUG_RETURN(Field::pack(to, from, max_length, low_byte_first));
4031
Field_real::unpack(uchar *to, const uchar *from,
4032
uint param_data, bool low_byte_first)
4034
DBUG_ENTER("Field_real::unpack");
4035
DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
4036
#ifdef WORDS_BIGENDIAN
4037
if (low_byte_first != table->s->db_low_byte_first)
4039
const uchar *dptr= from + pack_length();
4040
while (dptr-- > from)
4042
DBUG_RETURN(from + pack_length());
4046
DBUG_RETURN(Field::unpack(to, from, param_data, low_byte_first));
4049
/****************************************************************************
4050
single precision float
4051
****************************************************************************/
4053
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
4057
double nr= my_strntod(cs,(char*) from,len,&end,&error);
4058
if (error || (!len || (((uint)(end-from) != len) && table->in_use->count_cuted_fields)))
4060
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4061
(error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
4062
error= error ? 1 : 2;
4064
Field_float::store(nr);
4069
int Field_float::store(double nr)
4071
ASSERT_COLUMN_MARKED_FOR_WRITE;
4072
int error= truncate(&nr, FLT_MAX);
4075
#ifdef WORDS_BIGENDIAN
4076
if (table->s->db_low_byte_first)
4082
memcpy_fixed(ptr,(uchar*) &j,sizeof(j));
4087
int Field_float::store(longlong nr, bool unsigned_val)
4089
return Field_float::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
4094
double Field_float::val_real(void)
4096
ASSERT_COLUMN_MARKED_FOR_READ;
4098
#ifdef WORDS_BIGENDIAN
4099
if (table->s->db_low_byte_first)
4105
memcpy_fixed((uchar*) &j,ptr,sizeof(j));
4106
return ((double) j);
4109
longlong Field_float::val_int(void)
4112
#ifdef WORDS_BIGENDIAN
4113
if (table->s->db_low_byte_first)
4119
memcpy_fixed((uchar*) &j,ptr,sizeof(j));
4120
return (longlong) rint(j);
4124
String *Field_float::val_str(String *val_buffer,
4125
String *val_ptr __attribute__((unused)))
4127
ASSERT_COLUMN_MARKED_FOR_READ;
4129
#ifdef WORDS_BIGENDIAN
4130
if (table->s->db_low_byte_first)
4136
memcpy_fixed((uchar*) &nr,ptr,sizeof(nr));
4138
uint to_length=max(field_length,70);
4139
val_buffer->alloc(to_length);
4140
char *to=(char*) val_buffer->ptr();
4143
if (dec >= NOT_FIXED_DEC)
4144
len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
4148
We are safe here because the buffer length is >= 70, and
4149
fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
4150
will be not longer than 69 chars + terminating '\0'.
4152
len= my_fcvt(nr, dec, to, NULL);
4154
val_buffer->length((uint) len);
4156
prepend_zeros(val_buffer);
4161
int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
4164
#ifdef WORDS_BIGENDIAN
4165
if (table->s->db_low_byte_first)
4173
memcpy_fixed(&a,a_ptr,sizeof(float));
4174
memcpy_fixed(&b,b_ptr,sizeof(float));
4176
return (a < b) ? -1 : (a > b) ? 1 : 0;
4179
#define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
4181
void Field_float::sort_string(uchar *to,uint length __attribute__((unused)))
4184
#ifdef WORDS_BIGENDIAN
4185
if (table->s->db_low_byte_first)
4191
memcpy_fixed(&nr,ptr,sizeof(float));
4194
if (nr == (float) 0.0)
4195
{ /* Change to zero string */
4197
bzero((char*) tmp+1,sizeof(nr)-1);
4201
#ifdef WORDS_BIGENDIAN
4202
memcpy_fixed(tmp,&nr,sizeof(nr));
4204
tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
4206
if (tmp[0] & 128) /* Negative */
4207
{ /* make complement */
4209
for (i=0 ; i < sizeof(nr); i++)
4210
tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
4214
ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
4216
exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
4217
tmp[0]= (uchar) (exp_part >> 8);
4218
tmp[1]= (uchar) exp_part;
4224
bool Field_float::send_binary(Protocol *protocol)
4226
ASSERT_COLUMN_MARKED_FOR_READ;
4227
return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
4232
Save the field metadata for float fields.
4234
Saves the pack length in the first byte.
4236
@param metadata_ptr First byte of field metadata
4238
@returns number of bytes written to metadata_ptr
4240
int Field_float::do_save_field_metadata(uchar *metadata_ptr)
4242
*metadata_ptr= pack_length();
4247
void Field_float::sql_type(String &res) const
4249
if (dec == NOT_FIXED_DEC)
4251
res.set_ascii(STRING_WITH_LEN("float"));
4255
CHARSET_INFO *cs= res.charset();
4256
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4257
"float(%d,%d)",(int) field_length,dec));
4259
add_zerofill_and_unsigned(res);
4263
/****************************************************************************
4264
double precision floating point numbers
4265
****************************************************************************/
4267
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
4271
double nr= my_strntod(cs,(char*) from, len, &end, &error);
4272
if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
4274
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4275
(error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
4276
error= error ? 1 : 2;
4278
Field_double::store(nr);
4283
int Field_double::store(double nr)
4285
ASSERT_COLUMN_MARKED_FOR_WRITE;
4286
int error= truncate(&nr, DBL_MAX);
4288
#ifdef WORDS_BIGENDIAN
4289
if (table->s->db_low_byte_first)
4291
float8store(ptr,nr);
4295
doublestore(ptr,nr);
4300
int Field_double::store(longlong nr, bool unsigned_val)
4302
return Field_double::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
4307
If a field has fixed length, truncate the double argument pointed to by 'nr'
4309
Also ensure that the argument is within [-max_value; max_value] range.
4312
int Field_real::truncate(double *nr, double max_value)
4321
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4324
else if (unsigned_flag && res < 0)
4327
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4333
uint order= field_length - dec;
4334
uint step= array_elements(log_10) - 1;
4336
for (; order > step; order-= step)
4337
max_value*= log_10[step];
4338
max_value*= log_10[order];
4339
max_value-= 1.0 / log_10[dec];
4341
/* Check for infinity so we don't get NaN in calculations */
4344
double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
4345
res= floor(res) + tmp;
4349
if (res < -max_value)
4352
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4354
else if (res > max_value)
4357
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4368
int Field_real::store_decimal(const my_decimal *dm)
4371
my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
4375
double Field_double::val_real(void)
4377
ASSERT_COLUMN_MARKED_FOR_READ;
4379
#ifdef WORDS_BIGENDIAN
4380
if (table->s->db_low_byte_first)
4390
longlong Field_double::val_int(void)
4392
ASSERT_COLUMN_MARKED_FOR_READ;
4395
#ifdef WORDS_BIGENDIAN
4396
if (table->s->db_low_byte_first)
4403
/* Check whether we fit into longlong range */
4404
if (j <= (double) LONGLONG_MIN)
4406
res= (longlong) LONGLONG_MIN;
4409
if (j >= (double) (uint64_t) LONGLONG_MAX)
4411
res= (longlong) LONGLONG_MAX;
4414
return (longlong) rint(j);
4418
char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4419
String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
4420
str= val_str(&tmp, 0);
4421
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
4422
ER_TRUNCATED_WRONG_VALUE,
4423
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
4430
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
4432
ASSERT_COLUMN_MARKED_FOR_READ;
4433
double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
4434
return decimal_value;
4438
String *Field_double::val_str(String *val_buffer,
4439
String *val_ptr __attribute__((unused)))
4441
ASSERT_COLUMN_MARKED_FOR_READ;
4443
#ifdef WORDS_BIGENDIAN
4444
if (table->s->db_low_byte_first)
4452
uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
4453
val_buffer->alloc(to_length);
4454
char *to=(char*) val_buffer->ptr();
4457
if (dec >= NOT_FIXED_DEC)
4458
len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
4460
len= my_fcvt(nr, dec, to, NULL);
4462
val_buffer->length((uint) len);
4464
prepend_zeros(val_buffer);
4468
bool Field_double::send_binary(Protocol *protocol)
4470
return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
4474
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
4476
ASSERT_COLUMN_MARKED_FOR_READ;
4478
#ifdef WORDS_BIGENDIAN
4479
if (table->s->db_low_byte_first)
4487
doubleget(a, a_ptr);
4488
doubleget(b, b_ptr);
4490
return (a < b) ? -1 : (a > b) ? 1 : 0;
4494
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
4496
/* The following should work for IEEE */
4498
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
4501
#ifdef WORDS_BIGENDIAN
4502
if (table->s->db_low_byte_first)
4509
change_double_for_sort(nr, to);
4514
Save the field metadata for double fields.
4516
Saves the pack length in the first byte of the field metadata array
4517
at index of *metadata_ptr.
4519
@param metadata_ptr First byte of field metadata
4521
@returns number of bytes written to metadata_ptr
4523
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
4525
*metadata_ptr= pack_length();
4530
void Field_double::sql_type(String &res) const
4532
CHARSET_INFO *cs=res.charset();
4533
if (dec == NOT_FIXED_DEC)
4535
res.set_ascii(STRING_WITH_LEN("double"));
4539
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4540
"double(%d,%d)",(int) field_length,dec));
4542
add_zerofill_and_unsigned(res);
4547
TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
4548
2038-01-01 00:00:00 UTC stored as number of seconds since Unix
4551
Up to one of timestamps columns in the table can be automatically
4552
set on row update and/or have NOW() as default value.
4553
TABLE::timestamp_field points to Field object for such timestamp with
4554
auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
4555
field, and is used by handler code which performs updates required.
4557
Actually SQL-99 says that we should allow niladic functions (like NOW())
4558
as defaults for any field. Current limitations (only NOW() and only
4559
for one TIMESTAMP field) are because of restricted binary .frm format
4560
and should go away in the future.
4562
Also because of this limitation of binary .frm format we use 5 different
4563
unireg_check values with TIMESTAMP field to distinguish various cases of
4564
DEFAULT or ON UPDATE values. These values are:
4566
TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
4567
auto-set-on-update (or now() as default) in this table before, then this
4568
field has NOW() as default and is updated when row changes, else it is
4569
field which has 0 as default value and is not automatically updated.
4570
TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
4571
automatically (TIMESTAMP DEFAULT NOW())
4572
TIMESTAMP_UN_FIELD - field which is set on update automatically but has not
4573
NOW() as default (but it may has 0 or some other const timestamp as
4574
default) (TIMESTAMP ON UPDATE NOW()).
4575
TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on
4576
update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
4577
NONE - field which is not auto-set on update with some other than NOW()
4578
default value (TIMESTAMP DEFAULT 0).
4580
Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are
4581
left only for preserving ability to read old tables. Such fields replaced
4582
with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is
4583
because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for
4584
"TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such
4585
specification too but ignored default value for first timestamp, which of
4586
course is non-standard.) In most cases user won't notice any change, only
4587
exception is different behavior of old/new timestamps during ALTER TABLE.
4590
Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
4591
uchar *null_ptr_arg, uchar null_bit_arg,
4592
enum utype unireg_check_arg,
4593
const char *field_name_arg,
4596
:Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
4597
unireg_check_arg, field_name_arg, cs)
4599
/* For 4.0 MYD and 4.0 InnoDB compatibility */
4600
flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
4601
if (!share->timestamp_field && unireg_check != NONE)
4603
/* This timestamp has auto-update */
4604
share->timestamp_field= this;
4605
flags|= TIMESTAMP_FLAG;
4606
if (unireg_check != TIMESTAMP_DN_FIELD)
4607
flags|= ON_UPDATE_NOW_FLAG;
4612
Field_timestamp::Field_timestamp(bool maybe_null_arg,
4613
const char *field_name_arg,
4615
:Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
4616
maybe_null_arg ? (uchar*) "": 0, 0,
4617
NONE, field_name_arg, cs)
4619
/* For 4.0 MYD and 4.0 InnoDB compatibility */
4620
flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
4621
if (unireg_check != TIMESTAMP_DN_FIELD)
4622
flags|= ON_UPDATE_NOW_FLAG;
4627
Get auto-set type for TIMESTAMP field.
4629
Returns value indicating during which operations this TIMESTAMP field
4630
should be auto-set to current timestamp.
4632
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
4634
switch (unireg_check)
4636
case TIMESTAMP_DN_FIELD:
4637
return TIMESTAMP_AUTO_SET_ON_INSERT;
4638
case TIMESTAMP_UN_FIELD:
4639
return TIMESTAMP_AUTO_SET_ON_UPDATE;
4640
case TIMESTAMP_OLD_FIELD:
4642
Although we can have several such columns in legacy tables this
4643
function should be called only for first of them (i.e. the one
4644
having auto-set property).
4646
DBUG_ASSERT(table->timestamp_field == this);
4648
case TIMESTAMP_DNUN_FIELD:
4649
return TIMESTAMP_AUTO_SET_ON_BOTH;
4652
Normally this function should not be called for TIMESTAMPs without
4656
return TIMESTAMP_NO_AUTO_SET;
4661
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
4663
ASSERT_COLUMN_MARKED_FOR_WRITE;
4667
bool have_smth_to_conv;
4668
my_bool in_dst_time_gap;
4669
THD *thd= table ? table->in_use : current_thd;
4671
/* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4672
have_smth_to_conv= (str_to_datetime(from, len, &l_time, 1, &error) >
4673
MYSQL_TIMESTAMP_ERROR);
4675
if (error || !have_smth_to_conv)
4678
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4679
from, len, MYSQL_TIMESTAMP_DATETIME, 1);
4682
/* Only convert a correct date (not a zero date) */
4683
if (have_smth_to_conv && l_time.month)
4685
if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
4687
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4688
ER_WARN_DATA_OUT_OF_RANGE,
4689
from, len, MYSQL_TIMESTAMP_DATETIME, !error);
4692
else if (in_dst_time_gap)
4694
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4695
ER_WARN_INVALID_TIMESTAMP,
4696
from, len, MYSQL_TIMESTAMP_DATETIME, !error);
4700
store_timestamp(tmp);
4705
int Field_timestamp::store(double nr)
4708
if (nr < 0 || nr > 99991231235959.0)
4710
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4711
ER_WARN_DATA_OUT_OF_RANGE,
4712
nr, MYSQL_TIMESTAMP_DATETIME);
4713
nr= 0; // Avoid overflow on buff
4716
error|= Field_timestamp::store((longlong) rint(nr), FALSE);
4721
int Field_timestamp::store(longlong nr, bool unsigned_val)
4723
ASSERT_COLUMN_MARKED_FOR_WRITE;
4725
my_time_t timestamp= 0;
4727
my_bool in_dst_time_gap;
4728
THD *thd= table ? table->in_use : current_thd;
4730
/* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4731
longlong tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
4732
MODE_NO_ZERO_DATE) |
4733
MODE_NO_ZERO_IN_DATE, &error);
4741
if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
4743
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4744
ER_WARN_DATA_OUT_OF_RANGE,
4745
nr, MYSQL_TIMESTAMP_DATETIME, 1);
4748
if (in_dst_time_gap)
4750
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4751
ER_WARN_INVALID_TIMESTAMP,
4752
nr, MYSQL_TIMESTAMP_DATETIME, 1);
4756
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4757
WARN_DATA_TRUNCATED,
4758
nr, MYSQL_TIMESTAMP_DATETIME, 1);
4760
store_timestamp(timestamp);
4764
double Field_timestamp::val_real(void)
4766
ASSERT_COLUMN_MARKED_FOR_READ;
4767
return (double) Field_timestamp::val_int();
4770
longlong Field_timestamp::val_int(void)
4772
ASSERT_COLUMN_MARKED_FOR_READ;
4774
MYSQL_TIME time_tmp;
4775
THD *thd= table ? table->in_use : current_thd;
4777
thd->time_zone_used= 1;
4778
#ifdef WORDS_BIGENDIAN
4779
if (table && table->s->db_low_byte_first)
4780
temp=uint4korr(ptr);
4785
if (temp == 0L) // No time
4786
return(0); /* purecov: inspected */
4788
thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
4790
return time_tmp.year * LL(10000000000) + time_tmp.month * LL(100000000) +
4791
time_tmp.day * 1000000L + time_tmp.hour * 10000L +
4792
time_tmp.minute * 100 + time_tmp.second;
4796
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
4798
ASSERT_COLUMN_MARKED_FOR_READ;
4800
MYSQL_TIME time_tmp;
4801
THD *thd= table ? table->in_use : current_thd;
4804
val_buffer->alloc(field_length+1);
4805
to= (char*) val_buffer->ptr();
4806
val_buffer->length(field_length);
4808
thd->time_zone_used= 1;
4809
#ifdef WORDS_BIGENDIAN
4810
if (table && table->s->db_low_byte_first)
4811
temp=uint4korr(ptr);
4817
{ /* Zero time is "000000" */
4818
val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_bin);
4821
val_buffer->set_charset(&my_charset_bin); // Safety
4823
thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
4825
temp= time_tmp.year % 100;
4826
if (temp < YY_PART_YEAR - 1)
4836
temp2=temp/10; temp=temp-temp2*10;
4837
*to++= (char) ('0'+(char) (temp2));
4838
*to++= (char) ('0'+(char) (temp));
4840
temp=time_tmp.month;
4841
temp2=temp/10; temp=temp-temp2*10;
4842
*to++= (char) ('0'+(char) (temp2));
4843
*to++= (char) ('0'+(char) (temp));
4846
temp2=temp/10; temp=temp-temp2*10;
4847
*to++= (char) ('0'+(char) (temp2));
4848
*to++= (char) ('0'+(char) (temp));
4851
temp2=temp/10; temp=temp-temp2*10;
4852
*to++= (char) ('0'+(char) (temp2));
4853
*to++= (char) ('0'+(char) (temp));
4855
temp=time_tmp.minute;
4856
temp2=temp/10; temp=temp-temp2*10;
4857
*to++= (char) ('0'+(char) (temp2));
4858
*to++= (char) ('0'+(char) (temp));
4860
temp=time_tmp.second;
4861
temp2=temp/10; temp=temp-temp2*10;
4862
*to++= (char) ('0'+(char) (temp2));
4863
*to++= (char) ('0'+(char) (temp));
4869
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
4872
THD *thd= table ? table->in_use : current_thd;
4873
thd->time_zone_used= 1;
4874
#ifdef WORDS_BIGENDIAN
4875
if (table && table->s->db_low_byte_first)
4876
temp=uint4korr(ptr);
4881
{ /* Zero time is "000000" */
4882
if (fuzzydate & TIME_NO_ZERO_DATE)
4884
bzero((char*) ltime,sizeof(*ltime));
4888
thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
4893
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
4895
return Field_timestamp::get_date(ltime,0);
4899
bool Field_timestamp::send_binary(Protocol *protocol)
4902
Field_timestamp::get_date(&tm, 0);
4903
return protocol->store(&tm);
4907
int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
4910
#ifdef WORDS_BIGENDIAN
4911
if (table && table->s->db_low_byte_first)
4922
return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
4926
void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
4928
#ifdef WORDS_BIGENDIAN
4929
if (!table || !table->s->db_low_byte_first)
4947
void Field_timestamp::sql_type(String &res) const
4949
res.set_ascii(STRING_WITH_LEN("timestamp"));
4953
void Field_timestamp::set_time()
4955
THD *thd= table ? table->in_use : current_thd;
4956
long tmp= (long) thd->query_start();
4958
store_timestamp(tmp);
4961
/****************************************************************************
4963
** In string context: HH:MM:SS
4964
** In number context: HHMMSS
4965
** Stored as a 3 byte unsigned int
4966
****************************************************************************/
4968
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
4975
if (str_to_time(from, len, <ime, &warning))
4979
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4980
from, len, MYSQL_TIMESTAMP_TIME, 1);
4984
if (warning & MYSQL_TIME_WARN_TRUNCATED)
4986
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4987
WARN_DATA_TRUNCATED,
4988
from, len, MYSQL_TIMESTAMP_TIME, 1);
4991
if (warning & MYSQL_TIME_WARN_OUT_OF_RANGE)
4993
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4994
ER_WARN_DATA_OUT_OF_RANGE,
4995
from, len, MYSQL_TIMESTAMP_TIME, !error);
5000
tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
5010
int Field_time::store_time(MYSQL_TIME *ltime, timestamp_type time_type)
5012
long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
5013
(ltime->minute * 100 + ltime->second);
5016
return Field_time::store((longlong) tmp, FALSE);
5020
int Field_time::store(double nr)
5022
ASSERT_COLUMN_MARKED_FOR_WRITE;
5025
if (nr > (double)TIME_MAX_VALUE)
5027
tmp= TIME_MAX_VALUE;
5028
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5029
ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
5032
else if (nr < (double)-TIME_MAX_VALUE)
5034
tmp= -TIME_MAX_VALUE;
5035
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5036
ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
5041
tmp=(long) floor(fabs(nr)); // Remove fractions
5044
if (tmp % 100 > 59 || tmp/100 % 100 > 59)
5047
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5048
ER_WARN_DATA_OUT_OF_RANGE, nr,
5049
MYSQL_TIMESTAMP_TIME);
5058
int Field_time::store(longlong nr, bool unsigned_val)
5060
ASSERT_COLUMN_MARKED_FOR_WRITE;
5063
if (nr < (longlong) -TIME_MAX_VALUE && !unsigned_val)
5065
tmp= -TIME_MAX_VALUE;
5066
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5067
ER_WARN_DATA_OUT_OF_RANGE, nr,
5068
MYSQL_TIMESTAMP_TIME, 1);
5071
else if (nr > (longlong) TIME_MAX_VALUE || (nr < 0 && unsigned_val))
5073
tmp= TIME_MAX_VALUE;
5074
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5075
ER_WARN_DATA_OUT_OF_RANGE, nr,
5076
MYSQL_TIMESTAMP_TIME, 1);
5082
if (tmp % 100 > 59 || tmp/100 % 100 > 59)
5085
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5086
ER_WARN_DATA_OUT_OF_RANGE, nr,
5087
MYSQL_TIMESTAMP_TIME, 1);
5096
double Field_time::val_real(void)
5098
ASSERT_COLUMN_MARKED_FOR_READ;
5099
uint32 j= (uint32) uint3korr(ptr);
5103
longlong Field_time::val_int(void)
5105
ASSERT_COLUMN_MARKED_FOR_READ;
5106
return (longlong) sint3korr(ptr);
5112
This function is multi-byte safe as the result string is always of type
5116
String *Field_time::val_str(String *val_buffer,
5117
String *val_ptr __attribute__((unused)))
5119
ASSERT_COLUMN_MARKED_FOR_READ;
5121
val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
5122
long tmp=(long) sint3korr(ptr);
5129
ltime.day= (uint) 0;
5130
ltime.hour= (uint) (tmp/10000);
5131
ltime.minute= (uint) (tmp/100 % 100);
5132
ltime.second= (uint) (tmp % 100);
5133
make_time((DATE_TIME_FORMAT*) 0, <ime, val_buffer);
5140
Normally we would not consider 'time' as a valid date, but we allow
5141
get_date() here to be able to do things like
5142
DATE_FORMAT(time, "%l.%i %p")
5145
bool Field_time::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5148
THD *thd= table ? table->in_use : current_thd;
5149
if (!(fuzzydate & TIME_FUZZY_DATE))
5151
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
5152
ER_WARN_DATA_OUT_OF_RANGE,
5153
ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
5157
tmp=(long) sint3korr(ptr);
5164
ltime->hour=tmp/10000;
5165
tmp-=ltime->hour*10000;
5166
ltime->minute= tmp/100;
5167
ltime->second= tmp % 100;
5168
ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
5173
bool Field_time::get_time(MYSQL_TIME *ltime)
5175
long tmp=(long) sint3korr(ptr);
5183
ltime->hour= (int) (tmp/10000);
5184
tmp-=ltime->hour*10000;
5185
ltime->minute= (int) tmp/100;
5186
ltime->second= (int) tmp % 100;
5187
ltime->second_part=0;
5188
ltime->time_type= MYSQL_TIMESTAMP_TIME;
5193
bool Field_time::send_binary(Protocol *protocol)
5196
Field_time::get_time(&tm);
5197
tm.day= tm.hour/24; // Move hours to days
5198
tm.hour-= tm.day*24;
5199
return protocol->store_time(&tm);
5203
int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
5206
a=(int32) sint3korr(a_ptr);
5207
b=(int32) sint3korr(b_ptr);
5208
return (a < b) ? -1 : (a > b) ? 1 : 0;
5211
void Field_time::sort_string(uchar *to,uint length __attribute__((unused)))
5213
to[0] = (uchar) (ptr[2] ^ 128);
5218
void Field_time::sql_type(String &res) const
5220
res.set_ascii(STRING_WITH_LEN("time"));
5223
/****************************************************************************
5225
** Save in a byte the year 0, 1901->2155
5226
** Can handle 2 byte or 4 byte years!
5227
****************************************************************************/
5229
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
5231
ASSERT_COLUMN_MARKED_FOR_WRITE;
5234
longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
5236
if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 ||
5237
error == MY_ERRNO_ERANGE)
5240
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
5243
if (table->in_use->count_cuted_fields &&
5244
(error= check_int(cs, from, len, end, error)))
5246
if (error == 1) /* empty or incorrect string */
5254
if (nr != 0 || len != 4)
5256
if (nr < YY_PART_YEAR)
5257
nr+=100; // 2000 - 2069
5261
*ptr= (char) (uchar) nr;
5266
int Field_year::store(double nr)
5268
if (nr < 0.0 || nr >= 2155.0)
5270
(void) Field_year::store((longlong) -1, FALSE);
5273
return Field_year::store((longlong) nr, FALSE);
5277
int Field_year::store(longlong nr, bool unsigned_val)
5279
ASSERT_COLUMN_MARKED_FOR_WRITE;
5280
if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
5283
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
5286
if (nr != 0 || field_length != 4) // 0000 -> 0; 00 -> 2000
5288
if (nr < YY_PART_YEAR)
5289
nr+=100; // 2000 - 2069
5293
*ptr= (char) (uchar) nr;
5298
bool Field_year::send_binary(Protocol *protocol)
5300
ASSERT_COLUMN_MARKED_FOR_READ;
5301
uint64_t tmp= Field_year::val_int();
5302
return protocol->store_short(tmp);
5306
double Field_year::val_real(void)
5308
return (double) Field_year::val_int();
5312
longlong Field_year::val_int(void)
5314
ASSERT_COLUMN_MARKED_FOR_READ;
5315
int tmp= (int) ptr[0];
5316
if (field_length != 4)
5317
tmp%=100; // Return last 2 char
5320
return (longlong) tmp;
5324
String *Field_year::val_str(String *val_buffer,
5325
String *val_ptr __attribute__((unused)))
5327
val_buffer->alloc(5);
5328
val_buffer->length(field_length);
5329
char *to=(char*) val_buffer->ptr();
5330
sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
5335
void Field_year::sql_type(String &res) const
5337
CHARSET_INFO *cs=res.charset();
5338
res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
5339
"year(%d)",(int) field_length));
5343
/****************************************************************************
5345
** In string context: YYYY-MM-DD
5346
** In number context: YYYYMMDD
5347
** Stored as a 4 byte unsigned int
5348
****************************************************************************/
5350
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
5352
ASSERT_COLUMN_MARKED_FOR_WRITE;
5356
THD *thd= table ? table->in_use : current_thd;
5358
if (str_to_datetime(from, len, &l_time, TIME_FUZZY_DATE |
5359
(thd->variables.sql_mode &
5360
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5361
MODE_INVALID_DATES)),
5362
&error) <= MYSQL_TIMESTAMP_ERROR)
5368
tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
5371
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
5372
from, len, MYSQL_TIMESTAMP_DATE, 1);
5374
#ifdef WORDS_BIGENDIAN
5375
if (table && table->s->db_low_byte_first)
5386
int Field_date::store(double nr)
5390
if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
5391
nr=floor(nr/1000000.0); // Timestamp to date
5392
if (nr < 0.0 || nr > 99991231.0)
5395
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5396
ER_WARN_DATA_OUT_OF_RANGE,
5397
nr, MYSQL_TIMESTAMP_DATE);
5401
tmp= (longlong) rint(nr);
5403
return Field_date::store(tmp, TRUE);
5407
int Field_date::store(longlong nr, bool unsigned_val)
5409
ASSERT_COLUMN_MARKED_FOR_WRITE;
5410
MYSQL_TIME not_used;
5412
longlong initial_nr= nr;
5413
THD *thd= table ? table->in_use : current_thd;
5415
nr= number_to_datetime(nr, ¬_used, (TIME_FUZZY_DATE |
5416
(thd->variables.sql_mode &
5417
(MODE_NO_ZERO_IN_DATE |
5419
MODE_INVALID_DATES))), &error);
5427
if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
5428
nr= (longlong) floor(nr/1000000.0); // Timestamp to date
5431
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5432
error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
5433
WARN_DATA_TRUNCATED, initial_nr,
5434
MYSQL_TIMESTAMP_DATETIME, 1);
5436
#ifdef WORDS_BIGENDIAN
5437
if (table && table->s->db_low_byte_first)
5448
bool Field_date::send_binary(Protocol *protocol)
5450
longlong tmp= Field_date::val_int();
5452
tm.year= (uint32) tmp/10000L % 10000;
5453
tm.month= (uint32) tmp/100 % 100;
5454
tm.day= (uint32) tmp % 100;
5455
return protocol->store_date(&tm);
5459
double Field_date::val_real(void)
5461
ASSERT_COLUMN_MARKED_FOR_READ;
5463
#ifdef WORDS_BIGENDIAN
5464
if (table && table->s->db_low_byte_first)
5469
return (double) (uint32) j;
5473
longlong Field_date::val_int(void)
5475
ASSERT_COLUMN_MARKED_FOR_READ;
5477
#ifdef WORDS_BIGENDIAN
5478
if (table && table->s->db_low_byte_first)
5483
return (longlong) (uint32) j;
5487
String *Field_date::val_str(String *val_buffer,
5488
String *val_ptr __attribute__((unused)))
5490
ASSERT_COLUMN_MARKED_FOR_READ;
5492
val_buffer->alloc(field_length);
5494
#ifdef WORDS_BIGENDIAN
5495
if (table && table->s->db_low_byte_first)
5501
ltime.year= (int) ((uint32) tmp/10000L % 10000);
5502
ltime.month= (int) ((uint32) tmp/100 % 100);
5503
ltime.day= (int) ((uint32) tmp % 100);
5504
make_date((DATE_TIME_FORMAT *) 0, <ime, val_buffer);
5509
bool Field_date::get_time(MYSQL_TIME *ltime)
5511
bzero((char *)ltime, sizeof(MYSQL_TIME));
5516
int Field_date::cmp(const uchar *a_ptr, const uchar *b_ptr)
5519
#ifdef WORDS_BIGENDIAN
5520
if (table && table->s->db_low_byte_first)
5531
return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
5535
void Field_date::sort_string(uchar *to,uint length __attribute__((unused)))
5537
#ifdef WORDS_BIGENDIAN
5538
if (!table || !table->s->db_low_byte_first)
5555
void Field_date::sql_type(String &res) const
5557
res.set_ascii(STRING_WITH_LEN("date"));
5561
/****************************************************************************
5562
** The new date type
5563
** This is identical to the old date type, but stored on 3 bytes instead of 4
5564
** In number context: YYYYMMDD
5565
****************************************************************************/
5568
Store string into a date field
5571
Field_newdate::store()
5573
len Length of date field
5574
cs Character set (not used)
5578
1 Value was cut during conversion
5580
3 Datetime value that was cut (warning level NOTE)
5581
This is used by opt_range.cc:get_mm_leaf(). Note that there is a
5582
nearly-identical class Field_date doesn't ever return 3 from its
5586
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
5588
ASSERT_COLUMN_MARKED_FOR_WRITE;
5592
THD *thd= table ? table->in_use : current_thd;
5593
enum enum_mysql_timestamp_type ret;
5594
if ((ret= str_to_datetime(from, len, &l_time,
5596
(thd->variables.sql_mode &
5597
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5598
MODE_INVALID_DATES))),
5599
&error)) <= MYSQL_TIMESTAMP_ERROR)
5606
tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
5607
if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
5608
(l_time.hour || l_time.minute || l_time.second || l_time.second_part))
5609
error= 3; // Datetime was cut (note)
5613
set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
5614
MYSQL_ERROR::WARN_LEVEL_WARN,
5615
WARN_DATA_TRUNCATED,
5616
from, len, MYSQL_TIMESTAMP_DATE, 1);
5618
int3store(ptr, tmp);
5623
int Field_newdate::store(double nr)
5625
if (nr < 0.0 || nr > 99991231235959.0)
5627
int3store(ptr,(int32) 0);
5628
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5629
WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
5632
return Field_newdate::store((longlong) rint(nr), FALSE);
5636
int Field_newdate::store(longlong nr, bool unsigned_val)
5638
ASSERT_COLUMN_MARKED_FOR_WRITE;
5642
THD *thd= table ? table->in_use : current_thd;
5643
if (number_to_datetime(nr, &l_time,
5645
(thd->variables.sql_mode &
5646
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5647
MODE_INVALID_DATES))),
5654
tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
5656
if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
5657
(l_time.hour || l_time.minute || l_time.second || l_time.second_part))
5661
set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
5662
MYSQL_ERROR::WARN_LEVEL_WARN,
5664
ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED,
5665
nr,MYSQL_TIMESTAMP_DATE, 1);
5672
int Field_newdate::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
5674
ASSERT_COLUMN_MARKED_FOR_WRITE;
5677
if (time_type == MYSQL_TIMESTAMP_DATE ||
5678
time_type == MYSQL_TIMESTAMP_DATETIME)
5680
tmp=ltime->year*16*32+ltime->month*32+ltime->day;
5681
if (check_date(ltime, tmp != 0,
5683
(current_thd->variables.sql_mode &
5684
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5685
MODE_INVALID_DATES))), &error))
5687
char buff[MAX_DATE_STRING_REP_LENGTH];
5688
String str(buff, sizeof(buff), &my_charset_latin1);
5689
make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
5690
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
5691
str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
5693
if (!error && ltime->time_type != MYSQL_TIMESTAMP_DATE &&
5694
(ltime->hour || ltime->minute || ltime->second || ltime->second_part))
5696
char buff[MAX_DATE_STRING_REP_LENGTH];
5697
String str(buff, sizeof(buff), &my_charset_latin1);
5698
make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
5699
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_NOTE,
5700
WARN_DATA_TRUNCATED,
5701
str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
5709
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
5716
bool Field_newdate::send_binary(Protocol *protocol)
5719
Field_newdate::get_date(&tm,0);
5720
return protocol->store_date(&tm);
5724
double Field_newdate::val_real(void)
5726
ASSERT_COLUMN_MARKED_FOR_READ;
5727
return (double) Field_newdate::val_int();
5731
longlong Field_newdate::val_int(void)
5733
ASSERT_COLUMN_MARKED_FOR_READ;
5734
ulong j= uint3korr(ptr);
5735
j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
5736
return (longlong) j;
5740
String *Field_newdate::val_str(String *val_buffer,
5741
String *val_ptr __attribute__((unused)))
5743
ASSERT_COLUMN_MARKED_FOR_READ;
5744
val_buffer->alloc(field_length);
5745
val_buffer->length(field_length);
5746
uint32 tmp=(uint32) uint3korr(ptr);
5748
char *pos=(char*) val_buffer->ptr()+10;
5750
/* Open coded to get more speed */
5751
*pos--=0; // End NULL
5752
part=(int) (tmp & 31);
5753
*pos--= (char) ('0'+part%10);
5754
*pos--= (char) ('0'+part/10);
5756
part=(int) (tmp >> 5 & 15);
5757
*pos--= (char) ('0'+part%10);
5758
*pos--= (char) ('0'+part/10);
5760
part=(int) (tmp >> 9);
5761
*pos--= (char) ('0'+part%10); part/=10;
5762
*pos--= (char) ('0'+part%10); part/=10;
5763
*pos--= (char) ('0'+part%10); part/=10;
5764
*pos= (char) ('0'+part);
5769
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5771
uint32 tmp=(uint32) uint3korr(ptr);
5772
ltime->day= tmp & 31;
5773
ltime->month= (tmp >> 5) & 15;
5774
ltime->year= (tmp >> 9);
5775
ltime->time_type= MYSQL_TIMESTAMP_DATE;
5776
ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
5777
return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
5782
bool Field_newdate::get_time(MYSQL_TIME *ltime)
5784
return Field_newdate::get_date(ltime,0);
5788
int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
5791
a=(uint32) uint3korr(a_ptr);
5792
b=(uint32) uint3korr(b_ptr);
5793
return (a < b) ? -1 : (a > b) ? 1 : 0;
5797
void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
5805
void Field_newdate::sql_type(String &res) const
5807
res.set_ascii(STRING_WITH_LEN("date"));
5811
/****************************************************************************
5813
** In string context: YYYY-MM-DD HH:MM:DD
5814
** In number context: YYYYMMDDHHMMDD
5815
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
5816
****************************************************************************/
5818
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
5820
ASSERT_COLUMN_MARKED_FOR_WRITE;
5821
MYSQL_TIME time_tmp;
5824
enum enum_mysql_timestamp_type func_res;
5825
THD *thd= table ? table->in_use : current_thd;
5827
func_res= str_to_datetime(from, len, &time_tmp,
5829
(thd->variables.sql_mode &
5830
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5831
MODE_INVALID_DATES))),
5833
if ((int) func_res > (int) MYSQL_TIMESTAMP_ERROR)
5834
tmp= TIME_to_ulonglong_datetime(&time_tmp);
5836
error= 1; // Fix if invalid zero date
5839
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5840
ER_WARN_DATA_OUT_OF_RANGE,
5841
from, len, MYSQL_TIMESTAMP_DATETIME, 1);
5843
#ifdef WORDS_BIGENDIAN
5844
if (table && table->s->db_low_byte_first)
5850
longlongstore(ptr,tmp);
5855
int Field_datetime::store(double nr)
5858
if (nr < 0.0 || nr > 99991231235959.0)
5860
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5861
ER_WARN_DATA_OUT_OF_RANGE,
5862
nr, MYSQL_TIMESTAMP_DATETIME);
5866
error|= Field_datetime::store((longlong) rint(nr), FALSE);
5871
int Field_datetime::store(longlong nr, bool unsigned_val)
5873
ASSERT_COLUMN_MARKED_FOR_WRITE;
5874
MYSQL_TIME not_used;
5876
longlong initial_nr= nr;
5877
THD *thd= table ? table->in_use : current_thd;
5879
nr= number_to_datetime(nr, ¬_used, (TIME_FUZZY_DATE |
5880
(thd->variables.sql_mode &
5881
(MODE_NO_ZERO_IN_DATE |
5883
MODE_INVALID_DATES))), &error);
5892
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5893
error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
5894
WARN_DATA_TRUNCATED, initial_nr,
5895
MYSQL_TIMESTAMP_DATETIME, 1);
5897
#ifdef WORDS_BIGENDIAN
5898
if (table && table->s->db_low_byte_first)
5904
longlongstore(ptr,nr);
5909
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
5911
ASSERT_COLUMN_MARKED_FOR_WRITE;
5915
We don't perform range checking here since values stored in TIME
5916
structure always fit into DATETIME range.
5918
if (time_type == MYSQL_TIMESTAMP_DATE ||
5919
time_type == MYSQL_TIMESTAMP_DATETIME)
5921
tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
5922
(ltime->hour*10000L+ltime->minute*100+ltime->second));
5923
if (check_date(ltime, tmp != 0,
5925
(current_thd->variables.sql_mode &
5926
(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5927
MODE_INVALID_DATES))), &error))
5929
char buff[MAX_DATE_STRING_REP_LENGTH];
5930
String str(buff, sizeof(buff), &my_charset_latin1);
5931
make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
5932
set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
5933
str.ptr(), str.length(), MYSQL_TIMESTAMP_DATETIME,1);
5940
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
5942
#ifdef WORDS_BIGENDIAN
5943
if (table && table->s->db_low_byte_first)
5949
longlongstore(ptr,tmp);
5953
bool Field_datetime::send_binary(Protocol *protocol)
5956
Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
5957
return protocol->store(&tm);
5961
double Field_datetime::val_real(void)
5963
return (double) Field_datetime::val_int();
5966
longlong Field_datetime::val_int(void)
5968
ASSERT_COLUMN_MARKED_FOR_READ;
5970
#ifdef WORDS_BIGENDIAN
5971
if (table && table->s->db_low_byte_first)
5980
String *Field_datetime::val_str(String *val_buffer,
5981
String *val_ptr __attribute__((unused)))
5983
ASSERT_COLUMN_MARKED_FOR_READ;
5984
val_buffer->alloc(field_length);
5985
val_buffer->length(field_length);
5991
#ifdef WORDS_BIGENDIAN
5992
if (table && table->s->db_low_byte_first)
5996
longlongget(tmp,ptr);
5999
Avoid problem with slow longlong arithmetic and sprintf
6002
part1=(long) (tmp/LL(1000000));
6003
part2=(long) (tmp - (uint64_t) part1*LL(1000000));
6005
pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
6007
*pos--= (char) ('0'+(char) (part2%10)); part2/=10;
6008
*pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
6010
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6011
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6013
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6014
*pos--= (char) ('0'+(char) part3);
6016
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
6017
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
6019
*pos--= (char) ('0'+(char) (part1%10)); part1/=10;
6020
*pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
6022
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6023
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6024
*pos--= (char) ('0'+(char) (part3%10)); part3/=10;
6025
*pos=(char) ('0'+(char) part3);
6029
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
6031
longlong tmp=Field_datetime::val_int();
6033
part1=(uint32) (tmp/LL(1000000));
6034
part2=(uint32) (tmp - (uint64_t) part1*LL(1000000));
6036
ltime->time_type= MYSQL_TIMESTAMP_DATETIME;
6038
ltime->second_part= 0;
6039
ltime->second= (int) (part2%100);
6040
ltime->minute= (int) (part2/100%100);
6041
ltime->hour= (int) (part2/10000);
6042
ltime->day= (int) (part1%100);
6043
ltime->month= (int) (part1/100%100);
6044
ltime->year= (int) (part1/10000);
6045
return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
6048
bool Field_datetime::get_time(MYSQL_TIME *ltime)
6050
return Field_datetime::get_date(ltime,0);
6053
int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
6056
#ifdef WORDS_BIGENDIAN
6057
if (table && table->s->db_low_byte_first)
6065
longlongget(a,a_ptr);
6066
longlongget(b,b_ptr);
6068
return ((uint64_t) a < (uint64_t) b) ? -1 :
6069
((uint64_t) a > (uint64_t) b) ? 1 : 0;
6072
void Field_datetime::sort_string(uchar *to,uint length __attribute__((unused)))
6074
#ifdef WORDS_BIGENDIAN
6075
if (!table || !table->s->db_low_byte_first)
6101
void Field_datetime::sql_type(String &res) const
6103
res.set_ascii(STRING_WITH_LEN("datetime"));
6106
/****************************************************************************
6108
** A string may be varchar or binary
6109
****************************************************************************/
6112
Report "not well formed" or "cannot convert" error
6113
after storing a character string info a field.
6116
check_string_copy_error()
6118
well_formed_error_pos - where not well formed data was first met
6119
cannot_convert_error_pos - where a not-convertable character was first met
6120
end - end of the string
6121
cs - character set of the string
6124
As of version 5.0 both cases return the same error:
6126
"Invalid string value: 'xxx' for column 't' at row 1"
6128
Future versions will possibly introduce a new error message:
6130
"Cannot convert character string: 'xxx' for column 't' at row 1"
6133
FALSE - If errors didn't happen
6134
TRUE - If an error happened
6138
check_string_copy_error(Field_str *field,
6139
const char *well_formed_error_pos,
6140
const char *cannot_convert_error_pos,
6144
const char *pos, *end_orig;
6147
if (!(pos= well_formed_error_pos) &&
6148
!(pos= cannot_convert_error_pos))
6152
set_if_smaller(end, pos + 6);
6154
for (t= tmp; pos < end; pos++)
6157
If the source string is ASCII compatible (mbminlen==1)
6158
and the source character is in ASCII printable range (0x20..0x7F),
6159
then display the character as is.
6161
Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
6162
or the source character is not in the printable range,
6163
then print the character using HEX notation.
6165
if (((unsigned char) *pos) >= 0x20 &&
6166
((unsigned char) *pos) <= 0x7F &&
6175
*t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
6176
*t++= _dig_vec_upper[((unsigned char) *pos) & 15];
6186
push_warning_printf(field->table->in_use,
6187
field->table->in_use->abort_on_warning ?
6188
MYSQL_ERROR::WARN_LEVEL_ERROR :
6189
MYSQL_ERROR::WARN_LEVEL_WARN,
6190
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
6191
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
6192
"string", tmp, field->field_name,
6193
(ulong) field->table->in_use->row_count);
6199
Check if we lost any important data and send a truncation error/warning
6202
Field_longstr::report_if_important_data()
6203
ptr - Truncated rest of string
6204
end - End of truncated string
6207
0 - None was truncated (or we don't count cut fields)
6208
2 - Some bytes was truncated
6211
Check if we lost any important data (anything in a binary string,
6212
or any non-space in others). If only trailing spaces was lost,
6213
send a truncation note, otherwise send a truncation error.
6217
Field_longstr::report_if_important_data(const char *ptr, const char *end)
6219
if ((ptr < end) && table->in_use->count_cuted_fields)
6221
if (test_if_important_data(field_charset, ptr, end))
6223
if (table->in_use->abort_on_warning)
6224
set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
6226
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6228
else /* If we lost only spaces then produce a NOTE, not a WARNING */
6229
set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
6236
/* Copy a string and fill with space */
6238
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
6240
ASSERT_COLUMN_MARKED_FOR_WRITE;
6242
const char *well_formed_error_pos;
6243
const char *cannot_convert_error_pos;
6244
const char *from_end_pos;
6246
/* See the comment for Field_long::store(long long) */
6247
DBUG_ASSERT(table->in_use == current_thd);
6249
copy_length= well_formed_copy_nchars(field_charset,
6250
(char*) ptr, field_length,
6252
field_length / field_charset->mbmaxlen,
6253
&well_formed_error_pos,
6254
&cannot_convert_error_pos,
6257
/* Append spaces if the string was shorter than the field. */
6258
if (copy_length < field_length)
6259
field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
6260
field_length-copy_length,
6261
field_charset->pad_char);
6263
if (check_string_copy_error(this, well_formed_error_pos,
6264
cannot_convert_error_pos, from + length, cs))
6267
return report_if_important_data(from_end_pos, from + length);
6272
Store double value in Field_string or Field_varstring.
6274
Pretty prints double number into field_length characters buffer.
6279
int Field_str::store(double nr)
6281
ASSERT_COLUMN_MARKED_FOR_WRITE;
6282
char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
6283
uint local_char_length= field_length / charset()->mbmaxlen;
6287
length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
6290
if (table->in_use->abort_on_warning)
6291
set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
6293
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6295
return store(buff, length, charset());
6299
uint Field::is_equal(Create_field *new_field)
6301
return (new_field->sql_type == real_type());
6305
/* If one of the fields is binary and the other one isn't return 1 else 0 */
6307
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32 flag_arg)
6309
return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
6310
!(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
6311
(!(new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
6312
(flag_arg & (BINCMP_FLAG | BINARY_FLAG))));
6316
uint Field_str::is_equal(Create_field *new_field)
6318
if (compare_str_field_flags(new_field, flags))
6321
return ((new_field->sql_type == real_type()) &&
6322
new_field->charset == field_charset &&
6323
new_field->length == max_display_length());
6327
int Field_string::store(longlong nr, bool unsigned_val)
6331
CHARSET_INFO *cs=charset();
6332
l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
6333
unsigned_val ? 10 : -10, nr);
6334
return Field_string::store(buff,(uint)l,cs);
6338
int Field_longstr::store_decimal(const my_decimal *d)
6340
char buff[DECIMAL_MAX_STR_LENGTH+1];
6341
String str(buff, sizeof(buff), &my_charset_bin);
6342
my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
6343
return store(str.ptr(), str.length(), str.charset());
6346
uint32 Field_longstr::max_data_length() const
6348
return field_length + (field_length > 255 ? 2 : 1);
6352
double Field_string::val_real(void)
6354
ASSERT_COLUMN_MARKED_FOR_READ;
6357
CHARSET_INFO *cs= charset();
6360
result= my_strntod(cs,(char*) ptr,field_length,&end,&error);
6361
if (!table->in_use->no_errors &&
6362
(error || (field_length != (uint32)(end - (char*) ptr) &&
6363
!check_if_only_end_space(cs, end,
6364
(char*) ptr + field_length))))
6366
char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
6367
String tmp(buf, sizeof(buf), cs);
6368
tmp.copy((char*) ptr, field_length, cs);
6369
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
6370
ER_TRUNCATED_WRONG_VALUE,
6371
ER(ER_TRUNCATED_WRONG_VALUE),
6372
"DOUBLE", tmp.c_ptr());
6378
longlong Field_string::val_int(void)
6380
ASSERT_COLUMN_MARKED_FOR_READ;
6383
CHARSET_INFO *cs= charset();
6386
result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
6387
if (!table->in_use->no_errors &&
6388
(error || (field_length != (uint32)(end - (char*) ptr) &&
6389
!check_if_only_end_space(cs, end,
6390
(char*) ptr + field_length))))
6392
char buf[LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE];
6393
String tmp(buf, sizeof(buf), cs);
6394
tmp.copy((char*) ptr, field_length, cs);
6395
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
6396
ER_TRUNCATED_WRONG_VALUE,
6397
ER(ER_TRUNCATED_WRONG_VALUE),
6398
"INTEGER", tmp.c_ptr());
6404
String *Field_string::val_str(String *val_buffer __attribute__((unused)),
6407
ASSERT_COLUMN_MARKED_FOR_READ;
6408
/* See the comment for Field_long::store(long long) */
6409
DBUG_ASSERT(table->in_use == current_thd);
6411
if (table->in_use->variables.sql_mode &
6412
MODE_PAD_CHAR_TO_FULL_LENGTH)
6413
length= my_charpos(field_charset, ptr, ptr + field_length, field_length);
6415
length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
6417
val_ptr->set((const char*) ptr, length, field_charset);
6422
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
6424
ASSERT_COLUMN_MARKED_FOR_READ;
6425
int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
6426
charset(), decimal_value);
6427
if (!table->in_use->no_errors && err)
6429
char buf[DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE];
6430
CHARSET_INFO *cs= charset();
6431
String tmp(buf, sizeof(buf), cs);
6432
tmp.copy((char*) ptr, field_length, cs);
6433
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
6434
ER_TRUNCATED_WRONG_VALUE,
6435
ER(ER_TRUNCATED_WRONG_VALUE),
6436
"DECIMAL", tmp.c_ptr());
6439
return decimal_value;
6443
int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
6447
if (field_charset->mbmaxlen != 1)
6449
uint char_len= field_length/field_charset->mbmaxlen;
6450
a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
6451
b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
6454
a_len= b_len= field_length;
6456
We have to remove end space to be able to compare multi-byte-characters
6457
like in latin_de 'ae' and 0xe4
6459
return field_charset->coll->strnncollsp(field_charset,
6466
void Field_string::sort_string(uchar *to,uint length)
6468
IF_DBUG(uint tmp=) my_strnxfrm(field_charset,
6471
DBUG_ASSERT(tmp == length);
6475
void Field_string::sql_type(String &res) const
6477
THD *thd= table->in_use;
6478
CHARSET_INFO *cs=res.charset();
6481
length= cs->cset->snprintf(cs,(char*) res.ptr(),
6482
res.alloced_length(), "%s(%d)",
6483
((type() == MYSQL_TYPE_VAR_STRING &&
6484
!thd->variables.new_mode) ?
6485
(has_charset() ? "varchar" : "varbinary") :
6486
(has_charset() ? "char" : "binary")),
6487
(int) field_length / charset()->mbmaxlen);
6489
if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
6490
has_charset() && (charset()->state & MY_CS_BINSORT))
6491
res.append(STRING_WITH_LEN(" binary"));
6495
uchar *Field_string::pack(uchar *to, const uchar *from,
6497
bool low_byte_first __attribute__((unused)))
6499
uint length= min(field_length,max_length);
6500
uint local_char_length= max_length/field_charset->mbmaxlen;
6501
if (length > local_char_length)
6502
local_char_length= my_charpos(field_charset, from, from+length,
6504
set_if_smaller(length, local_char_length);
6505
while (length && from[length-1] == field_charset->pad_char)
6508
// Length always stored little-endian
6509
*to++= (uchar) length;
6510
if (field_length > 255)
6511
*to++= (uchar) (length >> 8);
6513
// Store the actual bytes of the string
6514
memcpy(to, from, length);
6520
Unpack a string field from row data.
6522
This method is used to unpack a string field from a master whose size
6523
of the field is less than that of the slave. Note that there can be a
6524
variety of field types represented with this class. Certain types like
6525
ENUM or SET are processed differently. Hence, the upper byte of the
6526
@c param_data argument contains the result of field->real_type() from
6529
@param to Destination of the data
6530
@param from Source of the data
6531
@param param_data Real type (upper) and length (lower) values
6533
@return New pointer into memory based on from + length of the data
6536
Field_string::unpack(uchar *to,
6539
bool low_byte_first __attribute__((unused)))
6542
param_data ? min(param_data & 0x00ff, field_length) : field_length;
6545
if (from_length > 255)
6547
length= uint2korr(from);
6551
length= (uint) *from++;
6553
memcpy(to, from, length);
6554
// Pad the string with the pad character of the fields charset
6555
bfill(to + length, field_length - length, field_charset->pad_char);
6561
Save the field metadata for string fields.
6563
Saves the real type in the first byte and the field length in the
6564
second byte of the field metadata array at index of *metadata_ptr and
6565
*(metadata_ptr + 1).
6567
@param metadata_ptr First byte of field metadata
6569
@returns number of bytes written to metadata_ptr
6571
int Field_string::do_save_field_metadata(uchar *metadata_ptr)
6573
*metadata_ptr= real_type();
6574
*(metadata_ptr + 1)= field_length;
6580
Compare two packed keys
6587
insert_or_update 1 if this is an insert or update
6595
int Field_string::pack_cmp(const uchar *a, const uchar *b, uint length,
6596
my_bool insert_or_update)
6598
uint a_length, b_length;
6601
a_length= uint2korr(a);
6602
b_length= uint2korr(b);
6608
a_length= (uint) *a++;
6609
b_length= (uint) *b++;
6611
return field_charset->coll->strnncollsp(field_charset,
6619
Compare a packed key against row.
6621
@param key Original key
6622
@param length Key length. (May be less than field length)
6623
@param insert_or_update 1 if this is an insert or update
6633
int Field_string::pack_cmp(const uchar *key, uint length,
6634
my_bool insert_or_update)
6636
uint row_length, local_key_length;
6640
local_key_length= uint2korr(key);
6644
local_key_length= (uint) *key++;
6646
/* Only use 'length' of key, not field_length */
6648
while (end > ptr && end[-1] == ' ')
6650
row_length= (uint) (end - ptr);
6652
return field_charset->coll->strnncollsp(field_charset,
6654
key, local_key_length,
6659
uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
6662
return uint2korr(data_ptr)+2;
6663
return (uint) *data_ptr + 1;
6667
uint Field_string::max_packed_col_length(uint max_length)
6669
return (max_length > 255 ? 2 : 1)+max_length;
6673
uint Field_string::get_key_image(uchar *buff, uint length, imagetype type_arg)
6675
uint bytes = my_charpos(field_charset, (char*) ptr,
6676
(char*) ptr + field_length,
6677
length / field_charset->mbmaxlen);
6678
memcpy(buff, ptr, bytes);
6680
field_charset->cset->fill(field_charset, (char*) buff + bytes,
6681
length - bytes, field_charset->pad_char);
6686
Field *Field_string::new_field(MEM_ROOT *root, struct st_table *new_table,
6690
if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
6691
field= Field::new_field(root, new_table, keep_type);
6692
else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
6693
new_table->s, charset())))
6696
Old VARCHAR field which should be modified to a VARCHAR on copy
6697
This is done to ensure that ALTER TABLE will convert old VARCHAR fields
6698
to now VARCHAR fields.
6700
field->init(new_table);
6702
Normally orig_table is different from table only if field was created
6703
via ::new_field. Here we alter the type of field, so ::new_field is
6704
not applicable. But we still need to preserve the original field
6705
metadata for the client-server protocol.
6707
field->orig_table= orig_table;
6713
/****************************************************************************
6715
Data in field->ptr is stored as:
6716
1 or 2 bytes length-prefix-header (from Field_varstring::length_bytes)
6720
When VARCHAR is stored in a key (for handler::index_read() etc) it's always
6721
stored with a 2 byte prefix. (Just like blob keys).
6723
Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
6724
The exception is if there is a prefix key field that is part of a long
6725
VARCHAR, in which case field_length for this may be 1 but the length_bytes
6727
****************************************************************************/
6729
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
6732
Save the field metadata for varstring fields.
6734
Saves the field length in the first byte. Note: may consume
6735
2 bytes. Caller must ensure second byte is contiguous with
6736
first byte (e.g. array index 0,1).
6738
@param metadata_ptr First byte of field metadata
6740
@returns number of bytes written to metadata_ptr
6742
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
6744
char *ptr= (char *)metadata_ptr;
6745
DBUG_ASSERT(field_length <= 65535);
6746
int2store(ptr, field_length);
6750
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
6752
ASSERT_COLUMN_MARKED_FOR_WRITE;
6754
const char *well_formed_error_pos;
6755
const char *cannot_convert_error_pos;
6756
const char *from_end_pos;
6758
copy_length= well_formed_copy_nchars(field_charset,
6759
(char*) ptr + length_bytes,
6762
field_length / field_charset->mbmaxlen,
6763
&well_formed_error_pos,
6764
&cannot_convert_error_pos,
6767
if (length_bytes == 1)
6768
*ptr= (uchar) copy_length;
6770
int2store(ptr, copy_length);
6772
if (check_string_copy_error(this, well_formed_error_pos,
6773
cannot_convert_error_pos, from + length, cs))
6776
return report_if_important_data(from_end_pos, from + length);
6780
int Field_varstring::store(longlong nr, bool unsigned_val)
6784
length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
6790
return Field_varstring::store(buff, length, field_charset);
6794
double Field_varstring::val_real(void)
6796
ASSERT_COLUMN_MARKED_FOR_READ;
6799
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6800
return my_strntod(field_charset, (char*) ptr+length_bytes, length,
6801
&end_not_used, ¬_used);
6805
longlong Field_varstring::val_int(void)
6807
ASSERT_COLUMN_MARKED_FOR_READ;
6810
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6811
return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
6812
&end_not_used, ¬_used);
6815
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
6818
ASSERT_COLUMN_MARKED_FOR_READ;
6819
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6820
val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
6825
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
6827
ASSERT_COLUMN_MARKED_FOR_READ;
6828
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6829
str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
6830
charset(), decimal_value);
6831
return decimal_value;
6835
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
6838
uint a_length, b_length;
6841
if (length_bytes == 1)
6843
a_length= (uint) *a_ptr;
6844
b_length= (uint) *b_ptr;
6848
a_length= uint2korr(a_ptr);
6849
b_length= uint2korr(b_ptr);
6851
set_if_smaller(a_length, max_len);
6852
set_if_smaller(b_length, max_len);
6853
diff= field_charset->coll->strnncollsp(field_charset,
6866
varstring and blob keys are ALWAYS stored with a 2 byte length prefix
6869
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
6871
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6872
uint local_char_length= max_key_length / field_charset->mbmaxlen;
6874
local_char_length= my_charpos(field_charset, ptr + length_bytes,
6875
ptr + length_bytes + length, local_char_length);
6876
set_if_smaller(length, local_char_length);
6877
return field_charset->coll->strnncollsp(field_charset,
6882
uint2korr(key_ptr), 0);
6887
Compare to key segments (always 2 byte length prefix).
6890
This is used only to compare key segments created for index_read().
6891
(keys are created and compared in key.cc)
6894
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
6896
return field_charset->coll->strnncollsp(field_charset,
6897
a + HA_KEY_BLOB_LENGTH,
6899
b + HA_KEY_BLOB_LENGTH,
6905
void Field_varstring::sort_string(uchar *to,uint length)
6907
uint tot_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6909
if (field_charset == &my_charset_bin)
6911
/* Store length last in high-byte order to sort longer strings first */
6912
if (length_bytes == 1)
6913
to[length-1]= tot_length;
6915
mi_int2store(to+length-2, tot_length);
6916
length-= length_bytes;
6919
tot_length= my_strnxfrm(field_charset,
6920
to, length, ptr + length_bytes,
6922
DBUG_ASSERT(tot_length == length);
6926
enum ha_base_keytype Field_varstring::key_type() const
6928
enum ha_base_keytype res;
6931
res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
6933
res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
6938
void Field_varstring::sql_type(String &res) const
6940
THD *thd= table->in_use;
6941
CHARSET_INFO *cs=res.charset();
6944
length= cs->cset->snprintf(cs,(char*) res.ptr(),
6945
res.alloced_length(), "%s(%d)",
6946
(has_charset() ? "varchar" : "varbinary"),
6947
(int) field_length / charset()->mbmaxlen);
6949
if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
6950
has_charset() && (charset()->state & MY_CS_BINSORT))
6951
res.append(STRING_WITH_LEN(" binary"));
6955
uint32 Field_varstring::data_length()
6957
return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
6960
uint32 Field_varstring::used_length()
6962
return length_bytes == 1 ? 1 + (uint32) (uchar) *ptr : 2 + uint2korr(ptr);
6966
Functions to create a packed row.
6967
Here the number of length bytes are depending on the given max_length
6970
uchar *Field_varstring::pack(uchar *to, const uchar *from,
6972
bool low_byte_first __attribute__((unused)))
6974
uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
6975
set_if_smaller(max_length, field_length);
6976
if (length > max_length)
6979
/* Length always stored little-endian */
6980
*to++= length & 0xFF;
6981
if (max_length > 255)
6982
*to++= (length >> 8) & 0xFF;
6984
/* Store bytes of string */
6986
memcpy(to, from+length_bytes, length);
6992
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
6993
bool low_byte_first __attribute__((unused)))
6995
uint length= length_bytes == 1 ? (uint) *key : uint2korr(key);
6996
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
6997
max_length/field_charset->mbmaxlen : max_length);
6999
if (length > local_char_length)
7001
local_char_length= my_charpos(field_charset, key, key+length,
7003
set_if_smaller(length, local_char_length);
7005
*to++= (char) (length & 255);
7006
if (max_length > 255)
7007
*to++= (char) (length >> 8);
7009
memcpy(to, key, length);
7015
Unpack a key into a record buffer.
7017
A VARCHAR key has a maximum size of 64K-1.
7018
In its packed form, the length field is one or two bytes long,
7019
depending on 'max_length'.
7021
@param to Pointer into the record buffer.
7022
@param key Pointer to the packed key.
7023
@param max_length Key length limit from key description.
7026
Pointer to end of 'key' (To the next key part if multi-segment key)
7030
Field_varstring::unpack_key(uchar *to, const uchar *key, uint max_length,
7031
bool low_byte_first __attribute__((unused)))
7033
/* get length of the blob key */
7034
uint32 length= *key++;
7035
if (max_length > 255)
7036
length+= (*key++) << 8;
7038
/* put the length into the record buffer */
7039
if (length_bytes == 1)
7040
*ptr= (uchar) length;
7042
int2store(ptr, length);
7043
memcpy(ptr + length_bytes, key, length);
7044
return key + length;
7048
Create a packed key that will be used for storage in the index tree.
7050
@param to Store packed key segment here
7051
@param from Key segment (as given to index_read())
7052
@param max_length Max length of key
7059
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
7060
bool low_byte_first __attribute__((unused)))
7062
/* Key length is always stored as 2 bytes */
7063
uint length= uint2korr(from);
7064
if (length > max_length)
7066
*to++= (char) (length & 255);
7067
if (max_length > 255)
7068
*to++= (char) (length >> 8);
7070
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
7076
Unpack a varstring field from row data.
7078
This method is used to unpack a varstring field from a master
7079
whose size of the field is less than that of the slave.
7082
The string length is always packed little-endian.
7084
@param to Destination of the data
7085
@param from Source of the data
7086
@param param_data Length bytes from the master's field data
7088
@return New pointer into memory based on from + length of the data
7091
Field_varstring::unpack(uchar *to, const uchar *from,
7093
bool low_byte_first __attribute__((unused)))
7096
uint l_bytes= (param_data && (param_data < field_length)) ?
7097
(param_data <= 255) ? 1 : 2 : length_bytes;
7102
if (length_bytes == 2)
7105
else /* l_bytes == 2 */
7107
length= uint2korr(from);
7112
memcpy(to+ length_bytes, from, length);
7117
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
7118
uint key_length_arg,
7119
my_bool insert_or_update)
7121
uint a_length, b_length;
7122
if (key_length_arg > 255)
7124
a_length=uint2korr(a); a+= 2;
7125
b_length=uint2korr(b); b+= 2;
7129
a_length= (uint) *a++;
7130
b_length= (uint) *b++;
7132
return field_charset->coll->strnncollsp(field_charset,
7139
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
7140
my_bool insert_or_update)
7142
uchar *a= ptr+ length_bytes;
7143
uint a_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7145
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
7146
key_length_arg / field_charset->mbmaxlen :
7149
if (key_length_arg > 255)
7151
b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
7154
b_length= (uint) *b++;
7156
if (a_length > local_char_length)
7158
local_char_length= my_charpos(field_charset, a, a+a_length,
7160
set_if_smaller(a_length, local_char_length);
7163
return field_charset->coll->strnncollsp(field_charset,
7170
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
7173
return uint2korr(data_ptr)+2;
7174
return (uint) *data_ptr + 1;
7178
uint Field_varstring::max_packed_col_length(uint max_length)
7180
return (max_length > 255 ? 2 : 1)+max_length;
7183
uint Field_varstring::get_key_image(uchar *buff, uint length, imagetype type)
7185
uint f_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7186
uint local_char_length= length / field_charset->mbmaxlen;
7187
uchar *pos= ptr+length_bytes;
7188
local_char_length= my_charpos(field_charset, pos, pos + f_length,
7190
set_if_smaller(f_length, local_char_length);
7191
/* Key is always stored with 2 bytes */
7192
int2store(buff,f_length);
7193
memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
7194
if (f_length < length)
7197
Must clear this as we do a memcmp in opt_range.cc to detect
7200
bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
7202
return HA_KEY_BLOB_LENGTH+f_length;
7206
void Field_varstring::set_key_image(const uchar *buff,uint length)
7208
length= uint2korr(buff); // Real length is here
7209
(void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
7214
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7217
uint32 a_length,b_length;
7219
if (length_bytes == 1)
7221
a_length= (uint) *a_ptr;
7222
b_length= (uint) *b_ptr;
7226
a_length= uint2korr(a_ptr);
7227
b_length= uint2korr(b_ptr);
7229
set_if_smaller(a_length, max_length);
7230
set_if_smaller(b_length, max_length);
7231
if (a_length != b_length)
7233
return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
7237
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
7240
Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
7243
res->length_bytes= length_bytes;
7248
Field *Field_varstring::new_key_field(MEM_ROOT *root,
7249
struct st_table *new_table,
7250
uchar *new_ptr, uchar *new_null_ptr,
7253
Field_varstring *res;
7254
if ((res= (Field_varstring*) Field::new_key_field(root,
7260
/* Keys length prefixes are always packed with 2 bytes */
7261
res->length_bytes= 2;
7267
uint Field_varstring::is_equal(Create_field *new_field)
7269
if (new_field->sql_type == real_type() &&
7270
new_field->charset == field_charset)
7272
if (new_field->length == max_display_length())
7273
return IS_EQUAL_YES;
7274
if (new_field->length > max_display_length() &&
7275
((new_field->length <= 255 && max_display_length() <= 255) ||
7276
(new_field->length > 255 && max_display_length() > 255)))
7277
return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
7283
void Field_varstring::hash(ulong *nr, ulong *nr2)
7287
*nr^= (*nr << 1) | 1;
7291
uint len= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7292
CHARSET_INFO *cs= charset();
7293
cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
7298
/****************************************************************************
7300
** A blob is saved as a length and a pointer. The length is stored in the
7301
** packlength slot and may be from 1-4.
7302
****************************************************************************/
7304
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
7305
enum utype unireg_check_arg, const char *field_name_arg,
7306
TABLE_SHARE *share, uint blob_pack_length,
7308
:Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
7309
null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
7311
packlength(blob_pack_length)
7314
share->blob_fields++;
7315
/* TODO: why do not fill table->s->blob_field array here? */
7319
void Field_blob::store_length(uchar *i_ptr,
7322
bool low_byte_first)
7324
switch (i_packlength) {
7326
i_ptr[0]= (uchar) i_number;
7329
#ifdef WORDS_BIGENDIAN
7332
int2store(i_ptr,(unsigned short) i_number);
7336
shortstore(i_ptr,(unsigned short) i_number);
7339
int3store(i_ptr,i_number);
7342
#ifdef WORDS_BIGENDIAN
7345
int4store(i_ptr,i_number);
7349
longstore(i_ptr,i_number);
7354
uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg, bool low_byte_first)
7356
switch (packlength_arg) {
7358
return (uint32) pos[0];
7362
#ifdef WORDS_BIGENDIAN
7368
return (uint32) tmp;
7371
return (uint32) uint3korr(pos);
7375
#ifdef WORDS_BIGENDIAN
7381
return (uint32) tmp;
7384
return 0; // Impossible
7389
Put a blob length field into a record buffer.
7391
Depending on the maximum length of a blob, its length field is
7392
put into 1 to 4 bytes. This is a property of the blob object,
7393
described by 'packlength'.
7395
@param pos Pointer into the record buffer.
7396
@param length The length value to put.
7399
void Field_blob::put_length(uchar *pos, uint32 length)
7401
switch (packlength) {
7403
*pos= (char) length;
7406
int2store(pos, length);
7409
int3store(pos, length);
7412
int4store(pos, length);
7418
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
7420
ASSERT_COLUMN_MARKED_FOR_WRITE;
7421
uint copy_length, new_length;
7422
const char *well_formed_error_pos;
7423
const char *cannot_convert_error_pos;
7424
const char *from_end_pos, *tmp;
7425
char buff[STRING_BUFFER_USUAL_SIZE];
7426
String tmpstr(buff,sizeof(buff), &my_charset_bin);
7430
bzero(ptr,Field_blob::pack_length());
7434
if (from == value.ptr())
7436
uint32 dummy_offset;
7437
if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
7439
Field_blob::store_length(length);
7440
bmove(ptr+packlength,(char*) &from,sizeof(char*));
7443
if (tmpstr.copy(from, length, cs))
7448
new_length= min(max_data_length(), field_charset->mbmaxlen * length);
7449
if (value.alloc(new_length))
7453
if (f_is_hex_escape(flags))
7455
copy_length= my_copy_with_hex_escaping(field_charset,
7456
(char*) value.ptr(), new_length,
7458
Field_blob::store_length(copy_length);
7460
bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
7464
"length" is OK as "nchars" argument to well_formed_copy_nchars as this
7465
is never used to limit the length of the data. The cut of long data
7466
is done with the new_length value.
7468
copy_length= well_formed_copy_nchars(field_charset,
7469
(char*) value.ptr(), new_length,
7472
&well_formed_error_pos,
7473
&cannot_convert_error_pos,
7476
Field_blob::store_length(copy_length);
7478
bmove(ptr+packlength,(uchar*) &tmp,sizeof(char*));
7480
if (check_string_copy_error(this, well_formed_error_pos,
7481
cannot_convert_error_pos, from + length, cs))
7484
return report_if_important_data(from_end_pos, from + length);
7487
/* Fatal OOM error */
7488
bzero(ptr,Field_blob::pack_length());
7493
int Field_blob::store(double nr)
7495
CHARSET_INFO *cs=charset();
7496
value.set_real(nr, NOT_FIXED_DEC, cs);
7497
return Field_blob::store(value.ptr(),(uint) value.length(), cs);
7501
int Field_blob::store(longlong nr, bool unsigned_val)
7503
CHARSET_INFO *cs=charset();
7504
value.set_int(nr, unsigned_val, cs);
7505
return Field_blob::store(value.ptr(), (uint) value.length(), cs);
7509
double Field_blob::val_real(void)
7511
ASSERT_COLUMN_MARKED_FOR_READ;
7513
char *end_not_used, *blob;
7517
memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
7520
length= get_length(ptr);
7522
return my_strntod(cs, blob, length, &end_not_used, ¬_used);
7526
longlong Field_blob::val_int(void)
7528
ASSERT_COLUMN_MARKED_FOR_READ;
7531
memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
7534
uint32 length=get_length(ptr);
7535
return my_strntoll(charset(),blob,length,10,NULL,¬_used);
7538
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
7541
ASSERT_COLUMN_MARKED_FOR_READ;
7543
memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
7545
val_ptr->set("",0,charset()); // A bit safer than ->length(0)
7547
val_ptr->set((const char*) blob,get_length(ptr),charset());
7552
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
7554
ASSERT_COLUMN_MARKED_FOR_READ;
7557
memcpy_fixed(&blob, ptr+packlength, sizeof(const uchar*));
7564
length= get_length(ptr);
7566
str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
7568
return decimal_value;
7572
int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
7575
return field_charset->coll->strnncollsp(field_charset,
7576
a, a_length, b, b_length,
7581
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
7584
uchar *blob1,*blob2;
7585
memcpy_fixed(&blob1,a_ptr+packlength,sizeof(char*));
7586
memcpy_fixed(&blob2,b_ptr+packlength,sizeof(char*));
7587
uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
7588
set_if_smaller(a_len, max_length);
7589
set_if_smaller(b_len, max_length);
7590
return Field_blob::cmp(blob1,a_len,blob2,b_len);
7594
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7599
uint32 a_length,b_length;
7600
memcpy_fixed(&a,a_ptr+packlength,sizeof(char*));
7601
memcpy_fixed(&b,b_ptr+packlength,sizeof(char*));
7602
a_length=get_length(a_ptr);
7603
if (a_length > max_length)
7604
a_length=max_length;
7605
b_length=get_length(b_ptr);
7606
if (b_length > max_length)
7607
b_length=max_length;
7608
diff=memcmp(a,b,min(a_length,b_length));
7609
return diff ? diff : (int) (a_length - b_length);
7613
/* The following is used only when comparing a key */
7615
uint Field_blob::get_key_image(uchar *buff,uint length, imagetype type_arg)
7617
uint32 blob_length= get_length(ptr);
7621
uint local_char_length= length / field_charset->mbmaxlen;
7622
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
7624
set_if_smaller(blob_length, local_char_length);
7626
if ((uint32) length > blob_length)
7629
Must clear this as we do a memcmp in opt_range.cc to detect
7632
bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
7633
length=(uint) blob_length;
7635
int2store(buff,length);
7636
memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
7637
return HA_KEY_BLOB_LENGTH+length;
7641
void Field_blob::set_key_image(const uchar *buff,uint length)
7643
length= uint2korr(buff);
7644
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
7649
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
7652
uint blob_length=get_length(ptr);
7653
memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
7654
CHARSET_INFO *cs= charset();
7655
uint local_char_length= max_key_length / cs->mbmaxlen;
7656
local_char_length= my_charpos(cs, blob1, blob1+blob_length,
7658
set_if_smaller(blob_length, local_char_length);
7659
return Field_blob::cmp(blob1, blob_length,
7660
key_ptr+HA_KEY_BLOB_LENGTH,
7661
uint2korr(key_ptr));
7664
int Field_blob::key_cmp(const uchar *a,const uchar *b)
7666
return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
7667
b+HA_KEY_BLOB_LENGTH, uint2korr(b));
7672
Save the field metadata for blob fields.
7674
Saves the pack length in the first byte of the field metadata array
7675
at index of *metadata_ptr.
7677
@param metadata_ptr First byte of field metadata
7679
@returns number of bytes written to metadata_ptr
7681
int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
7683
*metadata_ptr= pack_length_no_ptr();
7688
uint32 Field_blob::sort_length() const
7690
return (uint32) (current_thd->variables.max_sort_length +
7691
(field_charset == &my_charset_bin ? 0 : packlength));
7695
void Field_blob::sort_string(uchar *to,uint length)
7698
uint blob_length=get_length();
7704
if (field_charset == &my_charset_bin)
7709
Store length of blob last in blob to shorter blobs before longer blobs
7711
length-= packlength;
7714
switch (packlength) {
7716
*pos= (char) blob_length;
7719
mi_int2store(pos, blob_length);
7722
mi_int3store(pos, blob_length);
7725
mi_int4store(pos, blob_length);
7729
memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
7731
blob_length=my_strnxfrm(field_charset,
7732
to, length, blob, blob_length);
7733
DBUG_ASSERT(blob_length == length);
7738
void Field_blob::sql_type(String &res) const
7742
switch (packlength) {
7743
default: str="tiny"; length=4; break;
7744
case 2: str=""; length=0; break;
7745
case 3: str="medium"; length= 6; break;
7746
case 4: str="long"; length=4; break;
7748
res.set_ascii(str,length);
7749
if (charset() == &my_charset_bin)
7750
res.append(STRING_WITH_LEN("blob"));
7753
res.append(STRING_WITH_LEN("text"));
7757
uchar *Field_blob::pack(uchar *to, const uchar *from,
7758
uint max_length, bool low_byte_first)
7760
DBUG_ENTER("Field_blob::pack");
7761
DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
7762
" max_length: %u; low_byte_first: %d",
7763
(ulong) to, (ulong) from,
7764
max_length, low_byte_first));
7765
DBUG_DUMP("record", from, table->s->reclength);
7768
uint32 length=get_length(); // Length of from string
7771
Store max length, which will occupy packlength bytes. If the max
7772
length given is smaller than the actual length of the blob, we
7773
just store the initial bytes of the blob.
7775
store_length(to, packlength, min(length, max_length), low_byte_first);
7778
Store the actual blob data, which will occupy 'length' bytes.
7782
get_ptr((uchar**) &from);
7783
memcpy(to+packlength, from,length);
7785
ptr=save; // Restore org row pointer
7786
DBUG_DUMP("packed", to, packlength + length);
7787
DBUG_RETURN(to+packlength+length);
7792
Unpack a blob field from row data.
7794
This method is used to unpack a blob field from a master whose size of
7795
the field is less than that of the slave. Note: This method is included
7796
to satisfy inheritance rules, but is not needed for blob fields. It
7797
simply is used as a pass-through to the original unpack() method for
7800
@param to Destination of the data
7801
@param from Source of the data
7802
@param param_data @c TRUE if base types should be stored in little-
7803
endian format, @c FALSE if native format should
7806
@return New pointer into memory based on from + length of the data
7808
const uchar *Field_blob::unpack(uchar *to,
7811
bool low_byte_first)
7813
DBUG_ENTER("Field_blob::unpack");
7814
DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
7815
" param_data: %u; low_byte_first: %d",
7816
(ulong) to, (ulong) from, param_data, low_byte_first));
7817
uint const master_packlength=
7818
param_data > 0 ? param_data & 0xFF : packlength;
7819
uint32 const length= get_length(from, master_packlength, low_byte_first);
7820
DBUG_DUMP("packed", from, length + master_packlength);
7821
bitmap_set_bit(table->write_set, field_index);
7822
store(reinterpret_cast<const char*>(from) + master_packlength,
7823
length, field_charset);
7824
DBUG_DUMP("record", to, table->s->reclength);
7825
DBUG_RETURN(from + master_packlength + length);
7828
/* Keys for blobs are like keys on varchars */
7830
int Field_blob::pack_cmp(const uchar *a, const uchar *b, uint key_length_arg,
7831
my_bool insert_or_update)
7833
uint a_length, b_length;
7834
if (key_length_arg > 255)
7836
a_length=uint2korr(a); a+=2;
7837
b_length=uint2korr(b); b+=2;
7841
a_length= (uint) *a++;
7842
b_length= (uint) *b++;
7844
return field_charset->coll->strnncollsp(field_charset,
7851
int Field_blob::pack_cmp(const uchar *b, uint key_length_arg,
7852
my_bool insert_or_update)
7855
uint a_length, b_length;
7856
memcpy_fixed(&a,ptr+packlength,sizeof(char*));
7858
return key_length_arg > 0 ? -1 : 0;
7860
a_length= get_length(ptr);
7861
if (key_length_arg > 255)
7863
b_length= uint2korr(b); b+=2;
7866
b_length= (uint) *b++;
7867
return field_charset->coll->strnncollsp(field_charset,
7873
/** Create a packed key that will be used for storage from a MySQL row. */
7876
Field_blob::pack_key(uchar *to, const uchar *from, uint max_length,
7877
bool low_byte_first __attribute__((unused)))
7881
uint32 length=get_length(); // Length of from string
7882
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
7883
max_length/field_charset->mbmaxlen : max_length);
7885
get_ptr((uchar**) &from);
7886
if (length > local_char_length)
7887
local_char_length= my_charpos(field_charset, from, from+length,
7889
set_if_smaller(length, local_char_length);
7890
*to++= (uchar) length;
7891
if (max_length > 255) // 2 byte length
7892
*to++= (uchar) (length >> 8);
7893
memcpy(to, from, length);
7894
ptr=save; // Restore org row pointer
7900
Unpack a blob key into a record buffer.
7902
A blob key has a maximum size of 64K-1.
7903
In its packed form, the length field is one or two bytes long,
7904
depending on 'max_length'.
7905
Depending on the maximum length of a blob, its length field is
7906
put into 1 to 4 bytes. This is a property of the blob object,
7907
described by 'packlength'.
7908
Blobs are internally stored apart from the record buffer, which
7909
contains a pointer to the blob buffer.
7912
@param to Pointer into the record buffer.
7913
@param from Pointer to the packed key.
7914
@param max_length Key length limit from key description.
7917
Pointer into 'from' past the last byte copied from packed key.
7921
Field_blob::unpack_key(uchar *to, const uchar *from, uint max_length,
7922
bool low_byte_first __attribute__((unused)))
7924
/* get length of the blob key */
7925
uint32 length= *from++;
7926
if (max_length > 255)
7927
length+= *from++ << 8;
7929
/* put the length into the record buffer */
7930
put_length(to, length);
7932
/* put the address of the blob buffer or NULL */
7934
memcpy_fixed(to + packlength, &from, sizeof(from));
7936
bzero(to + packlength, sizeof(from));
7938
/* point to first byte of next field in 'from' */
7939
return from + length;
7943
/** Create a packed key that will be used for storage from a MySQL key. */
7946
Field_blob::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
7947
bool low_byte_first __attribute__((unused)))
7949
uint length=uint2korr(from);
7950
if (length > max_length)
7952
*to++= (char) (length & 255);
7953
if (max_length > 255)
7954
*to++= (char) (length >> 8);
7956
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
7961
uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
7964
return uint2korr(data_ptr)+2;
7965
return (uint) *data_ptr + 1;
7969
uint Field_blob::max_packed_col_length(uint max_length)
7971
return (max_length > 255 ? 2 : 1)+max_length;
7975
uint Field_blob::is_equal(Create_field *new_field)
7977
if (compare_str_field_flags(new_field, flags))
7980
return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
7981
&& new_field->charset == field_charset &&
7982
((Field_blob *)new_field->field)->max_data_length() ==
7987
/****************************************************************************
7989
** This is a string which only can have a selection of different values.
7990
** If one uses this string in a number context one gets the type number.
7991
****************************************************************************/
7993
enum ha_base_keytype Field_enum::key_type() const
7995
switch (packlength) {
7996
default: return HA_KEYTYPE_BINARY;
7997
case 2: return HA_KEYTYPE_USHORT_INT;
7998
case 3: return HA_KEYTYPE_UINT24;
7999
case 4: return HA_KEYTYPE_ULONG_INT;
8000
case 8: return HA_KEYTYPE_ULONGLONG;
8004
void Field_enum::store_type(uint64_t value)
8006
switch (packlength) {
8007
case 1: ptr[0]= (uchar) value; break;
8009
#ifdef WORDS_BIGENDIAN
8010
if (table->s->db_low_byte_first)
8012
int2store(ptr,(unsigned short) value);
8016
shortstore(ptr,(unsigned short) value);
8018
case 3: int3store(ptr,(long) value); break;
8020
#ifdef WORDS_BIGENDIAN
8021
if (table->s->db_low_byte_first)
8023
int4store(ptr,value);
8027
longstore(ptr,(long) value);
8030
#ifdef WORDS_BIGENDIAN
8031
if (table->s->db_low_byte_first)
8033
int8store(ptr,value);
8037
longlongstore(ptr,value); break;
8044
Storing a empty string in a enum field gives a warning
8045
(if there isn't a empty value in the enum)
8048
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
8050
ASSERT_COLUMN_MARKED_FOR_WRITE;
8053
char buff[STRING_BUFFER_USUAL_SIZE];
8054
String tmpstr(buff,sizeof(buff), &my_charset_bin);
8056
/* Convert character set if necessary */
8057
if (String::needs_conversion(length, cs, field_charset, ¬_used))
8060
tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
8062
length= tmpstr.length();
8065
/* Remove end space */
8066
length= field_charset->cset->lengthsp(field_charset, from, length);
8067
uint tmp=find_type2(typelib, from, length, field_charset);
8070
if (length < 6) // Can't be more than 99999 enums
8072
/* This is for reading numbers with LOAD DATA INFILE */
8074
tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
8075
if (err || end != from+length || tmp > typelib->count)
8078
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8080
if (!table->in_use->count_cuted_fields)
8084
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8086
store_type((uint64_t) tmp);
8091
int Field_enum::store(double nr)
8093
return Field_enum::store((longlong) nr, FALSE);
8097
int Field_enum::store(longlong nr, bool unsigned_val)
8099
ASSERT_COLUMN_MARKED_FOR_WRITE;
8101
if ((uint64_t) nr > typelib->count || nr == 0)
8103
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8104
if (nr != 0 || table->in_use->count_cuted_fields)
8110
store_type((uint64_t) (uint) nr);
8115
double Field_enum::val_real(void)
8117
return (double) Field_enum::val_int();
8121
longlong Field_enum::val_int(void)
8123
ASSERT_COLUMN_MARKED_FOR_READ;
8124
switch (packlength) {
8126
return (longlong) ptr[0];
8130
#ifdef WORDS_BIGENDIAN
8131
if (table->s->db_low_byte_first)
8136
return (longlong) tmp;
8139
return (longlong) uint3korr(ptr);
8143
#ifdef WORDS_BIGENDIAN
8144
if (table->s->db_low_byte_first)
8149
return (longlong) tmp;
8154
#ifdef WORDS_BIGENDIAN
8155
if (table->s->db_low_byte_first)
8159
longlongget(tmp,ptr);
8163
return 0; // impossible
8168
Save the field metadata for enum fields.
8170
Saves the real type in the first byte and the pack length in the
8171
second byte of the field metadata array at index of *metadata_ptr and
8172
*(metadata_ptr + 1).
8174
@param metadata_ptr First byte of field metadata
8176
@returns number of bytes written to metadata_ptr
8178
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
8180
*metadata_ptr= real_type();
8181
*(metadata_ptr + 1)= pack_length();
8186
String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
8189
uint tmp=(uint) Field_enum::val_int();
8190
if (!tmp || tmp > typelib->count)
8191
val_ptr->set("", 0, field_charset);
8193
val_ptr->set((const char*) typelib->type_names[tmp-1],
8194
typelib->type_lengths[tmp-1],
8199
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
8202
ptr= (uchar*) a_ptr;
8203
uint64_t a=Field_enum::val_int();
8204
ptr= (uchar*) b_ptr;
8205
uint64_t b=Field_enum::val_int();
8207
return (a < b) ? -1 : (a > b) ? 1 : 0;
8210
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
8212
uint64_t value=Field_enum::val_int();
8214
for (uint i=0 ; i < packlength ; i++)
8216
*to-- = (uchar) (value & 255);
8222
void Field_enum::sql_type(String &res) const
8225
String enum_item(buffer, sizeof(buffer), res.charset());
8228
res.append(STRING_WITH_LEN("enum("));
8231
uint *len= typelib->type_lengths;
8232
for (const char **pos= typelib->type_names; *pos; pos++, len++)
8237
/* convert to res.charset() == utf8, then quote */
8238
enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8239
append_unescaped(&res, enum_item.ptr(), enum_item.length());
8246
Field *Field_enum::new_field(MEM_ROOT *root, struct st_table *new_table,
8249
Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
8251
res->typelib= copy_typelib(root, typelib);
8258
This is a string which can have a collection of different values.
8259
Each string value is separated with a ','.
8260
For example "One,two,five"
8261
If one uses this string in a number context one gets the bits as a longlong
8266
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
8268
ASSERT_COLUMN_MARKED_FOR_WRITE;
8269
bool got_warning= 0;
8273
uint32 not_used_offset;
8274
char buff[STRING_BUFFER_USUAL_SIZE];
8275
String tmpstr(buff,sizeof(buff), &my_charset_bin);
8277
/* Convert character set if necessary */
8278
if (String::needs_conversion(length, cs, field_charset, ¬_used_offset))
8281
tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
8283
length= tmpstr.length();
8285
uint64_t tmp= find_set(typelib, from, length, field_charset,
8286
¬_used, ¬_used2, &got_warning);
8287
if (!tmp && length && length < 22)
8289
/* This is for reading numbers with LOAD DATA INFILE */
8291
tmp=my_strntoull(cs,from,length,10,&end,&err);
8292
if (err || end != from+length ||
8293
tmp > (uint64_t) (((longlong) 1 << typelib->count) - (longlong) 1))
8296
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8299
else if (got_warning)
8300
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8306
int Field_set::store(longlong nr, bool unsigned_val)
8308
ASSERT_COLUMN_MARKED_FOR_WRITE;
8310
uint64_t max_nr= set_bits(uint64_t, typelib->count);
8311
if ((uint64_t) nr > max_nr)
8314
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8317
store_type((uint64_t) nr);
8322
String *Field_set::val_str(String *val_buffer,
8323
String *val_ptr __attribute__((unused)))
8325
uint64_t tmp=(uint64_t) Field_enum::val_int();
8328
val_buffer->length(0);
8329
val_buffer->set_charset(field_charset);
8330
while (tmp && bitnr < (uint) typelib->count)
8334
if (val_buffer->length())
8335
val_buffer->append(&field_separator, 1, &my_charset_latin1);
8336
String str(typelib->type_names[bitnr],
8337
typelib->type_lengths[bitnr],
8339
val_buffer->append(str);
8348
void Field_set::sql_type(String &res) const
8351
String set_item(buffer, sizeof(buffer), res.charset());
8354
res.append(STRING_WITH_LEN("set("));
8357
uint *len= typelib->type_lengths;
8358
for (const char **pos= typelib->type_names; *pos; pos++, len++)
8363
/* convert to res.charset() == utf8, then quote */
8364
set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8365
append_unescaped(&res, set_item.ptr(), set_item.length());
8373
1 if the fields are equally defined
8375
0 if the fields are unequally defined
8378
bool Field::eq_def(Field *field)
8380
if (real_type() != field->real_type() || charset() != field->charset() ||
8381
pack_length() != field->pack_length())
8388
returns 1 if the fields are equally defined
8390
bool Field_enum::eq_def(Field *field)
8392
if (!Field::eq_def(field))
8394
TYPELIB *from_lib=((Field_enum*) field)->typelib;
8396
if (typelib->count < from_lib->count)
8398
for (uint i=0 ; i < from_lib->count ; i++)
8399
if (my_strnncoll(field_charset,
8400
(const uchar*)typelib->type_names[i],
8401
strlen(typelib->type_names[i]),
8402
(const uchar*)from_lib->type_names[i],
8403
strlen(from_lib->type_names[i])))
8410
returns 1 if the fields are equally defined
8412
bool Field_num::eq_def(Field *field)
8414
if (!Field::eq_def(field))
8416
Field_num *from_num= (Field_num*) field;
8418
if (unsigned_flag != from_num->unsigned_flag ||
8419
(zerofill && !from_num->zerofill && !zero_pack()) ||
8420
dec != from_num->dec)
8426
uint Field_num::is_equal(Create_field *new_field)
8428
return ((new_field->sql_type == real_type()) &&
8429
((new_field->flags & UNSIGNED_FLAG) == (uint) (flags &
8431
((new_field->flags & AUTO_INCREMENT_FLAG) ==
8432
(uint) (flags & AUTO_INCREMENT_FLAG)) &&
8433
(new_field->length <= max_display_length()));
8440
We store the first 0 - 6 uneven bits among the null bits
8441
at the start of the record. The rest bytes are stored in
8446
CREATE TABLE t1 (a int, b bit(17), c bit(21) not null, d bit(8));
8447
We would store data as follows in the record:
8450
1 7 - reserve for delete
8451
6 - null bit for 'a'
8452
5 - null bit for 'b'
8453
4 - first (high) bit of 'b'
8454
3 - first (high) bit of 'c'
8455
2 - second bit of 'c'
8456
1 - third bit of 'c'
8457
0 - forth bit of 'c'
8458
2 7 - firth bit of 'c'
8459
6 - null bit for 'd'
8460
3 - 6 four bytes for 'a'
8461
7 - 8 two bytes for 'b'
8462
9 - 10 two bytes for 'c'
8466
Field_bit::Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
8467
uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
8468
enum utype unireg_check_arg, const char *field_name_arg)
8469
: Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
8470
unireg_check_arg, field_name_arg),
8471
bit_ptr(bit_ptr_arg), bit_ofs(bit_ofs_arg), bit_len(len_arg & 7),
8472
bytes_in_rec(len_arg / 8)
8474
flags|= UNSIGNED_FLAG;
8476
Ensure that Field::eq() can distinguish between two different bit fields.
8477
(two bit fields that are not null, may have same ptr and null_ptr)
8480
null_bit= bit_ofs_arg;
8484
void Field_bit::hash(ulong *nr, ulong *nr2)
8488
*nr^= (*nr << 1) | 1;
8492
CHARSET_INFO *cs= &my_charset_bin;
8493
longlong value= Field_bit::val_int();
8495
mi_int8store(tmp,value);
8496
cs->coll->hash_sort(cs, tmp, 8, nr, nr2);
8502
Field_bit::do_last_null_byte() const
8505
Code elsewhere is assuming that bytes are 8 bits, so I'm using
8506
that value instead of the correct one: CHAR_BIT.
8508
REFACTOR SUGGESTION (Matz): Change to use the correct number of
8509
bits. On systems with CHAR_BIT > 8 (not very common), the storage
8510
will lose the extra bits.
8512
DBUG_PRINT("test", ("bit_ofs: %d, bit_len: %d bit_ptr: 0x%lx",
8513
bit_ofs, bit_len, (long) bit_ptr));
8517
else if (bit_ofs + bit_len > 8)
8518
result= bit_ptr + 1;
8523
return (size_t) (result - table->record[0]) + 1;
8524
return LAST_NULL_BYTE_UNDEF;
8528
Field *Field_bit::new_key_field(MEM_ROOT *root,
8529
struct st_table *new_table,
8530
uchar *new_ptr, uchar *new_null_ptr,
8534
if ((res= (Field_bit*) Field::new_key_field(root, new_table,
8535
new_ptr, new_null_ptr,
8538
/* Move bits normally stored in null_pointer to new_ptr */
8539
res->bit_ptr= new_ptr;
8542
res->ptr++; // Store rest of data here
8548
uint Field_bit::is_equal(Create_field *new_field)
8550
return (new_field->sql_type == real_type() &&
8551
new_field->length == max_display_length());
8555
int Field_bit::store(const char *from, uint length, CHARSET_INFO *cs)
8557
ASSERT_COLUMN_MARKED_FOR_WRITE;
8560
for (; length && !*from; from++, length--) {}; // skip left 0's
8561
delta= bytes_in_rec - length;
8564
(delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
8565
(!bit_len && delta < 0))
8567
set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
8568
memset(ptr, 0xff, bytes_in_rec);
8569
if (table->in_use->really_abort_on_warning())
8570
set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
8572
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
8575
/* delta is >= -1 here */
8579
clr_rec_bits(bit_ptr, bit_ofs, bit_len);
8581
memcpy(ptr + delta, from, length);
8583
else if (delta == 0)
8586
clr_rec_bits(bit_ptr, bit_ofs, bit_len);
8587
memcpy(ptr, from, length);
8593
set_rec_bits((uchar) *from, bit_ptr, bit_ofs, bit_len);
8596
memcpy(ptr, from, bytes_in_rec);
8602
int Field_bit::store(double nr)
8604
return Field_bit::store((longlong) nr, FALSE);
8608
int Field_bit::store(longlong nr, bool unsigned_val)
8612
mi_int8store(buf, nr);
8613
return store(buf, 8, NULL);
8617
int Field_bit::store_decimal(const my_decimal *val)
8620
longlong i= convert_decimal2longlong(val, 1, &err);
8621
return test(err | store(i, TRUE));
8625
double Field_bit::val_real(void)
8627
return (double) Field_bit::val_int();
8631
longlong Field_bit::val_int(void)
8633
ASSERT_COLUMN_MARKED_FOR_READ;
8637
bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
8638
bits<<= (bytes_in_rec * 8);
8641
switch (bytes_in_rec) {
8642
case 0: return bits;
8643
case 1: return bits | (uint64_t) ptr[0];
8644
case 2: return bits | mi_uint2korr(ptr);
8645
case 3: return bits | mi_uint3korr(ptr);
8646
case 4: return bits | mi_uint4korr(ptr);
8647
case 5: return bits | mi_uint5korr(ptr);
8648
case 6: return bits | mi_uint6korr(ptr);
8649
case 7: return bits | mi_uint7korr(ptr);
8650
default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
8655
String *Field_bit::val_str(String *val_buffer,
8656
String *val_ptr __attribute__((unused)))
8658
ASSERT_COLUMN_MARKED_FOR_READ;
8659
char buff[sizeof(longlong)];
8660
uint length= min(pack_length(), sizeof(longlong));
8661
uint64_t bits= val_int();
8662
mi_int8store(buff,bits);
8664
val_buffer->alloc(length);
8665
memcpy_fixed((char*) val_buffer->ptr(), buff+8-length, length);
8666
val_buffer->length(length);
8667
val_buffer->set_charset(&my_charset_bin);
8672
my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
8674
ASSERT_COLUMN_MARKED_FOR_READ;
8675
int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
8676
return deciaml_value;
8681
Compare two bit fields using pointers within the record.
8684
a Pointer to field->ptr in first record
8685
b Pointer to field->ptr in second record
8686
max_len Maximum length used in index
8688
This method is used from key_rec_cmp used by merge sorts used
8689
by partitioned index read and later other similar places.
8690
The a and b pointer must be pointers to the field in a record
8691
(not the table->record[0] necessarily)
8693
int Field_bit::cmp_max(const uchar *a, const uchar *b, uint max_len)
8695
my_ptrdiff_t a_diff= a - ptr;
8696
my_ptrdiff_t b_diff= b - ptr;
8700
uchar bits_a= get_rec_bits(bit_ptr+a_diff, bit_ofs, bit_len);
8701
uchar bits_b= get_rec_bits(bit_ptr+b_diff, bit_ofs, bit_len);
8702
if ((flag= (int) (bits_a - bits_b)))
8705
return memcmp(a, b, field_length);
8709
int Field_bit::key_cmp(const uchar *str, uint length)
8714
uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
8715
if ((flag= (int) (bits - *str)))
8720
return memcmp(ptr, str, length);
8724
int Field_bit::cmp_offset(uint row_offset)
8729
uchar bits_a= get_rec_bits(bit_ptr, bit_ofs, bit_len);
8730
uchar bits_b= get_rec_bits(bit_ptr + row_offset, bit_ofs, bit_len);
8731
if ((flag= (int) (bits_a - bits_b)))
8734
return memcmp(ptr, ptr + row_offset, bytes_in_rec);
8738
uint Field_bit::get_key_image(uchar *buff, uint length, imagetype type_arg)
8742
uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
8746
uint data_length = min(length, bytes_in_rec);
8747
memcpy(buff, ptr, data_length);
8748
return data_length + 1;
8753
Save the field metadata for bit fields.
8755
Saves the bit length in the first byte and bytes in record in the
8756
second byte of the field metadata array at index of *metadata_ptr and
8757
*(metadata_ptr + 1).
8759
@param metadata_ptr First byte of field metadata
8761
@returns number of bytes written to metadata_ptr
8763
int Field_bit::do_save_field_metadata(uchar *metadata_ptr)
8765
*metadata_ptr= bit_len;
8766
*(metadata_ptr + 1)= bytes_in_rec;
8772
Returns the number of bytes field uses in row-based replication
8775
This method is used in row-based replication to determine the number
8776
of bytes that the field consumes in the row record format. This is
8777
used to skip fields in the master that do not exist on the slave.
8779
@param field_metadata Encoded size in field metadata
8781
@returns The size of the field based on the field metadata.
8783
uint Field_bit::pack_length_from_metadata(uint field_metadata)
8785
uint const from_len= (field_metadata >> 8U) & 0x00ff;
8786
uint const from_bit_len= field_metadata & 0x00ff;
8787
uint const source_size= from_len + ((from_bit_len > 0) ? 1 : 0);
8788
return (source_size);
8793
Check to see if field size is compatible with destination.
8795
This method is used in row-based replication to verify that the slave's
8796
field size is less than or equal to the master's field size. The
8797
encoded field metadata (from the master or source) is decoded and compared
8798
to the size of this field (the slave or destination).
8800
@param field_metadata Encoded size in field metadata
8802
@retval 0 if this field's size is < the source field's size
8803
@retval 1 if this field's size is >= the source field's size
8805
int Field_bit::compatible_field_size(uint field_metadata)
8808
uint const source_size= pack_length_from_metadata(field_metadata);
8809
uint const destination_size= row_pack_length();
8810
uint const from_bit_len= field_metadata & 0x00ff;
8811
uint const from_len= (field_metadata >> 8U) & 0x00ff;
8812
if ((bit_len == 0) || (from_bit_len == 0))
8813
compatible= (source_size <= destination_size);
8814
else if (from_bit_len > bit_len)
8815
compatible= (from_len < bytes_in_rec);
8817
compatible= ((from_bit_len <= bit_len) && (from_len <= bytes_in_rec));
8818
return (compatible);
8823
void Field_bit::sql_type(String &res) const
8825
CHARSET_INFO *cs= res.charset();
8826
ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
8827
"bit(%d)", (int) field_length);
8828
res.length((uint) length);
8833
Field_bit::pack(uchar *to, const uchar *from, uint max_length,
8834
bool low_byte_first __attribute__((unused)))
8836
DBUG_ASSERT(max_length > 0);
8841
We have the following:
8843
ptr Points into a field in record R1
8844
from Points to a field in a record R2
8845
bit_ptr Points to the byte (in the null bytes) that holds the
8847
from_bitp Points to the byte that holds the odd bits of R2
8849
We have the following:
8851
ptr - bit_ptr = from - from_bitp
8853
We want to isolate 'from_bitp', so this gives:
8855
ptr - bit_ptr - from = - from_bitp
8856
- ptr + bit_ptr + from = from_bitp
8857
bit_ptr + from - ptr = from_bitp
8859
uchar bits= get_rec_bits(bit_ptr + (from - ptr), bit_ofs, bit_len);
8862
length= min(bytes_in_rec, max_length - (bit_len > 0));
8863
memcpy(to, from, length);
8869
Unpack a bit field from row data.
8871
This method is used to unpack a bit field from a master whose size
8872
of the field is less than that of the slave.
8874
@param to Destination of the data
8875
@param from Source of the data
8876
@param param_data Bit length (upper) and length (lower) values
8878
@return New pointer into memory based on from + length of the data
8881
Field_bit::unpack(uchar *to, const uchar *from, uint param_data,
8882
bool low_byte_first __attribute__((unused)))
8884
uint const from_len= (param_data >> 8U) & 0x00ff;
8885
uint const from_bit_len= param_data & 0x00ff;
8887
If the parameter data is zero (i.e., undefined), or if the master
8888
and slave have the same sizes, then use the old unpack() method.
8890
if (param_data == 0 || ((from_bit_len == bit_len) && (from_len == bytes_in_rec)))
8895
set_rec_bits is a macro, don't put the post-increment in the
8896
argument since that might cause strange side-effects.
8898
For the choice of the second argument, see the explanation for
8901
set_rec_bits(*from, bit_ptr + (to - ptr), bit_ofs, bit_len);
8904
memcpy(to, from, bytes_in_rec);
8905
return from + bytes_in_rec;
8909
We are converting a smaller bit field to a larger one here.
8910
To do that, we first need to construct a raw value for the original
8911
bit value stored in the from buffer. Then that needs to be converted
8912
to the larger field then sent to store() for writing to the field.
8913
Lastly the odd bits need to be masked out if the bytes_in_rec > 0.
8914
Otherwise stray bits can cause spurious values.
8916
uint new_len= (field_length + 7) / 8;
8917
char *value= (char *)my_alloca(new_len);
8918
bzero(value, new_len);
8919
uint len= from_len + ((from_bit_len > 0) ? 1 : 0);
8920
memcpy(value + (new_len - len), from, len);
8922
Mask out the unused bits in the partial byte.
8923
TODO: Add code to the master to always mask these bits and remove
8926
if ((from_bit_len > 0) && (from_len > 0))
8927
value[new_len - len]= value[new_len - len] & ((1U << from_bit_len) - 1);
8928
bitmap_set_bit(table->write_set,field_index);
8929
store(value, new_len, system_charset_info);
8935
void Field_bit::set_default()
8939
my_ptrdiff_t const offset= table->s->default_values - table->record[0];
8940
uchar bits= get_rec_bits(bit_ptr + offset, bit_ofs, bit_len);
8941
set_rec_bits(bits, bit_ptr, bit_ofs, bit_len);
8943
Field::set_default();
8947
Bit field support for non-MyISAM tables.
8950
Field_bit_as_char::Field_bit_as_char(uchar *ptr_arg, uint32 len_arg,
8951
uchar *null_ptr_arg, uchar null_bit_arg,
8952
enum utype unireg_check_arg,
8953
const char *field_name_arg)
8954
:Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0, 0,
8955
unireg_check_arg, field_name_arg)
8957
flags|= UNSIGNED_FLAG;
8959
bytes_in_rec= (len_arg + 7) / 8;
8963
int Field_bit_as_char::store(const char *from, uint length, CHARSET_INFO *cs)
8965
ASSERT_COLUMN_MARKED_FOR_WRITE;
8967
uchar bits= (uchar) (field_length & 7);
8969
for (; length && !*from; from++, length--) {}; // skip left 0's
8970
delta= bytes_in_rec - length;
8973
(delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
8975
memset(ptr, 0xff, bytes_in_rec);
8977
*ptr&= ((1 << bits) - 1); /* set first uchar */
8978
if (table->in_use->really_abort_on_warning())
8979
set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
8981
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
8985
memcpy(ptr + delta, from, length);
8990
void Field_bit_as_char::sql_type(String &res) const
8992
CHARSET_INFO *cs= res.charset();
8993
ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
8994
"bit(%d)", (int) field_length);
8995
res.length((uint) length);
8999
/*****************************************************************************
9000
Handling of field and Create_field
9001
*****************************************************************************/
9004
Convert create_field::length from number of characters to number of bytes.
9007
void Create_field::create_length_to_internal_length(void)
9010
case MYSQL_TYPE_TINY_BLOB:
9011
case MYSQL_TYPE_MEDIUM_BLOB:
9012
case MYSQL_TYPE_LONG_BLOB:
9013
case MYSQL_TYPE_BLOB:
9014
case MYSQL_TYPE_VAR_STRING:
9015
case MYSQL_TYPE_STRING:
9016
case MYSQL_TYPE_VARCHAR:
9017
length*= charset->mbmaxlen;
9019
pack_length= calc_pack_length(sql_type, length);
9021
case MYSQL_TYPE_ENUM:
9022
case MYSQL_TYPE_SET:
9023
/* Pack_length already calculated in sql_parse.cc */
9024
length*= charset->mbmaxlen;
9025
key_length= pack_length;
9027
case MYSQL_TYPE_BIT:
9028
if (f_bit_as_char(pack_flag))
9030
key_length= pack_length= ((length + 7) & ~7) / 8;
9034
pack_length= length / 8;
9035
/* We need one extra byte to store the bits we save among the null bits */
9036
key_length= pack_length + test(length & 7);
9039
case MYSQL_TYPE_NEWDECIMAL:
9040
key_length= pack_length=
9041
my_decimal_get_binary_size(my_decimal_length_to_precision(length,
9048
key_length= pack_length= calc_pack_length(sql_type, length);
9055
Init for a tmp table field. To be extended if need be.
9057
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
9058
uint32 length_arg, uint32 decimals_arg,
9059
bool maybe_null, bool is_unsigned)
9062
sql_type= sql_type_arg;
9063
char_length= length= length_arg;;
9064
unireg_check= Field::NONE;
9066
charset= &my_charset_bin;
9067
pack_flag= (FIELDFLAG_NUMBER |
9068
((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
9069
(maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
9070
(is_unsigned ? 0 : FIELDFLAG_DECIMAL));
9075
Initialize field definition for create.
9077
@param thd Thread handle
9078
@param fld_name Field name
9079
@param fld_type Field type
9080
@param fld_length Field length
9081
@param fld_decimals Decimal (if any)
9082
@param fld_type_modifier Additional type information
9083
@param fld_default_value Field default value (if any)
9084
@param fld_on_update_value The value of ON UPDATE clause
9085
@param fld_comment Field comment
9086
@param fld_change Field change
9087
@param fld_interval_list Interval list (if any)
9088
@param fld_charset Field charset
9096
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
9097
char *fld_length, char *fld_decimals,
9098
uint fld_type_modifier, Item *fld_default_value,
9099
Item *fld_on_update_value, LEX_STRING *fld_comment,
9100
char *fld_change, List<String> *fld_interval_list,
9101
CHARSET_INFO *fld_charset, uint fld_geom_type,
9102
enum ha_storage_media storage_type,
9103
enum column_format_type column_format)
9105
uint sign_len, allowed_type_modifier= 0;
9106
ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
9108
DBUG_ENTER("Create_field::init()");
9111
field_name= fld_name;
9112
def= fld_default_value;
9113
flags= fld_type_modifier;
9114
flags|= (((uint)storage_type & STORAGE_TYPE_MASK) << FIELD_STORAGE_FLAGS);
9115
flags|= (((uint)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
9116
unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
9117
Field::NEXT_NUMBER : Field::NONE);
9118
decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
9119
if (decimals >= NOT_FIXED_DEC)
9121
my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
9130
pack_length= key_length= 0;
9131
charset= fld_charset;
9132
interval_list.empty();
9134
comment= *fld_comment;
9136
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
9137
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
9139
if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
9140
(fld_type_modifier & NOT_NULL_FLAG) && fld_type != MYSQL_TYPE_TIMESTAMP)
9141
flags|= NO_DEFAULT_VALUE_FLAG;
9143
if (fld_length && !(length= (uint) atoi(fld_length)))
9144
fld_length= 0; /* purecov: inspected */
9145
sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
9148
case MYSQL_TYPE_TINY:
9150
length= MAX_TINYINT_WIDTH+sign_len;
9151
allowed_type_modifier= AUTO_INCREMENT_FLAG;
9153
case MYSQL_TYPE_SHORT:
9155
length= MAX_SMALLINT_WIDTH+sign_len;
9156
allowed_type_modifier= AUTO_INCREMENT_FLAG;
9158
case MYSQL_TYPE_INT24:
9160
length= MAX_MEDIUMINT_WIDTH+sign_len;
9161
allowed_type_modifier= AUTO_INCREMENT_FLAG;
9163
case MYSQL_TYPE_LONG:
9165
length= MAX_INT_WIDTH+sign_len;
9166
allowed_type_modifier= AUTO_INCREMENT_FLAG;
9168
case MYSQL_TYPE_LONGLONG:
9170
length= MAX_BIGINT_WIDTH;
9171
allowed_type_modifier= AUTO_INCREMENT_FLAG;
9173
case MYSQL_TYPE_NULL:
9175
case MYSQL_TYPE_NEWDECIMAL:
9176
my_decimal_trim(&length, &decimals);
9177
if (length > DECIMAL_MAX_PRECISION)
9179
my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
9180
DECIMAL_MAX_PRECISION);
9183
if (length < decimals)
9185
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
9189
my_decimal_precision_to_length(length, decimals,
9190
fld_type_modifier & UNSIGNED_FLAG);
9192
my_decimal_get_binary_size(length, decimals);
9194
case MYSQL_TYPE_VARCHAR:
9196
Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
9197
if they don't have a default value
9199
max_field_charlength= MAX_FIELD_VARCHARLENGTH;
9201
case MYSQL_TYPE_STRING:
9203
case MYSQL_TYPE_BLOB:
9204
case MYSQL_TYPE_TINY_BLOB:
9205
case MYSQL_TYPE_LONG_BLOB:
9206
case MYSQL_TYPE_MEDIUM_BLOB:
9207
if (fld_default_value)
9209
/* Allow empty as default value. */
9211
res= fld_default_value->val_str(&str);
9213
A default other than '' is always an error, and any non-NULL
9214
specified default is an error in strict mode.
9216
if (res->length() || (thd->variables.sql_mode &
9217
(MODE_STRICT_TRANS_TABLES |
9218
MODE_STRICT_ALL_TABLES)))
9220
my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
9221
fld_name); /* purecov: inspected */
9227
Otherwise a default of '' is just a warning.
9229
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
9230
ER_BLOB_CANT_HAVE_DEFAULT,
9231
ER(ER_BLOB_CANT_HAVE_DEFAULT),
9238
case MYSQL_TYPE_YEAR:
9239
if (!fld_length || length != 2)
9240
length= 4; /* Default length */
9241
flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
9243
case MYSQL_TYPE_FLOAT:
9244
/* change FLOAT(precision) to FLOAT or DOUBLE */
9245
allowed_type_modifier= AUTO_INCREMENT_FLAG;
9246
if (fld_length && !fld_decimals)
9248
uint tmp_length= length;
9249
if (tmp_length > PRECISION_FOR_DOUBLE)
9251
my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
9254
else if (tmp_length > PRECISION_FOR_FLOAT)
9256
sql_type= MYSQL_TYPE_DOUBLE;
9257
length= DBL_DIG+7; /* -[digits].E+### */
9260
length= FLT_DIG+6; /* -[digits].E+## */
9261
decimals= NOT_FIXED_DEC;
9264
if (!fld_length && !fld_decimals)
9267
decimals= NOT_FIXED_DEC;
9269
if (length < decimals &&
9270
decimals != NOT_FIXED_DEC)
9272
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
9276
case MYSQL_TYPE_DOUBLE:
9277
allowed_type_modifier= AUTO_INCREMENT_FLAG;
9278
if (!fld_length && !fld_decimals)
9281
decimals= NOT_FIXED_DEC;
9283
if (length < decimals &&
9284
decimals != NOT_FIXED_DEC)
9286
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
9290
case MYSQL_TYPE_TIMESTAMP:
9293
/* Compressed date YYYYMMDDHHMMSS */
9294
length= MAX_DATETIME_COMPRESSED_WIDTH;
9296
else if (length != MAX_DATETIME_WIDTH)
9299
We support only even TIMESTAMP lengths less or equal than 14
9300
and 19 as length of 4.1 compatible representation.
9302
length= ((length+1)/2)*2; /* purecov: inspected */
9303
length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
9305
flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
9306
if (fld_default_value)
9308
/* Grammar allows only NOW() value for ON UPDATE clause */
9309
if (fld_default_value->type() == Item::FUNC_ITEM &&
9310
((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
9312
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
9313
Field::TIMESTAMP_DN_FIELD);
9315
We don't need default value any longer moreover it is dangerous.
9316
Everything handled by unireg_check further.
9321
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
9327
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
9328
or ON UPDATE values then for the sake of compatiblity we should treat
9329
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
9330
have another TIMESTAMP column with auto-set option before this one)
9331
or DEFAULT 0 (in other cases).
9332
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
9333
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
9334
information about all TIMESTAMP fields in table will be availiable.
9336
If we have TIMESTAMP NULL column without explicit DEFAULT value
9337
we treat it as having DEFAULT NULL attribute.
9339
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
9340
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
9344
case MYSQL_TYPE_DATE:
9345
/* Old date type. */
9346
sql_type= MYSQL_TYPE_NEWDATE;
9348
case MYSQL_TYPE_NEWDATE:
9351
case MYSQL_TYPE_TIME:
9354
case MYSQL_TYPE_DATETIME:
9355
length= MAX_DATETIME_WIDTH;
9357
case MYSQL_TYPE_SET:
9359
pack_length= get_set_pack_length(fld_interval_list->elements);
9361
List_iterator<String> it(*fld_interval_list);
9364
interval_list.push_back(tmp);
9366
Set fake length to 1 to pass the below conditions.
9367
Real length will be set in mysql_prepare_table()
9368
when we know the character set of the column
9373
case MYSQL_TYPE_ENUM:
9375
/* Should be safe. */
9376
pack_length= get_enum_pack_length(fld_interval_list->elements);
9378
List_iterator<String> it(*fld_interval_list);
9381
interval_list.push_back(tmp);
9382
length= 1; /* See comment for MYSQL_TYPE_SET above. */
9385
case MYSQL_TYPE_VAR_STRING:
9386
DBUG_ASSERT(0); /* Impossible. */
9388
case MYSQL_TYPE_BIT:
9392
if (length > MAX_BIT_FIELD_LENGTH)
9394
my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name,
9395
MAX_BIT_FIELD_LENGTH);
9398
pack_length= (length + 7) / 8;
9401
case MYSQL_TYPE_GEOMETRY:
9402
case MYSQL_TYPE_DECIMAL:
9403
DBUG_ASSERT(0); /* Was obsolete */
9405
/* Remember the value of length */
9406
char_length= length;
9408
if (!(flags & BLOB_FLAG) &&
9409
((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
9410
fld_type != MYSQL_TYPE_ENUM &&
9411
(fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
9413
fld_type != MYSQL_TYPE_STRING &&
9414
fld_type != MYSQL_TYPE_VARCHAR)))
9416
my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
9417
fld_type == MYSQL_TYPE_VARCHAR ||
9418
fld_type == MYSQL_TYPE_STRING) ? ER_TOO_BIG_FIELDLENGTH :
9419
ER_TOO_BIG_DISPLAYWIDTH,
9421
fld_name, max_field_charlength); /* purecov: inspected */
9424
fld_type_modifier&= AUTO_INCREMENT_FLAG;
9425
if ((~allowed_type_modifier) & fld_type_modifier)
9427
my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
9431
DBUG_RETURN(FALSE); /* success */
9435
enum_field_types get_blob_type_from_length(ulong length)
9437
enum_field_types type;
9439
type= MYSQL_TYPE_TINY_BLOB;
9440
else if (length < 65536)
9441
type= MYSQL_TYPE_BLOB;
9442
else if (length < 256L*256L*256L)
9443
type= MYSQL_TYPE_MEDIUM_BLOB;
9445
type= MYSQL_TYPE_LONG_BLOB;
9451
Make a field from the .frm file info
9454
uint32 calc_pack_length(enum_field_types type,uint32 length)
9457
case MYSQL_TYPE_VAR_STRING:
9458
case MYSQL_TYPE_STRING:
9459
case MYSQL_TYPE_DECIMAL: return (length);
9460
case MYSQL_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
9461
case MYSQL_TYPE_YEAR:
9462
case MYSQL_TYPE_TINY : return 1;
9463
case MYSQL_TYPE_SHORT : return 2;
9464
case MYSQL_TYPE_INT24:
9465
case MYSQL_TYPE_NEWDATE:
9466
case MYSQL_TYPE_TIME: return 3;
9467
case MYSQL_TYPE_TIMESTAMP:
9468
case MYSQL_TYPE_DATE:
9469
case MYSQL_TYPE_LONG : return 4;
9470
case MYSQL_TYPE_FLOAT : return sizeof(float);
9471
case MYSQL_TYPE_DOUBLE: return sizeof(double);
9472
case MYSQL_TYPE_DATETIME:
9473
case MYSQL_TYPE_LONGLONG: return 8; /* Don't crash if no longlong */
9474
case MYSQL_TYPE_NULL : return 0;
9475
case MYSQL_TYPE_TINY_BLOB: return 1+portable_sizeof_char_ptr;
9476
case MYSQL_TYPE_BLOB: return 2+portable_sizeof_char_ptr;
9477
case MYSQL_TYPE_MEDIUM_BLOB: return 3+portable_sizeof_char_ptr;
9478
case MYSQL_TYPE_LONG_BLOB: return 4+portable_sizeof_char_ptr;
9479
case MYSQL_TYPE_SET:
9480
case MYSQL_TYPE_ENUM:
9481
case MYSQL_TYPE_NEWDECIMAL:
9482
abort(); return 0; // This shouldn't happen
9483
case MYSQL_TYPE_BIT: return length / 8;
9490
uint pack_length_to_packflag(uint type)
9493
case 1: return f_settype((uint) MYSQL_TYPE_TINY);
9494
case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
9495
case 3: return f_settype((uint) MYSQL_TYPE_INT24);
9496
case 4: return f_settype((uint) MYSQL_TYPE_LONG);
9497
case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
9499
return 0; // This shouldn't happen
9503
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
9504
uchar *null_pos, uchar null_bit,
9506
enum_field_types field_type,
9507
CHARSET_INFO *field_charset,
9508
Field::utype unireg_check,
9510
const char *field_name)
9512
uchar *bit_ptr= NULL;
9513
uchar bit_offset= 0;
9514
if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
9517
bit_offset= null_bit;
9518
if (f_maybe_null(pack_flag)) // if null field
9520
bit_ptr+= (null_bit == 7); // shift bit_ptr and bit_offset
9521
bit_offset= (bit_offset + 1) & 7;
9525
if (!f_maybe_null(pack_flag))
9532
null_bit= ((uchar) 1) << null_bit;
9535
switch (field_type) {
9536
case MYSQL_TYPE_DATE:
9537
case MYSQL_TYPE_NEWDATE:
9538
case MYSQL_TYPE_TIME:
9539
case MYSQL_TYPE_DATETIME:
9540
case MYSQL_TYPE_TIMESTAMP:
9541
field_charset= &my_charset_bin;
9545
if (f_is_alpha(pack_flag))
9547
if (!f_is_packed(pack_flag))
9549
if (field_type == MYSQL_TYPE_STRING ||
9550
field_type == MYSQL_TYPE_DECIMAL || // 3.23 or 4.0 string
9551
field_type == MYSQL_TYPE_VAR_STRING)
9552
return new Field_string(ptr,field_length,null_pos,null_bit,
9553
unireg_check, field_name,
9555
if (field_type == MYSQL_TYPE_VARCHAR)
9556
return new Field_varstring(ptr,field_length,
9557
HA_VARCHAR_PACKLENGTH(field_length),
9559
unireg_check, field_name,
9565
uint pack_length=calc_pack_length((enum_field_types)
9566
f_packtype(pack_flag),
9569
if (f_is_blob(pack_flag))
9570
return new Field_blob(ptr,null_pos,null_bit,
9571
unireg_check, field_name, share,
9572
pack_length, field_charset);
9575
if (f_is_enum(pack_flag))
9576
return new Field_enum(ptr,field_length,null_pos,null_bit,
9577
unireg_check, field_name,
9578
pack_length, interval, field_charset);
9580
return new Field_set(ptr,field_length,null_pos,null_bit,
9581
unireg_check, field_name,
9582
pack_length, interval, field_charset);
9586
switch (field_type) {
9587
case MYSQL_TYPE_DECIMAL:
9588
return new Field_decimal(ptr,field_length,null_pos,null_bit,
9589
unireg_check, field_name,
9590
f_decimals(pack_flag),
9591
f_is_zerofill(pack_flag) != 0,
9592
f_is_dec(pack_flag) == 0);
9593
case MYSQL_TYPE_NEWDECIMAL:
9594
return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
9595
unireg_check, field_name,
9596
f_decimals(pack_flag),
9597
f_is_zerofill(pack_flag) != 0,
9598
f_is_dec(pack_flag) == 0);
9599
case MYSQL_TYPE_FLOAT:
9600
return new Field_float(ptr,field_length,null_pos,null_bit,
9601
unireg_check, field_name,
9602
f_decimals(pack_flag),
9603
f_is_zerofill(pack_flag) != 0,
9604
f_is_dec(pack_flag)== 0);
9605
case MYSQL_TYPE_DOUBLE:
9606
return new Field_double(ptr,field_length,null_pos,null_bit,
9607
unireg_check, field_name,
9608
f_decimals(pack_flag),
9609
f_is_zerofill(pack_flag) != 0,
9610
f_is_dec(pack_flag)== 0);
9611
case MYSQL_TYPE_TINY:
9612
return new Field_tiny(ptr,field_length,null_pos,null_bit,
9613
unireg_check, field_name,
9614
f_is_zerofill(pack_flag) != 0,
9615
f_is_dec(pack_flag) == 0);
9616
case MYSQL_TYPE_SHORT:
9617
return new Field_short(ptr,field_length,null_pos,null_bit,
9618
unireg_check, field_name,
9619
f_is_zerofill(pack_flag) != 0,
9620
f_is_dec(pack_flag) == 0);
9621
case MYSQL_TYPE_INT24:
9622
return new Field_medium(ptr,field_length,null_pos,null_bit,
9623
unireg_check, field_name,
9624
f_is_zerofill(pack_flag) != 0,
9625
f_is_dec(pack_flag) == 0);
9626
case MYSQL_TYPE_LONG:
9627
return new Field_long(ptr,field_length,null_pos,null_bit,
9628
unireg_check, field_name,
9629
f_is_zerofill(pack_flag) != 0,
9630
f_is_dec(pack_flag) == 0);
9631
case MYSQL_TYPE_LONGLONG:
9632
return new Field_longlong(ptr,field_length,null_pos,null_bit,
9633
unireg_check, field_name,
9634
f_is_zerofill(pack_flag) != 0,
9635
f_is_dec(pack_flag) == 0);
9636
case MYSQL_TYPE_TIMESTAMP:
9637
return new Field_timestamp(ptr,field_length, null_pos, null_bit,
9638
unireg_check, field_name, share,
9640
case MYSQL_TYPE_YEAR:
9641
return new Field_year(ptr,field_length,null_pos,null_bit,
9642
unireg_check, field_name);
9643
case MYSQL_TYPE_DATE:
9644
return new Field_date(ptr,null_pos,null_bit,
9645
unireg_check, field_name, field_charset);
9646
case MYSQL_TYPE_NEWDATE:
9647
return new Field_newdate(ptr,null_pos,null_bit,
9648
unireg_check, field_name, field_charset);
9649
case MYSQL_TYPE_TIME:
9650
return new Field_time(ptr,null_pos,null_bit,
9651
unireg_check, field_name, field_charset);
9652
case MYSQL_TYPE_DATETIME:
9653
return new Field_datetime(ptr,null_pos,null_bit,
9654
unireg_check, field_name, field_charset);
9655
case MYSQL_TYPE_NULL:
9656
return new Field_null(ptr, field_length, unireg_check, field_name,
9658
case MYSQL_TYPE_BIT:
9659
return f_bit_as_char(pack_flag) ?
9660
new Field_bit_as_char(ptr, field_length, null_pos, null_bit,
9661
unireg_check, field_name) :
9662
new Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
9663
bit_offset, unireg_check, field_name);
9665
default: // Impossible (Wrong version)
9672
/** Create a field suitable for create of table. */
9674
Create_field::Create_field(Field *old_field,Field *orig_field)
9677
field_name=change=old_field->field_name;
9678
length= old_field->field_length;
9679
flags= old_field->flags;
9680
unireg_check=old_field->unireg_check;
9681
pack_length=old_field->pack_length();
9682
key_length= old_field->key_length();
9683
sql_type= old_field->real_type();
9684
charset= old_field->charset(); // May be NULL ptr
9685
comment= old_field->comment;
9686
decimals= old_field->decimals();
9688
/* Fix if the original table had 4 byte pointer blobs */
9689
if (flags & BLOB_FLAG)
9690
pack_length= (pack_length- old_field->table->s->blob_ptr_size +
9691
portable_sizeof_char_ptr);
9694
case MYSQL_TYPE_BLOB:
9695
switch (pack_length - portable_sizeof_char_ptr) {
9696
case 1: sql_type= MYSQL_TYPE_TINY_BLOB; break;
9697
case 2: sql_type= MYSQL_TYPE_BLOB; break;
9698
case 3: sql_type= MYSQL_TYPE_MEDIUM_BLOB; break;
9699
default: sql_type= MYSQL_TYPE_LONG_BLOB; break;
9701
length/= charset->mbmaxlen;
9702
key_length/= charset->mbmaxlen;
9704
case MYSQL_TYPE_STRING:
9705
/* Change CHAR -> VARCHAR if dynamic record length */
9706
if (old_field->type() == MYSQL_TYPE_VAR_STRING)
9707
sql_type= MYSQL_TYPE_VARCHAR;
9710
case MYSQL_TYPE_ENUM:
9711
case MYSQL_TYPE_SET:
9712
case MYSQL_TYPE_VARCHAR:
9713
case MYSQL_TYPE_VAR_STRING:
9714
/* This is corrected in create_length_to_internal_length */
9715
length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
9721
if (flags & (ENUM_FLAG | SET_FLAG))
9722
interval= ((Field_enum*) old_field)->typelib;
9726
char_length= length;
9728
if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
9729
old_field->ptr && orig_field &&
9730
(sql_type != MYSQL_TYPE_TIMESTAMP || /* set def only if */
9731
old_field->table->timestamp_field != old_field || /* timestamp field */
9732
unireg_check == Field::TIMESTAMP_UN_FIELD)) /* has default val */
9734
char buff[MAX_FIELD_WIDTH];
9735
String tmp(buff,sizeof(buff), charset);
9738
/* Get the value from default_values */
9739
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
9740
orig_field->table->record[0]);
9741
orig_field->move_field_offset(diff); // Points now at default_values
9742
if (!orig_field->is_real_null())
9744
char buff[MAX_FIELD_WIDTH], *pos;
9745
String tmp(buff, sizeof(buff), charset), *res;
9746
res= orig_field->val_str(&tmp);
9747
pos= (char*) sql_strmake(res->ptr(), res->length());
9748
def= new Item_string(pos, res->length(), charset);
9750
orig_field->move_field_offset(-diff); // Back to record[0]
9756
maximum possible display length for blob.
9762
uint32 Field_blob::max_display_length()
9767
return 255 * field_charset->mbmaxlen;
9769
return 65535 * field_charset->mbmaxlen;
9771
return 16777215 * field_charset->mbmaxlen;
9773
return (uint32) 4294967295U;
9775
DBUG_ASSERT(0); // we should never go here
9781
/*****************************************************************************
9783
*****************************************************************************/
9786
Produce warning or note about data saved into field.
9788
@param level - level of message (Note/Warning/Error)
9789
@param code - error code of message to be produced
9790
@param cuted_increment - whenever we should increase cut fields count or not
9793
This function won't produce warning and increase cut fields counter
9794
if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
9796
if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
9797
This allows us to avoid notes in optimisation, like convert_constant_item().
9800
1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
9806
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
9807
int cuted_increment)
9810
If this field was created only for type conversion purposes it
9811
will have table == NULL.
9813
THD *thd= table ? table->in_use : current_thd;
9814
if (thd->count_cuted_fields)
9816
thd->cuted_fields+= cuted_increment;
9817
push_warning_printf(thd, level, code, ER(code), field_name,
9821
return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
9826
Produce warning or note about datetime string data saved into field.
9828
@param level level of message (Note/Warning/Error)
9829
@param code error code of message to be produced
9830
@param str string value which we tried to save
9831
@param str_length length of string which we tried to save
9832
@param ts_type type of datetime value (datetime/date/time)
9833
@param cuted_increment whenever we should increase cut fields count or not
9836
This function will always produce some warning but won't increase cut
9837
fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
9842
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code,
9843
const char *str, uint str_length,
9844
timestamp_type ts_type, int cuted_increment)
9846
THD *thd= table ? table->in_use : current_thd;
9847
if ((thd->really_abort_on_warning() &&
9848
level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
9849
set_warning(level, code, cuted_increment))
9850
make_truncated_value_warning(thd, level, str, str_length, ts_type,
9856
Produce warning or note about integer datetime value saved into field.
9858
@param level level of message (Note/Warning/Error)
9859
@param code error code of message to be produced
9860
@param nr numeric value which we tried to save
9861
@param ts_type type of datetime value (datetime/date/time)
9862
@param cuted_increment whenever we should increase cut fields count or not
9865
This function will always produce some warning but won't increase cut
9866
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
9871
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code,
9872
longlong nr, timestamp_type ts_type,
9873
int cuted_increment)
9875
THD *thd= table ? table->in_use : current_thd;
9876
if (thd->really_abort_on_warning() ||
9877
set_warning(level, code, cuted_increment))
9880
char *str_end= longlong10_to_str(nr, str_nr, -10);
9881
make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr),
9882
ts_type, field_name);
9888
Produce warning or note about double datetime data saved into field.
9890
@param level level of message (Note/Warning/Error)
9891
@param code error code of message to be produced
9892
@param nr double value which we tried to save
9893
@param ts_type type of datetime value (datetime/date/time)
9896
This function will always produce some warning but won't increase cut
9897
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
9902
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code,
9903
double nr, timestamp_type ts_type)
9905
THD *thd= table ? table->in_use : current_thd;
9906
if (thd->really_abort_on_warning() ||
9907
set_warning(level, code, 1))
9909
/* DBL_DIG is enough to print '-[digits].E+###' */
9910
char str_nr[DBL_DIG + 8];
9911
uint str_len= my_sprintf(str_nr, (str_nr, "%g", nr));
9912
make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,