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
23
#include <drizzled/server_includes.h>
24
#include "sql_select.h"
26
#include <drizzled/drizzled_error_messages.h>
28
// Maximum allowed exponent value for converting string to decimal
29
#define MAX_EXPONENT 1024
31
/*****************************************************************************
32
Instansiate templates and static variables
33
*****************************************************************************/
35
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
36
template class List<Create_field>;
37
template class List_iterator<Create_field>;
42
Rules for merging different types of fields in UNION
44
NOTE: to avoid 256*256 table, gap in table types numeration is skiped
45
following #defines describe that gap and how to canculate number of fields
46
and index of field in thia array.
48
#define FIELDTYPE_TEAR_FROM (DRIZZLE_TYPE_VARCHAR + 1)
49
#define FIELDTYPE_TEAR_TO (DRIZZLE_TYPE_VIRTUAL - 1)
50
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
51
inline int field_type2index (enum_field_types field_type)
53
return (field_type < FIELDTYPE_TEAR_FROM ?
55
((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
59
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
61
/* DRIZZLE_TYPE_DECIMAL -> */
63
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
64
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_NEWDECIMAL,
66
DRIZZLE_TYPE_NEWDECIMAL,
69
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
70
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_VARCHAR,
71
//DRIZZLE_TYPE_LONGLONG
72
DRIZZLE_TYPE_NEWDECIMAL,
75
//DRIZZLE_TYPE_DATETIME
77
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
78
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
79
// DRIZZLE_TYPE_VIRTUAL
81
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
82
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_VARCHAR,
86
/* DRIZZLE_TYPE_TINY -> */
88
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
89
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_TINY,
94
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
95
DRIZZLE_TYPE_TINY, DRIZZLE_TYPE_VARCHAR,
96
//DRIZZLE_TYPE_LONGLONG
97
DRIZZLE_TYPE_LONGLONG,
100
//DRIZZLE_TYPE_DATETIME
101
DRIZZLE_TYPE_VARCHAR,
102
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
103
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
104
// DRIZZLE_TYPE_VIRTUAL
105
DRIZZLE_TYPE_VIRTUAL,
106
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
107
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_VARCHAR,
111
/* DRIZZLE_TYPE_LONG -> */
113
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
114
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_LONG,
117
//DRIZZLE_TYPE_DOUBLE
119
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
120
DRIZZLE_TYPE_LONG, DRIZZLE_TYPE_VARCHAR,
121
//DRIZZLE_TYPE_LONGLONG
122
DRIZZLE_TYPE_LONGLONG,
124
DRIZZLE_TYPE_VARCHAR,
125
//DRIZZLE_TYPE_DATETIME
126
DRIZZLE_TYPE_VARCHAR,
127
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
128
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
129
// DRIZZLE_TYPE_VIRTUAL
130
DRIZZLE_TYPE_VIRTUAL,
131
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
132
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_VARCHAR,
136
/* DRIZZLE_TYPE_DOUBLE -> */
138
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
139
DRIZZLE_TYPE_DOUBLE, DRIZZLE_TYPE_DOUBLE,
142
//DRIZZLE_TYPE_DOUBLE
144
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
145
DRIZZLE_TYPE_DOUBLE, DRIZZLE_TYPE_VARCHAR,
146
//DRIZZLE_TYPE_LONGLONG
149
DRIZZLE_TYPE_VARCHAR,
150
//DRIZZLE_TYPE_DATETIME
151
DRIZZLE_TYPE_VARCHAR,
152
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
153
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
154
// DRIZZLE_TYPE_VIRTUAL
155
DRIZZLE_TYPE_VIRTUAL,
156
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
157
DRIZZLE_TYPE_DOUBLE, DRIZZLE_TYPE_VARCHAR,
161
/* DRIZZLE_TYPE_NULL -> */
163
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
164
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_TINY,
167
//DRIZZLE_TYPE_DOUBLE
169
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
170
DRIZZLE_TYPE_NULL, DRIZZLE_TYPE_TIMESTAMP,
171
//DRIZZLE_TYPE_LONGLONG
172
DRIZZLE_TYPE_LONGLONG,
175
//DRIZZLE_TYPE_DATETIME
176
DRIZZLE_TYPE_DATETIME,
177
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
178
DRIZZLE_TYPE_NEWDATE, DRIZZLE_TYPE_VARCHAR,
179
// DRIZZLE_TYPE_VIRTUAL
180
DRIZZLE_TYPE_VIRTUAL,
181
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
182
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_ENUM,
186
/* DRIZZLE_TYPE_TIMESTAMP -> */
188
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
189
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
191
DRIZZLE_TYPE_VARCHAR,
192
//DRIZZLE_TYPE_DOUBLE
193
DRIZZLE_TYPE_VARCHAR,
194
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
195
DRIZZLE_TYPE_TIMESTAMP, DRIZZLE_TYPE_TIMESTAMP,
196
//DRIZZLE_TYPE_LONGLONG
197
DRIZZLE_TYPE_VARCHAR,
199
DRIZZLE_TYPE_DATETIME,
200
//DRIZZLE_TYPE_DATETIME
201
DRIZZLE_TYPE_DATETIME,
202
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
203
DRIZZLE_TYPE_NEWDATE, DRIZZLE_TYPE_VARCHAR,
204
// DRIZZLE_TYPE_VIRTUAL
205
DRIZZLE_TYPE_VIRTUAL,
206
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
207
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
211
/* DRIZZLE_TYPE_LONGLONG -> */
213
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
214
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_LONGLONG,
216
DRIZZLE_TYPE_LONGLONG,
217
//DRIZZLE_TYPE_DOUBLE
219
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
220
DRIZZLE_TYPE_LONGLONG, DRIZZLE_TYPE_VARCHAR,
221
//DRIZZLE_TYPE_LONGLONG
222
DRIZZLE_TYPE_LONGLONG,
224
DRIZZLE_TYPE_VARCHAR,
225
//DRIZZLE_TYPE_DATETIME
226
DRIZZLE_TYPE_VARCHAR,
227
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
228
DRIZZLE_TYPE_NEWDATE, DRIZZLE_TYPE_VARCHAR,
229
// DRIZZLE_TYPE_VIRTUAL
230
DRIZZLE_TYPE_VIRTUAL,
231
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
232
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_VARCHAR,
236
/* DRIZZLE_TYPE_TIME -> */
238
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
239
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
241
DRIZZLE_TYPE_VARCHAR,
242
//DRIZZLE_TYPE_DOUBLE
243
DRIZZLE_TYPE_VARCHAR,
244
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
245
DRIZZLE_TYPE_TIME, DRIZZLE_TYPE_DATETIME,
246
//DRIZZLE_TYPE_LONGLONG
247
DRIZZLE_TYPE_VARCHAR,
250
//DRIZZLE_TYPE_DATETIME
251
DRIZZLE_TYPE_DATETIME,
252
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
253
DRIZZLE_TYPE_NEWDATE, DRIZZLE_TYPE_VARCHAR,
254
// DRIZZLE_TYPE_VIRTUAL
255
DRIZZLE_TYPE_VIRTUAL,
256
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
257
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
261
/* DRIZZLE_TYPE_DATETIME -> */
263
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
264
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
266
DRIZZLE_TYPE_VARCHAR,
267
//DRIZZLE_TYPE_DOUBLE
268
DRIZZLE_TYPE_VARCHAR,
269
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
270
DRIZZLE_TYPE_DATETIME, DRIZZLE_TYPE_DATETIME,
271
//DRIZZLE_TYPE_LONGLONG
272
DRIZZLE_TYPE_VARCHAR,
274
DRIZZLE_TYPE_DATETIME,
275
//DRIZZLE_TYPE_DATETIME
276
DRIZZLE_TYPE_DATETIME,
277
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
278
DRIZZLE_TYPE_NEWDATE, DRIZZLE_TYPE_VARCHAR,
279
// DRIZZLE_TYPE_VIRTUAL
280
DRIZZLE_TYPE_VIRTUAL,
281
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
282
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
286
/* DRIZZLE_TYPE_NEWDATE -> */
288
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
289
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
291
DRIZZLE_TYPE_VARCHAR,
292
//DRIZZLE_TYPE_DOUBLE
293
DRIZZLE_TYPE_VARCHAR,
294
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
295
DRIZZLE_TYPE_NEWDATE, DRIZZLE_TYPE_DATETIME,
296
//DRIZZLE_TYPE_LONGLONG
297
DRIZZLE_TYPE_VARCHAR,
299
DRIZZLE_TYPE_DATETIME,
300
//DRIZZLE_TYPE_DATETIME
301
DRIZZLE_TYPE_DATETIME,
302
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
303
DRIZZLE_TYPE_NEWDATE, DRIZZLE_TYPE_VARCHAR,
304
// DRIZZLE_TYPE_VIRTUAL
305
DRIZZLE_TYPE_VIRTUAL,
306
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
307
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
311
/* DRIZZLE_TYPE_VARCHAR -> */
313
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
314
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
316
DRIZZLE_TYPE_VARCHAR,
317
//DRIZZLE_TYPE_DOUBLE
318
DRIZZLE_TYPE_VARCHAR,
319
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
320
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
321
//DRIZZLE_TYPE_LONGLONG
322
DRIZZLE_TYPE_VARCHAR,
324
DRIZZLE_TYPE_VARCHAR,
325
//DRIZZLE_TYPE_DATETIME
326
DRIZZLE_TYPE_VARCHAR,
327
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
328
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
329
// DRIZZLE_TYPE_VIRTUAL
330
DRIZZLE_TYPE_VIRTUAL,
331
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
332
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
336
/* DRIZZLE_TYPE_VIRTUAL -> */
338
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
339
DRIZZLE_TYPE_VIRTUAL, DRIZZLE_TYPE_VIRTUAL,
341
DRIZZLE_TYPE_VIRTUAL,
342
//DRIZZLE_TYPE_DOUBLE
343
DRIZZLE_TYPE_VIRTUAL,
344
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
345
DRIZZLE_TYPE_VIRTUAL, DRIZZLE_TYPE_VIRTUAL,
346
//DRIZZLE_TYPE_LONGLONG
347
DRIZZLE_TYPE_VIRTUAL,
349
DRIZZLE_TYPE_VIRTUAL,
350
//DRIZZLE_TYPE_DATETIME
351
DRIZZLE_TYPE_VIRTUAL,
352
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
353
DRIZZLE_TYPE_VIRTUAL, DRIZZLE_TYPE_VIRTUAL,
354
// DRIZZLE_TYPE_VIRTUAL
355
DRIZZLE_TYPE_VIRTUAL,
356
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
357
DRIZZLE_TYPE_VIRTUAL, DRIZZLE_TYPE_VIRTUAL,
359
DRIZZLE_TYPE_VIRTUAL,
361
/* DRIZZLE_TYPE_NEWDECIMAL -> */
363
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
364
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_NEWDECIMAL,
366
DRIZZLE_TYPE_NEWDECIMAL,
367
//DRIZZLE_TYPE_DOUBLE
369
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
370
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_VARCHAR,
371
//DRIZZLE_TYPE_LONGLONG
372
DRIZZLE_TYPE_NEWDECIMAL,
374
DRIZZLE_TYPE_VARCHAR,
375
//DRIZZLE_TYPE_DATETIME
376
DRIZZLE_TYPE_VARCHAR,
377
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
378
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
379
// DRIZZLE_TYPE_VIRTUAL
380
DRIZZLE_TYPE_VIRTUAL,
381
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
382
DRIZZLE_TYPE_NEWDECIMAL, DRIZZLE_TYPE_VARCHAR,
386
/* DRIZZLE_TYPE_ENUM -> */
388
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
389
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
391
DRIZZLE_TYPE_VARCHAR,
392
//DRIZZLE_TYPE_DOUBLE
393
DRIZZLE_TYPE_VARCHAR,
394
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
395
DRIZZLE_TYPE_ENUM, DRIZZLE_TYPE_VARCHAR,
396
//DRIZZLE_TYPE_LONGLONG
397
DRIZZLE_TYPE_VARCHAR,
399
DRIZZLE_TYPE_VARCHAR,
400
//DRIZZLE_TYPE_DATETIME
401
DRIZZLE_TYPE_VARCHAR,
402
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
403
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
404
// DRIZZLE_TYPE_VIRTUAL
405
DRIZZLE_TYPE_VIRTUAL,
406
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
407
DRIZZLE_TYPE_VARCHAR, DRIZZLE_TYPE_VARCHAR,
411
/* DRIZZLE_TYPE_BLOB -> */
413
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
414
DRIZZLE_TYPE_BLOB, DRIZZLE_TYPE_BLOB,
417
//DRIZZLE_TYPE_DOUBLE
419
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
420
DRIZZLE_TYPE_BLOB, DRIZZLE_TYPE_BLOB,
421
//DRIZZLE_TYPE_LONGLONG
425
//DRIZZLE_TYPE_DATETIME
427
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
428
DRIZZLE_TYPE_BLOB, DRIZZLE_TYPE_BLOB,
429
// DRIZZLE_TYPE_VIRTUAL
430
DRIZZLE_TYPE_VIRTUAL,
431
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
432
DRIZZLE_TYPE_BLOB, DRIZZLE_TYPE_BLOB,
439
Return type of which can carry value of both given types in UNION result.
441
@param a type for merging
442
@param b type for merging
448
enum_field_types Field::field_type_merge(enum_field_types a,
451
assert(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
452
assert(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
453
return field_types_merge_rules[field_type2index(a)]
454
[field_type2index(b)];
458
static Item_result field_types_result_type [FIELDTYPE_NUM]=
460
//DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_TINY
461
DECIMAL_RESULT, INT_RESULT,
464
//DRIZZLE_TYPE_DOUBLE
466
//DRIZZLE_TYPE_NULL DRIZZLE_TYPE_TIMESTAMP
467
STRING_RESULT, STRING_RESULT,
468
//DRIZZLE_TYPE_LONGLONG
472
//DRIZZLE_TYPE_DATETIME
474
//DRIZZLE_TYPE_NEWDATE DRIZZLE_TYPE_VARCHAR
475
STRING_RESULT, STRING_RESULT,
476
//DRIZZLE_TYPE_VIRTUAL
478
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
479
DECIMAL_RESULT, STRING_RESULT,
486
Test if the given string contains important data:
487
not spaces for character string,
488
or any data for binary string.
491
test_if_important_data()
497
false - If string does not have important data
498
true - If string has some important data
502
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
505
if (cs != &my_charset_bin)
506
str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
507
return (str < strend);
512
Detect Item_result by given field type of UNION merge result.
514
@param field_type given field type
517
Item_result (type of internal MySQL expression result)
520
Item_result Field::result_merge_type(enum_field_types field_type)
522
assert(field_type < FIELDTYPE_TEAR_FROM || field_type
523
> FIELDTYPE_TEAR_TO);
524
return field_types_result_type[field_type2index(field_type)];
527
/*****************************************************************************
528
Static help functions
529
*****************************************************************************/
533
Check whether a field type can be partially indexed by a key.
535
This is a static method, rather than a virtual function, because we need
536
to check the type of a non-Field in mysql_alter_table().
538
@param type field type
541
true Type can have a prefixed key
543
false Type can not have a prefixed key
546
bool Field::type_can_have_key_part(enum enum_field_types type)
549
case DRIZZLE_TYPE_VARCHAR:
550
case DRIZZLE_TYPE_BLOB:
559
Process decimal library return codes and issue warnings for overflow and
562
@param op_result decimal library return code (E_DEC_* see include/decimal.h)
567
0 no error or some other errors except overflow
570
int Field::warn_if_overflow(int op_result)
572
if (op_result == E_DEC_OVERFLOW)
574
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
577
if (op_result == E_DEC_TRUNCATED)
579
set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
580
/* We return 0 here as this is not a critical issue */
587
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
589
cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
591
while (length && my_isspace(cs,*str))
592
{ // Allow start space
597
if (*str == '+' || *str == '-')
600
if (!length || !(my_isdigit(cs,*str) || *str == '.'))
603
while (length && my_isdigit(cs,*str))
612
while (length && my_isdigit(cs,*str))
619
if (*str == 'E' || *str == 'e')
621
if (length < 3 || (str[1] != '+' && str[1] != '-') ||
622
!my_isdigit(cs,str[2]))
626
while (length && my_isdigit(cs,*str))
631
for (; length ; length--, str++)
633
if (!my_isspace(cs,*str))
642
Interpret field value as an integer but return the result as a string.
644
This is used for printing bit_fields as numbers while debugging.
647
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
649
const CHARSET_INFO * const cs= &my_charset_bin;
651
int64_t value= val_int();
653
if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
655
length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
656
MY_INT64_NUM_DECIMAL_DIGITS,
657
unsigned_val ? 10 : -10,
659
val_buffer->length(length);
664
/// This is used as a table name when the table structure is not set up
665
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
666
unsigned char null_bit_arg,
667
utype unireg_check_arg, const char *field_name_arg)
668
:ptr(ptr_arg), null_ptr(null_ptr_arg),
669
table(0), orig_table(0), table_name(0),
670
field_name(field_name_arg),
671
key_start(0), part_of_key(0), part_of_key_not_clustered(0),
672
part_of_sortkey(0), unireg_check(unireg_check_arg),
673
field_length(length_arg), null_bit(null_bit_arg),
674
is_created_from_null_item(false),
675
vcol_info(NULL), is_stored(true)
677
flags=null_ptr ? 0: NOT_NULL_FLAG;
678
comment.str= (char*) "";
684
void Field::hash(uint32_t *nr, uint32_t *nr2)
688
*nr^= (*nr << 1) | 1;
692
uint32_t len= pack_length();
693
const CHARSET_INFO * const cs= charset();
694
cs->coll->hash_sort(cs, ptr, len, nr, nr2);
699
Field::do_last_null_byte() const
701
assert(null_ptr == NULL || null_ptr >= table->record[0]);
703
return (size_t) (null_ptr - table->record[0]) + 1;
704
return LAST_NULL_BYTE_UNDEF;
708
void Field::copy_from_tmp(int row_offset)
710
memcpy(ptr,ptr+row_offset,pack_length());
713
*null_ptr= (unsigned char) ((null_ptr[0] & (unsigned char) ~(uint32_t) null_bit) | (null_ptr[row_offset] & (unsigned char) null_bit));
718
bool Field::send_binary(Protocol *protocol)
720
char buff[MAX_FIELD_WIDTH];
721
String tmp(buff,sizeof(buff),charset());
723
return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
728
Check to see if field size is compatible with destination.
730
This method is used in row-based replication to verify that the slave's
731
field size is less than or equal to the master's field size. The
732
encoded field metadata (from the master or source) is decoded and compared
733
to the size of this field (the slave or destination).
735
@param field_metadata Encoded size in field metadata
737
@retval 0 if this field's size is < the source field's size
738
@retval 1 if this field's size is >= the source field's size
740
int Field::compatible_field_size(uint32_t field_metadata)
742
uint32_t const source_size= pack_length_from_metadata(field_metadata);
743
uint32_t const destination_size= row_pack_length();
744
return (source_size <= destination_size);
748
int Field::store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
749
enum_check_fields check_level)
752
enum_check_fields old_check_level= table->in_use->count_cuted_fields;
753
table->in_use->count_cuted_fields= check_level;
754
res= store(to, length, cs);
755
table->in_use->count_cuted_fields= old_check_level;
761
Pack the field into a format suitable for storage and transfer.
763
To implement packing functionality, only the virtual function
764
should be overridden. The other functions are just convenience
765
functions and hence should not be overridden.
767
The value of <code>low_byte_first</code> is dependent on how the
768
packed data is going to be used: for local use, e.g., temporary
769
store on disk or in memory, use the native format since that is
770
faster. For data that is going to be transfered to other machines
771
(e.g., when writing data to the binary log), data should always be
772
stored in little-endian format.
774
@note The default method for packing fields just copy the raw bytes
775
of the record into the destination, but never more than
776
<code>max_length</code> characters.
779
Pointer to memory area where representation of field should be put.
782
Pointer to memory area where record representation of field is
786
Maximum length of the field, as given in the column definition. For
787
example, for <code>CHAR(1000)</code>, the <code>max_length</code>
788
is 1000. This information is sometimes needed to decide how to pack
791
@param low_byte_first
792
@c true if integers should be stored little-endian, @c false if
793
native format should be used. Note that for little-endian machines,
794
the value of this flag is a moot point since the native format is
798
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
799
bool low_byte_first __attribute__((unused)))
801
uint32_t length= pack_length();
802
set_if_smaller(length, max_length);
803
memcpy(to, from, length);
808
Unpack a field from row data.
810
This method is used to unpack a field from a master whose size of
811
the field is less than that of the slave.
813
The <code>param_data</code> parameter is a two-byte integer (stored
814
in the least significant 16 bits of the unsigned integer) usually
815
consisting of two parts: the real type in the most significant byte
816
and a original pack length in the least significant byte.
818
The exact layout of the <code>param_data</code> field is given by
819
the <code>Table_map_log_event::save_field_metadata()</code>.
821
This is the default method for unpacking a field. It just copies
822
the memory block in byte order (of original pack length bytes or
823
length of field, whichever is smaller).
825
@param to Destination of the data
826
@param from Source of the data
827
@param param_data Real type and original pack length of the field
830
@param low_byte_first
831
If this flag is @c true, all composite entities (e.g., lengths)
832
should be unpacked in little-endian format; otherwise, the entities
833
are unpacked in native order.
835
@return New pointer into memory based on from + length of the data
837
const unsigned char *
838
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
839
bool low_byte_first __attribute__((unused)))
841
uint32_t length=pack_length();
844
If from length is > 255, it has encoded data in the upper bits. Need
847
if (param_data > 255)
849
from_type= (param_data & 0xff00) >> 8U; // real_type.
850
param_data= param_data & 0x00ff; // length.
853
if ((param_data == 0) ||
854
(length == param_data) ||
855
(from_type != real_type()))
857
memcpy(to, from, length);
861
uint32_t len= (param_data && (param_data < length)) ?
864
memcpy(to, from, param_data > length ? length : len);
869
my_decimal *Field::val_decimal(my_decimal *decimal __attribute__((unused)))
871
/* This never have to be called */
877
void Field::make_field(Send_field *field)
879
if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
881
field->db_name= orig_table->s->db.str;
882
field->org_table_name= orig_table->s->table_name.str;
885
field->org_table_name= field->db_name= "";
888
field->table_name= orig_table->alias;
889
field->org_col_name= field_name;
893
field->table_name= "";
894
field->org_col_name= "";
896
field->col_name= field_name;
897
field->charsetnr= charset()->number;
898
field->length=field_length;
900
field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
906
Conversion from decimal to int64_t with checking overflow and
907
setting correct value (min/max) in case of overflow.
909
@param val value which have to be converted
910
@param unsigned_flag type of integer in which we convert val
911
@param err variable to pass error code
914
value converted from val
916
int64_t Field::convert_decimal2int64_t(const my_decimal *val,
917
bool unsigned_flag __attribute__((unused)), int *err)
920
if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
921
~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
924
i= (val->sign() ? INT64_MIN : INT64_MAX);
930
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
932
uint32_t store_length;
934
copy->length=pack_length();
936
if (flags & BLOB_FLAG)
938
copy->blob_field=(Field_blob*) this;
940
copy->length-= table->s->blob_ptr_size;
948
return copy->length+ store_length;
952
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
955
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
956
if (!(res=val_str(&tmp)) ||
957
str_to_datetime_with_warn(res->ptr(), res->length(),
958
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
963
bool Field::get_time(DRIZZLE_TIME *ltime)
966
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
967
if (!(res=val_str(&tmp)) ||
968
str_to_time_with_warn(res->ptr(), res->length(), ltime))
974
This is called when storing a date in a string.
977
Needs to be changed if/when we want to support different time formats.
980
int Field::store_time(DRIZZLE_TIME *ltime,
981
enum enum_drizzle_timestamp_type type_arg __attribute__((unused)))
983
char buff[MAX_DATE_STRING_REP_LENGTH];
984
uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
985
return store(buff, length, &my_charset_bin);
989
bool Field::optimize_range(uint32_t idx, uint32_t part)
991
return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
995
Field *Field::new_field(MEM_ROOT *root, Table *new_table,
996
bool keep_type __attribute__((unused)))
999
if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1002
if (tmp->table->maybe_null)
1003
tmp->flags&= ~NOT_NULL_FLAG;
1004
tmp->table= new_table;
1005
tmp->key_start.init(0);
1006
tmp->part_of_key.init(0);
1007
tmp->part_of_sortkey.init(0);
1008
tmp->unireg_check= Field::NONE;
1009
tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1010
tmp->reset_fields();
1015
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
1016
unsigned char *new_ptr, unsigned char *new_null_ptr,
1017
uint32_t new_null_bit)
1020
if ((tmp= new_field(root, new_table, table == new_table)))
1023
tmp->null_ptr= new_null_ptr;
1024
tmp->null_bit= new_null_bit;
1030
/* This is used to generate a field in Table from TABLE_SHARE */
1032
Field *Field::clone(MEM_ROOT *root, Table *new_table)
1035
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1037
tmp->init(new_table);
1038
tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
1039
new_table->s->default_values));
1045
/****************************************************************************
1047
****************************************************************************/
1049
int Field_tiny::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
1054
error= get_int(cs, from, len, &rnd, 255, -128, 127);
1060
int Field_tiny::store(double nr)
1069
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1072
else if (nr > 127.0)
1075
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1079
*ptr=(char) (int) nr;
1085
int Field_tiny::store(int64_t nr, bool unsigned_val)
1090
if (nr < 0 && unsigned_val)
1091
nr= 256; // Generate overflow
1095
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1101
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1111
double Field_tiny::val_real(void)
1113
int tmp= (int) ((signed char*) ptr)[0];
1114
return (double) tmp;
1118
int64_t Field_tiny::val_int(void)
1120
int tmp= (int) ((signed char*) ptr)[0];
1121
return (int64_t) tmp;
1125
String *Field_tiny::val_str(String *val_buffer,
1126
String *val_ptr __attribute__((unused)))
1128
const CHARSET_INFO * const cs= &my_charset_bin;
1130
uint32_t mlength=cmax(field_length+1,5*cs->mbmaxlen);
1131
val_buffer->alloc(mlength);
1132
char *to=(char*) val_buffer->ptr();
1134
length= (uint32_t) cs->cset->long10_to_str(cs,to,mlength,-10,
1135
(long) *((signed char*) ptr));
1137
val_buffer->length(length);
1142
bool Field_tiny::send_binary(Protocol *protocol)
1144
return protocol->store_tiny((int64_t) (int8_t) ptr[0]);
1147
int Field_tiny::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
1150
a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
1152
return (a < b) ? -1 : (a > b) ? 1 : 0;
1155
void Field_tiny::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
1157
to[0] = (char) (ptr[0] ^ (unsigned char) 128); /* Revers signbit */
1160
void Field_tiny::sql_type(String &res) const
1162
const CHARSET_INFO * const cs=res.charset();
1163
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1164
"tinyint(%d)",(int) field_length));
1168
Report "not well formed" or "cannot convert" error
1169
after storing a character string info a field.
1172
check_string_copy_error()
1174
well_formed_error_pos - where not well formed data was first met
1175
cannot_convert_error_pos - where a not-convertable character was first met
1176
end - end of the string
1177
cs - character set of the string
1180
As of version 5.0 both cases return the same error:
1182
"Invalid string value: 'xxx' for column 't' at row 1"
1184
Future versions will possibly introduce a new error message:
1186
"Cannot convert character string: 'xxx' for column 't' at row 1"
1189
false - If errors didn't happen
1190
true - If an error happened
1194
check_string_copy_error(Field_str *field,
1195
const char *well_formed_error_pos,
1196
const char *cannot_convert_error_pos,
1198
const CHARSET_INFO * const cs)
1200
const char *pos, *end_orig;
1203
if (!(pos= well_formed_error_pos) &&
1204
!(pos= cannot_convert_error_pos))
1208
set_if_smaller(end, pos + 6);
1210
for (t= tmp; pos < end; pos++)
1213
If the source string is ASCII compatible (mbminlen==1)
1214
and the source character is in ASCII printable range (0x20..0x7F),
1215
then display the character as is.
1217
Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
1218
or the source character is not in the printable range,
1219
then print the character using HEX notation.
1221
if (((unsigned char) *pos) >= 0x20 &&
1222
((unsigned char) *pos) <= 0x7F &&
1231
*t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
1232
*t++= _dig_vec_upper[((unsigned char) *pos) & 15];
1242
push_warning_printf(field->table->in_use,
1243
field->table->in_use->abort_on_warning ?
1244
DRIZZLE_ERROR::WARN_LEVEL_ERROR :
1245
DRIZZLE_ERROR::WARN_LEVEL_WARN,
1246
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1247
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1248
"string", tmp, field->field_name,
1249
(uint32_t) field->table->in_use->row_count);
1253
uint32_t Field::is_equal(Create_field *new_field)
1255
return (new_field->sql_type == real_type());
1260
1 if the fields are equally defined
1262
0 if the fields are unequally defined
1265
bool Field::eq_def(Field *field)
1267
if (real_type() != field->real_type() || charset() != field->charset() ||
1268
pack_length() != field->pack_length())
1275
returns 1 if the fields are equally defined
1277
bool Field_enum::eq_def(Field *field)
1279
if (!Field::eq_def(field))
1281
TYPELIB *from_lib=((Field_enum*) field)->typelib;
1283
if (typelib->count < from_lib->count)
1285
for (uint32_t i=0 ; i < from_lib->count ; i++)
1286
if (my_strnncoll(field_charset,
1287
(const unsigned char*)typelib->type_names[i],
1288
strlen(typelib->type_names[i]),
1289
(const unsigned char*)from_lib->type_names[i],
1290
strlen(from_lib->type_names[i])))
1295
/*****************************************************************************
1296
Handling of field and Create_field
1297
*****************************************************************************/
1300
Convert create_field::length from number of characters to number of bytes.
1303
void Create_field::create_length_to_internal_length(void)
1306
case DRIZZLE_TYPE_BLOB:
1307
case DRIZZLE_TYPE_VARCHAR:
1308
length*= charset->mbmaxlen;
1310
pack_length= calc_pack_length(sql_type, length);
1312
case DRIZZLE_TYPE_ENUM:
1313
/* Pack_length already calculated in sql_parse.cc */
1314
length*= charset->mbmaxlen;
1315
key_length= pack_length;
1317
case DRIZZLE_TYPE_NEWDECIMAL:
1318
key_length= pack_length=
1319
my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1326
key_length= pack_length= calc_pack_length(sql_type, length);
1333
Init for a tmp table field. To be extended if need be.
1335
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1336
uint32_t length_arg, uint32_t decimals_arg,
1337
bool maybe_null, bool is_unsigned)
1340
sql_type= sql_type_arg;
1341
char_length= length= length_arg;;
1342
unireg_check= Field::NONE;
1344
charset= &my_charset_bin;
1345
pack_flag= (FIELDFLAG_NUMBER |
1346
((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1347
(maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1348
(is_unsigned ? 0 : FIELDFLAG_DECIMAL));
1355
Initialize field definition for create.
1357
@param thd Thread handle
1358
@param fld_name Field name
1359
@param fld_type Field type
1360
@param fld_length Field length
1361
@param fld_decimals Decimal (if any)
1362
@param fld_type_modifier Additional type information
1363
@param fld_default_value Field default value (if any)
1364
@param fld_on_update_value The value of ON UPDATE clause
1365
@param fld_comment Field comment
1366
@param fld_change Field change
1367
@param fld_interval_list Interval list (if any)
1368
@param fld_charset Field charset
1369
@param fld_vcol_info Virtual column data
1377
bool Create_field::init(THD *thd __attribute__((unused)), char *fld_name, enum_field_types fld_type,
1378
char *fld_length, char *fld_decimals,
1379
uint32_t fld_type_modifier, Item *fld_default_value,
1380
Item *fld_on_update_value, LEX_STRING *fld_comment,
1381
char *fld_change, List<String> *fld_interval_list,
1382
const CHARSET_INFO * const fld_charset,
1383
uint32_t fld_geom_type __attribute__((unused)),
1384
enum column_format_type column_format,
1385
virtual_column_info *fld_vcol_info)
1387
uint32_t sign_len, allowed_type_modifier= 0;
1388
uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
1391
field_name= fld_name;
1392
def= fld_default_value;
1393
flags= fld_type_modifier;
1394
flags|= (((uint32_t)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1395
unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1396
Field::NEXT_NUMBER : Field::NONE);
1397
decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
1398
if (decimals >= NOT_FIXED_DEC)
1400
my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1409
pack_length= key_length= 0;
1410
charset= fld_charset;
1411
interval_list.empty();
1413
comment= *fld_comment;
1414
vcol_info= fld_vcol_info;
1417
/* Initialize data for a virtual field */
1418
if (fld_type == DRIZZLE_TYPE_VIRTUAL)
1420
assert(vcol_info && vcol_info->expr_item);
1421
is_stored= vcol_info->get_field_stored();
1423
Perform per item-type checks to determine if the expression is
1424
allowed for a virtual column.
1425
Note that validation of the specific function is done later in
1426
procedures open_table_from_share and fix_fields_vcol_func
1428
switch (vcol_info->expr_item->type()) {
1429
case Item::FUNC_ITEM:
1430
if (((Item_func *)vcol_info->expr_item)->functype() == Item_func::FUNC_SP)
1432
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1436
case Item::COPY_STR_ITEM:
1437
case Item::FIELD_AVG_ITEM:
1438
case Item::PROC_ITEM:
1439
case Item::REF_ITEM:
1440
case Item::FIELD_STD_ITEM:
1441
case Item::FIELD_VARIANCE_ITEM:
1442
case Item::INSERT_VALUE_ITEM:
1443
case Item::SUBSELECT_ITEM:
1444
case Item::CACHE_ITEM:
1445
case Item::TYPE_HOLDER:
1446
case Item::PARAM_ITEM:
1447
case Item::VIEW_FIXER_ITEM:
1448
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1452
// Continue with the field creation
1456
Make a field created for the real type.
1457
Note that "real" and virtual fields differ from each other
1458
only by Field::vcol_info, which is always 0 for normal columns.
1459
vcol_info is updated for fields later in procedure open_binary_frm.
1461
sql_type= fld_type= vcol_info->get_real_type();
1465
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1466
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1468
if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1469
(fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1470
flags|= NO_DEFAULT_VALUE_FLAG;
1472
if (fld_length && !(length= (uint32_t) atoi(fld_length)))
1473
fld_length= 0; /* purecov: inspected */
1474
sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1477
case DRIZZLE_TYPE_TINY:
1479
length= MAX_TINYINT_WIDTH+sign_len;
1480
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1482
case DRIZZLE_TYPE_LONG:
1484
length= MAX_INT_WIDTH+sign_len;
1485
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1487
case DRIZZLE_TYPE_LONGLONG:
1489
length= MAX_BIGINT_WIDTH;
1490
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1492
case DRIZZLE_TYPE_NULL:
1494
case DRIZZLE_TYPE_NEWDECIMAL:
1495
my_decimal_trim(&length, &decimals);
1496
if (length > DECIMAL_MAX_PRECISION)
1498
my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1499
DECIMAL_MAX_PRECISION);
1502
if (length < decimals)
1504
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1508
my_decimal_precision_to_length(length, decimals,
1509
fld_type_modifier & UNSIGNED_FLAG);
1511
my_decimal_get_binary_size(length, decimals);
1513
case DRIZZLE_TYPE_VARCHAR:
1515
Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1516
if they don't have a default value
1518
max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1520
case DRIZZLE_TYPE_BLOB:
1521
if (fld_default_value)
1523
/* Allow empty as default value. */
1525
res= fld_default_value->val_str(&str);
1529
case DRIZZLE_TYPE_DOUBLE:
1530
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1531
if (!fld_length && !fld_decimals)
1534
decimals= NOT_FIXED_DEC;
1536
if (length < decimals &&
1537
decimals != NOT_FIXED_DEC)
1539
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1543
case DRIZZLE_TYPE_TIMESTAMP:
1546
/* Compressed date YYYYMMDDHHMMSS */
1547
length= MAX_DATETIME_COMPRESSED_WIDTH;
1549
else if (length != MAX_DATETIME_WIDTH)
1552
We support only even TIMESTAMP lengths less or equal than 14
1553
and 19 as length of 4.1 compatible representation.
1555
length= ((length+1)/2)*2; /* purecov: inspected */
1556
length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1558
flags|= UNSIGNED_FLAG;
1559
if (fld_default_value)
1561
/* Grammar allows only NOW() value for ON UPDATE clause */
1562
if (fld_default_value->type() == Item::FUNC_ITEM &&
1563
((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
1565
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
1566
Field::TIMESTAMP_DN_FIELD);
1568
We don't need default value any longer moreover it is dangerous.
1569
Everything handled by unireg_check further.
1574
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
1580
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
1581
or ON UPDATE values then for the sake of compatiblity we should treat
1582
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
1583
have another TIMESTAMP column with auto-set option before this one)
1584
or DEFAULT 0 (in other cases).
1585
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
1586
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
1587
information about all TIMESTAMP fields in table will be availiable.
1589
If we have TIMESTAMP NULL column without explicit DEFAULT value
1590
we treat it as having DEFAULT NULL attribute.
1592
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
1593
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
1597
case DRIZZLE_TYPE_NEWDATE:
1600
case DRIZZLE_TYPE_TIME:
1603
case DRIZZLE_TYPE_DATETIME:
1604
length= MAX_DATETIME_WIDTH;
1606
case DRIZZLE_TYPE_ENUM:
1608
/* Should be safe. */
1609
pack_length= get_enum_pack_length(fld_interval_list->elements);
1611
List_iterator<String> it(*fld_interval_list);
1614
interval_list.push_back(tmp);
1615
length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1618
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1621
/* Remember the value of length */
1622
char_length= length;
1624
if (!(flags & BLOB_FLAG) &&
1625
((length > max_field_charlength &&
1626
fld_type != DRIZZLE_TYPE_ENUM &&
1627
(fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1628
(!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1630
my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ? ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
1632
fld_name, max_field_charlength); /* purecov: inspected */
1635
fld_type_modifier&= AUTO_INCREMENT_FLAG;
1636
if ((~allowed_type_modifier) & fld_type_modifier)
1638
my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1642
return(false); /* success */
1646
enum_field_types get_blob_type_from_length(uint32_t length __attribute__((unused)))
1648
enum_field_types type;
1650
type= DRIZZLE_TYPE_BLOB;
1657
Make a field from the .frm file info
1660
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1663
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1664
case DRIZZLE_TYPE_TINY : return 1;
1665
case DRIZZLE_TYPE_NEWDATE:
1666
case DRIZZLE_TYPE_TIME: return 3;
1667
case DRIZZLE_TYPE_TIMESTAMP:
1668
case DRIZZLE_TYPE_LONG : return 4;
1669
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1670
case DRIZZLE_TYPE_DATETIME:
1671
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1672
case DRIZZLE_TYPE_NULL : return 0;
1673
case DRIZZLE_TYPE_BLOB: return 4+portable_sizeof_char_ptr;
1674
case DRIZZLE_TYPE_ENUM:
1675
case DRIZZLE_TYPE_NEWDECIMAL:
1676
abort(); return 0; // This shouldn't happen
1683
uint32_t pack_length_to_packflag(uint32_t type)
1686
case 1: return f_settype((uint32_t) DRIZZLE_TYPE_TINY);
1689
case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
1690
case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1692
return 0; // This shouldn't happen
1696
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
1697
unsigned char *null_pos, unsigned char null_bit,
1699
enum_field_types field_type,
1700
const CHARSET_INFO * field_charset,
1701
Field::utype unireg_check,
1703
const char *field_name)
1705
if (!f_maybe_null(pack_flag))
1712
null_bit= ((unsigned char) 1) << null_bit;
1715
switch (field_type) {
1716
case DRIZZLE_TYPE_NEWDATE:
1717
case DRIZZLE_TYPE_TIME:
1718
case DRIZZLE_TYPE_DATETIME:
1719
case DRIZZLE_TYPE_TIMESTAMP:
1720
field_charset= &my_charset_bin;
1724
if (f_is_alpha(pack_flag))
1726
if (!f_is_packed(pack_flag))
1728
if (field_type == DRIZZLE_TYPE_VARCHAR)
1729
return new Field_varstring(ptr,field_length,
1730
HA_VARCHAR_PACKLENGTH(field_length),
1732
unireg_check, field_name,
1738
uint32_t pack_length=calc_pack_length((enum_field_types)
1739
f_packtype(pack_flag),
1742
if (f_is_blob(pack_flag))
1743
return new Field_blob(ptr,null_pos,null_bit,
1744
unireg_check, field_name, share,
1745
pack_length, field_charset);
1748
if (f_is_enum(pack_flag))
1749
return new Field_enum(ptr,field_length,null_pos,null_bit,
1750
unireg_check, field_name,
1751
pack_length, interval, field_charset);
1755
switch (field_type) {
1756
case DRIZZLE_TYPE_NEWDECIMAL:
1757
return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
1758
unireg_check, field_name,
1759
f_decimals(pack_flag),
1760
f_is_decimal_precision(pack_flag) != 0,
1761
f_is_dec(pack_flag) == 0);
1762
case DRIZZLE_TYPE_DOUBLE:
1763
return new Field_double(ptr,field_length,null_pos,null_bit,
1764
unireg_check, field_name,
1765
f_decimals(pack_flag),
1767
f_is_dec(pack_flag)== 0);
1768
case DRIZZLE_TYPE_TINY:
1769
return new Field_tiny(ptr,field_length,null_pos,null_bit,
1770
unireg_check, field_name,
1772
f_is_dec(pack_flag) == 0);
1773
case DRIZZLE_TYPE_LONG:
1774
return new Field_long(ptr,field_length,null_pos,null_bit,
1775
unireg_check, field_name,
1777
f_is_dec(pack_flag) == 0);
1778
case DRIZZLE_TYPE_LONGLONG:
1779
return new Field_int64_t(ptr,field_length,null_pos,null_bit,
1780
unireg_check, field_name,
1782
f_is_dec(pack_flag) == 0);
1783
case DRIZZLE_TYPE_TIMESTAMP:
1784
return new Field_timestamp(ptr,field_length, null_pos, null_bit,
1785
unireg_check, field_name, share,
1787
case DRIZZLE_TYPE_NEWDATE:
1788
return new Field_newdate(ptr,null_pos,null_bit,
1789
unireg_check, field_name, field_charset);
1790
case DRIZZLE_TYPE_TIME:
1791
return new Field_time(ptr,null_pos,null_bit,
1792
unireg_check, field_name, field_charset);
1793
case DRIZZLE_TYPE_DATETIME:
1794
return new Field_datetime(ptr,null_pos,null_bit,
1795
unireg_check, field_name, field_charset);
1796
case DRIZZLE_TYPE_NULL:
1797
return new Field_null(ptr, field_length, unireg_check, field_name,
1799
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1801
default: // Impossible (Wrong version)
1808
/** Create a field suitable for create of table. */
1810
Create_field::Create_field(Field *old_field,Field *orig_field)
1813
field_name=change=old_field->field_name;
1814
length= old_field->field_length;
1815
flags= old_field->flags;
1816
unireg_check=old_field->unireg_check;
1817
pack_length=old_field->pack_length();
1818
key_length= old_field->key_length();
1819
sql_type= old_field->real_type();
1820
charset= old_field->charset(); // May be NULL ptr
1821
comment= old_field->comment;
1822
decimals= old_field->decimals();
1823
vcol_info= old_field->vcol_info;
1824
is_stored= old_field->is_stored;
1826
/* Fix if the original table had 4 byte pointer blobs */
1827
if (flags & BLOB_FLAG)
1828
pack_length= (pack_length- old_field->table->s->blob_ptr_size +
1829
portable_sizeof_char_ptr);
1832
case DRIZZLE_TYPE_BLOB:
1833
sql_type= DRIZZLE_TYPE_BLOB;
1834
length/= charset->mbmaxlen;
1835
key_length/= charset->mbmaxlen;
1837
/* Change CHAR -> VARCHAR if dynamic record length */
1838
case DRIZZLE_TYPE_ENUM:
1839
case DRIZZLE_TYPE_VARCHAR:
1840
/* This is corrected in create_length_to_internal_length */
1841
length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1847
if (flags & (ENUM_FLAG | SET_FLAG))
1848
interval= ((Field_enum*) old_field)->typelib;
1852
char_length= length;
1854
if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
1855
old_field->ptr && orig_field &&
1856
(sql_type != DRIZZLE_TYPE_TIMESTAMP || /* set def only if */
1857
old_field->table->timestamp_field != old_field || /* timestamp field */
1858
unireg_check == Field::TIMESTAMP_UN_FIELD)) /* has default val */
1860
char buff[MAX_FIELD_WIDTH];
1861
String tmp(buff,sizeof(buff), charset);
1864
/* Get the value from default_values */
1865
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
1866
orig_field->table->record[0]);
1867
orig_field->move_field_offset(diff); // Points now at default_values
1868
if (!orig_field->is_real_null())
1870
char buff[MAX_FIELD_WIDTH], *pos;
1871
String tmp(buff, sizeof(buff), charset), *res;
1872
res= orig_field->val_str(&tmp);
1873
pos= (char*) sql_strmake(res->ptr(), res->length());
1874
def= new Item_string(pos, res->length(), charset);
1876
orig_field->move_field_offset(-diff); // Back to record[0]
1881
/*****************************************************************************
1883
*****************************************************************************/
1886
Produce warning or note about data saved into field.
1888
@param level - level of message (Note/Warning/Error)
1889
@param code - error code of message to be produced
1890
@param cuted_increment - whenever we should increase cut fields count or not
1893
This function won't produce warning and increase cut fields counter
1894
if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
1896
if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
1897
This allows us to avoid notes in optimisation, like convert_constant_item().
1900
1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
1906
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1907
int cuted_increment)
1910
If this field was created only for type conversion purposes it
1911
will have table == NULL.
1913
THD *thd= table ? table->in_use : current_thd;
1914
if (thd->count_cuted_fields)
1916
thd->cuted_fields+= cuted_increment;
1917
push_warning_printf(thd, level, code, ER(code), field_name,
1921
return level >= DRIZZLE_ERROR::WARN_LEVEL_WARN;
1926
Produce warning or note about datetime string data saved into field.
1928
@param level level of message (Note/Warning/Error)
1929
@param code error code of message to be produced
1930
@param str string value which we tried to save
1931
@param str_length length of string which we tried to save
1932
@param ts_type type of datetime value (datetime/date/time)
1933
@param cuted_increment whenever we should increase cut fields count or not
1936
This function will always produce some warning but won't increase cut
1937
fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
1942
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1944
const char *str, uint32_t str_length,
1945
enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
1947
THD *thd= table ? table->in_use : current_thd;
1948
if ((thd->really_abort_on_warning() &&
1949
level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1950
set_warning(level, code, cuted_increment))
1951
make_truncated_value_warning(thd, level, str, str_length, ts_type,
1957
Produce warning or note about integer datetime value saved into field.
1959
@param level level of message (Note/Warning/Error)
1960
@param code error code of message to be produced
1961
@param nr numeric value which we tried to save
1962
@param ts_type type of datetime value (datetime/date/time)
1963
@param cuted_increment whenever we should increase cut fields count or not
1966
This function will always produce some warning but won't increase cut
1967
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
1972
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1973
int64_t nr, enum enum_drizzle_timestamp_type ts_type,
1974
int cuted_increment)
1976
THD *thd= table ? table->in_use : current_thd;
1977
if (thd->really_abort_on_warning() ||
1978
set_warning(level, code, cuted_increment))
1981
char *str_end= int64_t10_to_str(nr, str_nr, -10);
1982
make_truncated_value_warning(thd, level, str_nr, (uint32_t) (str_end - str_nr),
1983
ts_type, field_name);
1989
Produce warning or note about double datetime data saved into field.
1991
@param level level of message (Note/Warning/Error)
1992
@param code error code of message to be produced
1993
@param nr double value which we tried to save
1994
@param ts_type type of datetime value (datetime/date/time)
1997
This function will always produce some warning but won't increase cut
1998
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2003
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
2004
double nr, enum enum_drizzle_timestamp_type ts_type)
2006
THD *thd= table ? table->in_use : current_thd;
2007
if (thd->really_abort_on_warning() ||
2008
set_warning(level, code, 1))
2010
/* DBL_DIG is enough to print '-[digits].E+###' */
2011
char str_nr[DBL_DIG + 8];
2012
uint32_t str_len= sprintf(str_nr, "%g", nr);
2013
make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,