~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/field.h

  • Committer: Monty Taylor
  • Date: 2008-07-05 11:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: monty@inaugust.com-20080705112018-fr12kkmgphtu7m29
Changes so that removal of duplicate curr_dir from my_sys.h work.

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
#ifdef HAVE_LONG_LONG
 
982
class Field_longlong :public Field_num {
 
983
public:
 
984
  Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
985
              uchar null_bit_arg,
 
986
              enum utype unireg_check_arg, const char *field_name_arg,
 
987
              bool zero_arg, bool unsigned_arg)
 
988
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
989
               unireg_check_arg, field_name_arg,
 
990
               0, zero_arg,unsigned_arg)
 
991
    {}
 
992
  Field_longlong(uint32 len_arg,bool maybe_null_arg,
 
993
                 const char *field_name_arg,
 
994
                  bool unsigned_arg)
 
995
    :Field_num((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0,0,
 
996
               NONE, field_name_arg,0,0,unsigned_arg)
 
997
    {}
 
998
  enum Item_result result_type () const { return INT_RESULT; }
 
999
  enum_field_types type() const { return MYSQL_TYPE_LONGLONG;}
 
1000
  enum ha_base_keytype key_type() const
 
1001
    { return unsigned_flag ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG; }
 
1002
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1003
  int store(double nr);
 
1004
  int store(longlong nr, bool unsigned_val);
 
1005
  int reset(void)
 
1006
  {
 
1007
    ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
 
1008
    return 0;
 
1009
  }
 
1010
  double val_real(void);
 
1011
  longlong val_int(void);
 
1012
  String *val_str(String*,String *);
 
1013
  bool send_binary(Protocol *protocol);
 
1014
  int cmp(const uchar *,const uchar *);
 
1015
  void sort_string(uchar *buff,uint length);
 
1016
  uint32 pack_length() const { return 8; }
 
1017
  void sql_type(String &str) const;
 
1018
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1019
  uint32 max_display_length() { return 20; }
 
1020
  virtual uchar *pack(uchar* to, const uchar *from,
 
1021
                      uint max_length, bool low_byte_first)
 
1022
  {
 
1023
    int64 val;
 
1024
#ifdef WORDS_BIGENDIAN
 
1025
    if (table->s->db_low_byte_first)
 
1026
      val = sint8korr(from);
 
1027
    else
 
1028
#endif
 
1029
      longlongget(val, from);
 
1030
 
 
1031
#ifdef WORDS_BIGENDIAN
 
1032
    if (low_byte_first)
 
1033
      int8store(to, val);
 
1034
    else
 
1035
#endif
 
1036
      longlongstore(to, val);
 
1037
    return to + sizeof(val);
 
1038
  }
 
1039
 
 
1040
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
1041
                              uint param_data, bool low_byte_first)
 
1042
  {
 
1043
    int64 val;
 
1044
#ifdef WORDS_BIGENDIAN
 
1045
    if (low_byte_first)
 
1046
      val = sint8korr(from);
 
1047
    else
 
1048
#endif
 
1049
      longlongget(val, from);
 
1050
 
 
1051
#ifdef WORDS_BIGENDIAN
 
1052
    if (table->s->db_low_byte_first)
 
1053
      int8store(to, val);
 
1054
    else
 
1055
#endif
 
1056
      longlongstore(to, val);
 
1057
    return from + sizeof(val);
 
1058
  }
 
1059
};
 
1060
#endif
 
1061
 
 
1062
 
 
1063
class Field_float :public Field_real {
 
1064
public:
 
1065
  Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1066
              uchar null_bit_arg,
 
1067
              enum utype unireg_check_arg, const char *field_name_arg,
 
1068
              uint8 dec_arg,bool zero_arg,bool unsigned_arg)
 
1069
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1070
                unireg_check_arg, field_name_arg,
 
1071
                dec_arg, zero_arg, unsigned_arg)
 
1072
    {}
 
1073
  Field_float(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
 
1074
              uint8 dec_arg)
 
1075
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, (uint) 0,
 
1076
                NONE, field_name_arg, dec_arg, 0, 0)
 
1077
    {}
 
1078
  enum_field_types type() const { return MYSQL_TYPE_FLOAT;}
 
1079
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_FLOAT; }
 
1080
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1081
  int store(double nr);
 
1082
  int store(longlong nr, bool unsigned_val);
 
1083
  int reset(void) { bzero(ptr,sizeof(float)); return 0; }
 
1084
  double val_real(void);
 
1085
  longlong val_int(void);
 
1086
  String *val_str(String*,String *);
 
1087
  bool send_binary(Protocol *protocol);
 
1088
  int cmp(const uchar *,const uchar *);
 
1089
  void sort_string(uchar *buff,uint length);
 
1090
  uint32 pack_length() const { return sizeof(float); }
 
1091
  uint row_pack_length() { return pack_length(); }
 
1092
  void sql_type(String &str) const;
 
1093
private:
 
1094
  int do_save_field_metadata(uchar *first_byte);
 
1095
};
 
1096
 
 
1097
 
 
1098
class Field_double :public Field_real {
 
1099
public:
 
1100
  Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1101
               uchar null_bit_arg,
 
1102
               enum utype unireg_check_arg, const char *field_name_arg,
 
1103
               uint8 dec_arg,bool zero_arg,bool unsigned_arg)
 
1104
    :Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1105
                unireg_check_arg, field_name_arg,
 
1106
                dec_arg, zero_arg, unsigned_arg)
 
1107
    {}
 
1108
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
 
1109
               uint8 dec_arg)
 
1110
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
 
1111
                NONE, field_name_arg, dec_arg, 0, 0)
 
1112
    {}
 
1113
  Field_double(uint32 len_arg, bool maybe_null_arg, const char *field_name_arg,
 
1114
               uint8 dec_arg, my_bool not_fixed_arg)
 
1115
    :Field_real((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "" : 0, (uint) 0,
 
1116
                NONE, field_name_arg, dec_arg, 0, 0)
 
1117
    {not_fixed= not_fixed_arg; }
 
1118
  enum_field_types type() const { return MYSQL_TYPE_DOUBLE;}
 
1119
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_DOUBLE; }
 
1120
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1121
  int  store(double nr);
 
1122
  int  store(longlong nr, bool unsigned_val);
 
1123
  int reset(void) { bzero(ptr,sizeof(double)); return 0; }
 
1124
  double val_real(void);
 
1125
  longlong val_int(void);
 
1126
  String *val_str(String*,String *);
 
1127
  bool send_binary(Protocol *protocol);
 
1128
  int cmp(const uchar *,const uchar *);
 
1129
  void sort_string(uchar *buff,uint length);
 
1130
  uint32 pack_length() const { return sizeof(double); }
 
