MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
field.h
1 #ifndef FIELD_INCLUDED
2 #define FIELD_INCLUDED
3 
4 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
19 #include "mysqld.h" /* system_charset_info */
20 #include "table.h" /* TABLE */
21 #include "sql_string.h" /* String */
22 #include "my_decimal.h" /* my_decimal */
23 #include "sql_error.h" /* Sql_condition */
24 #include "mysql_version.h" /* FRM_VER */
25 
26 /*
27 
28 Field class hierarchy
29 
30 
31 Field (abstract)
32 |
33 +--Field_bit
34 | +--Field_bit_as_char
35 |
36 +--Field_num (abstract)
37 | | +--Field_real (asbstract)
38 | | +--Field_decimal
39 | | +--Field_float
40 | | +--Field_double
41 | |
42 | +--Field_new_decimal
43 | +--Field_short
44 | +--Field_medium
45 | +--Field_long
46 | +--Field_longlong
47 | +--Field_tiny
48 | +--Field_year
49 |
50 +--Field_str (abstract)
51 | +--Field_longstr
52 | | +--Field_string
53 | | +--Field_varstring
54 | | +--Field_blob
55 | | +--Field_geom
56 | |
57 | +--Field_null
58 | +--Field_enum
59 | +--Field_set
60 |
61 +--Field_temporal (abstract)
62  +--Field_time_common (abstract)
63  | +--Field_time
64  | +--Field_timef
65  |
66  +--Field_temporal_with_date (abstract)
67  +--Field_newdate
68  +--Field_temporal_with_date_and_time (abstract)
69  +--Field_timestamp
70  +--Field_datetime
71  +--Field_temporal_with_date_and_timef (abstract)
72  +--Field_timestampf
73  +--Field_datetimef
74 */
75 
76 
77 class Send_field;
78 class Protocol;
79 class Create_field;
80 class Relay_log_info;
81 class Field;
82 
83 enum enum_check_fields
84 {
85  CHECK_FIELD_IGNORE,
86  CHECK_FIELD_WARN,
87  CHECK_FIELD_ERROR_FOR_NULL
88 };
89 
90 
91 enum Derivation
92 {
93  DERIVATION_IGNORABLE= 6,
94  DERIVATION_NUMERIC= 5,
95  DERIVATION_COERCIBLE= 4,
96  DERIVATION_SYSCONST= 3,
97  DERIVATION_IMPLICIT= 2,
98  DERIVATION_NONE= 1,
99  DERIVATION_EXPLICIT= 0
100 };
101 
108 enum type_conversion_status
109 {
111  TYPE_OK= 0,
116  TYPE_NOTE_TIME_TRUNCATED,
121  TYPE_WARN_OUT_OF_RANGE,
131  TYPE_NOTE_TRUNCATED,
140  TYPE_WARN_TRUNCATED,
142  TYPE_ERR_NULL_CONSTRAINT_VIOLATION,
147  TYPE_ERR_BAD_VALUE,
149  TYPE_ERR_OOM
150 };
151 
152 
153 #define STORAGE_TYPE_MASK 7
154 #define COLUMN_FORMAT_MASK 7
155 #define COLUMN_FORMAT_SHIFT 3
156 
157 #define my_charset_numeric my_charset_latin1
158 #define MY_REPERTOIRE_NUMERIC MY_REPERTOIRE_ASCII
159 
160 struct st_cache_field;
161 type_conversion_status field_conv(Field *to,Field *from);
162 
163 inline uint get_enum_pack_length(int elements)
164 {
165  return elements < 256 ? 1 : 2;
166 }
167 
168 inline uint get_set_pack_length(int elements)
169 {
170  uint len= (elements + 7) / 8;
171  return len > 4 ? 8 : len;
172 }
173 
174 inline type_conversion_status
175 decimal_err_to_type_conv_status(int dec_error)
176 {
177  if (dec_error & E_DEC_OOM)
178  return TYPE_ERR_OOM;
179 
180  if (dec_error & (E_DEC_DIV_ZERO | E_DEC_BAD_NUM))
181  return TYPE_ERR_BAD_VALUE;
182 
183  if (dec_error & E_DEC_TRUNCATED)
184  return TYPE_NOTE_TRUNCATED;
185 
186  if (dec_error & E_DEC_OVERFLOW)
187  return TYPE_WARN_OUT_OF_RANGE;
188 
189  if (dec_error == E_DEC_OK)
190  return TYPE_OK;
191 
192  // impossible
193  DBUG_ASSERT(false);
194  return TYPE_ERR_BAD_VALUE;
195 }
196 
201 inline type_conversion_status
202 time_warning_to_type_conversion_status(const int warn)
203 {
204  if (warn & MYSQL_TIME_NOTE_TRUNCATED)
205  return TYPE_NOTE_TIME_TRUNCATED;
206 
207  if (warn & MYSQL_TIME_WARN_OUT_OF_RANGE)
208  return TYPE_WARN_OUT_OF_RANGE;
209 
210  if (warn & MYSQL_TIME_WARN_TRUNCATED)
211  return TYPE_NOTE_TRUNCATED;
212 
213  if (warn & (MYSQL_TIME_WARN_ZERO_DATE | MYSQL_TIME_WARN_ZERO_IN_DATE))
214  return TYPE_ERR_BAD_VALUE;
215 
216  if (warn & MYSQL_TIME_WARN_INVALID_TIMESTAMP)
217  // date was fine but pointed to daylight saving time switch gap
218  return TYPE_OK;
219 
220  DBUG_ASSERT(!warn);
221  return TYPE_OK;
222 }
223 
224 #define ASSERT_COLUMN_MARKED_FOR_READ \
225 DBUG_ASSERT(!table || (!table->read_set || \
226  bitmap_is_set(table->read_set, field_index)))
227 #define ASSERT_COLUMN_MARKED_FOR_WRITE \
228 DBUG_ASSERT(!table || (!table->write_set || \
229  bitmap_is_set(table->write_set, field_index)))
230 
231 
240 inline bool is_temporal_type(enum_field_types type)
241 {
242  switch (type)
243  {
244  case MYSQL_TYPE_TIME:
245  case MYSQL_TYPE_DATETIME:
246  case MYSQL_TYPE_TIMESTAMP:
247  case MYSQL_TYPE_DATE:
248  case MYSQL_TYPE_NEWDATE:
249  return true;
250  default:
251  return false;
252  }
253 }
254 
255 
265 inline bool is_temporal_real_type(enum_field_types type)
266 {
267  switch (type)
268  {
269  case MYSQL_TYPE_TIME2:
270  case MYSQL_TYPE_TIMESTAMP2:
271  case MYSQL_TYPE_DATETIME2:
272  return true;
273  default:
274  return is_temporal_type(type);
275  }
276 }
277 
278 
287 inline bool is_temporal_type_with_time(enum_field_types type)
288 {
289  switch (type)
290  {
291  case MYSQL_TYPE_TIME:
292  case MYSQL_TYPE_DATETIME:
293  case MYSQL_TYPE_TIMESTAMP:
294  return true;
295  default:
296  return false;
297  }
298 }
299 
300 
309 inline bool is_temporal_type_with_date(enum_field_types type)
310 {
311  switch (type)
312  {
313  case MYSQL_TYPE_DATE:
314  case MYSQL_TYPE_DATETIME:
315  case MYSQL_TYPE_TIMESTAMP:
316  return true;
317  default:
318  return false;
319  }
320 }
321 
322 
331 inline bool is_temporal_type_with_date_and_time(enum_field_types type)
332 {
333  switch (type)
334  {
335  case MYSQL_TYPE_DATETIME:
336  case MYSQL_TYPE_TIMESTAMP:
337  return true;
338  default:
339  return false;
340  }
341 }
342 
343 
352 inline bool real_type_with_now_as_default(enum_field_types type)
353 {
354  return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
355  type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
356 }
357 
358 
367 inline bool real_type_with_now_on_update(enum_field_types type)
368 {
369  return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
370  type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
371 }
372 
373 
378 inline bool is_timestamp_type(enum_field_types type)
379 {
380  return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2;
381 }
382 
383 
391 inline enum_field_types real_type_to_type(enum_field_types real_type)
392 {
393  switch (real_type)
394  {
395  case MYSQL_TYPE_TIME2:
396  return MYSQL_TYPE_TIME;
397  case MYSQL_TYPE_DATETIME2:
398  return MYSQL_TYPE_DATETIME;
399  case MYSQL_TYPE_TIMESTAMP2:
400  return MYSQL_TYPE_TIMESTAMP;
401  case MYSQL_TYPE_NEWDATE:
402  return MYSQL_TYPE_DATE;
403  /* Note: NEWDECIMAL is a type, not only a real_type */
404  default: return real_type;
405  }
406 }
407 
408 
427 template<bool Is_big_endian>
428 void copy_integer(uchar *to, int to_length,
429  const uchar* from, int from_length,
430  bool is_unsigned)
431 {
432  if (Is_big_endian)
433  {
434  if (is_unsigned)
435  to[0]= from[0];
436  else
437  to[0]= (char)(from[0] ^ 128); // Reverse the sign bit.
438  memcpy(to + 1, from + 1, to_length - 1);
439  }
440  else
441  {
442  const int sign_byte= from[from_length - 1];
443  if (is_unsigned)
444  to[0]= sign_byte;
445  else
446  to[0]= static_cast<char>(sign_byte ^ 128); // Reverse the sign bit.
447  for (int i= 1, j= from_length - 2; i < to_length; ++i, --j)
448  to[i]= from[j];
449  }
450 }
451 
452 
453 class Field
454 {
455  Field(const Item &); /* Prevent use of these */
456  void operator=(Field &);
457 public:
458 
459  bool has_insert_default_function() const
460  {
461  return unireg_check == TIMESTAMP_DN_FIELD ||
462  unireg_check == TIMESTAMP_DNUN_FIELD;
463  }
464 
465  bool has_update_default_function() const
466  {
467  return unireg_check == TIMESTAMP_UN_FIELD ||
468  unireg_check == TIMESTAMP_DNUN_FIELD;
469  }
470 
471  /* To do: inherit Sql_alloc and get these for free */
472  static void *operator new(size_t size) throw ()
473  { return sql_alloc(size); }
474  static void *operator new(size_t size, MEM_ROOT *mem_root) throw () {
475  return alloc_root(mem_root, size);
476  }
477  static void operator delete(void *ptr, MEM_ROOT *mem_root)
478  { DBUG_ASSERT(false); /* never called */ }
479 
480  static void operator delete(void *ptr_arg, size_t size) throw()
481  { TRASH(ptr_arg, size); }
482 
483  uchar *ptr; // Position to field in record
484 
485 protected:
490  uchar *null_ptr;
491 
492 public:
493  /*
494  Note that you can use table->in_use as replacement for current_thd member
495  only inside of val_*() and store() members (e.g. you can't use it in cons)
496  */
497  TABLE *table; // Pointer for table
498  TABLE *orig_table; // Pointer to original table
499  const char **table_name, *field_name;
500  LEX_STRING comment;
501  /* Field is part of the following keys */
502  key_map key_start; /* Keys that starts with this field */
503  key_map part_of_key; /* All keys that includes this field */
504  key_map part_of_key_not_clustered;/* ^ but only for non-clustered keys */
505  key_map part_of_sortkey; /* ^ but only keys usable for sorting */
506  /*
507  We use three additional unireg types for TIMESTAMP to overcome limitation
508  of current binary format of .frm file. We'd like to be able to support
509  NOW() as default and on update value for such fields but unable to hold
510  this info anywhere except unireg_check field. This issue will be resolved
511  in more clean way with transition to new text based .frm format.
512  See also comment for Field_timestamp::Field_timestamp().
513  */
514  enum utype { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
515  CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
516  BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
517  TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
518  enum geometry_type
519  {
520  GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
521  GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
522  GEOM_GEOMETRYCOLLECTION = 7
523  };
524  enum imagetype { itRAW, itMBR};
525 
526  utype unireg_check;
527  uint32 field_length; // Length of field
528  uint32 flags;
529  uint16 field_index; // field number in fields array
530  uchar null_bit; // Bit used to test null bit
541 
542  Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
543  uchar null_bit_arg, utype unireg_check_arg,
544  const char *field_name_arg);
545  virtual ~Field() {}
546 
547  /* Store functions returns 1 on overflow and -1 on fatal error */
548  virtual type_conversion_status store(const char *to, uint length,
549  const CHARSET_INFO *cs)=0;
550  virtual type_conversion_status store(double nr)=0;
551  virtual type_conversion_status store(longlong nr, bool unsigned_val)=0;
563  virtual type_conversion_status store_packed(longlong nr)
564  {
565  return store(nr, 0);
566  }
567  virtual type_conversion_status store_decimal(const my_decimal *d)=0;
583  virtual type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
592  type_conversion_status store_time(MYSQL_TIME *ltime)
593  {
594  return store_time(ltime, 0);
595  }
596  type_conversion_status store(const char *to, uint length,
597  const CHARSET_INFO *cs,
598  enum_check_fields check_level);
599  virtual double val_real(void)=0;
600  virtual longlong val_int(void)=0;
606  virtual longlong val_time_temporal()
607  {
608  DBUG_ASSERT(0);
609  return 0;
610  }
616  virtual longlong val_date_temporal()
617  {
618  DBUG_ASSERT(0);
619  return 0;
620  }
626  {
627  // Return longlong TIME or DATETIME representation, depending on field type
628  if (type() == MYSQL_TYPE_TIME)
629  return val_time_temporal();
630  DBUG_ASSERT(is_temporal_with_date());
631  return val_date_temporal();
632  }
633  virtual my_decimal *val_decimal(my_decimal *)= 0;
634  inline String *val_str(String *str) { return val_str(str, str); }
635  /*
636  val_str(buf1, buf2) gets two buffers and should use them as follows:
637  if it needs a temp buffer to convert result to string - use buf1
638  example Field_tiny::val_str()
639  if the value exists as a string already - use buf2
640  example Field_string::val_str()
641  consequently, buf2 may be created as 'String buf;' - no memory
642  will be allocated for it. buf1 will be allocated to hold a
643  value if it's too small. Using allocated buffer for buf2 may result in
644  an unnecessary free (and later, may be an alloc).
645  This trickery is used to decrease a number of malloc calls.
646  */
647  virtual String *val_str(String*,String *)=0;
648  String *val_int_as_str(String *val_buffer, my_bool unsigned_flag);
649  /*
650  str_needs_quotes() returns TRUE if the value returned by val_str() needs
651  to be quoted when used in constructing an SQL query.
652  */
653  virtual bool str_needs_quotes() { return FALSE; }
654  virtual Item_result result_type () const=0;
665  virtual Item_result numeric_context_result_type() const
666  {
667  return result_type();
668  }
669  virtual Item_result cmp_type () const { return result_type(); }
670  virtual Item_result cast_to_int_type () const { return result_type(); }
671  static bool type_can_have_key_part(enum_field_types);
672  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
673  static Item_result result_merge_type(enum_field_types);
674  virtual bool eq(Field *field)
675  {
676  return (ptr == field->ptr && null_ptr == field->null_ptr &&
677  null_bit == field->null_bit && field->type() == type());
678  }
679  virtual bool eq_def(Field *field);
680 
681  /*
682  pack_length() returns size (in bytes) used to store field data in memory
683  (i.e. it returns the maximum size of the field in a row of the table,
684  which is located in RAM).
685  */
686  virtual uint32 pack_length() const { return (uint32) field_length; }
687 
688  /*
689  pack_length_in_rec() returns size (in bytes) used to store field data on
690  storage (i.e. it returns the maximal size of the field in a row of the
691  table, which is located on disk).
692  */
693  virtual uint32 pack_length_in_rec() const { return pack_length(); }
694  virtual bool compatible_field_size(uint metadata, Relay_log_info *rli,
695  uint16 mflags, int *order);
696  virtual uint pack_length_from_metadata(uint field_metadata)
697  {
698  DBUG_ENTER("Field::pack_length_from_metadata");
699  DBUG_RETURN(field_metadata);
700  }
701  virtual uint row_pack_length() const { return 0; }
702  virtual int save_field_metadata(uchar *first_byte)
703  { return do_save_field_metadata(first_byte); }
704 
705  /*
706  data_length() return the "real size" of the data in memory.
707  */
708  virtual uint32 data_length() { return pack_length(); }
709  virtual uint32 sort_length() const { return pack_length(); }
710 
717  virtual uint32 max_data_length() const {
718  return pack_length();
719  };
720 
721  virtual type_conversion_status reset(void)
722  {
723  memset(ptr, 0, pack_length());
724  return TYPE_OK;
725  }
726  virtual void reset_fields() {}
732  virtual bool get_timestamp(struct timeval *tm, int *warnings);
764  virtual void store_timestamp(const timeval *tm) { DBUG_ASSERT(false); }
765 
772  void store_timestamp(my_time_t sec)
773  {
774  struct timeval tm;
775  tm.tv_sec= sec;
776  tm.tv_usec= 0;
777  store_timestamp(&tm);
778  }
779  virtual void set_default()
780  {
781  if (has_insert_default_function())
782  {
784  return;
785  }
786 
787  my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
788  table->record[0]);
789  memcpy(ptr, ptr + l_offset, pack_length());
790  if (real_maybe_null())
791  *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
792  (null_ptr[l_offset] & null_bit));
793  }
794 
795 
802 
803 
811  virtual bool binary() const { return 1; }
812  virtual bool zero_pack() const { return 1; }
813  virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
814  virtual uint32 key_length() const { return pack_length(); }
815  virtual enum_field_types type() const =0;
816  virtual enum_field_types real_type() const { return type(); }
817  virtual enum_field_types binlog_type() const
818  {
819  /*
820  Binlog stores field->type() as type code by default.
821  This puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
822  with extra data type details put into metadata.
823 
824  We cannot store field->type() in case of temporal types with
825  fractional seconds: TIME(n), DATETIME(n) and TIMESTAMP(n),
826  because binlog records with MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
827  type codes do not have metadata.
828  So for temporal data types with fractional seconds we'll store
829  real_type() type codes instead, i.e.
830  MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2,
831  and put precision into metatada.
832 
833  Note: perhaps binlog should eventually be modified to store
834  real_type() instead of type() for all column types.
835  */
836  return type();
837  }
838  inline int cmp(const uchar *str) { return cmp(ptr,str); }
839  virtual int cmp_max(const uchar *a, const uchar *b, uint max_len)
840  { return cmp(a, b); }
841  virtual int cmp(const uchar *,const uchar *)=0;
842  virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L)
843  { return memcmp(a,b,pack_length()); }
844  virtual int cmp_offset(uint row_offset)
845  { return cmp(ptr,ptr+row_offset); }
846  virtual int cmp_binary_offset(uint row_offset)
847  { return cmp_binary(ptr, ptr+row_offset); };
848  virtual int key_cmp(const uchar *a,const uchar *b)
849  { return cmp(a, b); }
850  virtual int key_cmp(const uchar *str, uint length)
851  { return cmp(ptr,str); }
852  virtual uint decimals() const { return 0; }
853  /*
854  Caller beware: sql_type can change str.Ptr, so check
855  ptr() to see if it changed if you are using your own buffer
856  in str and restore it with set() if needed
857  */
858  virtual void sql_type(String &str) const =0;
859 
860  bool is_temporal() const
861  { return is_temporal_type(type()); }
862 
863  bool is_temporal_with_date() const
864  { return is_temporal_type_with_date(type()); }
865 
866  bool is_temporal_with_time() const
867  { return is_temporal_type_with_time(type()); }
868 
869  bool is_temporal_with_date_and_time() const
870  { return is_temporal_type_with_date_and_time(type()); }
871 
872  bool is_null(my_ptrdiff_t row_offset= 0) const
873  {
874  /*
875  if the field is NULLable, it returns NULLity based
876  on null_ptr[row_offset] value. Otherwise it returns
877  NULL flag depending on TABLE::null_row value.
878 
879  The table may have been marked as containing only NULL values
880  for all fields if it is a NULL-complemented row of an OUTER JOIN
881  or if the query is an implicitly grouped query (has aggregate
882  functions but no GROUP BY clause) with no qualifying rows. If
883  this is the case (in which TABLE::null_row is true) and the
884  field is not nullable, the field is considered to be NULL.
885 
886  Do not change the order of testing. Fields may be associated
887  with a TABLE object without being part of the current row.
888  For NULL value check to work for these fields, they must
889  have a valid null_ptr, and this pointer must be checked before
890  TABLE::null_row.
891 
892  */
893  return real_maybe_null() ?
894  test(null_ptr[row_offset] & null_bit) : table->null_row;
895  }
896 
897  bool is_real_null(my_ptrdiff_t row_offset= 0) const
898  { return real_maybe_null() ? test(null_ptr[row_offset] & null_bit) : false; }
899 
900  bool is_null_in_record(const uchar *record) const
901  { return real_maybe_null() ? test(record[null_offset()] & null_bit) : false; }
902 
903  void set_null(my_ptrdiff_t row_offset= 0)
904  {
905  if (real_maybe_null())
906  null_ptr[row_offset]|= null_bit;
907  }
908 
909  void set_notnull(my_ptrdiff_t row_offset= 0)
910  {
911  if (real_maybe_null())
912  null_ptr[row_offset]&= (uchar) ~null_bit;
913  }
914 
915  bool maybe_null(void) const
916  { return real_maybe_null() || table->maybe_null; }
917 
919  bool real_maybe_null(void) const
920  { return null_ptr != 0; }
921 
922  uint null_offset(const uchar *record) const
923  { return (uint) (null_ptr - record); }
924 
925  uint null_offset() const
926  { return null_offset(table->record[0]); }
927 
928  void set_null_ptr(uchar *p_null_ptr, uint p_null_bit)
929  {
930  null_ptr= p_null_ptr;
931  null_bit= p_null_bit;
932  }
933 
934  enum {
935  LAST_NULL_BYTE_UNDEF= 0
936  };
937 
938  /*
939  Find the position of the last null byte for the field.
940 
941  SYNOPSIS
942  last_null_byte()
943 
944  DESCRIPTION
945  Return a pointer to the last byte of the null bytes where the
946  field conceptually is placed.
947 
948  RETURN VALUE
949  The position of the last null byte relative to the beginning of
950  the record. If the field does not use any bits of the null
951  bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
952  */
953  size_t last_null_byte() const {
954  size_t bytes= do_last_null_byte();
955  DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
956  DBUG_ASSERT(bytes <= table->s->null_bytes);
957  return bytes;
958  }
959 
960  virtual void make_field(Send_field *);
961 
972  virtual void make_sort_key(uchar *buff, uint length) = 0;
973  virtual bool optimize_range(uint idx, uint part);
974  /*
975  This should be true for fields which, when compared with constant
976  items, can be casted to longlong. In this case we will at 'fix_fields'
977  stage cast the constant items to longlongs and at the execution stage
978  use field->val_int() for comparison. Used to optimize clauses like
979  'a_column BETWEEN date_const, date_const'.
980  */
981  virtual bool can_be_compared_as_longlong() const { return false; }
982  virtual void free() {}
983  virtual Field *new_field(MEM_ROOT *root, TABLE *new_table,
984  bool keep_type);
985  virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
986  uchar *new_ptr, uchar *new_null_ptr,
987  uint new_null_bit);
988 
989  Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr)
990  { return new_key_field(root, new_table, new_ptr, null_ptr, null_bit); }
991 
1001  virtual Field *clone() const =0;
1002 
1013  virtual Field *clone(MEM_ROOT *mem_root) const =0;
1014  inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
1015  {
1016  ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
1017  }
1018  inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
1019  virtual void move_field_offset(my_ptrdiff_t ptr_diff)
1020  {
1021  ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
1022  if (null_ptr)
1023  null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
1024  }
1025  virtual void get_image(uchar *buff, uint length, const CHARSET_INFO *cs)
1026  { memcpy(buff,ptr,length); }
1027  virtual void set_image(const uchar *buff,uint length,
1028  const CHARSET_INFO *cs)
1029  { memcpy(ptr,buff,length); }
1030 
1031 
1032  /*
1033  Copy a field part into an output buffer.
1034 
1035  SYNOPSIS
1036  Field::get_key_image()
1037  buff [out] output buffer
1038  length output buffer size
1039  type itMBR for geometry blobs, otherwise itRAW
1040 
1041  DESCRIPTION
1042  This function makes a copy of field part of size equal to or
1043  less than "length" parameter value.
1044  For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
1045  is padded by zero byte.
1046 
1047  NOTES
1048  For variable length character fields (i.e. UTF-8) the "length"
1049  parameter means a number of output buffer bytes as if all field
1050  characters have maximal possible size (mbmaxlen). In the other words,
1051  "length" parameter is a number of characters multiplied by
1052  field_charset->mbmaxlen.
1053 
1054  RETURN
1055  Number of copied bytes (excluding padded zero bytes -- see above).
1056  */
1057 
1058  virtual uint get_key_image(uchar *buff, uint length, imagetype type)
1059  {
1060  get_image(buff, length, &my_charset_bin);
1061  return length;
1062  }
1063  virtual void set_key_image(const uchar *buff,uint length)
1064  { set_image(buff,length, &my_charset_bin); }
1065  inline longlong val_int_offset(uint row_offset)
1066  {
1067  ptr+=row_offset;
1068  longlong tmp=val_int();
1069  ptr-=row_offset;
1070  return tmp;
1071  }
1072  inline longlong val_int(const uchar *new_ptr)
1073  {
1074  uchar *old_ptr= ptr;
1075  longlong return_value;
1076  ptr= (uchar*) new_ptr;
1077  return_value= val_int();
1078  ptr= old_ptr;
1079  return return_value;
1080  }
1081  inline String *val_str(String *str, const uchar *new_ptr)
1082  {
1083  uchar *old_ptr= ptr;
1084  ptr= (uchar*) new_ptr;
1085  val_str(str);
1086  ptr= old_ptr;
1087  return str;
1088  }
1089  virtual bool send_binary(Protocol *protocol);
1090 
1091  virtual uchar *pack(uchar *to, const uchar *from,
1092  uint max_length, bool low_byte_first);
1096  uchar *pack(uchar *to, const uchar *from)
1097  {
1098  DBUG_ENTER("Field::pack");
1099  uchar *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
1100  DBUG_RETURN(result);
1101  }
1102 
1103  virtual const uchar *unpack(uchar* to, const uchar *from,
1104  uint param_data, bool low_byte_first);
1108  const uchar *unpack(uchar* to, const uchar *from)
1109  {
1110  DBUG_ENTER("Field::unpack");
1111  const uchar *result= unpack(to, from, 0U, table->s->db_low_byte_first);
1112  DBUG_RETURN(result);
1113  }
1114 
1115  virtual uint packed_col_length(const uchar *to, uint length)
1116  { return length;}
1117  virtual uint max_packed_col_length(uint max_length)
1118  { return max_length;}
1119 
1120  uint offset(uchar *record)
1121  {
1122  return (uint) (ptr - record);
1123  }
1124  void copy_from_tmp(int offset);
1125  uint fill_cache_field(struct st_cache_field *copy);
1126  virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
1127  virtual bool get_time(MYSQL_TIME *ltime);
1128  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
1129  virtual const CHARSET_INFO *charset_for_protocol(void) const
1130  { return binary() ? &my_charset_bin : charset(); }
1131  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
1132  virtual bool has_charset(void) const { return FALSE; }
1133  /*
1134  match_collation_to_optimize_range() is to distinguish in
1135  range optimizer (see opt_range.cc) between real string types:
1136  CHAR, VARCHAR, TEXT
1137  and the other string-alike types with result_type() == STRING_RESULT:
1138  DATE, TIME, DATETIME, TIMESTAMP
1139  We need it to decide whether to test if collation of the operation
1140  matches collation of the field (needed only for real string types).
1141  QQ: shouldn't DATE/TIME types have their own XXX_RESULT types eventually?
1142  */
1143  virtual bool match_collation_to_optimize_range() const { return false; };
1144  virtual enum Derivation derivation(void) const
1145  { return DERIVATION_IMPLICIT; }
1146  virtual uint repertoire(void) const { return MY_REPERTOIRE_UNICODE30; }
1147  virtual void set_derivation(enum Derivation derivation_arg) { }
1148  bool set_warning(Sql_condition::enum_warning_level, unsigned int code,
1149  int cuted_increment) const;
1150  inline bool check_overflow(int op_result)
1151  {
1152  return (op_result == E_DEC_OVERFLOW);
1153  }
1154  inline bool check_truncated(int op_result)
1155  {
1156  return (op_result == E_DEC_TRUNCATED);
1157  }
1158  bool warn_if_overflow(int op_result);
1159  void init(TABLE *table_arg)
1160  {
1161  orig_table= table= table_arg;
1162  table_name= &table_arg->alias;
1163  }
1164 
1165  /* maximum possible display length */
1166  virtual uint32 max_display_length()= 0;
1167 
1175  virtual uint is_equal(Create_field *new_field);
1176  /* convert decimal to longlong with overflow check */
1177  longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
1178  bool *has_overflow);
1179  /* The max. number of characters */
1180  virtual uint32 char_length()
1181  {
1182  return field_length / charset()->mbmaxlen;
1183  }
1184 
1185  virtual geometry_type get_geometry_type()
1186  {
1187  /* shouldn't get here. */
1188  DBUG_ASSERT(0);
1189  return GEOM_GEOMETRY;
1190  }
1191 #ifndef DBUG_OFF
1192  /* Print field value into debug trace, in NULL-aware way. */
1193  void dbug_print()
1194  {
1195  if (is_real_null())
1196  fprintf(DBUG_FILE, "NULL");
1197  else
1198  {
1199  char buf[256];
1200  String str(buf, sizeof(buf), &my_charset_bin);
1201  str.length(0);
1202  String *pstr;
1203  pstr= val_str(&str);
1204  fprintf(DBUG_FILE, "'%s'", pstr->c_ptr_safe());
1205  }
1206  }
1207 #endif
1208 
1209  ha_storage_media field_storage_type() const
1210  {
1211  return (ha_storage_media)
1212  ((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
1213  }
1214 
1215  void set_storage_type(ha_storage_media storage_type_arg)
1216  {
1217  DBUG_ASSERT(field_storage_type() == HA_SM_DEFAULT);
1218  flags |= (storage_type_arg << FIELD_FLAGS_STORAGE_MEDIA);
1219  }
1220 
1221  column_format_type column_format() const
1222  {
1223  return (column_format_type)
1224  ((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
1225  }
1226 
1227  void set_column_format(column_format_type column_format_arg)
1228  {
1229  DBUG_ASSERT(column_format() == COLUMN_FORMAT_TYPE_DEFAULT);
1230  flags |= (column_format_arg << FIELD_FLAGS_COLUMN_FORMAT);
1231  }
1232 
1233  /* Validate the value stored in a field */
1234  virtual type_conversion_status validate_stored_val(THD *thd)
1235  { return TYPE_OK; }
1236 
1237  /* Hash value */
1238  virtual void hash(ulong *nr, ulong *nr2);
1239  friend int cre_myisam(char * name, register TABLE *form, uint options,
1240  ulonglong auto_increment_value);
1241  friend class Copy_field;
1242  friend class Item_avg_field;
1243  friend class Item_std_field;
1244  friend class Item_sum_num;
1245  friend class Item_sum_sum;
1246  friend class Item_sum_str;
1247  friend class Item_sum_count;
1248  friend class Item_sum_avg;
1249  friend class Item_sum_std;
1250  friend class Item_sum_min;
1251  friend class Item_sum_max;
1252  friend class Item_func_group_concat;
1253 
1254 private:
1255  /*
1256  Primitive for implementing last_null_byte().
1257 
1258  SYNOPSIS
1259  do_last_null_byte()
1260 
1261  DESCRIPTION
1262  Primitive for the implementation of the last_null_byte()
1263  function. This represents the inheritance interface and can be
1264  overridden by subclasses.
1265  */
1266  virtual size_t do_last_null_byte() const;
1267 
1278  virtual int do_save_field_metadata(uchar *metadata_ptr)
1279  { return 0; }
1280 
1281 protected:
1282  static void handle_int16(uchar *to, const uchar *from,
1283  bool low_byte_first_from, bool low_byte_first_to)
1284  {
1285  int16 val;
1286 #ifdef WORDS_BIGENDIAN
1287  if (low_byte_first_from)
1288  val = sint2korr(from);
1289  else
1290 #endif
1291  shortget(val, from);
1292 
1293 #ifdef WORDS_BIGENDIAN
1294  if (low_byte_first_to)
1295  int2store(to, val);
1296  else
1297 #endif
1298  shortstore(to, val);
1299  }
1300 
1301  static void handle_int24(uchar *to, const uchar *from,
1302  bool low_byte_first_from, bool low_byte_first_to)
1303  {
1304  int32 val;
1305 #ifdef WORDS_BIGENDIAN
1306  if (low_byte_first_from)
1307  val = sint3korr(from);
1308  else
1309 #endif
1310  val= (from[0] << 16) + (from[1] << 8) + from[2];
1311 
1312 #ifdef WORDS_BIGENDIAN
1313  if (low_byte_first_to)
1314  int2store(to, val);
1315  else
1316 #endif
1317  {
1318  to[0]= 0xFF & (val >> 16);
1319  to[1]= 0xFF & (val >> 8);
1320  to[2]= 0xFF & val;
1321  }
1322  }
1323 
1324  /*
1325  Helper function to pack()/unpack() int32 values
1326  */
1327  static void handle_int32(uchar *to, const uchar *from,
1328  bool low_byte_first_from, bool low_byte_first_to)
1329  {
1330  int32 val;
1331 #ifdef WORDS_BIGENDIAN
1332  if (low_byte_first_from)
1333  val = sint4korr(from);
1334  else
1335 #endif
1336  longget(val, from);
1337 
1338 #ifdef WORDS_BIGENDIAN
1339  if (low_byte_first_to)
1340  int4store(to, val);
1341  else
1342 #endif
1343  longstore(to, val);
1344  }
1345 
1346  /*
1347  Helper function to pack()/unpack() int64 values
1348  */
1349  static void handle_int64(uchar* to, const uchar *from,
1350  bool low_byte_first_from, bool low_byte_first_to)
1351  {
1352  int64 val;
1353 #ifdef WORDS_BIGENDIAN
1354  if (low_byte_first_from)
1355  val = sint8korr(from);
1356  else
1357 #endif
1358  longlongget(val, from);
1359 
1360 #ifdef WORDS_BIGENDIAN
1361  if (low_byte_first_to)
1362  int8store(to, val);
1363  else
1364 #endif
1365  longlongstore(to, val);
1366  }
1367 
1368  uchar *pack_int16(uchar *to, const uchar *from, bool low_byte_first_to)
1369  {
1370  handle_int16(to, from, table->s->db_low_byte_first, low_byte_first_to);
1371  return to + sizeof(int16);
1372  }
1373 
1374  const uchar *unpack_int16(uchar* to, const uchar *from,
1375  bool low_byte_first_from)
1376  {
1377  handle_int16(to, from, low_byte_first_from, table->s->db_low_byte_first);
1378  return from + sizeof(int16);
1379  }
1380 
1381  uchar *pack_int24(uchar *to, const uchar *from, bool low_byte_first_to)
1382  {
1383  handle_int24(to, from, table->s->db_low_byte_first, low_byte_first_to);
1384  return to + 3;
1385  }
1386 
1387  const uchar *unpack_int24(uchar* to, const uchar *from,
1388  bool low_byte_first_from)
1389  {
1390  handle_int24(to, from, low_byte_first_from, table->s->db_low_byte_first);
1391  return from + 3;
1392  }
1393 
1394  uchar *pack_int32(uchar *to, const uchar *from, bool low_byte_first_to)
1395  {
1396  handle_int32(to, from, table->s->db_low_byte_first, low_byte_first_to);
1397  return to + sizeof(int32);
1398  }
1399 
1400  const uchar *unpack_int32(uchar* to, const uchar *from,
1401  bool low_byte_first_from)
1402  {
1403  handle_int32(to, from, low_byte_first_from, table->s->db_low_byte_first);
1404  return from + sizeof(int32);
1405  }
1406 
1407  uchar *pack_int64(uchar* to, const uchar *from, bool low_byte_first_to)
1408  {
1409  handle_int64(to, from, table->s->db_low_byte_first, low_byte_first_to);
1410  return to + sizeof(int64);
1411  }
1412 
1413  const uchar *unpack_int64(uchar* to, const uchar *from,
1414  bool low_byte_first_from)
1415  {
1416  handle_int64(to, from, low_byte_first_from, table->s->db_low_byte_first);
1417  return from + sizeof(int64);
1418  }
1419 
1420  bool field_flags_are_binary()
1421  {
1422  return (flags & (BINCMP_FLAG | BINARY_FLAG)) != 0;
1423  }
1424 
1425 };
1426 
1427 
1428 class Field_num :public Field {
1429 public:
1430  const uint8 dec;
1431  bool zerofill,unsigned_flag; // Purify cannot handle bit fields
1432  Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1433  uchar null_bit_arg, utype unireg_check_arg,
1434  const char *field_name_arg,
1435  uint8 dec_arg, bool zero_arg, bool unsigned_arg);
1436  Item_result result_type () const { return REAL_RESULT; }
1437  enum Derivation derivation(void) const { return DERIVATION_NUMERIC; }
1438  uint repertoire(void) const { return MY_REPERTOIRE_NUMERIC; }
1439  const CHARSET_INFO *charset(void) const { return &my_charset_numeric; }
1440  void prepend_zeros(String *value);
1441  void add_zerofill_and_unsigned(String &res) const;
1442  friend class Create_field;
1443  uint decimals() const { return (uint) dec; }
1444  bool eq_def(Field *field);
1445  type_conversion_status store_decimal(const my_decimal *);
1446  type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
1448  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1449  bool get_time(MYSQL_TIME *ltime);
1450  uint is_equal(Create_field *new_field);
1451  uint row_pack_length() const { return pack_length(); }
1452  uint32 pack_length_from_metadata(uint field_metadata) {
1453  uint32 length= pack_length();
1454  DBUG_PRINT("result", ("pack_length_from_metadata(%d): %u",
1455  field_metadata, length));
1456  return length;
1457  }
1458  type_conversion_status check_int(const CHARSET_INFO *cs,
1459  const char *str, int length,
1460  const char *int_end, int error);
1461  type_conversion_status get_int(const CHARSET_INFO *cs,
1462  const char *from, uint len,
1463  longlong *rnd, ulonglong unsigned_max,
1464  longlong signed_min, longlong signed_max);
1465 };
1466 
1467 
1468 class Field_str :public Field {
1469 protected:
1470  const CHARSET_INFO *field_charset;
1471  enum Derivation field_derivation;
1472 public:
1473  Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1474  uchar null_bit_arg, utype unireg_check_arg,
1475  const char *field_name_arg, const CHARSET_INFO *charset);
1476  Item_result result_type () const { return STRING_RESULT; }
1477  Item_result numeric_context_result_type() const
1478  {
1479  return REAL_RESULT;
1480  }
1481  uint decimals() const { return NOT_FIXED_DEC; }
1482  void make_field(Send_field *field);
1483  type_conversion_status store(double nr);
1484  type_conversion_status store(longlong nr, bool unsigned_val)=0;
1485  type_conversion_status store_decimal(const my_decimal *);
1486  type_conversion_status store(const char *to, uint length,
1487  const CHARSET_INFO *cs)=0;
1488  uint repertoire(void) const
1489  {
1490  return my_charset_repertoire(field_charset);
1491  }
1492  const CHARSET_INFO *charset(void) const { return field_charset; }
1493  void set_charset(const CHARSET_INFO *charset_arg)
1494  { field_charset= charset_arg; }
1495  enum Derivation derivation(void) const { return field_derivation; }
1496  virtual void set_derivation(enum Derivation derivation_arg)
1497  { field_derivation= derivation_arg; }
1498  bool binary() const { return field_charset == &my_charset_bin; }
1499  uint32 max_display_length() { return field_length; }
1500  friend class Create_field;
1501  virtual bool str_needs_quotes() { return TRUE; }
1502  uint is_equal(Create_field *new_field);
1503 };
1504 
1505 
1506 /* base class for Field_string, Field_varstring and Field_blob */
1507 
1509 {
1510 protected:
1511  type_conversion_status report_if_important_data(const char *ptr,
1512  const char *end,
1513  bool count_spaces) const;
1514  type_conversion_status
1515  check_string_copy_error(const char *well_formed_error_pos,
1516  const char *cannot_convert_error_pos,
1517  const char *from_end_pos,
1518  const char *end,
1519  bool count_spaces,
1520  const CHARSET_INFO *cs) const;
1521 public:
1522  Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1523  uchar null_bit_arg, utype unireg_check_arg,
1524  const char *field_name_arg, const CHARSET_INFO *charset_arg)
1525  :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
1526  field_name_arg, charset_arg)
1527  {}
1528 
1529  type_conversion_status store_decimal(const my_decimal *d);
1530  uint32 max_data_length() const;
1531 };
1532 
1533 /* base class for float and double and decimal (old one) */
1534 class Field_real :public Field_num {
1535 public:
1536  my_bool not_fixed;
1537 
1538  Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1539  uchar null_bit_arg, utype unireg_check_arg,
1540  const char *field_name_arg,
1541  uint8 dec_arg, bool zero_arg, bool unsigned_arg)
1542  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
1543  field_name_arg, dec_arg, zero_arg, unsigned_arg),
1544  not_fixed(dec_arg >= NOT_FIXED_DEC)
1545  {}
1546  type_conversion_status store_decimal(const my_decimal *);
1547  type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
1549  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1550  bool get_time(MYSQL_TIME *ltime);
1551  bool truncate(double *nr, double max_length);
1552  uint32 max_display_length() { return field_length; }
1553  virtual const uchar *unpack(uchar* to, const uchar *from,
1554  uint param_data, bool low_byte_first);
1555  virtual uchar *pack(uchar* to, const uchar *from,
1556  uint max_length, bool low_byte_first);
1557 };
1558 
1559 
1560 class Field_decimal :public Field_real {
1561 public:
1562  Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1563  uchar null_bit_arg,
1564  enum utype unireg_check_arg, const char *field_name_arg,
1565  uint8 dec_arg,bool zero_arg,bool unsigned_arg)
1566  :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1567  unireg_check_arg, field_name_arg,
1568  dec_arg, zero_arg, unsigned_arg)
1569  {}
1570  enum_field_types type() const { return MYSQL_TYPE_DECIMAL;}
1571  enum ha_base_keytype key_type() const
1572  { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
1573  type_conversion_status reset(void);
1574  type_conversion_status store(const char *to, uint length,
1575  const CHARSET_INFO *charset);
1576  type_conversion_status store(double nr);
1577  type_conversion_status store(longlong nr, bool unsigned_val);
1578  double val_real(void);
1579  longlong val_int(void);
1580  String *val_str(String*,String *);
1581  int cmp(const uchar *,const uchar *);
1582  void make_sort_key(uchar *buff, uint length);
1583  void overflow(bool negative);
1584  bool zero_pack() const { return 0; }
1585  void sql_type(String &str) const;
1586  Field_decimal *clone(MEM_ROOT *mem_root) const {
1587  DBUG_ASSERT(type() == MYSQL_TYPE_DECIMAL);
1588  return new (mem_root) Field_decimal(*this);
1589  }
1591  DBUG_ASSERT(type() == MYSQL_TYPE_DECIMAL);
1592  return new Field_decimal(*this);
1593  }
1594  virtual const uchar *unpack(uchar* to, const uchar *from,
1595  uint param_data, bool low_byte_first)
1596  {
1597  return Field::unpack(to, from, param_data, low_byte_first);
1598  }
1599  virtual uchar *pack(uchar* to, const uchar *from,
1600  uint max_length, bool low_byte_first)
1601  {
1602  return Field::pack(to, from, max_length, low_byte_first);
1603  }
1604 };
1605 
1606 
1607 /* New decimal/numeric field which use fixed point arithmetic */
1609 private:
1610  int do_save_field_metadata(uchar *first_byte);
1611 public:
1612 
1613  /* The maximum number of decimal digits can be stored */
1614  uint precision;
1615  uint bin_size;
1616  /*
1617  Constructors take max_length of the field as a parameter - not the
1618  precision as the number of decimal digits allowed.
1619  So for example we need to count length from precision handling
1620  CREATE TABLE ( DECIMAL(x,y))
1621  */
1622  Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1623  uchar null_bit_arg,
1624  enum utype unireg_check_arg, const char *field_name_arg,
1625  uint8 dec_arg, bool zero_arg, bool unsigned_arg);
1626  Field_new_decimal(uint32 len_arg, bool maybe_null_arg,
1627  const char *field_name_arg, uint8 dec_arg,
1628  bool unsigned_arg);
1629  enum_field_types type() const { return MYSQL_TYPE_NEWDECIMAL;}
1630  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
1631  Item_result result_type () const { return DECIMAL_RESULT; }
1632  type_conversion_status reset(void);
1633  type_conversion_status store_value(const my_decimal *decimal_value);
1634  void set_value_on_overflow(my_decimal *decimal_value, bool sign);
1635  type_conversion_status store(const char *to, uint length,
1636  const CHARSET_INFO *charset);
1637  type_conversion_status store(double nr);
1638  type_conversion_status store(longlong nr, bool unsigned_val);
1639  type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
1640  type_conversion_status store_decimal(const my_decimal *);
1641  double val_real(void);
1642  longlong val_int(void);
1644  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1645  bool get_time(MYSQL_TIME *ltime);
1646  String *val_str(String*, String *);
1647  int cmp(const uchar *, const uchar *);
1648  void make_sort_key(uchar *buff, uint length);
1649  bool zero_pack() const { return 0; }
1650  void sql_type(String &str) const;
1651  uint32 max_display_length() { return field_length; }
1652  uint32 pack_length() const { return (uint32) bin_size; }
1653  uint pack_length_from_metadata(uint field_metadata);
1654  uint row_pack_length() const { return pack_length(); }
1655  bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
1656  uint16 mflags, int *order_var);
1657  uint is_equal(Create_field *new_field);
1658  Field_new_decimal *clone(MEM_ROOT *mem_root) const {
1659  DBUG_ASSERT(type() == MYSQL_TYPE_NEWDECIMAL);
1660  return new (mem_root) Field_new_decimal(*this);
1661  }
1663  DBUG_ASSERT(type() == MYSQL_TYPE_NEWDECIMAL);
1664  return new Field_new_decimal(*this);
1665  }
1666  virtual const uchar *unpack(uchar* to, const uchar *from,
1667  uint param_data, bool low_byte_first);
1668  static Field *create_from_item (Item *);
1669 };
1670 
1671 
1672 class Field_tiny :public Field_num {
1673 public:
1674  Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1675  uchar null_bit_arg,
1676  enum utype unireg_check_arg, const char *field_name_arg,
1677  bool zero_arg, bool unsigned_arg)
1678  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1679  unireg_check_arg, field_name_arg,
1680  0, zero_arg,unsigned_arg)
1681  {}
1682  enum Item_result result_type () const { return INT_RESULT; }
1683  enum_field_types type() const { return MYSQL_TYPE_TINY;}
1684  enum ha_base_keytype key_type() const
1685  { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
1686  type_conversion_status store(const char *to, uint length,
1687  const CHARSET_INFO *charset);
1688  type_conversion_status store(double nr);
1689  type_conversion_status store(longlong nr, bool unsigned_val);
1690  type_conversion_status reset(void) { ptr[0]=0; return TYPE_OK; }
1691  double val_real(void);
1692  longlong val_int(void);
1693  String *val_str(String*,String *);
1694  bool send_binary(Protocol *protocol);
1695  int cmp(const uchar *,const uchar *);
1696  void make_sort_key(uchar *buff, uint length);
1697  uint32 pack_length() const { return 1; }
1698  void sql_type(String &str) const;
1699  uint32 max_display_length() { return 4; }
1700  Field_tiny *clone(MEM_ROOT *mem_root) const {
1701  DBUG_ASSERT(type() == MYSQL_TYPE_TINY);
1702  return new (mem_root) Field_tiny(*this);
1703  }
1704  Field_tiny *clone() const {
1705  DBUG_ASSERT(type() == MYSQL_TYPE_TINY);
1706  return new Field_tiny(*this);
1707  }
1708  virtual uchar *pack(uchar* to, const uchar *from,
1709  uint max_length, bool low_byte_first)
1710  {
1711  *to= *from;
1712  return to + 1;
1713  }
1714 
1715  virtual const uchar *unpack(uchar* to, const uchar *from,
1716  uint param_data, bool low_byte_first)
1717  {
1718  *to= *from;
1719  return from + 1;
1720  }
1721 };
1722 
1723 
1724 class Field_short :public Field_num {
1725 public:
1726  Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1727  uchar null_bit_arg,
1728  enum utype unireg_check_arg, const char *field_name_arg,
1729  bool zero_arg, bool unsigned_arg)
1730  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1731  unireg_check_arg, field_name_arg,
1732  0, zero_arg,unsigned_arg)
1733  {}
1734  Field_short(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
1735  bool unsigned_arg)
1736  :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
1737  NONE, field_name_arg, 0, 0, unsigned_arg)
1738  {}
1739  enum Item_result result_type () const { return INT_RESULT; }
1740  enum_field_types type() const { return MYSQL_TYPE_SHORT;}
1741  enum ha_base_keytype key_type() const
1742  { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
1743  type_conversion_status store(const char *to, uint length,
1744  const CHARSET_INFO *charset);
1745  type_conversion_status store(double nr);
1746  type_conversion_status store(longlong nr, bool unsigned_val);
1747  type_conversion_status reset(void) { ptr[0]=ptr[1]=0; return TYPE_OK; }
1748  double val_real(void);
1749  longlong val_int(void);
1750  String *val_str(String*,String *);
1751  bool send_binary(Protocol *protocol);
1752  int cmp(const uchar *,const uchar *);
1753  void make_sort_key(uchar *buff, uint length);
1754  uint32 pack_length() const { return 2; }
1755  void sql_type(String &str) const;
1756  uint32 max_display_length() { return 6; }
1757  Field_short *clone(MEM_ROOT *mem_root) const {
1758  DBUG_ASSERT(type() == MYSQL_TYPE_SHORT);
1759  return new (mem_root) Field_short(*this);
1760  }
1761  Field_short *clone() const {
1762  DBUG_ASSERT(type() == MYSQL_TYPE_SHORT);
1763  return new Field_short(*this);
1764  }
1765  virtual uchar *pack(uchar* to, const uchar *from,
1766  uint max_length, bool low_byte_first)
1767  {
1768  return pack_int16(to, from, low_byte_first);
1769  }
1770 
1771  virtual const uchar *unpack(uchar* to, const uchar *from,
1772  uint param_data, bool low_byte_first)
1773  {
1774  return unpack_int16(to, from, low_byte_first);
1775  }
1776 };
1777 
1778 class Field_medium :public Field_num {
1779 public:
1780  Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1781  uchar null_bit_arg,
1782  enum utype unireg_check_arg, const char *field_name_arg,
1783  bool zero_arg, bool unsigned_arg)
1784  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1785  unireg_check_arg, field_name_arg,
1786  0, zero_arg,unsigned_arg)
1787  {}
1788  enum Item_result result_type () const { return INT_RESULT; }
1789  enum_field_types type() const { return MYSQL_TYPE_INT24;}
1790  enum ha_base_keytype key_type() const
1791  { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
1792  type_conversion_status store(const char *to, uint length,
1793  const CHARSET_INFO *charset);
1794  type_conversion_status store(double nr);
1795  type_conversion_status store(longlong nr, bool unsigned_val);
1796  type_conversion_status reset(void)
1797  {
1798  ptr[0]=ptr[1]=ptr[2]=0;
1799  return TYPE_OK;
1800  }
1801  double val_real(void);
1802  longlong val_int(void);
1803  String *val_str(String*,String *);
1804  bool send_binary(Protocol *protocol);
1805  int cmp(const uchar *,const uchar *);
1806  void make_sort_key(uchar *buff, uint length);
1807  uint32 pack_length() const { return 3; }
1808  void sql_type(String &str) const;
1809  uint32 max_display_length() { return 8; }
1810  Field_medium *clone(MEM_ROOT *mem_root) const {
1811  DBUG_ASSERT(type() == MYSQL_TYPE_INT24);
1812  return new (mem_root) Field_medium(*this);
1813  }
1814  Field_medium *clone() const {
1815  DBUG_ASSERT(type() == MYSQL_TYPE_INT24);
1816  return new Field_medium(*this);
1817  }
1818  virtual uchar *pack(uchar* to, const uchar *from,
1819  uint max_length, bool low_byte_first)
1820  {
1821  return Field::pack(to, from, max_length, low_byte_first);
1822  }
1823 
1824  virtual const uchar *unpack(uchar* to, const uchar *from,
1825  uint param_data, bool low_byte_first)
1826  {
1827  return Field::unpack(to, from, param_data, low_byte_first);
1828  }
1829 };
1830 
1831 
1832 class Field_long :public Field_num {
1833 public:
1834 
1835  static const int PACK_LENGTH= 4;
1836 
1837  Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1838  uchar null_bit_arg,
1839  enum utype unireg_check_arg, const char *field_name_arg,
1840  bool zero_arg, bool unsigned_arg)
1841  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1842  unireg_check_arg, field_name_arg,
1843  0, zero_arg,unsigned_arg)
1844  {}
1845  Field_long(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
1846  bool unsigned_arg)
1847  :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
1848  NONE, field_name_arg,0,0,unsigned_arg)
1849  {}
1850  enum Item_result result_type () const { return INT_RESULT; }
1851  enum_field_types type() const { return MYSQL_TYPE_LONG;}
1852  enum ha_base_keytype key_type() const
1853  { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
1854  type_conversion_status store(const char *to, uint length,
1855  const CHARSET_INFO *charset);
1856  type_conversion_status store(double nr);
1857  type_conversion_status store(longlong nr, bool unsigned_val);
1858  type_conversion_status reset(void)
1859  {
1860  ptr[0]=ptr[1]=ptr[2]=ptr[3]=0;
1861  return TYPE_OK;
1862  }
1863  double val_real(void);
1864  longlong val_int(void);
1865  bool send_binary(Protocol *protocol);
1866  String *val_str(String*,String *);
1867  int cmp(const uchar *,const uchar *);
1868  void make_sort_key(uchar *buff, uint length);
1869  uint32 pack_length() const { return PACK_LENGTH; }
1870  void sql_type(String &str) const;
1871  uint32 max_display_length() { return MY_INT32_NUM_DECIMAL_DIGITS; }
1872  Field_long *clone(MEM_ROOT *mem_root) const {
1873  DBUG_ASSERT(type() == MYSQL_TYPE_LONG);
1874  return new (mem_root) Field_long(*this);
1875  }
1876  Field_long *clone() const {
1877  DBUG_ASSERT(type() == MYSQL_TYPE_LONG);
1878  return new Field_long(*this);
1879  }
1880  virtual uchar *pack(uchar* to, const uchar *from,
1881  uint max_length __attribute__((unused)),
1882  bool low_byte_first)
1883  {
1884  return pack_int32(to, from, low_byte_first);
1885  }
1886  virtual const uchar *unpack(uchar* to, const uchar *from,
1887  uint param_data __attribute__((unused)),
1888  bool low_byte_first)
1889  {
1890  return unpack_int32(to, from, low_byte_first);
1891  }
1892 };
1893 
1894 
1895 #ifdef HAVE_LONG_LONG
1896 class Field_longlong :public Field_num {
1897 public:
1898  static const int PACK_LENGTH= 8;
1899 
1900  Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1901  uchar null_bit_arg,
1902  enum utype unireg_check_arg, const char *field_name_arg,
1903  bool zero_arg, bool unsigned_arg)
1904  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1905  unireg_check_arg, field_name_arg,
1906  0, zero_arg,unsigned_arg)
1907  {}
1908  Field_longlong(uint32 len_arg,bool maybe_null_arg,
1909  const char *field_name_arg,
1910  bool unsigned_arg)
1911  :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
1912  NONE, field_name_arg,0,0,unsigned_arg)
1913  {}
1914  enum Item_result result_type () const { return INT_RESULT; }
1915  enum_field_types type() const { return MYSQL_TYPE_LONGLONG;}
1916  enum ha_base_keytype key_type() const
1917  { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
1918  type_conversion_status store(const char *to, uint length,
1919  const CHARSET_INFO *charset);
1920  type_conversion_status store(double nr);
1921  type_conversion_status store(longlong nr, bool unsigned_val);
1922  type_conversion_status reset(void)
1923  {
1924  ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
1925  return TYPE_OK;
1926  }
1927  double val_real(void);
1928  longlong val_int(void);
1929  String *val_str(String*,String *);
1930  bool send_binary(Protocol *protocol);
1931  int cmp(const uchar *,const uchar *);
1932  void make_sort_key(uchar *buff, uint length);
1933  uint32 pack_length() const { return PACK_LENGTH; }
1934  void sql_type(String &str) const;
1935  bool can_be_compared_as_longlong() const { return true; }
1936  uint32 max_display_length() { return 20; }
1937  Field_longlong *clone(MEM_ROOT *mem_root) const {
1938  DBUG_ASSERT(type() == MYSQL_TYPE_LONGLONG);
1939  return new (mem_root) Field_longlong(*this);
1940  }
1941  Field_longlong *clone() const {
1942  DBUG_ASSERT(type() == MYSQL_TYPE_LONGLONG);
1943  return new Field_longlong(*this);
1944  }
1945  virtual uchar *pack(uchar* to, const uchar *from,
1946  uint max_length __attribute__((unused)),
1947  bool low_byte_first)
1948  {
1949  return pack_int64(to, from, low_byte_first);
1950  }
1951  virtual const uchar *unpack(uchar* to, const uchar *from,
1952  uint param_data __attribute__((unused)),
1953  bool low_byte_first)
1954  {
1955  return unpack_int64(to, from, low_byte_first);
1956  }
1957 };
1958 #endif
1959 
1960 
1961 class Field_float :public Field_real {
1962 public:
1963  Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1964  uchar null_bit_arg,
1965  enum utype unireg_check_arg, const char *field_name_arg,
1966  uint8 dec_arg,bool zero_arg,bool unsigned_arg)
1967  :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1968  unireg_check_arg, field_name_arg,
1969  dec_arg, zero_arg, unsigned_arg)
1970  {}
1971  Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
1972  uint8 dec_arg)
1973  :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
1974  NONE, field_name_arg, dec_arg, 0, 0)
1975  {}
1976  enum_field_types type() const { return MYSQL_TYPE_FLOAT;}
1977  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
1978  type_conversion_status store(const char *to, uint length,
1979  const CHARSET_INFO *charset);
1980  type_conversion_status store(double nr);
1981  type_conversion_status store(longlong nr, bool unsigned_val);
1982  type_conversion_status reset(void)
1983  {
1984  memset(ptr, 0, sizeof(float));
1985  return TYPE_OK;
1986  }
1987  double val_real(void);
1988  longlong val_int(void);
1989  String *val_str(String*,String *);
1990  bool send_binary(Protocol *protocol);
1991  int cmp(const uchar *,const uchar *);
1992  void make_sort_key(uchar *buff, uint length);
1993  uint32 pack_length() const { return sizeof(float); }
1994  uint row_pack_length() const { return pack_length(); }
1995  void sql_type(String &str) const;
1996  Field_float *clone(MEM_ROOT *mem_root) const {
1997  DBUG_ASSERT(type() == MYSQL_TYPE_FLOAT);
1998  return new (mem_root) Field_float(*this);
1999  }
2000  Field_float *clone() const {
2001  DBUG_ASSERT(type() == MYSQL_TYPE_FLOAT);
2002  return new Field_float(*this);
2003  }
2004 private:
2005  int do_save_field_metadata(uchar *first_byte);
2006 };
2007 
2008 
2009 class Field_double :public Field_real {
2010 public:
2011  Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2012  uchar null_bit_arg,
2013  enum utype unireg_check_arg, const char *field_name_arg,
2014  uint8 dec_arg,bool zero_arg,bool unsigned_arg)
2015  :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2016  unireg_check_arg, field_name_arg,
2017  dec_arg, zero_arg, unsigned_arg)
2018  {}
2019  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
2020  uint8 dec_arg)
2021  :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
2022  NONE, field_name_arg, dec_arg, 0, 0)
2023  {}
2024  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
2025  uint8 dec_arg, my_bool not_fixed_arg)
2026  :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
2027  NONE, field_name_arg, dec_arg, 0, 0)
2028  {not_fixed= not_fixed_arg; }
2029  enum_field_types type() const { return MYSQL_TYPE_DOUBLE;}
2030  enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
2031  type_conversion_status store(const char *to, uint length,
2032  const CHARSET_INFO *charset);
2033  type_conversion_status store(double nr);
2034  type_conversion_status store(longlong nr, bool unsigned_val);
2035  type_conversion_status reset(void)
2036  {
2037  memset(ptr, 0, sizeof(double));
2038  return TYPE_OK;
2039  }
2040  double val_real(void);
2041  longlong val_int(void);
2042  String *val_str(String*,String *);
2043  bool send_binary(Protocol *protocol);
2044  int cmp(const uchar *,const uchar *);
2045  void make_sort_key(uchar *buff, uint length);
2046  uint32 pack_length() const { return sizeof(double); }
2047  uint row_pack_length() const { return pack_length(); }
2048  void sql_type(String &str) const;
2049  Field_double *clone(MEM_ROOT *mem_root) const {
2050  DBUG_ASSERT(type() == MYSQL_TYPE_DOUBLE);
2051  return new (mem_root) Field_double(*this);
2052  }
2053  Field_double *clone() const {
2054  DBUG_ASSERT(type() == MYSQL_TYPE_DOUBLE);
2055  return new Field_double(*this);
2056  }
2057 private:
2058  int do_save_field_metadata(uchar *first_byte);
2059 };
2060 
2061 
2062 /* Everything saved in this will disappear. It will always return NULL */
2063 
2064 class Field_null :public Field_str {
2065  static uchar null[1];
2066 public:
2067  Field_null(uchar *ptr_arg, uint32 len_arg,
2068  enum utype unireg_check_arg, const char *field_name_arg,
2069  const CHARSET_INFO *cs)
2070  :Field_str(ptr_arg, len_arg, null, 1,
2071  unireg_check_arg, field_name_arg, cs)
2072  {}
2073  enum_field_types type() const { return MYSQL_TYPE_NULL;}
2074  type_conversion_status store(const char *to, uint length,
2075  const CHARSET_INFO *cs)
2076  {
2077  null[0]= 1;
2078  return TYPE_OK;
2079  }
2080  type_conversion_status store(double nr) { null[0]=1; return TYPE_OK; }
2081  type_conversion_status store(longlong nr, bool unsigned_val)
2082  {
2083  null[0]=1;
2084  return TYPE_OK;
2085  }
2086  type_conversion_status store_decimal(const my_decimal *d)
2087  {
2088  null[0]=1;
2089  return TYPE_OK;
2090  }
2091  type_conversion_status reset(void) { return TYPE_OK; }
2092  double val_real(void) { return 0.0;}
2093  longlong val_int(void) { return 0;}
2094  my_decimal *val_decimal(my_decimal *) { return 0; }
2095  String *val_str(String *value,String *value2)
2096  { value2->length(0); return value2;}
2097  int cmp(const uchar *a, const uchar *b) { return 0;}
2098  void make_sort_key(uchar *buff, uint length) {}
2099  uint32 pack_length() const { return 0; }
2100  void sql_type(String &str) const;
2101  uint32 max_display_length() { return 4; }
2102  Field_null *clone(MEM_ROOT *mem_root) const {
2103  DBUG_ASSERT(type() == MYSQL_TYPE_NULL);
2104  return new (mem_root) Field_null(*this);
2105  }
2106  Field_null *clone() const {
2107  DBUG_ASSERT(type() == MYSQL_TYPE_NULL);
2108  return new Field_null(*this);
2109  }
2110 };
2111 
2112 
2113 /*
2114  Abstract class for TIME, DATE, DATETIME, TIMESTAMP
2115  with and without fractional part.
2116 */
2117 class Field_temporal :public Field {
2118 protected:
2119  uint8 dec; // Number of fractional digits
2120 
2124  uint8 normalize_dec(uint8 dec_arg)
2125  { return dec_arg == NOT_FIXED_DEC ? DATETIME_MAX_DECIMALS : dec_arg; }
2126 
2137  virtual type_conversion_status store_internal(const MYSQL_TIME *ltime,
2138  int *error)= 0;
2139 
2149  virtual type_conversion_status store_internal_with_round(MYSQL_TIME *ltime,
2150  int *warnings)= 0;
2151 
2161  type_conversion_status store_lldiv_t(const lldiv_t *lld, int *warning);
2162 
2174  virtual bool convert_str_to_TIME(const char *str, uint len,
2175  const CHARSET_INFO *cs,
2176  MYSQL_TIME *ltime,
2177  MYSQL_TIME_STATUS *status)= 0;
2191  virtual type_conversion_status convert_number_to_TIME(longlong nr,
2192  bool unsigned_val,
2193  int nanoseconds,
2194  MYSQL_TIME *ltime,
2195  int *warning)= 0;
2196 
2206  longlong convert_number_to_datetime(longlong nr, bool unsigned_val,
2207  MYSQL_TIME *ltime, int *warning);
2208 
2218  void set_warnings(ErrConvString str, int warnings);
2219 
2233  virtual ulonglong date_flags(const THD *thd)
2234  {
2235  return 0;
2236  }
2242  inline ulonglong date_flags()
2243  {
2244  return date_flags(table ? table->in_use : current_thd);
2245  }
2246 
2256  void set_datetime_warning(Sql_condition::enum_warning_level level, uint code,
2257  ErrConvString str,
2258  timestamp_type ts_type, int cuted_increment);
2259 public:
2270  Field_temporal(uchar *ptr_arg,
2271  uchar *null_ptr_arg, uchar null_bit_arg,
2272  enum utype unireg_check_arg, const char *field_name_arg,
2273  uint32 len_arg, uint8 dec_arg)
2274  :Field(ptr_arg,
2275  len_arg + ((dec= normalize_dec(dec_arg)) ? dec + 1 : 0),
2276  null_ptr_arg, null_bit_arg,
2277  unireg_check_arg, field_name_arg)
2278  { flags|= BINARY_FLAG; }
2286  Field_temporal(bool maybe_null_arg, const char *field_name_arg,
2287  uint32 len_arg, uint8 dec_arg)
2288  :Field((uchar *) 0,
2289  len_arg + ((dec= normalize_dec(dec_arg)) ? dec + 1 : 0),
2290  maybe_null_arg ? (uchar *) "" : 0, 0,
2291  NONE, field_name_arg)
2292  { flags|= BINARY_FLAG; }
2293  virtual Item_result result_type() const { return STRING_RESULT; }
2294  virtual uint32 max_display_length() { return field_length; }
2295  virtual bool str_needs_quotes() { return TRUE; }
2296  virtual uint is_equal(Create_field *new_field);
2297  Item_result numeric_context_result_type() const
2298  {
2299  return dec ? DECIMAL_RESULT : INT_RESULT;
2300  }
2301  enum Item_result cmp_type() const { return INT_RESULT; }
2302  enum Derivation derivation() const { return DERIVATION_NUMERIC; }
2303  uint repertoire() const { return MY_REPERTOIRE_NUMERIC; }
2304  const CHARSET_INFO *charset() const { return &my_charset_numeric; }
2305  bool can_be_compared_as_longlong() const { return true; }
2306  bool binary() const { return true; }
2307  type_conversion_status store(const char *str, uint len,
2308  const CHARSET_INFO *cs);
2309  type_conversion_status store_decimal(const my_decimal *decimal);
2310  type_conversion_status store(longlong nr, bool unsigned_val);
2311  type_conversion_status store(double nr);
2312  double val_real() // FSP-enable types redefine it.
2313  {
2314  return (double) val_int();
2315  }
2316  my_decimal *val_decimal(my_decimal *decimal_value); // FSP types redefine it
2317 };
2318 
2319 
2326 protected:
2331  virtual bool get_date_internal(MYSQL_TIME *ltime)= 0;
2332 
2338  bool get_internal_check_zero(MYSQL_TIME *ltime, uint fuzzydate);
2339 
2340  type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
2341  int nanoseconds,
2342  MYSQL_TIME *ltime,
2343  int *warning);
2344  bool convert_str_to_TIME(const char *str, uint len, const CHARSET_INFO *cs,
2345  MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status);
2346  type_conversion_status store_internal_with_round(MYSQL_TIME *ltime,
2347  int *warnings);
2348 public:
2359  Field_temporal_with_date(uchar *ptr_arg, uchar *null_ptr_arg,
2360  uchar null_bit_arg,
2361  enum utype unireg_check_arg,
2362  const char *field_name_arg,
2363  uint8 int_length_arg, uint8 dec_arg)
2364  :Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg,
2365  unireg_check_arg, field_name_arg,
2366  int_length_arg, dec_arg)
2367  { }
2375  Field_temporal_with_date(bool maybe_null_arg, const char *field_name_arg,
2376  uint int_length_arg, uint8 dec_arg)
2377  :Field_temporal((uchar*) 0, maybe_null_arg ? (uchar*) "": 0, 0,
2378  NONE, field_name_arg, int_length_arg, dec_arg)
2379  { }
2380  bool send_binary(Protocol *protocol);
2381  type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
2382  String *val_str(String *, String *);
2383  longlong val_time_temporal();
2384  longlong val_date_temporal();
2385  bool get_time(MYSQL_TIME *ltime)
2386  {
2387  return get_date(ltime, TIME_FUZZY_DATE);
2388  }
2389  /* Validate the value stored in a field */
2390  virtual type_conversion_status validate_stored_val(THD *thd);
2391 };
2392 
2393 
2400 private:
2401  int do_save_field_metadata(uchar *metadata_ptr)
2402  {
2403  if (decimals())
2404  {
2405  *metadata_ptr= decimals();
2406  return 1;
2407  }
2408  return 0;
2409  }
2410 protected:
2417  void init_timestamp_flags();
2423  virtual void store_timestamp_internal(const struct timeval *tm)= 0;
2424  bool convert_TIME_to_timestamp(THD *thd, const MYSQL_TIME *ltime,
2425  struct timeval *tm, int *error);
2426 
2427 public:
2437  Field_temporal_with_date_and_time(uchar *ptr_arg, uchar *null_ptr_arg,
2438  uchar null_bit_arg,
2439  enum utype unireg_check_arg,
2440  const char *field_name_arg,
2441  uint8 dec_arg)
2442  :Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
2443  unireg_check_arg, field_name_arg,
2444  MAX_DATETIME_WIDTH, dec_arg)
2445  { }
2446  void store_timestamp(const struct timeval *tm);
2447 };
2448 
2449 
2456 private:
2457  int do_save_field_metadata(uchar *metadata_ptr)
2458  {
2459  *metadata_ptr= decimals();
2460  return 1;
2461  }
2462 public:
2472  Field_temporal_with_date_and_timef(uchar *ptr_arg, uchar *null_ptr_arg,
2473  uchar null_bit_arg,
2474  enum utype unireg_check_arg,
2475  const char *field_name_arg,
2476  uint8 dec_arg)
2477  :Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
2478  unireg_check_arg, field_name_arg,
2479  dec_arg)
2480  { }
2488  const char *field_name_arg,
2489  uint8 dec_arg)
2490  :Field_temporal_with_date_and_time((uchar *) 0,
2491  maybe_null_arg ? (uchar*) "" : 0, 0,
2492  NONE, field_name_arg, dec_arg)
2493  { }
2494 
2495  uint decimals() const { return dec; }
2496  const CHARSET_INFO *sort_charset() const { return &my_charset_bin; }
2497  void make_sort_key(uchar *to, uint length) { memcpy(to, ptr, length); }
2498  int cmp(const uchar *a_ptr, const uchar *b_ptr)
2499  {
2500  return memcmp(a_ptr, b_ptr, pack_length());
2501  }
2502  uint row_pack_length() const { return pack_length(); }
2503  double val_real();
2504  longlong val_int();
2505  my_decimal *val_decimal(my_decimal *decimal_value);
2506 };
2507 
2508 
2509 /*
2510  Field implementing TIMESTAMP data type without fractional seconds.
2511  We will be removed eventually.
2512 */
2514 protected:
2515  ulonglong date_flags(const THD *thd);
2516  type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
2517  bool get_date_internal(MYSQL_TIME *ltime);
2518  void store_timestamp_internal(const struct timeval *tm);
2519 public:
2520  static const int PACK_LENGTH= 4;
2521  Field_timestamp(uchar *ptr_arg, uint32 len_arg,
2522  uchar *null_ptr_arg, uchar null_bit_arg,
2523  enum utype unireg_check_arg, const char *field_name_arg);
2524  Field_timestamp(bool maybe_null_arg, const char *field_name_arg);
2525  enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;}
2526  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
2527  type_conversion_status store_packed(longlong nr);
2528  type_conversion_status reset(void)
2529  {
2530  ptr[0]=ptr[1]=ptr[2]=ptr[3]=0;
2531  return TYPE_OK;
2532  }
2533  longlong val_int(void);
2534  int cmp(const uchar *,const uchar *);
2535  void make_sort_key(uchar *buff, uint length);
2536  uint32 pack_length() const { return PACK_LENGTH; }
2537  void sql_type(String &str) const;
2538  bool zero_pack() const { return 0; }
2539  /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
2540  bool get_timestamp(struct timeval *tm, int *warnings);
2541  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
2542  Field_timestamp *clone(MEM_ROOT *mem_root) const {
2543  DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
2544  return new (mem_root) Field_timestamp(*this);
2545  }
2547  {
2548  DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
2549  return new Field_timestamp(*this);
2550  }
2551  uchar *pack(uchar *to, const uchar *from,
2552  uint max_length __attribute__((unused)), bool low_byte_first)
2553  {
2554  return pack_int32(to, from, low_byte_first);
2555  }
2556  const uchar *unpack(uchar* to, const uchar *from,
2557  uint param_data __attribute__((unused)),
2558  bool low_byte_first)
2559  {
2560  return unpack_int32(to, from, low_byte_first);
2561  }
2562  /* Validate the value stored in a field */
2563  virtual type_conversion_status validate_stored_val(THD *thd);
2564 };
2565 
2566 
2567 /*
2568  Field implementing TIMESTAMP(N) data type, where N=0..6.
2569 */
2571 protected:
2572  bool get_date_internal(MYSQL_TIME *ltime);
2573  type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
2574  ulonglong date_flags(const THD *thd);
2575  void store_timestamp_internal(const struct timeval *tm);
2576 public:
2577  static const int PACK_LENGTH= 8;
2588  Field_timestampf(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2589  enum utype unireg_check_arg, const char *field_name_arg,
2590  uint8 dec_arg);
2597  Field_timestampf(bool maybe_null_arg, const char *field_name_arg,
2598  uint8 dec_arg);
2599  Field_timestampf *clone(MEM_ROOT *mem_root) const
2600  {
2601  DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
2602  return new (mem_root) Field_timestampf(*this);
2603  }
2605  {
2606  DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
2607  return new Field_timestampf(*this);
2608  }
2609 
2610  enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP; }
2611  enum_field_types real_type() const { return MYSQL_TYPE_TIMESTAMP2; }
2612  enum_field_types binlog_type() const { return MYSQL_TYPE_TIMESTAMP2; }
2613  bool zero_pack() const { return 0; }
2614 
2615  uint32 pack_length() const
2616  {
2617  return my_timestamp_binary_length(dec);
2618  }
2619  virtual uint pack_length_from_metadata(uint field_metadata)
2620  {
2621  DBUG_ENTER("Field_timestampf::pack_length_from_metadata");
2622  uint tmp= my_timestamp_binary_length(field_metadata);
2623  DBUG_RETURN(tmp);
2624  }
2625 
2626  type_conversion_status reset();
2627  type_conversion_status store_packed(longlong nr);
2628  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
2629  void sql_type(String &str) const;
2630 
2631  bool get_timestamp(struct timeval *tm, int *warnings);
2632  /* Validate the value stored in a field */
2633  virtual type_conversion_status validate_stored_val(THD *thd);
2634 };
2635 
2636 
2637 class Field_year :public Field_tiny {
2638 public:
2639  Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2640  uchar null_bit_arg,
2641  enum utype unireg_check_arg, const char *field_name_arg)
2642  :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2643  unireg_check_arg, field_name_arg, 1, 1)
2644  {}
2645  enum_field_types type() const { return MYSQL_TYPE_YEAR;}
2646  type_conversion_status store(const char *to,uint length,
2647  const CHARSET_INFO *charset);
2648  type_conversion_status store(double nr);
2649  type_conversion_status store(longlong nr, bool unsigned_val);
2650  type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
2651  double val_real(void);
2652  longlong val_int(void);
2653  String *val_str(String*,String *);
2654  bool send_binary(Protocol *protocol);
2655  void sql_type(String &str) const;
2656  bool can_be_compared_as_longlong() const { return true; }
2657  Field_year *clone(MEM_ROOT *mem_root) const {
2658  DBUG_ASSERT(type() == MYSQL_TYPE_YEAR);
2659  return new (mem_root) Field_year(*this);
2660  }
2661  Field_year *clone() const {
2662  DBUG_ASSERT(type() == MYSQL_TYPE_YEAR);
2663  return new Field_year(*this);
2664  }
2665 };
2666 
2667 
2669 protected:
2670  static const int PACK_LENGTH= 3;
2671  ulonglong date_flags(const THD *thd);
2672  bool get_date_internal(MYSQL_TIME *ltime);
2673  type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
2674 
2675 public:
2676  Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2677  enum utype unireg_check_arg, const char *field_name_arg)
2678  :Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
2679  unireg_check_arg, field_name_arg,
2680  MAX_DATE_WIDTH, 0)
2681  { }
2682  Field_newdate(bool maybe_null_arg, const char *field_name_arg)
2683  :Field_temporal_with_date((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0,
2684  0, NONE, field_name_arg, MAX_DATE_WIDTH, 0)
2685  { }
2686  enum_field_types type() const { return MYSQL_TYPE_DATE;}
2687  enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; }
2688  enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
2689  type_conversion_status reset(void)
2690  {
2691  ptr[0]=ptr[1]=ptr[2]=0;
2692  return TYPE_OK;
2693  }
2694  type_conversion_status store_packed(longlong nr);
2695  longlong val_int(void);
2696  longlong val_time_temporal();
2697  longlong val_date_temporal();
2698  String *val_str(String*,String *);
2699  bool send_binary(Protocol *protocol);
2700  int cmp(const uchar *,const uchar *);
2701  void make_sort_key(uchar *buff, uint length);
2702  uint32 pack_length() const { return PACK_LENGTH; }
2703  void sql_type(String &str) const;
2704  bool zero_pack() const { return 1; }
2705  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
2706  Field_newdate *clone(MEM_ROOT *mem_root) const
2707  {
2708  DBUG_ASSERT(type() == MYSQL_TYPE_DATE);
2709  DBUG_ASSERT(real_type() == MYSQL_TYPE_NEWDATE);
2710  return new (mem_root) Field_newdate(*this);
2711  }
2713  {
2714  DBUG_ASSERT(type() == MYSQL_TYPE_DATE);
2715  DBUG_ASSERT(real_type() == MYSQL_TYPE_NEWDATE);
2716  return new Field_newdate(*this);
2717  }
2718 };
2719 
2720 
2725 protected:
2726  bool convert_str_to_TIME(const char *str, uint len, const CHARSET_INFO *cs,
2727  MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status);
2733  type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
2734  int nanoseconds,
2735  MYSQL_TIME *ltime,
2736  int *warning);
2741  virtual type_conversion_status store_internal(const MYSQL_TIME *ltime,
2742  int *error)= 0;
2747  virtual type_conversion_status store_internal_with_round(MYSQL_TIME *ltime,
2748  int *warnings);
2749 public:
2759  Field_time_common(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2760  enum utype unireg_check_arg, const char *field_name_arg,
2761  uint8 dec_arg)
2762  :Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg,
2763  unireg_check_arg, field_name_arg,
2764  MAX_TIME_WIDTH, dec_arg)
2765  { }
2772  Field_time_common(bool maybe_null_arg, const char *field_name_arg,
2773  uint8 dec_arg)
2774  :Field_temporal((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0, 0,
2775  NONE, field_name_arg, MAX_TIME_WIDTH, dec_arg)
2776  { }
2777  type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
2778  String *val_str(String*, String *);
2779  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
2780  longlong val_date_temporal();
2781  bool send_binary(Protocol *protocol);
2782 };
2783 
2784 
2785 /*
2786  Field implementing TIME data type without fractional seconds.
2787  We will be removed eventually.
2788 */
2790 protected:
2791  type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
2792 public:
2793  Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2794  enum utype unireg_check_arg, const char *field_name_arg)
2795  :Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg,
2796  unireg_check_arg, field_name_arg, 0)
2797  { }
2798  Field_time(bool maybe_null_arg, const char *field_name_arg)
2799  :Field_time_common((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0, 0,
2800  NONE, field_name_arg, 0)
2801  { }
2802  enum_field_types type() const { return MYSQL_TYPE_TIME;}
2803  enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
2804  type_conversion_status store_packed(longlong nr);
2805  type_conversion_status reset(void)
2806  {
2807  ptr[0]=ptr[1]=ptr[2]=0;
2808  return TYPE_OK;
2809  }
2810  longlong val_int(void);
2811  longlong val_time_temporal();
2812  bool get_time(MYSQL_TIME *ltime);
2813  int cmp(const uchar *,const uchar *);
2814  void make_sort_key(uchar *buff, uint length);
2815  uint32 pack_length() const { return 3; }
2816  void sql_type(String &str) const;
2817  bool zero_pack() const { return 1; }
2818  Field_time *clone(MEM_ROOT *mem_root) const {
2819  DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
2820  return new (mem_root) Field_time(*this);
2821  }
2822  Field_time *clone() const {
2823  DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
2824  return new Field_time(*this);
2825  }
2826 };
2827 
2828 
2829 /*
2830  Field implementing TIME(N) data type, where N=0..6.
2831 */
2833 private:
2834  int do_save_field_metadata(uchar *metadata_ptr)
2835  {
2836  *metadata_ptr= decimals();
2837  return 1;
2838  }
2839 protected:
2840  type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
2841 public:
2851  Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2852  enum utype unireg_check_arg, const char *field_name_arg,
2853  uint8 dec_arg)
2854  :Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg,
2855  unireg_check_arg, field_name_arg, dec_arg)
2856  { }
2863  Field_timef(bool maybe_null_arg, const char *field_name_arg, uint8 dec_arg)
2864  :Field_time_common((uchar *) 0, maybe_null_arg ? (uchar *) "" : 0, 0,
2865  NONE, field_name_arg, dec_arg)
2866  { }
2867  Field_timef *clone(MEM_ROOT *mem_root) const
2868  {
2869  DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
2870  return new (mem_root) Field_timef(*this);
2871  }
2873  {
2874  DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
2875  return new Field_timef(*this);
2876  }
2877  uint decimals() const { return dec; }
2878  enum_field_types type() const { return MYSQL_TYPE_TIME;}
2879  enum_field_types real_type() const { return MYSQL_TYPE_TIME2; }
2880  enum_field_types binlog_type() const { return MYSQL_TYPE_TIME2; }
2881  type_conversion_status store_packed(longlong nr);
2882  type_conversion_status reset();
2883  double val_real();
2884  longlong val_int();
2885  longlong val_time_temporal();
2886  bool get_time(MYSQL_TIME *ltime);
2887  my_decimal *val_decimal(my_decimal *);
2888  uint32 pack_length() const
2889  {
2890  return my_time_binary_length(dec);
2891  }
2892  virtual uint pack_length_from_metadata(uint field_metadata)
2893  {
2894  DBUG_ENTER("Field_timef::pack_length_from_metadata");
2895  uint tmp= my_time_binary_length(field_metadata);
2896  DBUG_RETURN(tmp);
2897  }
2898  uint row_pack_length() const { return pack_length(); }
2899  void sql_type(String &str) const;
2900  bool zero_pack() const { return 1; }
2901  const CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
2902  void make_sort_key(uchar *to, uint length) { memcpy(to, ptr, length); }
2903  int cmp(const uchar *a_ptr, const uchar *b_ptr)
2904  {
2905  return memcmp(a_ptr, b_ptr, pack_length());
2906  }
2907 };
2908 
2909 
2910 /*
2911  Field implementing DATETIME data type without fractional seconds.
2912  We will be removed eventually.
2913 */
2915 protected:
2916  type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
2917  bool get_date_internal(MYSQL_TIME *ltime);
2918  ulonglong date_flags(const THD *thd);
2919  void store_timestamp_internal(const struct timeval *tm);
2920 
2921 public:
2922  static const int PACK_LENGTH= 8;
2923 
2949  Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2950  enum utype unireg_check_arg, const char *field_name_arg)
2951  :Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
2952  unireg_check_arg, field_name_arg, 0)
2953  {}
2954  Field_datetime(bool maybe_null_arg, const char *field_name_arg)
2955  :Field_temporal_with_date_and_time((uchar *) 0,
2956  maybe_null_arg ? (uchar *) "" : 0,
2957  0, NONE, field_name_arg, 0)
2958  {}
2959  enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
2960 #ifdef HAVE_LONG_LONG
2961  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
2962 #endif
2963  using Field_temporal_with_date_and_time::store; // Make -Woverloaded-virtual
2964  type_conversion_status store(longlong nr, bool unsigned_val);
2965  type_conversion_status store_packed(longlong nr);
2966  type_conversion_status reset(void)
2967  {
2968  ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
2969  return TYPE_OK;
2970  }
2971  longlong val_int(void);
2972  String *val_str(String*,String *);
2973  int cmp(const uchar *,const uchar *);
2974  void make_sort_key(uchar *buff, uint length);
2975  uint32 pack_length() const { return PACK_LENGTH; }
2976  void sql_type(String &str) const;
2977  bool zero_pack() const { return 1; }
2978  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
2979  Field_datetime *clone(MEM_ROOT *mem_root) const
2980  {
2981  DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
2982  return new (mem_root) Field_datetime(*this);
2983  }
2985  {
2986  DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
2987  return new Field_datetime(*this);
2988  }
2989  uchar *pack(uchar* to, const uchar *from,
2990  uint max_length __attribute__((unused)), bool low_byte_first)
2991  {
2992  return pack_int64(to, from, low_byte_first);
2993  }
2994  const uchar *unpack(uchar* to, const uchar *from,
2995  uint param_data __attribute__((unused)),
2996  bool low_byte_first)
2997  {
2998  return unpack_int64(to, from, low_byte_first);
2999  }
3000 };
3001 
3002 
3003 /*
3004  Field implementing DATETIME(N) data type, where N=0..6.
3005 */
3007 protected:
3008  bool get_date_internal(MYSQL_TIME *ltime);
3009  type_conversion_status store_internal(const MYSQL_TIME *ltime, int *error);
3010  ulonglong date_flags(const THD *thd);
3011  void store_timestamp_internal(const struct timeval *tm);
3012 
3013 public:
3023  Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3024  enum utype unireg_check_arg, const char *field_name_arg,
3025  uint8 dec_arg)
3026  :Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
3027  unireg_check_arg, field_name_arg,
3028  dec_arg)
3029  {}
3037  Field_datetimef(bool maybe_null_arg, const char *field_name_arg,
3038  uint8 dec_arg)
3040  maybe_null_arg ? (uchar *) "" : 0, 0,
3041  NONE, field_name_arg, dec_arg)
3042  {}
3043  Field_datetimef *clone(MEM_ROOT *mem_root) const
3044  {
3045  DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
3046  return new (mem_root) Field_datetimef(*this);
3047  }
3049  {
3050  DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
3051  return new Field_datetimef(*this);
3052  }
3053 
3054  enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
3055  enum_field_types real_type() const { return MYSQL_TYPE_DATETIME2; }
3056  enum_field_types binlog_type() const { return MYSQL_TYPE_DATETIME2; }
3057  uint32 pack_length() const
3058  {
3059  return my_datetime_binary_length(dec);
3060  }
3061  virtual uint pack_length_from_metadata(uint field_metadata)
3062  {
3063  DBUG_ENTER("Field_datetimef::pack_length_from_metadata");
3064  uint tmp= my_datetime_binary_length(field_metadata);
3065  DBUG_RETURN(tmp);
3066  }
3067  bool zero_pack() const { return 1; }
3068 
3069  type_conversion_status store_packed(longlong nr);
3070  type_conversion_status reset();
3071  longlong val_date_temporal();
3072  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
3073  void sql_type(String &str) const;
3074 };
3075 
3076 
3078 public:
3079  bool can_alter_field_type;
3080  Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
3081  uchar null_bit_arg,
3082  enum utype unireg_check_arg, const char *field_name_arg,
3083  const CHARSET_INFO *cs)
3084  :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3085  unireg_check_arg, field_name_arg, cs),
3086  can_alter_field_type(1) {};
3087  Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
3088  const CHARSET_INFO *cs)
3089  :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3090  NONE, field_name_arg, cs),
3091  can_alter_field_type(1) {};
3092 
3093  enum_field_types type() const
3094  {
3095  return ((can_alter_field_type && orig_table &&
3096  orig_table->s->db_create_options & HA_OPTION_PACK_RECORD &&
3097  field_length >= 4) &&
3098  orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR ?
3099  MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING);
3100  }
3101  bool match_collation_to_optimize_range() const { return true; }
3102  enum ha_base_keytype key_type() const
3103  { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
3104  bool zero_pack() const { return 0; }
3105  type_conversion_status reset(void)
3106  {
3107  charset()->cset->fill(charset(),(char*) ptr, field_length,
3108  (has_charset() ? ' ' : 0));
3109  return TYPE_OK;
3110  }
3111  type_conversion_status store(const char *to,uint length,
3112  const CHARSET_INFO *charset);
3113  type_conversion_status store(longlong nr, bool unsigned_val);
3114  /* QQ: To be deleted */
3115  type_conversion_status store(double nr) { return Field_str::store(nr); }
3116  double val_real(void);
3117  longlong val_int(void);
3118  String *val_str(String*,String *);
3119  my_decimal *val_decimal(my_decimal *);
3120  int cmp(const uchar *,const uchar *);
3121  void make_sort_key(uchar *buff, uint length);
3122  void sql_type(String &str) const;
3123  virtual uchar *pack(uchar *to, const uchar *from,
3124  uint max_length, bool low_byte_first);
3125  virtual const uchar *unpack(uchar* to, const uchar *from,
3126  uint param_data, bool low_byte_first);
3127  uint pack_length_from_metadata(uint field_metadata)
3128  {
3129  DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
3130  if (field_metadata == 0)
3131  return row_pack_length();
3132  return (((field_metadata >> 4) & 0x300) ^ 0x300) + (field_metadata & 0x00ff);
3133  }
3134  bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
3135  uint16 mflags, int *order_var);
3136  uint row_pack_length() const { return field_length; }
3137  int pack_cmp(const uchar *a,const uchar *b,uint key_length,
3138  my_bool insert_or_update);
3139  int pack_cmp(const uchar *b,uint key_length,my_bool insert_or_update);
3140  uint packed_col_length(const uchar *to, uint length);
3141  uint max_packed_col_length(uint max_length);
3142  enum_field_types real_type() const { return MYSQL_TYPE_STRING; }
3143  bool has_charset(void) const
3144  { return charset() == &my_charset_bin ? FALSE : TRUE; }
3145  Field *new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
3146  Field_string *clone(MEM_ROOT *mem_root) const {
3147  DBUG_ASSERT(real_type() == MYSQL_TYPE_STRING);
3148  return new (mem_root) Field_string(*this);
3149  }
3150  Field_string *clone() const {
3151  DBUG_ASSERT(real_type() == MYSQL_TYPE_STRING);
3152  return new Field_string(*this);
3153  }
3154  virtual uint get_key_image(uchar *buff,uint length, imagetype type);
3155 private:
3156  int do_save_field_metadata(uchar *first_byte);
3157 };
3158 
3159 
3161 public:
3162  /*
3163  The maximum space available in a Field_varstring, in bytes. See
3164  length_bytes.
3165  */
3166  static const uint MAX_SIZE;
3167  /* Store number of bytes used to store length (1 or 2) */
3168  uint32 length_bytes;
3169  Field_varstring(uchar *ptr_arg,
3170  uint32 len_arg, uint length_bytes_arg,
3171  uchar *null_ptr_arg, uchar null_bit_arg,
3172  enum utype unireg_check_arg, const char *field_name_arg,
3173  TABLE_SHARE *share, const CHARSET_INFO *cs)
3174  :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3175  unireg_check_arg, field_name_arg, cs),
3176  length_bytes(length_bytes_arg)
3177  {
3178  share->varchar_fields++;
3179  }
3180  Field_varstring(uint32 len_arg,bool maybe_null_arg,
3181  const char *field_name_arg,
3182  TABLE_SHARE *share, const CHARSET_INFO *cs)
3183  :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3184  NONE, field_name_arg, cs),
3185  length_bytes(len_arg < 256 ? 1 :2)
3186  {
3187  share->varchar_fields++;
3188  }
3189 
3190  enum_field_types type() const { return MYSQL_TYPE_VARCHAR; }
3191  bool match_collation_to_optimize_range() const { return true; }
3192  enum ha_base_keytype key_type() const;
3193  uint row_pack_length() const { return field_length; }
3194  bool zero_pack() const { return 0; }
3195  type_conversion_status reset(void)
3196  {
3197  memset(ptr, 0, field_length+length_bytes);
3198  return TYPE_OK;
3199  }
3200  uint32 pack_length() const { return (uint32) field_length+length_bytes; }
3201  uint32 key_length() const { return (uint32) field_length; }
3202  uint32 sort_length() const
3203  {
3204  return (uint32) field_length + (field_charset == &my_charset_bin ?
3205  length_bytes : 0);
3206  }
3207  type_conversion_status store(const char *to,uint length,
3208  const CHARSET_INFO *charset);
3209  type_conversion_status store(longlong nr, bool unsigned_val);
3210  /* QQ: To be deleted */
3211  type_conversion_status store(double nr) { return Field_str::store(nr); }
3212  double val_real(void);
3213  longlong val_int(void);
3214  String *val_str(String*,String *);
3215  my_decimal *val_decimal(my_decimal *);
3216  int cmp_max(const uchar *, const uchar *, uint max_length);
3217  int cmp(const uchar *a,const uchar *b)
3218  {
3219  return cmp_max(a, b, ~0L);
3220  }
3221  void make_sort_key(uchar *buff, uint length);
3222  uint get_key_image(uchar *buff,uint length, imagetype type);
3223  void set_key_image(const uchar *buff,uint length);
3224  void sql_type(String &str) const;
3225  virtual uchar *pack(uchar *to, const uchar *from,
3226  uint max_length, bool low_byte_first);
3227  virtual const uchar *unpack(uchar* to, const uchar *from,
3228  uint param_data, bool low_byte_first);
3229  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
3230  int key_cmp(const uchar *,const uchar*);
3231  int key_cmp(const uchar *str, uint length);
3232  uint packed_col_length(const uchar *to, uint length);
3233  uint max_packed_col_length(uint max_length);
3234  uint32 data_length();
3235  enum_field_types real_type() const { return MYSQL_TYPE_VARCHAR; }
3236  bool has_charset(void) const
3237  { return charset() == &my_charset_bin ? FALSE : TRUE; }
3238  Field *new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
3239  Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
3240  uchar *new_ptr, uchar *new_null_ptr,
3241  uint new_null_bit);
3242  Field_varstring *clone(MEM_ROOT *mem_root) const {
3243  DBUG_ASSERT(type() == MYSQL_TYPE_VARCHAR);
3244  DBUG_ASSERT(real_type() == MYSQL_TYPE_VARCHAR);
3245  return new (mem_root) Field_varstring(*this);
3246  }
3248  DBUG_ASSERT(type() == MYSQL_TYPE_VARCHAR);
3249  DBUG_ASSERT(real_type() == MYSQL_TYPE_VARCHAR);
3250  return new Field_varstring(*this);
3251  }
3252  uint is_equal(Create_field *new_field);
3253  void hash(ulong *nr, ulong *nr2);
3254 private:
3255  int do_save_field_metadata(uchar *first_byte);
3256 };
3257 
3258 
3259 class Field_blob :public Field_longstr {
3260  virtual type_conversion_status store_internal(const char *from, uint length,
3261  const CHARSET_INFO *cs);
3265  type_conversion_status store_to_mem(const char *from, uint length,
3266  const CHARSET_INFO *cs,
3267  uint max_length,
3268  Blob_mem_storage *blob_storage);
3269 protected:
3274 
3279 
3283  void store_ptr_and_length(const char *from, uint32 length)
3284  {
3285  store_length(length);
3286  bmove(ptr + packlength, &from, sizeof(char *));
3287  }
3288 
3289 public:
3290  Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3291  enum utype unireg_check_arg, const char *field_name_arg,
3292  TABLE_SHARE *share, uint blob_pack_length, const CHARSET_INFO *cs);
3293  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
3294  const CHARSET_INFO *cs)
3295  :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3296  NONE, field_name_arg, cs),
3297  packlength(4)
3298  {
3299  flags|= BLOB_FLAG;
3300  }
3301  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
3302  const CHARSET_INFO *cs, bool set_packlength)
3303  :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
3304  NONE, field_name_arg, cs)
3305  {
3306  flags|= BLOB_FLAG;
3307  packlength= 4;
3308  if (set_packlength)
3309  {
3310  uint32 l_char_length= len_arg/cs->mbmaxlen;
3311  packlength= l_char_length <= 255 ? 1 :
3312  l_char_length <= 65535 ? 2 :
3313  l_char_length <= 16777215 ? 3 : 4;
3314  }
3315  }
3316  Field_blob(uint32 packlength_arg)
3317  :Field_longstr((uchar*) 0, 0, (uchar*) "", 0, NONE, "temp", system_charset_info),
3318  packlength(packlength_arg) {}
3319  /* Note that the default copy constructor is used, in clone() */
3320  enum_field_types type() const { return MYSQL_TYPE_BLOB;}
3321  bool match_collation_to_optimize_range() const { return true; }
3322  enum ha_base_keytype key_type() const
3323  { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
3324  type_conversion_status store(const char *to, uint length,
3325  const CHARSET_INFO *charset);
3326  type_conversion_status store(double nr);
3327  type_conversion_status store(longlong nr, bool unsigned_val);
3328  double val_real(void);
3329  longlong val_int(void);
3330  String *val_str(String*,String *);
3331  my_decimal *val_decimal(my_decimal *);
3332  int cmp_max(const uchar *, const uchar *, uint max_length);
3333  int cmp(const uchar *a,const uchar *b)
3334  { return cmp_max(a, b, ~0L); }
3335  int cmp(const uchar *a, uint32 a_length, const uchar *b, uint32 b_length);
3336  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
3337  int key_cmp(const uchar *,const uchar*);
3338  int key_cmp(const uchar *str, uint length);
3339  uint32 key_length() const { return 0; }
3340  void make_sort_key(uchar *buff, uint length);
3341  uint32 pack_length() const
3342  { return (uint32) (packlength + portable_sizeof_char_ptr); }
3343 
3352  uint32 pack_length_no_ptr() const
3353  { return (uint32) (packlength); }
3354  uint row_pack_length() const { return pack_length_no_ptr(); }
3355  uint32 sort_length() const;
3356  virtual uint32 max_data_length() const
3357  {
3358  return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
3359  }
3360  type_conversion_status reset(void)
3361  {
3362  memset(ptr, 0, packlength+sizeof(uchar*));
3363  return TYPE_OK;
3364  }
3365  void reset_fields() { memset(&value, 0, sizeof(value)); }
3366  uint32 get_field_buffer_size(void) { return value.alloced_length(); }
3367 #ifndef WORDS_BIGENDIAN
3368  static
3369 #endif
3370  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number, bool low_byte_first);
3371  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number)
3372  {
3373  store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
3374  }
3375  inline void store_length(uint32 number)
3376  {
3377  store_length(ptr, packlength, number);
3378  }
3379 
3388  uint32 get_packed_size(const uchar *ptr_arg, bool low_byte_first)
3389  {return packlength + get_length(ptr_arg, packlength, low_byte_first);}
3390 
3391  inline uint32 get_length(uint row_offset= 0)
3392  { return get_length(ptr+row_offset, this->packlength, table->s->db_low_byte_first); }
3393  uint32 get_length(const uchar *ptr, uint packlength, bool low_byte_first);
3394  uint32 get_length(const uchar *ptr_arg)
3395  { return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first); }
3396  void put_length(uchar *pos, uint32 length);
3397  inline void get_ptr(uchar **str)
3398  {
3399  memcpy(str, ptr+packlength, sizeof(uchar*));
3400  }
3401  inline void get_ptr(uchar **str, uint row_offset)
3402  {
3403  memcpy(str, ptr+packlength+row_offset, sizeof(char*));
3404  }
3405  inline void set_ptr(uchar *length, uchar *data)
3406  {
3407  memcpy(ptr,length,packlength);
3408  memcpy(ptr+packlength, &data,sizeof(char*));
3409  }
3410  void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, uchar *data)
3411  {
3412  uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
3413  store_length(ptr_ofs, packlength, length);
3414  memcpy(ptr_ofs+packlength, &data, sizeof(char*));
3415  }
3416  inline void set_ptr(uint32 length, uchar *data)
3417  {
3418  set_ptr_offset(0, length, data);
3419  }
3420  uint get_key_image(uchar *buff,uint length, imagetype type);
3421  void set_key_image(const uchar *buff,uint length);
3422  void sql_type(String &str) const;
3423  inline bool copy()
3424  {
3425  uchar *tmp;
3426  get_ptr(&tmp);
3427  if (value.copy((char*) tmp, get_length(), charset()))
3428  {
3429  Field_blob::reset();
3430  return 1;
3431  }
3432  tmp=(uchar*) value.ptr();
3433  memcpy(ptr+packlength, &tmp, sizeof(char*));
3434  return 0;
3435  }
3436  Field_blob *clone(MEM_ROOT *mem_root) const {
3437  DBUG_ASSERT(type() == MYSQL_TYPE_BLOB);
3438  return new (mem_root) Field_blob(*this);
3439  }
3440  Field_blob *clone() const {
3441  DBUG_ASSERT(type() == MYSQL_TYPE_BLOB);
3442  return new Field_blob(*this);
3443  }
3444  virtual uchar *pack(uchar *to, const uchar *from,
3445  uint max_length, bool low_byte_first);
3446  virtual const uchar *unpack(uchar *to, const uchar *from,
3447  uint param_data, bool low_byte_first);
3448  uint packed_col_length(const uchar *col_ptr, uint length);
3449  uint max_packed_col_length(uint max_length);
3450  void free() { value.free(); }
3451  inline void clear_temporary() { memset(&value, 0, sizeof(value)); }
3452  friend type_conversion_status field_conv(Field *to,Field *from);
3453  bool has_charset(void) const
3454  { return charset() == &my_charset_bin ? FALSE : TRUE; }
3455  uint32 max_display_length();
3456  uint32 char_length();
3457  uint is_equal(Create_field *new_field);
3458  inline bool in_read_set() { return bitmap_is_set(table->read_set, field_index); }
3459  inline bool in_write_set() { return bitmap_is_set(table->write_set, field_index); }
3460 private:
3461  int do_save_field_metadata(uchar *first_byte);
3462 };
3463 
3464 
3465 #ifdef HAVE_SPATIAL
3466 class Field_geom :public Field_blob {
3467  virtual type_conversion_status store_internal(const char *from, uint length,
3468  const CHARSET_INFO *cs);
3469 public:
3470  enum geometry_type geom_type;
3471 
3472  Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
3473  enum utype unireg_check_arg, const char *field_name_arg,
3474  TABLE_SHARE *share, uint blob_pack_length,
3475  enum geometry_type geom_type_arg)
3476  :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
3477  field_name_arg, share, blob_pack_length, &my_charset_bin)
3478  { geom_type= geom_type_arg; }
3479  Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
3480  TABLE_SHARE *share, enum geometry_type geom_type_arg)
3481  :Field_blob(len_arg, maybe_null_arg, field_name_arg, &my_charset_bin)
3482  { geom_type= geom_type_arg; }
3483  enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY2; }
3484  enum_field_types type() const { return MYSQL_TYPE_GEOMETRY; }
3485  bool match_collation_to_optimize_range() const { return false; }
3486  void sql_type(String &str) const;
3487  using Field_blob::store;
3488  type_conversion_status store(double nr);
3489  type_conversion_status store(longlong nr, bool unsigned_val);
3490  type_conversion_status store_decimal(const my_decimal *);
3491 
3496  type_conversion_status reset(void)
3497  {
3498  type_conversion_status res= Field_blob::reset();
3499  if (res != TYPE_OK)
3500  return res;
3501  return maybe_null() ? TYPE_OK : TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
3502  }
3503 
3504  geometry_type get_geometry_type() { return geom_type; };
3505  Field_geom *clone(MEM_ROOT *mem_root) const {
3506  DBUG_ASSERT(type() == MYSQL_TYPE_GEOMETRY);
3507  return new (mem_root) Field_geom(*this);
3508  }
3509  Field_geom *clone() const {
3510  DBUG_ASSERT(type() == MYSQL_TYPE_GEOMETRY);
3511  return new Field_geom(*this);
3512  }
3513  uint is_equal(Create_field *new_field);
3514 };
3515 #endif /*HAVE_SPATIAL*/
3516 
3517 
3518 class Field_enum :public Field_str {
3519 protected:
3520  uint packlength;
3521 public:
3522  TYPELIB *typelib;
3523  Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3524  uchar null_bit_arg,
3525  enum utype unireg_check_arg, const char *field_name_arg,
3526  uint packlength_arg,
3527  TYPELIB *typelib_arg,
3528  const CHARSET_INFO *charset_arg)
3529  :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3530  unireg_check_arg, field_name_arg, charset_arg),
3531  packlength(packlength_arg),typelib(typelib_arg)
3532  {
3533  flags|=ENUM_FLAG;
3534  }
3535  Field *new_field(MEM_ROOT *root, TABLE *new_table, bool keep_type);
3536  enum_field_types type() const { return MYSQL_TYPE_STRING; }
3537  bool match_collation_to_optimize_range() const { return false; }
3538  enum Item_result cmp_type () const { return INT_RESULT; }
3539  enum Item_result cast_to_int_type () const { return INT_RESULT; }
3540  enum ha_base_keytype key_type() const;
3541  type_conversion_status store(const char *to,uint length,
3542  const CHARSET_INFO *charset);
3543  type_conversion_status store(double nr);
3544  type_conversion_status store(longlong nr, bool unsigned_val);
3545  double val_real(void);
3546  my_decimal *val_decimal(my_decimal *decimal_value);
3547  longlong val_int(void);
3548  String *val_str(String*,String *);
3549  int cmp(const uchar *,const uchar *);
3550  void make_sort_key(uchar *buff, uint length);
3551  uint32 pack_length() const { return (uint32) packlength; }
3552  void store_type(ulonglong value);
3553  void sql_type(String &str) const;
3554  enum_field_types real_type() const { return MYSQL_TYPE_ENUM; }
3555  uint pack_length_from_metadata(uint field_metadata)
3556  { return (field_metadata & 0x00ff); }
3557  uint row_pack_length() const { return pack_length(); }
3558  virtual bool zero_pack() const { return 0; }
3559  bool optimize_range(uint idx, uint part) { return 0; }
3560  bool eq_def(Field *field);
3561  bool has_charset(void) const { return TRUE; }
3562  /* enum and set are sorted as integers */
3563  CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
3564  Field_enum *clone(MEM_ROOT *mem_root) const {
3565  DBUG_ASSERT(real_type() == MYSQL_TYPE_ENUM);
3566  return new (mem_root) Field_enum(*this);
3567  }
3568  Field_enum *clone() const {
3569  DBUG_ASSERT(real_type() == MYSQL_TYPE_ENUM);
3570  return new Field_enum(*this);
3571  }
3572  virtual uchar *pack(uchar *to, const uchar *from,
3573  uint max_length, bool low_byte_first);
3574  virtual const uchar *unpack(uchar *to, const uchar *from,
3575  uint param_data, bool low_byte_first);
3576 
3577 private:
3578  int do_save_field_metadata(uchar *first_byte);
3579  uint is_equal(Create_field *new_field);
3580 };
3581 
3582 
3583 class Field_set :public Field_enum {
3584 public:
3585  Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3586  uchar null_bit_arg,
3587  enum utype unireg_check_arg, const char *field_name_arg,
3588  uint32 packlength_arg,
3589  TYPELIB *typelib_arg, const CHARSET_INFO *charset_arg)
3590  :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3591  unireg_check_arg, field_name_arg,
3592  packlength_arg,
3593  typelib_arg,charset_arg),
3594  empty_set_string("", 0, charset_arg)
3595  {
3596  flags= (flags & ~ENUM_FLAG) | SET_FLAG;
3597  }
3598  type_conversion_status store(const char *to, uint length,
3599  const CHARSET_INFO *charset);
3600  type_conversion_status store(double nr)
3601  {
3602  return Field_set::store((longlong) nr, FALSE);
3603  }
3604  type_conversion_status store(longlong nr, bool unsigned_val);
3605  virtual bool zero_pack() const { return 1; }
3606  String *val_str(String*,String *);
3607  void sql_type(String &str) const;
3608  enum_field_types real_type() const { return MYSQL_TYPE_SET; }
3609  bool has_charset(void) const { return TRUE; }
3610  Field_set *clone(MEM_ROOT *mem_root) const {
3611  DBUG_ASSERT(real_type() == MYSQL_TYPE_SET);
3612  return new (mem_root) Field_set(*this);
3613  }
3614  Field_set *clone() const {
3615  DBUG_ASSERT(real_type() == MYSQL_TYPE_SET);
3616  return new Field_set(*this);
3617  }
3618 private:
3619  const String empty_set_string;
3620 };
3621 
3622 
3623 /*
3624  Note:
3625  To use Field_bit::cmp_binary() you need to copy the bits stored in
3626  the beginning of the record (the NULL bytes) to each memory you
3627  want to compare (where the arguments point).
3628 
3629  This is the reason:
3630  - Field_bit::cmp_binary() is only implemented in the base class
3631  (Field::cmp_binary()).
3632  - Field::cmp_binary() currenly use pack_length() to calculate how
3633  long the data is.
3634  - pack_length() includes size of the bits stored in the NULL bytes
3635  of the record.
3636 */
3637 class Field_bit :public Field {
3638 public:
3639  uchar *bit_ptr; // position in record where 'uneven' bits store
3640  uchar bit_ofs; // offset to 'uneven' high bits
3641  uint bit_len; // number of 'uneven' high bits
3642  uint bytes_in_rec;
3643  Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3644  uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
3645  enum utype unireg_check_arg, const char *field_name_arg);
3646  enum_field_types type() const { return MYSQL_TYPE_BIT; }
3647  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
3648  uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
3649  uint32 max_data_length() const { return (field_length + 7) / 8; }
3650  uint32 max_display_length() { return field_length; }
3651  Item_result result_type () const { return INT_RESULT; }
3652  type_conversion_status reset(void)
3653  {
3654  memset(ptr, 0, bytes_in_rec);
3655  if (bit_ptr && (bit_len > 0)) // reset odd bits among null bits
3656  clr_rec_bits(bit_ptr, bit_ofs, bit_len);
3657  return TYPE_OK;
3658  }
3659  type_conversion_status store(const char *to, uint length,
3660  const CHARSET_INFO *charset);
3661  type_conversion_status store(double nr);
3662  type_conversion_status store(longlong nr, bool unsigned_val);
3663  type_conversion_status store_decimal(const my_decimal *);
3664  double val_real(void);
3665  longlong val_int(void);
3666  String *val_str(String*, String *);
3667  virtual bool str_needs_quotes() { return TRUE; }
3668  my_decimal *val_decimal(my_decimal *);
3669  int cmp(const uchar *a, const uchar *b)
3670  {
3671  DBUG_ASSERT(ptr == a || ptr == b);
3672  if (ptr == a)
3673  return Field_bit::key_cmp(b, bytes_in_rec+test(bit_len));
3674  else
3675  return Field_bit::key_cmp(a, bytes_in_rec+test(bit_len)) * -1;
3676  }
3677  int cmp_binary_offset(uint row_offset)
3678  { return cmp_offset(row_offset); }
3679  int cmp_max(const uchar *a, const uchar *b, uint max_length);
3680  int key_cmp(const uchar *a, const uchar *b)
3681  { return cmp_binary((uchar *) a, (uchar *) b); }
3682  int key_cmp(const uchar *str, uint length);
3683  int cmp_offset(uint row_offset);
3684  void get_image(uchar *buff, uint length, const CHARSET_INFO *cs)
3685  { get_key_image(buff, length, itRAW); }
3686  void set_image(const uchar *buff,uint length, const CHARSET_INFO *cs)
3687  { Field_bit::store((char *) buff, length, cs); }
3688  uint get_key_image(uchar *buff, uint length, imagetype type);
3689  void set_key_image(const uchar *buff, uint length)
3690  { Field_bit::store((char*) buff, length, &my_charset_bin); }
3691  void make_sort_key(uchar *buff, uint length)
3692  { get_key_image(buff, length, itRAW); }
3693  uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
3694  uint32 pack_length_in_rec() const { return bytes_in_rec; }
3695  uint pack_length_from_metadata(uint field_metadata);
3696  uint row_pack_length() const
3697  { return (bytes_in_rec + ((bit_len > 0) ? 1 : 0)); }
3698  bool compatible_field_size(uint metadata, Relay_log_info *rli,
3699  uint16 mflags, int *order_var);
3700  void sql_type(String &str) const;
3701  virtual uchar *pack(uchar *to, const uchar *from,
3702  uint max_length, bool low_byte_first);
3703  virtual const uchar *unpack(uchar *to, const uchar *from,
3704  uint param_data, bool low_byte_first);
3705  virtual void set_default();
3706 
3707  Field *new_key_field(MEM_ROOT *root, TABLE *new_table,
3708  uchar *new_ptr, uchar *new_null_ptr,
3709  uint new_null_bit);
3710  void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
3711  {
3712  bit_ptr= bit_ptr_arg;
3713  bit_ofs= bit_ofs_arg;
3714  }
3715  bool eq(Field *field)
3716  {
3717  return (Field::eq(field) &&
3718  bit_ptr == ((Field_bit *)field)->bit_ptr &&
3719  bit_ofs == ((Field_bit *)field)->bit_ofs);
3720  }
3721  uint is_equal(Create_field *new_field);
3722  void move_field_offset(my_ptrdiff_t ptr_diff)
3723  {
3724  Field::move_field_offset(ptr_diff);
3725  bit_ptr= ADD_TO_PTR(bit_ptr, ptr_diff, uchar*);
3726  }
3727  void hash(ulong *nr, ulong *nr2);
3728  Field_bit *clone(MEM_ROOT *mem_root) const {
3729  DBUG_ASSERT(type() == MYSQL_TYPE_BIT);
3730  return new (mem_root) Field_bit(*this);
3731  }
3732  Field_bit *clone() const {
3733  DBUG_ASSERT(type() == MYSQL_TYPE_BIT);
3734  return new Field_bit(*this);
3735  }
3736 private:
3737  virtual size_t do_last_null_byte() const;
3738  int do_save_field_metadata(uchar *first_byte);
3739 };
3740 
3741 
3750 public:
3751  Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3752  uchar null_bit_arg,
3753  enum utype unireg_check_arg, const char *field_name_arg);
3754  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
3755  type_conversion_status store(const char *to, uint length,
3756  const CHARSET_INFO *charset);
3757  type_conversion_status store(double nr) { return Field_bit::store(nr); }
3758  type_conversion_status store(longlong nr, bool unsigned_val)
3759  { return Field_bit::store(nr, unsigned_val); }
3760  void sql_type(String &str) const;
3761  Field_bit_as_char *clone(MEM_ROOT *mem_root) const {
3762  return new (mem_root) Field_bit_as_char(*this);
3763  }
3764  Field_bit_as_char *clone() const { return new Field_bit_as_char(*this); }
3765 };
3766 
3767 
3768 /*
3769  Create field class for CREATE TABLE
3770 */
3771 
3773 {
3774 public:
3775  const char *field_name;
3776  const char *change; // If done with alter table
3777  const char *after; // Put column after this one
3778  LEX_STRING comment; // Comment for field
3779 
3789  enum enum_field_types sql_type;
3790  /*
3791  At various stages in execution this can be length of field in bytes or
3792  max number of characters.
3793  */
3794  ulong length;
3795  /*
3796  The value of `length' as set by parser: is the number of characters
3797  for most of the types, or of bytes for BLOBs or numeric types.
3798  */
3799  uint32 char_length;
3800  uint decimals, flags, pack_length, key_length;
3801  Field::utype unireg_check;
3802  TYPELIB *interval; // Which interval to use
3803  TYPELIB *save_interval; // Temporary copy for the above
3804  // Used only for UCS2 intervals
3805  List<String> interval_list;
3806  const CHARSET_INFO *charset;
3807  Field::geometry_type geom_type;
3808  Field *field; // For alter table
3809 
3810  uint8 row,col,sc_length,interval_id; // For rea_create_table
3811  uint offset,pack_flag;
3812  Create_field() :after(NULL) {}
3813  Create_field(Field *field, Field *orig_field);
3814  /* Used to make a clone of this object for ALTER/CREATE TABLE */
3815  Create_field *clone(MEM_ROOT *mem_root) const
3816  { return new (mem_root) Create_field(*this); }
3818 
3819  /* Init for a tmp table field. To be extended if need be. */
3820  void init_for_tmp_table(enum_field_types sql_type_arg,
3821  uint32 max_length, uint32 decimals,
3822  bool maybe_null, bool is_unsigned,
3823  uint pack_length = ~0U);
3824 
3825  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
3826  char *decimals, uint type_modifier, Item *default_value,
3827  Item *on_update_value, LEX_STRING *comment, char *change,
3828  List<String> *interval_list, const CHARSET_INFO *cs,
3829  uint uint_geom_type);
3830 
3831  bool field_flags_are_binary()
3832  {
3833  return (flags & (BINCMP_FLAG | BINARY_FLAG)) != 0;
3834  }
3835 
3836  ha_storage_media field_storage_type() const
3837  {
3838  return (ha_storage_media)
3839  ((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
3840  }
3841 
3842  column_format_type column_format() const
3843  {
3844  return (column_format_type)
3845  ((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
3846  }
3847 };
3848 
3849 
3850 /*
3851  A class for sending info to the client
3852 */
3853 
3854 class Send_field :public Sql_alloc {
3855  public:
3856  const char *db_name;
3857  const char *table_name,*org_table_name;
3858  const char *col_name,*org_col_name;
3859  ulong length;
3860  uint charsetnr, flags, decimals;
3861  enum_field_types type;
3862  Send_field() {}
3863 };
3864 
3865 
3866 /*
3867  A class for quick copying data to fields
3868 */
3869 
3870 class Copy_field :public Sql_alloc {
3875  typedef void Copy_func(Copy_field*);
3876  Copy_func *get_copy_func(Field *to, Field *from);
3877 public:
3878  uchar *from_ptr,*to_ptr;
3879  uchar *from_null_ptr,*to_null_ptr;
3880  my_bool *null_row;
3881  uint from_bit,to_bit;
3899  uint from_length,to_length;
3900  Field *from_field,*to_field;
3901  String tmp; // For items
3902 
3903  Copy_field() {}
3904  ~Copy_field() {}
3905  void set(Field *to,Field *from,bool save); // Field to field
3906  void set(uchar *to,Field *from); // Field to string
3907  void (*do_copy)(Copy_field *);
3908  void (*do_copy2)(Copy_field *); // Used to handle null values
3909 };
3910 
3911 
3912 Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
3913  uchar *null_pos, uchar null_bit,
3914  uint pack_flag, enum_field_types field_type,
3915  const CHARSET_INFO *cs,
3916  Field::geometry_type geom_type,
3917  Field::utype unireg_check,
3918  TYPELIB *interval, const char *field_name);
3919 uint pack_length_to_packflag(uint type);
3920 enum_field_types get_blob_type_from_length(ulong length);
3921 uint32 calc_pack_length(enum_field_types type,uint32 length);
3922 type_conversion_status set_field_to_null(Field *field);
3923 type_conversion_status set_field_to_null_with_conversions(Field *field,
3924  bool no_conversions);
3925 
3926 /*
3927  The following are for the interface with the .frm file
3928 */
3929 
3930 #define FIELDFLAG_DECIMAL 1
3931 #define FIELDFLAG_BINARY 1 // Shares same flag
3932 #define FIELDFLAG_NUMBER 2
3933 #define FIELDFLAG_ZEROFILL 4
3934 #define FIELDFLAG_PACK 120 // Bits used for packing
3935 #define FIELDFLAG_INTERVAL 256 // mangled with decimals!
3936 #define FIELDFLAG_BITFIELD 512 // mangled with decimals!
3937 #define FIELDFLAG_BLOB 1024 // mangled with decimals!
3938 #define FIELDFLAG_GEOM 2048 // mangled with decimals!
3939 
3940 #define FIELDFLAG_TREAT_BIT_AS_CHAR 4096 /* use Field_bit_as_char */
3941 
3942 #define FIELDFLAG_LEFT_FULLSCREEN 8192
3943 #define FIELDFLAG_RIGHT_FULLSCREEN 16384
3944 #define FIELDFLAG_FORMAT_NUMBER 16384 // predit: ###,,## in output
3945 #define FIELDFLAG_NO_DEFAULT 16384 /* sql */
3946 #define FIELDFLAG_SUM ((uint) 32768)// predit: +#fieldflag
3947 #define FIELDFLAG_MAYBE_NULL ((uint) 32768)// sql
3948 #define FIELDFLAG_HEX_ESCAPE ((uint) 0x10000)
3949 #define FIELDFLAG_PACK_SHIFT 3
3950 #define FIELDFLAG_DEC_SHIFT 8
3951 #define FIELDFLAG_MAX_DEC 31
3952 #define FIELDFLAG_NUM_SCREEN_TYPE 0x7F01
3953 #define FIELDFLAG_ALFA_SCREEN_TYPE 0x7800
3954 
3955 #define MTYP_TYPENR(type) (type & 127) /* Remove bits from type */
3956 
3957 #define f_is_dec(x) ((x) & FIELDFLAG_DECIMAL)
3958 #define f_is_num(x) ((x) & FIELDFLAG_NUMBER)
3959 #define f_is_zerofill(x) ((x) & FIELDFLAG_ZEROFILL)
3960 #define f_is_packed(x) ((x) & FIELDFLAG_PACK)
3961 #define f_packtype(x) (((x) >> FIELDFLAG_PACK_SHIFT) & 15)
3962 #define f_decimals(x) ((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
3963 #define f_is_alpha(x) (!f_is_num(x))
3964 #define f_is_binary(x) ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
3965 #define f_is_enum(x) (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
3966 #define f_is_bitfield(x) (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
3967 #define f_is_blob(x) (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
3968 #define f_is_geom(x) (((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
3969 #define f_is_equ(x) ((x) & (1+2+FIELDFLAG_PACK+31*256))
3970 #define f_settype(x) (((int) x) << FIELDFLAG_PACK_SHIFT)
3971 #define f_maybe_null(x) (x & FIELDFLAG_MAYBE_NULL)
3972 #define f_no_default(x) (x & FIELDFLAG_NO_DEFAULT)
3973 #define f_bit_as_char(x) ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
3974 #define f_is_hex_escape(x) ((x) & FIELDFLAG_HEX_ESCAPE)
3975 
3976 #endif /* FIELD_INCLUDED */