~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/field.h

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/*
 
17
  Because of the function new_field() all field classes that have static
 
18
  variables must declare the size_of() member function.
 
19
*/
 
20
 
 
21
#ifdef USE_PRAGMA_INTERFACE
 
22
#pragma interface                       /* gcc class implementation */
 
23
#endif
 
24
 
 
25
#define NOT_FIXED_DEC                   31
 
26
#define DATETIME_DEC                     6
 
27
const uint32 max_field_size= (uint32) 4294967295U;
 
28
 
 
29
class Send_field;
 
30
class Protocol;
 
31
class Create_field;
 
32
class Relay_log_info;
 
33
 
 
34
struct st_cache_field;
 
35
int field_conv(Field *to,Field *from);
 
36
 
 
37
inline uint get_enum_pack_length(int elements)
 
38
{
 
39
  return elements < 256 ? 1 : 2;
 
40
}
 
41
 
 
42
inline uint get_set_pack_length(int elements)
 
43
{
 
44
  uint len= (elements + 7) / 8;
 
45
  return len > 4 ? 8 : len;
 
46
}
 
47
 
 
48
class Field
 
49
{
 
50
  Field(const Item &);                          /* Prevent use of these */
 
51
  void operator=(Field &);
 
52
public:
 
53
  static void *operator new(size_t size) throw ()
 
54
  { return sql_alloc(size); }
 
55
  static void operator delete(void *ptr_arg, size_t size) { TRASH(ptr_arg, size); }
 
56
 
 
57
  uchar         *ptr;                   // Position to field in record
 
58
  uchar         *null_ptr;              // Byte where null_bit is
 
59
  /*
 
60
    Note that you can use table->in_use as replacement for current_thd member 
 
61
    only inside of val_*() and store() members (e.g. you can't use it in cons)
 
62
  */
 
63
  struct st_table *table;               // Pointer for table
 
64
  struct st_table *orig_table;          // Pointer to original table
 
65
  const char    **table_name, *field_name;
 
66
  LEX_STRING    comment;
 
67
  /* Field is part of the following keys */
 
68
  key_map       key_start, part_of_key, part_of_key_not_clustered;
 
69
  key_map       part_of_sortkey;
 
70
  /* 
 
71
    We use three additional unireg types for TIMESTAMP to overcome limitation 
 
72
    of current binary format of .frm file. We'd like to be able to support 
 
73
    NOW() as default and on update value for such fields but unable to hold 
 
74
    this info anywhere except unireg_check field. This issue will be resolved
 
75
    in more clean way with transition to new text based .frm format.
 
76
    See also comment for Field_timestamp::Field_timestamp().
 
77
  */
 
78
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
 
79
                CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
 
80
                BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
 
81
                TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
 
82
  enum geometry_type
 
83
  {
 
84
    GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
 
85
    GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
 
86
    GEOM_GEOMETRYCOLLECTION = 7
 
87
  };
 
88
  enum imagetype { itRAW, itMBR};
 
89
 
 
90
  utype         unireg_check;
 
91
  uint32        field_length;           // Length of field
 
92
  uint32        flags;
 
93
  uint16        field_index;            // field number in fields array
 
94
  uchar         null_bit;               // Bit used to test null bit
 
95
  /**
 
96
     If true, this field was created in create_tmp_field_from_item from a NULL
 
97
     value. This means that the type of the field is just a guess, and the type
 
98
     may be freely coerced to another type.
 
99
 
 
100
     @see create_tmp_field_from_item
 
101
     @see Item_type_holder::get_real_type
 
102
 
 
103
   */
 
104
  bool is_created_from_null_item;
 
105
 
 
106
  Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
 
107
        uchar null_bit_arg, utype unireg_check_arg,
 
108
        const char *field_name_arg);
 
109
  virtual ~Field() {}
 
110
  /* Store functions returns 1 on overflow and -1 on fatal error */
 
111
  virtual int  store(const char *to, uint length,CHARSET_INFO *cs)=0;
 
112
  virtual int  store(double nr)=0;
 
113
  virtual int  store(longlong nr, bool unsigned_val)=0;
 
114
  virtual int  store_decimal(const my_decimal *d)=0;
 
115
  virtual int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
 
116
  int store(const char *to, uint length, CHARSET_INFO *cs,
 
117
            enum_check_fields check_level);
 
118
  virtual double val_real(void)=0;
 
119
  virtual longlong val_int(void)=0;
 
120
  virtual my_decimal *val_decimal(my_decimal *);
 
121
  inline String *val_str(String *str) { return val_str(str, str); }
 
122
  /*
 
123
     val_str(buf1, buf2) gets two buffers and should use them as follows:
 
124
     if it needs a temp buffer to convert result to string - use buf1
 
125
       example Field_tiny::val_str()
 
126
     if the value exists as a string already - use buf2
 
127
       example Field_string::val_str()
 
128
     consequently, buf2 may be created as 'String buf;' - no memory
 
129
     will be allocated for it. buf1 will be allocated to hold a
 
130
     value if it's too small. Using allocated buffer for buf2 may result in
 
131
     an unnecessary free (and later, may be an alloc).
 
132
     This trickery is used to decrease a number of malloc calls.
 
133
  */
 
134
  virtual String *val_str(String*,String *)=0;
 
135
  String *val_int_as_str(String *val_buffer, my_bool unsigned_flag);
 
136
  /*
 
137
   str_needs_quotes() returns TRUE if the value returned by val_str() needs
 
138
   to be quoted when used in constructing an SQL query.
 
139
  */
 
140
  virtual bool str_needs_quotes() { return FALSE; }
 
141
  virtual Item_result result_type () const=0;
 
142
  virtual Item_result cmp_type () const { return result_type(); }
 
143
  virtual Item_result cast_to_int_type () const { return result_type(); }
 
144
  static bool type_can_have_key_part(enum_field_types);
 
145
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
 
146
  static Item_result result_merge_type(enum_field_types);
 
147
  virtual bool eq(Field *field)
 
148
  {
 
149
    return (ptr == field->ptr && null_ptr == field->null_ptr &&
 
150
            null_bit == field->null_bit && field->type() == type());
 
151
  }
 
152
  virtual bool eq_def(Field *field);
 
153
  
 
154
  /*
 
155
    pack_length() returns size (in bytes) used to store field data in memory
 
156
    (i.e. it returns the maximum size of the field in a row of the table,
 
157
    which is located in RAM).
 
158
  */
 
159
  virtual uint32 pack_length() const { return (uint32) field_length; }
 
160
 
 
161
  /*
 
162
    pack_length_in_rec() returns size (in bytes) used to store field data on
 
163
    storage (i.e. it returns the maximal size of the field in a row of the
 
164
    table, which is located on disk).
 
165
  */
 
166
  virtual uint32 pack_length_in_rec() const { return pack_length(); }
 
167
  virtual int compatible_field_size(uint field_metadata,
 
168
                                    const Relay_log_info *);
 
169
  virtual uint pack_length_from_metadata(uint field_metadata)
 
170
  { return field_metadata; }
 
171
  /*
 
172
    This method is used to return the size of the data in a row-based
 
173
    replication row record. The default implementation of returning 0 is
 
174
    designed to allow fields that do not use metadata to return TRUE (1)
 
175
    from compatible_field_size() which uses this function in the comparison.
 
176
    The default value for field metadata for fields that do not have 
 
177
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
 
178
 
 
179
    Note: While most classes that override this method return pack_length(),
 
180
    the classes Field_string, Field_varstring, and Field_blob return 
 
181
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
 
182
  */
 
183
  virtual uint row_pack_length() { return 0; }
 
184
  virtual int save_field_metadata(uchar *first_byte)
 
185
  { return do_save_field_metadata(first_byte); }
 
186
 
 
187
  /*
 
188
    data_length() return the "real size" of the data in memory.
 
189
  */
 
190
  virtual uint32 data_length() { return pack_length(); }
 
191
  virtual uint32 sort_length() const { return pack_length(); }
 
192
 
 
193
  /**
 
194
     Get the maximum size of the data in packed format.
 
195
 
 
196
     @return Maximum data length of the field when packed using the
 
197
     Field::pack() function.
 
198
   */
 
199
  virtual uint32 max_data_length() const {
 
200
    return pack_length();
 
201
  };
 
202
 
 
203
  virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
 
204
  virtual void reset_fields() {}
 
205
  virtual void set_default()
 
206
  {
 
207
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->s->default_values -
 
208
                                          table->record[0]);
 
209
    memcpy(ptr, ptr + l_offset, pack_length());
 
210
    if (null_ptr)
 
211
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) |
 
212
                  (null_ptr[l_offset] & null_bit));
 
213
  }
 
214
  virtual bool binary() const { return 1; }
 
215
  virtual bool zero_pack() const { return 1; }
 
216
  virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
 
217
  virtual uint32 key_length() const { return pack_length(); }
 
218
  virtual enum_field_types type() const =0;
 
219
  virtual enum_field_types real_type() const { return type(); }
 
220
  inline  int cmp(const uchar *str) { return cmp(ptr,str); }
 
221
  virtual int cmp_max(const uchar *a, const uchar *b, uint max_len)
 
222
    { return cmp(a, b); }
 
223
  virtual int cmp(const uchar *,const uchar *)=0;
 
224
  virtual int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L)
 
225
  { return memcmp(a,b,pack_length()); }
 
226
  virtual int cmp_offset(uint row_offset)
 
227
  { return cmp(ptr,ptr+row_offset); }
 
228
  virtual int cmp_binary_offset(uint row_offset)
 
229
  { return cmp_binary(ptr, ptr+row_offset); };
 
230
  virtual int key_cmp(const uchar *a,const uchar *b)
 
231
  { return cmp(a, b); }
 
232
  virtual int key_cmp(const uchar *str, uint length)
 
233
  { return cmp(ptr,str); }
 
234
  virtual uint decimals() const { return 0; }
 
235
  /*
 
236
    Caller beware: sql_type can change str.Ptr, so check
 
237
    ptr() to see if it changed if you are using your own buffer
 
238
    in str and restore it with set() if needed
 
239
  */
 
240
  virtual void sql_type(String &str) const =0;
 
241
  virtual uint size_of() const =0;              // For new field
 
242
  inline bool is_null(my_ptrdiff_t row_offset= 0)
 
243
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
 
244
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
 
245
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
 
246
  inline bool is_null_in_record(const uchar *record)
 
247
  {
 
248
    if (!null_ptr)
 
249
      return 0;
 
250
    return test(record[(uint) (null_ptr -table->record[0])] &
 
251
                null_bit);
 
252
  }
 
253
  inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
 
254
  {
 
255
    if (!null_ptr)
 
256
      return 0;
 
257
    return test(null_ptr[offset] & null_bit);
 
258
  }
 
259
  inline void set_null(my_ptrdiff_t row_offset= 0)
 
260
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
 
261
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
 
262
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
 
263
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
 
264
  inline bool real_maybe_null(void) { return null_ptr != 0; }
 
265
 
 
266
  enum {
 
267
    LAST_NULL_BYTE_UNDEF= 0
 
268
  };
 
269
 
 
270
  /*
 
271
    Find the position of the last null byte for the field.
 
272
 
 
273
    SYNOPSIS
 
274
      last_null_byte()
 
275
 
 
276
    DESCRIPTION
 
277
      Return a pointer to the last byte of the null bytes where the
 
278
      field conceptually is placed.
 
279
 
 
280
    RETURN VALUE
 
281
      The position of the last null byte relative to the beginning of
 
282
      the record. If the field does not use any bits of the null
 
283
      bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
 
284
   */
 
285
  size_t last_null_byte() const {
 
286
    size_t bytes= do_last_null_byte();
 
287
    DBUG_PRINT("debug", ("last_null_byte() ==> %ld", (long) bytes));
 
288
    DBUG_ASSERT(bytes <= table->s->null_bytes);
 
289
    return bytes;
 
290
  }
 
291
 
 
292
  virtual void make_field(Send_field *);
 
293
  virtual void sort_string(uchar *buff,uint length)=0;
 
