field.h 42.2 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown 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.
unknown's avatar
unknown committed
7

unknown's avatar
unknown 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.
unknown's avatar
unknown committed
12

unknown's avatar
unknown 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 */


/*
unknown's avatar
unknown committed
19 20
  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
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;
unknown's avatar
unknown committed
31
struct st_cache_field;
32
void field_conv(Field *to,Field *from);
unknown's avatar
unknown committed
33

34 35
class Field
{
unknown's avatar
unknown committed
36
  Field(const Item &);				/* Prevent use of these */
unknown's avatar
unknown committed
37 38
  void operator=(Field &);
public:
unknown's avatar
unknown committed
39
  static void *operator new(size_t size) {return (void*) sql_alloc((uint) size); }
unknown's avatar
unknown committed
40 41
  static void operator delete(void *ptr_arg, size_t size) {} /*lint -e715 */

42 43 44
  char		*ptr;			// Position to field in record
  uchar		*null_ptr;		// Byte where null_bit is
  struct st_table *table;		// Pointer for table
unknown's avatar
unknown committed
45
  const char	*table_name,*field_name;
46
  LEX_STRING	comment;
47
  ulong		query_id;		// For quick test of used fields
unknown's avatar
unknown committed
48
  /* Field is part of the following keys */
unknown's avatar
unknown committed
49 50 51 52
  key_map 	key_start,part_of_key,part_of_sortkey;
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
		CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
		BIT_FIELD, TIMESTAMP_FIELD,CAPITALIZE,BLOB_FIELD};
unknown's avatar
unknown committed
53 54 55

  enum imagetype { itRAW, itMBR};
  
unknown's avatar
unknown committed
56
  utype		unireg_check;
57
  uint32	field_length;		// Length of field
unknown's avatar
unknown committed
58
  uint16	flags;
59
  uchar		null_bit;		// Bit used to test null bit
unknown's avatar
unknown committed
60 61

  Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,uchar null_bit_arg,
unknown's avatar
unknown committed
62 63 64
	utype unireg_check_arg, const char *field_name_arg,
	struct st_table *table_arg);
  virtual ~Field() {}
65 66 67
  virtual int  store(const char *to,uint length,CHARSET_INFO *cs)=0;
  virtual int  store(double nr)=0;
  virtual int  store(longlong nr)=0;
unknown's avatar
unknown committed
68 69 70 71 72 73 74 75 76 77 78
  virtual void store_time(TIME *ltime,timestamp_type t_type);
  virtual double val_real(void)=0;
  virtual longlong val_int(void)=0;
  virtual String *val_str(String*,String *)=0;
  virtual Item_result result_type () const=0;
  virtual Item_result cmp_type () const { return result_type(); }
  bool eq(Field *field) { return ptr == field->ptr; }
  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() {}
unknown's avatar
unknown committed
79 80
  virtual void set_default()
  {
unknown's avatar
SCRUM  
unknown committed
81
    my_ptrdiff_t offset = table->default_values() - table->record[0];
unknown's avatar
SCRUM  
unknown committed
82
    memcpy(ptr, ptr + offset, pack_length());
83 84
    if (null_ptr)
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
unknown's avatar
SCRUM  
unknown committed
85
		  null_ptr[offset] & null_bit);
unknown's avatar
unknown committed
86
  }
unknown's avatar
unknown committed
87 88 89 90 91 92 93 94
  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;
unknown's avatar
unknown committed
95
  virtual int cmp_binary(const char *a,const char *b, uint32 max_length=~0L)
unknown's avatar
unknown committed
96 97 98 99 100 101 102 103 104 105
  { 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; }
unknown's avatar
unknown committed
106 107 108 109 110
  /*
    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
  */
111
  virtual void sql_type(String &str) const =0;
112
  virtual uint size_of() const =0;		// For new field
unknown's avatar
unknown committed
113 114 115 116 117 118 119
  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; }
  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)
unknown's avatar
unknown committed
120
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
unknown's avatar
unknown committed
121 122 123 124
  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;
125
  virtual bool optimize_range(uint idx);
unknown's avatar
unknown committed
126
  virtual bool store_for_compare() { return 0; }
