~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Brian Aker
  • Date: 2008-11-18 23:19:19 UTC
  • mfrom: (584.1.16 devel)
  • Revision ID: brian@tangent.org-20081118231919-w9sr347dtiwhccml
Merge of Monty's work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
#include <drizzled/sql_error.h>
29
29
#include <drizzled/my_decimal.h>
 
30
#include <drizzled/sql_bitmap.h>
 
31
#include <drizzled/sql_list.h>
 
32
#include <drizzled/lex_string.h>
30
33
 
31
34
#define DATETIME_DEC                     6
32
35
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
33
36
 
34
37
const uint32_t max_field_size= (uint32_t) 4294967295U;
35
38
 
 
39
class Table;
36
40
class Send_field;
37
41
class Protocol;
38
42
class Create_field;
 
43
class virtual_column_info;
 
44
 
 
45
typedef struct st_table_share TABLE_SHARE;
 
46
 
39
47
struct st_cache_field;
40
48
int field_conv(Field *to,Field *from);
41
49
 
50
58
  return len > 4 ? 8 : len;
51
59
}
52
60
 
53
 
class virtual_column_info: public Sql_alloc
54
 
{
55
 
public:
56
 
  Item *expr_item;
57
 
  LEX_STRING expr_str;
58
 
  Item *item_free_list;
59
 
  virtual_column_info() 
60
 
  : expr_item(0), item_free_list(0),
61
 
    field_type(DRIZZLE_TYPE_VIRTUAL),
62
 
    is_stored(false), data_inited(false)
63
 
  {
64
 
    expr_str.str= NULL;
65
 
    expr_str.length= 0;
66
 
  };
67
 
  ~virtual_column_info() {}
68
 
  enum_field_types get_real_type()
69
 
  {
70
 
    assert(data_inited);
71
 
    return data_inited ? field_type : DRIZZLE_TYPE_VIRTUAL;
72
 
  }
73
 
  void set_field_type(enum_field_types fld_type)
74
 
  {
75
 
    /* Calling this function can only be done once. */
76
 
    assert(not data_inited);
77
 
    data_inited= true;
78
 
    field_type= fld_type;
79
 
  }
80
 
  bool get_field_stored()
81
 
  {
82
 
    assert(data_inited);
83
 
    return data_inited ? is_stored : true;
84
 
  }
85
 
  void set_field_stored(bool stored)
86
 
  {
87
 
    is_stored= stored;
88
 
  }
89
 
private:
90
 
  /*
91
 
    The following data is only updated by the parser and read
92
 
    when a Create_field object is created/initialized.
93
 
  */
94
 
  enum_field_types field_type;   /* Real field type*/
95
 
  bool is_stored;             /* Indication that the field is 
96
 
                                    phisically stored in the database*/
97
 
  /*
98
 
    This flag is used to prevent other applications from
99
 
    reading and using incorrect data.
100
 
  */
101
 
  bool data_inited; 
102
 
};
103
 
 
104
61
class Field
105
62
{
106
63
  Field(const Item &);                          /* Prevent use of these */
124
81
  /* Field is part of the following keys */
125
82
  key_map       key_start, part_of_key, part_of_key_not_clustered;
126
83
  key_map       part_of_sortkey;
127
 
  /* 
128
 
    We use three additional unireg types for TIMESTAMP to overcome limitation 
129
 
    of current binary format of .frm file. We'd like to be able to support 
130
 
    NOW() as default and on update value for such fields but unable to hold 
 
84
  /*
 
85
    We use three additional unireg types for TIMESTAMP to overcome limitation
 
86
    of current binary format of .frm file. We'd like to be able to support
 
87
    NOW() as default and on update value for such fields but unable to hold
131
88
    this info anywhere except unireg_check field. This issue will be resolved
132
89
    in more clean way with transition to new text based .frm format.
133
90
    See also comment for Field_timestamp::Field_timestamp().
134
91
  */
135
92
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
136
 
                CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
 
93
                CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
137
94
                BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
138
95
                TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
139
96
  enum imagetype { itRAW, itMBR};
157
114
  /* Virtual column data */
158
115
  virtual_column_info *vcol_info;
159
116
  /*
160
 
    Indication that the field is phycically stored in tables 
 
117
    Indication that the field is physically stored in tables
161
118
    rather than just generated on SQL queries.
162
119
    As of now, false can only be set for generated-only virtual columns.
163
120
  */
168
125
        const char *field_name_arg);
169
126
  virtual ~Field() {}
170
127
  /* Store functions returns 1 on overflow and -1 on fatal error */
171
 
  virtual int  store(const char *to, uint32_t length, const CHARSET_INFO * const cs)=0;
 
128
  virtual int  store(const char *to, uint32_t length,
 
129
                     const CHARSET_INFO * const cs)=0;
