field.h 66.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
unknown's avatar
unknown committed
28 29

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

35 36 37 38 39 40 41 42 43 44 45
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;
}

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

54
  uchar		*ptr;			// Position to field in record
55
  uchar		*null_ptr;		// Byte where null_bit is
56 57 58 59
  /*
    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)
  */
60
  struct st_table *table;		// Pointer for table
61
  struct st_table *orig_table;		// Pointer to original table
unknown's avatar
Merge  
unknown committed
62
  const char	**table_name, *field_name;
63
  LEX_STRING	comment;
unknown's avatar
unknown committed
64
  /* Field is part of the following keys */
65 66
  key_map	key_start, part_of_key, part_of_key_not_clustered;
  key_map       part_of_sortkey;
67 68 69 70 71 72 73 74
  /* 
    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
75 76
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
		CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
77 78
                BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
                TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
unknown's avatar
unknown committed
79 80 81 82 83 84
  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
85
  enum imagetype { itRAW, itMBR};
unknown's avatar
unknown committed
86

unknown's avatar
unknown committed
87
  utype		unireg_check;
88
  uint32	field_length;		// Length of field
89
  uint32	flags;
90
  uint16        field_index;            // field number in fields array
91
  uchar		null_bit;		// Bit used to test null bit
unknown's avatar
unknown committed
92

93 94 95
  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
96
  virtual ~Field() {}
unknown's avatar
unknown committed
97
  /* Store functions returns 1 on overflow and -1 on fatal error */
98
  virtual int  store(const char *to, uint length,CHARSET_INFO *cs)=0;
99
  virtual int  store(double nr)=0;
100
  virtual int  store(longlong nr, bool unsigned_val)=0;
unknown's avatar
unknown committed
101
  virtual int  store_decimal(const my_decimal *d)=0;
102
  virtual int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
unknown's avatar
unknown committed
103 104
  virtual double val_real(void)=0;
  virtual longlong val_int(void)=0;
unknown's avatar
unknown committed
105
  virtual my_decimal *val_decimal(my_decimal *);
unknown's avatar
unknown committed
106 107 108
  inline String *val_str(String *str) { return val_str(str, str); }
  /*
     val_str(buf1, buf2) gets two buffers and should use them as follows:
109 110 111 112 113 114 115 116 117 118
     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
119
  virtual String *val_str(String*,String *)=0;
unknown's avatar
Merge  
unknown committed
120
  String *val_int_as_str(String *val_buffer, my_bool unsigned_flag);
121 122 123 124 125
  /*
   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
126 127
  virtual Item_result result_type () const=0;
  virtual Item_result cmp_type () const { return result_type(); }
128
  virtual Item_result cast_to_int_type () const { return result_type(); }
129
  static bool type_can_have_key_part(enum_field_types);
130 131
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
  static Item_result result_merge_type(enum_field_types);
132
  virtual bool eq(Field *field)
unknown's avatar
Merge  
unknown committed
133 134 135 136
  {
    return (ptr == field->ptr && null_ptr == field->null_ptr &&
            null_bit == field->null_bit);
  }
unknown's avatar
unknown committed
137
  virtual bool eq_def(Field *field);
138 139 140 141 142 143
  
  /*
    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
144
  virtual uint32 pack_length() const { return (uint32) field_length; }
145 146 147 148 149 150

  /*
    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
151
  virtual uint32 pack_length_in_rec() const { return pack_length(); }
152 153 154 155

  /*
    data_length() return the "real size" of the data in memory.
  */
156
  virtual uint32 data_length() { return pack_length(); }
157
  virtual uint32 sort_length() const { return pack_length(); }
158
  virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
unknown's avatar
unknown committed
159
  virtual void reset_fields() {}
unknown's avatar
unknown committed
160 161
  virtual void set_default()
  {
162
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
unknown's avatar
unknown committed
163
					  table->record[0]);
164
    memcpy(ptr, ptr + l_offset, pack_length());
165 166
    if (null_ptr)
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
167
		  null_ptr[l_offset] & null_bit);
unknown's avatar
unknown committed
168
  }
unknown's avatar
unknown committed
169 170 171 172 173 174
  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(); }
175 176
  inline  int cmp(const uchar *str) { return cmp(ptr,str); }
  virtual int cmp_max(const uchar *a, const uchar *b, uint max_len)
177
    { return cmp(a, b); }
178 179
  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
180
  { return memcmp(a,b,pack_length()); }
181 182 183
  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
184
  { return cmp_binary(ptr, ptr+row_offset); };
185 186 187 188
  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
189
  virtual uint decimals() const { return 0; }
unknown's avatar
unknown committed
190 191 192 193 194
  /*
    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
  */
195
  virtual void sql_type(String &str) const =0;
196
  virtual uint size_of() const =0;		// For new field