unknown's avatar
unknown committed
127 128 129 130
  Field *new_field(MEM_ROOT *root, struct st_table *new_table)
  {
    Field *tmp= (Field*) memdup_root(root,(char*) this,size_of());
    if (tmp)
unknown's avatar
unknown committed
131
    {
132 133
      tmp->table= new_table;
      tmp->key_start= tmp->part_of_key= tmp->part_of_sortkey= 0;
unknown's avatar
unknown committed
134
      tmp->unireg_check=Field::NONE;
135
      tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
unknown's avatar
unknown committed
136
		    ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
137
#ifdef PROBABLY_WRONG
138
      tmp->table_name= new_table->table_name;
139
#endif
unknown's avatar
unknown committed
140
      tmp->reset_fields();
unknown's avatar
unknown committed
141
    }
unknown's avatar
unknown committed
142 143 144 145 146 147
    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;
  }
unknown's avatar
unknown committed
148 149 150 151 152 153 154
  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*);
  }
unknown's avatar
unknown committed
155
  inline void get_image(char *buff,uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
156
    { memcpy(buff,ptr,length); }
unknown's avatar
unknown committed
157
  inline void set_image(char *buff,uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
158
    { memcpy(ptr,buff,length); }
unknown's avatar
unknown committed
159 160 161 162
  virtual void get_key_image(char *buff,uint length, CHARSET_INFO *cs, imagetype type)
    { get_image(buff,length,cs); }
  virtual void set_key_image(char *buff,uint length, CHARSET_INFO *cs)
    { set_image(buff,length,cs); }
unknown's avatar
unknown committed
163
  inline int cmp_image(char *buff,uint length)
164
    { return memcmp(ptr,buff,length); }
unknown's avatar
unknown committed
165 166 167 168 169 170 171
  inline longlong val_int_offset(uint row_offset)
    {
      ptr+=row_offset;
      longlong tmp=val_int();
      ptr-=row_offset;
      return tmp;
    }
unknown's avatar
unknown committed
172
  virtual bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
173 174
  virtual char *pack(char* to, const char *from, uint max_length=~(uint) 0)
  {
unknown's avatar
unknown committed
175
    uint32 length=pack_length();
unknown's avatar
unknown committed
176 177 178 179 180 181 182 183 184
    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;
  }
185 186 187 188 189 190
  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)
unknown's avatar
unknown committed
191 192 193
  {
    return pack(to,from,max_length);
  }
194 195
  virtual uint packed_col_length(const char *to, uint length)
  { return length;}
unknown's avatar
unknown committed
196
  virtual uint max_packed_col_length(uint max_length)
197
  { return max_length;}
unknown's avatar
unknown committed
198 199 200

  virtual int pack_cmp(const char *a,const char *b, uint key_length_arg)
  { return cmp(a,b); }
201 202
  virtual int pack_cmp(const char *b, uint key_length_arg)
  { return cmp(ptr,b); }
203
  uint offset();			// Should be inline ...
unknown's avatar
unknown committed
204 205 206 207
  void copy_from_tmp(int offset);
  uint fill_cache_field(struct st_cache_field *copy);
  virtual bool get_date(TIME *ltime,bool fuzzydate);
  virtual bool get_time(TIME *ltime);
unknown's avatar
unknown committed
208
  virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
unknown's avatar
unknown committed
209
  virtual void set_charset(CHARSET_INFO *charset) { }
unknown's avatar
unknown committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  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;
};


class Field_num :public Field {
public:
  const uint8 dec;
230
  bool zerofill,unsigned_flag;	// Purify cannot handle bit fields
unknown's avatar
unknown committed
231
  Field_num(char *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
232
	    uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
233 234
	    const char *field_name_arg,
	    struct st_table *table_arg,
unknown's avatar
unknown committed
235
	    uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
    :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 *);
unknown's avatar
unknown committed
250
  uint decimals() const { return (uint) dec; }
unknown's avatar
unknown committed
251 252 253 254 255 256
  uint size_of() const { return sizeof(*this); }
  bool eq_def(Field *field);
};


class Field_str :public Field {
257 258
protected:
  CHARSET_INFO *field_charset;
unknown's avatar
unknown committed
259 260
public:
  Field_str(char *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
261
	    uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
262
	    const char *field_name_arg,
263
	    struct st_table *table_arg,CHARSET_INFO *charset)
unknown's avatar
unknown committed
264 265
    :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
	   unireg_check_arg, field_name_arg, table_arg)
unknown's avatar
unknown committed
266 267 268 269 270
    { 
      field_charset=charset;
      if (binary())
        flags|=BINARY_FLAG;
    }
unknown's avatar
unknown committed
271
  Item_result result_type () const { return STRING_RESULT; }