172
130
  virtual int  store(double nr)=0;
173
131
  virtual int  store(int64_t nr, bool unsigned_val)=0;
174
132
  virtual int  store_decimal(const my_decimal *d)=0;
175
 
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
 
133
  virtual int store_time(DRIZZLE_TIME *ltime,
 
134
                         enum enum_drizzle_timestamp_type t_type);
176
135
  int store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
177
136
            enum_check_fields check_level);
178
137
  virtual double val_real(void)=0;
201
160
  virtual Item_result result_type () const=0;
202
161
  virtual Item_result cmp_type () const { return result_type(); }
203
162
  virtual Item_result cast_to_int_type () const { return result_type(); }
 
163
  /**
 
164
     Check whether a field type can be partially indexed by a key.
 
165
 
 
166
     This is a static method, rather than a virtual function, because we need
 
167
     to check the type of a non-Field in mysql_alter_table().
 
168
 
 
169
     @param type  field type
 
170
 
 
171
     @retval
 
172
       true  Type can have a prefixed key
 
173
     @retval
 
174
       false Type can not have a prefixed key
 
175
  */
204
176
  static bool type_can_have_key_part(enum_field_types);
205
177
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
 
178
 
 
179
  /**
 
180
     Detect Item_result by given field type of UNION merge result.
 
181
 
 
182
     @param field_type  given field type
 
183
 
 
184
     @return
 
185
       Item_result (type of internal MySQL expression result)
 
186
  */
206
187
  static Item_result result_merge_type(enum_field_types);
207
 
  virtual bool eq(Field *field)
208
 
  {
209
 
    return (ptr == field->ptr && null_ptr == field->null_ptr &&
210
 
            null_bit == field->null_bit);
211
 
  }
 
188
 
 
189
  virtual bool eq(Field *field);
212
190
  virtual bool eq_def(Field *field);
213
 
  
 
191
 
214
192
  /*
215
193
    pack_length() returns size (in bytes) used to store field data in memory
216
194
    (i.e. it returns the maximum size of the field in a row of the table,
217
195
    which is located in RAM).
218
196
  */
219
 
  virtual uint32_t pack_length() const { return (uint32_t) field_length; }
 
197
  virtual uint32_t pack_length() const;
220
198
 
221
199
  /*
222
200
    pack_length_in_rec() returns size (in bytes) used to store field data on
223
201
    storage (i.e. it returns the maximal size of the field in a row of the
224
202
    table, which is located on disk).
225
203
  */
226
 
  virtual uint32_t pack_length_in_rec() const { return pack_length(); }
 
204
  virtual uint32_t pack_length_in_rec() const;
227
205
  virtual int compatible_field_size(uint32_t field_metadata);
228
 
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata)
229
 
  { return field_metadata; }
 
206
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata);
 
207
 
230
208
  /*
231
209
    This method is used to return the size of the data in a row-based
232
210
    replication row record. The default implementation of returning 0 is
233
211
    designed to allow fields that do not use metadata to return true (1)
234
212
    from compatible_field_size() which uses this function in the comparison.
235
 
    The default value for field metadata for fields that do not have 
 
213
    The default value for field metadata for fields that do not have
236
214
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
237
215
 
238
216
    Note: While most classes that override this method return pack_length(),
239
 
    the classes Field_varstring, and Field_blob return 
 
217
    the classes Field_varstring, and Field_blob return
240
218
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
241
219
  */
242
 
  virtual uint32_t row_pack_length() { return 0; }
243
 
  virtual int save_field_metadata(unsigned char *first_byte)
244
 
  { return do_save_field_metadata(first_byte); }
 
220
  virtual uint32_t row_pack_length();
 
221
  virtual int save_field_metadata(unsigned char *first_byte);
245
222
 
246
223
  /*
247
224
    data_length() return the "real size" of the data in memory.
248
225
    For varstrings, this does _not_ include the length bytes.
249
226
  */
250
 
  virtual uint32_t data_length() { return pack_length(); }
 
227
  virtual uint32_t data_length();
251
228
  /*
252
229
    used_length() returns the number of bytes actually used to store the data
253
230
    of the field. So for a varstring it includes both lenght byte(s) and
254
231
    string data, and anything after data_length() bytes are unused.
255
232
  */
256
 
  virtual uint32_t used_length() { return pack_length(); }
257
 
  virtual uint32_t sort_length() const { return pack_length(); }
 
233
  virtual uint32_t used_length();
 
234
  virtual uint32_t sort_length() const;
258
235
 
259
236
  /**
260
237
     Get the maximum size of the data in packed format.
262
239
     @return Maximum data length of the field when packed using the
263
240
     Field::pack() function.
264
241
   */
