field.h 79.2 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000-2006 MySQL AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6

unknown's avatar
unknown committed
7 8 9 10
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
11

unknown's avatar
unknown committed
12 13 14 15 16 17
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/*
unknown's avatar
unknown committed
18 19
  Because of the function new_field() all field classes that have static
  variables must declare the size_of() member function.
unknown's avatar
unknown committed
20 21
*/

22
#ifdef USE_PRAGMA_INTERFACE
unknown's avatar
unknown committed
23 24 25 26
#pragma interface			/* gcc class implementation */
#endif

#define NOT_FIXED_DEC			31
unknown's avatar
unknown committed
27
#define DATETIME_DEC                     6
28
const uint32 max_field_size= (uint32) 4294967295U;
unknown's avatar
unknown committed
29 30

class Send_field;
31
class Protocol;
unknown's avatar
unknown committed
32
class Create_field;
unknown's avatar
unknown committed
33
struct st_cache_field;
34
int field_conv(Field *to,Field *from);
unknown's avatar
unknown committed
35

36 37 38 39 40 41 42 43 44 45 46
inline uint get_enum_pack_length(int elements)
{
  return elements < 256 ? 1 : 2;
}

inline uint get_set_pack_length(int elements)
{
  uint len= (elements + 7) / 8;
  return len > 4 ? 8 : len;
}

47 48
class Field
{
unknown's avatar
unknown committed
49
  Field(const Item &);				/* Prevent use of these */
unknown's avatar
unknown committed
50 51
  void operator=(Field &);
public:
52
  static void *operator new(size_t size) {return sql_alloc(size); }
unknown's avatar
Merge  
unknown committed
53
  static void operator delete(void *ptr_arg, size_t size) { TRASH(ptr_arg, size); }
unknown's avatar
unknown committed
54

55
  uchar		*ptr;			// Position to field in record
56
  uchar		*null_ptr;		// Byte where null_bit is
57 58 59 60
  /*
    Note that you can use table->in_use as replacement for current_thd member 
    only inside of val_*() and store() members (e.g. you can't use it in cons)
  */
61
  struct st_table *table;		// Pointer for table
62
  struct st_table *orig_table;		// Pointer to original table
unknown's avatar
Merge  
unknown committed
63
  const char	**table_name, *field_name;
64
  LEX_STRING	comment;
unknown's avatar
unknown committed
65
  /* Field is part of the following keys */
66 67
  key_map	key_start, part_of_key, part_of_key_not_clustered;
  key_map       part_of_sortkey;
68 69 70 71 72 73 74 75
  /* 
    We use three additional unireg types for TIMESTAMP to overcome limitation 
    of current binary format of .frm file. We'd like to be able to support 
    NOW() as default and on update value for such fields but unable to hold 
    this info anywhere except unireg_check field. This issue will be resolved
    in more clean way with transition to new text based .frm format.
    See also comment for Field_timestamp::Field_timestamp().
  */
unknown's avatar
unknown committed
76 77
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
		CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
78 79
                BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
                TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
unknown's avatar
unknown committed
80 81 82 83 84 85
  enum geometry_type
  {
    GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
    GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
    GEOM_GEOMETRYCOLLECTION = 7
  };
unknown's avatar
unknown committed
86
  enum imagetype { itRAW, itMBR};
unknown's avatar
unknown committed
87

unknown's avatar
unknown committed
88
  utype		unireg_check;
89
  uint32	field_length;		// Length of field
90
  uint32	flags;
91
  uint16        field_index;            // field number in fields array
92
  uchar		null_bit;		// Bit used to test null bit
93 94 95 96 97 98 99 100 101 102
  /**
     If true, this field was created in create_tmp_field_from_item from a NULL
     value. This means that the type of the field is just a guess, and the type
     may be freely coerced to another type.

     @see create_tmp_field_from_item
     @see Item_type_holder::get_real_type

   */
  bool is_created_from_null_item;
unknown's avatar
unknown committed
103

104 105 106
  Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
        uchar null_bit_arg, utype unireg_check_arg,
        const char *field_name_arg);
unknown's avatar
unknown committed
107
  virtual ~Field() {}
unknown's avatar
unknown committed
108
  /* Store functions returns 1 on overflow and -1 on fatal error */
109
  virtual int  store(const char *to, uint length,CHARSET_INFO *cs)=0;
110
  virtual int  store(double nr)=0;
111
  virtual int  store(longlong nr, bool unsigned_val)=0;
unknown's avatar
unknown committed
112
  virtual int  store_decimal(const my_decimal *d)=0;
113
  virtual int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
114 115
  int store(const char *to, uint length, CHARSET_INFO *cs,
            enum_check_fields check_level);
unknown's avatar
unknown committed
116 117
  virtual double val_real(void)=0;
  virtual longlong val_int(void)=0;
unknown's avatar
unknown committed
118
  virtual my_decimal *val_decimal(my_decimal *);
unknown's avatar
unknown committed
119 120 121
  inline String *val_str(String *str) { return val_str(str, str); }
  /*
     val_str(buf1, buf2) gets two buffers and should use them as follows:
122 123 124 125 126 127 128 129 130 131
     if it needs a temp buffer to convert result to string - use buf1
       example Field_tiny::val_str()
     if the value exists as a string already - use buf2
       example Field_string::val_str()
     consequently, buf2 may be created as 'String buf;' - no memory
     will be allocated for it. buf1 will be allocated to hold a
     value if it's too small. Using allocated buffer for buf2 may result in
     an unnecessary free (and later, may be an alloc).
     This trickery is used to decrease a number of malloc calls.
  */
unknown's avatar
unknown committed
132
  virtual String *val_str(String*,String *)=0;
unknown's avatar
Merge  
unknown committed
133
  String *val_int_as_str(String *val_buffer, my_bool unsigned_flag);
134 135 136 137 138
  /*
   str_needs_quotes() returns TRUE if the value returned by val_str() needs
   to be quoted when used in constructing an SQL query.
  */
  virtual bool str_needs_quotes() { return FALSE; }
unknown's avatar
unknown committed
139 140
  virtual Item_result result_type () const=0;
  virtual Item_result cmp_type () const { return result_type(); }
141
  virtual Item_result cast_to_int_type () const { return result_type(); }
142
  static bool type_can_have_key_part(enum_field_types);
143 144
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
  static Item_result result_merge_type(enum_field_types);
145
  virtual bool eq(Field *field)
unknown's avatar
Merge  
unknown committed
146 147 148 149
  {
    return (ptr == field->ptr && null_ptr == field->null_ptr &&
            null_bit == field->null_bit);
  }
unknown's avatar
unknown committed
150
  virtual bool eq_def(Field *field);
151 152 153 154 155 156
  
  /*
    pack_length() returns size (in bytes) used to store field data in memory
    (i.e. it returns the maximum size of the field in a row of the table,
    which is located in RAM).
  */
unknown's avatar
unknown committed
157
  virtual uint32 pack_length() const { return (uint32) field_length; }
158 159 160 161 162 163

  /*
    pack_length_in_rec() returns size (in bytes) used to store field data on
    storage (i.e. it returns the maximal size of the field in a row of the
    table, which is located on disk).
  */
unknown's avatar
Merge  
unknown committed
164
  virtual uint32 pack_length_in_rec() const { return pack_length(); }
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  virtual int compatible_field_size(uint field_metadata);
  virtual uint pack_length_from_metadata(uint field_metadata)
  { return field_metadata; }
  /*
    This method is used to return the size of the data in a row-based
    replication row record. The default implementation of returning 0 is
    designed to allow fields that do not use metadata to return TRUE (1)
    from compatible_field_size() which uses this function in the comparison.
    The default value for field metadata for fields that do not have 
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.

    Note: While most classes that override this method return pack_length(),
    the classes Field_string, Field_varstring, and Field_blob return 
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
  */
  virtual uint row_pack_length() { return 0; }
  virtual int save_field_metadata(uchar *first_byte)
  { return do_save_field_metadata(first_byte); }
183 184 185 186

  /*
    data_length() return the "real size" of the data in memory.
  */
187
  virtual uint32 data_length() { return pack_length(); }
188
  virtual uint32 sort_length() const { return pack_length(); }
189 190 191 192 193 194 195 196 197 198 199

  /**
     Get the maximum size of the data in packed format.

     @return Maximum data length of the field when packed using the
     Field::pack() function.
   */
  virtual uint32 max_data_length() const {
    return pack_length();
  };

200
  virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
unknown's avatar
unknown committed
201
  virtual void reset_fields() {}
unknown's avatar
unknown committed
202 203
  virtual void set_default()
  {
204
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
unknown's avatar
unknown committed
205
					  table->record[0]);
206
    memcpy(ptr, ptr + l_offset, pack_length());
207 208
    if (null_ptr)
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
209
		  null_ptr[l_offset] & null_bit);
unknown's avatar
unknown committed
210
  }
unknown's avatar
unknown committed
211 212 213 214 215 216
  virtual bool binary() const { return 1; }
  virtual bool zero_pack() const { return 1; }
  virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
  virtual uint32 key_length() const { return pack_length(); }
  virtual enum_field_types type() const =0;
  virtual enum_field_types real_type() const { return type(); }
217 218
  inline  int cmp(const uchar *str) { return cmp(ptr,str); }
  virtual int cmp_max(const uchar *a, const uchar *b, uint max_len)
219
    { return cmp(a, b); }
220 221
  virtual int cmp(const uchar *,const uchar *)=0;
  virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L)
unknown's avatar
unknown committed
222
  { return memcmp(a,b,pack_length()); }
223 224 225
  virtual int cmp_offset(uint row_offset)
  { return cmp(ptr,ptr+row_offset); }
  virtual int cmp_binary_offset(uint row_offset)
unknown's avatar
Merge  
unknown committed
226
  { return cmp_binary(ptr, ptr+row_offset); };
227 228 229 230
  virtual int key_cmp(const uchar *a,const uchar *b)
  { return cmp(a, b); }
  virtual int key_cmp(const uchar *str, uint length)
  { return cmp(ptr,str); }
unknown's avatar
unknown committed
231
  virtual uint decimals() const { return 0; }
unknown's avatar
unknown committed
232 233 234 235 236
  /*
    Caller beware: sql_type can change str.Ptr, so check
    ptr() to see if it changed if you are using your own buffer
    in str and restore it with set() if needed
  */
237
  virtual void sql_type(String &str) const =0;
238
  virtual uint size_of() const =0;		// For new field