unknown's avatar
unknown committed
197
  inline bool is_null(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
198
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
unknown's avatar
unknown committed
199
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
200
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
unknown's avatar
unknown committed
201 202 203 204
  inline bool is_null_in_record(const uchar *record)
  {
    if (!null_ptr)
      return 0;
205
    return test(record[(uint) (null_ptr -table->record[0])] &
unknown's avatar
unknown committed
206 207
		null_bit);
  }
unknown's avatar
unknown committed
208 209 210 211 212 213
  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
214
  inline void set_null(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
215
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
unknown's avatar
unknown committed
216
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
unknown's avatar
unknown committed
217
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
unknown's avatar
unknown committed
218 219
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
  inline bool real_maybe_null(void) { return null_ptr != 0; }
220

221 222 223 224
  enum {
    LAST_NULL_BYTE_UNDEF= 0
  };

225
  /*
226 227 228 229 230 231 232 233 234 235 236 237 238
    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.
239
   */
240 241
  size_t last_null_byte() const {
    size_t bytes= do_last_null_byte();
unknown's avatar
unknown committed
242
    DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
243 244 245 246
    DBUG_ASSERT(bytes <= table->s->null_bytes);
    return bytes;
  }

unknown's avatar
Merge  
unknown committed
247
  virtual void make_field(Send_field *);
248
  virtual void sort_string(uchar *buff,uint length)=0;
249
  virtual bool optimize_range(uint idx, uint part);
250 251 252 253 254 255 256 257
  /*
    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; }
258
  virtual void free() {}
259 260
  virtual Field *new_field(MEM_ROOT *root, struct st_table *new_table,
                           bool keep_type);
unknown's avatar
Merge  
unknown committed
261
  virtual Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
262
                               uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
Merge  
unknown committed
263
                               uint new_null_bit);
unknown's avatar
unknown committed
264
  Field *clone(MEM_ROOT *mem_root, struct st_table *new_table);
265
  inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
unknown's avatar
unknown committed
266 267 268
  {
    ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
  }
269
  inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
unknown's avatar
unknown committed
270
  virtual void move_field_offset(my_ptrdiff_t ptr_diff)
unknown's avatar
unknown committed
271
  {
272
    ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
unknown's avatar
unknown committed
273 274 275
    if (null_ptr)
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
  }
276
  inline void get_image(uchar *buff,uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
277
    { memcpy(buff,ptr,length); }
278
  inline void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
279
    { memcpy(ptr,buff,length); }
unknown's avatar
unknown committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307


  /*
    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).
  */

308
  virtual uint get_key_image(uchar *buff, uint length, imagetype type)
unknown's avatar
unknown committed
309
  {
310
    get_image(buff, length, &my_charset_bin);
unknown's avatar
unknown committed
311 312
    return length;
  }
313
  virtual void set_key_image(const uchar *buff,uint length)
unknown's avatar
Merge  
unknown committed
314
    { set_image(buff,length, &my_charset_bin); }
unknown's avatar
unknown committed
315 316 317 318 319 320 321
  inline longlong val_int_offset(uint row_offset)
    {
      ptr+=row_offset;
      longlong tmp=val_int();
      ptr-=row_offset;
      return tmp;
    }
322
  inline longlong val_int(const uchar *new_ptr)
323
  {
324
    uchar *old_ptr= ptr;
325
    longlong return_value;
326
    ptr= (uchar*) new_ptr;
327 328 329 330
    return_value= val_int();
    ptr= old_ptr;
    return return_value;
  }
331
  inline String *val_str(String *str, const uchar *new_ptr)
unknown's avatar
Merge  
unknown committed
332
  {
333 334
    uchar *old_ptr= ptr;
    ptr= (uchar*) new_ptr;
unknown's avatar
Merge  
unknown committed
335 336 337 338
    val_str(str);
    ptr= old_ptr;
    return str;
  }
unknown's avatar
unknown committed
339
  virtual bool send_binary(Protocol *protocol);
340
  virtual uchar *pack(uchar *to, const uchar *from, uint max_length=~(uint) 0)
unknown's avatar
unknown committed
341
  {
unknown's avatar
unknown committed
342
    uint32 length=pack_length();
unknown's avatar
unknown committed
343 344 345
    memcpy(to,from,length);
    return to+length;
  }
346
  virtual const uchar *unpack(uchar* to, const uchar *from)
unknown's avatar
unknown committed
347 348 349 350 351
  {
    uint length=pack_length();
    memcpy(to,from,length);
    return from+length;
  }
352
  virtual uchar *pack_key(uchar* to, const uchar *from, uint max_length)
353 354 355
  {
    return pack(to,from,max_length);
  }
356
  virtual uchar *pack_key_from_key_image(uchar* to, const uchar *from,
357
					uint max_length)
unknown's avatar
unknown committed
358 359 360
  {
    return pack(to,from,max_length);
  }
361 362
  virtual const uchar *unpack_key(uchar* to, const uchar *from,
                                  uint max_length)
363 364 365
  {
    return unpack(to,from);
  }
366
  virtual uint packed_col_length(const uchar *to, uint length)
367
  { return length;}
unknown's avatar
unknown committed
368
  virtual uint max_packed_col_length(uint max_length)
369
  { return max_length;}
unknown's avatar
unknown committed
370

371
  virtual int pack_cmp(const uchar *a,const uchar *b, uint key_length_arg,
unknown's avatar
Merge  
unknown committed
372
                       my_bool insert_or_update)
unknown's avatar
unknown committed
373
  { return cmp(a,b); }
374
  virtual int pack_cmp(const uchar *b, uint key_length_arg,
unknown's avatar
Merge  
unknown committed
375
                       my_bool insert_or_update)
376
  { return cmp(ptr,b); }
377
  uint offset(uchar *record)
378
  {
379
    return (uint) (ptr - record);
380
  }
unknown's avatar
unknown committed
381 382
  void copy_from_tmp(int offset);
  uint fill_cache_field(struct st_cache_field *copy);
383 384
  virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  virtual bool get_time(MYSQL_TIME *ltime);
unknown's avatar
unknown committed
385
  virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
386
  virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
387
  virtual bool has_charset(void) const { return FALSE; }
388
  virtual void set_charset(CHARSET_INFO *charset_arg) { }
389 390
  virtual enum Derivation derivation(void) const
  { return DERIVATION_IMPLICIT; }
391
  virtual void set_derivation(enum Derivation derivation_arg) { }
392
  bool set_warning(MYSQL_ERROR::enum_warning_level, unsigned int code,
393
                   int cuted_increment);
394
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code, 
395 396
                            const char *str, uint str_len,
                            timestamp_type ts_type, int cuted_increment);
397
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code, 
398 399
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment);
400
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, const uint code, 
401
                            double nr, timestamp_type ts_type);
unknown's avatar
unknown committed
402 403 404 405 406
  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
407 408 409 410 411 412
  void init(TABLE *table_arg)
  {
    orig_table= table= table_arg;
    table_name= &table_arg->alias;
  }

unknown's avatar
unknown committed
413
  /* maximum possible display length */
414
  virtual uint32 max_display_length()= 0;
unknown's avatar
unknown committed
415

unknown's avatar
unknown committed
416
  virtual uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
417 418 419
  /* convert decimal to longlong with overflow check */
  longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
                                    int *err);
420
  /* The max. number of characters */
421
  inline uint32 char_length() const
422 423 424 425
  {
    return field_length / charset()->mbmaxlen;
  }

426 427 428 429
  virtual geometry_type get_geometry_type()
  {
    /* shouldn't get here. */
    DBUG_ASSERT(0);
430
    return GEOM_GEOMETRY;
431
  }
432 433
  /* Hash value */
  virtual void hash(ulong *nr, ulong *nr2);
unknown's avatar
unknown committed
434
  friend bool reopen_table(THD *,struct st_table *,bool);
435
  friend int cre_myisam(char * name, register TABLE *form, uint options,
unknown's avatar
unknown committed
436 437 438 439 440 441 442 443 444 445 446 447
			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;
448
  friend class Item_func_group_concat;
449 450

private:
451 452 453 454 455 456 457 458 459 460 461
  /*
    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.
   */
462
  virtual size_t do_last_null_byte() const;
unknown's avatar
unknown committed
463 464 465 466 467 468
};


class Field_num :public Field {
public:
  const uint8 dec;
469
  bool zerofill,unsigned_flag;	// Purify cannot handle bit fields
470
  Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
471
	    uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
472
	    const char *field_name_arg,
unknown's avatar
unknown committed
473
            uint8 dec_arg, bool zero_arg, bool unsigned_arg);
unknown's avatar
unknown committed
474 475 476
  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
477
  friend class Create_field;
unknown's avatar
unknown committed
478
  void make_field(Send_field *);
unknown's avatar
unknown committed
479
  uint decimals() const { return (uint) dec; }
unknown's avatar
unknown committed
480 481
  uint size_of() const { return sizeof(*this); }
  bool eq_def(Field *field);
unknown's avatar
unknown committed
482 483
  int store_decimal(const my_decimal *);
  my_decimal *val_decimal(my_decimal *);
unknown's avatar
unknown committed
484
  uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
485 486 487 488 489
  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
490 491 492 493
};


class Field_str :public Field {
494 495
protected:
  CHARSET_INFO *field_charset;
496
  enum Derivation field_derivation;
unknown's avatar
unknown committed
497
public:
498
  Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
499
	    uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
500
	    const char *field_name_arg, CHARSET_INFO *charset);
unknown's avatar
unknown committed
501 502
  Item_result result_type () const { return STRING_RESULT; }
  uint decimals() const { return NOT_FIXED_DEC; }
unknown's avatar
unknown committed
503
  int  store(double nr);
504
  int  store(longlong nr, bool unsigned_val)=0;
unknown's avatar
unknown committed
505
  int  store_decimal(const my_decimal *);
unknown's avatar
unknown committed
506
  int  store(const char *to,uint length,CHARSET_INFO *cs)=0;
unknown's avatar
unknown committed
507
  uint size_of() const { return sizeof(*this); }
unknown's avatar
unknown committed
508
  CHARSET_INFO *charset(void) const { return field_charset; }
509
  void set_charset(CHARSET_INFO *charset_arg) { field_charset= charset_arg; }
510 511 512
  enum Derivation derivation(void) const { return field_derivation; }
  virtual void set_derivation(enum Derivation derivation_arg)
  { field_derivation= derivation_arg; }
513
  bool binary() const { return field_charset == &my_charset_bin; }
514
  uint32 max_display_length() { return field_length; }
unknown's avatar
unknown committed
515
  friend class Create_field;