265
 
  virtual uint32_t max_data_length() const {
266
 
    return pack_length();
267
 
  };
268
 
 
269
 
  virtual int reset(void) { memset(ptr, 0, pack_length()); return 0; }
270
 
  virtual void reset_fields() {}
271
 
  virtual void set_default()
272
 
  {
273
 
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
274
 
    memcpy(ptr, ptr + l_offset, pack_length());
275
 
    if (null_ptr)
276
 
      *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
277
 
  }
278
 
  virtual bool binary() const { return 1; }
279
 
  virtual bool zero_pack() const { return 1; }
280
 
  virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
281
 
  virtual uint32_t key_length() const { return pack_length(); }
 
242
  virtual uint32_t max_data_length() const;
 
243
  virtual int reset(void);
 
244
  virtual void reset_fields();
 
245
  virtual void set_default();
 
246
  virtual bool binary() const;
 
247
  virtual bool zero_pack() const;
 
248
  virtual enum ha_base_keytype key_type() const;
 
249
  virtual uint32_t key_length() const;
282
250
  virtual enum_field_types type() const =0;
283
 
  virtual enum_field_types real_type() const { return type(); }
 
251
  virtual enum_field_types real_type() const;
284
252
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
285
253
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
286
 
                      uint32_t max_len __attribute__((unused)))
287
 
    { return cmp(a, b); }
 
254
                      uint32_t max_len);
288
255
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
289
256
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
290
 
                         uint32_t  __attribute__((unused)) max_length=UINT32_MAX)
291
 
  { return memcmp(a,b,pack_length()); }
292
 
  virtual int cmp_offset(uint32_t row_offset)
293
 
  { return cmp(ptr,ptr+row_offset); }
294
 
  virtual int cmp_binary_offset(uint32_t row_offset)
295
 
  { return cmp_binary(ptr, ptr+row_offset); };
296
 
  virtual int key_cmp(const unsigned char *a,const unsigned char *b)
297
 
  { return cmp(a, b); }
298
 
  virtual int key_cmp(const unsigned char *str, uint32_t length __attribute__((unused)))
299
 
  { return cmp(ptr,str); }
300
 
  virtual uint32_t decimals() const { return 0; }
 
257
                         uint32_t max_length=UINT32_MAX);
 
258
  virtual int cmp_offset(uint32_t row_offset);
 
259
  virtual int cmp_binary_offset(uint32_t row_offset);
 
260
  virtual int key_cmp(const unsigned char *a,const unsigned char *b);
 
261
  virtual int key_cmp(const unsigned char *str, uint32_t length);
 
262
  virtual uint32_t decimals() const;
 
263
 
301
264
  /*
302
265
    Caller beware: sql_type can change str.Ptr, so check
303
266
    ptr() to see if it changed if you are using your own buffer
304
267
    in str and restore it with set() if needed
305
268
  */
306
269
  virtual void sql_type(String &str) const =0;
307
 
  virtual uint32_t size_of() const =0;          // For new field
308
 
  inline bool is_null(my_ptrdiff_t row_offset= 0)
309
 
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
310
 
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
311
 
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
312
 
  inline bool is_null_in_record(const unsigned char *record)
313
 
  {
314
 
    if (!null_ptr)
315
 
      return 0;
316
 
    return test(record[(uint32_t) (null_ptr -table->record[0])] &
317
 
                null_bit);
318
 
  }
319
 
  inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
320
 
  {
321
 
    if (!null_ptr)
322
 
      return 0;
323
 
    return test(null_ptr[offset] & null_bit);
324
 
  }
325
 
  inline void set_null(my_ptrdiff_t row_offset= 0)
326
 
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
327
 
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
328
 
    { if (null_ptr) null_ptr[row_offset]&= (unsigned char) ~null_bit; }
329
 
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
330
 
  inline bool real_maybe_null(void) { return null_ptr != 0; }
 
270
 
 
271
  // For new field
 
272
  virtual uint32_t size_of() const =0;
 
273
 
 
274
  bool is_null(my_ptrdiff_t row_offset= 0);
 
275
  bool is_real_null(my_ptrdiff_t row_offset= 0);
 
276
  bool is_null_in_record(const unsigned char *record);
 
277
  bool is_null_in_record_with_offset(my_ptrdiff_t offset);
 
278
  void set_null(my_ptrdiff_t row_offset= 0);
 
279
  void set_notnull(my_ptrdiff_t row_offset= 0);
 
280
  bool maybe_null(void);
 
281
  bool real_maybe_null(void);
331
282
 
