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)
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
568
771
int Field::warn_if_overflow(int op_result)
570
773
if (op_result == E_DEC_OVERFLOW)
572
775
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
573
return E_DEC_OVERFLOW;
575
778
if (op_result == E_DEC_TRUNCATED)
577
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
578
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 */
583
787
void Field::init(Table *table_arg)
585
789
orig_table= table= table_arg;
586
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);
589
872
/// 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,
873
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
593
874
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)
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)
612
flags= null_ptr ? 0: NOT_NULL_FLAG;
885
flags=null_ptr ? 0: NOT_NULL_FLAG;
613
886
comment.str= (char*) "";
618
892
void Field::hash(uint32_t *nr, uint32_t *nr2)
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
888
1689
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
891
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1692
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1693
case DRIZZLE_TYPE_TINY : return 1;
892
1694
case DRIZZLE_TYPE_DATE: return 3;
893
1695
case DRIZZLE_TYPE_TIMESTAMP:
894
case DRIZZLE_TYPE_LONG: return 4;
1696
case DRIZZLE_TYPE_LONG : return 4;
895
1697
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
896
1698
case DRIZZLE_TYPE_DATETIME:
897
1699
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;
1700
case DRIZZLE_TYPE_NULL : return 0;
1701
case DRIZZLE_TYPE_BLOB: return 4+portable_sizeof_char_ptr;
900
1702
case DRIZZLE_TYPE_ENUM:
901
case DRIZZLE_TYPE_DECIMAL:
1703
case DRIZZLE_TYPE_NEWDECIMAL:
908
1711
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)
1755
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)
1757
if (!f_is_packed(pack_flag))
970
return new (root) Field_blob(ptr,
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,
975
calc_pack_length(DRIZZLE_TYPE_LONG, 0),
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);
981
return new (root) Field_enum(ptr,
986
get_enum_pack_length(interval->count),
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);
994
case DRIZZLE_TYPE_DECIMAL:
995
return new (root) Field_decimal(ptr,
1003
false /* is_unsigned */);
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);
1004
1796
case DRIZZLE_TYPE_DOUBLE:
1005
return new (root) Field_double(ptr,
1013
false /* is_unsigned */);
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:
1014
1804
case DRIZZLE_TYPE_LONG:
1015
return new (root) Field_long(ptr,
1022
false /* is_unsigned */);
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);
1023
1809
case DRIZZLE_TYPE_LONGLONG:
1024
return new (root) Field_int64_t(ptr,
1031
false /* is_unsigned */);
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);
1032
1814
case DRIZZLE_TYPE_TIMESTAMP:
1033
return new (root) Field_timestamp(ptr,
1815
return new (root) Field_timestamp(ptr,field_length, null_pos, null_bit,
1816
unireg_check, field_name, share,
1041
1818
case DRIZZLE_TYPE_DATE:
1042
return new (root) Field_date(ptr,
1819
return new (root) Field_date(ptr,null_pos,null_bit,
1820
unireg_check, field_name, field_charset);
1047
1821
case DRIZZLE_TYPE_DATETIME:
1048
return new (root) Field_datetime(ptr,
1822
return new (root) Field_datetime(ptr,null_pos,null_bit,
1823
unireg_check, field_name, field_charset);
1053
1824
case DRIZZLE_TYPE_NULL:
1054
return new (root) Field_null(ptr,
1058
default: // Impossible (Wrong version)
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]
1064
1907
/*****************************************************************************
1065
1908
Warning handling
1066
1909
*****************************************************************************/
1068
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1070
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)
1073
1936
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)
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)
1109
2002
Session *session= table ? table->in_use : current_session;
1110
2003
if (session->really_abort_on_warning() ||
1111
2004
set_warning(level, code, cuted_increment))
1113
2006
char str_nr[22];
1114
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
2007
char *str_end= int64_t10_to_str(nr, str_nr, -10);
1115
2008
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1116
2009
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)
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)
1125
2032
Session *session= table ? table->in_use : current_session;
1126
2033
if (session->really_abort_on_warning() ||