unknown's avatar
unknown committed
516
  my_decimal *val_decimal(my_decimal *);
517
  virtual bool str_needs_quotes() { return TRUE; }
unknown's avatar
unknown committed
518 519
  bool compare_str_field_flags(Create_field *new_field, uint32 flags);
  uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
520 521
};

522 523 524

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

unknown's avatar
unknown committed
525 526 527
class Field_longstr :public Field_str
{
public:
528
  Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
529
                uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
530
                const char *field_name_arg, CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
531
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
unknown's avatar
unknown committed
532
               field_name_arg, charset_arg)
unknown's avatar
unknown committed
533 534 535 536 537 538 539 540
    {}

  int store_decimal(const my_decimal *d);
};

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

543
  Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
544 545 546 547
             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,
548
               field_name_arg, dec_arg, zero_arg, unsigned_arg),
549
    not_fixed(dec_arg >= NOT_FIXED_DEC)
unknown's avatar
unknown committed
550 551 552
    {}
  int store_decimal(const my_decimal *);
  my_decimal *val_decimal(my_decimal *);
553
  int truncate(double *nr, double max_length);
554
  uint32 max_display_length() { return field_length; }
555
  uint size_of() const { return sizeof(*this); }
unknown's avatar
unknown committed
556 557 558
};


unknown's avatar
unknown committed
559
class Field_decimal :public Field_real {
unknown's avatar
unknown committed
560
public:
561
  Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
562
		uchar null_bit_arg,
unknown's avatar
unknown committed
563
		enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
564
		uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
565
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
566
                unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
567
                dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
568
    {}
569
  enum_field_types type() const { return MYSQL_TYPE_DECIMAL;}
unknown's avatar
unknown committed
570
  enum ha_base_keytype key_type() const
571
  { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
572 573 574
  int reset(void);
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
575
  int store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
576 577 578
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
579 580
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
581 582 583 584 585 586
  void overflow(bool negative);
  bool zero_pack() const { return 0; }
  void sql_type(String &str) const;
};


unknown's avatar
unknown committed
587 588 589
/* New decimal/numeric field which use fixed point arithmetic */
class Field_new_decimal :public Field_num {
public:
unknown's avatar
unknown committed
590 591
  /* The maximum number of decimal digits can be stored */
  uint precision;
unknown's avatar
unknown committed
592
  uint bin_size;
unknown's avatar
unknown committed
593 594 595 596 597 598
  /*
    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)) 
  */
599
  Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
600 601 602 603
                    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
604
                    const char *field_name_arg, uint8 dec_arg,
unknown's avatar
unknown committed
605
                    bool unsigned_arg);
606
  enum_field_types type() const { return MYSQL_TYPE_NEWDECIMAL;}
unknown's avatar
unknown committed
607 608
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
  Item_result result_type () const { return DECIMAL_RESULT; }
609
  int  reset(void);
unknown's avatar
unknown committed
610 611 612 613
  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);
614
  int  store(longlong nr, bool unsigned_val);
615
  int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
unknown's avatar
unknown committed
616 617 618 619 620
  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 *);
621 622
  int cmp(const uchar *, const uchar *);
  void sort_string(uchar *buff, uint length);
unknown's avatar
unknown committed
623 624
  bool zero_pack() const { return 0; }
  void sql_type(String &str) const;
625
  uint32 max_display_length() { return field_length; }
unknown's avatar
unknown committed
626 627
  uint size_of() const { return sizeof(*this); } 
  uint32 pack_length() const { return (uint32) bin_size; }
unknown's avatar
unknown committed
628
  uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
629 630 631
};


unknown's avatar
unknown committed
632 633
class Field_tiny :public Field_num {
public:
634
  Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
635
	     uchar null_bit_arg,
unknown's avatar
unknown committed
636 637 638
	     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
639
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
640 641 642
	       0, zero_arg,unsigned_arg)
    {}
  enum Item_result result_type () const { return INT_RESULT; }
643
  enum_field_types type() const { return MYSQL_TYPE_TINY;}
unknown's avatar
unknown committed
644 645
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
646 647
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
648
  int store(longlong nr, bool unsigned_val);
649
  int reset(void) { ptr[0]=0; return 0; }
unknown's avatar
unknown committed
650 651 652
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
653
  bool send_binary(Protocol *protocol);
654 655
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
656 657
  uint32 pack_length() const { return 1; }
  void sql_type(String &str) const;
658
  uint32 max_display_length() { return 4; }
unknown's avatar
unknown committed
659 660 661 662 663
};


class Field_short :public Field_num {
public:
664
  Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
665
	      uchar null_bit_arg,
unknown's avatar
unknown committed
666 667 668
	      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
669
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
670 671
	       0, zero_arg,unsigned_arg)
    {}
672
  Field_short(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
673
	      bool unsigned_arg)
674
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
675
	       NONE, field_name_arg, 0, 0, unsigned_arg)
676
    {}
unknown's avatar
unknown committed
677
  enum Item_result result_type () const { return INT_RESULT; }
678
  enum_field_types type() const { return MYSQL_TYPE_SHORT;}
unknown's avatar
unknown committed
679 680
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
681 682
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
683
  int store(longlong nr, bool unsigned_val);
684
  int reset(void) { ptr[0]=ptr[1]=0; return 0; }
unknown's avatar
unknown committed
685 686 687
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
688
  bool send_binary(Protocol *protocol);
689 690
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
691 692
  uint32 pack_length() const { return 2; }
  void sql_type(String &str) const;
693
  uint32 max_display_length() { return 6; }
unknown's avatar
unknown committed
694 695 696 697 698
};


class Field_medium :public Field_num {
public:
699
  Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
700
	      uchar null_bit_arg,
unknown's avatar
unknown committed
701 702 703
	      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
704
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
705 706 707
	       0, zero_arg,unsigned_arg)
    {}
  enum Item_result result_type () const { return INT_RESULT; }
708
  enum_field_types type() const { return MYSQL_TYPE_INT24;}
unknown's avatar
unknown committed
709 710
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
711 712
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
713
  int store(longlong nr, bool unsigned_val);
714
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
unknown's avatar
unknown committed
715 716 717
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
718
  bool send_binary(Protocol *protocol);
719 720
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
721 722
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
723
  uint32 max_display_length() { return 8; }
unknown's avatar
unknown committed
724 725 726 727 728
};


class Field_long :public Field_num {
public:
729
  Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
730
	     uchar null_bit_arg,
unknown's avatar
unknown committed
731 732 733
	     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
734
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
735 736 737
	       0, zero_arg,unsigned_arg)
    {}
  Field_long(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
738
	     bool unsigned_arg)
739
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
740
	       NONE, field_name_arg,0,0,unsigned_arg)
unknown's avatar
unknown committed
741 742
    {}
  enum Item_result result_type () const { return INT_RESULT; }
743
  enum_field_types type() const { return MYSQL_TYPE_LONG;}
unknown's avatar
unknown committed
744 745
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
746 747
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
748
  int store(longlong nr, bool unsigned_val);
749
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
unknown's avatar
unknown committed
750 751
  double val_real(void);
  longlong val_int(void);
752
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
753
  String *val_str(String*,String *);
754 755
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
756 757
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
758
  uint32 max_display_length() { return MY_INT32_NUM_DECIMAL_DIGITS; }
unknown's avatar
unknown committed
759 760 761 762 763 764
};


#ifdef HAVE_LONG_LONG
class Field_longlong :public Field_num {
public:
765
  Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
766
	      uchar null_bit_arg,
unknown's avatar
unknown committed
767 768 769
	      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
770
	       unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
771 772
	       0, zero_arg,unsigned_arg)
    {}