unknown's avatar
unknown committed
239
  inline bool is_null(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
240
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
unknown's avatar
unknown committed
241
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
242
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
unknown's avatar
unknown committed
243 244 245 246
  inline bool is_null_in_record(const uchar *record)
  {
    if (!null_ptr)
      return 0;
247
    return test(record[(uint) (null_ptr -table->record[0])] &
unknown's avatar
unknown committed
248 249
		null_bit);
  }
unknown's avatar
unknown committed
250 251 252 253 254 255
  inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
  {
    if (!null_ptr)
      return 0;
    return test(null_ptr[offset] & null_bit);
  }
unknown's avatar
unknown committed
256
  inline void set_null(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
257
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
unknown's avatar
unknown committed
258
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
259
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
unknown's avatar
unknown committed
260 261
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
  inline bool real_maybe_null(void) { return null_ptr != 0; }
262

263 264 265 266
  enum {
    LAST_NULL_BYTE_UNDEF= 0
  };

267
  /*
268 269 270 271 272 273 274 275 276 277 278 279 280
    Find the position of the last null byte for the field.

    SYNOPSIS
      last_null_byte()

    DESCRIPTION
      Return a pointer to the last byte of the null bytes where the
      field conceptually is placed.

    RETURN VALUE
      The position of the last null byte relative to the beginning of
      the record. If the field does not use any bits of the null
      bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
281
   */
282 283
  size_t last_null_byte() const {
    size_t bytes= do_last_null_byte();
unknown's avatar
unknown committed
284
    DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
285 286 287 288
    DBUG_ASSERT(bytes <= table->s->null_bytes);
    return bytes;
  }

unknown's avatar
Merge  
unknown committed
289
  virtual void make_field(Send_field *);
290
  virtual void sort_string(uchar *buff,uint length)=0;
291
  virtual bool optimize_range(uint idx, uint part);
292 293 294 295 296 297 298 299
  /*
    This should be true for fields which, when compared with constant
    items, can be casted to longlong. In this case we will at 'fix_fields'
    stage cast the constant items to longlongs and at the execution stage
    use field->val_int() for comparison.  Used to optimize clauses like
    'a_column BETWEEN date_const, date_const'.
  */
  virtual bool can_be_compared_as_longlong() const { return FALSE; }
300
  virtual void free() {}
301 302
  virtual Field *new_field(MEM_ROOT *root, struct st_table *new_table,
                           bool keep_type);
unknown's avatar
Merge  
unknown committed
303
  virtual Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
304
                               uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
Merge  
unknown committed
305
                               uint new_null_bit);
unknown's avatar
unknown committed
306
  Field *clone(MEM_ROOT *mem_root, struct st_table *new_table);
307
  inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
unknown's avatar
unknown committed
308 309 310
  {
    ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
  }
311
  inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
unknown's avatar
unknown committed
312
  virtual void move_field_offset(my_ptrdiff_t ptr_diff)
unknown's avatar
unknown committed
313
  {
314
    ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
unknown's avatar
unknown committed
315 316 317
    if (null_ptr)
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
  }
318
  virtual void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
319
    { memcpy(buff,ptr,length); }
320
  virtual void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
321
    { memcpy(ptr,buff,length); }
unknown's avatar
unknown committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349


  /*
    Copy a field part into an output buffer.

    SYNOPSIS
      Field::get_key_image()
      buff   [out] output buffer
      length       output buffer size
      type         itMBR for geometry blobs, otherwise itRAW

    DESCRIPTION
      This function makes a copy of field part of size equal to or
      less than "length" parameter value.
      For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
      is padded by zero byte.

    NOTES
      For variable length character fields (i.e. UTF-8) the "length"
      parameter means a number of output buffer bytes as if all field
      characters have maximal possible size (mbmaxlen). In the other words,
      "length" parameter is a number of characters multiplied by
      field_charset->mbmaxlen.

    RETURN
      Number of copied bytes (excluding padded zero bytes -- see above).
  */

350
  virtual uint get_key_image(uchar *buff, uint length, imagetype type)
unknown's avatar
unknown committed
351
  {
352
    get_image(buff, length, &my_charset_bin);
unknown's avatar
unknown committed
353 354
    return length;
  }
355
  virtual void set_key_image(const uchar *buff,uint length)
unknown's avatar
Merge  
unknown committed
356
    { set_image(buff,length, &my_charset_bin); }
unknown's avatar
unknown committed
357 358 359 360 361 362 363
  inline longlong val_int_offset(uint row_offset)
    {
      ptr+=row_offset;
      longlong tmp=val_int();
      ptr-=row_offset;
      return tmp;
    }
364
  inline longlong val_int(const uchar *new_ptr)
365
  {
366
    uchar *old_ptr= ptr;
367
    longlong return_value;
368
    ptr= (uchar*) new_ptr;
369 370 371 372
    return_value= val_int();
    ptr= old_ptr;
    return return_value;
  }
373
  inline String *val_str(String *str, const uchar *new_ptr)
unknown's avatar
Merge  
unknown committed
374
  {
375 376
    uchar *old_ptr= ptr;
    ptr= (uchar*) new_ptr;
unknown's avatar
Merge  
unknown committed
377 378 379 380
    val_str(str);
    ptr= old_ptr;
    return str;
  }
unknown's avatar
unknown committed
381
  virtual bool send_binary(Protocol *protocol);
382 383 384 385 386 387 388

  virtual uchar *pack(uchar *to, const uchar *from,
                      uint max_length, bool low_byte_first);
  /**
     @overload Field::pack(uchar*, const uchar*, uint, bool)
  */
  uchar *pack(uchar *to, const uchar *from)
unknown's avatar
unknown committed
389
  {
390 391 392
    DBUG_ENTER("Field::pack");
    uchar *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
    DBUG_RETURN(result);
unknown's avatar
unknown committed
393
  }
394 395 396 397 398 399 400

  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first);
  /**
     @overload Field::unpack(uchar*, const uchar*, uint, bool)
  */
  const uchar *unpack(uchar* to, const uchar *from)
unknown's avatar
unknown committed
401
  {
402 403 404
    DBUG_ENTER("Field::unpack");
    const uchar *result= unpack(to, from, 0U, table->s->db_low_byte_first);
    DBUG_RETURN(result);
unknown's avatar
unknown committed
405
  }
406 407 408

  virtual uchar *pack_key(uchar* to, const uchar *from,
                          uint max_length, bool low_byte_first)
409
  {
410
    return pack(to, from, max_length, low_byte_first);
411
  }
412
  virtual uchar *pack_key_from_key_image(uchar* to, const uchar *from,
413
					uint max_length, bool low_byte_first)
unknown's avatar
unknown committed
414
  {
415
    return pack(to, from, max_length, low_byte_first);
unknown's avatar
unknown committed
416
  }
417
  virtual const uchar *unpack_key(uchar* to, const uchar *from,
418
                                  uint max_length, bool low_byte_first)
419
  {
420
    return unpack(to, from, max_length, low_byte_first);
421
  }
422
  virtual uint packed_col_length(const uchar *to, uint length)
423
  { return length;}
unknown's avatar
unknown committed
424
  virtual uint max_packed_col_length(uint max_length)
425
  { return max_length;}
unknown's avatar
unknown committed
426

427
  virtual int pack_cmp(const uchar *a,const uchar *b, uint key_length_arg,
unknown's avatar
Merge  
unknown committed
428
                       my_bool insert_or_update)
unknown's avatar
unknown committed
429
  { return cmp(a,b); }
430
  virtual int pack_cmp(const uchar *b, uint key_length_arg,
unknown's avatar
Merge  
unknown committed
431
                       my_bool insert_or_update)
432
  { return cmp(ptr,b); }
433
  uint offset(uchar *record)
434
  {
435
    return (uint) (ptr - record);
436
  }
unknown's avatar
unknown committed
437 438
  void copy_from_tmp(int offset);
  uint fill_cache_field(struct st_cache_field *copy);
439 440
  virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  virtual bool get_time(MYSQL_TIME *ltime);
unknown's avatar
unknown committed
441
  virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
442
  virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
443
  virtual bool has_charset(void) const { return FALSE; }
444
  virtual void set_charset(CHARSET_INFO *charset_arg) { }
445 446
  virtual enum Derivation derivation(void) const
  { return DERIVATION_IMPLICIT; }
447
  virtual void set_derivation(enum Derivation derivation_arg) { }
448
  bool set_warning(MYSQL_ERROR::enum_warning_level, unsigned int code,
449
                   int cuted_increment);
450
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code, 
451 452
                            const char *str, uint str_len,
                            timestamp_type ts_type, int cuted_increment);
453
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code, 
454 455
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment);
456
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, const uint code, 
457
                            double nr, timestamp_type ts_type);
unknown's avatar
unknown committed
458 459 460 461 462
  inline bool check_overflow(int op_result)
  {
    return (op_result == E_DEC_OVERFLOW);
  }
  int warn_if_overflow(int op_result);
unknown's avatar
unknown committed
463 464 465 466 467 468
  void init(TABLE *table_arg)
  {
    orig_table= table= table_arg;
    table_name= &table_arg->alias;
  }

unknown's avatar
unknown committed
469
  /* maximum possible display length */
470
  virtual uint32 max_display_length()= 0;
unknown's avatar
unknown committed
471

unknown's avatar
unknown committed
472
  virtual uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
473 474 475
  /* convert decimal to longlong with overflow check */
  longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
                                    int *err);
476
  /* The max. number of characters */
477
  inline uint32 char_length() const
478 479 480 481
  {
    return field_length / charset()->mbmaxlen;
  }

482 483 484 485
  virtual geometry_type get_geometry_type()
  {
    /* shouldn't get here. */
    DBUG_ASSERT(0);
486
    return GEOM_GEOMETRY;
487
  }
488 489
  /* Hash value */
  virtual void hash(ulong *nr, ulong *nr2);
unknown's avatar
unknown committed
490
  friend bool reopen_table(THD *,struct st_table *,bool);
491
  friend int cre_myisam(char * name, register TABLE *form, uint options,
unknown's avatar
unknown committed
492 493 494 495 496 497 498 499 500 501 502 503
			ulonglong auto_increment_value);
  friend class Copy_field;
  friend class Item_avg_field;
  friend class Item_std_field;
  friend class Item_sum_num;
  friend class Item_sum_sum;
  friend class Item_sum_str;
  friend class Item_sum_count;
  friend class Item_sum_avg;
  friend class Item_sum_std;
  friend class Item_sum_min;
  friend class Item_sum_max;
504
  friend class Item_func_group_concat;
505 506

private:
507 508 509 510 511 512 513 514 515 516 517
  /*
    Primitive for implementing last_null_byte().

    SYNOPSIS
      do_last_null_byte()

    DESCRIPTION
      Primitive for the implementation of the last_null_byte()
      function. This represents the inheritance interface and can be
      overridden by subclasses.
   */
518
  virtual size_t do_last_null_byte() const;
519 520 521 522 523 524 525 526 527 528 529 530 531

/**
   Retrieve the field metadata for fields.

   This default implementation returns 0 and saves 0 in the metadata_ptr
   value.

   @param   metadata_ptr   First byte of field metadata

   @returns 0 no bytes written.
*/
  virtual int do_save_field_metadata(uchar *metadata_ptr)
  { return 0; }
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