1131
  uint row_pack_length() { return pack_length(); }
 
1132
  void sql_type(String &str) const;
 
1133
private:
 
1134
  int do_save_field_metadata(uchar *first_byte);
 
1135
};
 
1136
 
 
1137
 
 
1138
/* Everything saved in this will disappear. It will always return NULL */
 
1139
 
 
1140
class Field_null :public Field_str {
 
1141
  static uchar null[1];
 
1142
public:
 
1143
  Field_null(uchar *ptr_arg, uint32 len_arg,
 
1144
             enum utype unireg_check_arg, const char *field_name_arg,
 
1145
             CHARSET_INFO *cs)
 
1146
    :Field_str(ptr_arg, len_arg, null, 1,
 
1147
               unireg_check_arg, field_name_arg, cs)
 
1148
    {}
 
1149
  enum_field_types type() const { return MYSQL_TYPE_NULL;}
 
1150
  int  store(const char *to, uint length, CHARSET_INFO *cs)
 
1151
  { null[0]=1; return 0; }
 
1152
  int store(double nr)   { null[0]=1; return 0; }
 
1153
  int store(longlong nr, bool unsigned_val) { null[0]=1; return 0; }
 
1154
  int store_decimal(const my_decimal *d)  { null[0]=1; return 0; }
 
1155
  int reset(void)         { return 0; }
 
1156
  double val_real(void)         { return 0.0;}
 
1157
  longlong val_int(void)        { return 0;}
 
1158
  my_decimal *val_decimal(my_decimal *) { return 0; }
 
1159
  String *val_str(String *value,String *value2)
 
1160
  { value2->length(0); return value2;}
 
1161
  int cmp(const uchar *a, const uchar *b) { return 0;}
 
1162
  void sort_string(uchar *buff, uint length)  {}
 
1163
  uint32 pack_length() const { return 0; }
 
1164
  void sql_type(String &str) const;
 
1165
  uint size_of() const { return sizeof(*this); }
 
1166
  uint32 max_display_length() { return 4; }
 
1167
};
 
1168
 
 
1169
 
 
1170
class Field_timestamp :public Field_str {
 
1171
public:
 
1172
  Field_timestamp(uchar *ptr_arg, uint32 len_arg,
 
1173
                  uchar *null_ptr_arg, uchar null_bit_arg,
 
1174
                  enum utype unireg_check_arg, const char *field_name_arg,
 
1175
                  TABLE_SHARE *share, CHARSET_INFO *cs);
 
1176
  Field_timestamp(bool maybe_null_arg, const char *field_name_arg,
 
1177
                  CHARSET_INFO *cs);
 
1178
  enum_field_types type() const { return MYSQL_TYPE_TIMESTAMP;}
 
1179
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
 
1180
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1181
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1182
  int  store(double nr);
 
1183
  int  store(longlong nr, bool unsigned_val);
 
1184
  int  reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
 
1185
  double val_real(void);
 
1186
  longlong val_int(void);
 
1187
  String *val_str(String*,String *);
 
1188
  bool send_binary(Protocol *protocol);
 
1189
  int cmp(const uchar *,const uchar *);
 
1190
  void sort_string(uchar *buff,uint length);
 
1191
  uint32 pack_length() const { return 4; }
 
1192
  void sql_type(String &str) const;
 
1193
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1194
  bool zero_pack() const { return 0; }
 
1195
  void set_time();
 
1196
  virtual void set_default()
 
1197
  {
 
1198
    if (table->timestamp_field == this &&
 
1199
        unireg_check != TIMESTAMP_UN_FIELD)
 
1200
      set_time();
 
1201
    else
 
1202
      Field::set_default();
 
1203
  }
 
1204
  /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
 
1205
  inline long get_timestamp(my_bool *null_value)
 
1206
  {
 
1207
    if ((*null_value= is_null()))
 
1208
      return 0;
 
1209
#ifdef WORDS_BIGENDIAN
 
1210
    if (table && table->s->db_low_byte_first)
 
1211
      return sint4korr(ptr);
 
1212
#endif
 
1213
    long tmp;
 
1214
    longget(tmp,ptr);
 
1215
    return tmp;
 
1216
  }
 
1217
  inline void store_timestamp(my_time_t timestamp)
 
1218
  {
 
1219
#ifdef WORDS_BIGENDIAN
 
1220
    if (table && table->s->db_low_byte_first)
 
1221
    {
 
1222
      int4store(ptr,timestamp);
 
1223
    }
 
1224
    else
 
1225
#endif
 
1226
      longstore(ptr,(uint32) timestamp);
 
1227
  }
 
1228
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
1229
  bool get_time(MYSQL_TIME *ltime);
 
1230
  timestamp_auto_set_type get_auto_set_type() const;
 
1231
};
 
1232
 
 
1233
 
 
1234
class Field_year :public Field_tiny {
 
1235
public:
 
1236
  Field_year(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1237
             uchar null_bit_arg,
 
1238
             enum utype unireg_check_arg, const char *field_name_arg)
 
1239
    :Field_tiny(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1240
                unireg_check_arg, field_name_arg, 1, 1)
 
1241
    {}
 
1242
  enum_field_types type() const { return MYSQL_TYPE_YEAR;}
 
1243
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1244
  int  store(double nr);
 
1245
  int  store(longlong nr, bool unsigned_val);
 
1246
  double val_real(void);
 
1247
  longlong val_int(void);
 
1248
  String *val_str(String*,String *);
 
1249
  bool send_binary(Protocol *protocol);
 
1250
  void sql_type(String &str) const;
 
1251
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1252
};
 
1253
 
 
1254
 
 
1255
class Field_date :public Field_str {
 
1256
public:
 
1257
  Field_date(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1258
             enum utype unireg_check_arg, const char *field_name_arg,
 
1259
             CHARSET_INFO *cs)
 
1260
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
 
1261
               unireg_check_arg, field_name_arg, cs)
 
1262
    {}
 
1263
  Field_date(bool maybe_null_arg, const char *field_name_arg,
 
1264
             CHARSET_INFO *cs)
 
1265
    :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
 
1266
               NONE, field_name_arg, cs) {}
 
1267
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
 
1268
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONG_INT; }
 
1269
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1270
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1271
  int store(double nr);
 
1272
  int store(longlong nr, bool unsigned_val);
 
1273
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=ptr[3]=0; return 0; }
 
1274
  double val_real(void);
 
1275
  longlong val_int(void);
 
1276
  String *val_str(String*,String *);
 
1277
  bool get_time(MYSQL_TIME *ltime);
 
1278
  bool send_binary(Protocol *protocol);
 
1279
  int cmp(const uchar *,const uchar *);
 