773 774
  Field_longlong(uint32 len_arg,bool maybe_null_arg,
		 const char *field_name_arg,
unknown's avatar
unknown committed
775
		  bool unsigned_arg)
776
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
777
	       NONE, field_name_arg,0,0,unsigned_arg)
unknown's avatar
unknown committed
778 779
    {}
  enum Item_result result_type () const { return INT_RESULT; }
780
  enum_field_types type() const { return MYSQL_TYPE_LONGLONG;}
unknown's avatar
unknown committed
781 782
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
783 784
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
785
  int store(longlong nr, bool unsigned_val);
786 787 788 789 790
  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
791 792 793
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
794
  bool send_binary(Protocol *protocol);
795 796
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
797 798
  uint32 pack_length() const { return 8; }
  void sql_type(String &str) const;
799
  bool can_be_compared_as_longlong() const { return TRUE; }
800
  uint32 max_display_length() { return 20; }
unknown's avatar
unknown committed
801 802 803
};
#endif

unknown's avatar
unknown committed
804 805

class Field_float :public Field_real {
unknown's avatar
unknown committed
806
public:
807
  Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
808
	      uchar null_bit_arg,
unknown's avatar
unknown committed
809
	      enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
810 811
              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
812
                unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
813
                dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
814
    {}
815
  Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
816
	      uint8 dec_arg)
817
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
unknown's avatar
unknown committed
818
                NONE, field_name_arg, dec_arg, 0, 0)
819
    {}
820
  enum_field_types type() const { return MYSQL_TYPE_FLOAT;}
unknown's avatar
unknown committed
821
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
822 823
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
824
  int store(longlong nr, bool unsigned_val);
825
  int reset(void) { bzero(ptr,sizeof(float)); return 0; }
unknown's avatar
unknown committed
826 827 828
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
829
  bool send_binary(Protocol *protocol);
830 831
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
832 833 834 835 836
  uint32 pack_length() const { return sizeof(float); }
  void sql_type(String &str) const;
};


unknown's avatar
unknown committed
837
class Field_double :public Field_real {
unknown's avatar
unknown committed
838
public:
839
  Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
840
	       uchar null_bit_arg,
unknown's avatar
unknown committed
841
	       enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
842
	       uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
843
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
844
                unireg_check_arg, field_name_arg,
845
                dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
846 847
    {}
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
848
	       uint8 dec_arg)
849
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
850
                NONE, field_name_arg, dec_arg, 0, 0)
851 852
    {}
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
853
	       uint8 dec_arg, my_bool not_fixed_arg)
854
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
855
                NONE, field_name_arg, dec_arg, 0, 0)
856
    {not_fixed= not_fixed_arg; }
857
  enum_field_types type() const { return MYSQL_TYPE_DOUBLE;}
unknown's avatar
unknown committed
858
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
859 860
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
861
  int  store(longlong nr, bool unsigned_val);
862
  int reset(void) { bzero(ptr,sizeof(double)); return 0; }
unknown's avatar
unknown committed
863 864 865
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
866
  bool send_binary(Protocol *protocol);
867 868
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
869 870 871 872 873
  uint32 pack_length() const { return sizeof(double); }
  void sql_type(String &str) const;
};


unknown's avatar
unknown committed
874
/* Everything saved in this will disappear. It will always return NULL */
unknown's avatar
unknown committed
875 876 877 878

class Field_null :public Field_str {
  static uchar null[1];
public:
879
  Field_null(uchar *ptr_arg, uint32 len_arg,
unknown's avatar
unknown committed
880
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
881
	     CHARSET_INFO *cs)
unknown's avatar
unknown committed
882
    :Field_str(ptr_arg, len_arg, null, 1,
unknown's avatar
unknown committed
883
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
884
    {}
885
  enum_field_types type() const { return MYSQL_TYPE_NULL;}
886 887
  int  store(const char *to, uint length, CHARSET_INFO *cs)
  { null[0]=1; return 0; }
888
  int store(double nr)   { null[0]=1; return 0; }
889 890
  int store(longlong nr, bool unsigned_val) { null[0]=1; return 0; }
  int store_decimal(const my_decimal *d)  { null[0]=1; return 0; }
891
  int reset(void)	  { return 0; }
unknown's avatar
unknown committed
892 893
  double val_real(void)		{ return 0.0;}
  longlong val_int(void)	{ return 0;}
unknown's avatar
unknown committed
894
  my_decimal *val_decimal(my_decimal *) { return 0; }
unknown's avatar
unknown committed
895 896
  String *val_str(String *value,String *value2)
  { value2->length(0); return value2;}
897 898
  int cmp(const uchar *a, const uchar *b) { return 0;}
  void sort_string(uchar *buff, uint length)  {}
unknown's avatar
unknown committed
899
  uint32 pack_length() const { return 0; }
900
  void sql_type(String &str) const;
unknown's avatar
unknown committed
901
  uint size_of() const { return sizeof(*this); }
902
  uint32 max_display_length() { return 4; }
unknown's avatar
unknown committed
903 904 905
};


906
class Field_timestamp :public Field_str {
unknown's avatar
unknown committed
907
public:
908
  Field_timestamp(uchar *ptr_arg, uint32 len_arg,
909
                  uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
910
		  enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
911
		  TABLE_SHARE *share, CHARSET_INFO *cs);
912
  Field_timestamp(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
913
		  CHARSET_INFO *cs);
914
  enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;}
unknown's avatar
unknown committed
915
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
916
  enum Item_result cmp_type () const { return INT_RESULT; }
917 918
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
919
  int  store(longlong nr, bool unsigned_val);
920
  int  reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
unknown's avatar
unknown committed
921 922 923
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
924
  bool send_binary(Protocol *protocol);
925 926
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
927 928
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
929
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
930 931
  bool zero_pack() const { return 0; }
  void set_time();
unknown's avatar
unknown committed
932 933
  virtual void set_default()
  {
934 935
    if (table->timestamp_field == this &&
        unireg_check != TIMESTAMP_UN_FIELD)
936 937 938
      set_time();
    else
      Field::set_default();
unknown's avatar
unknown committed
939
  }
940 941
  /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
  inline long get_timestamp(my_bool *null_value)
unknown's avatar
unknown committed
942
  {
943 944
    if ((*null_value= is_null()))
      return 0;
unknown's avatar
unknown committed
945
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
946
    if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
947 948 949 950 951 952
      return sint4korr(ptr);
#endif
    long tmp;
    longget(tmp,ptr);
    return tmp;
  }
953 954
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  bool get_time(MYSQL_TIME *ltime);
955
  timestamp_auto_set_type get_auto_set_type() const;
unknown's avatar
unknown committed
956 957 958 959 960
};


class Field_year :public Field_tiny {
public:
961
  Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
962
	     uchar null_bit_arg,
unknown's avatar
unknown committed
963
	     enum utype unireg_check_arg, const char *field_name_arg)
unknown's avatar
unknown committed
964
    :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
965
		unireg_check_arg, field_name_arg, 1, 1)
unknown's avatar
unknown committed
966
    {}
967
  enum_field_types type() const { return MYSQL_TYPE_YEAR;}
968 969
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
970
  int  store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
971 972 973
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
974
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
975
  void sql_type(String &str) const;
976
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
977 978 979 980 981
};


class Field_date :public Field_str {
public:
982
  Field_date(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
983
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
984
	     CHARSET_INFO *cs)
unknown's avatar
unknown committed
985
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
986
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
987
    {}
988
  Field_date(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
989
             CHARSET_INFO *cs)
990
    :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
991
	       NONE, field_name_arg, cs) {}
992
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
unknown's avatar
unknown committed
993 994
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
  enum Item_result cmp_type () const { return INT_RESULT; }
995 996
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
997
  int store(longlong nr, bool unsigned_val);