protected:
  /*
    Helper function to pack()/unpack() int32 values
  */
  static void handle_int32(uchar *to, const uchar *from,
                           bool low_byte_first_from, bool low_byte_first_to)
  {
    int32 val;
#ifdef WORDS_BIGENDIAN
    if (low_byte_first_from)
      val = sint4korr(from);
    else
#endif
      longget(val, from);

#ifdef WORDS_BIGENDIAN
    if (low_byte_first_to)
      int4store(to, val);
    else
#endif
      longstore(to, val);
  }

  /*
    Helper function to pack()/unpack() int64 values
  */
  static void handle_int64(uchar* to, const uchar *from,
                           bool low_byte_first_from, bool low_byte_first_to)
  {
    int64 val;
#ifdef WORDS_BIGENDIAN
    if (low_byte_first_from)
      val = sint8korr(from);
    else
#endif
      longlongget(val, from);

#ifdef WORDS_BIGENDIAN
    if (low_byte_first_to)
      int8store(to, val);
    else
#endif
      longlongstore(to, val);
  }

  uchar *pack_int32(uchar *to, const uchar *from, bool low_byte_first_to)
  {
    handle_int32(to, from, table->s->db_low_byte_first, low_byte_first_to);
    return to  + sizeof(int32);
  }

  const uchar *unpack_int32(uchar* to, const uchar *from,
                            bool low_byte_first_from)
  {
    handle_int32(to, from, low_byte_first_from, table->s->db_low_byte_first);
    return from + sizeof(int32);
  }

  uchar *pack_int64(uchar* to, const uchar *from, bool low_byte_first_to)
  {
    handle_int64(to, from, table->s->db_low_byte_first, low_byte_first_to);
    return to + sizeof(int64);
  }

  const uchar *unpack_int64(uchar* to, const uchar *from,
                            bool low_byte_first_from)
  {
    handle_int64(to, from, low_byte_first_from, table->s->db_low_byte_first);
    return from + sizeof(int64);
  }
unknown's avatar
unknown committed
603 604 605 606 607 608
};


class Field_num :public Field {
public:
  const uint8 dec;
609
  bool zerofill,unsigned_flag;	// Purify cannot handle bit fields
610
  Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
611
	    uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
612
	    const char *field_name_arg,
unknown's avatar
unknown committed
613
            uint8 dec_arg, bool zero_arg, bool unsigned_arg);
unknown's avatar
unknown committed
614 615 616
  Item_result result_type () const { return REAL_RESULT; }
  void prepend_zeros(String *value);
  void add_zerofill_and_unsigned(String &res) const;
unknown's avatar
unknown committed
617
  friend class Create_field;
unknown's avatar
unknown committed
618
  void make_field(Send_field *);
unknown's avatar
unknown committed
619
  uint decimals() const { return (uint) dec; }
unknown's avatar
unknown committed
620 621
  uint size_of() const { return sizeof(*this); }
  bool eq_def(Field *field);
unknown's avatar
unknown committed
622 623
  int store_decimal(const my_decimal *);
  my_decimal *val_decimal(my_decimal *);
unknown's avatar
unknown committed
624
  uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
625 626 627 628 629
  int check_int(CHARSET_INFO *cs, const char *str, int length,
                const char *int_end, int error);
  bool get_int(CHARSET_INFO *cs, const char *from, uint len, 
               longlong *rnd, ulonglong unsigned_max, 
               longlong signed_min, longlong signed_max);
unknown's avatar
unknown committed
630 631 632 633
};


class Field_str :public Field {
634 635
protected:
  CHARSET_INFO *field_charset;
636
  enum Derivation field_derivation;
unknown's avatar
unknown committed
637
public:
638
  Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
639
	    uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
640
	    const char *field_name_arg, CHARSET_INFO *charset);
unknown's avatar
unknown committed
641 642
  Item_result result_type () const { return STRING_RESULT; }
  uint decimals() const { return NOT_FIXED_DEC; }
unknown's avatar
unknown committed
643
  int  store(double nr);
644
  int  store(longlong nr, bool unsigned_val)=0;
unknown's avatar
unknown committed
645
  int  store_decimal(const my_decimal *);
unknown's avatar
unknown committed
646
  int  store(const char *to,uint length,CHARSET_INFO *cs)=0;
unknown's avatar
unknown committed
647
  uint size_of() const { return sizeof(*this); }
unknown's avatar
unknown committed
648
  CHARSET_INFO *charset(void) const { return field_charset; }
649
  void set_charset(CHARSET_INFO *charset_arg) { field_charset= charset_arg; }
650 651 652
  enum Derivation derivation(void) const { return field_derivation; }
  virtual void set_derivation(enum Derivation derivation_arg)
  { field_derivation= derivation_arg; }
653
  bool binary() const { return field_charset == &my_charset_bin; }
654
  uint32 max_display_length() { return field_length; }
unknown's avatar
unknown committed
655
  friend class Create_field;
unknown's avatar
unknown committed
656
  my_decimal *val_decimal(my_decimal *);
657
  virtual bool str_needs_quotes() { return TRUE; }
unknown's avatar
unknown committed
658 659
  bool compare_str_field_flags(Create_field *new_field, uint32 flags);
  uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
660 661
};

662 663 664

/* base class for Field_string, Field_varstring and Field_blob */

unknown's avatar
unknown committed
665 666
class Field_longstr :public Field_str
{
unknown's avatar
unknown committed
667
protected:
668 669
  int report_if_important_data(const char *ptr, const char *end,
                               bool count_spaces);
unknown's avatar
unknown committed
670
public:
671
  Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
672
                uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
673
                const char *field_name_arg, CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
674
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
unknown's avatar
unknown committed
675
               field_name_arg, charset_arg)
unknown's avatar
unknown committed
676 677 678
    {}

  int store_decimal(const my_decimal *d);
679
  uint32 max_data_length() const;
unknown's avatar
unknown committed
680 681 682 683 684
};

/* base class for float and double and decimal (old one) */
class Field_real :public Field_num {
public:
685
  my_bool not_fixed;
unknown's avatar
unknown committed
686

687
  Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
688 689 690 691
             uchar null_bit_arg, utype unireg_check_arg,
             const char *field_name_arg,
             uint8 dec_arg, bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
692
               field_name_arg, dec_arg, zero_arg, unsigned_arg),
693
    not_fixed(dec_arg >= NOT_FIXED_DEC)
unknown's avatar
unknown committed
694 695 696
    {}
  int store_decimal(const my_decimal *);
  my_decimal *val_decimal(my_decimal *);
697
  int truncate(double *nr, double max_length);
698
  uint32 max_display_length() { return field_length; }
699
  uint size_of() const { return sizeof(*this); }
700 701 702 703
  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first);
  virtual uchar *pack(uchar* to, const uchar *from,
                      uint max_length, bool low_byte_first);
unknown's avatar
unknown committed
704 705 706
};


unknown's avatar
unknown committed
707
class Field_decimal :public Field_real {
unknown's avatar
unknown committed
708
public:
709
  Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
710
		uchar null_bit_arg,
unknown's avatar
unknown committed
711
		enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
712
		uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
713
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
714
                unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
715
                dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
716
    {}
717
  enum_field_types type() const { return MYSQL_TYPE_DECIMAL;}
unknown's avatar
unknown committed
718
  enum ha_base_keytype key_type() const
719
  { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
720 721 722
  int reset(void);
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
723
  int store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
724 725 726
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
727 728
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
729 730 731
  void overflow(bool negative);
  bool zero_pack() const { return 0; }
  void sql_type(String &str) const;
732 733 734 735 736 737 738 739 740 741
  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first)
  {
    return Field::unpack(to, from, param_data, low_byte_first);
  }
  virtual uchar *pack(uchar* to, const uchar *from,
                      uint max_length, bool low_byte_first)
  {
    return Field::pack(to, from, max_length, low_byte_first);
  }
unknown's avatar
unknown committed
742 743 744
};


unknown's avatar
unknown committed
745 746
/* New decimal/numeric field which use fixed point arithmetic */
class Field_new_decimal :public Field_num {
747 748
private:
  int do_save_field_metadata(uchar *first_byte);
unknown's avatar
unknown committed
749
public:
unknown's avatar
unknown committed
750 751
  /* The maximum number of decimal digits can be stored */
  uint precision;
unknown's avatar
unknown committed
752
  uint bin_size;
unknown's avatar
unknown committed
753 754 755 756 757 758
  /*
    Constructors take max_length of the field as a parameter - not the
    precision as the number of decimal digits allowed.
    So for example we need to count length from precision handling
    CREATE TABLE ( DECIMAL(x,y)) 
  */
759
  Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
760 761 762 763
                    uchar null_bit_arg,
                    enum utype unireg_check_arg, const char *field_name_arg,
                    uint8 dec_arg, bool zero_arg, bool unsigned_arg);
  Field_new_decimal(uint32 len_arg, bool maybe_null_arg,
unknown's avatar
unknown committed
764
                    const char *field_name_arg, uint8 dec_arg,
unknown's avatar
unknown committed
765
                    bool unsigned_arg);
766
  enum_field_types type() const { return MYSQL_TYPE_NEWDECIMAL;}
unknown's avatar
unknown committed
767 768
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
  Item_result result_type () const { return DECIMAL_RESULT; }
769
  int  reset(void);
unknown's avatar
unknown committed
770 771 772 773
  bool store_value(const my_decimal *decimal_value);
  void set_value_on_overflow(my_decimal *decimal_value, bool sign);
  int  store(const char *to, uint length, CHARSET_INFO *charset);
  int  store(double nr);
774
  int  store(longlong nr, bool unsigned_val);
775
  int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
unknown's avatar
unknown committed
776 777 778 779 780
  int  store_decimal(const my_decimal *);
  double val_real(void);
  longlong val_int(void);
  my_decimal *val_decimal(my_decimal *);
  String *val_str(String*, String *);
781 782
  int cmp(const uchar *, const uchar *);
  void sort_string(uchar *buff, uint length);
unknown's avatar
unknown committed
783 784
  bool zero_pack() const { return 0; }
  void sql_type(String &str) const;
785
  uint32 max_display_length() { return field_length; }
unknown's avatar
unknown committed
786 787
  uint size_of() const { return sizeof(*this); } 
  uint32 pack_length() const { return (uint32) bin_size; }
788 789 790
  uint pack_length_from_metadata(uint field_metadata);
  uint row_pack_length() { return pack_length(); }
  int compatible_field_size(uint field_metadata);
unknown's avatar
unknown committed
791
  uint is_equal(Create_field *new_field);
792 793
  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first);
unknown's avatar
unknown committed
794 795 796
};


unknown's avatar
unknown committed
797 798
class Field_tiny :public Field_num {
public:
799
  Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
800
	     uchar null_bit_arg,
unknown's avatar
unknown committed
801 802 803
	     enum utype unireg_check_arg, const char *field_name_arg,
	     bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
804
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
805 806 807
	       0, zero_arg,unsigned_arg)
    {}
  enum Item_result result_type () const { return INT_RESULT; }
808
  enum_field_types type() const { return MYSQL_TYPE_TINY;}
unknown's avatar
unknown committed
809 810
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
811 812
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
813
  int store(longlong nr, bool unsigned_val);
814
  int reset(void) { ptr[0]=0; return 0; }
unknown's avatar
unknown committed
815 816 817
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
818
  bool send_binary(Protocol *protocol);
819 820
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
821 822
  uint32 pack_length() const { return 1; }
  void sql_type(String &str) const;
823
  uint32 max_display_length() { return 4; }
824 825 826 827 828 829 830 831 832 833 834 835 836 837

  virtual uchar *pack(uchar* to, const uchar *from,
                      uint max_length, bool low_byte_first)
  {
    *to= *from;
    return to + 1;
  }

  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first)
  {
    *to= *from;
    return from + 1;
  }