294
  virtual bool optimize_range(uint idx, uint part);
 
295
  /*
 
296
    This should be true for fields which, when compared with constant
 
297
    items, can be casted to longlong. In this case we will at 'fix_fields'
 
298
    stage cast the constant items to longlongs and at the execution stage
 
299
    use field->val_int() for comparison.  Used to optimize clauses like
 
300
    'a_column BETWEEN date_const, date_const'.
 
301
  */
 
302
  virtual bool can_be_compared_as_longlong() const { return FALSE; }
 
303
  virtual void free() {}
 
304
  virtual Field *new_field(MEM_ROOT *root, struct st_table *new_table,
 
305
                           bool keep_type);
 
306
  virtual Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
307
                               uchar *new_ptr, uchar *new_null_ptr,
 
308
                               uint new_null_bit);
 
309
  Field *clone(MEM_ROOT *mem_root, struct st_table *new_table);
 
310
  inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
 
311
  {
 
312
    ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
 
313
  }
 
314
  inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
 
315
  virtual void move_field_offset(my_ptrdiff_t ptr_diff)
 
316
  {
 
317
    ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
 
318
    if (null_ptr)
 
319
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
 
320
  }
 
321
  virtual void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
 
322
    { memcpy(buff,ptr,length); }
 
323
  virtual void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
 
324
    { memcpy(ptr,buff,length); }
 
325
 
 
326
 
 
327
  /*
 
328
    Copy a field part into an output buffer.
 
329
 
 
330
    SYNOPSIS
 
331
      Field::get_key_image()
 
332
      buff   [out] output buffer
 
333
      length       output buffer size
 
334
      type         itMBR for geometry blobs, otherwise itRAW
 
335
 
 
336
    DESCRIPTION
 
337
      This function makes a copy of field part of size equal to or
 
338
      less than "length" parameter value.
 
339
      For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
 
340
      is padded by zero byte.
 
341
 
 
342
    NOTES
 
343
      For variable length character fields (i.e. UTF-8) the "length"
 
344
      parameter means a number of output buffer bytes as if all field
 
345
      characters have maximal possible size (mbmaxlen). In the other words,
 
346
      "length" parameter is a number of characters multiplied by
 
347
      field_charset->mbmaxlen.
 
348
 
 
349
    RETURN
 
350
      Number of copied bytes (excluding padded zero bytes -- see above).
 
351
  */
 
352
 
 
353
  virtual uint get_key_image(uchar *buff, uint length, imagetype type)
 
354
  {
 
355
    get_image(buff, length, &my_charset_bin);
 
356
    return length;
 
357
  }
 
358
  virtual void set_key_image(const uchar *buff,uint length)
 
359
    { set_image(buff,length, &my_charset_bin); }
 
360
  inline longlong val_int_offset(uint row_offset)
 
361
    {
 
362
      ptr+=row_offset;
 
363
      longlong tmp=val_int();
 
364
      ptr-=row_offset;
 
365
      return tmp;
 
366
    }
 
367
  inline longlong val_int(const uchar *new_ptr)
 
368
  {
 
369
    uchar *old_ptr= ptr;
 
370
    longlong return_value;
 
371
    ptr= (uchar*) new_ptr;
 
372
    return_value= val_int();
 
373
    ptr= old_ptr;
 
374
    return return_value;
 
375
  }
 
376
  inline String *val_str(String *str, const uchar *new_ptr)
 
377
  {
 
378
    uchar *old_ptr= ptr;
 
379
    ptr= (uchar*) new_ptr;
 
380
    val_str(str);
 
381
    ptr= old_ptr;
 
382
    return str;
 
383
  }
 
384
  virtual bool send_binary(Protocol *protocol);
 
385
 
 
386
  virtual uchar *pack(uchar *to, const uchar *from,
 
387
                      uint max_length, bool low_byte_first);
 
388
  /**
 
389
     @overload Field::pack(uchar*, const uchar*, uint, bool)
 
390
  */
 
391
  uchar *pack(uchar *to, const uchar *from)
 
392
  {
 
393
    DBUG_ENTER("Field::pack");
 
394
    uchar *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
 
395
    DBUG_RETURN(result);
 
396
  }
 
397
 
 
398
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
399
                              uint param_data, bool low_byte_first);
 
400
  /**
 
401
     @overload Field::unpack(uchar*, const uchar*, uint, bool)
 
402
  */
 
403
  const uchar *unpack(uchar* to, const uchar *from)
 
404
  {
 
405
    DBUG_ENTER("Field::unpack");
 
406
    const uchar *result= unpack(to, from, 0U, table->s->db_low_byte_first);
 
407
    DBUG_RETURN(result);
 
408
  }
 
409
 
 
410
  virtual uchar *pack_key(uchar* to, const uchar *from,
 
411
                          uint max_length, bool low_byte_first)
 
412
  {
 
413
    return pack(to, from, max_length, low_byte_first);
 
414
  }
 
415
  virtual uchar *pack_key_from_key_image(uchar* to, const uchar *from,
 
416
                                        uint max_length, bool low_byte_first)
 
417
  {
 
418
    return pack(to, from, max_length, low_byte_first);
 
419
  }
 
420
  virtual const uchar *unpack_key(uchar* to, const uchar *from,
 
421
                                  uint max_length, bool low_byte_first)
 
422
  {
 
423
    return unpack(to, from, max_length, low_byte_first);
 
424
  }
 
425
  virtual uint packed_col_length(const uchar *to, uint length)
 
426
  { return length;}
 
427
  virtual uint max_packed_col_length(uint max_length)
 
428
  { return max_length;}
 
429
 
 
430
  virtual int pack_cmp(const uchar *a,const uchar *b, uint key_length_arg,
 
431
                       my_bool insert_or_update)
 
432
  { return cmp(a,b); }
 
433
  virtual int pack_cmp(const uchar *b, uint key_length_arg,
 
434
                       my_bool insert_or_update)
 
435
  { return cmp(ptr,b); }
 
436
  uint offset(uchar *record)
 
437
  {
 
438
    return (uint) (ptr - record);
 
439
  }
 
440
  void copy_from_tmp(int offset);
 
441
  uint fill_cache_field(struct st_cache_field *copy);
 
442
  virtual bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
443
  virtual bool get_time(MYSQL_TIME *ltime);
 
444
  virtual CHARSET_INFO *charset(void) const { return &my_charset_bin; }
 
445
  virtual CHARSET_INFO *sort_charset(void) const { return charset(); }
 
446
  virtual bool has_charset(void) const { return FALSE; }
 
447
  virtual void set_charset(CHARSET_INFO *charset_arg) { }
 
448
  virtual enum Derivation derivation(void) const
 
449
  { return DERIVATION_IMPLICIT; }
 
450
  virtual void set_derivation(enum Derivation derivation_arg) { }
 
451
  bool set_warning(MYSQL_ERROR::enum_warning_level, unsigned int code,
 
452
                   int cuted_increment);
 
453
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code, 
 
454
                            const char *str, uint str_len,
 
455
                            timestamp_type ts_type, int cuted_increment);
 
456
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, uint code, 
 
457
                            longlong nr, timestamp_type ts_type,
 
458
                            int cuted_increment);
 
459
  void set_datetime_warning(MYSQL_ERROR::enum_warning_level, const uint code, 
 
460
                            double nr, timestamp_type ts_type);
 
461
  inline bool check_overflow(int op_result)
 
462
  {
 
463
    return (op_result == E_DEC_OVERFLOW);
 
464
  }
 
465
  int warn_if_overflow(int op_result);
 
466
  void init(TABLE *table_arg)
 
467
  {
 
468
    orig_table= table= table_arg;
 
469
    table_name= &table_arg->alias;
 
470
  }
 
471
 
 
472
  /* maximum possible display length */
 
473
  virtual uint32 max_display_length()= 0;
 
474
 
 
475
  /**
 
476
    Whether a field being created is compatible with a existing one.
 
477
 
 
478
    Used by the ALTER TABLE code to evaluate whether the new definition
 
479
    of a table is compatible with the old definition so that it can
 
480
    determine if data needs to be copied over (table data change).
 
481
  */
 
482
  virtual uint is_equal(Create_field *new_field);
 
483
  /* convert decimal to longlong with overflow check */
 
484
  longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
 
485
                                    int *err);
 
486
  /* The max. number of characters */
 
487
  inline uint32 char_length() const
 
488
  {
 
489
    return field_length / charset()->mbmaxlen;
 
490
  }
 
491
 
 
492
  virtual geometry_type get_geometry_type()
 
493
  {
 
494
    /* shouldn't get here. */
 
495
    DBUG_ASSERT(0);
 
496
    return GEOM_GEOMETRY;
 
497
  }
 
498
  /* Hash value */
 
499
  virtual void hash(ulong *nr, ulong *nr2);
 
500
  friend bool reopen_table(THD *,struct st_table *,bool);
 
501
  friend int cre_myisam(char * name, register TABLE *form, uint options,
 
502
                        ulonglong auto_increment_value);
 
503
  friend class Copy_field;
 
504
  friend class Item_avg_field;
 
505
  friend class Item_std_field;
 
506
  friend class Item_sum_num;
 
507
  friend class Item_sum_sum;
 
508
  friend class Item_sum_str;
 
509
  friend class Item_sum_count;
 
510
  friend class Item_sum_avg;
 
511
  friend class Item_sum_std;
 
512
  friend class Item_sum_min;
 
513
  friend class Item_sum_max;
 
514
  friend class Item_func_group_concat;
 
515
 
 
516
private:
 
517
  /*
 
518
    Primitive for implementing last_null_byte().
 
519
 
 
520
    SYNOPSIS
 
521
      do_last_null_byte()
 
522
 
 
523
    DESCRIPTION
 
524
      Primitive for the implementation of the last_null_byte()
 
525
      function. This represents the inheritance interface and can be
 
526
      overridden by subclasses.
 
527
   */
 
528
  virtual size_t do_last_null_byte() const;
 
529
 
 
530
/**
 
531
   Retrieve the field metadata for fields.
 
532
 
 
533
   This default implementation returns 0 and saves 0 in the metadata_ptr
 
534
   value.
 
535
 
 
536
   @param   metadata_ptr   First byte of field metadata
 
537
 
 
538
   @returns 0 no bytes written.
 
539
*/
 
540
  virtual int do_save_field_metadata(uchar *metadata_ptr)
 
541
  { return 0; }
 
542
 
 
543
protected:
 
544
  /*
 
545
    Helper function to pack()/unpack() int32 values
 
546
  */
 
547
  static void handle_int32(uchar *to, const uchar *from,
 
548
                           bool low_byte_first_from, bool low_byte_first_to)
 
549
  {
 
550
    int32 val;
 
551
#ifdef WORDS_BIGENDIAN
 
552
    if (low_byte_first_from)
 
553
      val = sint4korr(from);
 
554
    else
 
555
#endif
 
556
      longget(val, from);
 
557
 
 
558
#ifdef WORDS_BIGENDIAN
 
559
    if (low_byte_first_to)
 
560
      int4store(to, val);
 
561
    else
 
562
#endif
 
563
      longstore(to, val);
 
564
  }
 
565
 
 
566
  /*
 
567
    Helper function to pack()/unpack() int64 values
 
568
  */
 
569
  static void handle_int64(uchar* to, const uchar *from,
 
570
                           bool low_byte_first_from, bool low_byte_first_to)
 
571
  {
 
572
    int64 val;
 
573
#ifdef WORDS_BIGENDIAN
 
574
    if (low_byte_first_from)
 
575
      val = sint8korr(from);
 
576
    else
 
577
#endif
 
578
      longlongget(val, from);
 
579
 
 
580
#ifdef WORDS_BIGENDIAN
 
581
    if (low_byte_first_to)
 
582
      int8store(to, val);
 
583
    else
 
584
#endif
 
585
      longlongstore(to, val);
 
586
  }
 
587
 
 
588
  uchar *pack_int32(uchar *to, const uchar *from, bool low_byte_first_to)
 
589
  {
 
590
    handle_int32(to, from, table->s->db_low_byte_first, low_byte_first_to);
 
591
    return to  + sizeof(int32);
 
592
  }
 