998
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
unknown's avatar
unknown committed
999 1000 1001
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1002
  bool send_binary(Protocol *protocol);
1003 1004
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1005 1006
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
1007
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1008 1009 1010
  bool zero_pack() const { return 1; }
};

unknown's avatar
unknown committed
1011

unknown's avatar
unknown committed
1012 1013
class Field_newdate :public Field_str {
public:
1014
  Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1015
		enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1016
		CHARSET_INFO *cs)
unknown's avatar
unknown committed
1017
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1018
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1019
    {}
1020 1021
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
  enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; }
unknown's avatar
unknown committed
1022 1023
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
  enum Item_result cmp_type () const { return INT_RESULT; }
1024 1025
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1026
  int  store(longlong nr, bool unsigned_val);
1027
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
1028
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
unknown's avatar
unknown committed
1029 1030 1031
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1032
  bool send_binary(Protocol *protocol);
1033 1034
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1035 1036
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
1037
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1038
  bool zero_pack() const { return 1; }
1039 1040
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  bool get_time(MYSQL_TIME *ltime);
unknown's avatar
unknown committed
1041 1042 1043 1044 1045
};


class Field_time :public Field_str {
public:
1046
  Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1047
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1048
	     CHARSET_INFO *cs)
unknown's avatar
unknown committed
1049
    :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1050
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1051
    {}
1052
  Field_time(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1053
             CHARSET_INFO *cs)
1054
    :Field_str((uchar*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
1055
	       NONE, field_name_arg, cs) {}
1056
  enum_field_types type() const { return MYSQL_TYPE_TIME;}
unknown's avatar
unknown committed
1057 1058
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
  enum Item_result cmp_type () const { return INT_RESULT; }
1059
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
1060 1061
  int store(const char *to,uint length,CHARSET_INFO *charset);
  int store(double nr);
1062
  int store(longlong nr, bool unsigned_val);
1063
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
unknown's avatar
unknown committed
1064 1065 1066
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1067
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
1068
  bool send_binary(Protocol *protocol);
1069
  bool get_time(MYSQL_TIME *ltime);
1070 1071
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1072 1073
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
1074
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1075 1076 1077 1078 1079 1080
  bool zero_pack() const { return 1; }
};


class Field_datetime :public Field_str {
public:
1081
  Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1082
		 enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1083
		 CHARSET_INFO *cs)
unknown's avatar
unknown committed
1084
    :Field_str(ptr_arg, 19, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1085
	       unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
1086
    {}
1087
  Field_datetime(bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1088
		 CHARSET_INFO *cs)
1089
    :Field_str((uchar*) 0,19, maybe_null_arg ? (uchar*) "": 0,0,
unknown's avatar
unknown committed
1090
	       NONE, field_name_arg, cs) {}
1091
  enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
unknown's avatar
unknown committed
1092 1093 1094 1095
#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
1096
  uint decimals() const { return DATETIME_DEC; }
1097 1098
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1099
  int  store(longlong nr, bool unsigned_val);
1100
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
1101 1102 1103 1104 1105
  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
1106 1107 1108
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1109
  bool send_binary(Protocol *protocol);
1110 1111
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1112 1113
  uint32 pack_length() const { return 8; }
  void sql_type(String &str) const;
1114
  bool can_be_compared_as_longlong() const { return TRUE; }
unknown's avatar
unknown committed
1115
  bool zero_pack() const { return 1; }
1116 1117
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
  bool get_time(MYSQL_TIME *ltime);
unknown's avatar
unknown committed
1118 1119 1120
};


unknown's avatar
unknown committed
1121
class Field_string :public Field_longstr {
unknown's avatar
unknown committed
1122
public:
1123
  bool can_alter_field_type;
1124
  Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
1125
	       uchar null_bit_arg,
unknown's avatar
unknown committed
1126
	       enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1127
	       CHARSET_INFO *cs)
unknown's avatar
unknown committed
1128
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1129
                   unireg_check_arg, field_name_arg, cs),
1130
     can_alter_field_type(1) {};
unknown's avatar
unknown committed
1131
  Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1132
               CHARSET_INFO *cs)
1133
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1134
                   NONE, field_name_arg, cs),
1135
     can_alter_field_type(1) {};
unknown's avatar
unknown committed
1136 1137 1138

  enum_field_types type() const
  {
1139
    return ((can_alter_field_type && orig_table &&
unknown's avatar
Merge  
unknown committed
1140 1141 1142 1143
             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
1144 1145
  }
  enum ha_base_keytype key_type() const
1146
    { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
unknown's avatar
unknown committed
1147
  bool zero_pack() const { return 0; }
1148 1149
  int reset(void)
  {
1150
    charset()->cset->fill(charset(),(char*) ptr, field_length,
1151
                          (has_charset() ? ' ' : 0));
1152 1153 1154
    return 0;
  }
  int store(const char *to,uint length,CHARSET_INFO *charset);
1155
  int store(longlong nr, bool unsigned_val);
1156
  int store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
unknown's avatar
unknown committed
1157 1158 1159
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
unknown's avatar
unknown committed
1160
  my_decimal *val_decimal(my_decimal *);
1161 1162
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1163
  void sql_type(String &str) const;
1164 1165 1166
  uchar *pack(uchar *to, const uchar *from, uint max_length=~(uint) 0);
  const uchar *unpack(uchar* to, const uchar *from);
  int pack_cmp(const uchar *a,const uchar *b,uint key_length,
unknown's avatar
Merge  
unknown committed
1167
               my_bool insert_or_update);
1168 1169
  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
1170 1171
  uint max_packed_col_length(uint max_length);
  uint size_of() const { return sizeof(*this); }
1172
  enum_field_types real_type() const { return MYSQL_TYPE_STRING; }
1173 1174
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
1175
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
1176
  virtual uint get_key_image(uchar *buff,uint length, imagetype type);
unknown's avatar
unknown committed
1177 1178 1179
};


unknown's avatar
unknown committed
1180
class Field_varstring :public Field_longstr {
unknown's avatar
unknown committed
1181
public:
unknown's avatar
unknown committed
1182 1183 1184 1185
  /*
    The maximum space available in a Field_varstring, in bytes. See
    length_bytes.
  */
unknown's avatar
unknown committed
1186
  static const uint MAX_SIZE= UINT_MAX16;
unknown's avatar
Merge  
unknown committed
1187 1188
  /* Store number of bytes used to store length (1 or 2) */
  uint32 length_bytes;
1189
  Field_varstring(uchar *ptr_arg,
unknown's avatar
Merge  
unknown committed
1190
                  uint32 len_arg, uint length_bytes_arg,
unknown's avatar
unknown committed
1191
                  uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1192
		  enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1193
		  TABLE_SHARE *share, CHARSET_INFO *cs)
unknown's avatar
unknown committed
1194
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1195
                   unireg_check_arg, field_name_arg, cs),
unknown's avatar
unknown committed
1196
     length_bytes(length_bytes_arg)
unknown's avatar
Merge  
unknown committed
1197
  {
unknown's avatar
unknown committed
1198
    share->varchar_fields++;
unknown's avatar
Merge  
unknown committed
1199
  }
unknown's avatar
unknown committed
1200
  Field_varstring(uint32 len_arg,bool maybe_null_arg,
unknown's avatar
unknown committed
1201
                  const char *field_name_arg,
unknown's avatar
unknown committed
1202
                  TABLE_SHARE *share, CHARSET_INFO *cs)
1203
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1204
                   NONE, field_name_arg, cs),
unknown's avatar
unknown committed
1205
     length_bytes(len_arg < 256 ? 1 :2)
unknown's avatar
Merge  
unknown committed
1206
  {
unknown's avatar
unknown committed
1207
    share->varchar_fields++;
unknown's avatar
Merge  
unknown committed
1208
  }
unknown's avatar
unknown committed
1209

unknown's avatar
Merge  
unknown committed
1210 1211
  enum_field_types type() const { return MYSQL_TYPE_VARCHAR; }
  enum ha_base_keytype key_type() const;
unknown's avatar
unknown committed
1212
  bool zero_pack() const { return 0; }
1213
  int  reset(void) { bzero(ptr,field_length+length_bytes); return 0; }
unknown's avatar
Merge  
unknown committed
1214
  uint32 pack_length() const { return (uint32) field_length+length_bytes; }
unknown's avatar
unknown committed
1215
  uint32 key_length() const { return (uint32) field_length; }
1216 1217 1218 1219 1220
  uint32 sort_length() const
  {
    return (uint32) field_length + (field_charset == &my_charset_bin ?
                                    length_bytes : 0);
  }
1221
  int  store(const char *to,uint length,CHARSET_INFO *charset);
1222
  int  store(longlong nr, bool unsigned_val);
1223
  int  store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
unknown's avatar
unknown committed
1224 1225 1226
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
unknown's avatar
unknown committed
1227
  my_decimal *val_decimal(my_decimal *);
1228 1229
  int cmp_max(const uchar *, const uchar *, uint max_length);
  int cmp(const uchar *a,const uchar *b)
1230
  {
unknown's avatar
unknown committed
1231
    return cmp_max(a, b, ~0L);
1232
  }
1233 1234 1235
  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
1236
  void sql_type(String &str) const;
1237 1238 1239 1240 1241 1242 1243
  uchar *pack(uchar *to, const uchar *from, uint max_length=~(uint) 0);
  uchar *pack_key(uchar *to, const uchar *from, uint max_length);
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
                                 uint max_length);
  const uchar *unpack(uchar* to, const uchar *from);
  const uchar *unpack_key(uchar* to, const uchar *from, uint max_length);
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
unknown's avatar
Merge  
unknown committed
1244
               my_bool insert_or_update);