unknown's avatar
unknown committed
838 839 840 841 842
};


class Field_short :public Field_num {
public:
843
  Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
844
	      uchar null_bit_arg,
unknown's avatar
unknown committed
845 846 847
	      enum utype unireg_check_arg, const char *field_name_arg,
	      bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
848
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
849 850
	       0, zero_arg,unsigned_arg)
    {}
851
  Field_short(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
852
	      bool unsigned_arg)
853
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
854
	       NONE, field_name_arg, 0, 0, unsigned_arg)
855
    {}
unknown's avatar
unknown committed
856
  enum Item_result result_type () const { return INT_RESULT; }
857
  enum_field_types type() const { return MYSQL_TYPE_SHORT;}
unknown's avatar
unknown committed
858 859
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
860 861
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
862
  int store(longlong nr, bool unsigned_val);
863
  int reset(void) { ptr[0]=ptr[1]=0; return 0; }
unknown's avatar
unknown committed
864 865 866
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
867
  bool send_binary(Protocol *protocol);
868 869
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
870 871
  uint32 pack_length() const { return 2; }
  void sql_type(String &str) const;
872
  uint32 max_display_length() { return 6; }
unknown's avatar
unknown committed
873

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  virtual uchar *pack(uchar* to, const uchar *from,
                      uint max_length, bool low_byte_first)
  {
    int16 val;
#ifdef WORDS_BIGENDIAN
    if (table->s->db_low_byte_first)
      val = sint2korr(from);
    else
#endif
      shortget(val, from);

#ifdef WORDS_BIGENDIAN
    if (low_byte_first)
      int2store(to, val);
    else
#endif
      shortstore(to, val);
    return to + sizeof(val);
  }

  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first)
  {
    int16 val;
#ifdef WORDS_BIGENDIAN
    if (low_byte_first)
      val = sint2korr(from);
    else
#endif
      shortget(val, from);

#ifdef WORDS_BIGENDIAN
    if (table->s->db_low_byte_first)
      int2store(to, val);
    else
#endif
      shortstore(to, val);
    return from + sizeof(val);
  }
};
unknown's avatar
unknown committed
914 915 916

class Field_medium :public Field_num {
public:
917
  Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
918
	      uchar null_bit_arg,
unknown's avatar
unknown committed
919 920 921
	      enum utype unireg_check_arg, const char *field_name_arg,
	      bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
922
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
923 924 925
	       0, zero_arg,unsigned_arg)
    {}
  enum Item_result result_type () const { return INT_RESULT; }
926
  enum_field_types type() const { return MYSQL_TYPE_INT24;}
unknown's avatar
unknown committed
927 928
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
929 930
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
931
  int store(longlong nr, bool unsigned_val);
932
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
unknown's avatar
unknown committed
933 934 935
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
936
  bool send_binary(Protocol *protocol);
937 938
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
939 940
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
941
  uint32 max_display_length() { return 8; }
942 943 944 945 946 947 948 949 950 951 952 953

  virtual uchar *pack(uchar* to, const uchar *from,
                      uint max_length, bool low_byte_first)
  {
    return Field::pack(to, from, max_length, low_byte_first);
  }

  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first)
  {
    return Field::unpack(to, from, param_data, low_byte_first);
  }
unknown's avatar
unknown committed
954 955 956 957 958
};


class Field_long :public Field_num {
public:
959
  Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
960
	     uchar null_bit_arg,
unknown's avatar
unknown committed
961 962 963
	     enum utype unireg_check_arg, const char *field_name_arg,
	     bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
964
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
965 966 967
	       0, zero_arg,unsigned_arg)
    {}
  Field_long(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
968
	     bool unsigned_arg)
969
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
970
	       NONE, field_name_arg,0,0,unsigned_arg)
unknown's avatar
unknown committed
971 972
    {}
  enum Item_result result_type () const { return INT_RESULT; }
973
  enum_field_types type() const { return MYSQL_TYPE_LONG;}
unknown's avatar
unknown committed
974 975
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
976 977
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
978
  int store(longlong nr, bool unsigned_val);
979
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
unknown's avatar
unknown committed
980 981
  double val_real(void);
  longlong val_int(void);
982
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
983
  String *val_str(String*,String *);
984 985
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
986 987
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
988
  uint32 max_display_length() { return MY_INT32_NUM_DECIMAL_DIGITS; }
989
  virtual uchar *pack(uchar* to, const uchar *from,
990 991
                      uint max_length __attribute__((unused)),
                      bool low_byte_first)
992
  {
993
    return pack_int32(to, from, low_byte_first);
994 995
  }
  virtual const uchar *unpack(uchar* to, const uchar *from,
996 997
                              uint param_data __attribute__((unused)),
                              bool low_byte_first)
998
  {
999
    return unpack_int32(to, from, low_byte_first);
1000
  }
unknown's avatar
unknown committed
1001 1002 1003 1004 1005 1006
};


#ifdef HAVE_LONG_LONG
class Field_longlong :public Field_num {
public:
1007
  Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1008
	      uchar null_bit_arg,
unknown's avatar
unknown committed
1009 1010 1011
	      enum utype unireg_check_arg, const char *field_name_arg,
	      bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1012
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
1013 1014
	       0, zero_arg,unsigned_arg)
    {}
1015 1016
  Field_longlong(uint32 len_arg,bool maybe_null_arg,
		 const char *field_name_arg,
unknown's avatar
unknown committed
1017
		  bool unsigned_arg)
1018
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
1019
	       NONE, field_name_arg,0,0,unsigned_arg)
unknown's avatar
unknown committed
1020 1021
    {}
  enum Item_result result_type () const { return INT_RESULT; }
1022
  enum_field_types type() const { return MYSQL_TYPE_LONGLONG;}
unknown's avatar
unknown committed
1023 1024
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
1025 1026
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
1027
  int store(longlong nr, bool unsigned_val);
1028 1029 1030 1031 1032
  int reset(void)
  {
    ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
    return 0;
  }
unknown's avatar
unknown committed
1033 1034 1035
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1036
  bool send_binary(Protocol *protocol);
1037 1038
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1039 1040
  uint32 pack_length() const { return 8; }
  void sql_type(String &str) const;
1041
  bool can_be_compared_as_longlong() const { return TRUE; }
1042
  uint32 max_display_length() { return 20; }
1043
  virtual uchar *pack(uchar* to, const uchar *from,
1044 1045
                      uint max_length  __attribute__((unused)),
                      bool low_byte_first)
1046
  {
1047
    return pack_int64(to, from, low_byte_first);
1048 1049
  }
  virtual const uchar *unpack(uchar* to, const uchar *from,
1050 1051
                              uint param_data __attribute__((unused)),
                              bool low_byte_first)
1052
  {
1053
    return unpack_int64(to, from, low_byte_first);
1054
  }
unknown's avatar
unknown committed
1055 1056 1057
};
#endif

unknown's avatar
unknown committed
1058 1059

class Field_float :public Field_real {
unknown's avatar
unknown committed
1060
public:
1061
  Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1062
	      uchar null_bit_arg,
unknown's avatar
unknown committed
1063
	      enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1064 1065
              uint8 dec_arg,bool zero_arg,bool unsigned_arg)
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1066
                unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
1067
                dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
1068
    {}
1069
  Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1070
	      uint8 dec_arg)
1071
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
unknown's avatar
unknown committed
1072
                NONE, field_name_arg, dec_arg, 0, 0)
1073
    {}
1074
  enum_field_types type() const { return MYSQL_TYPE_FLOAT;}
unknown's avatar
unknown committed
1075
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
1076 1077
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
1078
  int store(longlong nr, bool unsigned_val);
1079
  int reset(void) { bzero(ptr,sizeof(float)); return 0; }
unknown's avatar
unknown committed
1080 1081 1082
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1083
  bool send_binary(Protocol *protocol);
1084 1085
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1086
  uint32 pack_length() const { return sizeof(float); }
1087
  uint row_pack_length() { return pack_length(); }
unknown's avatar
unknown committed
1088
  void sql_type(String &str) const;
1089 1090
private:
  int do_save_field_metadata(uchar *first_byte);
unknown's avatar
unknown committed
1091 1092 1093
};


unknown's avatar
unknown committed
1094
class Field_double :public Field_real {
unknown's avatar
unknown committed
1095
public:
1096
  Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1097
	       uchar null_bit_arg,
unknown's avatar
unknown committed
1098
	       enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1099
	       uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
1100
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1101
                unireg_check_arg, field_name_arg,
1102
                dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
1103 1104
    {}
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1105
	       uint8 dec_arg)
1106
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
1107
                NONE, field_name_arg, dec_arg, 0, 0)
1108 1109
    {}
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
1110
	       uint8 dec_arg, my_bool not_fixed_arg)
1111
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
1112
                NONE, field_name_arg, dec_arg, 0, 0)
1113
    {not_fixed= not_fixed_arg; }
1114
  enum_field_types type() const { return MYSQL_TYPE_DOUBLE;}
unknown's avatar
unknown committed
1115
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
1116 1117
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1118
  int  store(longlong nr, bool unsigned_val);
1119
  int reset(void) { bzero(ptr,sizeof(double)); return 0; }
unknown's avatar
unknown committed
1120 1121 1122
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1123
  bool send_binary(Protocol *protocol);
1124 1125
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1126
  uint32 pack_length() const { return sizeof(double); }
1127
  uint row_pack_length() { return pack_length(); }
unknown's avatar
unknown committed
1128
  void sql_type(String &str) const;
1129 1130
private:
  int do_save_field_metadata(uchar *first_byte);
unknown's avatar
unknown committed
1131 1132 1133
};


unknown's avatar
unknown committed
1134
/* Everything saved in this will disappear. It will always return NULL */
unknown's avatar
unknown committed
1135 1136 1137 1138