593
 
 
594
  const uchar *unpack_int32(uchar* to, const uchar *from,
 
595
                            bool low_byte_first_from)
 
596
  {
 
597
    handle_int32(to, from, low_byte_first_from, table->s->db_low_byte_first);
 
598
    return from + sizeof(int32);
 
599
  }
 
600
 
 
601
  uchar *pack_int64(uchar* to, const uchar *from, bool low_byte_first_to)
 
602
  {
 
603
    handle_int64(to, from, table->s->db_low_byte_first, low_byte_first_to);
 
604
    return to + sizeof(int64);
 
605
  }
 
606
 
 
607
  const uchar *unpack_int64(uchar* to, const uchar *from,
 
608
                            bool low_byte_first_from)
 
609
  {
 
610
    handle_int64(to, from, low_byte_first_from, table->s->db_low_byte_first);
 
611
    return from + sizeof(int64);
 
612
  }
 
613
 
 
614
  bool field_flags_are_binary()
 
615
  {
 
616
    return (flags & (BINCMP_FLAG | BINARY_FLAG)) != 0;
 
617
  }
 
618
 
 
619
};
 
620
 
 
621
 
 
622
class Field_num :public Field {
 
623
public:
 
624
  const uint8 dec;
 
625
  bool zerofill,unsigned_flag;  // Purify cannot handle bit fields
 
626
  Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
627
            uchar null_bit_arg, utype unireg_check_arg,
 
628
            const char *field_name_arg,
 
629
            uint8 dec_arg, bool zero_arg, bool unsigned_arg);
 
630
  Item_result result_type () const { return REAL_RESULT; }
 
631
  void prepend_zeros(String *value);
 
632
  void add_zerofill_and_unsigned(String &res) const;
 
633
  friend class Create_field;
 
634
  void make_field(Send_field *);
 
635
  uint decimals() const { return (uint) dec; }
 
636
  uint size_of() const { return sizeof(*this); }
 
637
  bool eq_def(Field *field);
 
638
  int store_decimal(const my_decimal *);
 
639
  my_decimal *val_decimal(my_decimal *);
 
640
  uint is_equal(Create_field *new_field);
 
641
  int check_int(CHARSET_INFO *cs, const char *str, int length,
 
642
                const char *int_end, int error);
 
643
  bool get_int(CHARSET_INFO *cs, const char *from, uint len, 
 
644
               longlong *rnd, ulonglong unsigned_max, 
 
645
               longlong signed_min, longlong signed_max);
 
646
};
 
647
 
 
648
 
 
649
class Field_str :public Field {
 
650
protected:
 
651
  CHARSET_INFO *field_charset;
 
652
  enum Derivation field_derivation;
 
653
public:
 
654
  Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
 
655
            uchar null_bit_arg, utype unireg_check_arg,
 
656
            const char *field_name_arg, CHARSET_INFO *charset);
 
657
  Item_result result_type () const { return STRING_RESULT; }
 
658
  uint decimals() const { return NOT_FIXED_DEC; }
 
659
  int  store(double nr);
 
660
  int  store(longlong nr, bool unsigned_val)=0;
 
661
  int  store_decimal(const my_decimal *);
 
662
  int  store(const char *to,uint length,CHARSET_INFO *cs)=0;
 
663
  uint size_of() const { return sizeof(*this); }
 
664
  CHARSET_INFO *charset(void) const { return field_charset; }
 
665
  void set_charset(CHARSET_INFO *charset_arg) { field_charset= charset_arg; }
 
666
  enum Derivation derivation(void) const { return field_derivation; }
 
667
  virtual void set_derivation(enum Derivation derivation_arg)
 
668
  { field_derivation= derivation_arg; }
 
669
  bool binary() const { return field_charset == &my_charset_bin; }
 
670
  uint32 max_display_length() { return field_length; }
 
671
  friend class Create_field;
 
672
  my_decimal *val_decimal(my_decimal *);
 
673
  virtual bool str_needs_quotes() { return TRUE; }
 
674
  uint is_equal(Create_field *new_field);
 
675
};
 
676
 
 
677
 
 
678
/* base class for Field_string, Field_varstring and Field_blob */
 
679
 
 
680
class Field_longstr :public Field_str
 
681
{
 
682
protected:
 
683
  int report_if_important_data(const char *ptr, const char *end,
 
684
                               bool count_spaces);
 
685
public:
 
686
  Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
687
                uchar null_bit_arg, utype unireg_check_arg,
 
688
                const char *field_name_arg, CHARSET_INFO *charset_arg)
 
689
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
 
690
               field_name_arg, charset_arg)
 
691
    {}
 
692
 
 
693
  int store_decimal(const my_decimal *d);
 
694
  uint32 max_data_length() const;
 
695
};
 
696
 
 
697
/* base class for float and double and decimal (old one) */
 
698
class Field_real :public Field_num {
 
699
public:
 
700
  my_bool not_fixed;
 
701
 
 
702
  Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
703
             uchar null_bit_arg, utype unireg_check_arg,
 
704
             const char *field_name_arg,
 
705
             uint8 dec_arg, bool zero_arg, bool unsigned_arg)
 
706
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
 
707
               field_name_arg, dec_arg, zero_arg, unsigned_arg),
 
708
    not_fixed(dec_arg >= NOT_FIXED_DEC)
 
709
    {}
 
710
  int store_decimal(const my_decimal *);
 
711
  my_decimal *val_decimal(my_decimal *);
 
712
  int truncate(double *nr, double max_length);
 
713
  uint32 max_display_length() { return field_length; }
 
714
  uint size_of() const { return sizeof(*this); }
 
715
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
716
                              uint param_data, bool low_byte_first);
 
717
  virtual uchar *pack(uchar* to, const uchar *from,
 
718
                      uint max_length, bool low_byte_first);
 
719
};
 
720
 
 
721
 
 
722
class Field_decimal :public Field_real {
 
723
public:
 
724
  Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
725
                uchar null_bit_arg,
 
726
                enum utype unireg_check_arg, const char *field_name_arg,
 
727
                uint8 dec_arg,bool zero_arg,bool unsigned_arg)
 
728
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
729
                unireg_check_arg, field_name_arg,
 
730
                dec_arg, zero_arg, unsigned_arg)
 
731
    {}
 
732
  enum_field_types type() const { return MYSQL_TYPE_DECIMAL;}
 
733
  enum ha_base_keytype key_type() const
 
734
  { return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM; }
 
735
  int reset(void);
 
736
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
737
  int store(double nr);
 
738
  int store(longlong nr, bool unsigned_val);
 
739
  double val_real(void);
 
740
  longlong val_int(void);
 
741
  String *val_str(String*,String *);
 
742
  int cmp(const uchar *,const uchar *);
 
743
  void sort_string(uchar *buff,uint length);
 
744
  void overflow(bool negative);
 
745
  bool zero_pack() const { return 0; }
 
746
  void sql_type(String &str) const;
 
747
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
748
                              uint param_data, bool low_byte_first)
 
749
  {
 
750
    return Field::unpack(to, from, param_data, low_byte_first);
 
751
  }
 
752
  virtual uchar *pack(uchar* to, const uchar *from,
 
753
                      uint max_length, bool low_byte_first)
 
754
  {
 
755
    return Field::pack(to, from, max_length, low_byte_first);
 
756
  }
 
757
};
 
758
 
 
759
 
 
760
/* New decimal/numeric field which use fixed point arithmetic */
 
761
class Field_new_decimal :public Field_num {
 
762
private:
 
763
  int do_save_field_metadata(uchar *first_byte);
 
764
public:
 
765
  /* The maximum number of decimal digits can be stored */
 
766
  uint precision;
 
767
  uint bin_size;
 
768
  /*
 
769
    Constructors take max_length of the field as a parameter - not the
 
770
    precision as the number of decimal digits allowed.
 
771
    So for example we need to count length from precision handling
 
772
    CREATE TABLE ( DECIMAL(x,y)) 
 
773
  */
 
774
  Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
775
                    uchar null_bit_arg,
 
776
                    enum utype unireg_check_arg, const char *field_name_arg,
 
777
                    uint8 dec_arg, bool zero_arg, bool unsigned_arg);
 
778
  Field_new_decimal(uint32 len_arg, bool maybe_null_arg,
 
779
                    const char *field_name_arg, uint8 dec_arg,
 
780
                    bool unsigned_arg);
 
781
  enum_field_types type() const { return MYSQL_TYPE_NEWDECIMAL;}
 
782
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
 
783
  Item_result result_type () const { return DECIMAL_RESULT; }
 
784
  int  reset(void);
 
785
  bool store_value(const my_decimal *decimal_value);
 
786
  void set_value_on_overflow(my_decimal *decimal_value, bool sign);
 
787
  int  store(const char *to, uint length, CHARSET_INFO *charset);
 
788
  int  store(double nr);
 
789
  int  store(longlong nr, bool unsigned_val);
 
790
  int store_time(MYSQL_TIME *ltime, timestamp_type t_type);
 
791
  int  store_decimal(const my_decimal *);
 
792
  double val_real(void);
 
793
  longlong val_int(void);
 
794
  my_decimal *val_decimal(my_decimal *);
 
795
  String *val_str(String*, String *);
 
796
  int cmp(const uchar *, const uchar *);
 
797
  void sort_string(uchar *buff, uint length);
 
798
  bool zero_pack() const { return 0; }
 
799
  void sql_type(String &str) const;
 
800
  uint32 max_display_length() { return field_length; }
 
801
  uint size_of() const { return sizeof(*this); } 
 
802
  uint32 pack_length() const { return (uint32) bin_size; }
 
803
  uint pack_length_from_metadata(uint field_metadata);
 
804
  uint row_pack_length() { return pack_length(); }
 
805
  int compatible_field_size(uint field_metadata,
 
806
                            const Relay_log_info *rli);
 
807
  uint is_equal(Create_field *new_field);
 
808
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
809
                              uint param_data, bool low_byte_first);
 
810
  static Field *create_from_item (Item *);
 
811
};
 
812
 
 
813
 
 
814
class Field_tiny :public Field_num {
 
815
public:
 
816
  Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
817
             uchar null_bit_arg,
 
818
             enum utype unireg_check_arg, const char *field_name_arg,
 
819
             bool zero_arg, bool unsigned_arg)
 
820
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
821
               unireg_check_arg, field_name_arg,
 
822
               0, zero_arg,unsigned_arg)
 
823
    {}
 
824
  enum Item_result result_type () const { return INT_RESULT; }
 
825
  enum_field_types type() const { return MYSQL_TYPE_TINY;}
 
826
  enum ha_base_keytype key_type() const
 
827
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
 
828
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
829
  int store(double nr);
 
830
  int store(longlong nr, bool unsigned_val);
 
831
  int reset(void) { ptr[0]=0; return 0; }
 
832
  double val_real(void);
 
833
  longlong val_int(void);
 
834
  String *val_str(String*,String *);
 
835
  bool send_binary(Protocol *protocol);
 
836
  int cmp(const uchar *,const uchar *);
 
837
  void sort_string(uchar *buff,uint length);
 
838
  uint32 pack_length() const { return 1; }
 
839
  void sql_type(String &str) const;
 
840
  uint32 max_display_length() { return 4; }
 
841
 
 
842
  virtual uchar *pack(uchar* to, const uchar *from,
 
843
                      uint max_length, bool low_byte_first)
 
844
  {
 
845
    *to= *from;
 
846
    return to + 1;
 
847
  }
 
848
 
 
849
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
850
                              uint param_data, bool low_byte_first)
 
851
  {
 
852
    *to= *from;
 
853
    return from + 1;
 
854
  }
 
855
};
 
856
 
 
857
 
 
858
class Field_short :public Field_num {
 
859
public:
 
860
  Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
861
              uchar null_bit_arg,
 
862
              enum utype unireg_check_arg, const char *field_name_arg,
 
863
              bool zero_arg, bool unsigned_arg)
 
864
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
865
               unireg_check_arg, field_name_arg,
 