1245 1246 1247 1248 1249
  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
1250
  uint max_packed_col_length(uint max_length);
1251
  uint32 data_length();
unknown's avatar
unknown committed
1252
  uint size_of() const { return sizeof(*this); }
unknown's avatar
Merge  
unknown committed
1253
  enum_field_types real_type() const { return MYSQL_TYPE_VARCHAR; }
1254 1255
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
1256
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
unknown's avatar
Merge  
unknown committed
1257
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
1258
                       uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
Merge  
unknown committed
1259
                       uint new_null_bit);
unknown's avatar
unknown committed
1260
  uint is_equal(Create_field *new_field);
1261
  void hash(ulong *nr, ulong *nr2);
unknown's avatar
unknown committed
1262 1263 1264
};


unknown's avatar
unknown committed
1265
class Field_blob :public Field_longstr {
unknown's avatar
unknown committed
1266
protected:
unknown's avatar
unknown committed
1267
  uint packlength;
1268
  String value;				// For temporaries
unknown's avatar
unknown committed
1269
public:
1270
  Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
1271
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1272
	     TABLE_SHARE *share, uint blob_pack_length, CHARSET_INFO *cs);
unknown's avatar
unknown committed
1273
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1274
             CHARSET_INFO *cs)
1275
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1276
                   NONE, field_name_arg, cs),
unknown's avatar
unknown committed
1277
    packlength(4)
unknown's avatar
unknown committed
1278 1279 1280
  {
    flags|= BLOB_FLAG;
  }
1281
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1282
	     CHARSET_INFO *cs, bool set_packlength)
1283
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
1284
                   NONE, field_name_arg, cs)
1285 1286 1287 1288 1289
  {
    flags|= BLOB_FLAG;
    packlength= 4;
    if (set_packlength)
    {
1290 1291 1292 1293
      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;
1294 1295
    }
  }
1296
  enum_field_types type() const { return MYSQL_TYPE_BLOB;}
unknown's avatar
unknown committed
1297
  enum ha_base_keytype key_type() const
unknown's avatar
Merge  
unknown committed
1298
    { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
1299 1300
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1301
  int  store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1302 1303 1304
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
unknown's avatar
unknown committed
1305
  my_decimal *val_decimal(my_decimal *);
1306 1307
  int cmp_max(const uchar *, const uchar *, uint max_length);
  int cmp(const uchar *a,const uchar *b)
unknown's avatar
unknown committed
1308
    { return cmp_max(a, b, ~0L); }
1309 1310 1311 1312
  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
1313
  uint32 key_length() const { return 0; }
1314
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1315
  uint32 pack_length() const
unknown's avatar
Merge  
unknown committed
1316
  { return (uint32) (packlength+table->s->blob_ptr_size); }
1317
  uint32 sort_length() const;
1318
  inline uint32 max_data_length() const
1319
  {
1320
    return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
1321
  }
1322 1323
  int reset(void) { bzero(ptr, packlength+sizeof(uchar*)); return 0; }
  void reset_fields() { bzero((uchar*) &value,sizeof(value)); }
unknown's avatar
unknown committed
1324 1325 1326
#ifndef WORDS_BIGENDIAN
  static
#endif
unknown's avatar
unknown committed
1327
  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number);
1328 1329 1330 1331
  inline void store_length(uint32 number)
  {
    store_length(ptr, packlength, number);
  }
1332
 
unknown's avatar
unknown committed
1333
  inline uint32 get_length(uint row_offset=0)
unknown's avatar
unknown committed
1334
  { return get_length(ptr+row_offset); }
1335 1336 1337
  uint32 get_length(const uchar *ptr);
  void put_length(uchar *pos, uint32 length);
  inline void get_ptr(uchar **str)
unknown's avatar
unknown committed
1338
    {
1339
      memcpy_fixed((uchar*) str,ptr+packlength,sizeof(uchar*));
unknown's avatar
unknown committed
1340
    }
1341
  inline void get_ptr(uchar **str, uint row_offset)
1342
    {
1343
      memcpy_fixed((uchar*) str,ptr+packlength+row_offset,sizeof(char*));
1344
    }
1345
  inline void set_ptr(uchar *length, uchar *data)
unknown's avatar
unknown committed
1346 1347 1348 1349
    {
      memcpy(ptr,length,packlength);
      memcpy_fixed(ptr+packlength,&data,sizeof(char*));
    }
unknown's avatar
unknown committed
1350
  void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, uchar *data)
1351
    {
unknown's avatar
unknown committed
1352
      uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
1353 1354 1355
      store_length(ptr_ofs, packlength, length);
      memcpy_fixed(ptr_ofs+packlength,&data,sizeof(char*));
    }
1356
  inline void set_ptr(uint32 length, uchar *data)
unknown's avatar
unknown committed
1357
    {
1358
      set_ptr_offset(0, length, data);
unknown's avatar
unknown committed
1359
    }
1360 1361
  uint get_key_image(uchar *buff,uint length, imagetype type);
  void set_key_image(const uchar *buff,uint length);
unknown's avatar
unknown committed
1362 1363
  void sql_type(String &str) const;
  inline bool copy()
1364 1365
  {
    uchar *tmp;
unknown's avatar
unknown committed
1366
    get_ptr(&tmp);
1367
    if (value.copy((char*) tmp, get_length(),charset()))
unknown's avatar
unknown committed
1368 1369 1370 1371
    {
      Field_blob::reset();
      return 1;
    }
1372 1373
    tmp=(uchar*) value.ptr();
    memcpy_fixed(ptr+packlength,&tmp,sizeof(char*));
unknown's avatar
unknown committed
1374 1375
    return 0;
  }
