field.h 49.2 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
#ifdef USE_PRAGMA_INTERFACE
bk@work.mysql.com's avatar
bk@work.mysql.com committed
24 25 26 27
#pragma interface			/* gcc class implementation */
#endif

#define NOT_FIXED_DEC			31
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
28
#define DATETIME_DEC                     6
bk@work.mysql.com's avatar
bk@work.mysql.com committed
29 30

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

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

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

46 47
class Field
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
48
  Field(const Item &);				/* Prevent use of these */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
49 50
  void operator=(Field &);
public:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
51
  static void *operator new(size_t size) {return (void*) sql_alloc((uint) size); }
52
  static void operator delete(void *ptr_arg, size_t size) {
53
#ifdef SAFEMALLOC
54 55 56
    bfill(ptr_arg, size, 0x8F);
#endif
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
57

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

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

  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
97 98 99
	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
100
  /* Store functions returns 1 on overflow and -1 on fatal error */
101 102 103
  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
104 105 106
  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
107 108 109
  inline String *val_str(String *str) { return val_str(str, str); }
  /*
     val_str(buf1, buf2) gets two buffers and should use them as follows:
110 111 112 113 114 115 116 117 118 119
     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
120 121 122
  virtual String *val_str(String*,String *)=0;
  virtual Item_result result_type () const=0;
  virtual Item_result cmp_type () const { return result_type(); }
123
  virtual Item_result cast_to_int_type () const { return result_type(); }
124 125
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
  static Item_result result_merge_type(enum_field_types);
126
  bool eq(Field *field) { return ptr == field->ptr && null_ptr == field->null_ptr; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
127 128 129 130
  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() {}
131 132
  virtual void set_default()
  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
133 134
    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
135
    memcpy(ptr, ptr + offset, pack_length());
136 137
    if (null_ptr)
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
138
		  null_ptr[offset] & null_bit);
139
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
140 141 142 143 144 145 146 147
  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;
148
  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
149 150 151 152 153 154 155 156 157 158
  { 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
159 160 161 162 163
  /*
    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
  */
164
  virtual void sql_type(String &str) const =0;
165
  virtual uint size_of() const =0;		// For new field
bk@work.mysql.com's avatar
bk@work.mysql.com committed
166 167 168 169
  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
170 171 172 173 174 175 176
  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
177 178 179
  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)
180
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
181 182 183 184
  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;
185
  virtual bool optimize_range(uint idx, uint part);
186 187 188 189 190 191 192 193
  /*
    This should be true for fields which, when compared with constant
    items, can be casted to longlong. In this case we will at 'fix_fields'
    stage cast the constant items to longlongs and at the execution stage
    use field->val_int() for comparison.  Used to optimize clauses like
    'a_column BETWEEN date_const, date_const'.
  */
  virtual bool can_be_compared_as_longlong() const { return FALSE; }
194
  virtual void free() {}
195 196 197 198
  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
199
    {
200 201
      if (tmp->table->maybe_null)
	tmp->flags&= ~NOT_NULL_FLAG;
202
      tmp->table= new_table;
serg@serg.mylan's avatar
serg@serg.mylan committed
203 204 205
      tmp->key_start.init(0);
      tmp->part_of_key.init(0);
      tmp->part_of_sortkey.init(0);
206
      tmp->unireg_check=Field::NONE;
207
      tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
208
		    ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
209
#ifdef PROBABLY_WRONG
210
      tmp->table_name= new_table->table_name;
211
#endif
212
      tmp->reset_fields();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
213
    }
214 215 216 217 218 219
    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
220 221 222 223 224 225 226
  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*);
  }