272
  void add_binary_or_charset(String &res) const;
unknown's avatar
unknown committed
273 274 275
  uint decimals() const { return NOT_FIXED_DEC; }
  void make_field(Send_field *);
  uint size_of() const { return sizeof(*this); }
unknown's avatar
unknown committed
276
  CHARSET_INFO *charset(void) const { return field_charset; }
unknown's avatar
unknown committed
277

unknown's avatar
unknown committed
278
  void set_charset(CHARSET_INFO *charset) { field_charset=charset; }
279
  bool binary() const { return field_charset->state & MY_CS_BINSORT ? 1 : 0; }
280
  inline int cmp_image(char *buff,uint length)
281 282 283 284 285
  {
    if (binary())
      return memcmp(ptr,buff,length);
    else
      return my_strncasecmp(field_charset,ptr,buff,length);
286
  }
unknown's avatar
unknown committed
287
  friend class create_field;
unknown's avatar
unknown committed
288 289 290 291 292 293
};


class Field_decimal :public Field_num {
public:
  Field_decimal(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
294
		uchar null_bit_arg,
unknown's avatar
unknown committed
295 296
		enum utype unireg_check_arg, const char *field_name_arg,
		struct st_table *table_arg,
unknown's avatar
unknown committed
297
		uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
298 299 300 301 302 303
    :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
304
  { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
unknown's avatar
unknown committed
305
  void reset(void);
306 307 308
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
unknown's avatar
unknown committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322
  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;
};


class Field_tiny :public Field_num {
public:
  Field_tiny(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
323
	     uchar null_bit_arg,
unknown's avatar
unknown committed
324 325 326 327 328 329 330 331 332 333 334
	     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; }
335 336 337
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
338
  void reset(void) { ptr[0]=0; }
unknown's avatar
unknown committed
339 340 341
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
342
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
343 344 345 346 347 348 349 350 351 352
  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;
};


class Field_short :public Field_num {
public:
  Field_short(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
353
	      uchar null_bit_arg,
unknown's avatar
unknown committed
354 355 356 357 358 359 360 361 362 363 364
	      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_SHORT;}
  enum ha_base_keytype key_type() const
    { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
365 366 367
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
368
  void reset(void) { ptr[0]=ptr[1]=0; }
unknown's avatar
unknown committed
369 370 371
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
372
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
373 374 375 376 377 378 379 380 381 382
  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;
};


class Field_medium :public Field_num {
public:
  Field_medium(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
383
	      uchar null_bit_arg,
unknown's avatar
unknown committed
384 385 386 387 388 389 390 391 392 393 394
	      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; }
395 396 397
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int store(longlong nr);
398
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
unknown's avatar
unknown committed
399 400 401
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
402
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
403 404 405 406 407 408 409 410 411 412
  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;
};


class Field_long :public Field_num {
public:
  Field_long(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
413
	     uchar null_bit_arg,
unknown's avatar
unknown committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	     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; }
430 431 432
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
433
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
unknown's avatar
unknown committed
434 435
  double val_real(void);
  longlong val_int(void);
436
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
437 438 439 440 441 442 443 444 445 446 447 448
  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;
};


#ifdef HAVE_LONG_LONG
class Field_longlong :public Field_num {
public:
  Field_longlong(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
449
	      uchar null_bit_arg,
unknown's avatar
unknown committed
450 451 452 453 454 455 456
	      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)
    {}
457 458 459
  Field_longlong(uint32 len_arg,bool maybe_null_arg,
		 const char *field_name_arg,
		 struct st_table *table_arg, bool unsigned_arg)
unknown's avatar
unknown committed
460
    :Field_num((char*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
461
	       NONE, field_name_arg, table_arg,0,0,unsigned_arg)
unknown's avatar
unknown committed
462 463 464 465 466
    {}
  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; }
467 468 469
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
470
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0; }
unknown's avatar
unknown committed
471 472 473
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
474
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
475 476 477 478
  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;
479
  bool store_for_compare() { return 1; }
unknown's avatar
unknown committed
480 481 482 483 484 485
};
#endif

class Field_float :public Field_num {
public:
  Field_float(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
486
	      uchar null_bit_arg,
unknown's avatar
unknown committed
487 488
	      enum utype unireg_check_arg, const char *field_name_arg,
	      struct st_table *table_arg,
unknown's avatar
unknown committed
489
	      uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
490 491 492 493 494 495
    :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_FLOAT;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
496 497 498
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
499
  void reset(void) { bzero(ptr,sizeof(float)); }
unknown's avatar
unknown committed
500 501 502
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
503
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
504 505 506 507 508 509 510 511 512 513
  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;
};


class Field_double :public Field_num {
public:
  Field_double(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
514
	       uchar null_bit_arg,
unknown's avatar
unknown committed
515 516
	       enum utype unireg_check_arg, const char *field_name_arg,
	       struct st_table *table_arg,
unknown's avatar
unknown committed
517
	       uint8 dec_arg,bool zero_arg,bool unsigned_arg)
unknown's avatar
unknown committed
518 519 520 521 522
    :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,
unknown's avatar
unknown committed
523
	       struct st_table *table_arg, uint8 dec_arg)
unknown's avatar
unknown committed
524 525 526 527 528
    :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; }
529 530 531
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
532
  void reset(void) { bzero(ptr,sizeof(double)); }
unknown's avatar
unknown committed
533 534 535
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
536
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550
  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;
};


/* 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,
551
	     struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
552
    :Field_str(ptr_arg, len_arg, null, 1,
553
	       unireg_check_arg, field_name_arg, table_arg, cs)
unknown's avatar
unknown committed
554 555
    {}
  enum_field_types type() const { return FIELD_TYPE_NULL;}
556 557 558
  int  store(const char *to, uint length, CHARSET_INFO *cs) { null[0]=1; return 0; }
  int  store(double nr)   { null[0]=1; return 0; }
  int  store(longlong nr) { null[0]=1; return 0; }
559
  void reset(void)	  {}
unknown's avatar
unknown committed
560 561 562 563 564 565 566
  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; }
567
  void sql_type(String &str) const;
unknown's avatar
unknown committed
568 569 570 571
  uint size_of() const { return sizeof(*this); }
};


572
class Field_timestamp :public Field_str {
unknown's avatar
unknown committed
573 574 575
public:
  Field_timestamp(char *ptr_arg, uint32 len_arg,
		  enum utype unireg_check_arg, const char *field_name_arg,
576 577
		  struct st_table *table_arg,
		  CHARSET_INFO *cs);
unknown's avatar
unknown committed
578 579
  enum_field_types type() const { return FIELD_TYPE_TIMESTAMP;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
580
  enum Item_result cmp_type () const { return INT_RESULT; }
581 582 583
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
584
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
unknown's avatar
unknown committed
585 586 587
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
588
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
589 590 591 592 593 594 595
  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();
unknown's avatar
unknown committed
596 597 598 599
  virtual void set_default()
  {
    set_time();
  }
unknown's avatar
unknown committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  inline long get_timestamp()
  {
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
      return sint4korr(ptr);
#endif
    long tmp;
    longget(tmp,ptr);
    return tmp;
  }
  bool get_date(TIME *ltime,bool fuzzydate);
  bool get_time(TIME *ltime);
};


class Field_year :public Field_tiny {
public:
  Field_year(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
618
	     uchar null_bit_arg,
unknown's avatar
unknown committed
619 620 621 622 623 624
	     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;}
625 626 627
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
unknown's avatar
unknown committed
628 629 630
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
631
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
632 633 634 635 636 637
  void sql_type(String &str) const;
};


class Field_date :public Field_str {
public:
unknown's avatar
unknown committed
638
  Field_date(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
639
	     enum utype unireg_check_arg, const char *field_name_arg,
640
	     struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
641
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
642
	       unireg_check_arg, field_name_arg, table_arg, cs)
unknown's avatar
unknown committed
643
    {}
644
  Field_date(bool maybe_null_arg, const char *field_name_arg,
645
		 struct st_table *table_arg, CHARSET_INFO *cs)
646
    :Field_str((char*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
647
	       NONE, field_name_arg, table_arg, cs) {}
unknown's avatar
unknown committed
648 649 650
  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; }
651 652 653
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
654
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
unknown's avatar
unknown committed
655 656 657
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
658
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
659 660 661 662 663 664 665 666 667 668
  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; }
};

class Field_newdate :public Field_str {
public:
unknown's avatar
unknown committed
669
  Field_newdate(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
670
		enum utype unireg_check_arg, const char *field_name_arg,
671
		struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
672
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
673
	       unireg_check_arg, field_name_arg, table_arg, cs)
unknown's avatar
unknown committed
674 675 676 677 678
    {}
  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; }
679 680 681
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
unknown's avatar
unknown committed
682
  void store_time(TIME *ltime,timestamp_type type);
683
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
unknown's avatar
unknown committed
684 685 686
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
687
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
688 689 690 691 692 693 694 695 696 697 698 699 700
  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; }
  bool get_date(TIME *ltime,bool fuzzydate);
  bool get_time(TIME *ltime);
};


class Field_time :public Field_str {
public:
unknown's avatar
unknown committed
701
  Field_time(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
702
	     enum utype unireg_check_arg, const char *field_name_arg,
703
	     struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
704
    :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
705
	       unireg_check_arg, field_name_arg, table_arg, cs)
unknown's avatar
unknown committed
706
    {}
707
  Field_time(bool maybe_null_arg, const char *field_name_arg,
708
		 struct st_table *table_arg, CHARSET_INFO *cs)
709
    :Field_str((char*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
710
	       NONE, field_name_arg, table_arg, cs) {}
unknown's avatar
unknown committed
711 712 713
  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; }
714 715 716
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
717
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
unknown's avatar
unknown committed
718 719 720
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
721
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
722 723 724 725 726 727 728 729 730 731 732 733
  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; }
};


class Field_datetime :public Field_str {
public:
unknown's avatar
unknown committed
734
  Field_datetime(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
735
		 enum utype unireg_check_arg, const char *field_name_arg,
736
		 struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
737
    :Field_str(ptr_arg, 19, null_ptr_arg, null_bit_arg,
738
	       unireg_check_arg, field_name_arg, table_arg, cs)
unknown's avatar
unknown committed
739
    {}
740
  Field_datetime(bool maybe_null_arg, const char *field_name_arg,
741
		 struct st_table *table_arg, CHARSET_INFO *cs)
742
    :Field_str((char*) 0,19, maybe_null_arg ? (uchar*) "": 0,0,
743
	       NONE, field_name_arg, table_arg, cs) {}
unknown's avatar
unknown committed
744 745 746 747 748
  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; }
749 750 751
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
unknown's avatar
unknown committed
752
  void store_time(TIME *ltime,timestamp_type type);
753
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0; }
unknown's avatar
unknown committed
754 755 756
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
757
  bool send_binary(Protocol *protocol);
unknown's avatar
unknown committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771
  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; }
  bool get_date(TIME *ltime,bool fuzzydate);
  bool get_time(TIME *ltime);
};


class Field_string :public Field_str {
public:
  Field_string(char *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
772
	       uchar null_bit_arg,
unknown's avatar
unknown committed
773
	       enum utype unireg_check_arg, const char *field_name_arg,
774
	       struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
775
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
776
	       unireg_check_arg, field_name_arg, table_arg,cs) {};
unknown's avatar
unknown committed
777
  Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
778
	       struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
779
    :Field_str((char*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0,0,
780
	       NONE, field_name_arg, table_arg, cs) {};
unknown's avatar
unknown committed
781 782 783 784 785 786 787 788

  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
789
    { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
unknown's avatar
unknown committed
790
  bool zero_pack() const { return 0; }
791
  void reset(void) { charset()->fill(charset(),ptr,field_length,' '); }
792 793 794
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
unknown's avatar
unknown committed
795 796 797 798 799 800 801 802 803
  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);
  const char *unpack(char* to, const char *from);
  int pack_cmp(const char *a,const char *b,uint key_length);
804
  int pack_cmp(const char *b,uint key_length);
805
  uint packed_col_length(const char *to, uint length);
unknown's avatar
unknown committed
806 807 808 809 810 811 812 813 814
  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; }
};


class Field_varstring :public Field_str {
public:
  Field_varstring(char *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
815
		  uchar null_bit_arg,
unknown's avatar
unknown committed
816
		  enum utype unireg_check_arg, const char *field_name_arg,
817
		  struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
818
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
819
	       unireg_check_arg, field_name_arg, table_arg, cs) {};
unknown's avatar
unknown committed
820
  Field_varstring(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
821
		  struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
822
    :Field_str((char*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0,0,
823
	       NONE, field_name_arg, table_arg, cs) {};
unknown's avatar
unknown committed
824 825 826

  enum_field_types type() const { return FIELD_TYPE_VAR_STRING; }
  enum ha_base_keytype key_type() const
827
    { return binary() ? HA_KEYTYPE_VARBINARY : HA_KEYTYPE_VARTEXT; }
unknown's avatar
unknown committed
828 829 830 831
  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; }
832 833 834
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
unknown's avatar
unknown committed
835 836 837 838 839
  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);
unknown's avatar
unknown committed
840 841
  void get_key_image(char *buff,uint length, CHARSET_INFO *cs, imagetype type);
  void set_key_image(char *buff,uint length, CHARSET_INFO *cs);
unknown's avatar
unknown committed
842 843 844 845
  void sql_type(String &str) const;
  char *pack(char *to, const char *from, uint max_length=~(uint) 0);
  const char *unpack(char* to, const char *from);
  int pack_cmp(const char *a, const char *b, uint key_length);
846
  int pack_cmp(const char *b, uint key_length);
847
  uint packed_col_length(const char *to, uint length);
unknown's avatar
unknown committed
848 849 850 851 852 853 854 855
  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; }
};


class Field_blob :public Field_str {
  uint packlength;
856
  String value;				// For temporaries
unknown's avatar
unknown committed
857
  bool geom_flag;
unknown's avatar
unknown committed
858
public:
unknown's avatar
unknown committed
859
  Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
860 861
	     enum utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg,uint blob_pack_length,
862
	     CHARSET_INFO *cs);
unknown's avatar
unknown committed
863
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
864
	     struct st_table *table_arg, CHARSET_INFO *cs)
unknown's avatar
unknown committed
865
    :Field_str((char*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0,0,
866
	       NONE, field_name_arg, table_arg, cs),
867
    packlength(3), geom_flag(true)
unknown's avatar
unknown committed
868 869 870 871 872
    {
      flags|= BLOB_FLAG;
    }
  enum_field_types type() const { return FIELD_TYPE_BLOB;}
  enum ha_base_keytype key_type() const
873
    { return binary() ? HA_KEYTYPE_VARBINARY : HA_KEYTYPE_VARTEXT; }
874 875 876
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
unknown's avatar
unknown committed
877 878 879 880
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
  int cmp(const char *,const char*);
unknown's avatar
unknown committed
881
  int cmp(const char *a, uint32 a_length, const char *b, uint32 b_length);
unknown's avatar
unknown committed
882
  int cmp_offset(uint offset);
unknown's avatar
unknown committed
883
  int cmp_binary(const char *a,const char *b, uint32 max_length=~0L);
unknown's avatar
unknown committed
884 885 886 887 888
  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);
unknown's avatar
unknown committed
889 890 891
  uint32 pack_length() const
  { return (uint32) (packlength+table->blob_ptr_size); }
  void reset(void) { bzero(ptr, packlength+sizeof(char*)); }
unknown's avatar
unknown committed
892
  void reset_fields() { bzero((char*) &value,sizeof(value)); }
unknown's avatar
unknown committed
893 894
  void store_length(uint32 number);
  inline uint32 get_length(uint row_offset=0)
unknown's avatar
unknown committed
895
  { return get_length(ptr+row_offset); }
unknown's avatar
unknown committed
896
  uint32 get_length(const char *ptr);
unknown's avatar
unknown committed
897 898 899 900 901 902 903 904 905
  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*));
    }
unknown's avatar
unknown committed
906
  inline void set_ptr(uint32 length,char *data)
unknown's avatar
unknown committed
907 908 909 910
    {
      store_length(length);
      memcpy_fixed(ptr+packlength,&data,sizeof(char*));
    }
unknown's avatar
unknown committed
911 912
  void get_key_image(char *buff,uint length, CHARSET_INFO *cs, imagetype type);
  void set_key_image(char *buff,uint length, CHARSET_INFO *cs);
unknown's avatar
unknown committed
913 914 915 916
  void sql_type(String &str) const;
  inline bool copy()
  { char *tmp;
    get_ptr(&tmp);
917
    if (value.copy(tmp,get_length(),charset()))
unknown's avatar
unknown committed
918 919 920 921 922 923 924
    {
      Field_blob::reset();
      return 1;
    }
    tmp=(char*) value.ptr(); memcpy_fixed(ptr+packlength,&tmp,sizeof(char*));
    return 0;
  }
925 926 927 928
  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);
unknown's avatar
unknown committed
929
  int pack_cmp(const char *a, const char *b, uint key_length);
930
  int pack_cmp(const char *b, uint key_length);
931 932
  uint packed_col_length(const char *col_ptr, uint length);
  uint max_packed_col_length(uint max_length);
unknown's avatar
unknown committed
933 934 935 936 937 938 939
  inline void free() { value.free(); }
  inline void clear_temporary() { bzero((char*) &value,sizeof(value)); }
  friend void field_conv(Field *to,Field *from);
  uint size_of() const { return sizeof(*this); }
};


unknown's avatar
unknown committed
940 941 942 943
class Field_geom :public Field_blob {
public:
  Field_geom(char *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
	     enum utype unireg_check_arg, const char *field_name_arg,
944
	     struct st_table *table_arg,uint blob_pack_length)
unknown's avatar
unknown committed
945
     :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, 
unknown's avatar
unknown committed
946
                 field_name_arg, table_arg, blob_pack_length,&my_charset_bin) {}
unknown's avatar
unknown committed
947
  Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
948
	     struct st_table *table_arg)
unknown's avatar
unknown committed
949
     :Field_blob(len_arg, maybe_null_arg, field_name_arg,
unknown's avatar
unknown committed
950
                 table_arg, &my_charset_bin) {}
unknown's avatar
unknown committed
951
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY; }
unknown's avatar
unknown committed
952 953
  enum_field_types type() const { return FIELD_TYPE_GEOMETRY;}
  void sql_type(String &str) const;
unknown's avatar
unknown committed
954

unknown's avatar
unknown committed
955 956
  void get_key_image(char *buff,uint length, CHARSET_INFO *cs,imagetype type);
  void set_key_image(char *buff,uint length, CHARSET_INFO *cs);
unknown's avatar
unknown committed
957 958 959
};


unknown's avatar
unknown committed
960 961 962 963 964 965
class Field_enum :public Field_str {
protected:
  uint packlength;
public:
  TYPELIB *typelib;
  Field_enum(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
966
		 uchar null_bit_arg,
unknown's avatar
unknown committed
967 968
		 enum utype unireg_check_arg, const char *field_name_arg,
		 struct st_table *table_arg,uint packlength_arg,
969 970
		 TYPELIB *typelib_arg,
		 CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
971
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
972
	       unireg_check_arg, field_name_arg, table_arg, charset_arg),
unknown's avatar
unknown committed
973
    packlength(packlength_arg),typelib(typelib_arg)
unknown's avatar
unknown committed
974
  {
unknown's avatar
unknown committed
975
      flags|=ENUM_FLAG;
unknown's avatar
unknown committed
976
  }
unknown's avatar
unknown committed
977 978 979
  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;
980 981 982
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
983
  void reset() { bzero(ptr,packlength); }
unknown's avatar
unknown committed
984 985 986 987 988 989 990 991 992 993 994
  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; }
995
  bool optimize_range(uint idx) { return 0; }
unknown's avatar
unknown committed
996 997 998 999 1000 1001 1002
  bool eq_def(Field *field);
};


class Field_set :public Field_enum {
public:
  Field_set(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1003
	    uchar null_bit_arg,
unknown's avatar
unknown committed
1004 1005
	    enum utype unireg_check_arg, const char *field_name_arg,
	    struct st_table *table_arg,uint32 packlength_arg,
1006
	    TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
1007 1008 1009
    :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
		    unireg_check_arg, field_name_arg,
		    table_arg, packlength_arg,
1010
		    typelib_arg,charset_arg)
unknown's avatar
unknown committed
1011 1012 1013
    {
      flags=(flags & ~ENUM_FLAG) | SET_FLAG;
    }
1014 1015 1016
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr) { return Field_set::store((longlong) nr); }
  int  store(longlong nr);
unknown's avatar
unknown committed
1017 1018 1019 1020 1021 1022 1023 1024
  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; }
};


/*
unknown's avatar
unknown committed
1025
  Create field class for CREATE TABLE
unknown's avatar
unknown committed
1026 1027 1028 1029 1030
*/

class create_field :public Sql_alloc {
public:
  const char *field_name;
1031 1032
  const char *change;			// If done with alter table
  const char *after;			// Put column after this one
unknown's avatar
unknown committed
1033
  LEX_STRING comment;			// Comment for field
1034
  Item	*def;				// Default value
unknown's avatar
unknown committed
1035 1036 1037 1038
  enum	enum_field_types sql_type;
  uint32 length;
  uint decimals,flags,pack_length;
  Field::utype unireg_check;
1039
  TYPELIB *interval;			// Which interval to use
1040
  CHARSET_INFO *charset;
1041
  Field *field;				// For alter table
unknown's avatar
unknown committed
1042

1043
  uint8 row,col,sc_length,interval_id;	// For rea_create_table
unknown's avatar
unknown committed
1044 1045
  uint	offset,pack_flag;
  create_field() :after(0) {}
1046
  create_field(Field *field, Field *orig_field);
unknown's avatar
unknown committed
1047 1048 1049 1050
};


/*
unknown's avatar
unknown committed
1051
  A class for sending info to the client
unknown's avatar
unknown committed
1052 1053 1054 1055
*/

class Send_field {
 public:
unknown's avatar
unknown committed
1056 1057 1058
  const char *db_name;
  const char *table_name,*org_table_name;
  const char *col_name,*org_col_name;
1059 1060
  ulong length;
  uint charsetnr, flags, decimals;
unknown's avatar
unknown committed
1061 1062 1063 1064 1065 1066
  enum_field_types type;
  Send_field() {}
};


/*
1067
  A class for quick copying data to fields
unknown's avatar
unknown committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
*/

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;
1079
  String tmp;					// For items
unknown's avatar
unknown committed
1080 1081 1082

