~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/field.h

  • Committer: Monty Taylor
  • Date: 2008-07-05 22:08:52 UTC
  • mto: This revision was merged to the branch mainline in revision 77.
  • Revision ID: monty@inaugust.com-20080705220852-cqd9t6tfkhvlcf73
Removed HAVE_LONG_LONG, as this is now assumed.

Show diffs side-by-side

added added

removed removed

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