1280
  void sort_string(uchar *buff,uint length);
 
1281
  uint32 pack_length() const { return 4; }
 
1282
  void sql_type(String &str) const;
 
1283
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1284
  bool zero_pack() const { return 1; }
 
1285
};
 
1286
 
 
1287
 
 
1288
class Field_newdate :public Field_str {
 
1289
public:
 
1290
  Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1291
                enum utype unireg_check_arg, const char *field_name_arg,
 
1292
                CHARSET_INFO *cs)
 
1293
    :Field_str(ptr_arg, 10, null_ptr_arg, null_bit_arg,
 
1294
               unireg_check_arg, field_name_arg, cs)
 
1295
    {}
 
1296
  Field_newdate(bool maybe_null_arg, const char *field_name_arg,
 
1297
                CHARSET_INFO *cs)
 
1298
    :Field_str((uchar*) 0,10, maybe_null_arg ? (uchar*) "": 0,0,
 
1299
               NONE, field_name_arg, cs) {}
 
1300
  enum_field_types type() const { return MYSQL_TYPE_DATE;}
 
1301
  enum_field_types real_type() const { return MYSQL_TYPE_NEWDATE; }
 
1302
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_UINT24; }
 
1303
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1304
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1305
  int  store(double nr);
 
1306
  int  store(longlong nr, bool unsigned_val);
 
1307
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
 
1308
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
 
1309
  double val_real(void);
 
1310
  longlong val_int(void);
 
1311
  String *val_str(String*,String *);
 
1312
  bool send_binary(Protocol *protocol);
 
1313
  int cmp(const uchar *,const uchar *);
 
1314
  void sort_string(uchar *buff,uint length);
 
1315
  uint32 pack_length() const { return 3; }
 
1316
  void sql_type(String &str) const;
 
1317
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1318
  bool zero_pack() const { return 1; }
 
1319
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
1320
  bool get_time(MYSQL_TIME *ltime);
 
1321
};
 
1322
 
 
1323
 
 
1324
class Field_time :public Field_str {
 
1325
public:
 
1326
  Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1327
             enum utype unireg_check_arg, const char *field_name_arg,
 
1328
             CHARSET_INFO *cs)
 
1329
    :Field_str(ptr_arg, 8, null_ptr_arg, null_bit_arg,
 
1330
               unireg_check_arg, field_name_arg, cs)
 
1331
    {}
 
1332
  Field_time(bool maybe_null_arg, const char *field_name_arg,
 
1333
             CHARSET_INFO *cs)
 
1334
    :Field_str((uchar*) 0,8, maybe_null_arg ? (uchar*) "": 0,0,
 
1335
               NONE, field_name_arg, cs) {}
 
1336
  enum_field_types type() const { return MYSQL_TYPE_TIME;}
 
1337
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_INT24; }
 
1338
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1339
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
 
1340
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1341
  int store(double nr);
 
1342
  int store(longlong nr, bool unsigned_val);
 
1343
  int reset(void) { ptr[0]=ptr[1]=ptr[2]=0; return 0; }
 
1344
  double val_real(void);
 
1345
  longlong val_int(void);
 
1346
  String *val_str(String*,String *);
 
1347
  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
 
1348
  bool send_binary(Protocol *protocol);
 
1349
  bool get_time(MYSQL_TIME *ltime);
 
1350
  int cmp(const uchar *,const uchar *);
 
1351
  void sort_string(uchar *buff,uint length);
 
1352
  uint32 pack_length() const { return 3; }
 
1353
  void sql_type(String &str) const;
 
1354
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1355
  bool zero_pack() const { return 1; }
 
1356
};
 
1357
 
 
1358
 
 
1359
class Field_datetime :public Field_str {
 
1360
public:
 
1361
  Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1362
                 enum utype unireg_check_arg, const char *field_name_arg,
 
1363
                 CHARSET_INFO *cs)
 
1364
    :Field_str(ptr_arg, 19, null_ptr_arg, null_bit_arg,
 
1365
               unireg_check_arg, field_name_arg, cs)
 
1366
    {}
 
1367
  Field_datetime(bool maybe_null_arg, const char *field_name_arg,
 
1368
                 CHARSET_INFO *cs)
 
1369
    :Field_str((uchar*) 0,19, maybe_null_arg ? (uchar*) "": 0,0,
 
1370
               NONE, field_name_arg, cs) {}
 
1371
  enum_field_types type() const { return MYSQL_TYPE_DATETIME;}
 
1372
#ifdef HAVE_LONG_LONG
 
1373
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_ULONGLONG; }
 
1374
#endif
 
1375
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1376
  uint decimals() const { return DATETIME_DEC; }
 
1377
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1378
  int  store(double nr);
 
1379
  int  store(longlong nr, bool unsigned_val);
 
1380
  int store_time(MYSQL_TIME *ltime, timestamp_type type);
 
1381
  int reset(void)
 
1382
  {
 
1383
    ptr[0]=ptr[1]=ptr[2]=ptr[3]=ptr[4]=ptr[5]=ptr[6]=ptr[7]=0;
 
1384
    return 0;
 
1385
  }
 
1386
  double val_real(void);
 
1387
  longlong val_int(void);
 
1388
  String *val_str(String*,String *);
 
1389
  bool send_binary(Protocol *protocol);
 
1390
  int cmp(const uchar *,const uchar *);
 
1391
  void sort_string(uchar *buff,uint length);
 
1392
  uint32 pack_length() const { return 8; }
 
1393
  void sql_type(String &str) const;
 
1394
  bool can_be_compared_as_longlong() const { return TRUE; }
 
1395
  bool zero_pack() const { return 1; }
 
1396
  bool get_date(MYSQL_TIME *ltime,uint fuzzydate);
 
1397
  bool get_time(MYSQL_TIME *ltime);
 
1398
};
 
1399
 
 
1400
 
 
1401
class Field_string :public Field_longstr {
 
1402
public:
 
1403
  bool can_alter_field_type;
 
1404
  Field_string(uchar *ptr_arg, uint32 len_arg,uchar *null_ptr_arg,
 
1405
               uchar null_bit_arg,
 
1406
               enum utype unireg_check_arg, const char *field_name_arg,
 
1407
               CHARSET_INFO *cs)
 
1408
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1409
                   unireg_check_arg, field_name_arg, cs),
 
1410
     can_alter_field_type(1) {};
 
1411
  Field_string(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
1412
               CHARSET_INFO *cs)
 
1413
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1414
                   NONE, field_name_arg, cs),
 
1415
     can_alter_field_type(1) {};
 
1416
 
 
1417
  enum_field_types type() const
 