332
283
  enum {
333
284
    LAST_NULL_BYTE_UNDEF= 0
348
299
      the record. If the field does not use any bits of the null
349
300
      bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
350
301
   */
351
 
  size_t last_null_byte() const {
352
 
    size_t bytes= do_last_null_byte();
353
 
    assert(bytes <= table->getNullBytes());
354
 
    return bytes;
355
 
  }
 
302
  size_t last_null_byte() const;
356
303
 
357
304
  virtual void make_field(Send_field *);
358
305
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
452
399
  virtual bool send_binary(Protocol *protocol);
453
400
 
454
401
  virtual unsigned char *pack(unsigned char *to, const unsigned char *from,
455
 
                      uint32_t max_length, bool low_byte_first);
 
402
                              uint32_t max_length, bool low_byte_first);
456
403
  /**
457
 
     @overload Field::pack(unsigned char*, const unsigned char*, uint32_t, bool)
 
404
     @overload Field::pack(unsigned char*, const unsigned char*,
 
405
                           uint32_t, bool)
458
406
  */
459
 
  unsigned char *pack(unsigned char *to, const unsigned char *from)
460
 
  {
461
 
    unsigned char *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
462
 
    return(result);
463
 
  }
 
407
  unsigned char *pack(unsigned char *to, const unsigned char *from);
464
408
 
465
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
466
 
                              uint32_t param_data, bool low_byte_first);
 
409
  virtual const unsigned char *unpack(unsigned char* to,
 
410
                                      const unsigned char *from,
 
411
                                      uint32_t param_data,
 
412
                                      bool low_byte_first);
467
413
  /**
468
 
     @overload Field::unpack(unsigned char*, const unsigned char*, uint32_t, bool)
 
414
     @overload Field::unpack(unsigned char*, const unsigned char*,
 
415
                             uint32_t, bool)
469
416
  */
470
 
  const unsigned char *unpack(unsigned char* to, const unsigned char *from)
471
 
  {
472
 
    const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
473
 
    return(result);
474
 
  }
 
417
  const unsigned char *unpack(unsigned char* to, const unsigned char *from);
475
418
 
476
419
  virtual unsigned char *pack_key(unsigned char* to, const unsigned char *from,
477
420
                          uint32_t max_length, bool low_byte_first)
478
421
  {
479
422
    return pack(to, from, max_length, low_byte_first);
480
423
  }
481
 
  virtual unsigned char *pack_key_from_key_image(unsigned char* to, const unsigned char *from,
482
 
                                        uint32_t max_length, bool low_byte_first)
 
424
  virtual unsigned char *pack_key_from_key_image(unsigned char* to,
 
425
                                                 const unsigned char *from,
 
426
                                                 uint32_t max_length,
 
427
                                                 bool low_byte_first)
483
428
  {
484
429
    return pack(to, from, max_length, low_byte_first);
485
430
  }
486
 
  virtual const unsigned char *unpack_key(unsigned char* to, const unsigned char *from,
487
 
                                  uint32_t max_length, bool low_byte_first)
 
431
  virtual const unsigned char *unpack_key(unsigned char* to,
 
432
                                          const unsigned char *from,
 
433
                                          uint32_t max_length,
 
434
                                          bool low_byte_first)
488
435
  {
489
436
    return unpack(to, from, max_length, low_byte_first);
490
437
  }
491
 
  virtual uint32_t packed_col_length(const unsigned char *to __attribute__((unused)),
492
 
                                 uint32_t length)
493
 
  { return length;}
 
438
  virtual uint32_t packed_col_length(const unsigned char *to, uint32_t length);
494
439
  virtual uint32_t max_packed_col_length(uint32_t max_length)
495
440
  { return max_length;}
496
441
 
497
442
  virtual int pack_cmp(const unsigned char *a,const unsigned char *b,
498
 
                       uint32_t key_length_arg __attribute__((unused)),
499
 
                       bool insert_or_update __attribute__((unused)))
500
 
  { return cmp(a,b); }
 
443
                       uint32_t key_length_arg,
 
444
                       bool insert_or_update);
501
445
  virtual int pack_cmp(const unsigned char *b,
502
 
                       uint32_t key_length_arg __attribute__((unused)),
503
 
                       bool insert_or_update __attribute__((unused)))
504
 
  { return cmp(ptr,b); }
 
446
                       uint32_t key_length_arg,
 
447
                       bool insert_or_update);
 
448
 
505
449
  uint32_t offset(unsigned char *record)
506
450
  {
507
451
    return (uint32_t) (ptr - record);
534
478
    return (op_result == E_DEC_OVERFLOW);
535
479
  }
536
480
  int warn_if_overflow(int op_result);
537
 
  void init(Table *table_arg)
538
 
  {
539
 
    orig_table= table= table_arg;
540
 
    table_name= &table_arg->alias;
541
 
  }
 
481
  void init(Table *table_arg);
542
482
 
543
483
  /* maximum possible display length */
544
484
  virtual uint32_t max_display_length()= 0;