22
* @file This file implements the Field class and API
25
This file implements classes defined in field.h
27
#include <drizzled/server_includes.h>
28
#include "sql_select.h"
28
#include "drizzled/sql_select.h"
29
#include "drizzled/error.h"
30
#include "drizzled/field/str.h"
31
#include "drizzled/field/num.h"
32
#include "drizzled/field/blob.h"
33
#include "drizzled/field/enum.h"
34
#include "drizzled/field/null.h"
35
#include "drizzled/field/date.h"
36
#include "drizzled/field/decimal.h"
37
#include "drizzled/field/real.h"
38
#include "drizzled/field/double.h"
39
#include "drizzled/field/long.h"
40
#include "drizzled/field/int64_t.h"
41
#include "drizzled/field/num.h"
42
#include "drizzled/field/timestamp.h"
43
#include "drizzled/field/datetime.h"
44
#include "drizzled/field/varstring.h"
45
#include "drizzled/time_functions.h"
46
#include "drizzled/internal/m_string.h"
30
#include <drizzled/error.h>
31
#include <drizzled/virtual_column_info.h>
32
#include <drizzled/field/str.h>
33
#include <drizzled/field/longstr.h>
34
#include <drizzled/field/num.h>
35
#include <drizzled/field/blob.h>
36
#include <drizzled/field/enum.h>
37
#include <drizzled/field/null.h>
38
#include <drizzled/field/date.h>
39
#include <drizzled/field/decimal.h>
40
#include <drizzled/field/real.h>
41
#include <drizzled/field/double.h>
42
#include <drizzled/field/long.h>
43
#include <drizzled/field/int64_t.h>
44
#include <drizzled/field/num.h>
45
#include <drizzled/field/timestamp.h>
46
#include <drizzled/field/datetime.h>
47
#include <drizzled/field/varstring.h>
51
50
/*****************************************************************************
52
51
Instansiate templates and static variables
53
52
*****************************************************************************/
54
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
55
template class List<Create_field>;
56
template class List_iterator<Create_field>;
55
60
static enum_field_types
56
61
field_types_merge_rules [DRIZZLE_TYPE_MAX+1][DRIZZLE_TYPE_MAX+1]=
63
/* DRIZZLE_TYPE_TINY -> */
73
//DRIZZLE_TYPE_TIMESTAMP
75
//DRIZZLE_TYPE_LONGLONG
76
DRIZZLE_TYPE_LONGLONG,
77
//DRIZZLE_TYPE_DATETIME
81
//DRIZZLE_TYPE_VARCHAR
83
//DRIZZLE_TYPE_VIRTUAL
85
//DRIZZLE_TYPE_NEWDECIMAL
86
DRIZZLE_TYPE_NEWDECIMAL,
58
92
/* DRIZZLE_TYPE_LONG -> */
60
96
//DRIZZLE_TYPE_LONG
62
98
//DRIZZLE_TYPE_DOUBLE
390
521
return field_types_result_type[field_type];
393
525
bool Field::eq(Field *field)
395
527
return (ptr == field->ptr && null_ptr == field->null_ptr &&
396
528
null_bit == field->null_bit);
399
532
uint32_t Field::pack_length() const
401
534
return field_length;
404
538
uint32_t Field::pack_length_in_rec() const
406
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);
409
562
uint32_t Field::data_length()
411
564
return pack_length();
414
568
uint32_t Field::used_length()
416
570
return pack_length();
419
574
uint32_t Field::sort_length() const
421
576
return pack_length();
424
580
uint32_t Field::max_data_length() const
426
582
return pack_length();
429
586
int Field::reset(void)
431
588
memset(ptr, 0, pack_length());
435
593
void Field::reset_fields()
438
597
void Field::set_default()
440
ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->record[0]);
599
my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
441
600
memcpy(ptr, ptr + l_offset, pack_length());
443
602
*null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
445
if (this == table->next_number_field)
446
table->auto_increment_field_not_null= false;
449
606
bool Field::binary() const
454
612
bool Field::zero_pack() const
459
618
enum ha_base_keytype Field::key_type() const
461
620
return HA_KEYTYPE_BINARY;
464
624
uint32_t Field::key_length() const
466
626
return pack_length();
469
630
enum_field_types Field::real_type() const
474
636
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
476
638
return cmp(a, b);
479
642
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
481
644
return memcmp(a,b,pack_length());
484
648
int Field::cmp_offset(uint32_t row_offset)
486
650
return cmp(ptr,ptr+row_offset);
489
654
int Field::cmp_binary_offset(uint32_t row_offset)
491
656
return cmp_binary(ptr, ptr+row_offset);
494
660
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
496
662
return cmp(a, b);
499
666
int Field::key_cmp(const unsigned char *str, uint32_t)
501
668
return cmp(ptr,str);
504
672
uint32_t Field::decimals() const
509
bool Field::is_null(ptrdiff_t row_offset)
678
bool Field::is_null(my_ptrdiff_t row_offset)
511
680
return null_ptr ?
512
681
(null_ptr[row_offset] & null_bit ? true : false) :
516
bool Field::is_real_null(ptrdiff_t row_offset)
686
bool Field::is_real_null(my_ptrdiff_t row_offset)
518
688
return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
521
692
bool Field::is_null_in_record(const unsigned char *record)
525
return test(record[(uint32_t) (null_ptr -table->record[0])] & null_bit);
696
return test(record[(uint32_t) (null_ptr -table->record[0])] &
528
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
701
bool Field::is_null_in_record_with_offset(my_ptrdiff_t with_offset)
532
705
return test(null_ptr[with_offset] & null_bit);
535
void Field::set_null(ptrdiff_t row_offset)
709
void Field::set_null(my_ptrdiff_t row_offset)
538
712
null_ptr[row_offset]|= null_bit;
541
void Field::set_notnull(ptrdiff_t row_offset)
716
void Field::set_notnull(my_ptrdiff_t row_offset)
544
719
null_ptr[row_offset]&= (unsigned char) ~null_bit;
547
723
bool Field::maybe_null(void)
549
725
return null_ptr != 0 || table->maybe_null;
552
729
bool Field::real_maybe_null(void)
554
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
*****************************************************************************/
557
747
bool Field::type_can_have_key_part(enum enum_field_types type)
583
788
void Field::init(Table *table_arg)
585
790
orig_table= table= table_arg;
586
791
table_name= &table_arg->alias;
796
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
798
cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
800
while (length && my_isspace(cs,*str))
801
{ // Allow start space
806
if (*str == '+' || *str == '-')
809
if (!length || !(my_isdigit(cs,*str) || *str == '.'))
812
while (length && my_isdigit(cs,*str))
821
while (length && my_isdigit(cs,*str))
828
if (*str == 'E' || *str == 'e')
830
if (length < 3 || (str[1] != '+' && str[1] != '-') ||
831
!my_isdigit(cs,str[2]))
835
while (length && my_isdigit(cs,*str))
840
for (; length ; length--, str++)
842
if (!my_isspace(cs,*str))
851
Interpret field value as an integer but return the result as a string.
853
This is used for printing bit_fields as numbers while debugging.
856
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
858
const CHARSET_INFO * const cs= &my_charset_bin;
860
int64_t value= val_int();
862
if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
864
length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
865
MY_INT64_NUM_DECIMAL_DIGITS,
866
unsigned_val ? 10 : -10,
868
val_buffer->length(length);
589
873
/// This is used as a table name when the table structure is not set up
590
Field::Field(unsigned char *ptr_arg,
592
unsigned char *null_ptr_arg,
874
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
593
875
unsigned char null_bit_arg,
594
utype unireg_check_arg,
595
const char *field_name_arg)
598
null_ptr(null_ptr_arg),
602
field_name(field_name_arg),
605
part_of_key_not_clustered(0),
607
unireg_check(unireg_check_arg),
608
field_length(length_arg),
609
null_bit(null_bit_arg),
610
is_created_from_null_item(false)
876
utype unireg_check_arg, const char *field_name_arg)
877
:ptr(ptr_arg), null_ptr(null_ptr_arg),
878
table(0), orig_table(0), table_name(0),
879
field_name(field_name_arg),
880
key_start(0), part_of_key(0), part_of_key_not_clustered(0),
881
part_of_sortkey(0), unireg_check(unireg_check_arg),
882
field_length(length_arg), null_bit(null_bit_arg),
883
is_created_from_null_item(false),
884
vcol_info(NULL), is_stored(true)
612
flags= null_ptr ? 0: NOT_NULL_FLAG;
886
flags=null_ptr ? 0: NOT_NULL_FLAG;
613
887
comment.str= (char*) "";
618
893
void Field::hash(uint32_t *nr, uint32_t *nr2)
1324
/*****************************************************************************
1325
Handling of field and Create_field
1326
*****************************************************************************/
1329
Convert create_field::length from number of characters to number of bytes.
1332
void Create_field::create_length_to_internal_length(void)
1335
case DRIZZLE_TYPE_BLOB:
1336
case DRIZZLE_TYPE_VARCHAR:
1337
length*= charset->mbmaxlen;
1339
pack_length= calc_pack_length(sql_type, length);
1341
case DRIZZLE_TYPE_ENUM:
1342
/* Pack_length already calculated in ::init() */
1343
length*= charset->mbmaxlen;
1344
key_length= pack_length;
1346
case DRIZZLE_TYPE_NEWDECIMAL:
1347
key_length= pack_length=
1348
my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1355
key_length= pack_length= calc_pack_length(sql_type, length);
1362
Init for a tmp table field. To be extended if need be.
1364
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1365
uint32_t length_arg, uint32_t decimals_arg,
1366
bool maybe_null, bool is_unsigned)
1369
sql_type= sql_type_arg;
1370
char_length= length= length_arg;;
1371
unireg_check= Field::NONE;
1373
charset= &my_charset_bin;
1374
pack_flag= (FIELDFLAG_NUMBER |
1375
((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1376
(maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1377
(is_unsigned ? 0 : FIELDFLAG_DECIMAL));
1384
Initialize field definition for create.
1386
@param session Thread handle
1387
@param fld_name Field name
1388
@param fld_type Field type
1389
@param fld_length Field length
1390
@param fld_decimals Decimal (if any)
1391
@param fld_type_modifier Additional type information
1392
@param fld_default_value Field default value (if any)
1393
@param fld_on_update_value The value of ON UPDATE clause
1394
@param fld_comment Field comment
1395
@param fld_change Field change
1396
@param fld_interval_list Interval list (if any)
1397
@param fld_charset Field charset
1398
@param fld_vcol_info Virtual column data
1406
bool Create_field::init(Session *, char *fld_name, enum_field_types fld_type,
1407
char *fld_length, char *fld_decimals,
1408
uint32_t fld_type_modifier, Item *fld_default_value,
1409
Item *fld_on_update_value, LEX_STRING *fld_comment,
1410
char *fld_change, List<String> *fld_interval_list,
1411
const CHARSET_INFO * const fld_charset,
1412
uint32_t, enum column_format_type column_format_in,
1413
virtual_column_info *fld_vcol_info)
1415
uint32_t sign_len, allowed_type_modifier= 0;
1416
uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
1419
field_name= fld_name;
1420
def= fld_default_value;
1421
flags= fld_type_modifier;
1422
flags|= (((uint32_t)column_format_in & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1423
unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1424
Field::NEXT_NUMBER : Field::NONE);
1425
decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
1426
if (decimals >= NOT_FIXED_DEC)
1428
my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1437
pack_length= key_length= 0;
1438
charset= fld_charset;
1439
interval_list.empty();
1441
comment= *fld_comment;
1442
vcol_info= fld_vcol_info;
1445
/* Initialize data for a virtual field */
1446
if (fld_type == DRIZZLE_TYPE_VIRTUAL)
1448
assert(vcol_info && vcol_info->expr_item);
1449
is_stored= vcol_info->get_field_stored();
1451
Perform per item-type checks to determine if the expression is
1452
allowed for a virtual column.
1453
Note that validation of the specific function is done later in
1454
procedures open_table_from_share and fix_fields_vcol_func
1456
switch (vcol_info->expr_item->type()) {
1457
case Item::FUNC_ITEM:
1458
if (((Item_func *)vcol_info->expr_item)->functype() == Item_func::FUNC_SP)
1460
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1464
case Item::COPY_STR_ITEM:
1465
case Item::FIELD_AVG_ITEM:
1466
case Item::PROC_ITEM:
1467
case Item::REF_ITEM:
1468
case Item::FIELD_STD_ITEM:
1469
case Item::FIELD_VARIANCE_ITEM:
1470
case Item::INSERT_VALUE_ITEM:
1471
case Item::SUBSELECT_ITEM:
1472
case Item::CACHE_ITEM:
1473
case Item::TYPE_HOLDER:
1474
case Item::PARAM_ITEM:
1475
my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1478
// Continue with the field creation
1482
Make a field created for the real type.
1483
Note that "real" and virtual fields differ from each other
1484
only by Field::vcol_info, which is always 0 for normal columns.
1485
vcol_info is updated for fields later in procedure open_binary_frm.
1487
sql_type= fld_type= vcol_info->get_real_type();
1491
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1492
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1494
if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1495
(fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1496
flags|= NO_DEFAULT_VALUE_FLAG;
1498
if (fld_length && !(length= (uint32_t) atoi(fld_length)))
1499
fld_length= 0; /* purecov: inspected */
1500
sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1503
case DRIZZLE_TYPE_TINY:
1505
length= MAX_TINYINT_WIDTH+sign_len;
1506
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1508
case DRIZZLE_TYPE_LONG:
1510
length= MAX_INT_WIDTH+sign_len;
1511
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1513
case DRIZZLE_TYPE_LONGLONG:
1515
length= MAX_BIGINT_WIDTH;
1516
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1518
case DRIZZLE_TYPE_NULL:
1520
case DRIZZLE_TYPE_NEWDECIMAL:
1521
my_decimal_trim(&length, &decimals);
1522
if (length > DECIMAL_MAX_PRECISION)
1524
my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1525
DECIMAL_MAX_PRECISION);
1528
if (length < decimals)
1530
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1534
my_decimal_precision_to_length(length, decimals,
1535
fld_type_modifier & UNSIGNED_FLAG);
1537
my_decimal_get_binary_size(length, decimals);
1539
case DRIZZLE_TYPE_VARCHAR:
1541
Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1542
if they don't have a default value
1544
max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1546
case DRIZZLE_TYPE_BLOB:
1547
if (fld_default_value)
1549
/* Allow empty as default value. */
1551
res= fld_default_value->val_str(&str);
1554
my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
1562
case DRIZZLE_TYPE_DOUBLE:
1563
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1564
if (!fld_length && !fld_decimals)
1567
decimals= NOT_FIXED_DEC;
1569
if (length < decimals &&
1570
decimals != NOT_FIXED_DEC)
1572
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1576
case DRIZZLE_TYPE_TIMESTAMP:
1579
/* Compressed date YYYYMMDDHHMMSS */
1580
length= MAX_DATETIME_COMPRESSED_WIDTH;
1582
else if (length != MAX_DATETIME_WIDTH)
1585
We support only even TIMESTAMP lengths less or equal than 14
1586
and 19 as length of 4.1 compatible representation.
1588
length= ((length+1)/2)*2; /* purecov: inspected */
1589
length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1591
flags|= UNSIGNED_FLAG;
1592
if (fld_default_value)
1594
/* Grammar allows only NOW() value for ON UPDATE clause */
1595
if (fld_default_value->type() == Item::FUNC_ITEM &&
1596
((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
1598
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
1599
Field::TIMESTAMP_DN_FIELD);
1601
We don't need default value any longer moreover it is dangerous.
1602
Everything handled by unireg_check further.
1607
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
1613
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
1614
or ON UPDATE values then for the sake of compatiblity we should treat
1615
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
1616
have another TIMESTAMP column with auto-set option before this one)
1617
or DEFAULT 0 (in other cases).
1618
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
1619
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
1620
information about all TIMESTAMP fields in table will be availiable.
1622
If we have TIMESTAMP NULL column without explicit DEFAULT value
1623
we treat it as having DEFAULT NULL attribute.
1625
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
1626
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
1630
case DRIZZLE_TYPE_DATE:
1633
case DRIZZLE_TYPE_DATETIME:
1634
length= MAX_DATETIME_WIDTH;
1636
case DRIZZLE_TYPE_ENUM:
1638
/* Should be safe. */
1639
pack_length= get_enum_pack_length(fld_interval_list->elements);
1641
List_iterator<String> it(*fld_interval_list);
1644
interval_list.push_back(tmp);
1645
length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1648
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1651
/* Remember the value of length */
1652
char_length= length;
1654
if (!(flags & BLOB_FLAG) &&
1655
((length > max_field_charlength &&
1656
fld_type != DRIZZLE_TYPE_ENUM &&
1657
(fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1658
(!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1660
my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ? ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
1662
fld_name, max_field_charlength); /* purecov: inspected */
1665
fld_type_modifier&= AUTO_INCREMENT_FLAG;
1666
if ((~allowed_type_modifier) & fld_type_modifier)
1668
my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1672
return(false); /* success */
1676
enum_field_types get_blob_type_from_length(uint32_t)
1678
enum_field_types type;
1680
type= DRIZZLE_TYPE_BLOB;
1687
Make a field from the .frm file info
888
1690
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
891
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1693
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1694
case DRIZZLE_TYPE_TINY : return 1;
892
1695
case DRIZZLE_TYPE_DATE: return 3;
893
1696
case DRIZZLE_TYPE_TIMESTAMP:
894
case DRIZZLE_TYPE_LONG: return 4;
1697
case DRIZZLE_TYPE_LONG : return 4;
895
1698
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
896
1699
case DRIZZLE_TYPE_DATETIME:
897
1700
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
898
case DRIZZLE_TYPE_NULL: return 0;
899
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1701
case DRIZZLE_TYPE_NULL : return 0;
1702
case DRIZZLE_TYPE_BLOB: return 4+portable_sizeof_char_ptr;
900
1703
case DRIZZLE_TYPE_ENUM:
901
case DRIZZLE_TYPE_DECIMAL:
1704
case DRIZZLE_TYPE_NEWDECIMAL:
908
1712
uint32_t pack_length_to_packflag(uint32_t type)
956
if (field_type == DRIZZLE_TYPE_VARCHAR ||
957
field_type == DRIZZLE_TYPE_BLOB ||
958
field_type == DRIZZLE_TYPE_ENUM)
1756
if (f_is_alpha(pack_flag))
960
if (field_type == DRIZZLE_TYPE_VARCHAR)
961
return new (root) Field_varstring(ptr,field_length,
962
HA_VARCHAR_PACKLENGTH(field_length),
968
if (field_type == DRIZZLE_TYPE_BLOB)
1758
if (!f_is_packed(pack_flag))
970
return new (root) Field_blob(ptr,
1760
if (field_type == DRIZZLE_TYPE_VARCHAR)
1761
return new (root) Field_varstring(ptr,field_length,
1762
HA_VARCHAR_PACKLENGTH(field_length),
1764
unireg_check, field_name,
975
calc_pack_length(DRIZZLE_TYPE_LONG, 0),
1770
uint32_t pack_length=calc_pack_length((enum_field_types)
1771
f_packtype(pack_flag),
1774
if (f_is_blob(pack_flag))
1775
return new (root) Field_blob(ptr,null_pos,null_bit,
1776
unireg_check, field_name, share,
1777
pack_length, field_charset);
981
return new (root) Field_enum(ptr,
986
get_enum_pack_length(interval->count),
1780
if (f_is_enum(pack_flag))
1782
return new (root) Field_enum(ptr,field_length,null_pos,null_bit,
1783
unireg_check, field_name,
1784
get_enum_pack_length(interval->count),
1785
interval, field_charset);
994
case DRIZZLE_TYPE_DECIMAL:
995
return new (root) Field_decimal(ptr,
1003
false /* is_unsigned */);
1790
switch (field_type) {
1791
case DRIZZLE_TYPE_NEWDECIMAL:
1792
return new (root) Field_new_decimal(ptr,field_length,null_pos,null_bit,
1793
unireg_check, field_name,
1794
f_decimals(pack_flag),
1795
f_is_decimal_precision(pack_flag) != 0,
1796
f_is_dec(pack_flag) == 0);
1004
1797
case DRIZZLE_TYPE_DOUBLE:
1005
return new (root) Field_double(ptr,
1013
false /* is_unsigned */);
1798
return new (root) Field_double(ptr,field_length,null_pos,null_bit,
1799
unireg_check, field_name,
1800
f_decimals(pack_flag),
1802
f_is_dec(pack_flag)== 0);
1803
case DRIZZLE_TYPE_TINY:
1014
1805
case DRIZZLE_TYPE_LONG:
1015
return new (root) Field_long(ptr,
1022
false /* is_unsigned */);
1806
return new (root) Field_long(ptr,field_length,null_pos,null_bit,
1807
unireg_check, field_name,
1809
f_is_dec(pack_flag) == 0);
1023
1810
case DRIZZLE_TYPE_LONGLONG:
1024
return new (root) Field_int64_t(ptr,
1031
false /* is_unsigned */);
1811
return new (root) Field_int64_t(ptr,field_length,null_pos,null_bit,
1812
unireg_check, field_name,
1814
f_is_dec(pack_flag) == 0);
1032
1815
case DRIZZLE_TYPE_TIMESTAMP:
1033
return new (root) Field_timestamp(ptr,
1816
return new (root) Field_timestamp(ptr,field_length, null_pos, null_bit,
1817
unireg_check, field_name, share,
1041
1819
case DRIZZLE_TYPE_DATE:
1042
return new (root) Field_date(ptr,
1820
return new (root) Field_date(ptr,null_pos,null_bit,
1821
unireg_check, field_name, field_charset);
1047
1822
case DRIZZLE_TYPE_DATETIME:
1048
return new (root) Field_datetime(ptr,
1823
return new (root) Field_datetime(ptr,null_pos,null_bit,
1824
unireg_check, field_name, field_charset);
1053
1825
case DRIZZLE_TYPE_NULL:
1054
return new (root) Field_null(ptr,
1058
default: // Impossible (Wrong version)
1826
return new (root) Field_null(ptr, field_length, unireg_check, field_name,
1828
case DRIZZLE_TYPE_VIRTUAL: // Must not happen
1830
default: // Impossible (Wrong version)
1837
/** Create a field suitable for create of table. */
1839
Create_field::Create_field(Field *old_field,Field *orig_field)
1842
field_name=change=old_field->field_name;
1843
length= old_field->field_length;
1844
flags= old_field->flags;
1845
unireg_check=old_field->unireg_check;
1846
pack_length=old_field->pack_length();
1847
key_length= old_field->key_length();
1848
sql_type= old_field->real_type();
1849
charset= old_field->charset(); // May be NULL ptr
1850
comment= old_field->comment;
1851
decimals= old_field->decimals();
1852
vcol_info= old_field->vcol_info;
1853
is_stored= old_field->is_stored;
1855
/* Fix if the original table had 4 byte pointer blobs */
1856
if (flags & BLOB_FLAG)
1857
pack_length= (pack_length- old_field->table->s->blob_ptr_size +
1858
portable_sizeof_char_ptr);
1861
case DRIZZLE_TYPE_BLOB:
1862
sql_type= DRIZZLE_TYPE_BLOB;
1863
length/= charset->mbmaxlen;
1864
key_length/= charset->mbmaxlen;
1866
/* Change CHAR -> VARCHAR if dynamic record length */
1867
case DRIZZLE_TYPE_ENUM:
1868
case DRIZZLE_TYPE_VARCHAR:
1869
/* This is corrected in create_length_to_internal_length */
1870
length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1876
if (flags & (ENUM_FLAG | SET_FLAG))
1877
interval= ((Field_enum*) old_field)->typelib;
1881
char_length= length;
1883
if (!(flags & (NO_DEFAULT_VALUE_FLAG )) &&
1884
old_field->ptr && orig_field &&
1885
(sql_type != DRIZZLE_TYPE_TIMESTAMP || /* set def only if */
1886
old_field->table->timestamp_field != old_field || /* timestamp field */
1887
unireg_check == Field::TIMESTAMP_UN_FIELD)) /* has default val */
1891
/* Get the value from default_values */
1892
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
1893
orig_field->table->record[0]);
1894
orig_field->move_field_offset(diff); // Points now at default_values
1895
if (!orig_field->is_real_null())
1897
char buff[MAX_FIELD_WIDTH], *pos;
1898
String tmp(buff, sizeof(buff), charset), *res;
1899
res= orig_field->val_str(&tmp);
1900
pos= (char*) sql_strmake(res->ptr(), res->length());
1901
def= new Item_string(pos, res->length(), charset);
1903
orig_field->move_field_offset(-diff); // Back to record[0]
1064
1908
/*****************************************************************************
1065
1909
Warning handling
1066
1910
*****************************************************************************/
1068
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1070
int cuted_increment)
1913
Produce warning or note about data saved into field.
1915
@param level - level of message (Note/Warning/Error)
1916
@param code - error code of message to be produced
1917
@param cuted_increment - whenever we should increase cut fields count or not
1920
This function won't produce warning and increase cut fields counter
1921
if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
1923
if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
1924
This allows us to avoid notes in optimisation, like convert_constant_item().
1927
1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
1933
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1934
int cuted_increment)
1073
1937
If this field was created only for type conversion purposes it
1103
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1106
enum enum_drizzle_timestamp_type ts_type,
1107
int cuted_increment)
1984
Produce warning or note about integer datetime value saved into field.
1986
@param level level of message (Note/Warning/Error)
1987
@param code error code of message to be produced
1988
@param nr numeric value which we tried to save
1989
@param ts_type type of datetime value (datetime/date/time)
1990
@param cuted_increment whenever we should increase cut fields count or not
1993
This function will always produce some warning but won't increase cut
1994
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
1999
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
2000
int64_t nr, enum enum_drizzle_timestamp_type ts_type,
2001
int cuted_increment)
1109
2003
Session *session= table ? table->in_use : current_session;
1110
2004
if (session->really_abort_on_warning() ||
1111
2005
set_warning(level, code, cuted_increment))
1113
2007
char str_nr[22];
1114
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
2008
char *str_end= int64_t10_to_str(nr, str_nr, -10);
1115
2009
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1116
2010
ts_type, field_name);
1120
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1121
const uint32_t code,
1123
enum enum_drizzle_timestamp_type ts_type)
2016
Produce warning or note about double datetime data saved into field.
2018
@param level level of message (Note/Warning/Error)
2019
@param code error code of message to be produced
2020
@param nr double value which we tried to save
2021
@param ts_type type of datetime value (datetime/date/time)
2024
This function will always produce some warning but won't increase cut
2025
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
2030
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, const uint32_t code,
2031
double nr, enum enum_drizzle_timestamp_type ts_type)
1125
2033
Session *session= table ? table->in_use : current_session;
1126
2034
if (session->really_abort_on_warning() ||