1418
  {
 
1419
    return ((can_alter_field_type && orig_table &&
 
1420
             orig_table->s->db_create_options & HA_OPTION_PACK_RECORD &&
 
1421
             field_length >= 4) &&
 
1422
            orig_table->s->frm_version < FRM_VER_TRUE_VARCHAR ?
 
1423
            MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING);
 
1424
  }
 
1425
  enum ha_base_keytype key_type() const
 
1426
    { return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT; }
 
1427
  bool zero_pack() const { return 0; }
 
1428
  int reset(void)
 
1429
  {
 
1430
    charset()->cset->fill(charset(),(char*) ptr, field_length,
 
1431
                          (has_charset() ? ' ' : 0));
 
1432
    return 0;
 
1433
  }
 
1434
  int store(const char *to,uint length,CHARSET_INFO *charset);
 
1435
  int store(longlong nr, bool unsigned_val);
 
1436
  int store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
 
1437
  double val_real(void);
 
1438
  longlong val_int(void);
 
1439
  String *val_str(String*,String *);
 
1440
  my_decimal *val_decimal(my_decimal *);
 
1441
  int cmp(const uchar *,const uchar *);
 
1442
  void sort_string(uchar *buff,uint length);
 
1443
  void sql_type(String &str) const;
 
1444
  virtual uchar *pack(uchar *to, const uchar *from,
 
1445
                      uint max_length, bool low_byte_first);
 
1446
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
1447
                              uint param_data, bool low_byte_first);
 
1448
  uint pack_length_from_metadata(uint field_metadata)
 
1449
  { return (field_metadata & 0x00ff); }
 
1450
  uint row_pack_length() { return (field_length + 1); }
 
1451
  int pack_cmp(const uchar *a,const uchar *b,uint key_length,
 
1452
               my_bool insert_or_update);
 
1453
  int pack_cmp(const uchar *b,uint key_length,my_bool insert_or_update);
 
1454
  uint packed_col_length(const uchar *to, uint length);
 
1455
  uint max_packed_col_length(uint max_length);
 
1456
  uint size_of() const { return sizeof(*this); }
 
1457
  enum_field_types real_type() const { return MYSQL_TYPE_STRING; }
 
1458
  bool has_charset(void) const
 
1459
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
 
1460
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
 
1461
  virtual uint get_key_image(uchar *buff,uint length, imagetype type);
 
1462
private:
 
1463
  int do_save_field_metadata(uchar *first_byte);
 
1464
};
 
1465
 
 
1466
 
 
1467
class Field_varstring :public Field_longstr {
 
1468
public:
 
1469
  /*
 
1470
    The maximum space available in a Field_varstring, in bytes. See
 
1471
    length_bytes.
 
1472
  */
 
1473
  static const uint MAX_SIZE;
 
1474
  /* Store number of bytes used to store length (1 or 2) */
 
1475
  uint32 length_bytes;
 
1476
  Field_varstring(uchar *ptr_arg,
 
1477
                  uint32 len_arg, uint length_bytes_arg,
 
1478
                  uchar *null_ptr_arg, uchar null_bit_arg,
 
1479
                  enum utype unireg_check_arg, const char *field_name_arg,
 
1480
                  TABLE_SHARE *share, CHARSET_INFO *cs)
 
1481
    :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1482
                   unireg_check_arg, field_name_arg, cs),
 
1483
     length_bytes(length_bytes_arg)
 
1484
  {
 
1485
    share->varchar_fields++;
 
1486
  }
 
1487
  Field_varstring(uint32 len_arg,bool maybe_null_arg,
 
1488
                  const char *field_name_arg,
 
1489
                  TABLE_SHARE *share, CHARSET_INFO *cs)
 
1490
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1491
                   NONE, field_name_arg, cs),
 
1492
     length_bytes(len_arg < 256 ? 1 :2)
 
1493
  {
 
1494
    share->varchar_fields++;
 
1495
  }
 
1496
 
 
1497
  enum_field_types type() const { return MYSQL_TYPE_VARCHAR; }
 
1498
  enum ha_base_keytype key_type() const;
 
1499
  uint row_pack_length() { return field_length; }
 
1500
  bool zero_pack() const { return 0; }
 
1501
  int  reset(void) { bzero(ptr,field_length+length_bytes); return 0; }
 
1502
  uint32 pack_length() const { return (uint32) field_length+length_bytes; }
 
1503
  uint32 key_length() const { return (uint32) field_length; }
 
1504
  uint32 sort_length() const
 
1505
  {
 
1506
    return (uint32) field_length + (field_charset == &my_charset_bin ?
 
1507
                                    length_bytes : 0);
 
1508
  }
 
1509
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1510
  int  store(longlong nr, bool unsigned_val);
 
1511
  int  store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
 
1512
  double val_real(void);
 
1513
  longlong val_int(void);
 
1514
  String *val_str(String*,String *);
 
1515
  my_decimal *val_decimal(my_decimal *);
 
1516
  int cmp_max(const uchar *, const uchar *, uint max_length);
 
1517
  int cmp(const uchar *a,const uchar *b)
 
1518
  {
 
1519
    return cmp_max(a, b, ~0L);
 
1520
  }
 
1521
  void sort_string(uchar *buff,uint length);
 
1522
  uint get_key_image(uchar *buff,uint length, imagetype type);
 
1523
  void set_key_image(const uchar *buff,uint length);
 
1524
  void sql_type(String &str) const;
 
1525
  virtual uchar *pack(uchar *to, const uchar *from,
 
1526
                      uint max_length, bool low_byte_first);
 
1527
  uchar *pack_key(uchar *to, const uchar *from, uint max_length, bool low_byte_first);
 
1528
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
 
1529
                                 uint max_length, bool low_byte_first);
 
1530
  virtual const uchar *unpack(uchar* to, const uchar *from,
 
1531
                              uint param_data, bool low_byte_first);
 
1532
  const uchar *unpack_key(uchar* to, const uchar *from,
 
1533
                          uint max_length, bool low_byte_first);
 
1534
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
 
1535
               my_bool insert_or_update);
 
1536
  int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
 
1537
  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
 
1538
  int key_cmp(const uchar *,const uchar*);
 
1539
  int key_cmp(const uchar *str, uint length);
 
1540
  uint packed_col_length(const uchar *to, uint length);
 
1541
  uint max_packed_col_length(uint max_length);
 
1542
  uint32 data_length();
 
1543
  uint32 used_length();
 
1544
  uint size_of() const { return sizeof(*this); }
 
1545
  enum_field_types real_type() const { return MYSQL_TYPE_VARCHAR; }
 
1546
  bool has_charset(void) const
 
1547
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
 
1548
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
 
1549
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
1550
                       uchar *new_ptr, uchar *new_null_ptr,
 
1551
                       uint new_null_bit);
 