866
               0, zero_arg,unsigned_arg)
 
867
    {}
 
868
  Field_short(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
869
              bool unsigned_arg)
 
870
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
 
871
               NONE, field_name_arg, 0, 0, unsigned_arg)
 
872
    {}
 
873
  enum Item_result result_type () const { return INT_RESULT; }
 
874
  enum_field_types type() const { return MYSQL_TYPE_SHORT;}
 
875
  enum ha_base_keytype key_type() const
 
876
    { return unsigned_flag ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;}
 
877
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
878
  int store(double nr);
 
879
  int store(longlong nr, bool unsigned_val);
 
880
  int reset(void) { ptr[0]=ptr[1]=0; return 0; }
 
881
  double val_real(void);
 
882
  longlong val_int(void);
 
883
  String *val_str(String*,String *);
 
884
  bool send_binary(Protocol *protocol);
 
885
  int cmp(const uchar *,const uchar *);
 
886
  void sort_string(uchar *buff,uint length);
 
887
  uint32 pack_length() const { return 2; }
 
888
  void sql_type(String &str) const;
 
889
  uint32 max_display_length() { return 6; }
 
890
 
 
891
  virtual uchar *pack(uchar* to, const uchar *from,
 
892
                      uint max_length, bool low_byte_first)
 
893
  {
 
894
    int16 val;
 
895
#ifdef WORDS_BIGENDIAN
 
896
    if (table->s->db_low_byte_first)
 
897
      val = sint2korr(from);
 
898
    else
 
899
#endif
 
900
      shortget(val, from);
 
901
 
 
902
#ifdef WORDS_BIGENDIAN
 
903
    if (low_byte_first)
 
904
      int2store(to, val);
 
905
    else
 
906
#endif
 
907
      shortstore(to, val);
 
908
    return to + sizeof(val);
 
909
  }
 
910
 
 
911
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
912
                              uint param_data, bool low_byte_first)
 
913
  {
 
914
    int16 val;
 
915
#ifdef WORDS_BIGENDIAN
 
916
    if (low_byte_first)
 
917
      val = sint2korr(from);
 
918
    else
 
919
#endif
 
920
      shortget(val, from);
 
921
 
 
922
#ifdef WORDS_BIGENDIAN
 
923
    if (table->s->db_low_byte_first)
 
924
      int2store(to, val);
 
925
    else
 
926
#endif
 
927
      shortstore(to, val);
 
928
    return from + sizeof(val);
 
929
  }
 
930
};
 
931
 
 
932
class Field_medium :public Field_num {
 
933
public:
 
934
  Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
935
              uchar null_bit_arg,
 
936
              enum utype unireg_check_arg, const char *field_name_arg,
 
937
              bool zero_arg, bool unsigned_arg)
 
938
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
939
               unireg_check_arg, field_name_arg,
 
940
               0, zero_arg,unsigned_arg)
 
941
    {}
 
942
  enum Item_result result_type () const { return INT_RESULT; }
 
943
  enum_field_types type() const { return MYSQL_TYPE_INT24;}
 
944
  enum ha_base_keytype key_type() const
 
945
    { return unsigned_flag ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24; }
 
946
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
947
  int store(double nr);
 
948
  int store(longlong nr, bool unsigned_val);
 
949
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
 
950
  double val_real(void);
 
951
  longlong val_int(void);
 
952
  String *val_str(String*,String *);
 
953
  bool send_binary(Protocol *protocol);
 
954
  int cmp(const uchar *,const uchar *);
 
955
  void sort_string(uchar *buff,uint length);
 
956
  uint32 pack_length() const { return 3; }
 
957
  void sql_type(String &str) const;
 
958
  uint32 max_display_length() { return 8; }
 
959
 
 
960
  virtual uchar *pack(uchar* to, const uchar *from,
 
961
                      uint max_length, bool low_byte_first)
 
962
  {
 
963
    return Field::pack(to, from, max_length, low_byte_first);
 
964
  }
 
965
 
 
966
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
967
                              uint param_data, bool low_byte_first)
 
968
  {
 
969
    return Field::unpack(to, from, param_data, low_byte_first);
 
970
  }
 
971
};
 
972
 
 
973
 
 
974
class Field_long :public Field_num {
 
975
public:
 
976
  Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
977
             uchar null_bit_arg,
 
978
             enum utype unireg_check_arg, const char *field_name_arg,
 
979
             bool zero_arg, bool unsigned_arg)
 
980
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
981
               unireg_check_arg, field_name_arg,
 
982
               0, zero_arg,unsigned_arg)
 
983
    {}
 
984
  Field_long(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
985
             bool unsigned_arg)
 
986
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
 
987
               NONE, field_name_arg,0,0,unsigned_arg)
 
988
    {}
 
989
  enum Item_result result_type () const { return INT_RESULT; }
 
990
  enum_field_types type() const { return MYSQL_TYPE_LONG;}
 
991
  enum ha_base_keytype key_type() const
 
992
    { return unsigned_flag ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT; }
 
993
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
994
  int store(double nr);
 
995
  int store(longlong nr, bool unsigned_val);
 
996
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
 
997
  double val_real(void);
 
998
  longlong val_int(void);
 
999
  bool send_binary(Protocol *protocol);
 
1000
  String *val_str(String*,String *);
 
1001
  int cmp(const uchar *,const uchar *);
 
1002
  void sort_string(uchar *buff,uint length);
 
1003
  uint32 pack_length() const { return 4; }
 
1004
  void sql_type(String &str) const;
 
1005
  uint32 max_display_length() { return MY_INT32_NUM_DECIMAL_DIGITS; }
 
1006
  virtual uchar *pack(uchar* to, const uchar *from,
 
1007
                      uint max_length __attribute__((unused)),
 
1008
                      bool low_byte_first)
 
1009
  {
 
1010
    return pack_int32(to, from, low_byte_first);
 
1011
  }
 
1012
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
1013
                              uint param_data __attribute__((unused)),
 
1014
                              bool low_byte_first)
 
1015
  {
 
1016
    return unpack_int32(to, from, low_byte_first);
 
1017
  }
 
1018
};
 
1019
 
 
1020
 
 
1021
#ifdef HAVE_LONG_LONG
 
1022
class Field_longlong :public Field_num {
 
1023
public:
 
1024
  Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1025
              uchar null_bit_arg,
 
1026
              enum utype unireg_check_arg, const char *field_name_arg,
 
1027
              bool zero_arg, bool unsigned_arg)
 
1028
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1029
               unireg_check_arg, field_name_arg,
 
1030
               0, zero_arg,unsigned_arg)
 
1031
    {}
 
1032
  Field_longlong(uint32 len_arg,bool maybe_null_arg,
 
1033
                 const char *field_name_arg,
 
1034
                  bool unsigned_arg)
 
1035
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
 
1036
               NONE, field_name_arg,0,0,unsigned_arg)
 
1037
    {}
 
1038
  enum Item_result result_type () const { return INT_RESULT; }
 
1039
  enum_field_types type() const { return MYSQL_TYPE_LONGLONG;}
 
1040
  enum ha_base_keytype key_type() const
 
1041
    { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
 
1042
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1043
  int store(double nr);
 
1044
  int store(longlong nr, bool unsigned_val);
 
1045
  int reset(void)
 
1046
  {
 
1047
    ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
 
1048
    return 0;
 
1049
  }
 
1050
  double val_real(void);
 
1051
  longlong val_int(void);
 
1052
  String *val_str(String*,String *);
 
1053
  bool send_binary(Protocol *protocol);
 
1054
  int cmp(const uchar *,const uchar *);
 
1055
  void sort_string(uchar *buff,uint length);
 
1056
  uint32 pack_length() const { return 8; }
 
1057
  void sql_type(String &str) const;
 
1058
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1059
  uint32 max_display_length() { return 20; }
 
1060
  virtual uchar *pack(uchar* to, const uchar *from,
 
1061
                      uint max_length  __attribute__((unused)),
 
1062
                      bool low_byte_first)
 
1063
  {
 
1064
    return pack_int64(to, from, low_byte_first);
 
1065
  }
 
1066
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
1067
                              uint param_data __attribute__((unused)),
 
1068
                              bool low_byte_first)
 
1069
  {
 
1070
    return unpack_int64(to, from, low_byte_first);
 
1071
  }
 
1072
};
 
1073
#endif
 
1074
 
 
1075
 
 
1076
class Field_float :public Field_real {
 
1077
public:
 
1078
  Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1079
              uchar null_bit_arg,
 
1080
              enum utype unireg_check_arg, const char *field_name_arg,
 
1081
              uint8 dec_arg,bool zero_arg,bool unsigned_arg)
 
1082
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1083
                unireg_check_arg, field_name_arg,
 
1084
                dec_arg, zero_arg, unsigned_arg)
 
1085
    {}
 
1086
  Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
 
1087
              uint8 dec_arg)
 
1088
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
 
1089
                NONE, field_name_arg, dec_arg, 0, 0)
 
1090
    {}
 
1091
  enum_field_types type() const { return MYSQL_TYPE_FLOAT;}
 
1092
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
 
1093
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1094
  int store(double nr);
 
1095
  int store(longlong nr, bool unsigned_val);
 
1096
  int reset(void) { bzero(ptr,sizeof(float)); return 0; }
 
1097
  double val_real(void);
 
1098
  longlong val_int(void);
 
1099
  String *val_str(String*,String *);
 
1100
  bool send_binary(Protocol *protocol);
 
1101
  int cmp(const uchar *,const uchar *);
 
1102
  void sort_string(uchar *buff,uint length);
 
1103
  uint32 pack_length() const { return sizeof(float); }
 
1104
  uint row_pack_length() { return pack_length(); }
 
1105
  void sql_type(String &str) const;
 
1106
private:
 
1107
  int do_save_field_metadata(uchar *first_byte);
 
1108
};
 
1109
 
 
1110
 
 
1111
class Field_double :public Field_real {
 
1112
public:
 
1113
  Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1114
               uchar null_bit_arg,
 
1115
               enum utype unireg_check_arg, const char *field_name_arg,
 
1116
               uint8 dec_arg,bool zero_arg,bool unsigned_arg)
 
1117
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1118
                unireg_check_arg, field_name_arg,
 
1119
                dec_arg, zero_arg, unsigned_arg)
 
1120
    {}
 
1121
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
 
1122
               uint8 dec_arg)
 
1123
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
 
1124
                NONE, field_name_arg, dec_arg, 0, 0)
 
1125
    {}
 
1126
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
 
1127
               uint8 dec_arg, my_bool not_fixed_arg)
 
1128
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
 
1129
                NONE, field_name_arg, dec_arg, 0, 0)
 
1130
    {not_fixed= not_fixed_arg; }
 
1131
  enum_field_types type() const { return MYSQL_TYPE_DOUBLE;}
 
1132
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
 
1133
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1134
  int  store(double nr);
 
1135
  int  store(longlong nr, bool unsigned_val);
 
1136
  int reset(void) { bzero(ptr,sizeof(double)); return 0; }
 
1137
  double val_real(void);
 
1138
  longlong val_int(void);
 
1139
  String *val_str(String*,String *);
 
1140
  bool send_binary(Protocol *protocol);
 
1141
  int cmp(const uchar *,const uchar *);
 
1142
  void sort_string(uchar *buff,uint length);
 
1143
  uint32 pack_length() const { return sizeof(double); }
 
1144
  uint row_pack_length() { return pack_length(); }
 
1145
  void sql_type(String &str) const;
 
1146
private:
 
1147
  int do_save_field_metadata(uchar *first_byte);
 
1148
};
 
1149
 
 
1150
 
 
1151
/* Everything saved in this will disappear. It will always return NULL */
 