class Field_null :public Field_str {
  static uchar null[1];
public:
1139
  Field_null(uchar *ptr_arg, uint32 len_arg,
unknown's avatar
unknown committed
1140
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1141
	     CHARSET_INFO *cs)
unknown's avatar
unknown committed
1142
    :Field_str(ptr_arg, len_arg, null, 1,
unknown's avatar
unknown committed
1143
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1144
    {}
1145
  enum_field_types type() const { return MYSQL_TYPE_NULL;}
1146 1147
  int  store(const char *to, uint length, CHARSET_INFO *cs)
  { null[0]=1; return 0; }
1148
  int store(double nr)   { null[0]=1; return 0; }
1149 1150
  int store(longlong nr, bool unsigned_val) { null[0]=1; return 0; }
  int store_decimal(const my_decimal *d)  { null[0]=1; return 0; }
1151
  int reset(void)	  { return 0; }
unknown's avatar
unknown committed
1152 1153
  double val_real(void)		{ return 0.0;}
  longlong val_int(void)	{ return 0;}
unknown's avatar
unknown committed
1154
  my_decimal *val_decimal(my_decimal *) { return 0; }
unknown's avatar
unknown committed
1155 1156
  String *val_str(String *value,String *value2)
  { value2->length(0); return value2;}
1157 1158
  int cmp(const uchar *a, const uchar *b) { return 0;}
  void sort_string(uchar *buff, uint length)  {}
unknown's avatar
unknown committed
1159
  uint32 pack_length() const { return 0; }
1160
  void sql_type(String &str) const;
unknown's avatar
unknown committed
1161
  uint size_of() const { return sizeof(*this); }
1162
  uint32 max_display_length() { return 4; }
unknown's avatar
unknown committed
1163 1164 1165
};


1166
class Field_timestamp :public Field_str {
unknown's avatar
unknown committed
1167
public:
1168
  Field_timestamp(uchar *ptr_arg, uint32 len_arg,
1169
                  uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1170
		  enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1171
		  TABLE_SHARE *share, CHARSET_INFO *cs);
1172
  Field_timestamp(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1173
		  CHARSET_INFO *cs);
1174
  enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;}
unknown's avatar
unknown committed
1175
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
1176
  enum Item_result cmp_type () const { return INT_RESULT; }
1177 1178
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1179
  int  store(longlong nr, bool unsigned_val);
1180
  int  reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
unknown's avatar
unknown committed
1181 1182 1183
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1184
  bool send_binary(Protocol *protocol);
1185 1186
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1187 1188
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
1189
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1190 1191
  bool zero_pack() const { return 0; }
  void set_time();
unknown's avatar
unknown committed
1192 1193
  virtual void set_default()
  {
1194 1195
    if (table->timestamp_field == this &&
        unireg_check != TIMESTAMP_UN_FIELD)
1196 1197 1198
      set_time();
    else
      Field::set_default();
unknown's avatar
unknown committed
1199
  }
1200 1201
  /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
  inline long get_timestamp(my_bool *null_value)
unknown's avatar
unknown committed
1202
  {
1203 1204
    if ((*null_value= is_null()))
      return 0;
unknown's avatar
unknown committed
1205
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
1206
    if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
1207 1208 1209 1210 1211 1212
      return sint4korr(ptr);
#endif
    long tmp;
    longget(tmp,ptr);
    return tmp;
  }
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
  inline void store_timestamp(my_time_t timestamp)
  {
#ifdef WORDS_BIGENDIAN
    if (table && table->s->db_low_byte_first)
    {
      int4store(ptr,timestamp);
    }
    else
#endif
      longstore(ptr,(uint32) timestamp);
  }
1224 1225
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  bool get_time(MYSQL_TIME *ltime);
1226
  timestamp_auto_set_type get_auto_set_type() const;
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  uchar *pack(uchar *to, const uchar *from,
              uint max_length __attribute__((unused)), bool low_byte_first)
  {
    return pack_int32(to, from, low_byte_first);
  }
  const uchar *unpack(uchar* to, const uchar *from,
                      uint param_data __attribute__((unused)),
                      bool low_byte_first)
  {
    return unpack_int32(to, from, low_byte_first);
  }
unknown's avatar
unknown committed
1238 1239 1240 1241 1242
};


class Field_year :public Field_tiny {
public:
1243
  Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1244
	     uchar null_bit_arg,
unknown's avatar
unknown committed
1245
	     enum utype unireg_check_arg, const char *field_name_arg)
unknown's avatar
unknown committed
1246
    :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1247
		unireg_check_arg, field_name_arg, 1, 1)
unknown's avatar
unknown committed
1248
    {}
1249
  enum_field_types type() const { return MYSQL_TYPE_YEAR;}
1250 1251
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1252
  int  store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1253 1254 1255
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1256
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
1257
  void sql_type(String &str) const;
1258
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1259 1260 1261 1262 1263
};


class Field_date :public Field_str {
public:
1264
  Field_date(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1265
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1266
	     CHARSET_INFO *cs)
unknown's avatar
unknown committed
1267
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1268
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1269
    {}
1270
  Field_date(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1271
             CHARSET_INFO *cs)
1272
    :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
1273
	       NONE, field_name_arg, cs) {}
1274
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
unknown's avatar
unknown committed
1275 1276
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
  enum Item_result cmp_type () const { return INT_RESULT; }
1277 1278
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
1279
  int store(longlong nr, bool unsigned_val);
1280
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
unknown's avatar
unknown committed
1281 1282 1283
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1284
  bool get_time(MYSQL_TIME *ltime);
1285
  bool send_binary(Protocol *protocol);
1286 1287
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1288 1289
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
1290
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1291
  bool zero_pack() const { return 1; }
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
  uchar *pack(uchar* to, const uchar *from,
              uint max_length __attribute__((unused)), bool low_byte_first)
  {
    return pack_int32(to, from, low_byte_first);
  }
  const uchar *unpack(uchar* to, const uchar *from,
                      uint param_data __attribute__((unused)),
                      bool low_byte_first)
  {
    return unpack_int32(to, from, low_byte_first);
  }
unknown's avatar
unknown committed
1303 1304
};

unknown's avatar
unknown committed
1305

unknown's avatar
unknown committed
1306 1307
class Field_newdate :public Field_str {
public:
1308
  Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1309
		enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1310
		CHARSET_INFO *cs)
unknown's avatar
unknown committed
1311
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1312
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1313
    {}
1314
  Field_newdate(bool maybe_null_arg, const char *field_name_arg,
1315 1316 1317
                CHARSET_INFO *cs)
    :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
               NONE, field_name_arg, cs) {}
1318 1319
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
  enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; }
unknown's avatar
unknown committed
1320 1321
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
  enum Item_result cmp_type () const { return INT_RESULT; }
1322 1323
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1324
  int  store(longlong nr, bool unsigned_val);
1325
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
1326
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
unknown's avatar
unknown committed
1327 1328 1329
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1330
  bool send_binary(Protocol *protocol);
1331 1332
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1333 1334
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
1335
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1336
  bool zero_pack() const { return 1; }
1337 1338
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  bool get_time(MYSQL_TIME *ltime);
unknown's avatar
unknown committed
1339 1340 1341 1342 1343
};


class Field_time :public Field_str {
public:
1344
  Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1345
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1346
	     CHARSET_INFO *cs)
unknown's avatar
unknown committed
1347
    :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1348
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1349
    {}
1350
  Field_time(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1351
             CHARSET_INFO *cs)
1352
    :Field_str((uchar*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
1353
	       NONE, field_name_arg, cs) {}
1354
  enum_field_types type() const { return MYSQL_TYPE_TIME;}
unknown's avatar
unknown committed
1355 1356
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
  enum Item_result cmp_type () const { return INT_RESULT; }
1357
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
1358 1359
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
1360
  int store(longlong nr, bool unsigned_val);
1361
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
unknown's avatar
unknown committed
1362 1363 1364
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1365
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1366
  bool send_binary(Protocol *protocol);
1367
  bool get_time(MYSQL_TIME *ltime);
1368 1369
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1370 1371
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
1372
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1373 1374 1375 1376 1377 1378
  bool zero_pack() const { return 1; }
};


class Field_datetime :public Field_str {
public:
1379
  Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1380
		 enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1381
		 CHARSET_INFO *cs)
unknown's avatar
unknown committed
1382
    :Field_str(ptr_arg, 19, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1383
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1384
    {}
1385
  Field_datetime(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1386
		 CHARSET_INFO *cs)
1387
    :Field_str((uchar*) 0,19, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
1388
	       NONE, field_name_arg, cs) {}
1389
  enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
unknown's avatar
unknown committed
1390 1391 1392 1393
#ifdef HAVE_LONG_LONG
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
#endif
  enum Item_result cmp_type () const { return INT_RESULT; }
unknown's avatar
unknown committed
1394
  uint decimals() const { return DATETIME_DEC; }
1395 1396
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1397
  int  store(longlong nr, bool unsigned_val);
1398
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
1399 1400 1401 1402 1403
  int reset(void)
  {
    ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
    return 0;
  }
unknown's avatar
unknown committed
1404 1405 1406
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1407
  bool send_binary(Protocol *protocol);
1408 1409
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1410 1411
  uint32 pack_length() const { return 8; }
  void sql_type(String &str) const;
1412
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1413
  bool zero_pack() const { return 1; }
1414 1415
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  bool get_time(MYSQL_TIME *ltime);
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  uchar *pack(uchar* to, const uchar *from,
              uint max_length __attribute__((unused)), bool low_byte_first)
  {
    return pack_int64(to, from, low_byte_first);
  }
  const uchar *unpack(uchar* to, const uchar *from,
                      uint param_data __attribute__((unused)),
                      bool low_byte_first)
  {
    return unpack_int64(to, from, low_byte_first);
  }
unknown's avatar
unknown committed
1427 1428 1429
};


unknown's avatar
unknown committed
1430
class Field_string :public Field_longstr {
unknown's avatar
unknown committed
1431
public:
1432
  bool can_alter_field_type;
1433
  Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
1434
	       uchar null_bit_arg,
unknown's avatar
unknown committed
1435
	       enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1436
	       CHARSET_INFO *cs)
unknown's avatar
unknown committed
1437
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1438
                   unireg_check_arg, field_name_arg, cs),
1439
     can_alter_field_type(1) {};
unknown's avatar
unknown committed
1440
  Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1441
               CHARSET_INFO *cs)
1442
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1443
                   NONE, field_name_arg, cs),
1444
     can_alter_field_type(1) {};
unknown's avatar
unknown committed
1445 1446 1447

  enum_field_types type() const
  {
1448
    return ((can_alter_field_type && orig_table &&
unknown's avatar
Merge  
unknown committed
1449 1450 1451 1452
             orig_table->s->db_create_options & HA_OPTION_PACK_RECORD &&
	     field_length >= 4) &&
            orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR ?
	    MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING);
unknown's avatar
unknown committed
1453 1454
  }
  enum ha_base_keytype key_type() const
1455
    { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
unknown's avatar
unknown committed
1456
  bool zero_pack() const { return 0; }
1457 1458
  int reset(void)
  {
1459
    charset()->cset->fill(charset(),(char*) ptr, field_length,
1460
                          (has_charset() ? ' ' : 0));
1461 1462 1463
    return 0;
  }
  int store(const char *to,uint length,CHARSET_INFO *charset);
1464
  int store(longlong nr, bool unsigned_val);
1465
  int store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
unknown's avatar
unknown committed
1466 1467 1468
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
unknown's avatar
unknown committed
1469
  my_decimal *val_decimal(my_decimal *);
1470 1471
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1472
  void sql_type(String &str) const;
1473 1474 1475 1476
  virtual uchar *pack(uchar *to, const uchar *from,
                      uint max_length, bool low_byte_first);
  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first);
1477 1478 1479
  uint pack_length_from_metadata(uint field_metadata)
  { return (field_metadata & 0x00ff); }
  uint row_pack_length() { return (field_length + 1); }
1480
  int pack_cmp(const uchar *a,const uchar *b,uint key_length,
unknown's avatar
Merge  
unknown committed
1481
               my_bool insert_or_update);
1482 1483
  int pack_cmp(const uchar *b,uint key_length,my_bool insert_or_update);
  uint packed_col_length(const uchar *to, uint length);
unknown's avatar
unknown committed
1484 1485
  uint max_packed_col_length(uint max_length);
  uint size_of() const { return sizeof(*this); }
1486
  enum_field_types real_type() const { return MYSQL_TYPE_STRING; }
1487 1488
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
1489
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
1490
  virtual uint get_key_image(uchar *buff,uint length, imagetype type);