1376 1377 1378 1379 1380 1381 1382
  uchar *pack(uchar *to, const uchar *from, uint max_length= ~(uint) 0);
  uchar *pack_key(uchar *to, const uchar *from, uint max_length);
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
                                 uint max_length);
  const uchar *unpack(uchar *to, const uchar *from);
  const uchar *unpack_key(uchar* to, const uchar *from, uint max_length);
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
unknown's avatar
Merge  
unknown committed
1383
               my_bool insert_or_update);
1384 1385
  int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
  uint packed_col_length(const uchar *col_ptr, uint length);
1386
  uint max_packed_col_length(uint max_length);
1387
  void free() { value.free(); }
1388
  inline void clear_temporary() { bzero((uchar*) &value,sizeof(value)); }
1389
  friend int field_conv(Field *to,Field *from);
unknown's avatar
unknown committed
1390
  uint size_of() const { return sizeof(*this); }
1391
  bool has_charset(void) const
1392
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
1393
  uint32 max_display_length();
unknown's avatar
unknown committed
1394
  uint is_equal(Create_field *new_field);
1395 1396
};

unknown's avatar
Merge  
unknown committed
1397

unknown's avatar
unknown committed
1398
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
1399 1400
class Field_geom :public Field_blob {
public:
unknown's avatar
unknown committed
1401
  enum geometry_type geom_type;
1402

1403
  Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
unknown's avatar
unknown committed
1404
	     enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1405
	     TABLE_SHARE *share, uint blob_pack_length,
unknown's avatar
unknown committed
1406
	     enum geometry_type geom_type_arg)
unknown's avatar
unknown committed
1407
     :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, 
unknown's avatar
unknown committed
1408
                 field_name_arg, share, blob_pack_length, &my_charset_bin)
unknown's avatar
unknown committed
1409
  { geom_type= geom_type_arg; }
unknown's avatar
unknown committed
1410
  Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1411 1412
	     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
1413
  { geom_type= geom_type_arg; }
unknown's avatar
Merge  
unknown committed
1414
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY2; }
1415
  enum_field_types type() const { return MYSQL_TYPE_GEOMETRY; }
unknown's avatar
unknown committed
1416
  void sql_type(String &str) const;
unknown's avatar
unknown committed
1417
  int  store(const char *to, uint length, CHARSET_INFO *charset);
1418
  int  store(double nr);
1419
  int  store(longlong nr, bool unsigned_val);
1420
  int  store_decimal(const my_decimal *);
1421
  uint get_key_image(uchar *buff,uint length,imagetype type);
1422
  uint size_of() const { return sizeof(*this); }
1423
  int  reset(void) { return !maybe_null() || Field_blob::reset(); }
1424
  geometry_type get_geometry_type() { return geom_type; };
unknown's avatar
unknown committed
1425
};
unknown's avatar
unknown committed
1426
#endif /*HAVE_SPATIAL*/
unknown's avatar
unknown committed
1427

unknown's avatar
Merge  
unknown committed
1428

unknown's avatar
unknown committed
1429 1430 1431 1432 1433
class Field_enum :public Field_str {
protected:
  uint packlength;
public:
  TYPELIB *typelib;
1434
  Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1435 1436 1437 1438 1439
             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
1440
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1441
	       unireg_check_arg, field_name_arg, charset_arg),
unknown's avatar
unknown committed
1442
    packlength(packlength_arg),typelib(typelib_arg)
unknown's avatar
unknown committed
1443
  {
unknown's avatar
unknown committed
1444
      flags|=ENUM_FLAG;
unknown's avatar
unknown committed
1445
  }
1446
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
1447
  enum_field_types type() const { return MYSQL_TYPE_STRING; }
unknown's avatar
unknown committed
1448
  enum Item_result cmp_type () const { return INT_RESULT; }
1449
  enum Item_result cast_to_int_type () const { return INT_RESULT; }
unknown's avatar
unknown committed
1450
  enum ha_base_keytype key_type() const;
1451 1452
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
1453
  int  store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1454 1455 1456
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
1457 1458
  int cmp(const uchar *,const uchar *);
  void sort_string(uchar *buff,uint length);
unknown's avatar
unknown committed
1459 1460 1461 1462
  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); }
1463
  enum_field_types real_type() const { return MYSQL_TYPE_ENUM; }
unknown's avatar
unknown committed
1464
  virtual bool zero_pack() const { return 0; }
1465
  bool optimize_range(uint idx, uint part) { return 0; }
unknown's avatar
unknown committed
1466
  bool eq_def(Field *field);
1467
  bool has_charset(void) const { return TRUE; }
1468 1469
  /* enum and set are sorted as integers */
  CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
unknown's avatar
unknown committed
1470 1471 1472 1473 1474
};


class Field_set :public Field_enum {
public:
1475
  Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1476
	    uchar null_bit_arg,
unknown's avatar
unknown committed
1477
	    enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
1478
	    uint32 packlength_arg,
1479
	    TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
1480 1481
    :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
		    unireg_check_arg, field_name_arg,
unknown's avatar
unknown committed
1482 1483
                packlength_arg,
                typelib_arg,charset_arg)
unknown's avatar
unknown committed
1484 1485 1486
    {
      flags=(flags & ~ENUM_FLAG) | SET_FLAG;
    }
1487
  int  store(const char *to,uint length,CHARSET_INFO *charset);
1488 1489
  int  store(double nr) { return Field_set::store((longlong) nr, FALSE); }
  int  store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1490 1491 1492
  virtual bool zero_pack() const { return 1; }
  String *val_str(String*,String *);
  void sql_type(String &str) const;
1493
  enum_field_types real_type() const { return MYSQL_TYPE_SET; }
1494
  bool has_charset(void) const { return TRUE; }
unknown's avatar
unknown committed
1495 1496 1497
};


1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
/*
  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
1512 1513 1514 1515 1516
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
1517
  uint bytes_in_rec;
1518
  Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
Merge  
unknown committed
1519
            uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
unknown's avatar
unknown committed
1520
            enum utype unireg_check_arg, const char *field_name_arg);
1521
  enum_field_types type() const { return MYSQL_TYPE_BIT; }
unknown's avatar
Merge  
unknown committed
1522
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
unknown's avatar
unknown committed
1523
  uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
1524
  uint32 max_display_length() { return field_length; }
unknown's avatar
Merge  
unknown committed
1525 1526
  uint size_of() const { return sizeof(*this); }
  Item_result result_type () const { return INT_RESULT; }
1527
  int reset(void) { bzero(ptr, bytes_in_rec); return 0; }
unknown's avatar
Merge  
unknown committed
1528 1529
  int store(const char *to, uint length, CHARSET_INFO *charset);
  int store(double nr);
1530
  int store(longlong nr, bool unsigned_val);
unknown's avatar
unknown committed
1531
  int store_decimal(const my_decimal *);
unknown's avatar
Merge  
unknown committed
1532 1533 1534
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*, String *);
1535
  virtual bool str_needs_quotes() { return TRUE; }
unknown's avatar
unknown committed
1536
  my_decimal *val_decimal(my_decimal *);
1537
  int cmp(const uchar *a, const uchar *b)
unknown's avatar
Merge  
unknown committed
1538
  { return cmp_binary(a, b); }
1539 1540
  int cmp_binary_offset(uint row_offset)
  { return cmp_offset(row_offset); }
1541 1542 1543 1544
  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
1545
  int cmp_offset(uint row_offset);
1546 1547 1548 1549
  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
1550
  { get_key_image(buff, length, itRAW); }
unknown's avatar
unknown committed
1551 1552
  uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
  uint32 pack_length_in_rec() const { return bytes_in_rec; }
unknown's avatar
Merge  
unknown committed
1553
  void sql_type(String &str) const;
1554 1555
  uchar *pack(uchar *to, const uchar *from, uint max_length=~(uint) 0);
  const uchar *unpack(uchar* to, const uchar *from);
1556 1557
  virtual void set_default();

unknown's avatar
Merge  
unknown committed
1558
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
1559
                       uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
Merge  
unknown committed
1560
                       uint new_null_bit);
unknown's avatar
unknown committed
1561 1562 1563 1564 1565
  void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
  {
    bit_ptr= bit_ptr_arg;
    bit_ofs= bit_ofs_arg;
  }
1566 1567 1568 1569 1570 1571 1572
  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
1573
  uint is_equal(Create_field *new_field);
unknown's avatar
unknown committed
1574 1575 1576 1577 1578
  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*);
  }
1579 1580

private:
1581
  virtual size_t do_last_null_byte() const;
unknown's avatar
Merge  
unknown committed
1582 1583
};

1584

1585 1586 1587 1588 1589 1590 1591
/**
  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.
 */
