582
479
//DRIZZLE_TYPE_VARCHAR
584
//DRIZZLE_TYPE_DECIMAL
481
//DRIZZLE_TYPE_VIRTUAL
483
//DRIZZLE_TYPE_NEWDECIMAL
586
485
//DRIZZLE_TYPE_ENUM
588
487
//DRIZZLE_TYPE_BLOB
592
//DRIZZLE_TYPE_BOOLEAN
596
//DRIZZLE_TYPE_MICROTIME
600
bool test_if_important_data(const CHARSET_INFO * const cs,
493
Test if the given string contains important data:
494
not spaces for character string,
495
or any data for binary string.
498
test_if_important_data()
504
false - If string does not have important data
505
true - If string has some important data
509
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
604
512
if (cs != &my_charset_bin)
605
513
str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
606
514
return (str < strend);
609
void *Field::operator new(size_t size)
611
return memory::sql_alloc(size);
614
void *Field::operator new(size_t size, memory::Root *mem_root)
616
return mem_root->alloc_root(static_cast<uint32_t>(size));
619
enum_field_types Field::field_type_merge(enum_field_types a,
622
assert(a < enum_field_types_size);
623
assert(b < enum_field_types_size);
624
return field_types_merge_rules[a][b];
627
518
Item_result Field::result_merge_type(enum_field_types field_type)
629
assert(field_type < enum_field_types_size);
520
assert(field_type <= DRIZZLE_TYPE_MAX);
630
521
return field_types_result_type[field_type];
633
525
bool Field::eq(Field *field)
635
527
return (ptr == field->ptr && null_ptr == field->null_ptr &&
636
528
null_bit == field->null_bit);
639
532
uint32_t Field::pack_length() const
641
534
return field_length;
644
538
uint32_t Field::pack_length_in_rec() const
646
540
return pack_length();
544
uint32_t Field::pack_length_from_metadata(uint32_t field_metadata)
546
return field_metadata;
550
uint32_t Field::row_pack_length()
556
int Field::save_field_metadata(unsigned char *first_byte)
558
return do_save_field_metadata(first_byte);
649
562
uint32_t Field::data_length()
651
564
return pack_length();
654
568
uint32_t Field::used_length()
656
570
return pack_length();
659
574
uint32_t Field::sort_length() const
661
576
return pack_length();
664
580
uint32_t Field::max_data_length() const
666
582
return pack_length();
669
586
int Field::reset(void)
671
588
memset(ptr, 0, pack_length());
675
593
void Field::reset_fields()
678
597
void Field::set_default()
680
ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
599
my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
681
600
memcpy(ptr, ptr + l_offset, pack_length());
683
602
*null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
685
if (this == table->next_number_field)
686
table->auto_increment_field_not_null= false;
689
606
bool Field::binary() const
694
612
bool Field::zero_pack() const
699
618
enum ha_base_keytype Field::key_type() const
701
620
return HA_KEYTYPE_BINARY;
704
624
uint32_t Field::key_length() const
706
626
return pack_length();
709
630
enum_field_types Field::real_type() const
714
636
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
716
638
return cmp(a, b);
719
642
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
721
644
return memcmp(a,b,pack_length());
724
648
int Field::cmp_offset(uint32_t row_offset)
726
650
return cmp(ptr,ptr+row_offset);
729
654
int Field::cmp_binary_offset(uint32_t row_offset)
731
656
return cmp_binary(ptr, ptr+row_offset);
734
660
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
736
662
return cmp(a, b);
739
666
int Field::key_cmp(const unsigned char *str, uint32_t)
741
668
return cmp(ptr,str);
744
672
uint32_t Field::decimals() const
749
bool Field::is_null(ptrdiff_t row_offset) const
678
bool Field::is_null(my_ptrdiff_t row_offset)
751
680
return null_ptr ?
752
681
(null_ptr[row_offset] & null_bit ? true : false) :
756
bool Field::is_real_null(ptrdiff_t row_offset) const
686
bool Field::is_real_null(my_ptrdiff_t row_offset)
758
688
return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
761
bool Field::is_null_in_record(const unsigned char *record) const
692
bool Field::is_null_in_record(const unsigned char *record)
765
return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
696
return test(record[(uint32_t) (null_ptr -table->record[0])] &
768
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
701
bool Field::is_null_in_record_with_offset(my_ptrdiff_t with_offset)
772
705
return test(null_ptr[with_offset] & null_bit);
775
void Field::set_null(ptrdiff_t row_offset)
709
void Field::set_null(my_ptrdiff_t row_offset)
778
712
null_ptr[row_offset]|= null_bit;
781
void Field::set_notnull(ptrdiff_t row_offset)
716
void Field::set_notnull(my_ptrdiff_t row_offset)
784
719
null_ptr[row_offset]&= (unsigned char) ~null_bit;
787
bool Field::maybe_null(void) const
723
bool Field::maybe_null(void)
789
725
return null_ptr != 0 || table->maybe_null;
792
bool Field::real_maybe_null(void) const
729
bool Field::real_maybe_null(void)
794
731
return null_ptr != 0;
735
size_t Field::last_null_byte() const
737
size_t bytes= do_last_null_byte();
738
assert(bytes <= table->getNullBytes());
743
/*****************************************************************************
744
Static help functions
745
*****************************************************************************/
797
747
bool Field::type_can_have_key_part(enum enum_field_types type)
760
Process decimal library return codes and issue warnings for overflow and
763
@param op_result decimal library return code (E_DEC_* see include/decimal.h)
768
0 no error or some other errors except overflow
808
771
int Field::warn_if_overflow(int op_result)
810
773
if (op_result == E_DEC_OVERFLOW)
812
775
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
813
return E_DEC_OVERFLOW;
815
778
if (op_result == E_DEC_TRUNCATED)
817
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
818
return E_DEC_TRUNCATED;
780
set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
781
/* We return 0 here as this is not a critical issue */
823
787
void Field::init(Table *table_arg)
825
789
orig_table= table= table_arg;
790
table_name= &table_arg->alias;
795
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
797
cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
799
while (length && my_isspace(cs,*str))
800
{ // Allow start space
805
if (*str == '+' || *str == '-')
808
if (!length || !(my_isdigit(cs,*str) || *str == '.'))
811
while (length && my_isdigit(cs,*str))
820
while (length && my_isdigit(cs,*str))
827
if (*str == 'E' || *str == 'e')
829
if (length < 3 || (str[1] != '+' && str[1] != '-') ||
830
!my_isdigit(cs,str[2]))
834
while (length && my_isdigit(cs,*str))
839
for (; length ; length--, str++)
841
if (!my_isspace(cs,*str))
850
Interpret field value as an integer but return the result as a string.
852
This is used for printing bit_fields as numbers while debugging.
855
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
857
const CHARSET_INFO * const cs= &my_charset_bin;
859
int64_t value= val_int();
861
if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
863
length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
864
MY_INT64_NUM_DECIMAL_DIGITS,
865
unsigned_val ? 10 : -10,
867
val_buffer->length(length);
828
872
/// This is used as a table name when the table structure is not set up
829
Field::Field(unsigned char *ptr_arg,
831
unsigned char *null_ptr_arg,
873
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
832
874
unsigned char null_bit_arg,
833
utype unireg_check_arg,
834
const char *field_name_arg) :
836
null_ptr(null_ptr_arg),
839
field_name(field_name_arg),
840
comment(NULL_LEX_STRING),
843
part_of_key_not_clustered(0),
845
unireg_check(unireg_check_arg),
846
field_length(length_arg),
847
flags(null_ptr ? 0: NOT_NULL_FLAG),
849
null_bit(null_bit_arg),
850
is_created_from_null_item(false)
875
utype unireg_check_arg, const char *field_name_arg)
876
:ptr(ptr_arg), null_ptr(null_ptr_arg),
877
table(0), orig_table(0), table_name(0),
878
field_name(field_name_arg),
879
key_start(0), part_of_key(0), part_of_key_not_clustered(0),
880
part_of_sortkey(0), unireg_check(unireg_check_arg),
881
field_length(length_arg), null_bit(null_bit_arg),
882
is_created_from_null_item(false),
883
vcol_info(NULL), is_stored(true)
885
flags=null_ptr ? 0: NOT_NULL_FLAG;
886
comment.str= (char*) "";
854
void Field::hash(uint32_t *nr, uint32_t *nr2) const
892
void Field::hash(uint32_t *nr, uint32_t *nr2)
1012
1186
return copy->length+ store_length;
1015
bool Field::get_date(type::Time <ime, uint32_t fuzzydate) const
1017
char buff[type::Time::MAX_STRING_LENGTH];
1018
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1020
assert(getTable() and getTable()->getSession());
1022
if (not (res= val_str_internal(&tmp)) or
1023
str_to_datetime_with_warn(getTable()->getSession(),
1024
res->ptr(), res->length(),
1025
<ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1033
bool Field::get_time(type::Time <ime) const
1035
char buff[type::Time::MAX_STRING_LENGTH];
1036
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1038
if (not (res= val_str_internal(&tmp)) or
1039
str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), <ime))
1047
int Field::store_time(type::Time <ime, type::timestamp_t)
1053
return store(tmp.ptr(), tmp.length(), &my_charset_bin);
1056
bool Field::optimize_range(uint32_t idx, uint32_t)
1058
return test(table->index_flags(idx) & HA_READ_RANGE);
1061
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
1190
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1193
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1194
if (!(res=val_str(&tmp)) ||
1195
str_to_datetime_with_warn(res->ptr(), res->length(),
1196
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
1201
bool Field::get_time(DRIZZLE_TIME *ltime)
1204
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1205
if (!(res=val_str(&tmp)) ||
1206
str_to_time_with_warn(res->ptr(), res->length(), ltime))
1212
This is called when storing a date in a string.
1215
Needs to be changed if/when we want to support different time formats.
1218
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
1220
char buff[MAX_DATE_STRING_REP_LENGTH];
1221
uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
1222
return store(buff, length, &my_charset_bin);
1226
bool Field::optimize_range(uint32_t idx, uint32_t part)
1228
return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1232
Field *Field::new_field(MEM_ROOT *root, Table *new_table, bool)
1064
if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
1235
if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1067
1238
if (tmp->table->maybe_null)
1068
1239
tmp->flags&= ~NOT_NULL_FLAG;
1069
1240
tmp->table= new_table;
1070
tmp->key_start.reset();
1071
tmp->part_of_key.reset();
1072
tmp->part_of_sortkey.reset();
1241
tmp->key_start.init(0);
1242
tmp->part_of_key.init(0);
1243
tmp->part_of_sortkey.init(0);
1073
1244
tmp->unireg_check= Field::NONE;
1074
tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG);
1245
tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1075
1246
tmp->reset_fields();
1079
Field *Field::new_key_field(memory::Root *root, Table *new_table,
1080
unsigned char *new_ptr,
1081
unsigned char *new_null_ptr,
1251
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
1252
unsigned char *new_ptr, unsigned char *new_null_ptr,
1082
1253
uint32_t new_null_bit)
1085
1256
if ((tmp= new_field(root, new_table, table == new_table)))
1088
1259
tmp->null_ptr= new_null_ptr;
1089
1260
tmp->null_bit= new_null_bit;
1094
Field *Field::clone(memory::Root *root, Table *new_table)
1266
/* This is used to generate a field in Table from TABLE_SHARE */
1268
Field *Field::clone(MEM_ROOT *root, Table *new_table)
1097
if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
1271
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1099
1273
tmp->init(new_table);
1100
tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1101
new_table->getDefaultValues()));
1274
tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
1275
new_table->s->default_values));
1107
uint32_t Field::is_equal(CreateField *new_field_ptr)
1281
uint32_t Field::is_equal(Create_field *new_field_ptr)
1109
1283
return (new_field_ptr->sql_type == real_type());
1288
1 if the fields are equally defined
1290
0 if the fields are unequally defined
1112
1293
bool Field::eq_def(Field *field)
1114
1295
if (real_type() != field->real_type() || charset() != field->charset() ||
1303
returns 1 if the fields are equally defined
1120
1305
bool Field_enum::eq_def(Field *field)
1122
1307
if (!Field::eq_def(field))
1125
1309
TYPELIB *from_lib=((Field_enum*) field)->typelib;
1127
1311
if (typelib->count < from_lib->count)
1130
1313
for (uint32_t i=0 ; i < from_lib->count ; i++)
1132
1314
if (my_strnncoll(field_charset,
1133
1315
(const unsigned char*)typelib->type_names[i],
1134
1316
strlen(typelib->type_names[i]),
1135
1317
(const unsigned char*)from_lib->type_names[i],
1136
1318
strlen(from_lib->type_names[i])))
1323
/*****************************************************************************
1324
Handling of field and Create_field
1325
*****************************************************************************/
1328
Convert create_field::length from number of characters to number of bytes.
1331
void Create_field::create_length_to_internal_length(void)
1334
case DRIZZLE_TYPE_BLOB:
1335
case DRIZZLE_TYPE_VARCHAR:
1336
length*= charset->mbmaxlen;
1338
pack_length= calc_pack_length(sql_type, length);
1340
case DRIZZLE_TYPE_ENUM:
1341
/* Pack_length already calculated in ::init() */
1342
length*= charset->mbmaxlen;
1343
key_length= pack_length;
1345
case DRIZZLE_TYPE_NEWDECIMAL:
1346
key_length= pack_length=
1347
my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1354
key_length= pack_length= calc_pack_length(sql_type, length);
1361
Init for a tmp table field. To be extended if need be.
1363
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1364
uint32_t length_arg, uint32_t decimals_arg,
1365
bool maybe_null, bool is_unsigned)
1368
sql_type= sql_type_arg;
1369
char_length= length= length_arg;;
1370
unireg_check= Field::NONE;
1372
charset= &my_charset_bin;
1373
pack_flag= (FIELDFLAG_NUMBER |
1374
((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1375
(maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1376
(is_unsigned ? 0 : FIELDFLAG_DECIMAL));
1383
Initialize field definition for create.
1385
@param session Thread handle
1386
@param fld_name Field name
1387
@param fld_type Field type
1388
@param fld_length Field length
1389
@param fld_decimals Decimal (if any)
1390
@param fld_type_modifier Additional type information
1391
@param fld_default_value Field default value (if any)
1392
@param fld_on_update_value The value of ON UPDATE clause
1393
@param fld_comment Field comment
1394
@param fld_change Field change
1395
@param fld_interval_list Interval list (if any)
1396
@param fld_charset Field charset
1397
@param fld_vcol_info Virtual column data
1405
bool Create_field::init(Session *, char *fld_name, enum_field_types fld_type,
1406
char *fld_length, char *fld_decimals,
1407
uint32_t fld_type_modifier, Item *fld_default_value,
1408
Item *fld_on_update_value, LEX_STRING *fld_comment,
1409
char *fld_change, List<String> *fld_interval_list,
1410
const CHARSET_INFO * const fld_charset,
1411
uint32_t, enum column_format_type column_format_in,
1412
virtual_column_info *fld_vcol_info)
1414
uint32_t sign_len, allowed_type_modifier= 0;
1415
uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
1418
field_name= fld_name;
1419
def= fld_default_value;
1420
flags= fld_type_modifier;
1421
flags|= (((uint32_t)column_format_in & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1422
unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1423
Field::NEXT_NUMBER : Field::NONE);
1424
decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
1425
if (decimals >= NOT_FIXED_DEC)
1427
my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1436
pack_length= key_length= 0;
1437
charset= fld_charset;
1438
interval_list.empty();
1440
comment= *fld_comment;
1441
vcol_info= fld_vcol_info;
1444
/* Initialize data for a virtual field */
1445
if (fld_type == DRIZZLE_TYPE_VIRTUAL)
1447
assert(vcol_info && vcol_info->expr_item);
1448
is_stored= vcol_info->get_field_stored();
1450
Perform per item-type checks to determine if the expression is
1451
allowed for a virtual column.
1452
Note that validation of the specific function is done later in
1453
procedures open_table_from_share and fix_fields_vcol_func
1455
switch (vcol_info->expr_item->type()) {
1456
case Item::FUNC_ITEM:
1457
if (((Item_func *)vcol_info->expr_item)->functype() == Item_func::FUNC_SP)
1459
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1463
case Item::COPY_STR_ITEM:
1464
case Item::FIELD_AVG_ITEM:
1465
case Item::PROC_ITEM:
1466
case Item::REF_ITEM:
1467
case Item::FIELD_STD_ITEM:
1468
case Item::FIELD_VARIANCE_ITEM:
1469
case Item::INSERT_VALUE_ITEM:
1470
case Item::SUBSELECT_ITEM:
1471
case Item::CACHE_ITEM:
1472
case Item::TYPE_HOLDER:
1473
case Item::PARAM_ITEM:
1474
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1477
// Continue with the field creation
1481
Make a field created for the real type.
1482
Note that "real" and virtual fields differ from each other
1483
only by Field::vcol_info, which is always 0 for normal columns.
1484
vcol_info is updated for fields later in procedure open_binary_frm.
1486
sql_type= fld_type= vcol_info->get_real_type();
1490
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1491
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1493
if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1494
(fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1495
flags|= NO_DEFAULT_VALUE_FLAG;
1497
if (fld_length && !(length= (uint32_t) atoi(fld_length)))
1498
fld_length= 0; /* purecov: inspected */
1499
sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1502
case DRIZZLE_TYPE_TINY:
1504
length= MAX_TINYINT_WIDTH+sign_len;
1505
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1507
case DRIZZLE_TYPE_LONG:
1509
length= MAX_INT_WIDTH+sign_len;
1510
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1512
case DRIZZLE_TYPE_LONGLONG:
1514
length= MAX_BIGINT_WIDTH;
1515
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1517
case DRIZZLE_TYPE_NULL:
1519
case DRIZZLE_TYPE_NEWDECIMAL:
1520
my_decimal_trim(&length, &decimals);
1521
if (length > DECIMAL_MAX_PRECISION)
1523
my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1524
DECIMAL_MAX_PRECISION);
1527
if (length < decimals)
1529
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1533
my_decimal_precision_to_length(length, decimals,
1534
fld_type_modifier & UNSIGNED_FLAG);
1536
my_decimal_get_binary_size(length, decimals);
1538
case DRIZZLE_TYPE_VARCHAR:
1540
Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1541
if they don't have a default value
1543
max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1545
case DRIZZLE_TYPE_BLOB:
1546
if (fld_default_value)
1548
/* Allow empty as default value. */
1550
res= fld_default_value->val_str(&str);
1553
my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
1561
case DRIZZLE_TYPE_DOUBLE:
1562
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1563
if (!fld_length && !fld_decimals)
1566
decimals= NOT_FIXED_DEC;
1568
if (length < decimals &&
1569
decimals != NOT_FIXED_DEC)
1571
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1575
case DRIZZLE_TYPE_TIMESTAMP:
1578
/* Compressed date YYYYMMDDHHMMSS */
1579
length= MAX_DATETIME_COMPRESSED_WIDTH;
1581
else if (length != MAX_DATETIME_WIDTH)
1584
We support only even TIMESTAMP lengths less or equal than 14
1585
and 19 as length of 4.1 compatible representation.
1587
length= ((length+1)/2)*2; /* purecov: inspected */
1588
length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1590
flags|= UNSIGNED_FLAG;
1591
if (fld_default_value)
1593
/* Grammar allows only NOW() value for ON UPDATE clause */
1594
if (fld_default_value->type() == Item::FUNC_ITEM &&
1595
((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
1597
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
1598
Field::TIMESTAMP_DN_FIELD);
1600
We don't need default value any longer moreover it is dangerous.
1601
Everything handled by unireg_check further.
1606
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
1612
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
1613
or ON UPDATE values then for the sake of compatiblity we should treat
1614
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
1615
have another TIMESTAMP column with auto-set option before this one)
1616
or DEFAULT 0 (in other cases).
1617
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
1618
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
1619
information about all TIMESTAMP fields in table will be availiable.
1621
If we have TIMESTAMP NULL column without explicit DEFAULT value
1622
we treat it as having DEFAULT NULL attribute.
1624
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
1625
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
1629
case DRIZZLE_TYPE_DATE:
1632
case DRIZZLE_TYPE_DATETIME:
1633
length= MAX_DATETIME_WIDTH;
1635
case DRIZZLE_TYPE_ENUM:
1637
/* Should be safe. */
1638
pack_length= get_enum_pack_length(fld_interval_list->elements);
1640
List_iterator<String> it(*fld_interval_list);
1643
interval_list.push_back(tmp);
1644
length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1647
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1650
/* Remember the value of length */
1651
char_length= length;
1653
if (!(flags & BLOB_FLAG) &&
1654
((length > max_field_charlength &&
1655
fld_type != DRIZZLE_TYPE_ENUM &&
1656
(fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1657
(!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1659
my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ? ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
1661
fld_name, max_field_charlength); /* purecov: inspected */
1664
fld_type_modifier&= AUTO_INCREMENT_FLAG;
1665
if ((~allowed_type_modifier) & fld_type_modifier)
1667
my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1671
return(false); /* success */
1675
enum_field_types get_blob_type_from_length(uint32_t)
1677
enum_field_types type;
1679
type= DRIZZLE_TYPE_BLOB;
1686
Make a field from the .frm file info
1143
1689
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1145
1691
switch (type) {
1146
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1147
case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1148
case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1149
case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1150
case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1151
case DRIZZLE_TYPE_DATE:
1152
case DRIZZLE_TYPE_ENUM:
1153
case DRIZZLE_TYPE_LONG: return 4;
1692
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1693
case DRIZZLE_TYPE_TINY : return 1;
1694
case DRIZZLE_TYPE_DATE: return 3;
1695
case DRIZZLE_TYPE_TIMESTAMP:
1696
case DRIZZLE_TYPE_LONG : return 4;
1154
1697
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1155
case DRIZZLE_TYPE_TIME:
1156
1698
case DRIZZLE_TYPE_DATETIME:
1157
1699
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1158
case DRIZZLE_TYPE_NULL: return 0;
1159
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1160
case DRIZZLE_TYPE_DECIMAL:
1700
case DRIZZLE_TYPE_NULL : return 0;
1701
case DRIZZLE_TYPE_BLOB: return 4+portable_sizeof_char_ptr;
1702
case DRIZZLE_TYPE_ENUM:
1703
case DRIZZLE_TYPE_NEWDECIMAL:
1168
1711
uint32_t pack_length_to_packflag(uint32_t type)
1170
1713
switch (type) {
1171
1714
case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1172
1715
case 2: assert(1);
1173
1716
case 3: assert(1);
1174
case 4: return f_settype(DRIZZLE_TYPE_LONG);
1175
case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
1717
case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
1718
case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1177
1720
return 0; // This shouldn't happen
1724
Field *make_field(TABLE_SHARE *share, MEM_ROOT *root,
1725
unsigned char *ptr, uint32_t field_length,
1726
unsigned char *null_pos, unsigned char null_bit,
1728
enum_field_types field_type,
1729
const CHARSET_INFO * field_charset,
1730
Field::utype unireg_check,
1732
const char *field_name)
1735
root= current_mem_root();
1737
if (!f_maybe_null(pack_flag))
1744
null_bit= ((unsigned char) 1) << null_bit;
1747
switch (field_type) {
1748
case DRIZZLE_TYPE_DATE:
1749
case DRIZZLE_TYPE_DATETIME:
1750
case DRIZZLE_TYPE_TIMESTAMP:
1751
field_charset= &my_charset_bin;
1755
if (f_is_alpha(pack_flag))
1757
if (!f_is_packed(pack_flag))
1759
if (field_type == DRIZZLE_TYPE_VARCHAR)
1760
return new (root) Field_varstring(ptr,field_length,
1761
HA_VARCHAR_PACKLENGTH(field_length),
1763
unireg_check, field_name,
1769
uint32_t pack_length=calc_pack_length((enum_field_types)
1770
f_packtype(pack_flag),
1773
if (f_is_blob(pack_flag))
1774
return new (root) Field_blob(ptr,null_pos,null_bit,
1775
unireg_check, field_name, share,
1776
pack_length, field_charset);
1779
if (f_is_enum(pack_flag))
1781
return new (root) Field_enum(ptr,field_length,null_pos,null_bit,
1782
unireg_check, field_name,
1783
get_enum_pack_length(interval->count),
1784
interval, field_charset);
1789
switch (field_type) {
1790
case DRIZZLE_TYPE_NEWDECIMAL:
1791
return new (root) Field_new_decimal(ptr,field_length,null_pos,null_bit,
1792
unireg_check, field_name,
1793
f_decimals(pack_flag),
1794
f_is_decimal_precision(pack_flag) != 0,
1795
f_is_dec(pack_flag) == 0);
1796
case DRIZZLE_TYPE_DOUBLE:
1797
return new (root) Field_double(ptr,field_length,null_pos,null_bit,
1798
unireg_check, field_name,
1799
f_decimals(pack_flag),
1801
f_is_dec(pack_flag)== 0);
1802
case DRIZZLE_TYPE_TINY:
1804
case DRIZZLE_TYPE_LONG:
1805
return new (root) Field_long(ptr,field_length,null_pos,null_bit,
1806
unireg_check, field_name,
1808
f_is_dec(pack_flag) == 0);
1809
case DRIZZLE_TYPE_LONGLONG:
1810
return new (root) Field_int64_t(ptr,field_length,null_pos,null_bit,
1811
unireg_check, field_name,
1813
f_is_dec(pack_flag) == 0);
1814
case DRIZZLE_TYPE_TIMESTAMP:
1815
return new (root) Field_timestamp(ptr,field_length, null_pos, null_bit,
1816
unireg_check, field_name, share,
1818
case DRIZZLE_TYPE_DATE:
1819
return new (root) Field_date(ptr,null_pos,null_bit,
1820
unireg_check, field_name, field_charset);
1821
case DRIZZLE_TYPE_DATETIME:
1822
return new (root) Field_datetime(ptr,null_pos,null_bit,
1823
unireg_check, field_name, field_charset);
1824
case DRIZZLE_TYPE_NULL:
1825
return new (root) Field_null(ptr, field_length, unireg_check, field_name,
1827
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1829
default: // Impossible (Wrong version)
1836
/** Create a field suitable for create of table. */
1838
Create_field::Create_field(Field *old_field,Field *orig_field)
1841
field_name=change=old_field->field_name;
1842
length= old_field->field_length;
1843
flags= old_field->flags;
1844
unireg_check=old_field->unireg_check;
1845
pack_length=old_field->pack_length();
1846
key_length= old_field->key_length();
1847
sql_type= old_field->real_type();
1848
charset= old_field->charset(); // May be NULL ptr
1849
comment= old_field->comment;
1850
decimals= old_field->decimals();
1851
vcol_info= old_field->vcol_info;
1852
is_stored= old_field->is_stored;
1854
/* Fix if the original table had 4 byte pointer blobs */
1855
if (flags & BLOB_FLAG)
1856
pack_length= (pack_length- old_field->table->s->blob_ptr_size +
1857
portable_sizeof_char_ptr);
1860
case DRIZZLE_TYPE_BLOB:
1861
sql_type= DRIZZLE_TYPE_BLOB;
1862
length/= charset->mbmaxlen;
1863
key_length/= charset->mbmaxlen;
1865
/* Change CHAR -> VARCHAR if dynamic record length */
1866
case DRIZZLE_TYPE_ENUM:
1867
case DRIZZLE_TYPE_VARCHAR:
1868
/* This is corrected in create_length_to_internal_length */
1869
length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1875
if (flags & (ENUM_FLAG | SET_FLAG))
1876
interval= ((Field_enum*) old_field)->typelib;
1880
char_length= length;
1882
if (!(flags & (NO_DEFAULT_VALUE_FLAG )) &&
1883
old_field->ptr && orig_field &&
1884
(sql_type != DRIZZLE_TYPE_TIMESTAMP || /* set def only if */
1885
old_field->table->timestamp_field != old_field || /* timestamp field */
1886
unireg_check == Field::TIMESTAMP_UN_FIELD)) /* has default val */
1890
/* Get the value from default_values */
1891
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
1892
orig_field->table->record[0]);
1893
orig_field->move_field_offset(diff); // Points now at default_values
1894
if (!orig_field->is_real_null())
1896
char buff[MAX_FIELD_WIDTH], *pos;
1897
String tmp(buff, sizeof(buff), charset), *res;
1898
res= orig_field->val_str(&tmp);
1899
pos= (char*) sql_strmake(res->ptr(), res->length());
1900
def= new Item_string(pos, res->length(), charset);
1902
orig_field->move_field_offset(-diff); // Back to record[0]
1180
1907
/*****************************************************************************
1181
1908
Warning handling
1182
1909
*****************************************************************************/
1184
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1185
drizzled::error_t code,
1186
int cuted_increment)
1912
Produce warning or note about data saved into field.
1914
@param level - level of message (Note/Warning/Error)
1915
@param code - error code of message to be produced
1916
@param cuted_increment - whenever we should increase cut fields count or not
1919
This function won't produce warning and increase cut fields counter
1920
if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
1922
if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
1923
This allows us to avoid notes in optimisation, like convert_constant_item().
1926
1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
1932
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1933
int cuted_increment)
1189
1936
If this field was created only for type conversion purposes it
1204
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1205
drizzled::error_t code,
1207
uint32_t str_length,
1208
type::timestamp_t ts_type,
1209
int cuted_increment)
1952
Produce warning or note about datetime string data saved into field.
1954
@param level level of message (Note/Warning/Error)
1955
@param code error code of message to be produced
1956
@param str string value which we tried to save
1957
@param str_length length of string which we tried to save
1958
@param ts_type type of datetime value (datetime/date/time)
1959
@param cuted_increment whenever we should increase cut fields count or not
1962
This function will always produce some warning but won't increase cut
1963
fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
1968
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1970
const char *str, uint32_t str_length,
1971
enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
1211
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1213
if ((session->abortOnWarning() and
1973
Session *session= table ? table->in_use : current_session;
1974
if ((session->really_abort_on_warning() &&
1214
1975
level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1215
1976
set_warning(level, code, cuted_increment))
1216
1977
make_truncated_value_warning(session, level, str, str_length, ts_type,
1220
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1221
drizzled::error_t code,
1223
type::timestamp_t ts_type,
1224
int cuted_increment)
1983
Produce warning or note about integer datetime value saved into field.
1985
@param level level of message (Note/Warning/Error)
1986
@param code error code of message to be produced
1987
@param nr numeric value which we tried to save
1988
@param ts_type type of datetime value (datetime/date/time)
1989
@param cuted_increment whenever we should increase cut fields count or not
1992
This function will always produce some warning but won't increase cut
1993
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
1998
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1999
int64_t nr, enum enum_drizzle_timestamp_type ts_type,
2000
int cuted_increment)
1226
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1228
if (session->abortOnWarning() or
2002
Session *session= table ? table->in_use : current_session;
2003
if (session->really_abort_on_warning() ||
1229
2004
set_warning(level, code, cuted_increment))
1231
char str_nr[DECIMAL_LONGLONG_DIGITS];
1232
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
2007
char *str_end= int64_t10_to_str(nr, str_nr, -10);
1233
2008
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1234
2009
ts_type, field_name);
1238
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1239
const drizzled::error_t code,
1241
type::timestamp_t ts_type)
2015
Produce warning or note about double datetime data saved into field.
2017
@param level level of message (Note/Warning/Error)
2018
@param code error code of message to be produced
2019
@param nr double value which we tried to save
2020
@param ts_type type of datetime value (datetime/date/time)
2023
This function will always produce some warning but won't increase cut
2024
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2029
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, const uint32_t code,
2030
double nr, enum enum_drizzle_timestamp_type ts_type)
1243
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1245
if (session->abortOnWarning() or
2032
Session *session= table ? table->in_use : current_session;
2033
if (session->really_abort_on_warning() ||
1246
2034
set_warning(level, code, 1))
1248
2036
/* DBL_DIG is enough to print '-[digits].E+###' */
1249
2037
char str_nr[DBL_DIG + 8];
1250
uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
2038
uint32_t str_len= sprintf(str_nr, "%g", nr);
1251
2039
make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1256
bool Field::isReadSet() const
1258
return table->isReadSet(field_index);
1261
bool Field::isWriteSet()
1263
return table->isWriteSet(field_index);
1266
void Field::setReadSet(bool arg)
1269
table->setReadSet(field_index);
1271
table->clearReadSet(field_index);
1274
void Field::setWriteSet(bool arg)
1277
table->setWriteSet(field_index);
1279
table->clearWriteSet(field_index);
1282
void Field::pack_num(uint64_t arg, unsigned char *destination)
1284
if (not destination)
1287
int64_tstore(destination, arg);
1290
void Field::pack_num(uint32_t arg, unsigned char *destination)
1292
if (not destination)
1295
longstore(destination, arg);
1298
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1303
int64_tget(destination, arg);
1308
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1313
longget(destination, arg);
1318
std::ostream& operator<<(std::ostream& output, const Field &field)
1320
output << "Field:(";
1321
output << field.field_name;
1323
output << drizzled::display::type(field.real_type());
1326
if (field.flags & NOT_NULL_FLAG)
1327
output << " NOT_NULL";
1329
if (field.flags & PRI_KEY_FLAG)
1330
output << ", PRIMARY KEY";
1332
if (field.flags & UNIQUE_KEY_FLAG)
1333
output << ", UNIQUE KEY";
1335
if (field.flags & MULTIPLE_KEY_FLAG)
1336
output << ", MULTIPLE KEY";
1338
if (field.flags & BLOB_FLAG)
1341
if (field.flags & UNSIGNED_FLAG)
1342
output << ", UNSIGNED";
1344
if (field.flags & BINARY_FLAG)
1345
output << ", BINARY";
1349
return output; // for multiple << operators.
1352
} /* namespace drizzled */