1552
  uint is_equal(Create_field *new_field);
 
1553
  void hash(ulong *nr, ulong *nr2);
 
1554
private:
 
1555
  int do_save_field_metadata(uchar *first_byte);
 
1556
};
 
1557
 
 
1558
 
 
1559
class Field_blob :public Field_longstr {
 
1560
protected:
 
1561
  uint packlength;
 
1562
  String value;                         // For temporaries
 
1563
public:
 
1564
  Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
 
1565
             enum utype unireg_check_arg, const char *field_name_arg,
 
1566
             TABLE_SHARE *share, uint blob_pack_length, CHARSET_INFO *cs);
 
1567
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
1568
             CHARSET_INFO *cs)
 
1569
    :Field_longstr((uchar*) 0, len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1570
                   NONE, field_name_arg, cs),
 
1571
    packlength(4)
 
1572
  {
 
1573
    flags|= BLOB_FLAG;
 
1574
  }
 
1575
  Field_blob(uint32 len_arg,bool maybe_null_arg, const char *field_name_arg,
 
1576
             CHARSET_INFO *cs, bool set_packlength)
 
1577
    :Field_longstr((uchar*) 0,len_arg, maybe_null_arg ? (uchar*) "": 0, 0,
 
1578
                   NONE, field_name_arg, cs)
 
1579
  {
 
1580
    flags|= BLOB_FLAG;
 
1581
    packlength= 4;
 
1582
    if (set_packlength)
 
1583
    {
 
1584
      uint32 l_char_length= len_arg/cs->mbmaxlen;
 
1585
      packlength= l_char_length <= 255 ? 1 :
 
1586
                  l_char_length <= 65535 ? 2 :
 
1587
                  l_char_length <= 16777215 ? 3 : 4;
 
1588
    }
 
1589
  }
 
1590
  Field_blob(uint32 packlength_arg)
 
1591
    :Field_longstr((uchar*) 0, 0, (uchar*) "", 0, NONE, "temp", system_charset_info),
 
1592
    packlength(packlength_arg) {}
 
1593
  enum_field_types type() const { return MYSQL_TYPE_BLOB;}
 
1594
  enum ha_base_keytype key_type() const
 
1595
    { return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2; }
 
1596
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1597
  int  store(double nr);
 
1598
  int  store(longlong nr, bool unsigned_val);
 
1599
  double val_real(void);
 
1600
  longlong val_int(void);
 
1601
  String *val_str(String*,String *);
 
1602
  my_decimal *val_decimal(my_decimal *);
 
1603
  int cmp_max(const uchar *, const uchar *, uint max_length);
 
1604
  int cmp(const uchar *a,const uchar *b)
 
1605
    { return cmp_max(a, b, ~0L); }
 
1606
  int cmp(const uchar *a, uint32 a_length, const uchar *b, uint32 b_length);
 
1607
  int cmp_binary(const uchar *a,const uchar *b, uint32 max_length=~0L);
 
1608
  int key_cmp(const uchar *,const uchar*);
 
1609
  int key_cmp(const uchar *str, uint length);
 
1610
  uint32 key_length() const { return 0; }
 
1611
  void sort_string(uchar *buff,uint length);
 
1612
  uint32 pack_length() const
 
1613
  { return (uint32) (packlength+table->s->blob_ptr_size); }
 
1614
 
 
1615
  /**
 
1616
     Return the packed length without the pointer size added. 
 
1617
 
 
1618
     This is used to determine the size of the actual data in the row
 
1619
     buffer.
 
1620
 
 
1621
     @returns The length of the raw data itself without the pointer.
 
1622
  */
 
1623
  uint32 pack_length_no_ptr() const
 
1624
  { return (uint32) (packlength); }
 
1625
  uint row_pack_length() { return pack_length_no_ptr(); }
 
1626
  uint32 sort_length() const;
 
1627
  virtual uint32 max_data_length() const
 
1628
  {
 
1629
    return (uint32) (((uint64_t) 1 << (packlength*8)) -1);
 
1630
  }
 
1631
  int reset(void) { bzero(ptr, packlength+sizeof(uchar*)); return 0; }
 
1632
  void reset_fields() { bzero((uchar*) &value,sizeof(value)); }
 
1633
#ifndef WORDS_BIGENDIAN
 
1634
  static
 
1635
#endif
 
1636
  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number, bool low_byte_first);
 
1637
  void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number)
 
1638
  {
 
1639
    store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
 
1640
  }
 
1641
  inline void store_length(uint32 number)
 
1642
  {
 
1643
    store_length(ptr, packlength, number);
 
1644
  }
 
1645
 
 
1646
  /**
 
1647
     Return the packed length plus the length of the data. 
 
1648
 
 
1649
     This is used to determine the size of the data plus the 
 
1650
     packed length portion in the row data.
 
1651
 
 
1652
     @returns The length in the row plus the size of the data.
 
1653
  */
 
1654
  uint32 get_packed_size(const uchar *ptr_arg, bool low_byte_first)
 
1655
    {return packlength + get_length(ptr_arg, packlength, low_byte_first);}
 
1656
 
 
1657
  inline uint32 get_length(uint row_offset= 0)
 
1658
  { return get_length(ptr+row_offset, this->packlength, table->s->db_low_byte_first); }
 
1659
  uint32 get_length(const uchar *ptr, uint packlength, bool low_byte_first);
 
1660
  uint32 get_length(const uchar *ptr_arg)
 
1661
  { return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first); }
 
1662
  void put_length(uchar *pos, uint32 length);
 
1663
  inline void get_ptr(uchar **str)
 
1664
    {
 
1665
      memcpy_fixed((uchar*) str,ptr+packlength,sizeof(uchar*));
 
1666
    }
 
1667
  inline void get_ptr(uchar **str, uint row_offset)
 
1668
    {
 
1669
      memcpy_fixed((uchar*) str,ptr+packlength+row_offset,sizeof(char*));
 
1670
    }
 
1671
  inline void set_ptr(uchar *length, uchar *data)
 
1672
    {
 
1673
      memcpy(ptr,length,packlength);
 
1674
      memcpy_fixed(ptr+packlength,&data,sizeof(char*));
 
1675
    }
 
1676
  void set_ptr_offset(my_ptrdiff_t ptr_diff, uint32 length, uchar *data)
 
1677
    {
 
1678
      uchar *ptr_ofs= ADD_TO_PTR(ptr,ptr_diff,uchar*);
 
1679
      store_length(ptr_ofs, packlength, length);
 
1680
      memcpy_fixed(ptr_ofs+packlength,&data,sizeof(char*));
 
1681
    }
 
1682
  inline void set_ptr(uint32 length, uchar *data)
 