1152
 
 
1153
class Field_null :public Field_str {
 
1154
  static uchar null[1];
 
1155
public:
 
1156
  Field_null(uchar *ptr_arg, uint32 len_arg,
 
1157
             enum utype unireg_check_arg, const char *field_name_arg,
 
1158
             CHARSET_INFO *cs)
 
1159
    :Field_str(ptr_arg, len_arg, null, 1,
 
1160
               unireg_check_arg, field_name_arg, cs)
 
1161
    {}
 
1162
  enum_field_types type() const { return MYSQL_TYPE_NULL;}
 
1163
  int  store(const char *to, uint length, CHARSET_INFO *cs)
 
1164
  { null[0]=1; return 0; }
 
1165
  int store(double nr)   { null[0]=1; return 0; }
 
1166
  int store(longlong nr, bool unsigned_val) { null[0]=1; return 0; }
 
1167
  int store_decimal(const my_decimal *d)  { null[0]=1; return 0; }
 
1168
  int reset(void)         { return 0; }
 
1169
  double val_real(void)         { return 0.0;}
 
1170
  longlong val_int(void)        { return 0;}
 
1171
  my_decimal *val_decimal(my_decimal *) { return 0; }
 
1172
  String *val_str(String *value,String *value2)
 
1173
  { value2->length(0); return value2;}
 
1174
  int cmp(const uchar *a, const uchar *b) { return 0;}
 
1175
  void sort_string(uchar *buff, uint length)  {}
 
1176
  uint32 pack_length() const { return 0; }
 
1177
  void sql_type(String &str) const;
 
1178
  uint size_of() const { return sizeof(*this); }
 
1179
  uint32 max_display_length() { return 4; }
 
1180
};
 
1181
 
 
1182
 
 
1183
class Field_timestamp :public Field_str {
 
1184
public:
 
1185
  Field_timestamp(uchar *ptr_arg, uint32 len_arg,
 
1186
                  uchar *null_ptr_arg, uchar null_bit_arg,
 
1187
                  enum utype unireg_check_arg, const char *field_name_arg,
 
1188
                  TABLE_SHARE *share, CHARSET_INFO *cs);
 
1189
  Field_timestamp(bool maybe_null_arg, const char *field_name_arg,
 
1190
                  CHARSET_INFO *cs);
 
1191
  enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;}
 
1192
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
 
1193
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1194
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1195
  int  store(double nr);
 
1196
  int  store(longlong nr, bool unsigned_val);
 
1197
  int  reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
 
1198
  double val_real(void);
 
1199
  longlong val_int(void);
 
1200
  String *val_str(String*,String *);
 
1201
  bool send_binary(Protocol *protocol);
 
1202
  int cmp(const uchar *,const uchar *);
 
1203
  void sort_string(uchar *buff,uint length);
 
1204
  uint32 pack_length() const { return 4; }
 
1205
  void sql_type(String &str) const;
 
1206
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1207
  bool zero_pack() const { return 0; }
 
1208
  void set_time();
 
1209
  virtual void set_default()
 
1210
  {
 
1211
    if (table->timestamp_field == this &&
 
1212
        unireg_check != TIMESTAMP_UN_FIELD)
 
1213
      set_time();
 
1214
    else
 
1215
      Field::set_default();
 
1216
  }
 
1217
  /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
 
1218
  inline long get_timestamp(my_bool *null_value)
 
1219
  {
 
1220
    if ((*null_value= is_null()))
 
1221
      return 0;
 
1222
#ifdef WORDS_BIGENDIAN
 
1223
    if (table && table->s->db_low_byte_first)
 
1224
      return sint4korr(ptr);
 
1225
#endif
 
1226
    long tmp;
 
1227
    longget(tmp,ptr);
 
1228
    return tmp;
 
1229
  }
 
1230
  inline void store_timestamp(my_time_t timestamp)
 
1231
  {
 
1232
#ifdef WORDS_BIGENDIAN
 
1233
    if (table && table->s->db_low_byte_first)
 
1234
    {
 
1235
      int4store(ptr,timestamp);
 
1236
    }
 
1237
    else
 
1238
#endif
 
1239
      longstore(ptr,(uint32) timestamp);
 
1240
  }
 
1241
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
1242
  bool get_time(MYSQL_TIME *ltime);
 
1243
  timestamp_auto_set_type get_auto_set_type() const;
 
1244
  uchar *pack(uchar *to, const uchar *from,
 
1245
              uint max_length __attribute__((unused)), bool low_byte_first)
 
1246
  {
 
1247
    return pack_int32(to, from, low_byte_first);
 
1248
  }
 
1249
  const uchar *unpack(uchar* to, const uchar *from,
 
1250
                      uint param_data __attribute__((unused)),
 
1251
                      bool low_byte_first)
 
1252
  {
 
1253
    return unpack_int32(to, from, low_byte_first);
 
1254
  }
 
1255
};
 
1256
 
 
1257
 
 
1258
class Field_year :public Field_tiny {
 
1259
public:
 
1260
  Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1261
             uchar null_bit_arg,
 
1262
             enum utype unireg_check_arg, const char *field_name_arg)
 
1263
    :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1264
                unireg_check_arg, field_name_arg, 1, 1)
 
1265
    {}
 
1266
  enum_field_types type() const { return MYSQL_TYPE_YEAR;}
 
1267
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1268
  int  store(double nr);
 
1269
  int  store(longlong nr, bool unsigned_val);
 
1270
  double val_real(void);
 
1271
  longlong val_int(void);
 
1272
  String *val_str(String*,String *);
 
1273
  bool send_binary(Protocol *protocol);
 
1274
  void sql_type(String &str) const;
 
1275
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1276
};
 
1277
 
 
1278
 
 
1279
class Field_date :public Field_str {
 
1280
public:
 
1281
  Field_date(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1282
             enum utype unireg_check_arg, const char *field_name_arg,
 
1283
             CHARSET_INFO *cs)
 
1284
    :Field_str(ptr_arg, MAX_DATE_WIDTH, null_ptr_arg, null_bit_arg,
 
1285
               unireg_check_arg, field_name_arg, cs)
 
1286
    {}
 
1287
  Field_date(bool maybe_null_arg, const char *field_name_arg,
 
1288
             CHARSET_INFO *cs)
 
1289
    :Field_str((uchar*) 0, MAX_DATE_WIDTH, maybe_null_arg ? (uchar*) "": 0,0,
 
1290
               NONE, field_name_arg, cs) {}
 
1291
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
 
1292
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
 
1293
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1294
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1295
  int store(double nr);
 
1296
  int store(longlong nr, bool unsigned_val);
 
1297
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
 
1298
  double val_real(void);
 
1299
  longlong val_int(void);
 
1300
  String *val_str(String*,String *);
 
1301
  bool get_time(MYSQL_TIME *ltime);
 
1302
  bool send_binary(Protocol *protocol);
 
1303
  int cmp(const uchar *,const uchar *);
 
1304
  void sort_string(uchar *buff,uint length);
 
1305
  uint32 pack_length() const { return 4; }
 
1306
  void sql_type(String &str) const;
 
1307
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1308
  bool zero_pack() const { return 1; }
 
1309
  uchar *pack(uchar* to, const uchar *from,
 
1310
              uint max_length __attribute__((unused)), bool low_byte_first)
 
1311
  {
 
1312
    return pack_int32(to, from, low_byte_first);
 
1313
  }
 
1314
  const uchar *unpack(uchar* to, const uchar *from,
 
1315
                      uint param_data __attribute__((unused)),
 
1316
                      bool low_byte_first)
 
1317
  {
 
1318
    return unpack_int32(to, from, low_byte_first);
 
1319
  }
 
1320
};
 
1321
 
 
1322
 
 
1323
class Field_newdate :public Field_str {
 
1324
public:
 
1325
  Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1326
                enum utype unireg_check_arg, const char *field_name_arg,
 
1327
                CHARSET_INFO *cs)
 
1328
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
 
1329
               unireg_check_arg, field_name_arg, cs)
 
1330
    {}
 
1331
  Field_newdate(bool maybe_null_arg, const char *field_name_arg,
 
1332
                CHARSET_INFO *cs)
 
1333
    :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
 
1334
               NONE, field_name_arg, cs) {}
 
1335
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
 
1336
  enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; }
 
1337
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
 
1338
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1339
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1340
  int  store(double nr);
 
1341
  int  store(longlong nr, bool unsigned_val);
 
1342
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
 
1343
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
 
1344
  double val_real(void);
 
1345
  longlong val_int(void);
 
1346
  String *val_str(String*,String *);
 
1347
  bool send_binary(Protocol *protocol);
 
1348
  int cmp(const uchar *,const uchar *);
 
1349
  void sort_string(uchar *buff,uint length);
 
1350
  uint32 pack_length() const { return 3; }
 
1351
  void sql_type(String &str) const;
 
1352
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1353
  bool zero_pack() const { return 1; }
 
1354
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
1355
  bool get_time(MYSQL_TIME *ltime);
 
1356
};
 
1357
 
 
1358
 
 
1359
class Field_time :public Field_str {
 
1360
public:
 
1361
  Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1362
             enum utype unireg_check_arg, const char *field_name_arg,
 
1363
             CHARSET_INFO *cs)
 
1364
    :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
 
1365
               unireg_check_arg, field_name_arg, cs)
 
1366
    {}
 
1367
  Field_time(bool maybe_null_arg, const char *field_name_arg,
 
1368
             CHARSET_INFO *cs)
 
1369
    :Field_str((uchar*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
 
1370
               NONE, field_name_arg, cs) {}
 
1371
  enum_field_types type() const { return MYSQL_TYPE_TIME;}
 
1372
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
 
1373
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1374
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
 
1375
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1376
  int store(double nr);
 
1377
  int store(longlong nr, bool unsigned_val);
 
1378
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
 
1379
  double val_real(void);
 
1380
  longlong val_int(void);
 
1381
  String *val_str(String*,String *);
 
1382
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
 
1383
  bool send_binary(Protocol *protocol);
 
1384
  bool get_time(MYSQL_TIME *ltime);
 
1385
  int cmp(const uchar *,const uchar *);
 
1386
  void sort_string(uchar *buff,uint length);
 
1387
  uint32 pack_length() const { return 3; }
 
1388
  void sql_type(String &str) const;
 
1389
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1390
  bool zero_pack() const { return 1; }
 
1391
};
 
1392
 
 
1393
 
 
1394
class Field_datetime :public Field_str {
 
1395
public:
 
1396
  Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1397
                 enum utype unireg_check_arg, const char *field_name_arg,
 
1398
                 CHARSET_INFO *cs)
 
1399
    :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
 
1400
               unireg_check_arg, field_name_arg, cs)
 
1401
    {}
 
1402
  Field_datetime(bool maybe_null_arg, const char *field_name_arg,
 
1403
                 CHARSET_INFO *cs)
 
1404
    :Field_str((uchar*) 0, MAX_DATETIME_WIDTH, maybe_null_arg ? (uchar*) "": 0,0,
 
1405
               NONE, field_name_arg, cs) {}
 
1406
  enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
 
1407
#ifdef HAVE_LONG_LONG
 
1408
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
 
1409
#endif
 
1410
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1411
  uint decimals() const { return DATETIME_DEC; }
 
1412
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1413
  int  store(double nr);
 
1414
  int  store(longlong nr, bool unsigned_val);
 
1415
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
 
1416
  int reset(void)
 
1417
  {
 
1418
    ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
 
1419
    return 0;
 
1420
  }
 
1421
  double val_real(void);
 
1422
  longlong val_int(void);
 
1423
  String *val_str(String*,String *);
 
1424
  bool send_binary(Protocol *protocol);
 
1425
  int cmp(const uchar *,const uchar *);
 
1426
  void sort_string(uchar *buff,uint length);
 
1427
  uint32 pack_length() const { return 8; }
 
1428
  void sql_type(String &str) const;
 
1429
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1430
  bool zero_pack() const { return 1; }
 
1431
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
1432
  bool get_time(MYSQL_TIME *ltime);
 
1433
  uchar *pack(uchar* to, const uchar *from,
 
1434
              uint max_length __attribute__((unused)), bool low_byte_first)
 
1435
  {
 
1436
    return pack_int64(to, from, low_byte_first);
 
1437
  }
 
1438
  const uchar *unpack(uchar* to, const uchar *from,
 
1439
                      uint param_data __attribute__((unused)),
 
1440
                      bool low_byte_first)
 