1491 1492
private:
  int do_save_field_metadata(uchar *first_byte);
unknown's avatar
unknown committed
1493 1494 1495
};


unknown's avatar
unknown committed
1496
class Field_varstring :public Field_longstr {
unknown's avatar
unknown committed
1497
public:
unknown's avatar
unknown committed
1498 1499 1500 1501
  /*
    The maximum space available in a Field_varstring, in bytes. See
    length_bytes.
  */
unknown's avatar
unknown committed
1502
  static const uint MAX_SIZE;
unknown's avatar
Merge  
unknown committed
1503 1504
  /* Store number of bytes used to store length (1 or 2) */
  uint32 length_bytes;
1505
  Field_varstring(uchar *ptr_arg,
unknown's avatar
Merge  
unknown committed
1506
                  uint32 len_arg, uint length_bytes_arg,
unknown's avatar
unknown committed
1507
                  uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1508
		  enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1509
		  TABLE_SHARE *share, CHARSET_INFO *cs)
unknown's avatar
unknown committed
1510
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1511
                   unireg_check_arg, field_name_arg, cs),
unknown's avatar
unknown committed
1512
     length_bytes(length_bytes_arg)
unknown's avatar
Merge  
unknown committed
1513
  {
unknown's avatar
unknown committed
1514
    share->varchar_fields++;
unknown's avatar
Merge  
unknown committed
1515
  }
unknown's avatar
unknown committed
1516
  Field_varstring(uint32 len_arg,bool maybe_null_arg,
unknown's avatar
unknown committed
1517
                  const char *field_name_arg,
unknown's avatar
unknown committed
1518
                  TABLE_SHARE *share, CHARSET_INFO *cs)
1519
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1520
                   NONE, field_name_arg, cs),
unknown's avatar
unknown committed
1521
     length_bytes(len_arg < 256 ? 1 :2)
unknown's avatar
Merge  
unknown committed
1522
  {
unknown's avatar
unknown committed
1523
    share->varchar_fields++;
unknown's avatar
Merge  
unknown committed
1524
  }
unknown's avatar
unknown committed
1525

unknown's avatar
Merge  
unknown committed
1526 1527
  enum_field_types type() const { return MYSQL_TYPE_VARCHAR; }
  enum ha_base_keytype key_type() const;
1528
  uint row_pack_length() { return field_length; }
unknown's avatar
unknown committed
1529
  bool zero_pack() const { return 0; }
1530
  int  reset(void) { bzero(ptr,field_length+length_bytes); return 0; }
unknown's avatar
Merge  
unknown committed
1531
  uint32 pack_length() const { return (uint32) field_length+length_bytes; }
unknown's avatar
unknown committed
1532
  uint32 key_length() const { return (uint32) field_length; }
1533 1534 1535 1536 1537
  uint32 sort_length() const
  {
    return (uint32) field_length + (field_charset == &my_charset_bin ?
                                    length_bytes : 0);
  }
1538
  int  store(const char *to,uint length,CHARSET_INFO *charset);
1539
  int  store(longlong nr, bool unsigned_val);
1540
  int  store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
unknown's avatar
unknown committed
1541 1542 1543
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
unknown's avatar
unknown committed
1544
  my_decimal *val_decimal(my_decimal *);
1545 1546
  int cmp_max(const uchar *, const uchar *, uint max_length);
  int cmp(const uchar *a,const uchar *b)
1547
  {
unknown's avatar
unknown committed
1548
    return cmp_max(a, b, ~0L);
1549
  }
1550 1551 1552
  void sort_string(uchar *buff,uint length);
  uint get_key_image(uchar *buff,uint length, imagetype type);
  void set_key_image(const uchar *buff,uint length);
unknown's avatar
unknown committed
1553
  void sql_type(String &str) const;
1554 1555 1556
  virtual uchar *pack(uchar *to, const uchar *from,
                      uint max_length, bool low_byte_first);
  uchar *pack_key(uchar *to, const uchar *from, uint max_length, bool low_byte_first);
1557
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
1558 1559 1560 1561 1562
                                 uint max_length, bool low_byte_first);
  virtual const uchar *unpack(uchar* to, const uchar *from,
                              uint param_data, bool low_byte_first);
  const uchar *unpack_key(uchar* to, const uchar *from,
                          uint max_length, bool low_byte_first);
1563
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
unknown's avatar
Merge  
unknown committed
1564
               my_bool insert_or_update);
1565 1566 1567 1568 1569
  int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
  int key_cmp(const uchar *,const uchar*);
  int key_cmp(const uchar *str, uint length);
  uint packed_col_length(const uchar *to, uint length);
unknown's avatar
unknown committed
1570
  uint max_packed_col_length(uint max_length);
1571
  uint32 data_length();
unknown's avatar
unknown committed
1572
  uint size_of() const { return sizeof(*this); }
unknown's avatar
Merge  
unknown committed
1573
  enum_field_types real_type() const { return MYSQL_TYPE_VARCHAR; }
1574 1575
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
1576
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
unknown's avatar
Merge  
unknown committed
1577
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
1578
                       uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
Merge  
unknown committed
1579
                       uint new_null_bit);
unknown's avatar
unknown committed
1580
  uint is_equal(Create_field *new_field);
1581
  void hash(ulong *nr, ulong *nr2);
1582 1583
private:
  int do_save_field_metadata(uchar *first_byte);
unknown's avatar
unknown committed
1584 1585 1586
};


unknown's avatar
unknown committed
1587
class Field_blob :public Field_longstr {
unknown's avatar
unknown committed
1588
protected:
unknown's avatar
unknown committed
1589
  uint packlength;
1590
  String value;				// For temporaries
unknown's avatar
unknown committed
1591
public:
1592
  Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1593
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1594
	     TABLE_SHARE *share, uint blob_pack_length, CHARSET_INFO *cs);
unknown's avatar
unknown committed
1595
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1596
             CHARSET_INFO *cs)
1597
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1598
                   NONE, field_name_arg, cs),
unknown's avatar
unknown committed
1599
    packlength(4)
unknown's avatar
unknown committed
1600 1601 1602
  {
    flags|= BLOB_FLAG;
  }
1603
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1604
	     CHARSET_INFO *cs, bool set_packlength)
1605
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1606
                   NONE, field_name_arg, cs)
1607 1608 1609 1610 1611
  {
    flags|= BLOB_FLAG;
    packlength= 4;
    if (set_packlength)
    {
1612 1613 1614 1615
      uint32 l_char_length= len_arg/cs->mbmaxlen;
      packlength= l_char_length <= 255 ? 1 :
                  l_char_length <= 65535 ? 2 :
                  l_char_length <= 16777215 ? 3 : 4;
1616 1617
    }
  }
1618 1619 1620
  Field_blob(uint32 packlength_arg)
    :Field_longstr((uchar*) 0, 0, (uchar*) "", 0, NONE, "temp", system_charset_info),
    packlength(packlength_arg) {}
1621
  enum_field_types type() const { return MYSQL_TYPE_BLOB;}
unknown's avatar
unknown committed
1622
  enum ha_base_keytype key_type() const
unknown's avatar
Merge  
unknown committed
1623
    { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
1624 1625
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1626
  int  store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1627 1628 1629
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
unknown's avatar
unknown committed
1630
  my_decimal *val_decimal(my_decimal *);
1631 1632
  int cmp_max(const uchar *, const uchar *, uint max_length);
  int cmp(const uchar *a,const uchar *b)
unknown's avatar
unknown committed
1633
    { return cmp_max(a, b, ~0L); }
1634 1635 1636 1637
  int cmp(const uchar *a, uint32 a_length, const uchar *b, uint32 b_length);
  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
  int key_cmp(const uchar *,const uchar*);
  int key_cmp(const uchar *str, uint length);
unknown's avatar
unknown committed
1638
  uint32 key_length() const { return 0; }
1639
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1640
  uint32 pack_length() const
unknown's avatar
Merge  
unknown committed
1641
  { return (uint32) (packlength+table->s->blob_ptr_size); }
1642 1643 1644 1645 1646 1647 1648

  /**
     Return the packed length without the pointer size added. 

     This is used to determine the size of the actual data in the row
     buffer.

1649
     @returns The length of the raw data itself without the pointer.
1650 1651 1652
  */
  uint32 pack_length_no_ptr() const
  { return (uint32) (packlength); }
1653
  uint row_pack_length() { return pack_length_no_ptr(); }
1654
  uint32 sort_length() const;
1655
  virtual uint32 max_data_length() const
1656
  {
1657
    return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
1658
  }
1659 1660
  int reset(void) { bzero(ptr, packlength+sizeof(uchar*)); return 0; }
  void reset_fields() { bzero((uchar*) &value,sizeof(value)); }
unknown's avatar
unknown committed
1661 1662 1663
#ifndef WORDS_BIGENDIAN
  static
#endif
1664 1665 1666 1667 1668
  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number, bool low_byte_first);
  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number)
  {
    store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
  }
1669 1670 1671 1672
  inline void store_length(uint32 number)
  {
    store_length(ptr, packlength, number);
  }
1673 1674 1675 1676 1677 1678 1679

  /**
     Return the packed length plus the length of the data. 

     This is used to determine the size of the data plus the 
     packed length portion in the row data.

1680
     @returns The length in the row plus the size of the data.
1681
  */
1682
  uint32 get_packed_size(const uchar *ptr_arg, bool low_byte_first)
1683
    {return packlength + get_length(ptr_arg, packlength, low_byte_first);}
1684 1685

  inline uint32 get_length(uint row_offset= 0)
1686 1687
  { return get_length(ptr+row_offset, this->packlength, table->s->db_low_byte_first); }
  uint32 get_length(const uchar *ptr, uint packlength, bool low_byte_first);
1688
  uint32 get_length(const uchar *ptr_arg)
1689
  { return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first); }
1690 1691
  void put_length(uchar *pos, uint32 length);
  inline void get_ptr(uchar **str)
unknown's avatar
unknown committed
1692
    {
1693
      memcpy_fixed((uchar*) str,ptr+packlength,sizeof(uchar*));
unknown's avatar
unknown committed
1694
    }
1695
  inline void get_ptr(uchar **str, uint row_offset)
1696
    {
1697
      memcpy_fixed((uchar*) str,ptr+packlength+row_offset,sizeof(char*));
1698
    }
1699
  inline void set_ptr(uchar *length, uchar *data)
unknown's avatar
unknown committed
1700 1701 1702 1703
    {
      memcpy(ptr,length,packlength);
      memcpy_fixed(ptr+packlength,&data,sizeof(char*));
    }
unknown's avatar
unknown committed
1704
  void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, uchar *data)
1705
    {
unknown's avatar
unknown committed
1706
      uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
1707 1708 1709
      store_length(ptr_ofs, packlength, length);
      memcpy_fixed(ptr_ofs+packlength,&data,sizeof(char*));
    }
1710
  inline void set_ptr(uint32 length, uchar *data)
unknown's avatar
unknown committed
1711
    {
1712
      set_ptr_offset(0, length, data);
unknown's avatar
unknown committed
1713
    }
1714 1715
  uint get_key_image(uchar *buff,uint length, imagetype type);
  void set_key_image(const uchar *buff,uint length);