  Copy_field() {}
  ~Copy_field() {}
1083 1084
  void set(Field *to,Field *from,bool save);	// Field to field 
  void set(char *to,Field *from);		// Field to string
unknown's avatar
unknown committed
1085
  void (*do_copy)(Copy_field *);
1086
  void (*do_copy2)(Copy_field *);		// Used to handle null values
unknown's avatar
unknown committed
1087 1088 1089 1090
};


Field *make_field(char *ptr, uint32 field_length,
unknown's avatar
unknown committed
1091
		  uchar *null_pos, uchar null_bit,
1092
		  uint pack_flag, enum_field_types field_type,
1093
		  CHARSET_INFO *cs,
1094
		  Field::utype unireg_check,
unknown's avatar
unknown committed
1095 1096 1097 1098 1099
		  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);
bool set_field_to_null(Field *field);
1100
bool set_field_to_null_with_conversions(Field *field, bool no_conversions);
unknown's avatar
unknown committed
1101
uint find_enum(TYPELIB *typelib,const char *x, uint length);
1102 1103
ulonglong find_set(TYPELIB *typelib,const char *x, uint length,
		   char **err_pos, uint *err_len);
1104 1105
bool test_if_int(const char *str, int length, const char *int_end,
		 CHARSET_INFO *cs);
