field.h 49.3 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   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.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18
   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 */


/*
19 20
  Because of the function new_field() all field classes that have static
  variables must declare the size_of() member function.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
21 22 23 24 25 26 27 28 29
*/

#ifdef __GNUC__
#pragma interface			/* gcc class implementation */
#endif

#define NOT_FIXED_DEC			31

class Send_field;
30
class Protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
31
struct st_cache_field;
32
void field_conv(Field *to,Field *from);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
33

34 35
class Field
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
36
  Field(const Item &);				/* Prevent use of these */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
37 38
  void operator=(Field &);
public:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
39
  static void *operator new(size_t size) {return (void*) sql_alloc((uint) size); }
40
  static void operator delete(void *ptr_arg, size_t size) {
41
#ifdef SAFEMALLOC
42 43 44
    bfill(ptr_arg, size, 0x8F);
#endif
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
45

46 47
  char		*ptr;			// Position to field in record
  uchar		*null_ptr;		// Byte where null_bit is
48 49 50 51
  /*
    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)
  */
52
  struct st_table *table;		// Pointer for table
53
  struct st_table *orig_table;		// Pointer to original table
54
  const char	*table_name,*field_name;
55
  LEX_STRING	comment;
56
  ulong		query_id;		// For quick test of used fields
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
57
  /* Field is part of the following keys */
58
  key_map	key_start,part_of_key,part_of_sortkey;
59 60 61 62 63 64 65 66
  /* 
    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().
  */
67 68
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
		CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
69 70
                BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
                TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
71 72 73 74 75 76
  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
  };
77
  enum imagetype { itRAW, itMBR};
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
78 79 80 81 82 83 84 85 86 87 88
  enum field_cast_enum
  {
    FIELD_CAST_STOP, FIELD_CAST_DECIMAL, FIELD_CAST_TINY, FIELD_CAST_SHORT,
    FIELD_CAST_MEDIUM, FIELD_CAST_LONG, FIELD_CAST_LONGLONG,
    FIELD_CAST_FLOAT, FIELD_CAST_DOUBLE,
    FIELD_CAST_NULL,
    FIELD_CAST_TIMESTAMP, FIELD_CAST_YEAR, FIELD_CAST_DATE, FIELD_CAST_NEWDATE,
    FIELD_CAST_TIME, FIELD_CAST_DATETIME,
    FIELD_CAST_STRING, FIELD_CAST_VARSTRING, FIELD_CAST_BLOB,
    FIELD_CAST_GEOM, FIELD_CAST_ENUM, FIELD_CAST_SET
  };
89

90
  utype		unireg_check;
91
  uint32	field_length;		// Length of field
92
  uint16	flags;
93
  uchar		null_bit;		// Bit used to test null bit
94 95

  Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
96 97 98
	utype unireg_check_arg, const char *field_name_arg,
	struct st_table *table_arg);
  virtual ~Field() {}
monty@mysql.com's avatar
monty@mysql.com committed
99
  /* Store functions returns 1 on overflow and -1 on fatal error */
100 101 102
  virtual int  store(const char *to,uint length,CHARSET_INFO *cs)=0;
  virtual int  store(double nr)=0;
  virtual int  store(longlong nr)=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
103 104 105
  virtual void store_time(TIME *ltime,timestamp_type t_type);
  virtual double val_real(void)=0;
  virtual longlong val_int(void)=0;
serg@serg.mylan's avatar
serg@serg.mylan 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.
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
119 120 121
  virtual String *val_str(String*,String *)=0;
  virtual Item_result result_type () const=0;
  virtual Item_result cmp_type () const { return result_type(); }
122
  bool eq(Field *field) { return ptr == field->ptr && null_ptr == field->null_ptr; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
123 124 125 126
  virtual bool eq_def(Field *field);
  virtual uint32 pack_length() const { return (uint32) field_length; }
  virtual void reset(void) { bzero(ptr,pack_length()); }
  virtual void reset_fields() {}
127 128
  virtual void set_default()
  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
129 130
    my_ptrdiff_t offset = (my_ptrdiff_t) (table->default_values -
					  table->record[0]);
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
131
    memcpy(ptr, ptr + offset, pack_length());
132 133
    if (null_ptr)
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
134
		  null_ptr[offset] & null_bit);
135
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
136 137 138 139 140 141 142 143
  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(); }
  inline  int cmp(const char *str) { return cmp(ptr,str); }
  virtual int cmp(const char *,const char *)=0;
144
  virtual int cmp_binary(const char *a,const char *b, uint32 max_length=~0L)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
145 146 147 148 149 150 151 152 153 154
  { return memcmp(a,b,pack_length()); }
  virtual int cmp_offset(uint row_offset)
  { return memcmp(ptr,ptr+row_offset,pack_length()); }
  virtual int cmp_binary_offset(uint row_offset)
  { return memcmp(ptr,ptr+row_offset,pack_length()); }
  virtual int key_cmp(const byte *a,const byte *b)
  { return cmp((char*) a,(char*) b); }
  virtual int key_cmp(const byte *str, uint length)
  { return cmp(ptr,(char*) str); }
  virtual uint decimals() const { return 0; }
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
155 156 157 158 159
  /*
    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
  */
160
  virtual void sql_type(String &str) const =0;
161
  virtual uint size_of() const =0;		// For new field