1683
    {
 
1684
      set_ptr_offset(0, length, data);
 
1685
    }
 
1686
  uint get_key_image(uchar *buff,uint length, imagetype type);
 
1687
  void set_key_image(const uchar *buff,uint length);
 
1688
  void sql_type(String &str) const;
 
1689
  inline bool copy()
 
1690
  {
 
1691
    uchar *tmp;
 
1692
    get_ptr(&tmp);
 
1693
    if (value.copy((char*) tmp, get_length(), charset()))
 
1694
    {
 
1695
      Field_blob::reset();
 
1696
      return 1;
 
1697
    }
 
1698
    tmp=(uchar*) value.ptr();
 
1699
    memcpy_fixed(ptr+packlength,&tmp,sizeof(char*));
 
1700
    return 0;
 
1701
  }
 
1702
  virtual uchar *pack(uchar *to, const uchar *from,
 
1703
                      uint max_length, bool low_byte_first);
 
1704
  uchar *pack_key(uchar *to, const uchar *from,
 
1705
                  uint max_length, bool low_byte_first);
 
1706
  uchar *pack_key_from_key_image(uchar* to, const uchar *from,
 
1707
                                 uint max_length, bool low_byte_first);
 
1708
  virtual const uchar *unpack(uchar *to, const uchar *from,
 
1709
                              uint param_data, bool low_byte_first);
 
1710
  const uchar *unpack_key(uchar* to, const uchar *from,
 
1711
                          uint max_length, bool low_byte_first);
 
1712
  int pack_cmp(const uchar *a, const uchar *b, uint key_length,
 
1713
               my_bool insert_or_update);
 
1714
  int pack_cmp(const uchar *b, uint key_length,my_bool insert_or_update);
 
1715
  uint packed_col_length(const uchar *col_ptr, uint length);
 
1716
  uint max_packed_col_length(uint max_length);
 
1717
  void free() { value.free(); }
 
1718
  inline void clear_temporary() { bzero((uchar*) &value,sizeof(value)); }
 
1719
  friend int field_conv(Field *to,Field *from);
 
1720
  uint size_of() const { return sizeof(*this); }
 
1721
  bool has_charset(void) const
 
1722
  { return charset() == &my_charset_bin ? FALSE : TRUE; }
 
1723
  uint32 max_display_length();
 
1724
  uint is_equal(Create_field *new_field);
 
1725
  inline bool in_read_set() { return bitmap_is_set(table->read_set, field_index); }
 
1726
  inline bool in_write_set() { return bitmap_is_set(table->write_set, field_index); }
 
1727
private:
 
1728
  int do_save_field_metadata(uchar *first_byte);
 
1729
};
 
1730
 
 
1731
 
 
1732
class Field_enum :public Field_str {
 
1733
protected:
 
1734
  uint packlength;
 
1735
public:
 
1736
  TYPELIB *typelib;
 
1737
  Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1738
             uchar null_bit_arg,
 
1739
             enum utype unireg_check_arg, const char *field_name_arg,
 
1740
             uint packlength_arg,
 
1741
             TYPELIB *typelib_arg,
 
1742
             CHARSET_INFO *charset_arg)
 
1743
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1744
               unireg_check_arg, field_name_arg, charset_arg),
 
1745
    packlength(packlength_arg),typelib(typelib_arg)
 
1746
  {
 
1747
      flags|=ENUM_FLAG;
 
1748
  }
 
1749
  Field *new_field(MEM_ROOT *root, struct st_table *new_table, bool keep_type);
 
1750
  enum_field_types type() const { return MYSQL_TYPE_STRING; }
 
1751
  enum Item_result cmp_type () const { return INT_RESULT; }
 
1752
  enum Item_result cast_to_int_type () const { return INT_RESULT; }
 
1753
  enum ha_base_keytype key_type() const;
 
1754
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1755
  int  store(double nr);
 
1756
  int  store(longlong nr, bool unsigned_val);
 
1757
  double val_real(void);
 
1758
  longlong val_int(void);
 
1759
  String *val_str(String*,String *);
 
1760
  int cmp(const uchar *,const uchar *);
 
1761
  void sort_string(uchar *buff,uint length);
 
1762
  uint32 pack_length() const { return (uint32) packlength; }
 
1763
  void store_type(uint64_t value);
 
1764
  void sql_type(String &str) const;
 
1765
  uint size_of() const { return sizeof(*this); }
 
1766
  enum_field_types real_type() const { return MYSQL_TYPE_ENUM; }
 
1767
  uint pack_length_from_metadata(uint field_metadata)
 
1768
  { return (field_metadata & 0x00ff); }
 
1769
  uint row_pack_length() { return pack_length(); }
 
1770
  virtual bool zero_pack() const { return 0; }
 
1771
  bool optimize_range(uint idx, uint part) { return 0; }
 
1772
  bool eq_def(Field *field);
 
1773
  bool has_charset(void) const { return TRUE; }
 
1774
  /* enum and set are sorted as integers */
 
1775
  CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
 
1776
private:
 
1777
  int do_save_field_metadata(uchar *first_byte);
 
1778
};
 
1779
 
 
1780
 
 
1781
class Field_set :public Field_enum {
 
1782
public:
 
1783
  Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1784
            uchar null_bit_arg,
 
1785
            enum utype unireg_check_arg, const char *field_name_arg,
 
1786
            uint32 packlength_arg,
 
1787
            TYPELIB *typelib_arg, CHARSET_INFO *charset_arg)
 
1788
    :Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
1789
                    unireg_check_arg, field_name_arg,
 
1790
                packlength_arg,
 
1791
                typelib_arg,charset_arg)
 
1792
    {
 
1793
      flags=(flags & ~ENUM_FLAG) | SET_FLAG;
 
1794
    }
 
1795
  int  store(const char *to,uint length,CHARSET_INFO *charset);
 
1796
  int  store(double nr) { return Field_set::store((longlong) nr, FALSE); }
 
1797
  int  store(longlong nr, bool unsigned_val);
 
1798
 
 
1799
  virtual bool zero_pack() const { return 1; }
 
1800
  String *val_str(String*,String *);
 
1801
  void sql_type(String &str) const;
 
1802
  enum_field_types real_type() const { return MYSQL_TYPE_SET; }
 
1803
  bool has_charset(void) const { return TRUE; }
 
1804
};
 
1805
 
 
1806
 
 
1807
/*
 
1808
  Note:
 
1809
    To use Field_bit::cmp_binary() you need to copy the bits stored in
 
1810
    the beginning of the record (the NULL bytes) to each memory you
 
1811
    want to compare (where the arguments point).
 
1812
 
 
1813
    This is the reason:
 
1814
    - Field_bit::cmp_binary() is only implemented in the base class
 
1815
      (Field::cmp_binary()).
 
1816
    - Field::cmp_binary() currenly use pack_length() to calculate how
 
1817
      long the data is.
 
1818
    - pack_length() includes size of the bits stored in the NULL bytes
 
1819
      of the record.
 
1820
*/
 