1441
  {
 
1442
    return unpack_int64(to, from, low_byte_first);
 
1443
  }
 
1444
};
 
1445
 
 
1446
 
 
1447
class Field_string :public Field_longstr {
 
1448
public:
 
1449
  bool can_alter_field_type;
 
1450
  Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
 
1451
               uchar null_bit_arg,
 
1452
               enum utype unireg_check_arg, const char *field_name_arg,
 
1453
               CHARSET_INFO *cs)
 
1454
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1455
                   unireg_check_arg, field_name_arg, cs),
 
1456
     can_alter_field_type(1) {};
 
1457
  Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
1458
               CHARSET_INFO *cs)
 
1459
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1460
                   NONE, field_name_arg, cs),
 
1461
     can_alter_field_type(1) {};
 
1462
 
 
1463
  enum_field_types type() const
 
1464
  {
 
1465
    return ((can_alter_field_type && orig_table &&
 
1466
             orig_table->s->db_create_options & HA_OPTION_PACK_RECORD &&
 
1467
             field_length >= 4) &&
 
1468
            orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR ?
 
1469
            MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING);
 
1470
  }
 
1471
  enum ha_base_keytype key_type() const
 
1472
    { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
 
1473
  bool zero_pack() const { return 0; }
 
1474
  int reset(void)
 
1475
  {
 
1476
    charset()->cset->fill(charset(),(char*) ptr, field_length,
 
1477
                          (has_charset() ? ' ' : 0));
 
1478
    return 0;
 
1479
  }
 
1480
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1481
  int store(longlong nr, bool unsigned_val);
 
1482
  int store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
 
1483
  double val_real(void);
 
1484
  longlong val_int(void);
 
1485
  String *val_str(String*,String *);
 
1486
  my_decimal *val_decimal(my_decimal *);
 
1487
  int cmp(const uchar *,const uchar *);
 
1488
  void sort_string(uchar *buff,uint length);
 
1489
  void sql_type(String &str) const;
 
1490
  virtual uchar *pack(uchar *to, const uchar *from,
 
1491
                      uint max_length, bool low_byte_first);
 
1492
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
1493
                              uint param_data, bool low_byte_first);
 
1494
  uint pack_length_from_metadata(uint field_metadata)
 
1495
  {
 
1496
    DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
 
1497
    if (field_metadata == 0)
 
1498
      return row_pack_length();
 
1499
    return (((field_metadata >> 4) & 0x300) ^ 0x300) + (field_metadata & 0x00ff);
 
1500
  }
 
1501
  int compatible_field_size(uint field_metadata,
 
1502
                            const Relay_log_info *rli);
 
1503
  uint row_pack_length() { return (field_length + 1); }
 
1504
  int pack_cmp(const uchar *a,const uchar *b,uint key_length,
 
1505
               my_bool insert_or_update);
 
1506
  int pack_cmp(const uchar *b,uint key_length,my_bool insert_or_update);
 
1507
  uint packed_col_length(const uchar *to, uint length);
 
1508
  uint max_packed_col_length(uint max_length);
 
1509
  uint size_of() const { return sizeof(*this); }
 
1510
  enum_field_types real_type() const { return MYSQL_TYPE_STRING; }
 
1511
  bool has_charset(void) const
 
1512
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
 
1513
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
 
1514
  virtual uint get_key_image(uchar *buff,uint length, imagetype type);
 
1515
private:
 
1516
  int do_save_field_metadata(uchar *first_byte);
 
1517
};
 
1518
 
 
1519
 
 
1520
class Field_varstring :public Field_longstr {
 
1521
public:
 
1522
  /*
 
1523
    The maximum space available in a Field_varstring, in bytes. See
 
1524
    length_bytes.
 
1525
  */
 
1526
  static const uint MAX_SIZE;
 
1527
  /* Store number of bytes used to store length (1 or 2) */
 
1528
  uint32 length_bytes;
 
1529
  Field_varstring(uchar *ptr_arg,
 
1530
                  uint32 len_arg, uint length_bytes_arg,
 
1531
                  uchar *null_ptr_arg, uchar null_bit_arg,
 
1532
                  enum utype unireg_check_arg, const char *field_name_arg,
 
1533
                  TABLE_SHARE *share, CHARSET_INFO *cs)
 
1534
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1535
                   unireg_check_arg, field_name_arg, cs),
 
1536
     length_bytes(length_bytes_arg)
 
1537
  {
 
1538
    share->varchar_fields++;
 
1539
  }
 
1540
  Field_varstring(uint32 len_arg,bool maybe_null_arg,
 
1541
                  const char *field_name_arg,
 
1542
                  TABLE_SHARE *share, CHARSET_INFO *cs)
 
1543
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1544
                   NONE, field_name_arg, cs),
 
1545
     length_bytes(len_arg < 256 ? 1 :2)
 
1546
  {
 
1547
    share->varchar_fields++;
 
1548
  }
 
1549
 
 
1550
  enum_field_types type() const { return MYSQL_TYPE_VARCHAR; }
 
1551
  enum ha_base_keytype key_type() const;
 
1552
  uint row_pack_length() { return field_length; }
 
1553
  bool zero_pack() const { return 0; }
 
1554
  int  reset(void) { bzero(ptr,field_length+length_bytes); return 0; }
 
1555
  uint32 pack_length() const { return (uint32) field_length+length_bytes; }
 
1556
  uint32 key_length() const { return (uint32) field_length; }
 
1557
  uint32 sort_length() const
 
1558
  {
 
1559
    return (uint32) field_length + (field_charset == &my_charset_bin ?
 
1560
                                    length_bytes : 0);
 
1561
  }
 
1562
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1563
  int  store(longlong nr, bool unsigned_val);
 
1564
  int  store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
 
1565
  double val_real(void);
 
1566
  longlong val_int(void);
 
1567
  String *val_str(String*,String *);
 
1568
  my_decimal *val_decimal(my_decimal *);
 
1569
  int cmp_max(const uchar *, const uchar *, uint max_length);
 
1570
  int cmp(const uchar *a,const uchar *b)
 
1571
  {
 
1572
    return cmp_max(a, b, ~0L);
 
1573
  }
 
1574
  void sort_string(uchar *buff,uint length);
 
1575
  uint get_key_image(uchar *buff,uint length, imagetype type);
 
1576
  void set_key_image(const uchar *buff,uint length);
 
1577
  void sql_type(String &str) const;
 
1578
  virtual uchar *pack(uchar *to, const uchar *from,
 
1579
                      uint max_length, bool low_byte_first);
 
1580
  uchar *pack_key(uchar *to, const uchar *from, uint max_length, bool low_byte_first);
 
1581
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
 
1582
                                 uint max_length, bool low_byte_first);
 
1583
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
1584
                              uint param_data, bool low_byte_first);
 
1585
  const uchar *unpack_key(uchar* to, const uchar *from,
 
1586
                          uint max_length, bool low_byte_first);
 
1587
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
 
1588
               my_bool insert_or_update);
 
1589
  int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
 
1590
  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
 
1591
  int key_cmp(const uchar *,const uchar*);
 
1592
  int key_cmp(const uchar *str, uint length);
 
1593
  uint packed_col_length(const uchar *to, uint length);
 
1594
  uint max_packed_col_length(uint max_length);
 
1595
  uint32 data_length();
 
1596
  uint size_of() const { return sizeof(*this); }
 
1597
  enum_field_types real_type() const { return MYSQL_TYPE_VARCHAR; }
 
1598
  bool has_charset(void) const
 
1599
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
 
1600
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
 
1601
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
1602
                       uchar *new_ptr, uchar *new_null_ptr,
 
1603
                       uint new_null_bit);
 
1604
  uint is_equal(Create_field *new_field);
 
1605
  void hash(ulong *nr, ulong *nr2);
 
1606
private:
 
1607
  int do_save_field_metadata(uchar *first_byte);
 
1608
};
 
1609
 
 
1610
 
 
1611
class Field_blob :public Field_longstr {
 
1612
protected:
 
1613
  /**
 
1614
    The number of bytes used to represent the length of the blob.
 
1615
  */
 
1616
  uint packlength;
 
1617
  
 
1618
  /**
 
1619
    The 'value'-object is a cache fronting the storage engine.
 
1620
  */
 
1621
  String value;
 
1622
  
 
1623
public:
 
1624
  Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1625
             enum utype unireg_check_arg, const char *field_name_arg,
 
1626
             TABLE_SHARE *share, uint blob_pack_length, CHARSET_INFO *cs);
 
1627
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
1628
             CHARSET_INFO *cs)
 
1629
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1630
                   NONE, field_name_arg, cs),
 
1631
    packlength(4)
 
1632
  {
 
1633
    flags|= BLOB_FLAG;
 
1634
  }
 
1635
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
1636
             CHARSET_INFO *cs, bool set_packlength)
 
1637
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1638
                   NONE, field_name_arg, cs)
 
1639
  {
 
1640
    flags|= BLOB_FLAG;
 
1641
    packlength= 4;
 
1642
    if (set_packlength)
 
1643
    {
 
1644
      uint32 l_char_length= len_arg/cs->mbmaxlen;
 
1645
      packlength= l_char_length <= 255 ? 1 :
 
1646
                  l_char_length <= 65535 ? 2 :
 
1647
                  l_char_length <= 16777215 ? 3 : 4;
 
1648
    }
 
1649
  }
 
1650
  Field_blob(uint32 packlength_arg)
 
1651
    :Field_longstr((uchar*) 0, 0, (uchar*) "", 0, NONE, "temp", system_charset_info),
 
1652
    packlength(packlength_arg) {}
 
1653
  enum_field_types type() const { return MYSQL_TYPE_BLOB;}
 
1654
  enum ha_base_keytype key_type() const
 
1655
    { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
 
1656
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1657
  int  store(double nr);
 
1658
  int  store(longlong nr, bool unsigned_val);
 
1659
  double val_real(void);
 
1660
  longlong val_int(void);
 
1661
  String *val_str(String*,String *);
 
1662
  my_decimal *val_decimal(my_decimal *);
 
1663
  int cmp_max(const uchar *, const uchar *, uint max_length);
 
1664
  int cmp(const uchar *a,const uchar *b)
 
1665
    { return cmp_max(a, b, ~0L); }
 
1666
  int cmp(const uchar *a, uint32 a_length, const uchar *b, uint32 b_length);
 
1667
  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
 
1668
  int key_cmp(const uchar *,const uchar*);
 
1669
  int key_cmp(const uchar *str, uint length);
 
1670
  uint32 key_length() const { return 0; }
 
1671
  void sort_string(uchar *buff,uint length);
 
1672
  uint32 pack_length() const
 
1673
  { return (uint32) (packlength+table->s->blob_ptr_size); }
 
1674
 
 
1675
  /**
 
1676
     Return the packed length without the pointer size added. 
 
1677
 
 
1678
     This is used to determine the size of the actual data in the row
 
1679
     buffer.
 
1680
 
 
1681
     @returns The length of the raw data itself without the pointer.
 
1682
  */
 
1683
  uint32 pack_length_no_ptr() const
 
1684
  { return (uint32) (packlength); }
 
1685
  uint row_pack_length() { return pack_length_no_ptr(); }
 
1686
  uint32 sort_length() const;
 
1687
  virtual uint32 max_data_length() const
 
1688
  {
 
1689
    return (uint32) (((ulonglong) 1 << (packlength*8)) -1);
 
1690
  }
 
1691
  int reset(void) { bzero(ptr, packlength+sizeof(uchar*)); return 0; }
 
1692
  void reset_fields() { bzero((uchar*) &value,sizeof(value)); }
 
1693
  uint32 get_field_buffer_size(void) { return value.alloced_length(); }
 
1694
#ifndef WORDS_BIGENDIAN
 
1695
  static
 
1696
#endif
 
1697
  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number, bool low_byte_first);
 
1698
  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number)
 
1699
  {
 
1700
    store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
 
1701
  }
 
1702
  inline void store_length(uint32 number)
 
1703
  {
 
1704
    store_length(ptr, packlength, number);
 
1705
  }
 