unknown's avatar
unknown committed
1716 1717
  void sql_type(String &str) const;
  inline bool copy()
1718 1719
  {
    uchar *tmp;
unknown's avatar
unknown committed
1720
    get_ptr(&tmp);
1721
    if (value.copy((char*) tmp, get_length(), charset()))
unknown's avatar
unknown committed
1722 1723 1724 1725
    {
      Field_blob::reset();
      return 1;
    }
1726 1727
    tmp=(uchar*) value.ptr();
    memcpy_fixed(ptr+packlength,&tmp,sizeof(char*));
unknown's avatar
unknown committed
1728 1729
    return 0;
  }
1730 1731 1732 1733
  virtual uchar *pack(uchar *to, const uchar *from,
                      uint max_length, bool low_byte_first);
  uchar *pack_key(uchar *to, const uchar *from,
                  uint max_length, bool low_byte_first);
1734
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
1735 1736 1737 1738 1739
                                 uint max_length, bool low_byte_first);
  virtual const uchar *unpack(uchar *to, const uchar *from,
                              uint param_data, bool low_byte_first);
  const uchar *unpack_key(uchar* to, const uchar *from,
                          uint max_length, bool low_byte_first);
1740
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
unknown's avatar
Merge  
unknown committed
1741
               my_bool insert_or_update);
1742 1743
  int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
  uint packed_col_length(const uchar *col_ptr, uint length);
1744
  uint max_packed_col_length(uint max_length);
1745
  void free() { value.free(); }
1746
  inline void clear_temporary() { bzero((uchar*) &value,sizeof(value)); }
1747
  friend int field_conv(Field *to,Field *from);
unknown's avatar
unknown committed
1748
  uint size_of() const { return sizeof(*this); }
1749
  bool has_charset(void) const
1750
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
1751
  uint32 max_display_length();
unknown's avatar
unknown committed
1752
  uint is_equal(Create_field *new_field);
1753 1754
  inline bool in_read_set() { return bitmap_is_set(table->read_set, field_index); }
  inline bool in_write_set() { return bitmap_is_set(table->write_set, field_index); }
1755 1756
private:
  int do_save_field_metadata(uchar *first_byte);
1757 1758
};

unknown's avatar
Merge  
unknown committed
1759

unknown's avatar
unknown committed
1760
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
1761 1762
class Field_geom :public Field_blob {
public:
unknown's avatar
unknown committed
1763
  enum geometry_type geom_type;
1764

1765
  Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
unknown's avatar
unknown committed
1766
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1767
	     TABLE_SHARE *share, uint blob_pack_length,
unknown's avatar
unknown committed
1768
	     enum geometry_type geom_type_arg)
unknown's avatar
unknown committed
1769
     :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, 
unknown's avatar
unknown committed
1770
                 field_name_arg, share, blob_pack_length, &my_charset_bin)
unknown's avatar
unknown committed
1771
  { geom_type= geom_type_arg; }
unknown's avatar
unknown committed
1772
  Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1773 1774
	     TABLE_SHARE *share, enum geometry_type geom_type_arg)
    :Field_blob(len_arg, maybe_null_arg, field_name_arg, &my_charset_bin)
unknown's avatar
unknown committed
1775
  { geom_type= geom_type_arg; }
unknown's avatar
Merge  
unknown committed
1776
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY2; }
1777
  enum_field_types type() const { return MYSQL_TYPE_GEOMETRY; }
unknown's avatar
unknown committed
1778
  void sql_type(String &str) const;
unknown's avatar
unknown committed
1779
  int  store(const char *to, uint length, CHARSET_INFO *charset);
1780
  int  store(double nr);
1781
  int  store(longlong nr, bool unsigned_val);
1782
  int  store_decimal(const my_decimal *);
1783
  uint size_of() const { return sizeof(*this); }
1784
  int  reset(void) { return !maybe_null() || Field_blob::reset(); }
1785
  geometry_type get_geometry_type() { return geom_type; };
unknown's avatar
unknown committed
1786
};
unknown's avatar
unknown committed
1787
#endif /*HAVE_SPATIAL*/
unknown's avatar
unknown committed
1788

unknown's avatar
Merge  
unknown committed
1789

unknown's avatar
unknown committed
1790 1791 1792 1793 1794
class Field_enum :public Field_str {
protected:
  uint packlength;
public:
  TYPELIB *typelib;
1795
  Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1796 1797 1798 1799 1800
             uchar null_bit_arg,
             enum utype unireg_check_arg, const char *field_name_arg,
             uint packlength_arg,
             TYPELIB *typelib_arg,
             CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
1801
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1802
	       unireg_check_arg, field_name_arg, charset_arg),
unknown's avatar
unknown committed
1803
    packlength(packlength_arg),typelib(typelib_arg)
unknown's avatar
unknown committed
1804
  {
unknown's avatar
unknown committed
1805
      flags|=ENUM_FLAG;
unknown's avatar
unknown committed
1806
  }
1807
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
1808
  enum_field_types type() const { return MYSQL_TYPE_STRING; }
unknown's avatar
unknown committed
1809
  enum Item_result cmp_type () const { return INT_RESULT; }
1810
  enum Item_result cast_to_int_type () const { return INT_RESULT; }
unknown's avatar
unknown committed
1811
  enum ha_base_keytype key_type() const;
1812 1813
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1814
  int  store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1815 1816 1817
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1818 1819
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1820 1821 1822 1823
  uint32 pack_length() const { return (uint32) packlength; }
  void store_type(ulonglong value);
  void sql_type(String &str) const;
  uint size_of() const { return sizeof(*this); }
1824
  enum_field_types real_type() const { return MYSQL_TYPE_ENUM; }
1825 1826 1827
  uint pack_length_from_metadata(uint field_metadata)
  { return (field_metadata & 0x00ff); }
  uint row_pack_length() { return pack_length(); }
unknown's avatar
unknown committed
1828
  virtual bool zero_pack() const { return 0; }
1829
  bool optimize_range(uint idx, uint part) { return 0; }
unknown's avatar
unknown committed
1830
  bool eq_def(Field *field);
1831
  bool has_charset(void) const { return TRUE; }
1832 1833
  /* enum and set are sorted as integers */
  CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
1834 1835
private:
  int do_save_field_metadata(uchar *first_byte);
unknown's avatar
unknown committed
1836 1837 1838 1839 1840
};


class Field_set :public Field_enum {
public:
1841
  Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1842
	    uchar null_bit_arg,
unknown's avatar
unknown committed
1843
	    enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1844
	    uint32 packlength_arg,
1845
	    TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
1846 1847
    :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
		    unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
1848 1849
                packlength_arg,
                typelib_arg,charset_arg)
unknown's avatar
unknown committed
1850 1851 1852
    {
      flags=(flags & ~ENUM_FLAG) | SET_FLAG;
    }
1853
  int  store(const char *to,uint length,CHARSET_INFO *charset);
1854 1855
  int  store(double nr) { return Field_set::store((longlong) nr, FALSE); }
  int  store(longlong nr, bool unsigned_val);
1856

unknown's avatar
unknown committed
1857 1858 1859
  virtual bool zero_pack() const { return 1; }
  String *val_str(String*,String *);
  void sql_type(String &str) const;
1860
  enum_field_types real_type() const { return MYSQL_TYPE_SET; }
1861
  bool has_charset(void) const { return TRUE; }
unknown's avatar
unknown committed
1862 1863 1864
};


1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
/*
  Note:
    To use Field_bit::cmp_binary() you need to copy the bits stored in
    the beginning of the record (the NULL bytes) to each memory you
    want to compare (where the arguments point).

    This is the reason:
    - Field_bit::cmp_binary() is only implemented in the base class
      (Field::cmp_binary()).
    - Field::cmp_binary() currenly use pack_length() to calculate how
      long the data is.
    - pack_length() includes size of the bits stored in the NULL bytes
      of the record.
*/
unknown's avatar
Merge  
unknown committed
1879 1880 1881 1882 1883
class Field_bit :public Field {
public:
  uchar *bit_ptr;     // position in record where 'uneven' bits store
  uchar bit_ofs;      // offset to 'uneven' high bits
  uint bit_len;       // number of 'uneven' high bits
unknown's avatar
unknown committed
1884
  uint bytes_in_rec;
1885
  Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
Merge  
unknown committed
1886
            uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
unknown's avatar
unknown committed
1887
            enum utype unireg_check_arg, const char *field_name_arg);
1888
  enum_field_types type() const { return MYSQL_TYPE_BIT; }
unknown's avatar
Merge  
unknown committed
1889
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
unknown's avatar
unknown committed
1890
  uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
1891
  uint32 max_data_length() const { return (field_length + 7) / 8; }
1892
  uint32 max_display_length() { return field_length; }
unknown's avatar
Merge  
unknown committed
1893 1894
  uint size_of() const { return sizeof(*this); }
  Item_result result_type () const { return INT_RESULT; }
1895
  int reset(void) { bzero(ptr, bytes_in_rec); return 0; }
unknown's avatar
Merge  
unknown committed
1896 1897
  int store(const char *to, uint length, CHARSET_INFO *charset);
  int store(double nr);
1898
  int store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1899
  int store_decimal(const my_decimal *);
unknown's avatar
Merge  
unknown committed
1900 1901 1902
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*, String *);
1903
  virtual bool str_needs_quotes() { return TRUE; }
unknown's avatar
unknown committed
1904
  my_decimal *val_decimal(my_decimal *);
1905
  int cmp(const uchar *a, const uchar *b)
unknown's avatar
unknown committed
1906 1907
  { 
    DBUG_ASSERT(ptr == a);
unknown's avatar
unknown committed
1908
    return Field_bit::key_cmp(b, bytes_in_rec+test(bit_len));
unknown's avatar
unknown committed
1909
  }
1910 1911
  int cmp_binary_offset(uint row_offset)
  { return cmp_offset(row_offset); }
1912 1913 1914 1915
  int cmp_max(const uchar *a, const uchar *b, uint max_length);
  int key_cmp(const uchar *a, const uchar *b)
  { return cmp_binary((uchar *) a, (uchar *) b); }
  int key_cmp(const uchar *str, uint length);
unknown's avatar
Merge  
unknown committed
1916
  int cmp_offset(uint row_offset);
1917
  void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
1918
  { get_key_image(buff, length, itRAW); }   
1919
  void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
1920
  { Field_bit::store((char *) buff, length, cs); }
1921 1922 1923 1924
  uint get_key_image(uchar *buff, uint length, imagetype type);
  void set_key_image(const uchar *buff, uint length)
  { Field_bit::store((char*) buff, length, &my_charset_bin); }
  void sort_string(uchar *buff, uint length)
unknown's avatar
Merge  
unknown committed
1925
  { get_key_image(buff, length, itRAW); }
unknown's avatar
unknown committed
1926 1927
  uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
  uint32 pack_length_in_rec() const { return bytes_in_rec; }
1928 1929 1930 1931
  uint pack_length_from_metadata(uint field_metadata);
  uint row_pack_length()
  { return (bytes_in_rec + ((bit_len > 0) ? 1 : 0)); }
  int compatible_field_size(uint field_metadata);
unknown's avatar
Merge  
unknown committed
1932
  void sql_type(String &str) const;
