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"
25
#include "drizzled/server_includes.h"
30
#include <drizzled/error.h>
31
#include <drizzled/field/str.h>
32
#include <drizzled/field/num.h>
33
#include <drizzled/field/blob.h>
34
#include <drizzled/field/enum.h>
35
#include <drizzled/field/null.h>
36
#include <drizzled/field/date.h>
37
#include <drizzled/field/decimal.h>
38
#include <drizzled/field/real.h>
39
#include <drizzled/field/double.h>
40
#include <drizzled/field/long.h>
41
#include <drizzled/field/int64_t.h>
42
#include <drizzled/field/num.h>
43
#include <drizzled/field/timestamp.h>
44
#include <drizzled/field/datetime.h>
45
#include <drizzled/field/varstring.h>
27
#include "drizzled/sql_select.h"
28
#include "drizzled/error.h"
29
#include "drizzled/field/str.h"
30
#include "drizzled/field/num.h"
31
#include "drizzled/field/blob.h"
32
#include "drizzled/field/enum.h"
33
#include "drizzled/field/null.h"
34
#include "drizzled/field/date.h"
35
#include "drizzled/field/decimal.h"
36
#include "drizzled/field/real.h"
37
#include "drizzled/field/double.h"
38
#include "drizzled/field/long.h"
39
#include "drizzled/field/int64_t.h"
40
#include "drizzled/field/num.h"
41
#include "drizzled/field/timestamp.h"
42
#include "drizzled/field/datetime.h"
43
#include "drizzled/field/varstring.h"
48
45
/*****************************************************************************
49
46
Instansiate templates and static variables
463
430
return field_types_result_type[field_type];
467
433
bool Field::eq(Field *field)
469
435
return (ptr == field->ptr && null_ptr == field->null_ptr &&
470
436
null_bit == field->null_bit);
474
439
uint32_t Field::pack_length() const
476
441
return field_length;
480
444
uint32_t Field::pack_length_in_rec() const
482
446
return pack_length();
486
449
uint32_t Field::pack_length_from_metadata(uint32_t field_metadata)
488
451
return field_metadata;
492
454
uint32_t Field::row_pack_length()
498
459
int Field::save_field_metadata(unsigned char *first_byte)
500
461
return do_save_field_metadata(first_byte);
504
464
uint32_t Field::data_length()
506
466
return pack_length();
510
469
uint32_t Field::used_length()
512
471
return pack_length();
516
474
uint32_t Field::sort_length() const
518
476
return pack_length();
522
479
uint32_t Field::max_data_length() const
524
481
return pack_length();
528
484
int Field::reset(void)
530
486
memset(ptr, 0, pack_length());
535
490
void Field::reset_fields()
539
493
void Field::set_default()
541
my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
495
ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->record[0]);
542
496
memcpy(ptr, ptr + l_offset, pack_length());
544
498
*null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
546
if(this == table->next_number_field)
547
table->auto_increment_field_not_null= 0;
500
if (this == table->next_number_field)
501
table->auto_increment_field_not_null= false;
551
504
bool Field::binary() const
557
509
bool Field::zero_pack() const
563
514
enum ha_base_keytype Field::key_type() const
565
516
return HA_KEYTYPE_BINARY;
569
519
uint32_t Field::key_length() const
571
521
return pack_length();
575
524
enum_field_types Field::real_type() const
581
529
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
583
531
return cmp(a, b);
587
534
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
589
536
return memcmp(a,b,pack_length());
593
539
int Field::cmp_offset(uint32_t row_offset)
595
541
return cmp(ptr,ptr+row_offset);
599
544
int Field::cmp_binary_offset(uint32_t row_offset)
601
546
return cmp_binary(ptr, ptr+row_offset);
605
549
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
607
551
return cmp(a, b);
611
554
int Field::key_cmp(const unsigned char *str, uint32_t)
613
556
return cmp(ptr,str);
617
559
uint32_t Field::decimals() const
623
564
bool Field::is_null(my_ptrdiff_t row_offset)
625
566
return null_ptr ?
814
657
return val_buffer;
818
660
/// This is used as a table name when the table structure is not set up
819
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
661
Field::Field(unsigned char *ptr_arg,
663
unsigned char *null_ptr_arg,
820
664
unsigned char null_bit_arg,
821
utype unireg_check_arg, const char *field_name_arg)
822
:ptr(ptr_arg), null_ptr(null_ptr_arg),
823
table(0), orig_table(0), table_name(0),
824
field_name(field_name_arg),
825
key_start(0), part_of_key(0), part_of_key_not_clustered(0),
826
part_of_sortkey(0), unireg_check(unireg_check_arg),
827
field_length(length_arg), null_bit(null_bit_arg),
828
is_created_from_null_item(false)
665
utype unireg_check_arg,
666
const char *field_name_arg)
669
null_ptr(null_ptr_arg),
673
field_name(field_name_arg),
676
part_of_key_not_clustered(0),
678
unireg_check(unireg_check_arg),
679
field_length(length_arg),
680
null_bit(null_bit_arg),
681
is_created_from_null_item(false)
830
flags=null_ptr ? 0: NOT_NULL_FLAG;
683
flags= null_ptr ? 0: NOT_NULL_FLAG;
831
684
comment.str= (char*) "";
837
689
void Field::hash(uint32_t *nr, uint32_t *nr2)
908
Pack the field into a format suitable for storage and transfer.
910
To implement packing functionality, only the virtual function
911
should be overridden. The other functions are just convenience
912
functions and hence should not be overridden.
914
The value of <code>low_byte_first</code> is dependent on how the
915
packed data is going to be used: for local use, e.g., temporary
916
store on disk or in memory, use the native format since that is
917
faster. For data that is going to be transfered to other machines
918
(e.g., when writing data to the binary log), data should always be
919
stored in little-endian format.
921
@note The default method for packing fields just copy the raw bytes
922
of the record into the destination, but never more than
923
<code>max_length</code> characters.
926
Pointer to memory area where representation of field should be put.
929
Pointer to memory area where record representation of field is
933
Maximum length of the field, as given in the column definition. For
934
example, for <code>CHAR(1000)</code>, the <code>max_length</code>
935
is 1000. This information is sometimes needed to decide how to pack
938
@param low_byte_first
939
@c true if integers should be stored little-endian, @c false if
940
native format should be used. Note that for little-endian machines,
941
the value of this flag is a moot point since the native format is
945
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
735
unsigned char *Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length, bool)
948
737
uint32_t length= pack_length();
949
738
set_if_smaller(length, max_length);
951
740
return to+length;
955
743
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
957
unsigned char *result= this->pack(to, from, UINT32_MAX,
958
table->s->db_low_byte_first);
745
unsigned char *result= this->pack(to, from, UINT32_MAX, table->s->db_low_byte_first);
964
Unpack a field from row data.
966
This method is used to unpack a field from a master whose size of
967
the field is less than that of the slave.
969
The <code>param_data</code> parameter is a two-byte integer (stored
970
in the least significant 16 bits of the unsigned integer) usually
971
consisting of two parts: the real type in the most significant byte
972
and a original pack length in the least significant byte.
974
The exact layout of the <code>param_data</code> field is given by
975
the <code>Table_map_log_event::save_field_metadata()</code>.
977
This is the default method for unpacking a field. It just copies
978
the memory block in byte order (of original pack length bytes or
979
length of field, whichever is smaller).
981
@param to Destination of the data
982
@param from Source of the data
983
@param param_data Real type and original pack length of the field
986
@param low_byte_first
987
If this flag is @c true, all composite entities (e.g., lengths)
988
should be unpacked in little-endian format; otherwise, the entities
989
are unpacked in native order.
991
@return New pointer into memory based on from + length of the data
993
const unsigned char *
994
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
749
const unsigned char *Field::unpack(unsigned char* to,
750
const unsigned char *from,
997
754
uint32_t length=pack_length();
998
755
int from_type= 0;
1268
/*****************************************************************************
1269
Handling of field and Create_field
1270
*****************************************************************************/
1273
Convert create_field::length from number of characters to number of bytes.
1276
void Create_field::create_length_to_internal_length(void)
1279
case DRIZZLE_TYPE_BLOB:
1280
case DRIZZLE_TYPE_VARCHAR:
1281
length*= charset->mbmaxlen;
1283
pack_length= calc_pack_length(sql_type, length);
1285
case DRIZZLE_TYPE_ENUM:
1286
/* Pack_length already calculated in ::init() */
1287
length*= charset->mbmaxlen;
1288
key_length= pack_length;
1290
case DRIZZLE_TYPE_NEWDECIMAL:
1291
key_length= pack_length=
1292
my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1299
key_length= pack_length= calc_pack_length(sql_type, length);
1306
Init for a tmp table field. To be extended if need be.
1308
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1309
uint32_t length_arg, uint32_t decimals_arg,
1310
bool maybe_null, bool is_unsigned)
1313
sql_type= sql_type_arg;
1314
char_length= length= length_arg;;
1315
unireg_check= Field::NONE;
1317
charset= &my_charset_bin;
1318
pack_flag= (FIELDFLAG_NUMBER |
1319
((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1320
(maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1321
(is_unsigned ? 0 : FIELDFLAG_DECIMAL));
1326
Initialize field definition for create.
1328
@param session Thread handle
1329
@param fld_name Field name
1330
@param fld_type Field type
1331
@param fld_length Field length
1332
@param fld_decimals Decimal (if any)
1333
@param fld_type_modifier Additional type information
1334
@param fld_default_value Field default value (if any)
1335
@param fld_on_update_value The value of ON UPDATE clause
1336
@param fld_comment Field comment
1337
@param fld_change Field change
1338
@param fld_interval_list Interval list (if any)
1339
@param fld_charset Field charset
1347
bool Create_field::init(Session *, char *fld_name, enum_field_types fld_type,
1348
char *fld_length, char *fld_decimals,
1349
uint32_t fld_type_modifier, Item *fld_default_value,
1350
Item *fld_on_update_value, LEX_STRING *fld_comment,
1351
char *fld_change, List<String> *fld_interval_list,
1352
const CHARSET_INFO * const fld_charset,
1353
uint32_t, enum column_format_type column_format_in)
1356
uint32_t sign_len, allowed_type_modifier= 0;
1357
uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
1360
field_name= fld_name;
1361
def= fld_default_value;
1362
flags= fld_type_modifier;
1363
flags|= (((uint32_t)column_format_in & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1364
unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1365
Field::NEXT_NUMBER : Field::NONE);
1366
decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
1367
if (decimals >= NOT_FIXED_DEC)
1369
my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1378
pack_length= key_length= 0;
1379
charset= fld_charset;
1380
interval_list.empty();
1382
comment= *fld_comment;
1385
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1386
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1388
if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1389
(fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1390
flags|= NO_DEFAULT_VALUE_FLAG;
1392
if (fld_length && !(length= (uint32_t) atoi(fld_length)))
1393
fld_length= 0; /* purecov: inspected */
1394
sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1397
case DRIZZLE_TYPE_TINY:
1399
length= MAX_TINYINT_WIDTH+sign_len;
1400
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1402
case DRIZZLE_TYPE_LONG:
1404
length= MAX_INT_WIDTH+sign_len;
1405
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1407
case DRIZZLE_TYPE_LONGLONG:
1409
length= MAX_BIGINT_WIDTH;
1410
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1412
case DRIZZLE_TYPE_NULL:
1414
case DRIZZLE_TYPE_NEWDECIMAL:
1415
my_decimal_trim(&length, &decimals);
1416
if (length > DECIMAL_MAX_PRECISION)
1418
my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1419
DECIMAL_MAX_PRECISION);
1422
if (length < decimals)
1424
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1428
my_decimal_precision_to_length(length, decimals,
1429
fld_type_modifier & UNSIGNED_FLAG);
1431
my_decimal_get_binary_size(length, decimals);
1433
case DRIZZLE_TYPE_VARCHAR:
1435
Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1436
if they don't have a default value
1438
max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1440
case DRIZZLE_TYPE_BLOB:
1441
if (fld_default_value)
1443
/* Allow empty as default value. */
1445
res= fld_default_value->val_str(&str);
1448
my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
1456
case DRIZZLE_TYPE_DOUBLE:
1457
allowed_type_modifier= AUTO_INCREMENT_FLAG;
1458
if (!fld_length && !fld_decimals)
1461
decimals= NOT_FIXED_DEC;
1463
if (length < decimals &&
1464
decimals != NOT_FIXED_DEC)
1466
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1470
case DRIZZLE_TYPE_TIMESTAMP:
1473
/* Compressed date YYYYMMDDHHMMSS */
1474
length= MAX_DATETIME_COMPRESSED_WIDTH;
1476
else if (length != MAX_DATETIME_WIDTH)
1479
We support only even TIMESTAMP lengths less or equal than 14
1480
and 19 as length of 4.1 compatible representation.
1482
length= ((length+1)/2)*2; /* purecov: inspected */
1483
length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1485
flags|= UNSIGNED_FLAG;
1486
if (fld_default_value)
1488
/* Grammar allows only NOW() value for ON UPDATE clause */
1489
if (fld_default_value->type() == Item::FUNC_ITEM &&
1490
((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
1492
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
1493
Field::TIMESTAMP_DN_FIELD);
1495
We don't need default value any longer moreover it is dangerous.
1496
Everything handled by unireg_check further.
1501
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
1507
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
1508
or ON UPDATE values then for the sake of compatiblity we should treat
1509
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
1510
have another TIMESTAMP column with auto-set option before this one)
1511
or DEFAULT 0 (in other cases).
1512
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
1513
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
1514
information about all TIMESTAMP fields in table will be availiable.
1516
If we have TIMESTAMP NULL column without explicit DEFAULT value
1517
we treat it as having DEFAULT NULL attribute.
1519
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
1520
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
1524
case DRIZZLE_TYPE_DATE:
1527
case DRIZZLE_TYPE_DATETIME:
1528
length= MAX_DATETIME_WIDTH;
1530
case DRIZZLE_TYPE_ENUM:
1532
/* Should be safe. */
1533
pack_length= get_enum_pack_length(fld_interval_list->elements);
1535
List_iterator<String> it(*fld_interval_list);
1538
interval_list.push_back(tmp);
1539
length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1543
/* Remember the value of length */
1544
char_length= length;
1546
if (!(flags & BLOB_FLAG) &&
1547
((length > max_field_charlength &&
1548
fld_type != DRIZZLE_TYPE_ENUM &&
1549
(fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1550
(!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1552
my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ? ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
1554
fld_name, max_field_charlength); /* purecov: inspected */
1557
fld_type_modifier&= AUTO_INCREMENT_FLAG;
1558
if ((~allowed_type_modifier) & fld_type_modifier)
1560
my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1564
return(false); /* success */
1568
enum_field_types get_blob_type_from_length(uint32_t)
1570
enum_field_types type;
1572
type= DRIZZLE_TYPE_BLOB;
1579
981
Make a field from the .frm file info
1582
983
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1585
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1586
case DRIZZLE_TYPE_TINY : return 1;
986
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
987
case DRIZZLE_TYPE_TINY: return 1;
1587
988
case DRIZZLE_TYPE_DATE: return 3;
1588
989
case DRIZZLE_TYPE_TIMESTAMP:
1589
case DRIZZLE_TYPE_LONG : return 4;
990
case DRIZZLE_TYPE_LONG: return 4;
1590
991
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1591
992
case DRIZZLE_TYPE_DATETIME:
1592
993
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1593
case DRIZZLE_TYPE_NULL : return 0;
1594
case DRIZZLE_TYPE_BLOB: return 4+portable_sizeof_char_ptr;
994
case DRIZZLE_TYPE_NULL: return 0;
995
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1595
996
case DRIZZLE_TYPE_ENUM:
1596
997
case DRIZZLE_TYPE_NEWDECIMAL:
1727
/** Create a field suitable for create of table. */
1729
Create_field::Create_field(Field *old_field,Field *orig_field)
1732
field_name=change=old_field->field_name;
1733
length= old_field->field_length;
1734
flags= old_field->flags;
1735
unireg_check=old_field->unireg_check;
1736
pack_length=old_field->pack_length();
1737
key_length= old_field->key_length();
1738
sql_type= old_field->real_type();
1739
charset= old_field->charset(); // May be NULL ptr
1740
comment= old_field->comment;
1741
decimals= old_field->decimals();
1743
/* Fix if the original table had 4 byte pointer blobs */
1744
if (flags & BLOB_FLAG)
1745
pack_length= (pack_length- old_field->table->s->blob_ptr_size +
1746
portable_sizeof_char_ptr);
1749
case DRIZZLE_TYPE_BLOB:
1750
sql_type= DRIZZLE_TYPE_BLOB;
1751
length/= charset->mbmaxlen;
1752
key_length/= charset->mbmaxlen;
1754
/* Change CHAR -> VARCHAR if dynamic record length */
1755
case DRIZZLE_TYPE_ENUM:
1756
case DRIZZLE_TYPE_VARCHAR:
1757
/* This is corrected in create_length_to_internal_length */
1758
length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1764
if (flags & (ENUM_FLAG | SET_FLAG))
1765
interval= ((Field_enum*) old_field)->typelib;
1769
char_length= length;
1771
if (!(flags & (NO_DEFAULT_VALUE_FLAG )) &&
1772
old_field->ptr && orig_field &&
1773
(sql_type != DRIZZLE_TYPE_TIMESTAMP || /* set def only if */
1774
old_field->table->timestamp_field != old_field || /* timestamp field */
1775
unireg_check == Field::TIMESTAMP_UN_FIELD)) /* has default val */
1779
/* Get the value from default_values */
1780
diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
1781
orig_field->table->record[0]);
1782
orig_field->move_field_offset(diff); // Points now at default_values
1783
if (!orig_field->is_real_null())
1785
char buff[MAX_FIELD_WIDTH], *pos;
1786
String tmp(buff, sizeof(buff), charset), *res;
1787
res= orig_field->val_str(&tmp);
1788
pos= (char*) sql_strmake(res->ptr(), res->length());
1789
def= new Item_string(pos, res->length(), charset);
1791
orig_field->move_field_offset(-diff); // Back to record[0]
1796
1128
/*****************************************************************************
1797
1129
Warning handling
1798
1130
*****************************************************************************/
1801
Produce warning or note about data saved into field.
1803
@param level - level of message (Note/Warning/Error)
1804
@param code - error code of message to be produced
1805
@param cuted_increment - whenever we should increase cut fields count or not
1808
This function won't produce warning and increase cut fields counter
1809
if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
1811
if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
1812
This allows us to avoid notes in optimisation, like convert_constant_item().
1815
1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
1821
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1822
int cuted_increment)
1132
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1134
int cuted_increment)
1825
1137
If this field was created only for type conversion purposes it