1706
 
 
1707
  /**
 
1708
     Return the packed length plus the length of the data. 
 
1709
 
 
1710
     This is used to determine the size of the data plus the 
 
1711
     packed length portion in the row data.
 
1712
 
 
1713
     @returns The length in the row plus the size of the data.
 
1714
  */
 
1715
  uint32 get_packed_size(const uchar *ptr_arg, bool low_byte_first)
 
1716
    {return packlength + get_length(ptr_arg, packlength, low_byte_first);}
 
1717
 
 
1718
  inline uint32 get_length(uint row_offset= 0)
 
1719
  { return get_length(ptr+row_offset, this->packlength, table->s->db_low_byte_first); }
 
1720
  uint32 get_length(const uchar *ptr, uint packlength, bool low_byte_first);
 
1721
  uint32 get_length(const uchar *ptr_arg)
 
1722
  { return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first); }
 
1723
  void put_length(uchar *pos, uint32 length);
 
1724
  inline void get_ptr(uchar **str)
 
1725
    {
 
1726
      memcpy_fixed((uchar*) str,ptr+packlength,sizeof(uchar*));
 
1727
    }
 
1728
  inline void get_ptr(uchar **str, uint row_offset)
 
1729
    {
 
1730
      memcpy_fixed((uchar*) str,ptr+packlength+row_offset,sizeof(char*));
 
1731
    }
 
1732
  inline void set_ptr(uchar *length, uchar *data)
 
1733
    {
 
1734
      memcpy(ptr,length,packlength);
 
1735
      memcpy_fixed(ptr+packlength,&data,sizeof(char*));
 
1736
    }
 
1737
  void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, uchar *data)
 
1738
    {
 
1739
      uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
 
1740
      store_length(ptr_ofs, packlength, length);
 
1741
      memcpy_fixed(ptr_ofs+packlength,&data,sizeof(char*));
 
1742
    }
 
1743
  inline void set_ptr(uint32 length, uchar *data)
 
1744
    {
 
1745
      set_ptr_offset(0, length, data);
 
1746
    }
 
1747
  uint get_key_image(uchar *buff,uint length, imagetype type);
 
1748
  void set_key_image(const uchar *buff,uint length);
 
1749
  void sql_type(String &str) const;
 
1750
  inline bool copy()
 
1751
  {
 
1752
    uchar *tmp;
 
1753
    get_ptr(&tmp);
 
1754
    if (value.copy((char*) tmp, get_length(), charset()))
 
1755
    {
 
1756
      Field_blob::reset();
 
1757
      return 1;
 
1758
    }
 
1759
    tmp=(uchar*) value.ptr();
 
1760
    memcpy_fixed(ptr+packlength,&tmp,sizeof(char*));
 
1761
    return 0;
 
1762
  }
 
1763
  virtual uchar *pack(uchar *to, const uchar *from,
 
1764
                      uint max_length, bool low_byte_first);
 
1765
  uchar *pack_key(uchar *to, const uchar *from,
 
1766
                  uint max_length, bool low_byte_first);
 
1767
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
 
1768
                                 uint max_length, bool low_byte_first);
 
1769
  virtual const uchar *unpack(uchar *to, const uchar *from,
 
1770
                              uint param_data, bool low_byte_first);
 
1771
  const uchar *unpack_key(uchar* to, const uchar *from,
 
1772
                          uint max_length, bool low_byte_first);
 
1773
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
 
1774
               my_bool insert_or_update);
 
1775
  int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
 
1776
  uint packed_col_length(const uchar *col_ptr, uint length);
 
1777
  uint max_packed_col_length(uint max_length);
 
1778
  void free() { value.free(); }
 
1779
  inline void clear_temporary() { bzero((uchar*) &value,sizeof(value)); }
 
1780
  friend int field_conv(Field *to,Field *from);
 
1781
  uint size_of() const { return sizeof(*this); }
 
1782
  bool has_charset(void) const
 
1783
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
 
1784
  uint32 max_display_length();
 
1785
  uint is_equal(Create_field *new_field);
 
1786
  inline bool in_read_set() { return bitmap_is_set(table->read_set, field_index); }
 
1787
  inline bool in_write_set() { return bitmap_is_set(table->write_set, field_index); }
 
1788
private:
 
1789
  int do_save_field_metadata(uchar *first_byte);
 
1790
};
 
1791
 
 
1792
 
 
1793
#ifdef HAVE_SPATIAL
 
1794
class Field_geom :public Field_blob {
 
1795
public:
 
1796
  enum geometry_type geom_type;
 
1797
 
 
1798
  Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
 
1799
             enum utype unireg_check_arg, const char *field_name_arg,
 
1800
             TABLE_SHARE *share, uint blob_pack_length,
 
1801
             enum geometry_type geom_type_arg)
 
1802
     :Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, unireg_check_arg, 
 
1803
                 field_name_arg, share, blob_pack_length, &my_charset_bin)
 
1804
  { geom_type= geom_type_arg; }
 
1805
  Field_geom(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
1806
             TABLE_SHARE *share, enum geometry_type geom_type_arg)
 
1807
    :Field_blob(len_arg, maybe_null_arg, field_name_arg, &my_charset_bin)
 
1808
  { geom_type= geom_type_arg; }
 
1809
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_VARBINARY2; }
 
1810
  enum_field_types type() const { return MYSQL_TYPE_GEOMETRY; }
 
1811
  void sql_type(String &str) const;
 
1812
  int  store(const char *to, uint length, CHARSET_INFO *charset);
 
1813
  int  store(double nr);
 
1814
  int  store(longlong nr, bool unsigned_val);
 
1815
  int  store_decimal(const my_decimal *);
 
1816
  uint size_of() const { return sizeof(*this); }
 
1817
  int  reset(void) { return !maybe_null() || Field_blob::reset(); }
 
1818
  geometry_type get_geometry_type() { return geom_type; };
 
1819
};
 
1820
#endif /*HAVE_SPATIAL*/
 
1821
 
 
1822
 
 
1823
class Field_enum :public Field_str {
 
1824
protected:
 
1825
  uint packlength;
 
1826
public:
 
1827
  TYPELIB *typelib;
 
1828
  Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1829
             uchar null_bit_arg,
 
1830
             enum utype unireg_check_arg, const char *field_name_arg,
 
1831
             uint packlength_arg,
 
1832
             TYPELIB *typelib_arg,
 
1833
             CHARSET_INFO *charset_arg)
 
1834
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1835
               unireg_check_arg, field_name_arg, charset_arg),
 
1836
    packlength(packlength_arg),typelib(typelib_arg)
 
1837
  {
 
1838
      flags|=ENUM_FLAG;
 
1839
  }
 
1840
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
 
1841
  enum_field_types type() const { return MYSQL_TYPE_STRING; }
 
1842
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1843
  enum Item_result cast_to_int_type () const { return INT_RESULT; }
 
1844
  enum ha_base_keytype key_type() const;
 
1845
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1846
  int  store(double nr);
 
1847
  int  store(longlong nr, bool unsigned_val);
 
1848
  double val_real(void);
 
1849
  longlong val_int(void);
 
1850
  String *val_str(String*,String *);
 
1851
  int cmp(const uchar *,const uchar *);
 
1852
  void sort_string(uchar *buff,uint length);
 
1853
  uint32 pack_length() const { return (uint32) packlength; }
 
1854
  void store_type(ulonglong value);
 
1855
  void sql_type(String &str) const;
 
1856
  uint size_of() const { return sizeof(*this); }
 
1857
  enum_field_types real_type() const { return MYSQL_TYPE_ENUM; }
 
1858
  uint pack_length_from_metadata(uint field_metadata)
 
1859
  { return (field_metadata & 0x00ff); }
 
1860
  uint row_pack_length() { return pack_length(); }
 
1861
  virtual bool zero_pack() const { return 0; }
 
1862
  bool optimize_range(uint idx, uint part) { return 0; }
 
1863
  bool eq_def(Field *field);
 
1864
  bool has_charset(void) const { return TRUE; }
 
1865
  /* enum and set are sorted as integers */
 
1866
  CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
 
1867
private:
 
1868
  int do_save_field_metadata(uchar *first_byte);
 
1869
  uint is_equal(Create_field *new_field);
 
1870
};
 
1871
 
 
1872
 
 
1873
class Field_set :public Field_enum {
 
1874
public:
 
1875
  Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1876
            uchar null_bit_arg,
 
1877
            enum utype unireg_check_arg, const char *field_name_arg,
 
1878
            uint32 packlength_arg,
 
1879
            TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
 
1880
    :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1881
                    unireg_check_arg, field_name_arg,
 
1882
                packlength_arg,
 
1883
                typelib_arg,charset_arg)
 
1884
    {
 
1885
      flags=(flags & ~ENUM_FLAG) | SET_FLAG;
 
1886
    }
 
1887
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1888
  int  store(double nr) { return Field_set::store((longlong) nr, FALSE); }
 
1889
  int  store(longlong nr, bool unsigned_val);
 
1890
 
 
1891
  virtual bool zero_pack() const { return 1; }
 
1892
  String *val_str(String*,String *);
 
1893
  void sql_type(String &str) const;
 
1894
  enum_field_types real_type() const { return MYSQL_TYPE_SET; }
 
1895
  bool has_charset(void) const { return TRUE; }
 
1896
};
 
1897
 
 
1898
 
 
1899
/*
 
1900
  Note:
 
1901
    To use Field_bit::cmp_binary() you need to copy the bits stored in
 
1902
    the beginning of the record (the NULL bytes) to each memory you
 
1903
    want to compare (where the arguments point).
 
1904
 
 
1905
    This is the reason:
 
1906
    - Field_bit::cmp_binary() is only implemented in the base class
 
1907
      (Field::cmp_binary()).
 
1908
    - Field::cmp_binary() currenly use pack_length() to calculate how
 
1909
      long the data is.
 
1910
    - pack_length() includes size of the bits stored in the NULL bytes
 
1911
      of the record.
 
1912
*/
 
1913
class Field_bit :public Field {
 
1914
public:
 
1915
  uchar *bit_ptr;     // position in record where 'uneven' bits store
 
1916
  uchar bit_ofs;      // offset to 'uneven' high bits
 
1917
  uint bit_len;       // number of 'uneven' high bits
 
1918
  uint bytes_in_rec;
 
1919
  Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1920
            uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
 
1921
            enum utype unireg_check_arg, const char *field_name_arg);
 
1922
  enum_field_types type() const { return MYSQL_TYPE_BIT; }
 
1923
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
 
1924
  uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
 
1925
  uint32 max_data_length() const { return (field_length + 7) / 8; }
 
1926
  uint32 max_display_length() { return field_length; }
 
1927
  uint size_of() const { return sizeof(*this); }
 
1928
  Item_result result_type () const { return INT_RESULT; }
 
1929
  int reset(void) { 
 
1930
    bzero(ptr, bytes_in_rec); 
 
1931
    if (bit_ptr && (bit_len > 0))  // reset odd bits among null bits
 
1932
      clr_rec_bits(bit_ptr, bit_ofs, bit_len);
 
1933
    return 0; 
 
1934
  }
 
1935
  int store(const char *to, uint length, CHARSET_INFO *charset);
 
1936
  int store(double nr);
 
1937
  int store(longlong nr, bool unsigned_val);
 
1938
  int store_decimal(const my_decimal *);
 
1939
  double val_real(void);
 
1940
  longlong val_int(void);
 
1941
  String *val_str(String*, String *);
 
1942
  virtual bool str_needs_quotes() { return TRUE; }
 
1943
  my_decimal *val_decimal(my_decimal *);
 
1944
  int cmp(const uchar *a, const uchar *b)
 
1945
  { 
 
1946
    DBUG_ASSERT(ptr == a);
 
1947
    return Field_bit::key_cmp(b, bytes_in_rec+test(bit_len));
 
1948
  }
 
1949
  int cmp_binary_offset(uint row_offset)
 
1950
  { return cmp_offset(row_offset); }
 
1951
  int cmp_max(const uchar *a, const uchar *b, uint max_length);
 