1821
class Field_bit :public Field {
 
1822
public:
 
1823
  uchar *bit_ptr;     // position in record where 'uneven' bits store
 
1824
  uchar bit_ofs;      // offset to 'uneven' high bits
 
1825
  uint bit_len;       // number of 'uneven' high bits
 
1826
  uint bytes_in_rec;
 
1827
  Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1828
            uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
 
1829
            enum utype unireg_check_arg, const char *field_name_arg);
 
1830
  enum_field_types type() const { return MYSQL_TYPE_BIT; }
 
1831
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BIT; }
 
1832
  uint32 key_length() const { return (uint32) (field_length + 7) / 8; }
 
1833
  uint32 max_data_length() const { return (field_length + 7) / 8; }
 
1834
  uint32 max_display_length() { return field_length; }
 
1835
  uint size_of() const { return sizeof(*this); }
 
1836
  Item_result result_type () const { return INT_RESULT; }
 
1837
  int reset(void) { bzero(ptr, bytes_in_rec); return 0; }
 
1838
  int store(const char *to, uint length, CHARSET_INFO *charset);
 
1839
  int store(double nr);
 
1840
  int store(longlong nr, bool unsigned_val);
 
1841
  int store_decimal(const my_decimal *);
 
1842
  double val_real(void);
 
1843
  longlong val_int(void);
 
1844
  String *val_str(String*, String *);
 
1845
  virtual bool str_needs_quotes() { return TRUE; }
 
1846
  my_decimal *val_decimal(my_decimal *);
 
1847
  int cmp(const uchar *a, const uchar *b)
 
1848
  { 
 
1849
    DBUG_ASSERT(ptr == a);
 
1850
    return Field_bit::key_cmp(b, bytes_in_rec+test(bit_len));
 
1851
  }
 
1852
  int cmp_binary_offset(uint row_offset)
 
1853
  { return cmp_offset(row_offset); }
 
1854
  int cmp_max(const uchar *a, const uchar *b, uint max_length);
 
1855
  int key_cmp(const uchar *a, const uchar *b)
 
1856
  { return cmp_binary((uchar *) a, (uchar *) b); }
 
1857
  int key_cmp(const uchar *str, uint length);
 
1858
  int cmp_offset(uint row_offset);
 
1859
  void get_image(uchar *buff, uint length, CHARSET_INFO *cs)
 
1860
  { get_key_image(buff, length, itRAW); }   
 
1861
  void set_image(const uchar *buff,uint length, CHARSET_INFO *cs)
 
1862
  { Field_bit::store((char *) buff, length, cs); }
 
1863
  uint get_key_image(uchar *buff, uint length, imagetype type);
 
1864
  void set_key_image(const uchar *buff, uint length)
 
1865
  { Field_bit::store((char*) buff, length, &my_charset_bin); }
 
1866
  void sort_string(uchar *buff, uint length)
 
1867
  { get_key_image(buff, length, itRAW); }
 
1868
  uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
 
1869
  uint32 pack_length_in_rec() const { return bytes_in_rec; }
 
1870
  uint pack_length_from_metadata(uint field_metadata);
 
1871
  uint row_pack_length()
 
1872
  { return (bytes_in_rec + ((bit_len > 0) ? 1 : 0)); }
 
1873
  int compatible_field_size(uint field_metadata);
 
1874
  void sql_type(String &str) const;
 
1875
  virtual uchar *pack(uchar *to, const uchar *from,
 
1876
                      uint max_length, bool low_byte_first);
 
1877
  virtual const uchar *unpack(uchar *to, const uchar *from,
 
1878
                              uint param_data, bool low_byte_first);
 
1879
  virtual void set_default();
 
1880
 
 
1881
  Field *new_key_field(MEM_ROOT *root, struct st_table *new_table,
 
1882
                       uchar *new_ptr, uchar *new_null_ptr,
 
1883
                       uint new_null_bit);
 
1884
  void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg)
 
1885
  {
 
1886
    bit_ptr= bit_ptr_arg;
 
1887
    bit_ofs= bit_ofs_arg;
 
1888
  }
 
1889
  bool eq(Field *field)
 
1890
  {
 
1891
    return (Field::eq(field) &&
 
1892
            field->type() == type() &&
 
1893
            bit_ptr == ((Field_bit *)field)->bit_ptr &&
 
1894
            bit_ofs == ((Field_bit *)field)->bit_ofs);
 
1895
  }
 
1896
  uint is_equal(Create_field *new_field);
 
1897
  void move_field_offset(my_ptrdiff_t ptr_diff)
 
1898
  {
 
1899
    Field::move_field_offset(ptr_diff);
 
1900
    bit_ptr= ADD_TO_PTR(bit_ptr, ptr_diff, uchar*);
 
1901
  }
 
1902
  void hash(ulong *nr, ulong *nr2);
 
1903
 
 
1904
private:
 
1905
  virtual size_t do_last_null_byte() const;
 
1906
  int do_save_field_metadata(uchar *first_byte);
 
1907
};
 
1908
 
 
1909
 
 
1910
/**
 
1911
  BIT field represented as chars for non-MyISAM tables.
 
1912
 
 
1913
  @todo The inheritance relationship is backwards since Field_bit is
 
1914
  an extended version of Field_bit_as_char and not the other way
 
1915
  around. Hence, we should refactor it to fix the hierarchy order.
 
1916
 */
 
1917
class Field_bit_as_char: public Field_bit {
 
1918
public:
 
1919
  Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
 
1920
                    uchar null_bit_arg,
 
1921
                    enum utype unireg_check_arg, const char *field_name_arg);
 
1922
  enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
 
1923
  uint size_of() const { return sizeof(*this); }
 
1924
  int store(const char *to, uint length, CHARSET_INFO *charset);
 
1925
  int store(double nr) { return Field_bit::store(nr); }
 
1926
  int store(longlong nr, bool unsigned_val)
 
1927
  { return Field_bit::store(nr, unsigned_val); }
 
1928
  void sql_type(String &str) const;
 
1929
};
 
1930
 
 
1931
 
603
1932
/*
604
1933
  Create field class for CREATE TABLE
605
1934
*/
617
1946
    At various stages in execution this can be length of field in bytes or
618
1947
    max number of characters. 
619
1948
  */
620
 
  uint32_t length;
 
1949
  ulong length;