unknown's avatar
unknown committed
1106 1107

/*
1108
  The following are for the interface with the .frm file
unknown's avatar
unknown committed
1109 1110 1111
*/

#define FIELDFLAG_DECIMAL		1
1112
#define FIELDFLAG_BINARY		1	// Shares same flag 
unknown's avatar
unknown committed
1113 1114
#define FIELDFLAG_NUMBER		2
#define FIELDFLAG_ZEROFILL		4
1115
#define FIELDFLAG_PACK			120	// Bits used for packing
unknown's avatar
unknown committed
1116
#define FIELDFLAG_INTERVAL		256
1117 1118
#define FIELDFLAG_BITFIELD		512	// mangled with dec!
#define FIELDFLAG_BLOB			1024	// mangled with dec!
unknown's avatar
unknown committed
1119 1120
#define FIELDFLAG_GEOM			2048

unknown's avatar
unknown committed
1121 1122
#define FIELDFLAG_LEFT_FULLSCREEN	8192
#define FIELDFLAG_RIGHT_FULLSCREEN	16384
1123 1124 1125
#define FIELDFLAG_FORMAT_NUMBER		16384	// predit: ###,,## in output
#define FIELDFLAG_SUM			((uint) 32768)// predit: +#fieldflag
#define FIELDFLAG_MAYBE_NULL		((uint) 32768)// sql
unknown's avatar
unknown committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
#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)
unknown's avatar
unknown committed
1141
#define f_decimals(x)		((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
unknown's avatar
unknown committed
1142 1143 1144 1145
#define f_is_alpha(x)		(!f_is_num(x))
#define f_is_enum(x)	((x) & FIELDFLAG_INTERVAL)
#define f_is_bitfield(x)	((x) & FIELDFLAG_BITFIELD)
#define f_is_blob(x)		(((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
unknown's avatar
unknown committed
1146
#define f_is_geom(x)		((x) & FIELDFLAG_GEOM)
unknown's avatar
unknown committed
1147 1148 1149
#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)