mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / sql / field.h
blob01e97650c5696fb2d83c587d2eb5dba999f252f1
1 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 #include "my_compare.h" /* for clr_rec_bits */
20 Because of the function new_field() all field classes that have static
21 variables must declare the size_of() member function.
24 #ifdef USE_PRAGMA_INTERFACE
25 #pragma interface /* gcc class implementation */
26 #endif
28 #define NOT_FIXED_DEC 31
29 #define DATETIME_DEC 6
30 const uint32 max_field_size= (uint32) 4294967295U;
32 class Send_field;
33 class Protocol;
34 class Create_field;
35 class Relay_log_info;
37 struct st_cache_field;
38 int field_conv(Field *to,Field *from);
40 inline uint get_enum_pack_length(int elements)
42 return elements < 256 ? 1 : 2;
45 inline uint get_set_pack_length(int elements)
47 uint len= (elements + 7) / 8;
48 return len > 4 ? 8 : len;
51 class Field
53 Field(const Item &); /* Prevent use of these */
54 void operator=(Field &);
55 public:
56 static void *operator new(size_t size) throw ()
57 { return sql_alloc(size); }
58 static void operator delete(void *ptr_arg, size_t size) { TRASH(ptr_arg, size); }
60 uchar *ptr; // Position to field in record
61 /**
62 Byte where the @c NULL bit is stored inside a record. If this Field is a
63 @c NOT @c NULL field, this member is @c NULL.
65 uchar *null_ptr;
67 Note that you can use table->in_use as replacement for current_thd member
68 only inside of val_*() and store() members (e.g. you can't use it in cons)
70 struct st_table *table; // Pointer for table
71 struct st_table *orig_table; // Pointer to original table
72 const char **table_name, *field_name;
73 LEX_STRING comment;
74 /* Field is part of the following keys */
75 key_map key_start, part_of_key, part_of_key_not_clustered;
76 key_map part_of_sortkey;
77 /*
78 We use three additional unireg types for TIMESTAMP to overcome limitation
79 of current binary format of .frm file. We'd like to be able to support
80 NOW() as default and on update value for such fields but unable to hold
81 this info anywhere except unireg_check field. This issue will be resolved
82 in more clean way with transition to new text based .frm format.
83 See also comment for Field_timestamp::Field_timestamp().
85 enum utype { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
86 CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
87 BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
88 TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
89 enum geometry_type
91 GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
92 GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
93 GEOM_GEOMETRYCOLLECTION = 7
95 enum imagetype { itRAW, itMBR};
97 utype unireg_check;
98 uint32 field_length; // Length of field
99 uint32 flags;
100 uint16 field_index; // field number in fields array
101 uchar null_bit; // Bit used to test null bit
103 If true, this field was created in create_tmp_field_from_item from a NULL
104 value. This means that the type of the field is just a guess, and the type
105 may be freely coerced to another type.
107 @see create_tmp_field_from_item
108 @see Item_type_holder::get_real_type
111 bool is_created_from_null_item;
113 Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
114 uchar null_bit_arg, utype unireg_check_arg,
115 const char *field_name_arg);
116 virtual ~Field() {}
117 /* Store functions returns 1 on overflow and -1 on fatal error */
118 virtual int store(const char *to, uint length,CHARSET_INFO *cs)=0;
119 virtual int store(double nr)=0;
120 virtual int store(longlong nr, bool unsigned_val)=0;
121 virtual int store_decimal(const my_decimal *d)=0;
122 virtual int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
123 int store(const char *to, uint length, CHARSET_INFO *cs,
124 enum_check_fields check_level);
125 virtual double val_real(void)=0;
126 virtual longlong val_int(void)=0;
127 virtual my_decimal *val_decimal(my_decimal *);
128 inline String *val_str(String *str) { return val_str(str, str); }
130 val_str(buf1, buf2) gets two buffers and should use them as follows:
131 if it needs a temp buffer to convert result to string - use buf1
132 example Field_tiny::val_str()
133 if the value exists as a string already - use buf2
134 example Field_string::val_str()
135 consequently, buf2 may be created as 'String buf;' - no memory
136 will be allocated for it. buf1 will be allocated to hold a
137 value if it's too small. Using allocated buffer for buf2 may result in
138 an unnecessary free (and later, may be an alloc).
139 This trickery is used to decrease a number of malloc calls.
141 virtual String *val_str(String*,String *)=0;
142 String *val_int_as_str(String *val_buffer, my_bool unsigned_flag);
144 str_needs_quotes() returns TRUE if the value returned by val_str() needs
145 to be quoted when used in constructing an SQL query.
147 virtual bool str_needs_quotes() { return FALSE; }
148 virtual Item_result result_type () const=0;
149 virtual Item_result cmp_type () const { return result_type(); }
150 virtual Item_result cast_to_int_type () const { return result_type(); }
151 static bool type_can_have_key_part(enum_field_types);
152 static enum_field_types field_type_merge(enum_field_types, enum_field_types);
153 static Item_result result_merge_type(enum_field_types);
154 virtual bool eq(Field *field)
156 return (ptr == field->ptr && null_ptr == field->null_ptr &&
157 null_bit == field->null_bit && field->type() == type());
159 virtual bool eq_def(Field *field);
162 pack_length() returns size (in bytes) used to store field data in memory
163 (i.e. it returns the maximum size of the field in a row of the table,
164 which is located in RAM).
166 virtual uint32 pack_length() const { return (uint32) field_length; }
169 pack_length_in_rec() returns size (in bytes) used to store field data on
170 storage (i.e. it returns the maximal size of the field in a row of the
171 table, which is located on disk).
173 virtual uint32 pack_length_in_rec() const { return pack_length(); }
174 virtual int compatible_field_size(uint field_metadata,
175 const Relay_log_info *, uint16 mflags);
176 virtual uint pack_length_from_metadata(uint field_metadata)
177 { return field_metadata; }
179 This method is used to return the size of the data in a row-based
180 replication row record. The default implementation of returning 0 is
181 designed to allow fields that do not use metadata to return TRUE (1)
182 from compatible_field_size() which uses this function in the comparison.
183 The default value for field metadata for fields that do not have
184 metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
186 Note: While most classes that override this method return pack_length(),
187 the classes Field_string, Field_varstring, and Field_blob return
188 field_length + 1, field_length, and pack_length_no_ptr() respectfully.
190 virtual uint row_pack_length() { return 0; }
191 virtual int save_field_metadata(uchar *first_byte)
192 { return do_save_field_metadata(first_byte); }
195 data_length() return the "real size" of the data in memory.
197 virtual uint32 data_length() { return pack_length(); }
198 virtual uint32 sort_length() const { return pack_length(); }
201 Get the maximum size of the data in packed format.
203 @return Maximum data length of the field when packed using the
204 Field::pack() function.
206 virtual uint32 max_data_length() const {
207 return pack_length();
210 virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
211 virtual void reset_fields() {}
212 virtual void set_default()
214 my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
215 table->record[0]);
216 memcpy(ptr, ptr + l_offset, pack_length());
217 if (null_ptr)
218 *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
219 (null_ptr[l_offset] & null_bit));
221 virtual bool binary() const { return 1; }
222 virtual bool zero_pack() const { return 1; }
223 virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
224 virtual uint32 key_length() const { return pack_length(); }
225 virtual enum_field_types type() const =0;
226 virtual enum_field_types real_type() const { return type(); }
227 inline int cmp(const uchar *str) { return cmp(ptr,str); }
228 virtual int cmp_max(const uchar *a, const uchar *b, uint max_len)
229 { return cmp(a, b); }
230 virtual int cmp(const uchar *,const uchar *)=0;
231 virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L)
232 { return memcmp(a,b,pack_length()); }
233 virtual int cmp_offset(uint row_offset)
234 { return cmp(ptr,ptr+row_offset); }
235 virtual int cmp_binary_offset(uint row_offset)
236 { return cmp_binary(ptr, ptr+row_offset); };
237 virtual int key_cmp(const uchar *a,const uchar *b)
238 { return cmp(a, b); }
239 virtual int key_cmp(const uchar *str, uint length)
240 { return cmp(ptr,str); }
241 virtual uint decimals() const { return 0; }
243 Caller beware: sql_type can change str.Ptr, so check
244 ptr() to see if it changed if you are using your own buffer
245 in str and restore it with set() if needed
247 virtual void sql_type(String &str) const =0;
248 virtual uint size_of() const =0; // For new field
249 inline bool is_null(my_ptrdiff_t row_offset= 0)
250 { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
251 inline bool is_real_null(my_ptrdiff_t row_offset= 0)
252 { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
253 inline bool is_null_in_record(const uchar *record)
255 if (!null_ptr)
256 return 0;
257 return test(record[(uint) (null_ptr -table->record[0])] &
258 null_bit);
260 inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
262 if (!null_ptr)
263 return 0;
264 return test(null_ptr[offset] & null_bit);
266 inline void set_null(my_ptrdiff_t row_offset= 0)
267 { if (null_ptr) null_ptr[row_offset]|= null_bit; }
268 inline void set_notnull(my_ptrdiff_t row_offset= 0)
269 { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
270 inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
272 Signals that this field is NULL-able.
274 inline bool real_maybe_null(void) { return null_ptr != 0; }
276 enum {
277 LAST_NULL_BYTE_UNDEF= 0
281 Find the position of the last null byte for the field.
283 SYNOPSIS
284 last_null_byte()
286 DESCRIPTION
287 Return a pointer to the last byte of the null bytes where the
288 field conceptually is placed.
290 RETURN VALUE
291 The position of the last null byte relative to the beginning of
292 the record. If the field does not use any bits of the null
293 bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
295 size_t last_null_byte() const {
296 size_t bytes= do_last_null_byte();
297 DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
298 DBUG_ASSERT(bytes <= table->s->null_bytes);
299 return bytes;
302 virtual void make_field(Send_field *);
303 virtual void sort_string(uchar *buff,uint length)=0;
304 virtual bool optimize_range(uint idx, uint part);
306 This should be true for fields which, when compared with constant
307 items, can be casted to longlong. In this case we will at 'fix_fields'
308 stage cast the constant items to longlongs and at the execution stage
309 use field->val_int() for comparison. Used to optimize clauses like
310 'a_column BETWEEN date_const, date_const'.
312 virtual bool can_be_compared_as_longlong() const { return FALSE; }
313 virtual void free() {}
314 virtual Field *new_field(MEM_ROOT *root, struct st_table *new_table,
315 bool keep_type);
316 virtual Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
317 uchar *new_ptr, uchar *new_null_ptr,
318 uint new_null_bit);
319 Field *clone(MEM_ROOT *mem_root, struct st_table *new_table);
320 inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
322 ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
324 inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
325 virtual void move_field_offset(my_ptrdiff_t ptr_diff)
327 ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
328 if (null_ptr)
329 null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
331 virtual void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
332 { memcpy(buff,ptr,length); }
333 virtual void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
334 { memcpy(ptr,buff,length); }
338 Copy a field part into an output buffer.
340 SYNOPSIS
341 Field::get_key_image()
342 buff [out] output buffer
343 length output buffer size
344 type itMBR for geometry blobs, otherwise itRAW
346 DESCRIPTION
347 This function makes a copy of field part of size equal to or
348 less than "length" parameter value.
349 For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
350 is padded by zero byte.
352 NOTES
353 For variable length character fields (i.e. UTF-8) the "length"
354 parameter means a number of output buffer bytes as if all field
355 characters have maximal possible size (mbmaxlen). In the other words,
356 "length" parameter is a number of characters multiplied by
357 field_charset->mbmaxlen.
359 RETURN
360 Number of copied bytes (excluding padded zero bytes -- see above).
363 virtual uint get_key_image(uchar *buff, uint length, imagetype type)
365 get_image(buff, length, &my_charset_bin);
366 return length;
368 virtual void set_key_image(const uchar *buff,uint length)
369 { set_image(buff,length, &my_charset_bin); }
370 inline longlong val_int_offset(uint row_offset)
372 ptr+=row_offset;
373 longlong tmp=val_int();
374 ptr-=row_offset;
375 return tmp;
377 inline longlong val_int(const uchar *new_ptr)
379 uchar *old_ptr= ptr;
380 longlong return_value;
381 ptr= (uchar*) new_ptr;
382 return_value= val_int();
383 ptr= old_ptr;
384 return return_value;
386 inline String *val_str(String *str, const uchar *new_ptr)
388 uchar *old_ptr= ptr;
389 ptr= (uchar*) new_ptr;
390 val_str(str);
391 ptr= old_ptr;
392 return str;
394 virtual bool send_binary(Protocol *protocol);
396 virtual uchar *pack(uchar *to, const uchar *from,
397 uint max_length, bool low_byte_first);
399 @overload Field::pack(uchar*, const uchar*, uint, bool)
401 uchar *pack(uchar *to, const uchar *from)
403 DBUG_ENTER("Field::pack");
404 uchar *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
405 DBUG_RETURN(result);
408 virtual const uchar *unpack(uchar* to, const uchar *from,
409 uint param_data, bool low_byte_first);
411 @overload Field::unpack(uchar*, const uchar*, uint, bool)
413 const uchar *unpack(uchar* to, const uchar *from)
415 DBUG_ENTER("Field::unpack");
416 const uchar *result= unpack(to, from, 0U, table->s->db_low_byte_first);
417 DBUG_RETURN(result);
420 virtual uchar *pack_key(uchar* to, const uchar *from,
421 uint max_length, bool low_byte_first)
423 return pack(to, from, max_length, low_byte_first);
425 virtual uchar *pack_key_from_key_image(uchar* to, const uchar *from,
426 uint max_length, bool low_byte_first)
428 return pack(to, from, max_length, low_byte_first);
430 virtual const uchar *unpack_key(uchar* to, const uchar *from,
431 uint max_length, bool low_byte_first)
433 return unpack(to, from, max_length, low_byte_first);
435 virtual uint packed_col_length(const uchar *to, uint length)
436 { return length;}
437 virtual uint max_packed_col_length(uint max_length)
438 { return max_length;}
440 virtual int pack_cmp(const uchar *a,const uchar *b, uint key_length_arg,
441 my_bool insert_or_update)
442 { return cmp(a,b); }
443 virtual int pack_cmp(const uchar *b, uint key_length_arg,
444 my_bool insert_or_update)
445 { return cmp(ptr,b); }
446 uint offset(uchar *record)
448 return (uint) (ptr - record);
450 void copy_from_tmp(int offset);
451 uint fill_cache_field(struct st_cache_field *copy);
452 virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
453 virtual bool get_time(MYSQL_TIME *ltime);
454 virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
455 virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
456 virtual bool has_charset(void) const { return FALSE; }
457 virtual void set_charset(CHARSET_INFO *charset_arg) { }
458 virtual enum Derivation derivation(void) const
459 { return DERIVATION_IMPLICIT; }
460 virtual void set_derivation(enum Derivation derivation_arg) { }
461 bool set_warning(MYSQL_ERROR::enum_warning_level, unsigned int code,
462 int cuted_increment);
463 void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code,
464 const char *str, uint str_len,
465 timestamp_type ts_type, int cuted_increment);
466 void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code,
467 longlong nr, timestamp_type ts_type,
468 int cuted_increment);
469 void set_datetime_warning(MYSQL_ERROR::enum_warning_level, const uint code,
470 double nr, timestamp_type ts_type);
471 inline bool check_overflow(int op_result)
473 return (op_result == E_DEC_OVERFLOW);
475 int warn_if_overflow(int op_result);
476 void init(TABLE *table_arg)
478 orig_table= table= table_arg;
479 table_name= &table_arg->alias;
482 /* maximum possible display length */
483 virtual uint32 max_display_length()= 0;
486 Whether a field being created is compatible with a existing one.
488 Used by the ALTER TABLE code to evaluate whether the new definition
489 of a table is compatible with the old definition so that it can
490 determine if data needs to be copied over (table data change).
492 virtual uint is_equal(Create_field *new_field);
493 /* convert decimal to longlong with overflow check */
494 longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
495 int *err);
496 /* The max. number of characters */
497 inline uint32 char_length() const
499 return field_length / charset()->mbmaxlen;
502 virtual geometry_type get_geometry_type()
504 /* shouldn't get here. */
505 DBUG_ASSERT(0);
506 return GEOM_GEOMETRY;
508 /* Hash value */
509 virtual void hash(ulong *nr, ulong *nr2);
510 friend bool reopen_table(THD *,struct st_table *,bool);
511 friend int cre_myisam(char * name, register TABLE *form, uint options,
512 ulonglong auto_increment_value);
513 friend class Copy_field;
514 friend class Item_avg_field;
515 friend class Item_std_field;
516 friend class Item_sum_num;
517 friend class Item_sum_sum;
518 friend class Item_sum_str;
519 friend class Item_sum_count;
520 friend class Item_sum_avg;
521 friend class Item_sum_std;
522 friend class Item_sum_min;
523 friend class Item_sum_max;
524 friend class Item_func_group_concat;
526 private:
528 Primitive for implementing last_null_byte().
530 SYNOPSIS
531 do_last_null_byte()
533 DESCRIPTION
534 Primitive for the implementation of the last_null_byte()
535 function. This represents the inheritance interface and can be
536 overridden by subclasses.
538 virtual size_t do_last_null_byte() const;
541 Retrieve the field metadata for fields.
543 This default implementation returns 0 and saves 0 in the metadata_ptr
544 value.
546 @param metadata_ptr First byte of field metadata
548 @returns 0 no bytes written.
550 virtual int do_save_field_metadata(uchar *metadata_ptr)
551 { return 0; }
553 protected:
555 Helper function to pack()/unpack() int32 values
557 static void handle_int32(uchar *to, const uchar *from,
558 bool low_byte_first_from, bool low_byte_first_to)
560 int32 val;
561 #ifdef WORDS_BIGENDIAN
562 if (low_byte_first_from)
563 val = sint4korr(from);
564 else
565 #endif
566 longget(val, from);
568 #ifdef WORDS_BIGENDIAN
569 if (low_byte_first_to)
570 int4store(to, val);
571 else
572 #endif
573 longstore(to, val);
577 Helper function to pack()/unpack() int64 values
579 static void handle_int64(uchar* to, const uchar *from,
580 bool low_byte_first_from, bool low_byte_first_to)
582 int64 val;
583 #ifdef WORDS_BIGENDIAN
584 if (low_byte_first_from)
585 val = sint8korr(from);
586 else
587 #endif
588 longlongget(val, from);
590 #ifdef WORDS_BIGENDIAN
591 if (low_byte_first_to)
592 int8store(to, val);
593 else
594 #endif
595 longlongstore(to, val);
598 uchar *pack_int32(uchar *to, const uchar *from, bool low_byte_first_to)
600 handle_int32(to, from, table->s->db_low_byte_first, low_byte_first_to);
601 return to + sizeof(int32);
604 const uchar *unpack_int32(uchar* to, const uchar *from,
605 bool low_byte_first_from)
607 handle_int32(to, from, low_byte_first_from, table->s->db_low_byte_first);
608 return from + sizeof(int32);
611 uchar *pack_int64(uchar* to, const uchar *from, bool low_byte_first_to)
613 handle_int64(to, from, table->s->db_low_byte_first, low_byte_first_to);
614 return to + sizeof(int64);
617 const uchar *unpack_int64(uchar* to, const uchar *from,
618 bool low_byte_first_from)
620 handle_int64(to, from, low_byte_first_from, table->s->db_low_byte_first);
621 return from + sizeof(int64);
624 bool field_flags_are_binary()
626 return (flags & (BINCMP_FLAG | BINARY_FLAG)) != 0;
632 class Field_num :public Field {
633 public:
634 const uint8 dec;
635 bool zerofill,unsigned_flag; // Purify cannot handle bit fields
636 Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
637 uchar null_bit_arg, utype unireg_check_arg,
638 const char *field_name_arg,
639 uint8 dec_arg, bool zero_arg, bool unsigned_arg);
640 Item_result result_type () const { return REAL_RESULT; }
641 void prepend_zeros(String *value);
642 void add_zerofill_and_unsigned(String &res) const;
643 friend class Create_field;
644 void make_field(Send_field *);
645 uint decimals() const { return (uint) dec; }
646 uint size_of() const { return sizeof(*this); }
647 bool eq_def(Field *field);
648 int store_decimal(const my_decimal *);
649 my_decimal *val_decimal(my_decimal *);
650 uint is_equal(Create_field *new_field);
651 int check_int(CHARSET_INFO *cs, const char *str, int length,
652 const char *int_end, int error);
653 bool get_int(CHARSET_INFO *cs, const char *from, uint len,
654 longlong *rnd, ulonglong unsigned_max,
655 longlong signed_min, longlong signed_max);
659 class Field_str :public Field {
660 protected:
661 CHARSET_INFO *field_charset;
662 enum Derivation field_derivation;
663 public:
664 Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
665 uchar null_bit_arg, utype unireg_check_arg,
666 const char *field_name_arg, CHARSET_INFO *charset);
667 Item_result result_type () const { return STRING_RESULT; }
668 uint decimals() const { return NOT_FIXED_DEC; }
669 int store(double nr);
670 int store(longlong nr, bool unsigned_val)=0;
671 int store_decimal(const my_decimal *);
672 int store(const char *to,uint length,CHARSET_INFO *cs)=0;
673 uint size_of() const { return sizeof(*this); }
674 CHARSET_INFO *charset(void) const { return field_charset; }
675 void set_charset(CHARSET_INFO *charset_arg) { field_charset= charset_arg; }
676 enum Derivation derivation(void) const { return field_derivation; }
677 virtual void set_derivation(enum Derivation derivation_arg)
678 { field_derivation= derivation_arg; }
679 bool binary() const { return field_charset == &my_charset_bin; }
680 uint32 max_display_length() { return field_length; }
681 friend class Create_field;
682 my_decimal *val_decimal(my_decimal *);
683 virtual bool str_needs_quotes() { return TRUE; }
684 uint is_equal(Create_field *new_field);
688 /* base class for Field_string, Field_varstring and Field_blob */
690 class Field_longstr :public Field_str
692 protected:
693 int report_if_important_data(const char *ptr, const char *end,
694 bool count_spaces);
695 public:
696 Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
697 uchar null_bit_arg, utype unireg_check_arg,
698 const char *field_name_arg, CHARSET_INFO *charset_arg)
699 :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
700 field_name_arg, charset_arg)
703 int store_decimal(const my_decimal *d);
704 uint32 max_data_length() const;
707 /* base class for float and double and decimal (old one) */
708 class Field_real :public Field_num {
709 public:
710 my_bool not_fixed;
712 Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
713 uchar null_bit_arg, utype unireg_check_arg,
714 const char *field_name_arg,
715 uint8 dec_arg, bool zero_arg, bool unsigned_arg)
716 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
717 field_name_arg, dec_arg, zero_arg, unsigned_arg),
718 not_fixed(dec_arg >= NOT_FIXED_DEC)
720 int store_decimal(const my_decimal *);
721 my_decimal *val_decimal(my_decimal *);
722 int truncate(double *nr, double max_length);
723 uint32 max_display_length() { return field_length; }
724 uint size_of() const { return sizeof(*this); }
725 virtual const uchar *unpack(uchar* to, const uchar *from,
726 uint param_data, bool low_byte_first);
727 virtual uchar *pack(uchar* to, const uchar *from,
728 uint max_length, bool low_byte_first);
732 class Field_decimal :public Field_real {
733 public:
734 Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
735 uchar null_bit_arg,
736 enum utype unireg_check_arg, const char *field_name_arg,
737 uint8 dec_arg,bool zero_arg,bool unsigned_arg)
738 :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
739 unireg_check_arg, field_name_arg,
740 dec_arg, zero_arg, unsigned_arg)
742 enum_field_types type() const { return MYSQL_TYPE_DECIMAL;}
743 enum ha_base_keytype key_type() const
744 { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
745 int reset(void);
746 int store(const char *to,uint length,CHARSET_INFO *charset);
747 int store(double nr);
748 int store(longlong nr, bool unsigned_val);
749 double val_real(void);
750 longlong val_int(void);
751 String *val_str(String*,String *);
752 int cmp(const uchar *,const uchar *);
753 void sort_string(uchar *buff,uint length);
754 void overflow(bool negative);
755 bool zero_pack() const { return 0; }
756 void sql_type(String &str) const;
757 virtual const uchar *unpack(uchar* to, const uchar *from,
758 uint param_data, bool low_byte_first)
760 return Field::unpack(to, from, param_data, low_byte_first);
762 virtual uchar *pack(uchar* to, const uchar *from,
763 uint max_length, bool low_byte_first)
765 return Field::pack(to, from, max_length, low_byte_first);
770 /* New decimal/numeric field which use fixed point arithmetic */
771 class Field_new_decimal :public Field_num {
772 private:
773 int do_save_field_metadata(uchar *first_byte);
774 public:
775 /* The maximum number of decimal digits can be stored */
776 uint precision;
777 uint bin_size;
779 Constructors take max_length of the field as a parameter - not the
780 precision as the number of decimal digits allowed.
781 So for example we need to count length from precision handling
782 CREATE TABLE ( DECIMAL(x,y))
784 Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
785 uchar null_bit_arg,
786 enum utype unireg_check_arg, const char *field_name_arg,
787 uint8 dec_arg, bool zero_arg, bool unsigned_arg);
788 Field_new_decimal(uint32 len_arg, bool maybe_null_arg,
789 const char *field_name_arg, uint8 dec_arg,
790 bool unsigned_arg);
791 enum_field_types type() const { return MYSQL_TYPE_NEWDECIMAL;}
792 enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
793 Item_result result_type () const { return DECIMAL_RESULT; }
794 int reset(void);
795 bool store_value(const my_decimal *decimal_value);
796 void set_value_on_overflow(my_decimal *decimal_value, bool sign);
797 int store(const char *to, uint length, CHARSET_INFO *charset);
798 int store(double nr);
799 int store(longlong nr, bool unsigned_val);
800 int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
801 int store_decimal(const my_decimal *);
802 double val_real(void);
803 longlong val_int(void);
804 my_decimal *val_decimal(my_decimal *);
805 String *val_str(String*, String *);
806 int cmp(const uchar *, const uchar *);
807 void sort_string(uchar *buff, uint length);
808 bool zero_pack() const { return 0; }
809 void sql_type(String &str) const;
810 uint32 max_display_length() { return field_length; }
811 uint size_of() const { return sizeof(*this); }
812 uint32 pack_length() const { return (uint32) bin_size; }
813 uint pack_length_from_metadata(uint field_metadata);
814 uint row_pack_length() { return pack_length(); }
815 int compatible_field_size(uint field_metadata,
816 const Relay_log_info *rli, uint16 mflags);
817 uint is_equal(Create_field *new_field);
818 virtual const uchar *unpack(uchar* to, const uchar *from,
819 uint param_data, bool low_byte_first);
820 static Field *create_from_item (Item *);
824 class Field_tiny :public Field_num {
825 public:
826 Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
827 uchar null_bit_arg,
828 enum utype unireg_check_arg, const char *field_name_arg,
829 bool zero_arg, bool unsigned_arg)
830 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
831 unireg_check_arg, field_name_arg,
832 0, zero_arg,unsigned_arg)
834 enum Item_result result_type () const { return INT_RESULT; }
835 enum_field_types type() const { return MYSQL_TYPE_TINY;}
836 enum ha_base_keytype key_type() const
837 { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
838 int store(const char *to,uint length,CHARSET_INFO *charset);
839 int store(double nr);
840 int store(longlong nr, bool unsigned_val);
841 int reset(void) { ptr[0]=0; return 0; }
842 double val_real(void);
843 longlong val_int(void);
844 String *val_str(String*,String *);
845 bool send_binary(Protocol *protocol);
846 int cmp(const uchar *,const uchar *);
847 void sort_string(uchar *buff,uint length);
848 uint32 pack_length() const { return 1; }
849 void sql_type(String &str) const;
850 uint32 max_display_length() { return 4; }
852 virtual uchar *pack(uchar* to, const uchar *from,
853 uint max_length, bool low_byte_first)
855 *to= *from;
856 return to + 1;
859 virtual const uchar *unpack(uchar* to, const uchar *from,
860 uint param_data, bool low_byte_first)
862 *to= *from;
863 return from + 1;
868 class Field_short :public Field_num {
869 public:
870 Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
871 uchar null_bit_arg,
872 enum utype unireg_check_arg, const char *field_name_arg,
873 bool zero_arg, bool unsigned_arg)
874 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
875 unireg_check_arg, field_name_arg,
876 0, zero_arg,unsigned_arg)
878 Field_short(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
879 bool unsigned_arg)
880 :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
881 NONE, field_name_arg, 0, 0, unsigned_arg)
883 enum Item_result result_type () const { return INT_RESULT; }
884 enum_field_types type() const { return MYSQL_TYPE_SHORT;}
885 enum ha_base_keytype key_type() const
886 { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
887 int store(const char *to,uint length,CHARSET_INFO *charset);
888 int store(double nr);
889 int store(longlong nr, bool unsigned_val);
890 int reset(void) { ptr[0]=ptr[1]=0; return 0; }
891 double val_real(void);
892 longlong val_int(void);
893 String *val_str(String*,String *);
894 bool send_binary(Protocol *protocol);
895 int cmp(const uchar *,const uchar *);
896 void sort_string(uchar *buff,uint length);
897 uint32 pack_length() const { return 2; }
898 void sql_type(String &str) const;
899 uint32 max_display_length() { return 6; }
901 virtual uchar *pack(uchar* to, const uchar *from,
902 uint max_length, bool low_byte_first)
904 int16 val;
905 #ifdef WORDS_BIGENDIAN
906 if (table->s->db_low_byte_first)
907 val = sint2korr(from);
908 else
909 #endif
910 shortget(val, from);
912 #ifdef WORDS_BIGENDIAN
913 if (low_byte_first)
914 int2store(to, val);
915 else
916 #endif
917 shortstore(to, val);
918 return to + sizeof(val);
921 virtual const uchar *unpack(uchar* to, const uchar *from,
922 uint param_data, bool low_byte_first)
924 int16 val;
925 #ifdef WORDS_BIGENDIAN
926 if (low_byte_first)
927 val = sint2korr(from);
928 else
929 #endif
930 shortget(val, from);
932 #ifdef WORDS_BIGENDIAN
933 if (table->s->db_low_byte_first)
934 int2store(to, val);
935 else
936 #endif
937 shortstore(to, val);
938 return from + sizeof(val);
942 class Field_medium :public Field_num {
943 public:
944 Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
945 uchar null_bit_arg,
946 enum utype unireg_check_arg, const char *field_name_arg,
947 bool zero_arg, bool unsigned_arg)
948 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
949 unireg_check_arg, field_name_arg,
950 0, zero_arg,unsigned_arg)
952 enum Item_result result_type () const { return INT_RESULT; }
953 enum_field_types type() const { return MYSQL_TYPE_INT24;}
954 enum ha_base_keytype key_type() const
955 { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
956 int store(const char *to,uint length,CHARSET_INFO *charset);
957 int store(double nr);
958 int store(longlong nr, bool unsigned_val);
959 int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
960 double val_real(void);
961 longlong val_int(void);
962 String *val_str(String*,String *);
963 bool send_binary(Protocol *protocol);
964 int cmp(const uchar *,const uchar *);
965 void sort_string(uchar *buff,uint length);
966 uint32 pack_length() const { return 3; }
967 void sql_type(String &str) const;
968 uint32 max_display_length() { return 8; }
970 virtual uchar *pack(uchar* to, const uchar *from,
971 uint max_length, bool low_byte_first)
973 return Field::pack(to, from, max_length, low_byte_first);
976 virtual const uchar *unpack(uchar* to, const uchar *from,
977 uint param_data, bool low_byte_first)
979 return Field::unpack(to, from, param_data, low_byte_first);
984 class Field_long :public Field_num {
985 public:
986 Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
987 uchar null_bit_arg,
988 enum utype unireg_check_arg, const char *field_name_arg,
989 bool zero_arg, bool unsigned_arg)
990 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
991 unireg_check_arg, field_name_arg,
992 0, zero_arg,unsigned_arg)
994 Field_long(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
995 bool unsigned_arg)
996 :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
997 NONE, field_name_arg,0,0,unsigned_arg)
999 enum Item_result result_type () const { return INT_RESULT; }
1000 enum_field_types type() const { return MYSQL_TYPE_LONG;}
1001 enum ha_base_keytype key_type() const
1002 { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
1003 int store(const char *to,uint length,CHARSET_INFO *charset);
1004 int store(double nr);
1005 int store(longlong nr, bool unsigned_val);
1006 int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
1007 double val_real(void);
1008 longlong val_int(void);
1009 bool send_binary(Protocol *protocol);
1010 String *val_str(String*,String *);
1011 int cmp(const uchar *,const uchar *);
1012 void sort_string(uchar *buff,uint length);
1013 uint32 pack_length() const { return 4; }
1014 void sql_type(String &str) const;
1015 uint32 max_display_length() { return MY_INT32_NUM_DECIMAL_DIGITS; }
1016 virtual uchar *pack(uchar* to, const uchar *from,
1017 uint max_length __attribute__((unused)),
1018 bool low_byte_first)
1020 return pack_int32(to, from, low_byte_first);
1022 virtual const uchar *unpack(uchar* to, const uchar *from,
1023 uint param_data __attribute__((unused)),
1024 bool low_byte_first)
1026 return unpack_int32(to, from, low_byte_first);
1031 #ifdef HAVE_LONG_LONG
1032 class Field_longlong :public Field_num {
1033 public:
1034 Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1035 uchar null_bit_arg,
1036 enum utype unireg_check_arg, const char *field_name_arg,
1037 bool zero_arg, bool unsigned_arg)
1038 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1039 unireg_check_arg, field_name_arg,
1040 0, zero_arg,unsigned_arg)
1042 Field_longlong(uint32 len_arg,bool maybe_null_arg,
1043 const char *field_name_arg,
1044 bool unsigned_arg)
1045 :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
1046 NONE, field_name_arg,0,0,unsigned_arg)
1048 enum Item_result result_type () const { return INT_RESULT; }
1049 enum_field_types type() const { return MYSQL_TYPE_LONGLONG;}
1050 enum ha_base_keytype key_type() const
1051 { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
1052 int store(const char *to,uint length,CHARSET_INFO *charset);
1053 int store(double nr);
1054 int store(longlong nr, bool unsigned_val);
1055 int reset(void)
1057 ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
1058 return 0;
1060 double val_real(void);
1061 longlong val_int(void);
1062 String *val_str(String*,String *);
1063 bool send_binary(Protocol *protocol);
1064 int cmp(const uchar *,const uchar *);
1065 void sort_string(uchar *buff,uint length);
1066 uint32 pack_length() const { return 8; }
1067 void sql_type(String &str) const;
1068 bool can_be_compared_as_longlong() const { return TRUE; }
1069 uint32 max_display_length() { return 20; }
1070 virtual uchar *pack(uchar* to, const uchar *from,
1071 uint max_length __attribute__((unused)),
1072 bool low_byte_first)
1074 return pack_int64(to, from, low_byte_first);
1076 virtual const uchar *unpack(uchar* to, const uchar *from,
1077 uint param_data __attribute__((unused)),
1078 bool low_byte_first)
1080 return unpack_int64(to, from, low_byte_first);
1083 #endif
1086 class Field_float :public Field_real {
1087 public:
1088 Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1089 uchar null_bit_arg,
1090 enum utype unireg_check_arg, const char *field_name_arg,
1091 uint8 dec_arg,bool zero_arg,bool unsigned_arg)
1092 :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1093 unireg_check_arg, field_name_arg,
1094 dec_arg, zero_arg, unsigned_arg)
1096 Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
1097 uint8 dec_arg)
1098 :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
1099 NONE, field_name_arg, dec_arg, 0, 0)
1101 enum_field_types type() const { return MYSQL_TYPE_FLOAT;}
1102 enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
1103 int store(const char *to,uint length,CHARSET_INFO *charset);
1104 int store(double nr);
1105 int store(longlong nr, bool unsigned_val);
1106 int reset(void) { bzero(ptr,sizeof(float)); return 0; }
1107 double val_real(void);
1108 longlong val_int(void);
1109 String *val_str(String*,String *);
1110 bool send_binary(Protocol *protocol);
1111 int cmp(const uchar *,const uchar *);
1112 void sort_string(uchar *buff,uint length);
1113 uint32 pack_length() const { return sizeof(float); }
1114 uint row_pack_length() { return pack_length(); }
1115 void sql_type(String &str) const;
1116 private:
1117 int do_save_field_metadata(uchar *first_byte);
1121 class Field_double :public Field_real {
1122 public:
1123 Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1124 uchar null_bit_arg,
1125 enum utype unireg_check_arg, const char *field_name_arg,
1126 uint8 dec_arg,bool zero_arg,bool unsigned_arg)
1127 :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1128 unireg_check_arg, field_name_arg,
1129 dec_arg, zero_arg, unsigned_arg)
1131 Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
1132 uint8 dec_arg)
1133 :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
1134 NONE, field_name_arg, dec_arg, 0, 0)
1136 Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
1137 uint8 dec_arg, my_bool not_fixed_arg)
1138 :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
1139 NONE, field_name_arg, dec_arg, 0, 0)
1140 {not_fixed= not_fixed_arg; }
1141 enum_field_types type() const { return MYSQL_TYPE_DOUBLE;}
1142 enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
1143 int store(const char *to,uint length,CHARSET_INFO *charset);
1144 int store(double nr);
1145 int store(longlong nr, bool unsigned_val);
1146 int reset(void) { bzero(ptr,sizeof(double)); return 0; }
1147 double val_real(void);
1148 longlong val_int(void);
1149 String *val_str(String*,String *);
1150 bool send_binary(Protocol *protocol);
1151 int cmp(const uchar *,const uchar *);
1152 void sort_string(uchar *buff,uint length);
1153 uint32 pack_length() const { return sizeof(double); }
1154 uint row_pack_length() { return pack_length(); }
1155 void sql_type(String &str) const;
1156 private:
1157 int do_save_field_metadata(uchar *first_byte);
1161 /* Everything saved in this will disappear. It will always return NULL */
1163 class Field_null :public Field_str {
1164 static uchar null[1];
1165 public:
1166 Field_null(uchar *ptr_arg, uint32 len_arg,
1167 enum utype unireg_check_arg, const char *field_name_arg,
1168 CHARSET_INFO *cs)
1169 :Field_str(ptr_arg, len_arg, null, 1,
1170 unireg_check_arg, field_name_arg, cs)
1172 enum_field_types type() const { return MYSQL_TYPE_NULL;}
1173 int store(const char *to, uint length, CHARSET_INFO *cs)
1174 { null[0]=1; return 0; }
1175 int store(double nr) { null[0]=1; return 0; }
1176 int store(longlong nr, bool unsigned_val) { null[0]=1; return 0; }
1177 int store_decimal(const my_decimal *d) { null[0]=1; return 0; }
1178 int reset(void) { return 0; }
1179 double val_real(void) { return 0.0;}
1180 longlong val_int(void) { return 0;}
1181 my_decimal *val_decimal(my_decimal *) { return 0; }
1182 String *val_str(String *value,String *value2)
1183 { value2->length(0); return value2;}
1184 int cmp(const uchar *a, const uchar *b) { return 0;}
1185 void sort_string(uchar *buff, uint length) {}
1186 uint32 pack_length() const { return 0; }
1187 void sql_type(String &str) const;
1188 uint size_of() const { return sizeof(*this); }
1189 uint32 max_display_length() { return 4; }
1193 class Field_timestamp :public Field_str {
1194 public:
1195 Field_timestamp(uchar *ptr_arg, uint32 len_arg,
1196 uchar *null_ptr_arg, uchar null_bit_arg,
1197 enum utype unireg_check_arg, const char *field_name_arg,
1198 TABLE_SHARE *share, CHARSET_INFO *cs);
1199 Field_timestamp(bool maybe_null_arg, const char *field_name_arg,
1200 CHARSET_INFO *cs);
1201 enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;}
1202 enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
1203 enum Item_result cmp_type () const { return INT_RESULT; }
1204 int store(const char *to,uint length,CHARSET_INFO *charset);
1205 int store(double nr);
1206 int store(longlong nr, bool unsigned_val);
1207 int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
1208 double val_real(void);
1209 longlong val_int(void);
1210 String *val_str(String*,String *);
1211 bool send_binary(Protocol *protocol);
1212 int cmp(const uchar *,const uchar *);
1213 void sort_string(uchar *buff,uint length);
1214 uint32 pack_length() const { return 4; }
1215 void sql_type(String &str) const;
1216 bool can_be_compared_as_longlong() const { return TRUE; }
1217 bool zero_pack() const { return 0; }
1218 void set_time();
1219 virtual void set_default()
1221 if (table->timestamp_field == this &&
1222 unireg_check != TIMESTAMP_UN_FIELD)
1223 set_time();
1224 else
1225 Field::set_default();
1227 /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
1228 inline long get_timestamp(my_bool *null_value)
1230 if ((*null_value= is_null()))
1231 return 0;
1232 #ifdef WORDS_BIGENDIAN
1233 if (table && table->s->db_low_byte_first)
1234 return sint4korr(ptr);
1235 #endif
1236 long tmp;
1237 longget(tmp,ptr);
1238 return tmp;
1240 inline void store_timestamp(my_time_t timestamp)
1242 #ifdef WORDS_BIGENDIAN
1243 if (table && table->s->db_low_byte_first)
1245 int4store(ptr,timestamp);
1247 else
1248 #endif
1249 longstore(ptr,(uint32) timestamp);
1251 bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
1252 bool get_time(MYSQL_TIME *ltime);
1253 timestamp_auto_set_type get_auto_set_type() const;
1254 uchar *pack(uchar *to, const uchar *from,
1255 uint max_length __attribute__((unused)), bool low_byte_first)
1257 return pack_int32(to, from, low_byte_first);
1259 const uchar *unpack(uchar* to, const uchar *from,
1260 uint param_data __attribute__((unused)),
1261 bool low_byte_first)
1263 return unpack_int32(to, from, low_byte_first);
1268 class Field_year :public Field_tiny {
1269 public:
1270 Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1271 uchar null_bit_arg,
1272 enum utype unireg_check_arg, const char *field_name_arg)
1273 :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1274 unireg_check_arg, field_name_arg, 1, 1)
1276 enum_field_types type() const { return MYSQL_TYPE_YEAR;}
1277 int store(const char *to,uint length,CHARSET_INFO *charset);
1278 int store(double nr);
1279 int store(longlong nr, bool unsigned_val);
1280 double val_real(void);
1281 longlong val_int(void);
1282 String *val_str(String*,String *);
1283 bool send_binary(Protocol *protocol);
1284 void sql_type(String &str) const;
1285 bool can_be_compared_as_longlong() const { return TRUE; }
1289 class Field_date :public Field_str {
1290 public:
1291 Field_date(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
1292 enum utype unireg_check_arg, const char *field_name_arg,
1293 CHARSET_INFO *cs)
1294 :Field_str(ptr_arg, MAX_DATE_WIDTH, null_ptr_arg, null_bit_arg,
1295 unireg_check_arg, field_name_arg, cs)
1297 Field_date(bool maybe_null_arg, const char *field_name_arg,
1298 CHARSET_INFO *cs)
1299 :Field_str((uchar*) 0, MAX_DATE_WIDTH, maybe_null_arg ? (uchar*) "": 0,0,
1300 NONE, field_name_arg, cs) {}
1301 enum_field_types type() const { return MYSQL_TYPE_DATE;}
1302 enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
1303 enum Item_result cmp_type () const { return INT_RESULT; }
1304 int store(const char *to,uint length,CHARSET_INFO *charset);
1305 int store(double nr);
1306 int store(longlong nr, bool unsigned_val);
1307 int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
1308 double val_real(void);
1309 longlong val_int(void);
1310 String *val_str(String*,String *);
1311 bool get_time(MYSQL_TIME *ltime);
1312 bool send_binary(Protocol *protocol);
1313 int cmp(const uchar *,const uchar *);
1314 void sort_string(uchar *buff,uint length);
1315 uint32 pack_length() const { return 4; }
1316 void sql_type(String &str) const;
1317 bool can_be_compared_as_longlong() const { return TRUE; }
1318 bool zero_pack() const { return 1; }
1319 uchar *pack(uchar* to, const uchar *from,
1320 uint max_length __attribute__((unused)), bool low_byte_first)
1322 return pack_int32(to, from, low_byte_first);
1324 const uchar *unpack(uchar* to, const uchar *from,
1325 uint param_data __attribute__((unused)),
1326 bool low_byte_first)
1328 return unpack_int32(to, from, low_byte_first);
1333 class Field_newdate :public Field_str {
1334 public:
1335 Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
1336 enum utype unireg_check_arg, const char *field_name_arg,
1337 CHARSET_INFO *cs)
1338 :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
1339 unireg_check_arg, field_name_arg, cs)
1341 Field_newdate(bool maybe_null_arg, const char *field_name_arg,
1342 CHARSET_INFO *cs)
1343 :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
1344 NONE, field_name_arg, cs) {}
1345 enum_field_types type() const { return MYSQL_TYPE_DATE;}
1346 enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; }
1347 enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
1348 enum Item_result cmp_type () const { return INT_RESULT; }
1349 int store(const char *to,uint length,CHARSET_INFO *charset);
1350 int store(double nr);
1351 int store(longlong nr, bool unsigned_val);
1352 int store_time(MYSQL_TIME *ltime, timestamp_type type);
1353 int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
1354 double val_real(void);
1355 longlong val_int(void);
1356 String *val_str(String*,String *);
1357 bool send_binary(Protocol *protocol);
1358 int cmp(const uchar *,const uchar *);
1359 void sort_string(uchar *buff,uint length);
1360 uint32 pack_length() const { return 3; }
1361 void sql_type(String &str) const;
1362 bool can_be_compared_as_longlong() const { return TRUE; }
1363 bool zero_pack() const { return 1; }
1364 bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
1365 bool get_time(MYSQL_TIME *ltime);
1369 class Field_time :public Field_str {
1370 public:
1371 Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
1372 enum utype unireg_check_arg, const char *field_name_arg,
1373 CHARSET_INFO *cs)
1374 :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
1375 unireg_check_arg, field_name_arg, cs)
1377 Field_time(bool maybe_null_arg, const char *field_name_arg,
1378 CHARSET_INFO *cs)
1379 :Field_str((uchar*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
1380 NONE, field_name_arg, cs) {}
1381 enum_field_types type() const { return MYSQL_TYPE_TIME;}
1382 enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
1383 enum Item_result cmp_type () const { return INT_RESULT; }
1384 int store_time(MYSQL_TIME *ltime, timestamp_type type);
1385 int store(const char *to,uint length,CHARSET_INFO *charset);
1386 int store(double nr);
1387 int store(longlong nr, bool unsigned_val);
1388 int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
1389 double val_real(void);
1390 longlong val_int(void);
1391 String *val_str(String*,String *);
1392 bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1393 bool send_binary(Protocol *protocol);
1394 bool get_time(MYSQL_TIME *ltime);
1395 int cmp(const uchar *,const uchar *);
1396 void sort_string(uchar *buff,uint length);
1397 uint32 pack_length() const { return 3; }
1398 void sql_type(String &str) const;
1399 bool can_be_compared_as_longlong() const { return TRUE; }
1400 bool zero_pack() const { return 1; }
1404 class Field_datetime :public Field_str {
1405 public:
1406 Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
1407 enum utype unireg_check_arg, const char *field_name_arg,
1408 CHARSET_INFO *cs)
1409 :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
1410 unireg_check_arg, field_name_arg, cs)
1412 Field_datetime(bool maybe_null_arg, const char *field_name_arg,
1413 CHARSET_INFO *cs)
1414 :Field_str((uchar*) 0, MAX_DATETIME_WIDTH, maybe_null_arg ? (uchar*) "": 0,0,
1415 NONE, field_name_arg, cs) {}
1416 enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
1417 #ifdef HAVE_LONG_LONG
1418 enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
1419 #endif
1420 enum Item_result cmp_type () const { return INT_RESULT; }
1421 uint decimals() const { return DATETIME_DEC; }
1422 int store(const char *to,uint length,CHARSET_INFO *charset);
1423 int store(double nr);
1424 int store(longlong nr, bool unsigned_val);
1425 int store_time(MYSQL_TIME *ltime, timestamp_type type);
1426 int reset(void)
1428 ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
1429 return 0;
1431 double val_real(void);
1432 longlong val_int(void);
1433 String *val_str(String*,String *);
1434 bool send_binary(Protocol *protocol);
1435 int cmp(const uchar *,const uchar *);
1436 void sort_string(uchar *buff,uint length);
1437 uint32 pack_length() const { return 8; }
1438 void sql_type(String &str) const;
1439 bool can_be_compared_as_longlong() const { return TRUE; }
1440 bool zero_pack() const { return 1; }
1441 bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
1442 bool get_time(MYSQL_TIME *ltime);
1443 uchar *pack(uchar* to, const uchar *from,
1444 uint max_length __attribute__((unused)), bool low_byte_first)
1446 return pack_int64(to, from, low_byte_first);
1448 const uchar *unpack(uchar* to, const uchar *from,
1449 uint param_data __attribute__((unused)),
1450 bool low_byte_first)
1452 return unpack_int64(to, from, low_byte_first);
1457 class Field_string :public Field_longstr {
1458 public:
1459 bool can_alter_field_type;
1460 Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
1461 uchar null_bit_arg,
1462 enum utype unireg_check_arg, const char *field_name_arg,
1463 CHARSET_INFO *cs)
1464 :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1465 unireg_check_arg, field_name_arg, cs),
1466 can_alter_field_type(1) {};
1467 Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
1468 CHARSET_INFO *cs)
1469 :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
1470 NONE, field_name_arg, cs),
1471 can_alter_field_type(1) {};
1473 enum_field_types type() const
1475 return ((can_alter_field_type && orig_table &&
1476 orig_table->s->db_create_options & HA_OPTION_PACK_RECORD &&
1477 field_length >= 4) &&
1478 orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR ?
1479 MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING);
1481 enum ha_base_keytype key_type() const
1482 { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
1483 bool zero_pack() const { return 0; }
1484 int reset(void)
1486 charset()->cset->fill(charset(),(char*) ptr, field_length,
1487 (has_charset() ? ' ' : 0));
1488 return 0;
1490 int store(const char *to,uint length,CHARSET_INFO *charset);
1491 int store(longlong nr, bool unsigned_val);
1492 int store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
1493 double val_real(void);
1494 longlong val_int(void);
1495 String *val_str(String*,String *);
1496 my_decimal *val_decimal(my_decimal *);
1497 int cmp(const uchar *,const uchar *);
1498 void sort_string(uchar *buff,uint length);
1499 void sql_type(String &str) const;
1500 virtual uchar *pack(uchar *to, const uchar *from,
1501 uint max_length, bool low_byte_first);
1502 virtual const uchar *unpack(uchar* to, const uchar *from,
1503 uint param_data, bool low_byte_first);
1504 uint pack_length_from_metadata(uint field_metadata)
1506 DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
1507 if (field_metadata == 0)
1508 return row_pack_length();
1509 return (((field_metadata >> 4) & 0x300) ^ 0x300) + (field_metadata & 0x00ff);
1511 int compatible_field_size(uint field_metadata,
1512 const Relay_log_info *rli, uint16 mflags);
1513 uint row_pack_length() { return (field_length + 1); }
1514 int pack_cmp(const uchar *a,const uchar *b,uint key_length,
1515 my_bool insert_or_update);
1516 int pack_cmp(const uchar *b,uint key_length,my_bool insert_or_update);
1517 uint packed_col_length(const uchar *to, uint length);
1518 uint max_packed_col_length(uint max_length);
1519 uint size_of() const { return sizeof(*this); }
1520 enum_field_types real_type() const { return MYSQL_TYPE_STRING; }
1521 bool has_charset(void) const
1522 { return charset() == &my_charset_bin ? FALSE : TRUE; }
1523 Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
1524 virtual uint get_key_image(uchar *buff,uint length, imagetype type);
1525 private:
1526 int do_save_field_metadata(uchar *first_byte);
1530 class Field_varstring :public Field_longstr {
1531 public:
1533 The maximum space available in a Field_varstring, in bytes. See
1534 length_bytes.
1536 static const uint MAX_SIZE;
1537 /* Store number of bytes used to store length (1 or 2) */
1538 uint32 length_bytes;
1539 Field_varstring(uchar *ptr_arg,
1540 uint32 len_arg, uint length_bytes_arg,
1541 uchar *null_ptr_arg, uchar null_bit_arg,
1542 enum utype unireg_check_arg, const char *field_name_arg,
1543 TABLE_SHARE *share, CHARSET_INFO *cs)
1544 :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1545 unireg_check_arg, field_name_arg, cs),
1546 length_bytes(length_bytes_arg)
1548 share->varchar_fields++;
1550 Field_varstring(uint32 len_arg,bool maybe_null_arg,
1551 const char *field_name_arg,
1552 TABLE_SHARE *share, CHARSET_INFO *cs)
1553 :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
1554 NONE, field_name_arg, cs),
1555 length_bytes(len_arg < 256 ? 1 :2)
1557 share->varchar_fields++;
1560 enum_field_types type() const { return MYSQL_TYPE_VARCHAR; }
1561 enum ha_base_keytype key_type() const;
1562 uint row_pack_length() { return field_length; }
1563 bool zero_pack() const { return 0; }
1564 int reset(void) { bzero(ptr,field_length+length_bytes); return 0; }
1565 uint32 pack_length() const { return (uint32) field_length+length_bytes; }
1566 uint32 key_length() const { return (uint32) field_length; }
1567 uint32 sort_length() const
1569 return (uint32) field_length + (field_charset == &my_charset_bin ?
1570 length_bytes : 0);
1572 int store(const char *to,uint length,CHARSET_INFO *charset);
1573 int store(longlong nr, bool unsigned_val);
1574 int store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
1575 double val_real(void);
1576 longlong val_int(void);
1577 String *val_str(String*,String *);
1578 my_decimal *val_decimal(my_decimal *);
1579 int cmp_max(const uchar *, const uchar *, uint max_length);
1580 int cmp(const uchar *a,const uchar *b)
1582 return cmp_max(a, b, ~0L);
1584 void sort_string(uchar *buff,uint length);
1585 uint get_key_image(uchar *buff,uint length, imagetype type);
1586 void set_key_image(const uchar *buff,uint length);
1587 void sql_type(String &str) const;
1588 virtual uchar *pack(uchar *to, const uchar *from,
1589 uint max_length, bool low_byte_first);
1590 uchar *pack_key(uchar *to, const uchar *from, uint max_length, bool low_byte_first);
1591 uchar *pack_key_from_key_image(uchar* to, const uchar *from,
1592 uint max_length, bool low_byte_first);
1593 virtual const uchar *unpack(uchar* to, const uchar *from,
1594 uint param_data, bool low_byte_first);
1595 const uchar *unpack_key(uchar* to, const uchar *from,
1596 uint max_length, bool low_byte_first);
1597 int pack_cmp(const uchar *a, const uchar *b, uint key_length,
1598 my_bool insert_or_update);
1599 int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
1600 int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
1601 int key_cmp(const uchar *,const uchar*);
1602 int key_cmp(const uchar *str, uint length);
1603 uint packed_col_length(const uchar *to, uint length);
1604 uint max_packed_col_length(uint max_length);
1605 uint32 data_length();
1606 uint size_of() const { return sizeof(*this); }
1607 enum_field_types real_type() const { return MYSQL_TYPE_VARCHAR; }
1608 bool has_charset(void) const
1609 { return charset() == &my_charset_bin ? FALSE : TRUE; }
1610 Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
1611 Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
1612 uchar *new_ptr, uchar *new_null_ptr,
1613 uint new_null_bit);
1614 uint is_equal(Create_field *new_field);
1615 void hash(ulong *nr, ulong *nr2);
1616 private:
1617 int do_save_field_metadata(uchar *first_byte);
1621 class Field_blob :public Field_longstr {
1622 protected:
1624 The number of bytes used to represent the length of the blob.
1626 uint packlength;
1629 The 'value'-object is a cache fronting the storage engine.
1631 String value;
1633 public:
1634 Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
1635 enum utype unireg_check_arg, const char *field_name_arg,
1636 TABLE_SHARE *share, uint blob_pack_length, CHARSET_INFO *cs);
1637 Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
1638 CHARSET_INFO *cs)
1639 :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
1640 NONE, field_name_arg, cs),
1641 packlength(4)
1643 flags|= BLOB_FLAG;
1645 Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
1646 CHARSET_INFO *cs, bool set_packlength)
1647 :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
1648 NONE, field_name_arg, cs)
1650 flags|= BLOB_FLAG;
1651 packlength= 4;
1652 if (set_packlength)
1654 uint32 l_char_length= len_arg/cs->mbmaxlen;
1655 packlength= l_char_length <= 255 ? 1 :
1656 l_char_length <= 65535 ? 2 :
1657 l_char_length <= 16777215 ? 3 : 4;
1660 Field_blob(uint32 packlength_arg)
1661 :Field_longstr((uchar*) 0, 0, (uchar*) "", 0, NONE, "temp", system_charset_info),
1662 packlength(packlength_arg) {}
1663 enum_field_types type() const { return MYSQL_TYPE_BLOB;}
1664 enum ha_base_keytype key_type() const
1665 { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
1666 int store(const char *to,uint length,CHARSET_INFO *charset);
1667 int store(double nr);
1668 int store(longlong nr, bool unsigned_val);
1669 double val_real(void);
1670 longlong val_int(void);
1671 String *val_str(String*,String *);
1672 my_decimal *val_decimal(my_decimal *);
1673 int cmp_max(const uchar *, const uchar *, uint max_length);
1674 int cmp(const uchar *a,const uchar *b)
1675 { return cmp_max(a, b, ~0L); }
1676 int cmp(const uchar *a, uint32 a_length, const uchar *b, uint32 b_length);
1677 int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
1678 int key_cmp(const uchar *,const uchar*);
1679 int key_cmp(const uchar *str, uint length);
1680 uint32 key_length() const { return 0; }
1681 void sort_string(uchar *buff,uint length);
1682 uint32 pack_length() const
1683 { return (uint32) (packlength+table->s->blob_ptr_size); }
1686 Return the packed length without the pointer size added.
1688 This is used to determine the size of the actual data in the row
1689 buffer.
1691 @returns The length of the raw data itself without the pointer.
1693 uint32 pack_length_no_ptr() const
1694 { return (uint32) (packlength); }
1695 uint row_pack_length() { return pack_length_no_ptr(); }
1696 uint32 sort_length() const;
1697 virtual uint32 max_data_length() const
1699 return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
1701 int reset(void) { bzero(ptr, packlength+sizeof(uchar*)); return 0; }
1702 void reset_fields() { bzero((uchar*) &value,sizeof(value)); }
1703 uint32 get_field_buffer_size(void) { return value.alloced_length(); }
1704 #ifndef WORDS_BIGENDIAN
1705 static
1706 #endif
1707 void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number, bool low_byte_first);
1708 void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number)
1710 store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
1712 inline void store_length(uint32 number)
1714 store_length(ptr, packlength, number);
1718 Return the packed length plus the length of the data.
1720 This is used to determine the size of the data plus the
1721 packed length portion in the row data.
1723 @returns The length in the row plus the size of the data.
1725 uint32 get_packed_size(const uchar *ptr_arg, bool low_byte_first)
1726 {return packlength + get_length(ptr_arg, packlength, low_byte_first);}
1728 inline uint32 get_length(uint row_offset= 0)
1729 { return get_length(ptr+row_offset, this->packlength, table->s->db_low_byte_first); }
1730 uint32 get_length(const uchar *ptr, uint packlength, bool low_byte_first);
1731 uint32 get_length(const uchar *ptr_arg)
1732 { return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first); }
1733 void put_length(uchar *pos, uint32 length);
1734 inline void get_ptr(uchar **str)
1736 memcpy_fixed((uchar*) str,ptr+packlength,sizeof(uchar*));
1738 inline void get_ptr(uchar **str, uint row_offset)
1740 memcpy_fixed((uchar*) str,ptr+packlength+row_offset,sizeof(char*));
1742 inline void set_ptr(uchar *length, uchar *data)
1744 memcpy(ptr,length,packlength);
1745 memcpy_fixed(ptr+packlength,&data,sizeof(char*));
1747 void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, uchar *data)
1749 uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
1750 store_length(ptr_ofs, packlength, length);
1751 memcpy_fixed(ptr_ofs+packlength,&data,sizeof(char*));
1753 inline void set_ptr(uint32 length, uchar *data)
1755 set_ptr_offset(0, length, data);
1757 uint get_key_image(uchar *buff,uint length, imagetype type);
1758 void set_key_image(const uchar *buff,uint length);
1759 void sql_type(String &str) const;
1760 inline bool copy()
1762 uchar *tmp;
1763 get_ptr(&tmp);
1764 if (value.copy((char*) tmp, get_length(), charset()))
1766 Field_blob::reset();
1767 return 1;
1769 tmp=(uchar*) value.ptr();
1770 memcpy_fixed(ptr+packlength,&tmp,sizeof(char*));
1771 return 0;
1773 virtual uchar *pack(uchar *to, const uchar *from,
1774 uint max_length, bool low_byte_first);
1775 uchar *pack_key(uchar *to, const uchar *from,
1776 uint max_length, bool low_byte_first);
1777 uchar *pack_key_from_key_image(uchar* to, const uchar *from,
1778 uint max_length, bool low_byte_first);
1779 virtual const uchar *unpack(uchar *to, const uchar *from,
1780 uint param_data, bool low_byte_first);
1781 const uchar *unpack_key(uchar* to, const uchar *from,
1782 uint max_length, bool low_byte_first);
1783 int pack_cmp(const uchar *a, const uchar *b, uint key_length,
1784 my_bool insert_or_update);
1785 int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
1786 uint packed_col_length(const uchar *col_ptr, uint length);
1787 uint max_packed_col_length(uint max_length);
1788 void free() { value.free(); }
1789 inline void clear_temporary() { bzero((uchar*) &value,sizeof(value)); }
1790 friend int field_conv(Field *to,Field *from);
1791 uint size_of() const { return sizeof(*this); }
1792 bool has_charset(void) const
1793 { return charset() == &my_charset_bin ? FALSE : TRUE; }
1794 uint32 max_display_length();
1795 uint is_equal(Create_field *new_field);
1796 inline bool in_read_set() { return bitmap_is_set(table->read_set, field_index); }
1797 inline bool in_write_set() { return bitmap_is_set(table->write_set, field_index); }
1798 private:
1799 int do_save_field_metadata(uchar *first_byte);
1803 #ifdef HAVE_SPATIAL
1804 class Field_geom :public Field_blob {
1805 public:
1806 enum geometry_type geom_type;
1808 Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
1809 enum utype unireg_check_arg, const char *field_name_arg,
1810 TABLE_SHARE *share, uint blob_pack_length,
1811 enum geometry_type geom_type_arg)
1812 :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
1813 field_name_arg, share, blob_pack_length, &my_charset_bin)
1814 { geom_type= geom_type_arg; }
1815 Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
1816 TABLE_SHARE *share, enum geometry_type geom_type_arg)
1817 :Field_blob(len_arg, maybe_null_arg, field_name_arg, &my_charset_bin)
1818 { geom_type= geom_type_arg; }
1819 enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY2; }
1820 enum_field_types type() const { return MYSQL_TYPE_GEOMETRY; }
1821 void sql_type(String &str) const;
1822 int store(const char *to, uint length, CHARSET_INFO *charset);
1823 int store(double nr);
1824 int store(longlong nr, bool unsigned_val);
1825 int store_decimal(const my_decimal *);
1826 uint size_of() const { return sizeof(*this); }
1829 Non-nullable GEOMETRY types cannot have defaults,
1830 but the underlying blob must still be reset.
1832 int reset(void) { return Field_blob::reset() || !maybe_null(); }
1834 geometry_type get_geometry_type() { return geom_type; };
1836 #endif /*HAVE_SPATIAL*/
1839 class Field_enum :public Field_str {
1840 protected:
1841 uint packlength;
1842 public:
1843 TYPELIB *typelib;
1844 Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1845 uchar null_bit_arg,
1846 enum utype unireg_check_arg, const char *field_name_arg,
1847 uint packlength_arg,
1848 TYPELIB *typelib_arg,
1849 CHARSET_INFO *charset_arg)
1850 :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1851 unireg_check_arg, field_name_arg, charset_arg),
1852 packlength(packlength_arg),typelib(typelib_arg)
1854 flags|=ENUM_FLAG;
1856 Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
1857 enum_field_types type() const { return MYSQL_TYPE_STRING; }
1858 enum Item_result cmp_type () const { return INT_RESULT; }
1859 enum Item_result cast_to_int_type () const { return INT_RESULT; }
1860 enum ha_base_keytype key_type() const;
1861 int store(const char *to,uint length,CHARSET_INFO *charset);
1862 int store(double nr);
1863 int store(longlong nr, bool unsigned_val);
1864 double val_real(void);
1865 longlong val_int(void);
1866 String *val_str(String*,String *);
1867 int cmp(const uchar *,const uchar *);
1868 void sort_string(uchar *buff,uint length);
1869 uint32 pack_length() const { return (uint32) packlength; }
1870 void store_type(ulonglong value);
1871 void sql_type(String &str) const;
1872 uint size_of() const { return sizeof(*this); }
1873 enum_field_types real_type() const { return MYSQL_TYPE_ENUM; }
1874 uint pack_length_from_metadata(uint field_metadata)
1875 { return (field_metadata & 0x00ff); }
1876 uint row_pack_length() { return pack_length(); }
1877 virtual bool zero_pack() const { return 0; }
1878 bool optimize_range(uint idx, uint part) { return 0; }
1879 bool eq_def(Field *field);
1880 bool has_charset(void) const { return TRUE; }
1881 /* enum and set are sorted as integers */
1882 CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
1883 private:
1884 int do_save_field_metadata(uchar *first_byte);
1885 uint is_equal(Create_field *new_field);
1889 class Field_set :public Field_enum {
1890 public:
1891 Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1892 uchar null_bit_arg,
1893 enum utype unireg_check_arg, const char *field_name_arg,
1894 uint32 packlength_arg,
1895 TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
1896 :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1897 unireg_check_arg, field_name_arg,
1898 packlength_arg,
1899 typelib_arg,charset_arg)
1901 flags=(flags & ~ENUM_FLAG) | SET_FLAG;
1903 int store(const char *to,uint length,CHARSET_INFO *charset);
1904 int store(double nr) { return Field_set::store((longlong) nr, FALSE); }
1905 int store(longlong nr, bool unsigned_val);
1907 virtual bool zero_pack() const { return 1; }
1908 String *val_str(String*,String *);
1909 void sql_type(String &str) const;
1910 enum_field_types real_type() const { return MYSQL_TYPE_SET; }
1911 bool has_charset(void) const { return TRUE; }
1916 Note:
1917 To use Field_bit::cmp_binary() you need to copy the bits stored in
1918 the beginning of the record (the NULL bytes) to each memory you
1919 want to compare (where the arguments point).
1921 This is the reason:
1922 - Field_bit::cmp_binary() is only implemented in the base class
1923 (Field::cmp_binary()).
1924 - Field::cmp_binary() currenly use pack_length() to calculate how
1925 long the data is.
1926 - pack_length() includes size of the bits stored in the NULL bytes
1927 of the record.
1929 class Field_bit :public Field {
1930 public:
1931 uchar *bit_ptr; // position in record where 'uneven' bits store
1932 uchar bit_ofs; // offset to 'uneven' high bits
1933 uint bit_len; // number of 'uneven' high bits
1934 uint bytes_in_rec;
1935 Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1936 uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
1937 enum utype unireg_check_arg, const char *field_name_arg);
1938 enum_field_types type() const { return MYSQL_TYPE_BIT; }
1939 enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
1940 uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
1941 uint32 max_data_length() const { return (field_length + 7) / 8; }
1942 uint32 max_display_length() { return field_length; }
1943 uint size_of() const { return sizeof(*this); }
1944 Item_result result_type () const { return INT_RESULT; }
1945 int reset(void) {
1946 bzero(ptr, bytes_in_rec);
1947 if (bit_ptr && (bit_len > 0)) // reset odd bits among null bits
1948 clr_rec_bits(bit_ptr, bit_ofs, bit_len);
1949 return 0;
1951 int store(const char *to, uint length, CHARSET_INFO *charset);
1952 int store(double nr);
1953 int store(longlong nr, bool unsigned_val);
1954 int store_decimal(const my_decimal *);
1955 double val_real(void);
1956 longlong val_int(void);
1957 String *val_str(String*, String *);
1958 virtual bool str_needs_quotes() { return TRUE; }
1959 my_decimal *val_decimal(my_decimal *);
1960 int cmp(const uchar *a, const uchar *b)
1962 DBUG_ASSERT(ptr == a);
1963 return Field_bit::key_cmp(b, bytes_in_rec+test(bit_len));
1965 int cmp_binary_offset(uint row_offset)
1966 { return cmp_offset(row_offset); }
1967 int cmp_max(const uchar *a, const uchar *b, uint max_length);
1968 int key_cmp(const uchar *a, const uchar *b)
1969 { return cmp_binary((uchar *) a, (uchar *) b); }
1970 int key_cmp(const uchar *str, uint length);
1971 int cmp_offset(uint row_offset);
1972 void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
1973 { get_key_image(buff, length, itRAW); }
1974 void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
1975 { Field_bit::store((char *) buff, length, cs); }
1976 uint get_key_image(uchar *buff, uint length, imagetype type);
1977 void set_key_image(const uchar *buff, uint length)
1978 { Field_bit::store((char*) buff, length, &my_charset_bin); }
1979 void sort_string(uchar *buff, uint length)
1980 { get_key_image(buff, length, itRAW); }
1981 uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
1982 uint32 pack_length_in_rec() const { return bytes_in_rec; }
1983 uint pack_length_from_metadata(uint field_metadata);
1984 uint row_pack_length()
1985 { return (bytes_in_rec + ((bit_len > 0) ? 1 : 0)); }
1986 int compatible_field_size(uint field_metadata,
1987 const Relay_log_info *rli, uint16 mflags);
1988 void sql_type(String &str) const;
1989 virtual uchar *pack(uchar *to, const uchar *from,
1990 uint max_length, bool low_byte_first);
1991 virtual const uchar *unpack(uchar *to, const uchar *from,
1992 uint param_data, bool low_byte_first);
1993 virtual void set_default();
1995 Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
1996 uchar *new_ptr, uchar *new_null_ptr,
1997 uint new_null_bit);
1998 void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
2000 bit_ptr= bit_ptr_arg;
2001 bit_ofs= bit_ofs_arg;
2003 bool eq(Field *field)
2005 return (Field::eq(field) &&
2006 bit_ptr == ((Field_bit *)field)->bit_ptr &&
2007 bit_ofs == ((Field_bit *)field)->bit_ofs);
2009 uint is_equal(Create_field *new_field);
2010 void move_field_offset(my_ptrdiff_t ptr_diff)
2012 Field::move_field_offset(ptr_diff);
2013 bit_ptr= ADD_TO_PTR(bit_ptr, ptr_diff, uchar*);
2015 void hash(ulong *nr, ulong *nr2);
2017 private:
2018 virtual size_t do_last_null_byte() const;
2019 int do_save_field_metadata(uchar *first_byte);
2024 BIT field represented as chars for non-MyISAM tables.
2026 @todo The inheritance relationship is backwards since Field_bit is
2027 an extended version of Field_bit_as_char and not the other way
2028 around. Hence, we should refactor it to fix the hierarchy order.
2030 class Field_bit_as_char: public Field_bit {
2031 public:
2032 Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2033 uchar null_bit_arg,
2034 enum utype unireg_check_arg, const char *field_name_arg);
2035 enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
2036 uint size_of() const { return sizeof(*this); }
2037 int store(const char *to, uint length, CHARSET_INFO *charset);
2038 int store(double nr) { return Field_bit::store(nr); }
2039 int store(longlong nr, bool unsigned_val)
2040 { return Field_bit::store(nr, unsigned_val); }
2041 void sql_type(String &str) const;
2046 Create field class for CREATE TABLE
2049 class Create_field :public Sql_alloc
2051 public:
2052 const char *field_name;
2053 const char *change; // If done with alter table
2054 const char *after; // Put column after this one
2055 LEX_STRING comment; // Comment for field
2056 Item *def; // Default value
2057 enum enum_field_types sql_type;
2059 At various stages in execution this can be length of field in bytes or
2060 max number of characters.
2062 ulong length;
2064 The value of `length' as set by parser: is the number of characters
2065 for most of the types, or of bytes for BLOBs or numeric types.
2067 uint32 char_length;
2068 uint decimals, flags, pack_length, key_length;
2069 Field::utype unireg_check;
2070 TYPELIB *interval; // Which interval to use
2071 TYPELIB *save_interval; // Temporary copy for the above
2072 // Used only for UCS2 intervals
2073 List<String> interval_list;
2074 CHARSET_INFO *charset;
2075 Field::geometry_type geom_type;
2076 Field *field; // For alter table
2078 uint8 row,col,sc_length,interval_id; // For rea_create_table
2079 uint offset,pack_flag;
2080 Create_field() :after(0) {}
2081 Create_field(Field *field, Field *orig_field);
2082 /* Used to make a clone of this object for ALTER/CREATE TABLE */
2083 Create_field *clone(MEM_ROOT *mem_root) const
2084 { return new (mem_root) Create_field(*this); }
2085 void create_length_to_internal_length(void);
2087 /* Init for a tmp table field. To be extended if need be. */
2088 void init_for_tmp_table(enum_field_types sql_type_arg,
2089 uint32 max_length, uint32 decimals,
2090 bool maybe_null, bool is_unsigned);
2092 bool init(THD *thd, char *field_name, enum_field_types type, char *length,
2093 char *decimals, uint type_modifier, Item *default_value,
2094 Item *on_update_value, LEX_STRING *comment, char *change,
2095 List<String> *interval_list, CHARSET_INFO *cs,
2096 uint uint_geom_type);
2098 bool field_flags_are_binary()
2100 return (flags & (BINCMP_FLAG | BINARY_FLAG)) != 0;
2106 A class for sending info to the client
2109 class Send_field {
2110 public:
2111 const char *db_name;
2112 const char *table_name,*org_table_name;
2113 const char *col_name,*org_col_name;
2114 ulong length;
2115 uint charsetnr, flags, decimals;
2116 enum_field_types type;
2117 Send_field() {}
2122 A class for quick copying data to fields
2125 class Copy_field :public Sql_alloc {
2127 Convenience definition of a copy function returned by
2128 get_copy_func.
2130 typedef void Copy_func(Copy_field*);
2131 Copy_func *get_copy_func(Field *to, Field *from);
2132 public:
2133 uchar *from_ptr,*to_ptr;
2134 uchar *from_null_ptr,*to_null_ptr;
2135 my_bool *null_row;
2136 uint from_bit,to_bit;
2138 Number of bytes in the fields pointed to by 'from_ptr' and
2139 'to_ptr'. Usually this is the number of bytes that are copied from
2140 'from_ptr' to 'to_ptr'.
2142 For variable-length fields (VARCHAR), the first byte(s) describe
2143 the actual length of the text. For VARCHARs with length
2144 < 256 there is 1 length byte
2145 >= 256 there is 2 length bytes
2146 Thus, if from_field is VARCHAR(10), from_length (and in most cases
2147 to_length) is 11. For VARCHAR(1024), the length is 1026. @see
2148 Field_varstring::length_bytes
2150 Note that for VARCHARs, do_copy() will be do_varstring*() which
2151 only copies the length-bytes (1 or 2) + the actual length of the
2152 text instead of from/to_length bytes. @see get_copy_func()
2154 uint from_length,to_length;
2155 Field *from_field,*to_field;
2156 String tmp; // For items
2158 Copy_field() {}
2159 ~Copy_field() {}
2160 void set(Field *to,Field *from,bool save); // Field to field
2161 void set(uchar *to,Field *from); // Field to string
2162 void (*do_copy)(Copy_field *);
2163 void (*do_copy2)(Copy_field *); // Used to handle null values
2167 Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
2168 uchar *null_pos, uchar null_bit,
2169 uint pack_flag, enum_field_types field_type,
2170 CHARSET_INFO *cs,
2171 Field::geometry_type geom_type,
2172 Field::utype unireg_check,
2173 TYPELIB *interval, const char *field_name);
2174 uint pack_length_to_packflag(uint type);
2175 enum_field_types get_blob_type_from_length(ulong length);
2176 uint32 calc_pack_length(enum_field_types type,uint32 length);
2177 int set_field_to_null(Field *field);
2178 int set_field_to_null_with_conversions(Field *field, bool no_conversions);
2181 The following are for the interface with the .frm file
2184 #define FIELDFLAG_DECIMAL 1
2185 #define FIELDFLAG_BINARY 1 // Shares same flag
2186 #define FIELDFLAG_NUMBER 2
2187 #define FIELDFLAG_ZEROFILL 4
2188 #define FIELDFLAG_PACK 120 // Bits used for packing
2189 #define FIELDFLAG_INTERVAL 256 // mangled with decimals!
2190 #define FIELDFLAG_BITFIELD 512 // mangled with decimals!
2191 #define FIELDFLAG_BLOB 1024 // mangled with decimals!
2192 #define FIELDFLAG_GEOM 2048 // mangled with decimals!
2194 #define FIELDFLAG_TREAT_BIT_AS_CHAR 4096 /* use Field_bit_as_char */
2196 #define FIELDFLAG_LEFT_FULLSCREEN 8192
2197 #define FIELDFLAG_RIGHT_FULLSCREEN 16384
2198 #define FIELDFLAG_FORMAT_NUMBER 16384 // predit: ###,,## in output
2199 #define FIELDFLAG_NO_DEFAULT 16384 /* sql */
2200 #define FIELDFLAG_SUM ((uint) 32768)// predit: +#fieldflag
2201 #define FIELDFLAG_MAYBE_NULL ((uint) 32768)// sql
2202 #define FIELDFLAG_HEX_ESCAPE ((uint) 0x10000)
2203 #define FIELDFLAG_PACK_SHIFT 3
2204 #define FIELDFLAG_DEC_SHIFT 8
2205 #define FIELDFLAG_MAX_DEC 31
2206 #define FIELDFLAG_NUM_SCREEN_TYPE 0x7F01
2207 #define FIELDFLAG_ALFA_SCREEN_TYPE 0x7800
2209 #define MTYP_TYPENR(type) (type & 127) /* Remove bits from type */
2211 #define f_is_dec(x) ((x) & FIELDFLAG_DECIMAL)
2212 #define f_is_num(x) ((x) & FIELDFLAG_NUMBER)
2213 #define f_is_zerofill(x) ((x) & FIELDFLAG_ZEROFILL)
2214 #define f_is_packed(x) ((x) & FIELDFLAG_PACK)
2215 #define f_packtype(x) (((x) >> FIELDFLAG_PACK_SHIFT) & 15)
2216 #define f_decimals(x) ((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
2217 #define f_is_alpha(x) (!f_is_num(x))
2218 #define f_is_binary(x) ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
2219 #define f_is_enum(x) (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
2220 #define f_is_bitfield(x) (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
2221 #define f_is_blob(x) (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
2222 #define f_is_geom(x) (((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
2223 #define f_is_equ(x) ((x) & (1+2+FIELDFLAG_PACK+31*256))
2224 #define f_settype(x) (((int) x) << FIELDFLAG_PACK_SHIFT)
2225 #define f_maybe_null(x) (x & FIELDFLAG_MAYBE_NULL)
2226 #define f_no_default(x) (x & FIELDFLAG_NO_DEFAULT)
2227 #define f_bit_as_char(x) ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
2228 #define f_is_hex_escape(x) ((x) & FIELDFLAG_HEX_ESCAPE)