621
1950
  /*
622
1951
    The value of `length' as set by parser: is the number of characters
623
1952
    for most of the types, or of bytes for BLOBs or numeric types.
624
1953
  */
625
 
  uint32_t char_length;
626
 
  uint32_t  decimals, flags, pack_length, key_length;
 
1954
  uint32 char_length;
 
1955
  uint  decimals, flags, pack_length, key_length;
627
1956
  Field::utype unireg_check;
628
1957
  TYPELIB *interval;                    // Which interval to use
629
1958
  TYPELIB *save_interval;               // Temporary copy for the above
630
1959
                                        // Used only for UCS2 intervals
631
1960
  List<String> interval_list;
632
 
  const CHARSET_INFO *charset;
 
1961
  CHARSET_INFO *charset;
633
1962
  Field *field;                         // For alter table
634
1963
 
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
 
 
 
1964
  uint8 row,col,sc_length,interval_id;  // For rea_create_table
 
1965
  uint  offset,pack_flag;
647
1966
  Create_field() :after(0) {}
648
1967
  Create_field(Field *field, Field *orig_field);
649
1968
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
651
1970
    { return new (mem_root) Create_field(*this); }
652
1971
  void create_length_to_internal_length(void);
653
1972
 
 
1973
  inline  enum ha_storage_media field_storage_type() const
 
1974
  {
 
1975
    return (enum ha_storage_media)
 
1976
      ((flags >> FIELD_STORAGE_FLAGS) & STORAGE_TYPE_MASK);
 
1977
  }
 
1978
 
654
1979
  inline enum column_format_type column_format() const
655
1980
  {
656
1981
    return (enum column_format_type)
659
1984
 
660
1985
  /* Init for a tmp table field. To be extended if need be. */
661
1986
  void init_for_tmp_table(enum_field_types sql_type_arg,
662
 
                          uint32_t max_length, uint32_t decimals,
 
1987
                          uint32 max_length, uint32 decimals,
663
1988
                          bool maybe_null, bool is_unsigned);
664
1989
 
665
1990
  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
666
 
            char *decimals, uint32_t type_modifier, Item *default_value,
 
1991
            char *decimals, uint type_modifier, Item *default_value,
667
1992
            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);
 
1993
            List<String> *interval_list, CHARSET_INFO *cs,
 
1994
            uint uint_geom_type,
 
1995
            enum ha_storage_media storage_type,
 
1996
            enum column_format_type column_format);
672
1997
};
673
1998
 
674
1999
 
681
2006
  const char *db_name;
682
2007
  const char *table_name,*org_table_name;
683
2008
  const char *col_name,*org_col_name;
684
 
  uint32_t length;
685
 
  uint32_t charsetnr, flags, decimals;
 
2009
  ulong length;
 
2010
  uint charsetnr, flags, decimals;
686
2011
  enum_field_types type;
687
2012
  Send_field() {}
688
2013
};
700
2025
  typedef void Copy_func(Copy_field*);
701
2026
  Copy_func *get_copy_func(Field *to, Field *from);
702
2027
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;
 
2028
  uchar *from_ptr,*to_ptr;
 
2029
  uchar *from_null_ptr,*to_null_ptr;
 
2030
  my_bool *null_row;
 
2031
  uint  from_bit,to_bit;
 
2032
  uint from_length,to_length;
708
2033
  Field *from_field,*to_field;
709
2034
  String tmp;                                   // For items
710
2035
 
711
2036
  Copy_field() {}
712
2037
  ~Copy_field() {}
713
2038
  void set(Field *to,Field *from,bool save);    // Field to field 
714
 
  void set(unsigned char *to,Field *from);              // Field to string
 
2039
  void set(uchar *to,Field *from);              // Field to string
715
2040
  void (*do_copy)(Copy_field *);
716
2041
  void (*do_copy2)(Copy_field *);               // Used to handle null values
717
2042
};
718
2043
 
719
2044
 
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,
 
2045
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
 
2046
                  uchar *null_pos, uchar null_bit,
 
2047
                  uint pack_flag, enum_field_types field_type,
 
2048
                  CHARSET_INFO *cs,
724
2049
                  Field::utype unireg_check,
725
2050
                  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);
 
2051
uint pack_length_to_packflag(uint type);
 
2052
enum_field_types get_blob_type_from_length(ulong length);
 
2053
uint32 calc_pack_length(enum_field_types type,uint32 length);
729
2054
int set_field_to_null(Field *field);
730
2055
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
731
2056
 
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
2057
/*
761
2058
  The following are for the interface with the .frm file
762
2059
*/
764
2061
#define FIELDFLAG_DECIMAL               1
765
2062
#define FIELDFLAG_BINARY                1       // Shares same flag
766
2063
#define FIELDFLAG_NUMBER                2
767
 
#define FIELDFLAG_DECIMAL_POSITION      4
 
2064
#define FIELDFLAG_ZEROFILL              4
768
2065
#define FIELDFLAG_PACK                  120     // Bits used for packing
769
2066
#define FIELDFLAG_INTERVAL              256     // mangled with decimals!
770
2067
#define FIELDFLAG_BITFIELD              512     // mangled with decimals!
777
2074
#define FIELDFLAG_RIGHT_FULLSCREEN      16384
778
2075
#define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
779
2076
#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)
 
2077
#define FIELDFLAG_SUM                   ((uint) 32768)// predit: +#fieldflag
 
2078
#define FIELDFLAG_MAYBE_NULL            ((uint) 32768)// sql
 
2079
#define FIELDFLAG_HEX_ESCAPE            ((uint) 0x10000)
783
2080
#define FIELDFLAG_PACK_SHIFT            3
784
2081
#define FIELDFLAG_DEC_SHIFT             8
785
2082
#define FIELDFLAG_MAX_DEC               31
790
2087
 
791
2088
#define f_is_dec(x)             ((x) & FIELDFLAG_DECIMAL)
792
2089
#define f_is_num(x)             ((x) & FIELDFLAG_NUMBER)
793
 
#define f_is_decimal_precision(x)       ((x) & FIELDFLAG_DECIMAL_POSITION)
 
2090
#define f_is_zerofill(x)        ((x) & FIELDFLAG_ZEROFILL)
794
2091
#define f_is_packed(x)          ((x) & FIELDFLAG_PACK)
795
2092
#define f_packtype(x)           (((x) >> FIELDFLAG_PACK_SHIFT) & 15)
796
 
#define f_decimals(x)           ((uint8_t) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
 
2093
#define f_decimals(x)           ((uint8) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
797
2094
#define f_is_alpha(x)           (!f_is_num(x))
798
2095
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
799
2096
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
805
2102
#define f_no_default(x)         (x & FIELDFLAG_NO_DEFAULT)
806
2103
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
807
2104
#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
 
};