1592 1593
class Field_bit_as_char: public Field_bit {
public:
1594
  Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1595
                    uchar null_bit_arg,
unknown's avatar
unknown committed
1596
                    enum utype unireg_check_arg, const char *field_name_arg);
1597
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
unknown's avatar
unknown committed
1598
  uint size_of() const { return sizeof(*this); }
1599 1600
  int store(const char *to, uint length, CHARSET_INFO *charset);
  int store(double nr) { return Field_bit::store(nr); }
1601 1602
  int store(longlong nr, bool unsigned_val)
  { return Field_bit::store(nr, unsigned_val); }
1603 1604 1605 1606
  void sql_type(String &str) const;
};


unknown's avatar
unknown committed
1607
/*
unknown's avatar
unknown committed
1608
  Create field class for CREATE TABLE
unknown's avatar
unknown committed
1609 1610
*/

unknown's avatar
unknown committed
1611
class Create_field :public Sql_alloc
1612
{
unknown's avatar
unknown committed
1613 1614
public:
  const char *field_name;
1615 1616
  const char *change;			// If done with alter table
  const char *after;			// Put column after this one
unknown's avatar
unknown committed
1617
  LEX_STRING comment;			// Comment for field
1618
  Item	*def;				// Default value
unknown's avatar
unknown committed
1619
  enum	enum_field_types sql_type;
1620 1621 1622 1623
  /*
    At various stages in execution this can be length of field in bytes or
    max number of characters. 
  */
unknown's avatar
Merge  
unknown committed
1624
  ulong length;
unknown's avatar
unknown committed
1625
  /*
1626 1627
    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
1628
  */
1629
  uint32 char_length;
unknown's avatar
Merge  
unknown committed
1630
  uint  decimals, flags, pack_length, key_length;
unknown's avatar
unknown committed
1631
  Field::utype unireg_check;
1632
  TYPELIB *interval;			// Which interval to use
unknown's avatar
unknown committed
1633 1634
  TYPELIB *save_interval;               // Temporary copy for the above
                                        // Used only for UCS2 intervals
1635
  List<String> interval_list;
1636
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
1637
  Field::geometry_type geom_type;
1638
  Field *field;				// For alter table
unknown's avatar
unknown committed
1639

1640
  uint8 row,col,sc_length,interval_id;	// For rea_create_table
unknown's avatar
unknown committed
1641
  uint	offset,pack_flag;
unknown's avatar
unknown committed
1642 1643
  Create_field() :after(0) {}
  Create_field(Field *field, Field *orig_field);
1644
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
unknown's avatar
unknown committed
1645 1646
  Create_field *clone(MEM_ROOT *mem_root) const
    { return new (mem_root) Create_field(*this); }
1647
  void create_length_to_internal_length(void);
1648 1649 1650 1651 1652

  /* 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);
1653 1654 1655 1656 1657 1658

  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
1659 1660 1661 1662
};


/*
unknown's avatar
unknown committed
1663
  A class for sending info to the client
unknown's avatar
unknown committed
1664 1665 1666 1667
*/

class Send_field {
 public:
unknown's avatar
unknown committed
1668 1669 1670
  const char *db_name;
  const char *table_name,*org_table_name;
  const char *col_name,*org_col_name;
1671 1672
  ulong length;
  uint charsetnr, flags, decimals;
unknown's avatar
unknown committed
1673 1674 1675 1676 1677 1678
  enum_field_types type;
  Send_field() {}
};


/*
1679
  A class for quick copying data to fields
unknown's avatar
unknown committed
1680 1681 1682 1683 1684
*/

class Copy_field :public Sql_alloc {
  void (*get_copy_func(Field *to,Field *from))(Copy_field *);
public:
1685
  uchar *from_ptr,*to_ptr;
unknown's avatar
unknown committed
1686 1687 1688 1689 1690
  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;
1691
  String tmp;					// For items
unknown's avatar
unknown committed
1692 1693 1694

  Copy_field() {}
  ~Copy_field() {}
1695
  void set(Field *to,Field *from,bool save);	// Field to field 
1696
  void set(uchar *to,Field *from);		// Field to string
unknown's avatar
unknown committed
1697
  void (*do_copy)(Copy_field *);
1698
  void (*do_copy2)(Copy_field *);		// Used to handle null values
unknown's avatar
unknown committed
1699 1700 1701
};


1702
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
unknown's avatar
unknown committed
1703
		  uchar *null_pos, uchar null_bit,
1704
		  uint pack_flag, enum_field_types field_type,
1705
		  CHARSET_INFO *cs,
unknown's avatar
unknown committed
1706
		  Field::geometry_type geom_type,
1707
		  Field::utype unireg_check,
unknown's avatar
unknown committed
1708
		  TYPELIB *interval, const char *field_name);
unknown's avatar
unknown committed
1709
uint pack_length_to_packflag(uint type);
unknown's avatar
Merge  
unknown committed
1710
enum_field_types get_blob_type_from_length(ulong length);
unknown's avatar
unknown committed
1711
uint32 calc_pack_length(enum_field_types type,uint32 length);
unknown's avatar
unknown committed
1712 1713
int set_field_to_null(Field *field);
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
unknown's avatar
unknown committed
1714 1715

/*
1716
  The following are for the interface with the .frm file
unknown's avatar
unknown committed
1717 1718 1719
*/

#define FIELDFLAG_DECIMAL		1
1720
#define FIELDFLAG_BINARY		1	// Shares same flag
unknown's avatar
unknown committed
1721 1722
#define FIELDFLAG_NUMBER		2
#define FIELDFLAG_ZEROFILL		4
1723
#define FIELDFLAG_PACK			120	// Bits used for packing
1724 1725 1726 1727
#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
1728

1729 1730
#define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */

unknown's avatar
unknown committed
1731 1732
#define FIELDFLAG_LEFT_FULLSCREEN	8192
#define FIELDFLAG_RIGHT_FULLSCREEN	16384
1733
#define FIELDFLAG_FORMAT_NUMBER		16384	// predit: ###,,## in output
unknown's avatar
Merge  
unknown committed
1734
#define FIELDFLAG_NO_DEFAULT		16384   /* sql */
1735 1736
#define FIELDFLAG_SUM			((uint) 32768)// predit: +#fieldflag
#define FIELDFLAG_MAYBE_NULL		((uint) 32768)// sql
unknown's avatar
unknown committed
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
#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
1750
#define f_decimals(x)		((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
unknown's avatar
unknown committed
1751
#define f_is_alpha(x)		(!f_is_num(x))
1752
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
1753 1754
#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
1755
#define f_is_blob(x)		(((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
1756
#define f_is_geom(x)		(((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
unknown's avatar
unknown committed
1757 1758 1759
#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
1760
#define f_no_default(x)		(x & FIELDFLAG_NO_DEFAULT)
1761
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)