1952
  int key_cmp(const uchar *a, const uchar *b)
 
1953
  { return cmp_binary((uchar *) a, (uchar *) b); }
 
1954
  int key_cmp(const uchar *str, uint length);
 
1955
  int cmp_offset(uint row_offset);
 
1956
  void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
 
1957
  { get_key_image(buff, length, itRAW); }   
 
1958
  void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
 
1959
  { Field_bit::store((char *) buff, length, cs); }
 
1960
  uint get_key_image(uchar *buff, uint length, imagetype type);
 
1961
  void set_key_image(const uchar *buff, uint length)
 
1962
  { Field_bit::store((char*) buff, length, &my_charset_bin); }
 
1963
  void sort_string(uchar *buff, uint length)
 
1964
  { get_key_image(buff, length, itRAW); }
 
1965
  uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
 
1966
  uint32 pack_length_in_rec() const { return bytes_in_rec; }
 
1967
  uint pack_length_from_metadata(uint field_metadata);
 
1968
  uint row_pack_length()
 
1969
  { return (bytes_in_rec + ((bit_len > 0) ? 1 : 0)); }
 
1970
  int compatible_field_size(uint field_metadata,
 
1971
                            const Relay_log_info *rli);
 
1972
  void sql_type(String &str) const;
 
1973
  virtual uchar *pack(uchar *to, const uchar *from,
 
1974
                      uint max_length, bool low_byte_first);
 
1975
  virtual const uchar *unpack(uchar *to, const uchar *from,
 
1976
                              uint param_data, bool low_byte_first);
 
1977
  virtual void set_default();
 
1978
 
 
1979
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
1980
                       uchar *new_ptr, uchar *new_null_ptr,
 
1981
                       uint new_null_bit);
 
1982
  void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
 
1983
  {
 
1984
    bit_ptr= bit_ptr_arg;
 
1985
    bit_ofs= bit_ofs_arg;
 
1986
  }
 
1987
  bool eq(Field *field)
 
1988
  {
 
1989
    return (Field::eq(field) &&
 
1990
            bit_ptr == ((Field_bit *)field)->bit_ptr &&
 
1991
            bit_ofs == ((Field_bit *)field)->bit_ofs);
 
1992
  }
 
1993
  uint is_equal(Create_field *new_field);
 
1994
  void move_field_offset(my_ptrdiff_t ptr_diff)
 
1995
  {
 
1996
    Field::move_field_offset(ptr_diff);
 
1997
    bit_ptr= ADD_TO_PTR(bit_ptr, ptr_diff, uchar*);
 
1998
  }
 
1999
  void hash(ulong *nr, ulong *nr2);
 
2000
 
 
2001
private:
 
2002
  virtual size_t do_last_null_byte() const;
 
2003
  int do_save_field_metadata(uchar *first_byte);
 
2004
};
 
2005
 
 
2006
 
 
2007
/**
 
2008
  BIT field represented as chars for non-MyISAM tables.
 
2009
 
 
2010
  @todo The inheritance relationship is backwards since Field_bit is
 
2011
  an extended version of Field_bit_as_char and not the other way
 
2012
  around. Hence, we should refactor it to fix the hierarchy order.
 
2013
 */
 
2014
class Field_bit_as_char: public Field_bit {
 
2015
public:
 
2016
  Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
2017
                    uchar null_bit_arg,
 
2018
                    enum utype unireg_check_arg, const char *field_name_arg);
 
2019
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
 
2020
  uint size_of() const { return sizeof(*this); }
 
2021
  int store(const char *to, uint length, CHARSET_INFO *charset);
 
2022
  int store(double nr) { return Field_bit::store(nr); }
 
2023
  int store(longlong nr, bool unsigned_val)
 
2024
  { return Field_bit::store(nr, unsigned_val); }
 
2025
  void sql_type(String &str) const;
 
2026
};
 
2027
 
 
2028
 
 
2029
/*
 
2030
  Create field class for CREATE TABLE
 
2031
*/
 
2032
 
 
2033
class Create_field :public Sql_alloc
 
2034
{
 
2035
public:
 
2036
  const char *field_name;
 
2037
  const char *change;                   // If done with alter table
 
2038
  const char *after;                    // Put column after this one
 
2039
  LEX_STRING comment;                   // Comment for field
 
2040
  Item  *def;                           // Default value
 
2041
  enum  enum_field_types sql_type;
 
2042
  /*
 
2043
    At various stages in execution this can be length of field in bytes or
 
2044
    max number of characters. 
 
2045
  */
 
2046
  ulong length;
 
2047
  /*
 
2048
    The value of `length' as set by parser: is the number of characters
 
2049
    for most of the types, or of bytes for BLOBs or numeric types.
 
2050
  */
 
2051
  uint32 char_length;
 
2052
  uint  decimals, flags, pack_length, key_length;
 
2053
  Field::utype unireg_check;
 
2054
  TYPELIB *interval;                    // Which interval to use
 
2055
  TYPELIB *save_interval;               // Temporary copy for the above
 
2056
                                        // Used only for UCS2 intervals
 
2057
  List<String> interval_list;
 
2058
  CHARSET_INFO *charset;
 
2059
  Field::geometry_type geom_type;
 
2060
  Field *field;                         // For alter table
 
2061
 
 
2062
  uint8 row,col,sc_length,interval_id;  // For rea_create_table
 
2063
  uint  offset,pack_flag;
 
2064
  Create_field() :after(0) {}
 
2065
  Create_field(Field *field, Field *orig_field);
 
2066
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
 
2067
  Create_field *clone(MEM_ROOT *mem_root) const
 
2068
    { return new (mem_root) Create_field(*this); }
 
2069
  void create_length_to_internal_length(void);
 
2070
 
 
2071
  /* Init for a tmp table field. To be extended if need be. */
 
2072
  void init_for_tmp_table(enum_field_types sql_type_arg,
 
2073
                          uint32 max_length, uint32 decimals,
 
2074
                          bool maybe_null, bool is_unsigned);
 
2075
 
 
2076
  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
 
2077
            char *decimals, uint type_modifier, Item *default_value,
 
2078
            Item *on_update_value, LEX_STRING *comment, char *change,
 
2079
            List<String> *interval_list, CHARSET_INFO *cs,
 
2080
            uint uint_geom_type);
 
2081
 
 
2082
  bool field_flags_are_binary()
 
2083
  {
 
2084
    return (flags & (BINCMP_FLAG | BINARY_FLAG)) != 0;
 
2085
  }
 
2086
};
 
2087
 
 
2088
 
 
2089
/*
 
2090
  A class for sending info to the client
 
2091
*/
 
2092
 
 
2093
class Send_field {
 
2094
 public:
 
2095
  const char *db_name;
 
2096
  const char *table_name,*org_table_name;
 
2097
  const char *col_name,*org_col_name;
 
2098
  ulong length;
 
2099
  uint charsetnr, flags, decimals;
 
2100
  enum_field_types type;
 
2101
  Send_field() {}
 
2102
};
 
2103
 
 
2104
 
 
2105
/*
 
2106
  A class for quick copying data to fields
 
2107
*/
 
2108
 
 
2109
class Copy_field :public Sql_alloc {
 
2110
  /**
 
2111
    Convenience definition of a copy function returned by
 
2112
    get_copy_func.
 
2113
  */
 
2114
  typedef void Copy_func(Copy_field*);
 
2115
  Copy_func *get_copy_func(Field *to, Field *from);
 
2116
public:
 
2117
  uchar *from_ptr,*to_ptr;
 
2118
  uchar *from_null_ptr,*to_null_ptr;
 
2119
  my_bool *null_row;
 
2120
  uint  from_bit,to_bit;
 
2121
  uint from_length,to_length;
 
2122
  Field *from_field,*to_field;
 
2123
  String tmp;                                   // For items
 
2124
 
 
2125
  Copy_field() {}
 
2126
  ~Copy_field() {}
 
2127
  void set(Field *to,Field *from,bool save);    // Field to field 
 
2128
  void set(uchar *to,Field *from);              // Field to string
 
2129
  void (*do_copy)(Copy_field *);
 
2130
  void (*do_copy2)(Copy_field *);               // Used to handle null values
 
2131
};
 
2132
 
 
2133
 
 
2134
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
 
2135
                  uchar *null_pos, uchar null_bit,
 
2136
                  uint pack_flag, enum_field_types field_type,
 
2137
                  CHARSET_INFO *cs,
 
2138
                  Field::geometry_type geom_type,
 
2139
                  Field::utype unireg_check,
 
2140
                  TYPELIB *interval, const char *field_name);
 
2141
uint pack_length_to_packflag(uint type);
 
2142
enum_field_types get_blob_type_from_length(ulong length);
 
2143
uint32 calc_pack_length(enum_field_types type,uint32 length);
 
2144
int set_field_to_null(Field *field);
 
2145
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
 
2146
 
 
2147
/*
 
2148
  The following are for the interface with the .frm file
 
2149
*/
 
2150
 
 
2151
#define FIELDFLAG_DECIMAL               1
 
2152
#define FIELDFLAG_BINARY                1       // Shares same flag
 
2153
#define FIELDFLAG_NUMBER                2
 
2154
#define FIELDFLAG_ZEROFILL              4
 
2155
#define FIELDFLAG_PACK                  120     // Bits used for packing
 
2156
#define FIELDFLAG_INTERVAL              256     // mangled with decimals!
 
2157
#define FIELDFLAG_BITFIELD              512     // mangled with decimals!
 
2158
#define FIELDFLAG_BLOB                  1024    // mangled with decimals!
 
2159
#define FIELDFLAG_GEOM                  2048    // mangled with decimals!
 
2160
 
 
2161
#define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */
 
2162
 
 
2163
#define FIELDFLAG_LEFT_FULLSCREEN       8192
 
2164
#define FIELDFLAG_RIGHT_FULLSCREEN      16384
 
2165
#define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
 
2166
#define FIELDFLAG_NO_DEFAULT            16384   /* sql */
 
2167
#define FIELDFLAG_SUM                   ((uint) 32768)// predit: +#fieldflag
 
2168
#define FIELDFLAG_MAYBE_NULL            ((uint) 32768)// sql
 
2169
#define FIELDFLAG_HEX_ESCAPE            ((uint) 0x10000)
 
2170
#define FIELDFLAG_PACK_SHIFT            3
 
2171
#define FIELDFLAG_DEC_SHIFT             8
 
2172
#define FIELDFLAG_MAX_DEC               31
 
2173
#define FIELDFLAG_NUM_SCREEN_TYPE       0x7F01
 
2174
#define FIELDFLAG_ALFA_SCREEN_TYPE      0x7800
 
2175
 
 
2176
#define MTYP_TYPENR(type) (type & 127)  /* Remove bits from type */
 
2177
 
 
2178
#define f_is_dec(x)             ((x) & FIELDFLAG_DECIMAL)
 
2179
#define f_is_num(x)             ((x) & FIELDFLAG_NUMBER)
 
2180
#define f_is_zerofill(x)        ((x) & FIELDFLAG_ZEROFILL)
 
2181
#define f_is_packed(x)          ((x) & FIELDFLAG_PACK)
 
2182
#define f_packtype(x)           (((x) >> FIELDFLAG_PACK_SHIFT) & 15)
 
2183
#define f_decimals(x)           ((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
 
2184
#define f_is_alpha(x)           (!f_is_num(x))
 
2185
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
 
2186
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
 
2187
#define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
 
2188
#define f_is_blob(x)            (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
 
2189
#define f_is_geom(x)            (((x) & (FIELDFLAG_GEOM | FIELDFLAG_NUMBER)) == FIELDFLAG_GEOM)
 
2190
#define f_is_equ(x)             ((x) & (1+2+FIELDFLAG_PACK+31*256))
 
2191
#define f_settype(x)            (((int) x) << FIELDFLAG_PACK_SHIFT)
 
2192
#define f_maybe_null(x)         (x & FIELDFLAG_MAYBE_NULL)
 
2193
#define f_no_default(x)         (x & FIELDFLAG_NO_DEFAULT)
 
2194
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
 
2195
#define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)