bk@work.mysql.com's avatar
bk@work.mysql.com committed
162 163 164 165
  inline bool is_null(uint row_offset=0)
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
  inline bool is_real_null(uint row_offset=0)
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
166 167 168 169 170 171 172
  inline bool is_null_in_record(const uchar *record)
  {
    if (!null_ptr)
      return 0;
    return test(record[(uint) (null_ptr - (uchar*) table->record[0])] &
		null_bit);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
173 174 175
  inline void set_null(int row_offset=0)
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
  inline void set_notnull(int row_offset=0)
176
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
177 178 179 180
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
  inline bool real_maybe_null(void) { return null_ptr != 0; }
  virtual void make_field(Send_field *)=0;
  virtual void sort_string(char *buff,uint length)=0;
181
  virtual bool optimize_range(uint idx, uint part);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
182
  virtual bool store_for_compare() { return 0; }
183
  virtual void free() {}
184 185 186 187
  Field *new_field(MEM_ROOT *root, struct st_table *new_table)
  {
    Field *tmp= (Field*) memdup_root(root,(char*) this,size_of());
    if (tmp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
188
    {
189 190
      if (tmp->table->maybe_null)
	tmp->flags&= ~NOT_NULL_FLAG;
191
      tmp->table= new_table;
serg@serg.mylan's avatar
serg@serg.mylan committed
192 193 194
      tmp->key_start.init(0);
      tmp->part_of_key.init(0);
      tmp->part_of_sortkey.init(0);
195
      tmp->unireg_check=Field::NONE;
196
      tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
197
		    ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
198
#ifdef PROBABLY_WRONG
199
      tmp->table_name= new_table->table_name;
200
#endif
201
      tmp->reset_fields();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
202
    }
203 204 205 206 207 208
    return tmp;
  }
  inline void move_field(char *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
  {
    ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
209 210 211 212 213 214 215
  inline void move_field(char *ptr_arg) { ptr=ptr_arg; }
  inline void move_field(my_ptrdiff_t ptr_diff)
  {
    ptr=ADD_TO_PTR(ptr,ptr_diff,char*);
    if (null_ptr)
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
  }
216
  inline void get_image(char *buff,uint length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
217
    { memcpy(buff,ptr,length); }
218
  inline void set_image(char *buff,uint length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
219
    { memcpy(ptr,buff,length); }
220 221
  virtual void get_key_image(char *buff,uint length, CHARSET_INFO *cs,
			     imagetype type)
222 223 224
    { get_image(buff,length,cs); }
  virtual void set_key_image(char *buff,uint length, CHARSET_INFO *cs)
    { set_image(buff,length,cs); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
225 226 227 228 229 230 231
  inline longlong val_int_offset(uint row_offset)
    {
      ptr+=row_offset;
      longlong tmp=val_int();
      ptr-=row_offset;
      return tmp;
    }
232
  virtual bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
233 234
  virtual char *pack(char* to, const char *from, uint max_length=~(uint) 0)
  {
235
    uint32 length=pack_length();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
236 237 238 239 240 241 242 243 244
    memcpy(to,from,length);
    return to+length;
  }
  virtual const char *unpack(char* to, const char *from)
  {
    uint length=pack_length();
    memcpy(to,from,length);
    return from+length;
  }
245 246 247 248 249 250
  virtual char *pack_key(char* to, const char *from, uint max_length)
  {
    return pack(to,from,max_length);
  }
  virtual char *pack_key_from_key_image(char* to, const char *from,
					uint max_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
251 252 253
  {
    return pack(to,from,max_length);
  }
254 255 256 257
  virtual const char *unpack_key(char* to, const char *from, uint max_length)
  {
    return unpack(to,from);
  }
258 259
  virtual uint packed_col_length(const char *to, uint length)
  { return length;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
260
  virtual uint max_packed_col_length(uint max_length)
261
  { return max_length;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
262 263 264

  virtual int pack_cmp(const char *a,const char *b, uint key_length_arg)
  { return cmp(a,b); }
265 266
  virtual int pack_cmp(const char *b, uint key_length_arg)
  { return cmp(ptr,b); }
267
  uint offset();			// Should be inline ...
bk@work.mysql.com's avatar
bk@work.mysql.com committed
268 269
  void copy_from_tmp(int offset);
  uint fill_cache_field(struct st_cache_field *copy);
270
  virtual bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
271
  virtual bool get_time(TIME *ltime);
272
  virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
273
  virtual bool has_charset(void) const { return FALSE; }
274
  virtual void set_charset(CHARSET_INFO *charset) { }
275 276 277 278 279 280 281 282 283 284
  bool set_warning(const unsigned int level, const unsigned int code, 
                   int cuted_increment);
  void set_datetime_warning(const uint level, const uint code, 
                            const char *str, uint str_len,
                            timestamp_type ts_type, int cuted_increment);
  void set_datetime_warning(const uint level, const uint code, 
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment);
  void set_datetime_warning(const uint level, const uint code, 
                            double nr, timestamp_type ts_type);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
285 286
  virtual field_cast_enum field_cast_type()= 0;
  bool field_cast_compatible(field_cast_enum type);
287 288
  /* maximum possible display length */
  virtual uint32 max_length()= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302
  friend bool reopen_table(THD *,struct st_table *,bool);
  friend int cre_myisam(my_string name, register TABLE *form, uint options,
			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;
303
  friend class Item_func_group_concat;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
304 305 306 307 308 309
};


class Field_num :public Field {
public:
  const uint8 dec;
310
  bool zerofill,unsigned_flag;	// Purify cannot handle bit fields
bk@work.mysql.com's avatar
bk@work.mysql.com committed
311
  Field_num(char *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
312
	    uchar null_bit_arg, utype unireg_check_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
313 314
	    const char *field_name_arg,
	    struct st_table *table_arg,
315
	    uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329
    :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	   unireg_check_arg, field_name_arg, table_arg),
     dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
    {
      if (zerofill)
	flags|=ZEROFILL_FLAG;
      if (unsigned_flag)
	flags|=UNSIGNED_FLAG;
    }
  Item_result result_type () const { return REAL_RESULT; }
  void prepend_zeros(String *value);
  void add_zerofill_and_unsigned(String &res) const;
  friend class create_field;
  void make_field(Send_field *);
330
  uint decimals() const { return (uint) dec; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
331 332 333 334 335 336
  uint size_of() const { return sizeof(*this); }
  bool eq_def(Field *field);
};


class Field_str :public Field {
337 338
protected:
  CHARSET_INFO *field_charset;
serg@serg.mylan's avatar
serg@serg.mylan committed
339
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
340
  Field_str(char *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
341
	    uchar null_bit_arg, utype unireg_check_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
342
	    const char *field_name_arg,
343
	    struct st_table *table_arg,CHARSET_INFO *charset)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
344
    :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
monty@mysql.com's avatar
monty@mysql.com committed
345
	   unireg_check_arg, field_name_arg, table_arg)
serg@serg.mylan's avatar
serg@serg.mylan committed
346
    {
347
      field_charset=charset;
348
      if (charset->state & MY_CS_BINSORT)
349 350
        flags|=BINARY_FLAG;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
351 352
  Item_result result_type () const { return STRING_RESULT; }
  uint decimals() const { return NOT_FIXED_DEC; }
serg@serg.mylan's avatar
serg@serg.mylan committed
353
  int  store(double nr);
354
  int  store(longlong nr)=0;
serg@serg.mylan's avatar
serg@serg.mylan committed
355
  int  store(const char *to,uint length,CHARSET_INFO *cs)=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
356 357
  void make_field(Send_field *);
  uint size_of() const { return sizeof(*this); }
358 359
  CHARSET_INFO *charset(void) const { return field_charset; }
  void set_charset(CHARSET_INFO *charset) { field_charset=charset; }
360
  bool binary() const { return field_charset == &my_charset_bin; }
361
  uint32 max_length() { return field_length; }
362
  friend class create_field;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
363 364 365 366 367 368
};


class Field_decimal :public Field_num {
public:
  Field_decimal(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
369
		uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
370 371
		enum utype unireg_check_arg, const char *field_name_arg,
		struct st_table *table_arg,
372
		uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
373 374 375 376 377 378
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       dec_arg, zero_arg,unsigned_arg)
    {}
  enum_field_types type() const { return FIELD_TYPE_DECIMAL;}
  enum ha_base_keytype key_type() const
379
  { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
380
  void reset(void);
381 382 383
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
384 385 386 387 388 389 390 391
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  void overflow(bool negative);
  bool zero_pack() const { return 0; }
  void sql_type(String &str) const;
392
  uint32 max_length() { return field_length; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
393
  field_cast_enum field_cast_type() { return FIELD_CAST_DECIMAL; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
394 395 396 397 398 399
};


class Field_tiny :public Field_num {
public:
  Field_tiny(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
400
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
401 402 403 404 405 406 407 408 409 410 411
	     enum utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg,
	     bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       0, zero_arg,unsigned_arg)
    {}
  enum Item_result result_type () const { return INT_RESULT; }
  enum_field_types type() const { return FIELD_TYPE_TINY;}
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
412 413 414
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
415
  void reset(void) { ptr[0]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
416 417 418
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
419
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
420 421 422 423
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 1; }
  void sql_type(String &str) const;
424
  uint32 max_length() { return 4; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
425
  field_cast_enum field_cast_type() { return FIELD_CAST_TINY; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
426 427 428 429 430 431
};


class Field_short :public Field_num {
public:
  Field_short(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
432
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
433 434 435 436 437 438 439
	      enum utype unireg_check_arg, const char *field_name_arg,
	      struct st_table *table_arg,
	      bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       0, zero_arg,unsigned_arg)
    {}
440 441 442 443 444
  Field_short(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
	      struct st_table *table_arg,bool unsigned_arg)
    :Field_num((char*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
	       NONE, field_name_arg, table_arg,0,0,unsigned_arg)
    {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
445 446 447 448
  enum Item_result result_type () const { return INT_RESULT; }
  enum_field_types type() const { return FIELD_TYPE_SHORT;}
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
449 450 451
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
452
  void reset(void) { ptr[0]=ptr[1]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
453 454 455
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
456
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
457 458 459 460
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 2; }
  void sql_type(String &str) const;
461
  uint32 max_length() { return 6; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
462
  field_cast_enum field_cast_type() { return FIELD_CAST_SHORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
463 464 465 466 467 468
};


class Field_medium :public Field_num {
public:
  Field_medium(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
469
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
470 471 472 473 474 475 476 477 478 479 480
	      enum utype unireg_check_arg, const char *field_name_arg,
	      struct st_table *table_arg,
	      bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       0, zero_arg,unsigned_arg)
    {}
  enum Item_result result_type () const { return INT_RESULT; }
  enum_field_types type() const { return FIELD_TYPE_INT24;}
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
481 482 483
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int store(longlong nr);
484
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
485 486 487
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
488
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
489 490 491 492
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
493
  uint32 max_length() { return 8; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
494
  field_cast_enum field_cast_type() { return FIELD_CAST_MEDIUM; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
495 496 497 498 499 500
};


class Field_long :public Field_num {
public:
  Field_long(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
501
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	     enum utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg,
	     bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       0, zero_arg,unsigned_arg)
    {}
  Field_long(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
	     struct st_table *table_arg,bool unsigned_arg)
    :Field_num((char*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
	       NONE, field_name_arg, table_arg,0,0,unsigned_arg)
    {}
  enum Item_result result_type () const { return INT_RESULT; }
  enum_field_types type() const { return FIELD_TYPE_LONG;}
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
518 519 520
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
521
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
522 523
  double val_real(void);
  longlong val_int(void);
524
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
525 526 527 528 529
  String *val_str(String*,String *);
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
530
  uint32 max_length() { return 11; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
531
  field_cast_enum field_cast_type() { return FIELD_CAST_LONG; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
532 533 534 535 536 537 538
};


#ifdef HAVE_LONG_LONG
class Field_longlong :public Field_num {
public:
  Field_longlong(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
539
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
540 541 542 543 544 545 546
	      enum utype unireg_check_arg, const char *field_name_arg,
	      struct st_table *table_arg,
	      bool zero_arg, bool unsigned_arg)
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       0, zero_arg,unsigned_arg)
    {}
547 548 549
  Field_longlong(uint32 len_arg,bool maybe_null_arg,
		 const char *field_name_arg,
		 struct st_table *table_arg, bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
550
    :Field_num((char*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
551
	       NONE, field_name_arg, table_arg,0,0,unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
552 553 554 555 556
    {}
  enum Item_result result_type () const { return INT_RESULT; }
  enum_field_types type() const { return FIELD_TYPE_LONGLONG;}
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
557 558 559
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
560
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
561 562 563
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
564
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
565 566 567 568
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 8; }
  void sql_type(String &str) const;
569
  bool store_for_compare() { return 1; }
570
  uint32 max_length() { return 20; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
571
  field_cast_enum field_cast_type() { return FIELD_CAST_LONGLONG; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
572 573 574 575 576 577
};
#endif

class Field_float :public Field_num {
public:
  Field_float(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
578
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
579 580
	      enum utype unireg_check_arg, const char *field_name_arg,
	      struct st_table *table_arg,
581
	      uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
582 583 584 585
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       dec_arg, zero_arg,unsigned_arg)
    {}
586 587 588 589 590
  Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
	      struct st_table *table_arg, uint8 dec_arg)
    :Field_num((char*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
	       NONE, field_name_arg, table_arg,dec_arg,0,0)
    {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
591 592
  enum_field_types type() const { return FIELD_TYPE_FLOAT;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
593 594 595
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
596
  void reset(void) { bzero(ptr,sizeof(float)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
597 598 599
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
600
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
601 602 603 604
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return sizeof(float); }
  void sql_type(String &str) const;
605
  uint32 max_length() { return 24; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
606
  field_cast_enum field_cast_type() { return FIELD_CAST_FLOAT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
607 608 609 610 611 612
};


class Field_double :public Field_num {
public:
  Field_double(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
613
	       uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
614 615
	       enum utype unireg_check_arg, const char *field_name_arg,
	       struct st_table *table_arg,
616
	       uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
617 618 619 620 621
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	       unireg_check_arg, field_name_arg, table_arg,
	       dec_arg, zero_arg,unsigned_arg)
    {}
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
622
	       struct st_table *table_arg, uint8 dec_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
623 624 625 626 627
    :Field_num((char*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
	       NONE, field_name_arg, table_arg,dec_arg,0,0)
    {}
  enum_field_types type() const { return FIELD_TYPE_DOUBLE;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
628 629 630
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
631
  void reset(void) { bzero(ptr,sizeof(double)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
632 633 634
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
635
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
636 637 638 639
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return sizeof(double); }
  void sql_type(String &str) const;
640
  uint32 max_length() { return 53; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
641
  field_cast_enum field_cast_type() { return FIELD_CAST_DOUBLE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
642 643 644 645 646 647 648 649 650 651
};


/* Everything saved in this will disapper. It will always return NULL */

class Field_null :public Field_str {
  static uchar null[1];
public:
  Field_null(char *ptr_arg, uint32 len_arg,
	     enum utype unireg_check_arg, const char *field_name_arg,
652
	     struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
653
    :Field_str(ptr_arg, len_arg, null, 1,
654
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
655 656
    {}
  enum_field_types type() const { return FIELD_TYPE_NULL;}
657 658
  int  store(const char *to, uint length, CHARSET_INFO *cs)
  { null[0]=1; return 0; }
659 660
  int  store(double nr)   { null[0]=1; return 0; }
  int  store(longlong nr) { null[0]=1; return 0; }
661
  void reset(void)	  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
662 663 664 665 666 667 668
  double val_real(void)		{ return 0.0;}
  longlong val_int(void)	{ return 0;}
  String *val_str(String *value,String *value2)
  { value2->length(0); return value2;}
  int cmp(const char *a, const char *b) { return 0;}
  void sort_string(char *buff, uint length)  {}
  uint32 pack_length() const { return 0; }
669
  void sql_type(String &str) const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
670
  uint size_of() const { return sizeof(*this); }
671
  uint32 max_length() { return 4; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
672
  field_cast_enum field_cast_type() { return FIELD_CAST_NULL; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
673 674 675
};


676
class Field_timestamp :public Field_str {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
677 678 679
public:
  Field_timestamp(char *ptr_arg, uint32 len_arg,
		  enum utype unireg_check_arg, const char *field_name_arg,
680 681
		  struct st_table *table_arg,
		  CHARSET_INFO *cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
682 683
  enum_field_types type() const { return FIELD_TYPE_TIMESTAMP;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
684
  enum Item_result cmp_type () const { return INT_RESULT; }
685 686 687
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
688
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
689 690 691
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
692
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
693 694 695 696 697 698 699
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
  bool store_for_compare() { return 1; }
  bool zero_pack() const { return 0; }
  void set_time();
700 701
  virtual void set_default()
  {
702 703
    if (table->timestamp_field == this &&
        unireg_check != TIMESTAMP_UN_FIELD)
704 705 706
      set_time();
    else
      Field::set_default();
707
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
708 709 710 711 712 713 714 715 716 717
  inline long get_timestamp()
  {
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
      return sint4korr(ptr);
#endif
    long tmp;
    longget(tmp,ptr);
    return tmp;
  }
718
  bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
719
  bool get_time(TIME *ltime);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
720
  field_cast_enum field_cast_type() { return FIELD_CAST_TIMESTAMP; }
721
  void set_timestamp_offsets();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
722 723 724 725 726 727
};


class Field_year :public Field_tiny {
public:
  Field_year(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
728
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
729 730 731 732 733 734
	     enum utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg)
    :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
		unireg_check_arg, field_name_arg, table_arg, 1, 1)
    {}
  enum_field_types type() const { return FIELD_TYPE_YEAR;}
735 736 737
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
738 739 740
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
741
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
742
  void sql_type(String &str) const;
743
  bool store_for_compare() { return 1; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
744
  field_cast_enum field_cast_type() { return FIELD_CAST_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
745 746 747 748 749
};


class Field_date :public Field_str {
public:
750
  Field_date(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
751
	     enum utype unireg_check_arg, const char *field_name_arg,
752
	     struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
753
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
754
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
755
    {}
756
  Field_date(bool maybe_null_arg, const char *field_name_arg,
757
		 struct st_table *table_arg, CHARSET_INFO *cs)
758
    :Field_str((char*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
759
	       NONE, field_name_arg, table_arg, cs) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
760 761 762
  enum_field_types type() const { return FIELD_TYPE_DATE;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
  enum Item_result cmp_type () const { return INT_RESULT; }
763 764 765
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
766
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
767 768 769
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
770
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
771 772 773 774 775 776
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 4; }
  void sql_type(String &str) const;
  bool store_for_compare() { return 1; }
  bool zero_pack() const { return 1; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
777
  field_cast_enum field_cast_type() { return FIELD_CAST_DATE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
778 779 780 781
};

class Field_newdate :public Field_str {
public:
782
  Field_newdate(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
783
		enum utype unireg_check_arg, const char *field_name_arg,
784
		struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
785
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
786
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
787 788 789 790 791
    {}
  enum_field_types type() const { return FIELD_TYPE_DATE;}
  enum_field_types real_type() const { return FIELD_TYPE_NEWDATE; }
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
  enum Item_result cmp_type () const { return INT_RESULT; }
792 793 794
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
795
  void store_time(TIME *ltime,timestamp_type type);
796
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
797 798 799
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
800
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
801 802 803 804 805 806
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
  bool store_for_compare() { return 1; }
  bool zero_pack() const { return 1; }
807
  bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
808
  bool get_time(TIME *ltime);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
809
  field_cast_enum field_cast_type() { return FIELD_CAST_NEWDATE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
810 811 812 813 814
};


class Field_time :public Field_str {
public:
815
  Field_time(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
816
	     enum utype unireg_check_arg, const char *field_name_arg,
817
	     struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
818
    :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
819
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
820
    {}
821
  Field_time(bool maybe_null_arg, const char *field_name_arg,
822
		 struct st_table *table_arg, CHARSET_INFO *cs)
823
    :Field_str((char*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
824
	       NONE, field_name_arg, table_arg, cs) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
825 826 827
  enum_field_types type() const { return FIELD_TYPE_TIME;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
  enum Item_result cmp_type () const { return INT_RESULT; }
828 829 830
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
831
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
832 833 834
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
monty@mysql.com's avatar
monty@mysql.com committed
835
  bool get_date(TIME *ltime, uint fuzzydate);
836
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
837 838 839 840 841 842 843
  bool get_time(TIME *ltime);
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 3; }
  void sql_type(String &str) const;
  bool store_for_compare() { return 1; }
  bool zero_pack() const { return 1; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
844
  field_cast_enum field_cast_type() { return FIELD_CAST_TIME; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
845 846 847 848 849
};


class Field_datetime :public Field_str {
public:
850
  Field_datetime(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
851
		 enum utype unireg_check_arg, const char *field_name_arg,
852
		 struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
853
    :Field_str(ptr_arg, 19, null_ptr_arg, null_bit_arg,
854
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
855
    {}
856
  Field_datetime(bool maybe_null_arg, const char *field_name_arg,
857
		 struct st_table *table_arg, CHARSET_INFO *cs)
858
    :Field_str((char*) 0,19, maybe_null_arg ? (uchar*) "": 0,0,
859
	       NONE, field_name_arg, table_arg, cs) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
860 861 862 863 864
  enum_field_types type() const { return FIELD_TYPE_DATETIME;}
#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; }
865 866 867
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
868
  void store_time(TIME *ltime,timestamp_type type);
869
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
870 871 872
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
873
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
874 875 876 877 878 879
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  uint32 pack_length() const { return 8; }
  void sql_type(String &str) const;
  bool store_for_compare() { return 1; }
  bool zero_pack() const { return 1; }
880
  bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
881
  bool get_time(TIME *ltime);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
882
  field_cast_enum field_cast_type() { return FIELD_CAST_DATETIME; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
883 884 885 886 887 888
};


class Field_string :public Field_str {
public:
  Field_string(char *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
889
	       uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
890
	       enum utype unireg_check_arg, const char *field_name_arg,
891
	       struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
892
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
893
	       unireg_check_arg, field_name_arg, table_arg,cs) {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
894
  Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
895
	       struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
896
    :Field_str((char*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0,0,
897
	       NONE, field_name_arg, table_arg, cs) {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
898 899 900 901 902 903 904 905

  enum_field_types type() const
  {
    return ((table && table->db_create_options & HA_OPTION_PACK_RECORD &&
	     field_length >= 4) ?
	    FIELD_TYPE_VAR_STRING : FIELD_TYPE_STRING);
  }
  enum ha_base_keytype key_type() const
906
    { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
907
  bool zero_pack() const { return 0; }
908
  void reset(void) { charset()->cset->fill(charset(),ptr,field_length,' '); }
909 910
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(longlong nr);
911
  int store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
912 913 914 915 916 917 918
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  void sql_type(String &str) const;
  char *pack(char *to, const char *from, uint max_length=~(uint) 0);
919
  char *pack_key(char *to, const char *from, uint max_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
920 921
  const char *unpack(char* to, const char *from);
  int pack_cmp(const char *a,const char *b,uint key_length);
922
  int pack_cmp(const char *b,uint key_length);
923
  uint packed_col_length(const char *to, uint length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
924 925 926
  uint max_packed_col_length(uint max_length);
  uint size_of() const { return sizeof(*this); }
  enum_field_types real_type() const { return FIELD_TYPE_STRING; }
927 928
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
929
  field_cast_enum field_cast_type() { return FIELD_CAST_STRING; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
930 931 932 933 934 935
};


class Field_varstring :public Field_str {
public:
  Field_varstring(char *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
936
		  uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
937
		  enum utype unireg_check_arg, const char *field_name_arg,
938
		  struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
939
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
monty@mysql.com's avatar
monty@mysql.com committed
940 941
	       unireg_check_arg, field_name_arg, table_arg, cs)
  {}
monty@mysql.com's avatar
monty@mysql.com committed
942 943 944
  Field_varstring(uint32 len_arg,bool maybe_null_arg,
		  const char *field_name_arg,
		  struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
945
    :Field_str((char*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0,0,
monty@mysql.com's avatar
monty@mysql.com committed
946 947
	       NONE, field_name_arg, table_arg, cs)
  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
948 949 950

  enum_field_types type() const { return FIELD_TYPE_VAR_STRING; }
  enum ha_base_keytype key_type() const
951
    { return binary() ? HA_KEYTYPE_VARBINARY : HA_KEYTYPE_VARTEXT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
952 953 954 955
  bool zero_pack() const { return 0; }
  void reset(void) { bzero(ptr,field_length+2); }
  uint32 pack_length() const { return (uint32) field_length+2; }
  uint32 key_length() const { return (uint32) field_length; }
956 957
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(longlong nr);
958
  int  store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
959 960 961 962 963
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
964 965
  void get_key_image(char *buff,uint length, CHARSET_INFO *cs, imagetype type);
  void set_key_image(char *buff,uint length, CHARSET_INFO *cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
966 967
  void sql_type(String &str) const;
  char *pack(char *to, const char *from, uint max_length=~(uint) 0);
968
  char *pack_key(char *to, const char *from, uint max_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
969 970
  const char *unpack(char* to, const char *from);
  int pack_cmp(const char *a, const char *b, uint key_length);
971
  int pack_cmp(const char *b, uint key_length);
972
  uint packed_col_length(const char *to, uint length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
973 974 975
  uint max_packed_col_length(uint max_length);
  uint size_of() const { return sizeof(*this); }
  enum_field_types real_type() const { return FIELD_TYPE_VAR_STRING; }
976 977
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
978
  field_cast_enum field_cast_type() { return FIELD_CAST_VARSTRING; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
979 980 981 982
};


class Field_blob :public Field_str {
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
983
protected:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
984
  uint packlength;
985
  String value;				// For temporaries
bk@work.mysql.com's avatar
bk@work.mysql.com committed
986
public:
987
  Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
988 989
	     enum utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg,uint blob_pack_length,
990
	     CHARSET_INFO *cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
991
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
992
	     struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
993
    :Field_str((char*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0,0,
994
	       NONE, field_name_arg, table_arg, cs),
hf@deer.(none)'s avatar
hf@deer.(none) committed
995
    packlength(4)
monty@mysql.com's avatar
monty@mysql.com committed
996 997 998
  {
    flags|= BLOB_FLAG;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
999 1000
  enum_field_types type() const { return FIELD_TYPE_BLOB;}
  enum ha_base_keytype key_type() const
1001
    { return binary() ? HA_KEYTYPE_VARBINARY : HA_KEYTYPE_VARTEXT; }
1002 1003 1004
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1005 1006 1007 1008
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
  int cmp(const char *,const char*);
1009
  int cmp(const char *a, uint32 a_length, const char *b, uint32 b_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1010
  int cmp_offset(uint offset);
1011
  int cmp_binary(const char *a,const char *b, uint32 max_length=~0L);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1012 1013 1014 1015 1016
  int cmp_binary_offset(uint row_offset);
  int key_cmp(const byte *,const byte*);
  int key_cmp(const byte *str, uint length);
  uint32 key_length() const { return 0; }
  void sort_string(char *buff,uint length);
1017 1018
  uint32 pack_length() const
  { return (uint32) (packlength+table->blob_ptr_size); }
1019
  inline uint32 max_data_length() const
1020
  {
1021
    return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
1022
  }
1023
  void reset(void) { bzero(ptr, packlength+sizeof(char*)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1024
  void reset_fields() { bzero((char*) &value,sizeof(value)); }
1025 1026
  void store_length(uint32 number);
  inline uint32 get_length(uint row_offset=0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1027
  { return get_length(ptr+row_offset); }
1028
  uint32 get_length(const char *ptr);
1029
  void put_length(char *pos, uint32 length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1030 1031 1032 1033 1034 1035 1036 1037 1038
  inline void get_ptr(char **str)
    {
      memcpy_fixed(str,ptr+packlength,sizeof(char*));
    }
  inline void set_ptr(char *length,char *data)
    {
      memcpy(ptr,length,packlength);
      memcpy_fixed(ptr+packlength,&data,sizeof(char*));
    }
1039
  inline void set_ptr(uint32 length,char *data)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1040 1041 1042 1043
    {
      store_length(length);
      memcpy_fixed(ptr+packlength,&data,sizeof(char*));
    }
1044 1045
  void get_key_image(char *buff,uint length, CHARSET_INFO *cs, imagetype type);
  void set_key_image(char *buff,uint length, CHARSET_INFO *cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1046 1047 1048 1049
  void sql_type(String &str) const;
  inline bool copy()
  { char *tmp;
    get_ptr(&tmp);
1050
    if (value.copy(tmp,get_length(),charset()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1051 1052 1053 1054 1055 1056 1057
    {
      Field_blob::reset();
      return 1;
    }
    tmp=(char*) value.ptr(); memcpy_fixed(ptr+packlength,&tmp,sizeof(char*));
    return 0;
  }
1058 1059 1060 1061
  char *pack(char *to, const char *from, uint max_length= ~(uint) 0);
  const char *unpack(char *to, const char *from);
  char *pack_key(char *to, const char *from, uint max_length);
  char *pack_key_from_key_image(char* to, const char *from, uint max_length);
1062
  const char *unpack_key(char* to, const char *from, uint max_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1063
  int pack_cmp(const char *a, const char *b, uint key_length);
1064
  int pack_cmp(const char *b, uint key_length);
1065 1066
  uint packed_col_length(const char *col_ptr, uint length);
  uint max_packed_col_length(uint max_length);
1067
  void free() { value.free(); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1068 1069 1070
  inline void clear_temporary() { bzero((char*) &value,sizeof(value)); }
  friend void field_conv(Field *to,Field *from);
  uint size_of() const { return sizeof(*this); }
1071
  bool has_charset(void) const
1072
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1073
  field_cast_enum field_cast_type() { return FIELD_CAST_BLOB; }
1074
  uint32 max_length();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1075 1076
};

hf@deer.(none)'s avatar
hf@deer.(none) committed
1077
#ifdef HAVE_SPATIAL
1078 1079
class Field_geom :public Field_blob {
public:
1080
  enum geometry_type geom_type;
1081

1082 1083
  Field_geom(char *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
	     enum utype unireg_check_arg, const char *field_name_arg,
1084 1085
	     struct st_table *table_arg,uint blob_pack_length,
	     enum geometry_type geom_type_arg)
1086
     :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, 
1087 1088
                 field_name_arg, table_arg, blob_pack_length,&my_charset_bin)
  { geom_type= geom_type_arg; }
1089
  Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
1090
	     struct st_table *table_arg, enum geometry_type geom_type_arg)
1091
     :Field_blob(len_arg, maybe_null_arg, field_name_arg,
1092 1093
                 table_arg, &my_charset_bin)
  { geom_type= geom_type_arg; }
1094
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY; }
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
1095
  enum_field_types type() const { return FIELD_TYPE_GEOMETRY; }
1096
  void sql_type(String &str) const;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
1097 1098 1099
  int  store(const char *to, uint length, CHARSET_INFO *charset);
  int  store(double nr) { return 1; }
  int  store(longlong nr) { return 1; }
1100

1101 1102
  void get_key_image(char *buff,uint length, CHARSET_INFO *cs,imagetype type);
  void set_key_image(char *buff,uint length, CHARSET_INFO *cs);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1103
  field_cast_enum field_cast_type() { return FIELD_CAST_GEOM; }
1104
};
hf@deer.(none)'s avatar
hf@deer.(none) committed
1105
#endif /*HAVE_SPATIAL*/
1106

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1107 1108 1109 1110 1111 1112
class Field_enum :public Field_str {
protected:
  uint packlength;
public:
  TYPELIB *typelib;
  Field_enum(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1113
		 uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1114 1115
		 enum utype unireg_check_arg, const char *field_name_arg,
		 struct st_table *table_arg,uint packlength_arg,
1116 1117
		 TYPELIB *typelib_arg,
		 CHARSET_INFO *charset_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1118
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1119
	       unireg_check_arg, field_name_arg, table_arg, charset_arg),
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1120
    packlength(packlength_arg),typelib(typelib_arg)
1121
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1122
      flags|=ENUM_FLAG;
1123
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1124 1125 1126
  enum_field_types type() const { return FIELD_TYPE_STRING; }
  enum Item_result cmp_type () const { return INT_RESULT; }
  enum ha_base_keytype key_type() const;
1127 1128 1129
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
1130
  void reset() { bzero(ptr,packlength); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
  int cmp(const char *,const char*);
  void sort_string(char *buff,uint length);
  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); }
  enum_field_types real_type() const { return FIELD_TYPE_ENUM; }
  virtual bool zero_pack() const { return 0; }
1142
  bool optimize_range(uint idx, uint part) { return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1143
  bool eq_def(Field *field);
1144
  bool has_charset(void) const { return TRUE; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1145
  field_cast_enum field_cast_type() { return FIELD_CAST_ENUM; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1146 1147 1148 1149 1150 1151
};


class Field_set :public Field_enum {
public:
  Field_set(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1152
	    uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1153 1154
	    enum utype unireg_check_arg, const char *field_name_arg,
	    struct st_table *table_arg,uint32 packlength_arg,
1155
	    TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1156 1157 1158
    :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
		    unireg_check_arg, field_name_arg,
		    table_arg, packlength_arg,
1159
		    typelib_arg,charset_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1160 1161 1162
    {
      flags=(flags & ~ENUM_FLAG) | SET_FLAG;
    }
1163 1164 1165
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr) { return Field_set::store((longlong) nr); }
  int  store(longlong nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1166 1167 1168 1169
  virtual bool zero_pack() const { return 1; }
  String *val_str(String*,String *);
  void sql_type(String &str) const;
  enum_field_types real_type() const { return FIELD_TYPE_SET; }
1170
  bool has_charset(void) const { return TRUE; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1171
  field_cast_enum field_cast_type() { return FIELD_CAST_SET; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1172 1173 1174 1175
};


/*
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
1176
  Create field class for CREATE TABLE
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1177 1178 1179 1180 1181
*/

class create_field :public Sql_alloc {
public:
  const char *field_name;
1182 1183
  const char *change;			// If done with alter table
  const char *after;			// Put column after this one
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1184
  LEX_STRING comment;			// Comment for field
1185
  Item	*def;				// Default value
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1186 1187 1188 1189
  enum	enum_field_types sql_type;
  uint32 length;
  uint decimals,flags,pack_length;
  Field::utype unireg_check;
1190
  TYPELIB *interval;			// Which interval to use
1191
  CHARSET_INFO *charset;
1192
  Field::geometry_type geom_type;
1193
  Field *field;				// For alter table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1194

1195
  uint8 row,col,sc_length,interval_id;	// For rea_create_table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1196 1197
  uint	offset,pack_flag;
  create_field() :after(0) {}
1198
  create_field(Field *field, Field *orig_field);
1199
  void create_length_to_internal_length(void);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1200 1201 1202 1203
};


/*
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
1204
  A class for sending info to the client
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1205 1206 1207 1208
*/

class Send_field {
 public:
1209 1210 1211
  const char *db_name;
  const char *table_name,*org_table_name;
  const char *col_name,*org_col_name;
1212 1213
  ulong length;
  uint charsetnr, flags, decimals;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1214 1215 1216 1217 1218 1219
  enum_field_types type;
  Send_field() {}
};


/*
1220
  A class for quick copying data to fields
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
*/

class Copy_field :public Sql_alloc {
  void (*get_copy_func(Field *to,Field *from))(Copy_field *);
public:
  char *from_ptr,*to_ptr;
  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;
1232
  String tmp;					// For items
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1233 1234 1235

  Copy_field() {}
  ~Copy_field() {}
1236 1237
  void set(Field *to,Field *from,bool save);	// Field to field 
  void set(char *to,Field *from);		// Field to string
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1238
  void (*do_copy)(Copy_field *);
1239
  void (*do_copy2)(Copy_field *);		// Used to handle null values
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1240 1241 1242 1243
};


Field *make_field(char *ptr, uint32 field_length,
1244
		  uchar *null_pos, uchar null_bit,
1245
		  uint pack_flag, enum_field_types field_type,
1246
		  CHARSET_INFO *cs,
1247
		  Field::geometry_type geom_type,
1248
		  Field::utype unireg_check,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1249 1250 1251 1252
		  TYPELIB *interval, const char *field_name,
		  struct st_table *table);
uint pack_length_to_packflag(uint type);
uint32 calc_pack_length(enum_field_types type,uint32 length);
hf@deer.(none)'s avatar
hf@deer.(none) committed
1253 1254
int set_field_to_null(Field *field);
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
1255 1256
bool test_if_int(const char *str, int length, const char *int_end,
		 CHARSET_INFO *cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1257 1258

/*
1259
  The following are for the interface with the .frm file
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1260 1261 1262
*/

#define FIELDFLAG_DECIMAL		1
1263
#define FIELDFLAG_BINARY		1	// Shares same flag
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1264 1265
#define FIELDFLAG_NUMBER		2
#define FIELDFLAG_ZEROFILL		4
1266
#define FIELDFLAG_PACK			120	// Bits used for packing
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1267
#define FIELDFLAG_INTERVAL		256
1268 1269
#define FIELDFLAG_BITFIELD		512	// mangled with dec!
#define FIELDFLAG_BLOB			1024	// mangled with dec!
1270 1271
#define FIELDFLAG_GEOM			2048

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1272 1273
#define FIELDFLAG_LEFT_FULLSCREEN	8192
#define FIELDFLAG_RIGHT_FULLSCREEN	16384
1274 1275 1276
#define FIELDFLAG_FORMAT_NUMBER		16384	// predit: ###,,## in output
#define FIELDFLAG_SUM			((uint) 32768)// predit: +#fieldflag
#define FIELDFLAG_MAYBE_NULL		((uint) 32768)// sql
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
#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 FIELD_SORT_REVERSE		16384

#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)
1292
#define f_decimals(x)		((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1293
#define f_is_alpha(x)		(!f_is_num(x))
1294 1295
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
#define f_is_enum(x)            ((x) & FIELDFLAG_INTERVAL)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1296 1297
#define f_is_bitfield(x)	((x) & FIELDFLAG_BITFIELD)
#define f_is_blob(x)		(((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
1298
#define f_is_geom(x)		((x) & FIELDFLAG_GEOM)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1299 1300 1301
#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)