1933 1934 1935 1936
  virtual uchar *pack(uchar *to, const uchar *from,
                      uint max_length, bool low_byte_first);
  virtual const uchar *unpack(uchar *to, const uchar *from,
                              uint param_data, bool low_byte_first);
1937 1938
  virtual void set_default();

unknown's avatar
Merge  
unknown committed
1939
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
1940
                       uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
Merge  
unknown committed
1941
                       uint new_null_bit);
unknown's avatar
unknown committed
1942 1943 1944 1945 1946
  void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
  {
    bit_ptr= bit_ptr_arg;
    bit_ofs= bit_ofs_arg;
  }
1947 1948 1949 1950 1951 1952 1953
  bool eq(Field *field)
  {
    return (Field::eq(field) &&
            field->type() == type() &&
            bit_ptr == ((Field_bit *)field)->bit_ptr &&
            bit_ofs == ((Field_bit *)field)->bit_ofs);
  }
unknown's avatar
unknown committed
1954
  uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
1955 1956 1957 1958 1959
  void move_field_offset(my_ptrdiff_t ptr_diff)
  {
    Field::move_field_offset(ptr_diff);
    bit_ptr= ADD_TO_PTR(bit_ptr, ptr_diff, uchar*);
  }
1960
  void hash(ulong *nr, ulong *nr2);
1961 1962

private:
1963
  virtual size_t do_last_null_byte() const;
1964
  int do_save_field_metadata(uchar *first_byte);
unknown's avatar
Merge  
unknown committed
1965 1966
};

1967

1968 1969 1970 1971 1972 1973 1974
/**
  BIT field represented as chars for non-MyISAM tables.

  @todo The inheritance relationship is backwards since Field_bit is
  an extended version of Field_bit_as_char and not the other way
  around. Hence, we should refactor it to fix the hierarchy order.
 */
1975 1976
class Field_bit_as_char: public Field_bit {
public:
1977
  Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1978
                    uchar null_bit_arg,
unknown's avatar
unknown committed
1979
                    enum utype unireg_check_arg, const char *field_name_arg);
1980
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
unknown's avatar
unknown committed
1981
  uint size_of() const { return sizeof(*this); }
1982 1983
  int store(const char *to, uint length, CHARSET_INFO *charset);
  int store(double nr) { return Field_bit::store(nr); }
1984 1985
  int store(longlong nr, bool unsigned_val)
  { return Field_bit::store(nr, unsigned_val); }
1986 1987 1988 1989
  void sql_type(String &str) const;
};


unknown's avatar
unknown committed
1990
/*
unknown's avatar
unknown committed
1991
  Create field class for CREATE TABLE
unknown's avatar
unknown committed
1992 1993
*/

unknown's avatar
unknown committed
1994
class Create_field :public Sql_alloc
1995
{
unknown's avatar
unknown committed
1996 1997
public:
  const char *field_name;
1998 1999
  const char *change;			// If done with alter table
  const char *after;			// Put column after this one
unknown's avatar
unknown committed
2000
  LEX_STRING comment;			// Comment for field
2001
  Item	*def;				// Default value
unknown's avatar
unknown committed
2002
  enum	enum_field_types sql_type;
2003 2004 2005 2006
  /*
    At various stages in execution this can be length of field in bytes or
    max number of characters. 
  */
unknown's avatar
Merge  
unknown committed
2007
  ulong length;
unknown's avatar
unknown committed
2008
  /*
2009 2010
    The value of `length' as set by parser: is the number of characters
    for most of the types, or of bytes for BLOBs or numeric types.
unknown's avatar
unknown committed
2011
  */
2012
  uint32 char_length;
unknown's avatar
Merge  
unknown committed
2013
  uint  decimals, flags, pack_length, key_length;
unknown's avatar
unknown committed
2014
  Field::utype unireg_check;
2015
  TYPELIB *interval;			// Which interval to use
unknown's avatar
unknown committed
2016 2017
  TYPELIB *save_interval;               // Temporary copy for the above
                                        // Used only for UCS2 intervals
2018
  List<String> interval_list;
2019
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
2020
  Field::geometry_type geom_type;
2021
  Field *field;				// For alter table
unknown's avatar
unknown committed
2022

2023
  uint8 row,col,sc_length,interval_id;	// For rea_create_table
unknown's avatar
unknown committed
2024
  uint	offset,pack_flag;
unknown's avatar
unknown committed
2025 2026
  Create_field() :after(0) {}
  Create_field(Field *field, Field *orig_field);
2027
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
unknown's avatar
unknown committed
2028 2029
  Create_field *clone(MEM_ROOT *mem_root) const
    { return new (mem_root) Create_field(*this); }
2030
  void create_length_to_internal_length(void);
2031 2032 2033 2034 2035

  /* Init for a tmp table field. To be extended if need be. */
  void init_for_tmp_table(enum_field_types sql_type_arg,
                          uint32 max_length, uint32 decimals,
                          bool maybe_null, bool is_unsigned);
2036 2037 2038 2039 2040 2041

  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
            char *decimals, uint type_modifier, Item *default_value,
            Item *on_update_value, LEX_STRING *comment, char *change,
            List<String> *interval_list, CHARSET_INFO *cs,
            uint uint_geom_type);
unknown's avatar
unknown committed
2042 2043 2044 2045
};


/*
unknown's avatar
unknown committed
2046
  A class for sending info to the client
unknown's avatar
unknown committed
2047 2048 2049 2050
*/

class Send_field {
 public:
unknown's avatar
unknown committed
2051 2052 2053
  const char *db_name;
  const char *table_name,*org_table_name;
  const char *col_name,*org_col_name;
2054 2055
  ulong length;
  uint charsetnr, flags, decimals;
unknown's avatar
unknown committed
2056 2057 2058 2059 2060 2061
  enum_field_types type;
  Send_field() {}
};


/*
2062
  A class for quick copying data to fields
unknown's avatar
unknown committed
2063 2064 2065
*/

class Copy_field :public Sql_alloc {
unknown's avatar
unknown committed
2066 2067 2068 2069 2070 2071
  /**
    Convenience definition of a copy function returned by
    get_copy_func.
  */
  typedef void Copy_func(Copy_field*);
  Copy_func *get_copy_func(Field *to, Field *from);
unknown's avatar
unknown committed
2072
public:
2073
  uchar *from_ptr,*to_ptr;
unknown's avatar
unknown committed
2074 2075 2076 2077 2078
  uchar *from_null_ptr,*to_null_ptr;
  my_bool *null_row;
  uint	from_bit,to_bit;
  uint from_length,to_length;
  Field *from_field,*to_field;
2079
  String tmp;					// For items
unknown's avatar
unknown committed
2080 2081 2082

  Copy_field() {}
  ~Copy_field() {}
2083
  void set(Field *to,Field *from,bool save);	// Field to field 
2084
  void set(uchar *to,Field *from);		// Field to string
unknown's avatar
unknown committed
2085
  void (*do_copy)(Copy_field *);
2086
  void (*do_copy2)(Copy_field *);		// Used to handle null values
unknown's avatar
unknown committed
2087 2088 2089
};


2090
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
unknown's avatar
unknown committed
2091
		  uchar *null_pos, uchar null_bit,
2092
		  uint pack_flag, enum_field_types field_type,
2093
		  CHARSET_INFO *cs,
unknown's avatar
unknown committed
2094
		  Field::geometry_type geom_type,
2095
		  Field::utype unireg_check,
unknown's avatar
unknown committed
2096
		  TYPELIB *interval, const char *field_name);
unknown's avatar
unknown committed
2097
uint pack_length_to_packflag(uint type);
unknown's avatar
Merge  
unknown committed
2098
enum_field_types get_blob_type_from_length(ulong length);
unknown's avatar
unknown committed
2099
uint32 calc_pack_length(enum_field_types type,uint32 length);
unknown's avatar
unknown committed
2100 2101
int set_field_to_null(Field *field);
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
unknown's avatar
unknown committed
2102 2103

/*
2104
  The following are for the interface with the .frm file
unknown's avatar
unknown committed
2105 2106 2107
*/

#define FIELDFLAG_DECIMAL		1
2108
#define FIELDFLAG_BINARY		1	// Shares same flag
unknown's avatar
unknown committed
2109 2110
#define FIELDFLAG_NUMBER		2
#define FIELDFLAG_ZEROFILL		4
2111
#define FIELDFLAG_PACK			120	// Bits used for packing
2112 2113 2114 2115
#define FIELDFLAG_INTERVAL		256     // mangled with decimals!
#define FIELDFLAG_BITFIELD		512	// mangled with decimals!
#define FIELDFLAG_BLOB			1024	// mangled with decimals!
#define FIELDFLAG_GEOM			2048    // mangled with decimals!
unknown's avatar
unknown committed
2116

2117 2118
#define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */

unknown's avatar
unknown committed
2119 2120
#define FIELDFLAG_LEFT_FULLSCREEN	8192
#define FIELDFLAG_RIGHT_FULLSCREEN	16384
2121
#define FIELDFLAG_FORMAT_NUMBER		16384	// predit: ###,,## in output
unknown's avatar
Merge  
unknown committed
2122
#define FIELDFLAG_NO_DEFAULT		16384   /* sql */
2123 2124
#define FIELDFLAG_SUM			((uint) 32768)// predit: +#fieldflag
#define FIELDFLAG_MAYBE_NULL		((uint) 32768)// sql
2125
#define FIELDFLAG_HEX_ESCAPE		((uint) 0x10000)
unknown's avatar
unknown committed
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
#define FIELDFLAG_PACK_SHIFT		3
#define FIELDFLAG_DEC_SHIFT		8
#define FIELDFLAG_MAX_DEC		31
#define FIELDFLAG_NUM_SCREEN_TYPE	0x7F01
#define FIELDFLAG_ALFA_SCREEN_TYPE	0x7800

#define MTYP_TYPENR(type) (type & 127)	/* Remove bits from type */

#define f_is_dec(x)		((x) & FIELDFLAG_DECIMAL)
#define f_is_num(x)		((x) & FIELDFLAG_NUMBER)
#define f_is_zerofill(x)	((x) & FIELDFLAG_ZEROFILL)
#define f_is_packed(x)		((x) & FIELDFLAG_PACK)
#define f_packtype(x)		(((x) >> FIELDFLAG_PACK_SHIFT) & 15)
unknown's avatar
unknown committed
2139
#define f_decimals(x)		((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
unknown's avatar
unknown committed
2140
#define f_is_alpha(x)		(!f_is_num(x))
2141
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
2142 2143
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
#define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
unknown's avatar
unknown committed
2144
#define f_is_blob(x)		(((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
2145
#define f_is_geom(x)		(((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
unknown's avatar
unknown committed
2146 2147 2148
#define f_is_equ(x)		((x) & (1+2+FIELDFLAG_PACK+31*256))
#define f_settype(x)		(((int) x) << FIELDFLAG_PACK_SHIFT)
#define f_maybe_null(x)		(x & FIELDFLAG_MAYBE_NULL)
unknown's avatar
Merge  
unknown committed
2149
#define f_no_default(x)		(x & FIELDFLAG_NO_DEFAULT)
2150
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
2151
#define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)