227
  inline void get_image(char *buff,uint length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
228
    { memcpy(buff,ptr,length); }
229
  inline void set_image(char *buff,uint length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
230
    { memcpy(ptr,buff,length); }
231 232
  virtual void get_key_image(char *buff,uint length, CHARSET_INFO *cs,
			     imagetype type)
233 234 235
    { 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
236 237 238 239 240 241 242
  inline longlong val_int_offset(uint row_offset)
    {
      ptr+=row_offset;
      longlong tmp=val_int();
      ptr-=row_offset;
      return tmp;
    }
243
  virtual bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
244 245
  virtual char *pack(char* to, const char *from, uint max_length=~(uint) 0)
  {
246
    uint32 length=pack_length();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
247 248 249 250 251 252 253 254 255
    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;
  }
256 257 258 259 260 261
  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
262 263 264
  {
    return pack(to,from,max_length);
  }
265 266 267 268
  virtual const char *unpack_key(char* to, const char *from, uint max_length)
  {
    return unpack(to,from);
  }
269 270
  virtual uint packed_col_length(const char *to, uint length)
  { return length;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
271
  virtual uint max_packed_col_length(uint max_length)
272
  { return max_length;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
273 274 275

  virtual int pack_cmp(const char *a,const char *b, uint key_length_arg)
  { return cmp(a,b); }
276 277
  virtual int pack_cmp(const char *b, uint key_length_arg)
  { return cmp(ptr,b); }
278
  uint offset();			// Should be inline ...
bk@work.mysql.com's avatar
bk@work.mysql.com committed
279 280
  void copy_from_tmp(int offset);
  uint fill_cache_field(struct st_cache_field *copy);
281
  virtual bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
282
  virtual bool get_time(TIME *ltime);
283
  virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
284
  virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
285
  virtual bool has_charset(void) const { return FALSE; }
286
  virtual void set_charset(CHARSET_INFO *charset) { }
287 288 289 290 291 292 293 294 295 296
  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);
297 298
  /* maximum possible display length */
  virtual uint32 max_length()= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312
  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;
313
  friend class Item_func_group_concat;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
314 315 316 317 318 319
};


class Field_num :public Field {
public:
  const uint8 dec;
320
  bool zerofill,unsigned_flag;	// Purify cannot handle bit fields
bk@work.mysql.com's avatar
bk@work.mysql.com committed
321
  Field_num(char *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
322
	    uchar null_bit_arg, utype unireg_check_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
323 324
	    const char *field_name_arg,
	    struct st_table *table_arg,
325
	    uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339
    :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 *);
340
  uint decimals() const { return (uint) dec; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
341 342 343 344 345 346
  uint size_of() const { return sizeof(*this); }
  bool eq_def(Field *field);
};


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


class Field_decimal :public Field_num {
public:
  Field_decimal(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
379
		uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
380 381
		enum utype unireg_check_arg, const char *field_name_arg,
		struct st_table *table_arg,
382
		uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
383 384 385 386 387 388
    :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
389
  { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
390
  void reset(void);
391 392 393
  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
394 395 396 397 398 399 400 401
  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;
402
  uint32 max_length() { return field_length; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
403 404 405 406 407 408
};


class Field_tiny :public Field_num {
public:
  Field_tiny(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
409
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
410 411 412 413 414 415 416 417 418 419 420
	     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; }
421 422 423
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
424
  void reset(void) { ptr[0]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
425 426 427
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
428
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
429 430 431 432
  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;
433
  uint32 max_length() { return 4; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
434 435 436 437 438 439
};


class Field_short :public Field_num {
public:
  Field_short(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
440
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
441 442 443 444 445 446 447
	      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)
    {}
448 449 450 451 452
  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
453 454 455 456
  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;}
457 458 459
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
460
  void reset(void) { ptr[0]=ptr[1]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
461 462 463
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
464
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
465 466 467 468
  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;
469
  uint32 max_length() { return 6; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
470 471 472 473 474 475
};


class Field_medium :public Field_num {
public:
  Field_medium(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
476
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
477 478 479 480 481 482 483 484 485 486 487
	      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; }
488 489 490
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int store(longlong nr);
491
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
492 493 494
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
495
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
496 497 498 499
  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;
500
  uint32 max_length() { return 8; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
501 502 503 504 505 506
};


class Field_long :public Field_num {
public:
  Field_long(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
507
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	     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; }
524 525 526
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
527
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
528 529
  double val_real(void);
  longlong val_int(void);
530
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
531 532 533 534 535
  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;
536
  uint32 max_length() { return 11; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
537 538 539 540 541 542 543
};


#ifdef HAVE_LONG_LONG
class Field_longlong :public Field_num {
public:
  Field_longlong(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
544
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
545 546 547 548 549 550 551
	      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)
    {}
552 553 554
  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
555
    :Field_num((char*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
556
	       NONE, field_name_arg, table_arg,0,0,unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
557 558 559 560 561
    {}
  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; }
562 563 564
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
565
  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
566 567 568
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
569
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
570 571 572 573
  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;
574
  bool can_be_compared_as_longlong() const { return TRUE; }
575
  uint32 max_length() { return 20; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
576 577 578 579 580 581
};
#endif

class Field_float :public Field_num {
public:
  Field_float(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
582
	      uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
583 584
	      enum utype unireg_check_arg, const char *field_name_arg,
	      struct st_table *table_arg,
585
	      uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
586 587 588 589
    :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)
    {}
590 591 592 593 594
  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
595 596
  enum_field_types type() const { return FIELD_TYPE_FLOAT;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
597 598 599
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
600
  void reset(void) { bzero(ptr,sizeof(float)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
601 602 603
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
604
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
605 606 607 608
  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;
609
  uint32 max_length() { return 24; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
610 611 612 613 614 615
};


class Field_double :public Field_num {
public:
  Field_double(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
616
	       uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
617 618
	       enum utype unireg_check_arg, const char *field_name_arg,
	       struct st_table *table_arg,
619
	       uint8 dec_arg,bool zero_arg,bool unsigned_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
620 621 622 623 624
    :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,
625
	       struct st_table *table_arg, uint8 dec_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
626 627 628 629 630
    :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; }
631 632 633
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
634
  void reset(void) { bzero(ptr,sizeof(double)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
635 636 637
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
638
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
639 640 641 642
  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;
643
  uint32 max_length() { return 53; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
644 645 646 647 648 649 650 651 652 653
};


/* 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,
654
	     struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
655
    :Field_str(ptr_arg, len_arg, null, 1,
656
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
657 658
    {}
  enum_field_types type() const { return FIELD_TYPE_NULL;}
659 660
  int  store(const char *to, uint length, CHARSET_INFO *cs)
  { null[0]=1; return 0; }
661 662
  int  store(double nr)   { null[0]=1; return 0; }
  int  store(longlong nr) { null[0]=1; return 0; }
663
  void reset(void)	  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
664 665 666 667 668 669 670
  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; }
671
  void sql_type(String &str) const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
672
  uint size_of() const { return sizeof(*this); }
673
  uint32 max_length() { return 4; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
674 675 676
};


677
class Field_timestamp :public Field_str {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
678 679
public:
  Field_timestamp(char *ptr_arg, uint32 len_arg,
680
                  uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
681
		  enum utype unireg_check_arg, const char *field_name_arg,
682 683
		  struct st_table *table_arg,
		  CHARSET_INFO *cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
684 685
  enum_field_types type() const { return FIELD_TYPE_TIMESTAMP;}
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
686
  enum Item_result cmp_type () const { return INT_RESULT; }
687 688 689
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
690
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
691 692 693
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
694
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
695 696 697 698
  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;
699
  bool can_be_compared_as_longlong() const { return TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
700 701
  bool zero_pack() const { return 0; }
  void set_time();
702 703
  virtual void set_default()
  {
704 705
    if (table->timestamp_field == this &&
        unireg_check != TIMESTAMP_UN_FIELD)
706 707 708
      set_time();
    else
      Field::set_default();
709
  }
710 711
  /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
  inline long get_timestamp(my_bool *null_value)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
712
  {
713 714
    if ((*null_value= is_null()))
      return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
715
#ifdef WORDS_BIGENDIAN
716
    if (table && table->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
717 718 719 720 721 722
      return sint4korr(ptr);
#endif
    long tmp;
    longget(tmp,ptr);
    return tmp;
  }
723
  bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
724
  bool get_time(TIME *ltime);
725
  timestamp_auto_set_type get_auto_set_type() const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
726 727 728 729 730 731
};


class Field_year :public Field_tiny {
public:
  Field_year(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
732
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
733 734 735 736 737 738
	     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;}
739 740 741
  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
742 743 744
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
745
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
746
  void sql_type(String &str) const;
747
  bool can_be_compared_as_longlong() const { return TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
748 749 750 751 752
};


class Field_date :public Field_str {
public:
753
  Field_date(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
754
	     enum utype unireg_check_arg, const char *field_name_arg,
755
	     struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
756
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
757
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
758
    {}
759
  Field_date(bool maybe_null_arg, const char *field_name_arg,
760
		 struct st_table *table_arg, CHARSET_INFO *cs)
761
    :Field_str((char*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
762
	       NONE, field_name_arg, table_arg, cs) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
763 764 765
  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; }
766 767 768
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
769
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
770 771 772
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
773
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
774 775 776 777
  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;
778
  bool can_be_compared_as_longlong() const { return TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
779 780 781 782 783
  bool zero_pack() const { return 1; }
};

class Field_newdate :public Field_str {
public:
784
  Field_newdate(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
785
		enum utype unireg_check_arg, const char *field_name_arg,
786
		struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
787
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
788
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
789 790 791 792 793
    {}
  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; }
794 795 796
  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
797
  void store_time(TIME *ltime,timestamp_type type);
798
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
799 800 801
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
802
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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;
807
  bool can_be_compared_as_longlong() const { return TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
808
  bool zero_pack() const { return 1; }
809
  bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
810 811 812 813 814 815
  bool get_time(TIME *ltime);
};


class Field_time :public Field_str {
public:
816
  Field_time(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
817
	     enum utype unireg_check_arg, const char *field_name_arg,
818
	     struct st_table *table_arg, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
819
    :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
820
	       unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
821
    {}
822
  Field_time(bool maybe_null_arg, const char *field_name_arg,
823
		 struct st_table *table_arg, CHARSET_INFO *cs)
824
    :Field_str((char*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
825
	       NONE, field_name_arg, table_arg, cs) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
826 827 828
  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; }
829 830 831
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
832
  void reset(void) { ptr[0]=ptr[1]=ptr[2]=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
833 834 835
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
monty@mysql.com's avatar
monty@mysql.com committed
836
  bool get_date(TIME *ltime, uint fuzzydate);
837
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
838 839 840 841 842
  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;
843
  bool can_be_compared_as_longlong() const { return TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
844 845 846 847 848 849
  bool zero_pack() const { return 1; }
};


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; }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
865
  uint decimals() const { return DATETIME_DEC; }
866 867 868
  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
869
  void store_time(TIME *ltime,timestamp_type type);
870
  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
871 872 873
  double val_real(void);
  longlong val_int(void);
  String *val_str(String*,String *);
874
  bool send_binary(Protocol *protocol);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
875 876 877 878
  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;
879
  bool can_be_compared_as_longlong() const { return TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
880
  bool zero_pack() const { return 1; }
881
  bool get_date(TIME *ltime,uint fuzzydate);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
882 883 884 885 886 887 888
  bool get_time(TIME *ltime);
};


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 919 920
  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);
921
  int pack_cmp(const char *b,uint key_length);
922
  uint packed_col_length(const char *to, uint length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
923 924 925
  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; }
926 927
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
928 929 930 931 932 933
};


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

  enum_field_types type() const { return FIELD_TYPE_VAR_STRING; }
  enum ha_base_keytype key_type() const
949
    { return binary() ? HA_KEYTYPE_VARBINARY : HA_KEYTYPE_VARTEXT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
950 951 952 953
  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; }
954 955
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(longlong nr);
956
  int  store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
957 958 959 960 961
  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);
962 963
  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
964 965
  void sql_type(String &str) const;
  char *pack(char *to, const char *from, uint max_length=~(uint) 0);
966
  char *pack_key(char *to, const char *from, uint max_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
967 968
  const char *unpack(char* to, const char *from);
  int pack_cmp(const char *a, const char *b, uint key_length);
969
  int pack_cmp(const char *b, uint key_length);
970
  uint packed_col_length(const char *to, uint length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
971 972 973
  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; }
974 975
  bool has_charset(void) const
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
976 977 978 979
};


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

hf@deer.(none)'s avatar
hf@deer.(none) committed
1073
#ifdef HAVE_SPATIAL
1074 1075
class Field_geom :public Field_blob {
public:
1076
  enum geometry_type geom_type;
1077

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

1097 1098
  void get_key_image(char *buff,uint length, CHARSET_INFO *cs,imagetype type);
  void set_key_image(char *buff,uint length, CHARSET_INFO *cs);
1099
};
hf@deer.(none)'s avatar
hf@deer.(none) committed
1100
#endif /*HAVE_SPATIAL*/
1101

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1102 1103 1104 1105 1106 1107
class Field_enum :public Field_str {
protected:
  uint packlength;
public:
  TYPELIB *typelib;
  Field_enum(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1108
		 uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1109 1110
		 enum utype unireg_check_arg, const char *field_name_arg,
		 struct st_table *table_arg,uint packlength_arg,
1111 1112
		 TYPELIB *typelib_arg,
		 CHARSET_INFO *charset_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1113
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1114
	       unireg_check_arg, field_name_arg, table_arg, charset_arg),
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1115
    packlength(packlength_arg),typelib(typelib_arg)
1116
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1117
      flags|=ENUM_FLAG;
1118
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1119 1120
  enum_field_types type() const { return FIELD_TYPE_STRING; }
  enum Item_result cmp_type () const { return INT_RESULT; }
1121
  enum Item_result cast_to_int_type () const { return INT_RESULT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1122
  enum ha_base_keytype key_type() const;
1123 1124 1125
  int  store(const char *to,uint length,CHARSET_INFO *charset);
  int  store(double nr);
  int  store(longlong nr);
1126
  void reset() { bzero(ptr,packlength); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
  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; }
1138
  bool optimize_range(uint idx, uint part) { return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1139
  bool eq_def(Field *field);
1140
  bool has_charset(void) const { return TRUE; }
1141 1142
  /* enum and set are sorted as integers */
  CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1143 1144 1145 1146 1147 1148
};


class Field_set :public Field_enum {
public:
  Field_set(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1149
	    uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1150 1151
	    enum utype unireg_check_arg, const char *field_name_arg,
	    struct st_table *table_arg,uint32 packlength_arg,
1152
	    TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1153 1154 1155
    :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
		    unireg_check_arg, field_name_arg,
		    table_arg, packlength_arg,
1156
		    typelib_arg,charset_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1157 1158 1159
    {
      flags=(flags & ~ENUM_FLAG) | SET_FLAG;
    }
1160 1161 1162
  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
1163 1164 1165 1166
  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; }
1167
  bool has_charset(void) const { return TRUE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1168 1169 1170 1171
};


/*
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
1172
  Create field class for CREATE TABLE
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1173 1174 1175 1176 1177
*/

class create_field :public Sql_alloc {
public:
  const char *field_name;
1178 1179
  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
1180
  LEX_STRING comment;			// Comment for field
1181
  Item	*def;				// Default value
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1182
  enum	enum_field_types sql_type;
1183 1184 1185 1186
  /*
    At various stages in execution this can be length of field in bytes or
    max number of characters. 
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1187
  uint32 length;
1188 1189 1190
  /*
    The value of 'length' before a call to create_length_to_internal_length
  */
1191
  uint32 char_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1192 1193
  uint decimals,flags,pack_length;
  Field::utype unireg_check;
1194
  TYPELIB *interval;			// Which interval to use
1195
  List<String> interval_list;
1196
  CHARSET_INFO *charset;
1197
  Field::geometry_type geom_type;
1198
  Field *field;				// For alter table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1199

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


/*
salle@geopard.online.bg's avatar
salle@geopard.online.bg committed
1209
  A class for sending info to the client
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1210 1211 1212 1213
*/

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


/*
1225
  A class for quick copying data to fields
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
*/

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;
1237
  String tmp;					// For items
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1238 1239 1240

  Copy_field() {}
  ~Copy_field() {}
1241 1242
  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
1243
  void (*do_copy)(Copy_field *);
1244
  void (*do_copy2)(Copy_field *);		// Used to handle null values
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1245 1246 1247 1248
};


Field *make_field(char *ptr, uint32 field_length,
1249
		  uchar *null_pos, uchar null_bit,
1250
		  uint pack_flag, enum_field_types field_type,
1251
		  CHARSET_INFO *cs,
1252
		  Field::geometry_type geom_type,
1253
		  Field::utype unireg_check,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1254 1255 1256 1257
		  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
1258 1259
int set_field_to_null(Field *field);
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
1260 1261
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
1262 1263

/*
1264
  The following are for the interface with the .frm file
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1265 1266 1267
*/

#define FIELDFLAG_DECIMAL		1
1268
#define FIELDFLAG_BINARY		1	// Shares same flag
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1269 1270
#define FIELDFLAG_NUMBER		2
#define FIELDFLAG_ZEROFILL		4
1271
#define FIELDFLAG_PACK			120	// Bits used for packing
1272 1273 1274 1275
#define FIELDFLAG_INTERVAL		256     // mangled with decimals!
#define FIELDFLAG_BITFIELD		512	// mangled with decimals!
#define FIELDFLAG_BLOB			1024	// mangled with decimals!
#define FIELDFLAG_GEOM			2048    // mangled with decimals!
1276

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1277 1278
#define FIELDFLAG_LEFT_FULLSCREEN	8192
#define FIELDFLAG_RIGHT_FULLSCREEN	16384
1279 1280 1281
#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
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
#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)
1297
#define f_decimals(x)		((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1298
#define f_is_alpha(x)		(!f_is_num(x))
1299
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
1300 1301
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
#define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1302
#define f_is_blob(x)		(((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
1303
#define f_is_geom(x)		(((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1304 1305 1306
#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)