531
642
//DRIZZLE_TYPE_LONGLONG
535
644
//DRIZZLE_TYPE_DATETIME
537
646
//DRIZZLE_TYPE_DATE
539
648
//DRIZZLE_TYPE_VARCHAR
541
//DRIZZLE_TYPE_VIRTUAL
650
//DRIZZLE_TYPE_DECIMAL
543
//DRIZZLE_TYPE_NEWDECIMAL DRIZZLE_TYPE_ENUM
544
DECIMAL_RESULT, STRING_RESULT,
545
654
//DRIZZLE_TYPE_BLOB
658
//DRIZZLE_TYPE_BOOLEAN
662
//DRIZZLE_TYPE_MICROTIME
551
Test if the given string contains important data:
552
not spaces for character string,
553
or any data for binary string.
556
test_if_important_data()
562
false - If string does not have important data
563
true - If string has some important data
567
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
668
bool test_if_important_data(const charset_info_st * const cs,
570
672
if (cs != &my_charset_bin)
571
673
str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
572
674
return (str < strend);
677
void *Field::operator new(size_t size)
679
return memory::sql_alloc(size);
682
void *Field::operator new(size_t size, memory::Root *mem_root)
684
return mem_root->alloc(size);
687
enum_field_types Field::field_type_merge(enum_field_types a,
690
assert(a < enum_field_types_size);
691
assert(b < enum_field_types_size);
692
return field_types_merge_rules[a][b];
576
695
Item_result Field::result_merge_type(enum_field_types field_type)
578
assert(field_type <= DRIZZLE_TYPE_MAX);
697
assert(field_type < enum_field_types_size);
579
698
return field_types_result_type[field_type];
583
701
bool Field::eq(Field *field)
585
703
return (ptr == field->ptr && null_ptr == field->null_ptr &&
586
704
null_bit == field->null_bit);
590
707
uint32_t Field::pack_length() const
592
709
return field_length;
596
712
uint32_t Field::pack_length_in_rec() const
598
714
return pack_length();
602
uint32_t Field::pack_length_from_metadata(uint32_t field_metadata)
604
return field_metadata;
608
uint32_t Field::row_pack_length()
614
int Field::save_field_metadata(unsigned char *first_byte)
616
return do_save_field_metadata(first_byte);
620
717
uint32_t Field::data_length()
622
719
return pack_length();
626
722
uint32_t Field::used_length()
628
724
return pack_length();
632
727
uint32_t Field::sort_length() const
634
729
return pack_length();
638
732
uint32_t Field::max_data_length() const
640
734
return pack_length();
644
737
int Field::reset(void)
646
739
memset(ptr, 0, pack_length());
651
743
void Field::reset_fields()
655
746
void Field::set_default()
657
my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
748
ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
658
749
memcpy(ptr, ptr + l_offset, pack_length());
660
751
*null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
753
if (this == table->next_number_field)
754
table->auto_increment_field_not_null= false;
664
757
bool Field::binary() const
670
762
bool Field::zero_pack() const
676
767
enum ha_base_keytype Field::key_type() const
678
769
return HA_KEYTYPE_BINARY;
682
772
uint32_t Field::key_length() const
684
774
return pack_length();
688
777
enum_field_types Field::real_type() const
694
782
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
696
784
return cmp(a, b);
700
787
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
702
789
return memcmp(a,b,pack_length());
706
792
int Field::cmp_offset(uint32_t row_offset)
708
794
return cmp(ptr,ptr+row_offset);
712
797
int Field::cmp_binary_offset(uint32_t row_offset)
714
799
return cmp_binary(ptr, ptr+row_offset);
718
802
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
720
804
return cmp(a, b);
724
807
int Field::key_cmp(const unsigned char *str, uint32_t)
726
809
return cmp(ptr,str);
730
812
uint32_t Field::decimals() const
736
bool Field::is_null(my_ptrdiff_t row_offset)
739
(null_ptr[row_offset] & null_bit ? true : false) :
744
bool Field::is_real_null(my_ptrdiff_t row_offset)
746
return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
750
bool Field::is_null_in_record(const unsigned char *record)
754
return test(record[(uint32_t) (null_ptr -table->record[0])] &
759
bool Field::is_null_in_record_with_offset(my_ptrdiff_t offset)
763
return test(null_ptr[offset] & null_bit);
767
void Field::set_null(my_ptrdiff_t row_offset)
817
bool Field::is_null(ptrdiff_t row_offset) const
819
return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : table->null_row;
822
bool Field::is_real_null(ptrdiff_t row_offset) const
824
return null_ptr && (null_ptr[row_offset] & null_bit);
827
bool Field::is_null_in_record(const unsigned char *record) const
829
return null_ptr && test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
832
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
834
return null_ptr && test(null_ptr[with_offset] & null_bit);
837
void Field::set_null(ptrdiff_t row_offset)
770
840
null_ptr[row_offset]|= null_bit;
774
void Field::set_notnull(my_ptrdiff_t row_offset)
843
void Field::set_notnull(ptrdiff_t row_offset)
777
846
null_ptr[row_offset]&= (unsigned char) ~null_bit;
781
bool Field::maybe_null(void)
849
bool Field::maybe_null(void) const
783
851
return null_ptr != 0 || table->maybe_null;
787
bool Field::real_maybe_null(void)
854
bool Field::real_maybe_null(void) const
789
856
return null_ptr != 0;
793
size_t Field::last_null_byte() const
795
size_t bytes= do_last_null_byte();
796
assert(bytes <= table->getNullBytes());
801
/*****************************************************************************
802
Static help functions
803
*****************************************************************************/
805
859
bool Field::type_can_have_key_part(enum enum_field_types type)
808
863
case DRIZZLE_TYPE_VARCHAR:
809
864
case DRIZZLE_TYPE_BLOB:
1254
1071
return copy->length+ store_length;
1258
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1261
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1262
if (!(res=val_str(&tmp)) ||
1263
str_to_datetime_with_warn(res->ptr(), res->length(),
1264
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
1269
bool Field::get_time(DRIZZLE_TIME *ltime)
1272
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1273
if (!(res=val_str(&tmp)) ||
1274
str_to_time_with_warn(res->ptr(), res->length(), ltime))
1280
This is called when storing a date in a string.
1283
Needs to be changed if/when we want to support different time formats.
1286
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
1288
char buff[MAX_DATE_STRING_REP_LENGTH];
1289
uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
1290
return store(buff, length, &my_charset_bin);
1294
bool Field::optimize_range(uint32_t idx, uint32_t part)
1296
return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1300
Field *Field::new_field(MEM_ROOT *root, Table *new_table, bool)
1303
if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1074
bool Field::get_date(type::Time <ime, uint32_t fuzzydate) const
1076
char buff[type::Time::MAX_STRING_LENGTH];
1077
String tmp(buff,sizeof(buff),&my_charset_bin);
1079
assert(getTable() and getTable()->getSession());
1081
String* res= val_str_internal(&tmp);
1082
return not res or str_to_datetime_with_warn(*getTable()->getSession(), *res, ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR;
1085
bool Field::get_time(type::Time <ime) const
1087
char buff[type::Time::MAX_STRING_LENGTH];
1088
String tmp(buff,sizeof(buff),&my_charset_bin);
1090
String* res= val_str_internal(&tmp);
1091
return not res or str_to_time_with_warn(*getTable()->getSession(), *res, ltime);
1094
int Field::store_time(type::Time <ime, type::timestamp_t)
1098
return store(tmp.ptr(), tmp.length(), &my_charset_bin);
1101
bool Field::optimize_range(uint32_t idx, uint32_t)
1103
return test(table->index_flags(idx) & HA_READ_RANGE);
1106
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
1108
Field* tmp= (Field*) root->memdup(this,size_of());
1306
1109
if (tmp->table->maybe_null)
1307
1110
tmp->flags&= ~NOT_NULL_FLAG;
1308
1111
tmp->table= new_table;
1309
tmp->key_start.init(0);
1310
tmp->part_of_key.init(0);
1311
tmp->part_of_sortkey.init(0);
1112
tmp->key_start.reset();
1113
tmp->part_of_key.reset();
1114
tmp->part_of_sortkey.reset();
1312
1115
tmp->unireg_check= Field::NONE;
1313
tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1116
tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG);
1314
1117
tmp->reset_fields();
1319
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
1320
unsigned char *new_ptr, unsigned char *new_null_ptr,
1121
Field *Field::new_key_field(memory::Root *root, Table *new_table,
1122
unsigned char *new_ptr,
1123
unsigned char *new_null_ptr,
1321
1124
uint32_t new_null_bit)
1324
if ((tmp= new_field(root, new_table, table == new_table)))
1327
tmp->null_ptr= new_null_ptr;
1328
tmp->null_bit= new_null_bit;
1334
/* This is used to generate a field in Table from TABLE_SHARE */
1336
Field *Field::clone(MEM_ROOT *root, Table *new_table)
1339
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1341
tmp->init(new_table);
1342
tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
1343
new_table->s->default_values));
1349
uint32_t Field::is_equal(Create_field *new_field)
1351
return (new_field->sql_type == real_type());
1356
1 if the fields are equally defined
1358
0 if the fields are unequally defined
1126
Field *tmp= new_field(root, new_table, table == new_table);
1128
tmp->null_ptr= new_null_ptr;
1129
tmp->null_bit= new_null_bit;
1133
Field *Field::clone(memory::Root *root, Table *new_table)
1135
Field *tmp= (Field*) root->memdup(this,size_of());
1136
tmp->init(new_table);
1137
tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() - new_table->getDefaultValues()));
1141
uint32_t Field::is_equal(CreateField *new_field_ptr)
1143
return new_field_ptr->sql_type == real_type();
1361
1146
bool Field::eq_def(Field *field)
1363
if (real_type() != field->real_type() || charset() != field->charset() ||
1364
pack_length() != field->pack_length())
1148
return real_type() == field->real_type() && charset() == field->charset() && pack_length() == field->pack_length();
1371
returns 1 if the fields are equally defined
1373
1151
bool Field_enum::eq_def(Field *field)
1375
1153
if (!Field::eq_def(field))
1377
1156
TYPELIB *from_lib=((Field_enum*) field)->typelib;
1379
1158
if (typelib->count < from_lib->count)
1381
1161
for (uint32_t i=0 ; i < from_lib->count ; i++)
1382
1163
if (my_strnncoll(field_charset,
1383
(const unsigned char*)typelib->type_names[i],
1384
strlen(typelib->type_names[i]),
1385
(const unsigned char*)from_lib->type_names[i],
1386
strlen(from_lib->type_names[i])))
1164
(const unsigned char*)typelib->type_names[i], strlen(typelib->type_names[i]),
1165
(const unsigned char*)from_lib->type_names[i], strlen(from_lib->type_names[i])))
1391
/*****************************************************************************
1392
Handling of field and Create_field
1393
*****************************************************************************/
1396
Convert create_field::length from number of characters to number of bytes.
1399
void Create_field::create_length_to_internal_length(void)
1402
case DRIZZLE_TYPE_BLOB:
1403
case DRIZZLE_TYPE_VARCHAR:
1404
length*= charset->mbmaxlen;
1406
pack_length= calc_pack_length(sql_type, length);
1408
case DRIZZLE_TYPE_ENUM:
1409
/* Pack_length already calculated in sql_parse.cc */
1410
length*= charset->mbmaxlen;
1411
key_length= pack_length;
1413
case DRIZZLE_TYPE_NEWDECIMAL:
1414
key_length= pack_length=
1415
my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1422
key_length= pack_length= calc_pack_length(sql_type, length);
1429
Init for a tmp table field. To be extended if need be.
1431
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1432
uint32_t length_arg, uint32_t decimals_arg,
1433
bool maybe_null, bool is_unsigned)
1436
sql_type= sql_type_arg;
1437
char_length= length= length_arg;;
1438
unireg_check= Field::NONE;
1440
charset= &my_charset_bin;
1441
pack_flag= (FIELDFLAG_NUMBER |
1442
((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1443
(maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1444
(is_unsigned ? 0 : FIELDFLAG_DECIMAL));
1451
Initialize field definition for create.
1453
@param session Thread handle
1454
@param fld_name Field name
1455
@param fld_type Field type
1456
@param fld_length Field length
1457
@param fld_decimals Decimal (if any)
1458
@param fld_type_modifier Additional type information
1459
@param fld_default_value Field default value (if any)
1460
@param fld_on_update_value The value of ON UPDATE clause
1461
@param fld_comment Field comment
1462
@param fld_change Field change
1463
@param fld_interval_list Interval list (if any)
1464
@param fld_charset Field charset
1465
@param fld_vcol_info Virtual column data
1473
bool Create_field::init(Session *, char *fld_name, enum_field_types fld_type,
1474
char *fld_length, char *fld_decimals,
1475
uint32_t fld_type_modifier, Item *fld_default_value,
1476
Item *fld_on_update_value, LEX_STRING *fld_comment,
1477
char *fld_change, List<String> *fld_interval_list,
1478
const CHARSET_INFO * const fld_charset,
1479
uint32_t, enum column_format_type column_format,
1480
virtual_column_info *fld_vcol_info)
1482
uint32_t sign_len, allowed_type_modifier= 0;
1483
uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
1486
field_name= fld_name;
1487
def= fld_default_value;
1488
flags= fld_type_modifier;
1489
flags|= (((uint32_t)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1490
unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1491
Field::NEXT_NUMBER : Field::NONE);
1492
decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
1493
if (decimals >= NOT_FIXED_DEC)
1495
my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1504
pack_length= key_length= 0;
1505
charset= fld_charset;
1506
interval_list.empty();
1508
comment= *fld_comment;
1509
vcol_info= fld_vcol_info;
1512
/* Initialize data for a virtual field */
1513
if (fld_type == DRIZZLE_TYPE_VIRTUAL)
1515
assert(vcol_info && vcol_info->expr_item);
1516
is_stored= vcol_info->get_field_stored();
1518
Perform per item-type checks to determine if the expression is
1519
allowed for a virtual column.
1520
Note that validation of the specific function is done later in
1521
procedures open_table_from_share and fix_fields_vcol_func
1523
switch (vcol_info->expr_item->type()) {
1524
case Item::FUNC_ITEM:
1525
if (((Item_func *)vcol_info->expr_item)->functype() == Item_func::FUNC_SP)
1527
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1531
case Item::COPY_STR_ITEM:
1532
case Item::FIELD_AVG_ITEM:
1533
case Item::PROC_ITEM:
1534
case Item::REF_ITEM:
1535
case Item::FIELD_STD_ITEM:
1536
case Item::FIELD_VARIANCE_ITEM:
1537
case Item::INSERT_VALUE_ITEM:
1538
case Item::SUBSELECT_ITEM:
1539
case Item::CACHE_ITEM:
1540
case Item::TYPE_HOLDER:
1541
case Item::PARAM_ITEM:
1542
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1546
// Continue with the field creation
1550
Make a field created for the real type.
1551
Note that "real" and virtual fields differ from each other
1552
only by Field::vcol_info, which is always 0 for normal columns.
1553
vcol_info is updated for fields later in procedure open_binary_frm.
1555
sql_type= fld_type= vcol_info->get_real_type();
1559
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1560
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1562
if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1563
(fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1564
flags|= NO_DEFAULT_VALUE_FLAG;
1566
if (fld_length && !(length= (uint32_t) atoi(fld_length)))
1567
fld_length= 0; /* purecov: inspected */
1568
sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1571
case DRIZZLE_TYPE_TINY:
1573
length= MAX_TINYINT_WIDTH+sign_len;
1574
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1576
case DRIZZLE_TYPE_LONG:
1578
length= MAX_INT_WIDTH+sign_len;
1579
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1581
case DRIZZLE_TYPE_LONGLONG:
1583
length= MAX_BIGINT_WIDTH;
1584
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1586
case DRIZZLE_TYPE_NULL:
1588
case DRIZZLE_TYPE_NEWDECIMAL:
1589
my_decimal_trim(&length, &decimals);
1590
if (length > DECIMAL_MAX_PRECISION)
1592
my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1593
DECIMAL_MAX_PRECISION);
1596
if (length < decimals)
1598
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1602
my_decimal_precision_to_length(length, decimals,
1603
fld_type_modifier & UNSIGNED_FLAG);
1605
my_decimal_get_binary_size(length, decimals);
1607
case DRIZZLE_TYPE_VARCHAR:
1609
Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1610
if they don't have a default value
1612
max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1614
case DRIZZLE_TYPE_BLOB:
1615
if (fld_default_value)
1617
/* Allow empty as default value. */
1619
res= fld_default_value->val_str(&str);
1623
case DRIZZLE_TYPE_DOUBLE:
1624
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1625
if (!fld_length && !fld_decimals)
1628
decimals= NOT_FIXED_DEC;
1630
if (length < decimals &&
1631
decimals != NOT_FIXED_DEC)
1633
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1637
case DRIZZLE_TYPE_TIMESTAMP:
1640
/* Compressed date YYYYMMDDHHMMSS */
1641
length= MAX_DATETIME_COMPRESSED_WIDTH;
1643
else if (length != MAX_DATETIME_WIDTH)
1646
We support only even TIMESTAMP lengths less or equal than 14
1647
and 19 as length of 4.1 compatible representation.
1649
length= ((length+1)/2)*2; /* purecov: inspected */
1650
length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1652
flags|= UNSIGNED_FLAG;
1653
if (fld_default_value)
1655
/* Grammar allows only NOW() value for ON UPDATE clause */
1656
if (fld_default_value->type() == Item::FUNC_ITEM &&
1657
((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
1659
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
1660
Field::TIMESTAMP_DN_FIELD);
1662
We don't need default value any longer moreover it is dangerous.
1663
Everything handled by unireg_check further.
1668
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
1674
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
1675
or ON UPDATE values then for the sake of compatiblity we should treat
1676
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
1677
have another TIMESTAMP column with auto-set option before this one)
1678
or DEFAULT 0 (in other cases).
1679
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
1680
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
1681
information about all TIMESTAMP fields in table will be availiable.
1683
If we have TIMESTAMP NULL column without explicit DEFAULT value
1684
we treat it as having DEFAULT NULL attribute.
1686
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
1687
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
1691
case DRIZZLE_TYPE_DATE:
1694
case DRIZZLE_TYPE_TIME:
1697
case DRIZZLE_TYPE_DATETIME:
1698
length= MAX_DATETIME_WIDTH;
1700
case DRIZZLE_TYPE_ENUM:
1702
/* Should be safe. */
1703
pack_length= get_enum_pack_length(fld_interval_list->elements);
1705
List_iterator<String> it(*fld_interval_list);
1708
interval_list.push_back(tmp);
1709
length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1712
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1715
/* Remember the value of length */
1716
char_length= length;
1718
if (!(flags & BLOB_FLAG) &&
1719
((length > max_field_charlength &&
1720
fld_type != DRIZZLE_TYPE_ENUM &&
1721
(fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1722
(!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1724
my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ? ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
1726
fld_name, max_field_charlength); /* purecov: inspected */
1729
fld_type_modifier&= AUTO_INCREMENT_FLAG;
1730
if ((~allowed_type_modifier) & fld_type_modifier)
1732
my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1736
return(false); /* success */
1740
enum_field_types get_blob_type_from_length(uint32_t)
1742
enum_field_types type;
1744
type= DRIZZLE_TYPE_BLOB;
1751
Make a field from the .frm file info
1754
1172
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1757
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1758
case DRIZZLE_TYPE_TINY : return 1;
1176
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1177
case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1178
case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1179
case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1180
case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1181
case DRIZZLE_TYPE_IPV6: return field::IPv6::max_string_length();
1759
1182
case DRIZZLE_TYPE_DATE:
1760
case DRIZZLE_TYPE_TIME: return 3;
1761
case DRIZZLE_TYPE_TIMESTAMP:
1762
case DRIZZLE_TYPE_LONG : return 4;
1183
case DRIZZLE_TYPE_ENUM:
1184
case DRIZZLE_TYPE_LONG: return 4;
1763
1185
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1186
case DRIZZLE_TYPE_TIME:
1764
1187
case DRIZZLE_TYPE_DATETIME:
1765
1188
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1766
case DRIZZLE_TYPE_NULL : return 0;
1767
case DRIZZLE_TYPE_BLOB: return 4+portable_sizeof_char_ptr;
1768
case DRIZZLE_TYPE_ENUM:
1769
case DRIZZLE_TYPE_NEWDECIMAL:
1770
abort(); return 0; // This shouldn't happen
1189
case DRIZZLE_TYPE_NULL: return 0;
1190
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1191
case DRIZZLE_TYPE_DECIMAL:
1777
1199
uint32_t pack_length_to_packflag(uint32_t type)
1780
case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1783
case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
1784
case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1203
case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1206
case 4: return f_settype(DRIZZLE_TYPE_LONG);
1207
case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
1786
1210
return 0; // This shouldn't happen
1790
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
1791
unsigned char *null_pos, unsigned char null_bit,
1793
enum_field_types field_type,
1794
const CHARSET_INFO * field_charset,
1795
Field::utype unireg_check,
1797
const char *field_name)
1799
if (!f_maybe_null(pack_flag))
1806
null_bit= ((unsigned char) 1) << null_bit;
1809
switch (field_type) {
1810
case DRIZZLE_TYPE_DATE:
1811
case DRIZZLE_TYPE_TIME:
1812
case DRIZZLE_TYPE_DATETIME:
1813
case DRIZZLE_TYPE_TIMESTAMP:
1814
field_charset= &my_charset_bin;
1818
if (f_is_alpha(pack_flag))
1820
if (!f_is_packed(pack_flag))
1822
if (field_type == DRIZZLE_TYPE_VARCHAR)
1823
return new Field_varstring(ptr,field_length,
1824
HA_VARCHAR_PACKLENGTH(field_length),
1826
unireg_check, field_name,
1832
uint32_t pack_length=calc_pack_length((enum_field_types)
1833
f_packtype(pack_flag),
1836
if (f_is_blob(pack_flag))
1837
return new Field_blob(ptr,null_pos,null_bit,
1838
unireg_check, field_name, share,
1839
pack_length, field_charset);
1842
if (f_is_enum(pack_flag))
1844
return new Field_enum(ptr,field_length,null_pos,null_bit,
1845
unireg_check, field_name,
1846
get_enum_pack_length(interval->count),
1847
interval, field_charset);
1852
switch (field_type) {
1853
case DRIZZLE_TYPE_NEWDECIMAL:
1854
return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
1855
unireg_check, field_name,
1856
f_decimals(pack_flag),
1857
f_is_decimal_precision(pack_flag) != 0,
1858
f_is_dec(pack_flag) == 0);
1859
case DRIZZLE_TYPE_DOUBLE:
1860
return new Field_double(ptr,field_length,null_pos,null_bit,
1861
unireg_check, field_name,
1862
f_decimals(pack_flag),
1864
f_is_dec(pack_flag)== 0);
1865
case DRIZZLE_TYPE_TINY:
1867
case DRIZZLE_TYPE_LONG:
1868
return new Field_long(ptr,field_length,null_pos,null_bit,
1869
unireg_check, field_name,
1871
f_is_dec(pack_flag) == 0);
1872
case DRIZZLE_TYPE_LONGLONG:
1873
return new Field_int64_t(ptr,field_length,null_pos,null_bit,
1874
unireg_check, field_name,
1876
f_is_dec(pack_flag) == 0);
1877
case DRIZZLE_TYPE_TIMESTAMP:
1878
return new Field_timestamp(ptr,field_length, null_pos, null_bit,
1879
unireg_check, field_name, share,
1881
case DRIZZLE_TYPE_DATE:
1882
return new Field_date(ptr,null_pos,null_bit,
1883
unireg_check, field_name, field_charset);
1884
case DRIZZLE_TYPE_TIME:
1885
return new Field_time(ptr,null_pos,null_bit,
1886
unireg_check, field_name, field_charset);
1887
case DRIZZLE_TYPE_DATETIME:
1888
return new Field_datetime(ptr,null_pos,null_bit,
1889
unireg_check, field_name, field_charset);
1890
case DRIZZLE_TYPE_NULL:
1891
return new Field_null(ptr, field_length, unireg_check, field_name,
1893
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1895
default: // Impossible (Wrong version)
1902
/** Create a field suitable for create of table. */
1904
Create_field::Create_field(Field *old_field,Field *orig_field)
1907
field_name=change=old_field->field_name;
1908
length= old_field->field_length;
1909
flags= old_field->flags;
1910
unireg_check=old_field->unireg_check;
1911
pack_length=old_field->pack_length();
1912
key_length= old_field->key_length();
1913
sql_type= old_field->real_type();
1914
charset= old_field->charset(); // May be NULL ptr
1915
comment= old_field->comment;
1916
decimals= old_field->decimals();
1917
vcol_info= old_field->vcol_info;
1918
is_stored= old_field->is_stored;
1920
/* Fix if the original table had 4 byte pointer blobs */
1921
if (flags & BLOB_FLAG)
1922
pack_length= (pack_length- old_field->table->s->blob_ptr_size +
1923
portable_sizeof_char_ptr);
1926
case DRIZZLE_TYPE_BLOB:
1927
sql_type= DRIZZLE_TYPE_BLOB;
1928
length/= charset->mbmaxlen;
1929
key_length/= charset->mbmaxlen;
1931
/* Change CHAR -> VARCHAR if dynamic record length */
1932
case DRIZZLE_TYPE_ENUM:
1933
case DRIZZLE_TYPE_VARCHAR:
1934
/* This is corrected in create_length_to_internal_length */
1935
length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1941
if (flags & (ENUM_FLAG | SET_FLAG))
1942
interval= ((Field_enum*) old_field)->typelib;
1946
char_length= length;
1948
if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
1949
old_field->ptr && orig_field &&
1950
(sql_type != DRIZZLE_TYPE_TIMESTAMP || /* set def only if */
1951
old_field->table->timestamp_field != old_field || /* timestamp field */
1952
unireg_check == Field::TIMESTAMP_UN_FIELD)) /* has default val */
1954
char buff[MAX_FIELD_WIDTH];
1955
String tmp(buff,sizeof(buff), charset);
1958
/* Get the value from default_values */
1959
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
1960
orig_field->table->record[0]);
1961
orig_field->move_field_offset(diff); // Points now at default_values
1962
if (!orig_field->is_real_null())
1964
char buff[MAX_FIELD_WIDTH], *pos;
1965
String tmp(buff, sizeof(buff), charset), *res;
1966
res= orig_field->val_str(&tmp);
1967
pos= (char*) sql_strmake(res->ptr(), res->length());
1968
def= new Item_string(pos, res->length(), charset);
1970
orig_field->move_field_offset(-diff); // Back to record[0]
1975
1213
/*****************************************************************************
1976
1214
Warning handling
1977
1215
*****************************************************************************/
1980
Produce warning or note about data saved into field.
1982
@param level - level of message (Note/Warning/Error)
1983
@param code - error code of message to be produced
1984
@param cuted_increment - whenever we should increase cut fields count or not
1987
This function won't produce warning and increase cut fields counter
1988
if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
1990
if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
1991
This allows us to avoid notes in optimisation, like convert_constant_item().
1994
1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
2000
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
2001
int cuted_increment)
1217
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1218
drizzled::error_t code,
1219
int cuted_increment)
2004
1222
If this field was created only for type conversion purposes it
2020
Produce warning or note about datetime string data saved into field.
2022
@param level level of message (Note/Warning/Error)
2023
@param code error code of message to be produced
2024
@param str string value which we tried to save
2025
@param str_length length of string which we tried to save
2026
@param ts_type type of datetime value (datetime/date/time)
2027
@param cuted_increment whenever we should increase cut fields count or not
2030
This function will always produce some warning but won't increase cut
2031
fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
2036
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
2038
const char *str, uint32_t str_length,
2039
enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
1237
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1238
drizzled::error_t code,
1240
uint32_t str_length,
1241
type::timestamp_t ts_type,
1242
int cuted_increment)
2041
Session *session= table ? table->in_use : current_session;
2042
if ((session->really_abort_on_warning() &&
1244
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1246
if ((session->abortOnWarning() and
2043
1247
level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
2044
1248
set_warning(level, code, cuted_increment))
2045
make_truncated_value_warning(session, level, str, str_length, ts_type,
1249
make_truncated_value_warning(*session, level, str_ref(str, str_length), ts_type, field_name);
2051
Produce warning or note about integer datetime value saved into field.
2053
@param level level of message (Note/Warning/Error)
2054
@param code error code of message to be produced
2055
@param nr numeric value which we tried to save
2056
@param ts_type type of datetime value (datetime/date/time)
2057
@param cuted_increment whenever we should increase cut fields count or not
2060
This function will always produce some warning but won't increase cut
2061
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2066
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
2067
int64_t nr, enum enum_drizzle_timestamp_type ts_type,
2068
int cuted_increment)
1252
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1253
drizzled::error_t code,
1255
type::timestamp_t ts_type,
1256
int cuted_increment)
2070
Session *session= table ? table->in_use : current_session;
2071
if (session->really_abort_on_warning() ||
1258
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1260
if (session->abortOnWarning() or
2072
1261
set_warning(level, code, cuted_increment))
2075
char *str_end= int64_t10_to_str(nr, str_nr, -10);
2076
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
2077
ts_type, field_name);
1263
char str_nr[DECIMAL_LONGLONG_DIGITS];
1264
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1265
make_truncated_value_warning(*session, level, str_ref(str_nr, (uint32_t) (str_end - str_nr)), ts_type, field_name);
2083
Produce warning or note about double datetime data saved into field.
2085
@param level level of message (Note/Warning/Error)
2086
@param code error code of message to be produced
2087
@param nr double value which we tried to save
2088
@param ts_type type of datetime value (datetime/date/time)
2091
This function will always produce some warning but won't increase cut
2092
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2097
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, const uint32_t code,
2098
double nr, enum enum_drizzle_timestamp_type ts_type)
1269
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1270
const drizzled::error_t code,
1272
type::timestamp_t ts_type)
2100
Session *session= table ? table->in_use : current_session;
2101
if (session->really_abort_on_warning() ||
1274
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1276
if (session->abortOnWarning() or
2102
1277
set_warning(level, code, 1))
2104
1279
/* DBL_DIG is enough to print '-[digits].E+###' */
2105
1280
char str_nr[DBL_DIG + 8];
2106
uint32_t str_len= sprintf(str_nr, "%g", nr);
2107
make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1281
uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
1282
make_truncated_value_warning(*session, level, str_ref(str_nr, str_len), ts_type, field_name);
1286
bool Field::isReadSet() const
1288
return table->isReadSet(field_index);
1291
bool Field::isWriteSet()
1293
return table->isWriteSet(field_index);
1296
void Field::setReadSet(bool arg)
1299
table->setReadSet(field_index);
1301
table->clearReadSet(field_index);
1304
void Field::setWriteSet(bool arg)
1307
table->setWriteSet(field_index);
1309
table->clearWriteSet(field_index);
1312
void Field::pack_num(uint64_t arg, unsigned char *destination)
1314
if (not destination)
1317
int64_tstore(destination, arg);
1320
void Field::pack_num(uint32_t arg, unsigned char *destination)
1322
if (not destination)
1325
longstore(destination, arg);
1328
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1333
int64_tget(destination, arg);
1338
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1343
longget(destination, arg);
1348
std::ostream& operator<<(std::ostream& output, const Field &field)
1350
output << "Field:(";
1351
output << field.field_name;
1353
output << drizzled::display::type(field.real_type());
1356
if (field.flags & NOT_NULL_FLAG)
1357
output << " NOT_NULL";
1359
if (field.flags & PRI_KEY_FLAG)
1360
output << ", PRIMARY KEY";
1362
if (field.flags & UNIQUE_KEY_FLAG)
1363
output << ", UNIQUE KEY";
1365
if (field.flags & MULTIPLE_KEY_FLAG)
1366
output << ", MULTIPLE KEY";
1368
if (field.flags & BLOB_FLAG)
1371
if (field.flags & UNSIGNED_FLAG)
1372
output << ", UNSIGNED";
1374
if (field.flags & BINARY_FLAG)
1375
output << ", BINARY";
1379
return output; // for multiple << operators.
1382
} /* namespace drizzled */