~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Brian Aker
  • Date: 2009-02-12 22:45:08 UTC
  • Revision ID: brian@tangent.org-20090212224508-mrd9jwgn1zjdpqdk
Minor refactoring (we will need to disconnect the code from the include
file).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  variables must declare the size_of() member function.
23
23
*/
24
24
 
25
 
#ifdef USE_PRAGMA_INTERFACE
26
 
#pragma interface                       /* gcc class implementation */
27
 
#endif
 
25
#ifndef DRIZZLED_FIELD_H
 
26
#define DRIZZLED_FIELD_H
 
27
 
 
28
#include <drizzled/sql_error.h>
 
29
#include <drizzled/my_decimal.h>
 
30
#include <drizzled/sql_bitmap.h>
 
31
#include <drizzled/sql_list.h>
 
32
/* System-wide common data structures */
 
33
#include <drizzled/structs.h>
 
34
#include <string>
28
35
 
29
36
#define DATETIME_DEC                     6
30
37
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
31
38
 
32
39
const uint32_t max_field_size= (uint32_t) 4294967295U;
33
40
 
 
41
class Table;
34
42
class Send_field;
35
43
class Protocol;
36
44
class Create_field;
 
45
class virtual_column_info;
 
46
 
 
47
class TABLE_SHARE;
 
48
 
 
49
class Field;
 
50
 
37
51
struct st_cache_field;
38
52
int field_conv(Field *to,Field *from);
39
53
 
54
68
  void operator=(Field &);
55
69
public:
56
70
  static void *operator new(size_t size) {return sql_alloc(size); }
57
 
  static void operator delete(void *ptr_arg __attribute__((unused)),
58
 
                              size_t size __attribute__((unused)))
 
71
  static void operator delete(void *, size_t)
59
72
  { TRASH(ptr_arg, size); }
60
73
 
61
74
  unsigned char         *ptr;                   // Position to field in record
62
75
  unsigned char         *null_ptr;              // Byte where null_bit is
63
76
  /*
64
 
    Note that you can use table->in_use as replacement for current_thd member 
 
77
    Note that you can use table->in_use as replacement for current_session member
65
78
    only inside of val_*() and store() members (e.g. you can't use it in cons)
66
79
  */
67
80
  Table *table;         // Pointer for table
71
84
  /* Field is part of the following keys */
72
85
  key_map       key_start, part_of_key, part_of_key_not_clustered;
73
86
  key_map       part_of_sortkey;
74
 
  /* 
75
 
    We use three additional unireg types for TIMESTAMP to overcome limitation 
76
 
    of current binary format of .frm file. We'd like to be able to support 
77
 
    NOW() as default and on update value for such fields but unable to hold 
 
87
  /*
 
88
    We use three additional unireg types for TIMESTAMP to overcome limitation
 
89
    of current binary format of .frm file. We'd like to be able to support
 
90
    NOW() as default and on update value for such fields but unable to hold
78
91
    this info anywhere except unireg_check field. This issue will be resolved
79
92
    in more clean way with transition to new text based .frm format.
80
93
    See also comment for Field_timestamp::Field_timestamp().
81
94
  */
82
95
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
83
 
                CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
 
96
                CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
84
97
                BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
85
98
                TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
86
99
  enum imagetype { itRAW, itMBR};
101
114
   */
102
115
  bool is_created_from_null_item;
103
116
 
 
117
  /* Virtual column data */
 
118
  virtual_column_info *vcol_info;
 
119
  /*
 
120
    Indication that the field is physically stored in tables
 
121
    rather than just generated on SQL queries.
 
122
    As of now, false can only be set for generated-only virtual columns.
 
123
  */
 
124
  bool is_stored;
 
125
 
104
126
  Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
105
127
        unsigned char null_bit_arg, utype unireg_check_arg,
106
128
        const char *field_name_arg);
107
129
  virtual ~Field() {}
108
130
  /* Store functions returns 1 on overflow and -1 on fatal error */
109
 
  virtual int  store(const char *to, uint32_t length, const CHARSET_INFO * const cs)=0;
110
 
  virtual int  store(double nr)=0;
111
 
  virtual int  store(int64_t nr, bool unsigned_val)=0;
112
 
  virtual int  store_decimal(const my_decimal *d)=0;
113
 
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
114
 
  int store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
 
131
  virtual int store(const char *to, uint32_t length,
 
132
                    const CHARSET_INFO * const cs)=0;
 
133
  virtual int store(double nr)=0;
 
134
  virtual int store(int64_t nr, bool unsigned_val)=0;
 
135
  virtual int store_decimal(const my_decimal *d)=0;
 
136
  int store(const char *to, uint32_t length,
 
137
            const CHARSET_INFO * const cs,
115
138
            enum_check_fields check_level);
 
139
  virtual int store_time(DRIZZLE_TIME *ltime,
 
140
                         enum enum_drizzle_timestamp_type t_type);
116
141
  virtual double val_real(void)=0;
117
142
  virtual int64_t val_int(void)=0;
118
143
  virtual my_decimal *val_decimal(my_decimal *);
139
164
  virtual Item_result result_type () const=0;
140
165
  virtual Item_result cmp_type () const { return result_type(); }
141
166
  virtual Item_result cast_to_int_type () const { return result_type(); }
 
167
  /**
 
168
     Check whether a field type can be partially indexed by a key.
 
169
 
 
170
     This is a static method, rather than a virtual function, because we need
 
171
     to check the type of a non-Field in mysql_alter_table().
 
172
 
 
173
     @param type  field type
 
174
 
 
175
     @retval
 
176
       true  Type can have a prefixed key
 
177
     @retval
 
178
       false Type can not have a prefixed key
 
179
  */
142
180
  static bool type_can_have_key_part(enum_field_types);
143
181
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
 
182
 
 
183
  /**
 
184
     Detect Item_result by given field type of UNION merge result.
 
185
 
 
186
     @param field_type  given field type
 
187
 
 
188
     @return
 
189
       Item_result (type of internal MySQL expression result)
 
190
  */
144
191
  static Item_result result_merge_type(enum_field_types);
145
 
  virtual bool eq(Field *field)
146
 
  {
147
 
    return (ptr == field->ptr && null_ptr == field->null_ptr &&
148
 
            null_bit == field->null_bit);
149
 
  }
 
192
 
 
193
  virtual bool eq(Field *field);
150
194
  virtual bool eq_def(Field *field);
151
 
  
 
195
 
152
196
  /*
153
197
    pack_length() returns size (in bytes) used to store field data in memory
154
198
    (i.e. it returns the maximum size of the field in a row of the table,
155
199
    which is located in RAM).
156
200
  */
157
 
  virtual uint32_t pack_length() const { return (uint32_t) field_length; }
 
201
  virtual uint32_t pack_length() const;
158
202
 
159
203
  /*
160
204
    pack_length_in_rec() returns size (in bytes) used to store field data on
161
205
    storage (i.e. it returns the maximal size of the field in a row of the
162
206
    table, which is located on disk).
163
207
  */
164
 
  virtual uint32_t pack_length_in_rec() const { return pack_length(); }
 
208
  virtual uint32_t pack_length_in_rec() const;
165
209
  virtual int compatible_field_size(uint32_t field_metadata);
166
 
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata)
167
 
  { return field_metadata; }
 
210
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata);
 
211
 
168
212
  /*
169
213
    This method is used to return the size of the data in a row-based
170
214
    replication row record. The default implementation of returning 0 is
171
215
    designed to allow fields that do not use metadata to return true (1)
172
216
    from compatible_field_size() which uses this function in the comparison.
173
 
    The default value for field metadata for fields that do not have 
 
217
    The default value for field metadata for fields that do not have
174
218
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
175
219
 
176
220
    Note: While most classes that override this method return pack_length(),
177
 
    the classes Field_varstring, and Field_blob return 
 
221
    the classes Field_varstring, and Field_blob return
178
222
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
179
223
  */
180
 
  virtual uint32_t row_pack_length() { return 0; }
181
 
  virtual int save_field_metadata(unsigned char *first_byte)
182
 
  { return do_save_field_metadata(first_byte); }
 
224
  virtual uint32_t row_pack_length();
 
225
  virtual int save_field_metadata(unsigned char *first_byte);
183
226
 
184
227
  /*
185
228
    data_length() return the "real size" of the data in memory.
186
229
    For varstrings, this does _not_ include the length bytes.
187
230
  */
188
 
  virtual uint32_t data_length() { return pack_length(); }
 
231
  virtual uint32_t data_length();
189
232
  /*
190
233
    used_length() returns the number of bytes actually used to store the data
191
234
    of the field. So for a varstring it includes both lenght byte(s) and
192
235
    string data, and anything after data_length() bytes are unused.
193
236
  */
194
 
  virtual uint32_t used_length() { return pack_length(); }
195
 
  virtual uint32_t sort_length() const { return pack_length(); }
 
237
  virtual uint32_t used_length();
 
238
  virtual uint32_t sort_length() const;
196
239
 
197
240
  /**
198
241
     Get the maximum size of the data in packed format.
200
243
     @return Maximum data length of the field when packed using the
201
244
     Field::pack() function.
202
245
   */
203
 
  virtual uint32_t max_data_length() const {
204
 
    return pack_length();
205
 
  };
206
 
 
207
 
  virtual int reset(void) { memset(ptr, 0, pack_length()); return 0; }
208
 
  virtual void reset_fields() {}
209
 
  virtual void set_default()
210
 
  {
211
 
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
212
 
    memcpy(ptr, ptr + l_offset, pack_length());
213
 
    if (null_ptr)
214
 
      *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
215
 
  }
216
 
  virtual bool binary() const { return 1; }
217
 
  virtual bool zero_pack() const { return 1; }
218
 
  virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
219
 
  virtual uint32_t key_length() const { return pack_length(); }
 
246
  virtual uint32_t max_data_length() const;
 
247
  virtual int reset(void);
 
248
  virtual void reset_fields();
 
249
  virtual void set_default();
 
250
  virtual bool binary() const;
 
251
  virtual bool zero_pack() const;
 
252
  virtual enum ha_base_keytype key_type() const;
 
253
  virtual uint32_t key_length() const;
220
254
  virtual enum_field_types type() const =0;
221
 
  virtual enum_field_types real_type() const { return type(); }
 
255
  virtual enum_field_types real_type() const;
222
256
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
223
257
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
224
 
                      uint32_t max_len __attribute__((unused)))
225
 
    { return cmp(a, b); }
 
258
                      uint32_t max_len);
226
259
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
227
260
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
228
 
                         uint32_t  __attribute__((unused)) max_length=UINT32_MAX)
229
 
  { return memcmp(a,b,pack_length()); }
230
 
  virtual int cmp_offset(uint32_t row_offset)
231
 
  { return cmp(ptr,ptr+row_offset); }
232
 
  virtual int cmp_binary_offset(uint32_t row_offset)
233
 
  { return cmp_binary(ptr, ptr+row_offset); };
234
 
  virtual int key_cmp(const unsigned char *a,const unsigned char *b)
235
 
  { return cmp(a, b); }
236
 
  virtual int key_cmp(const unsigned char *str, uint32_t length __attribute__((unused)))
237
 
  { return cmp(ptr,str); }
238
 
  virtual uint32_t decimals() const { return 0; }
 
261
                         uint32_t max_length=UINT32_MAX);
 
262
  virtual int cmp_offset(uint32_t row_offset);
 
263
  virtual int cmp_binary_offset(uint32_t row_offset);
 
264
  virtual int key_cmp(const unsigned char *a,const unsigned char *b);
 
265
  virtual int key_cmp(const unsigned char *str, uint32_t length);
 
266
  virtual uint32_t decimals() const;
 
267
 
239
268
  /*
240
269
    Caller beware: sql_type can change str.Ptr, so check
241
270
    ptr() to see if it changed if you are using your own buffer
242
271
    in str and restore it with set() if needed
243
272
  */
244
273
  virtual void sql_type(String &str) const =0;
245
 
  virtual uint32_t size_of() const =0;          // For new field
246
 
  inline bool is_null(my_ptrdiff_t row_offset= 0)
247
 
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
248
 
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
249
 
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
250
 
  inline bool is_null_in_record(const unsigned char *record)
251
 
  {
252
 
    if (!null_ptr)
253
 
      return 0;
254
 
    return test(record[(uint32_t) (null_ptr -table->record[0])] &
255
 
                null_bit);
256
 
  }
257
 
  inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
258
 
  {
259
 
    if (!null_ptr)
260
 
      return 0;
261
 
    return test(null_ptr[offset] & null_bit);
262
 
  }
263
 
  inline void set_null(my_ptrdiff_t row_offset= 0)
264
 
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
265
 
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
266
 
    { if (null_ptr) null_ptr[row_offset]&= (unsigned char) ~null_bit; }
267
 
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
268
 
  inline bool real_maybe_null(void) { return null_ptr != 0; }
 
274
 
 
275
  // For new field
 
276
  virtual uint32_t size_of() const =0;
 
277
 
 
278
  bool is_null(my_ptrdiff_t row_offset= 0);
 
279
  bool is_real_null(my_ptrdiff_t row_offset= 0);
 
280
  bool is_null_in_record(const unsigned char *record);
 
281
  bool is_null_in_record_with_offset(my_ptrdiff_t offset);
 
282
  void set_null(my_ptrdiff_t row_offset= 0);
 
283
  void set_notnull(my_ptrdiff_t row_offset= 0);
 
284
  bool maybe_null(void);
 
285
  bool real_maybe_null(void);
269
286
 
270
287
  enum {
271
288
    LAST_NULL_BYTE_UNDEF= 0
286
303
      the record. If the field does not use any bits of the null
287
304
      bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
288
305
   */
289
 
  size_t last_null_byte() const {
290
 
    size_t bytes= do_last_null_byte();
291
 
    assert(bytes <= table->getNullBytes());
292
 
    return bytes;
293
 
  }
 
306
  size_t last_null_byte() const;
294
307
 
295
308
  virtual void make_field(Send_field *);
296
309
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
322
335
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
323
336
  }
324
337
  virtual void get_image(unsigned char *buff, uint32_t length,
325
 
                         const CHARSET_INFO * const cs __attribute__((unused)))
 
338
                         const CHARSET_INFO * const)
326
339
    { memcpy(buff,ptr,length); }
 
340
  virtual void get_image(std::basic_string<unsigned char> &buff,
 
341
                         uint32_t length,
 
342
                         const CHARSET_INFO * const)
 
343
    { buff.append(ptr,length); }
327
344
  virtual void set_image(const unsigned char *buff,uint32_t length,
328
 
                         const CHARSET_INFO * const cs __attribute__((unused)))
 
345
                         const CHARSET_INFO * const)
329
346
    { memcpy(ptr,buff,length); }
330
347
 
331
348
 
355
372
      Number of copied bytes (excluding padded zero bytes -- see above).
356
373
  */
357
374
 
358
 
  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length,
359
 
                             imagetype type __attribute__((unused)))
 
375
  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length, imagetype)
 
376
  {
 
377
    get_image(buff, length, &my_charset_bin);
 
378
    return length;
 
379
  }
 
380
  virtual uint32_t get_key_image(std::basic_string<unsigned char> &buff,
 
381
                                 uint32_t length, imagetype)
360
382
  {
361
383
    get_image(buff, length, &my_charset_bin);
362
384
    return length;
363
385
  }
364
386
  virtual void set_key_image(const unsigned char *buff,uint32_t length)
365
 
    { set_image(buff,length, &my_charset_bin); }
 
387
  { set_image(buff,length, &my_charset_bin); }
366
388
  inline int64_t val_int_offset(uint32_t row_offset)
367
 
    {
368
 
      ptr+=row_offset;
369
 
      int64_t tmp=val_int();
370
 
      ptr-=row_offset;
371
 
      return tmp;
372
 
    }
 
389
  {
 
390
    ptr+=row_offset;
 
391
    int64_t tmp=val_int();
 
392
    ptr-=row_offset;
 
393
    return tmp;
 
394
  }
 
395
 
373
396
  inline int64_t val_int(const unsigned char *new_ptr)
374
397
  {
375
398
    unsigned char *old_ptr= ptr;
387
410
    ptr= old_ptr;
388
411
    return str;
389
412
  }
 
413
 
390
414
  virtual bool send_binary(Protocol *protocol);
391
415
 
392
 
  virtual unsigned char *pack(unsigned char *to, const unsigned char *from,
393
 
                      uint32_t max_length, bool low_byte_first);
394
 
  /**
395
 
     @overload Field::pack(unsigned char*, const unsigned char*, uint32_t, bool)
396
 
  */
397
 
  unsigned char *pack(unsigned char *to, const unsigned char *from)
398
 
  {
399
 
    unsigned char *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
400
 
    return(result);
401
 
  }
402
 
 
403
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
404
 
                              uint32_t param_data, bool low_byte_first);
405
 
  /**
406
 
     @overload Field::unpack(unsigned char*, const unsigned char*, uint32_t, bool)
407
 
  */
408
 
  const unsigned char *unpack(unsigned char* to, const unsigned char *from)
409
 
  {
410
 
    const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
411
 
    return(result);
412
 
  }
 
416
  virtual unsigned char *pack(unsigned char *to,
 
417
                              const unsigned char *from,
 
418
                              uint32_t max_length,
 
419
                              bool low_byte_first);
 
420
 
 
421
  unsigned char *pack(unsigned char *to, const unsigned char *from);
 
422
 
 
423
  virtual const unsigned char *unpack(unsigned char* to,
 
424
                                      const unsigned char *from,
 
425
                                      uint32_t param_data,
 
426
                                      bool low_byte_first);
 
427
  /**
 
428
     @overload Field::unpack(unsigned char*, const unsigned char*,
 
429
                             uint32_t, bool)
 
430
  */
 
431
  const unsigned char *unpack(unsigned char* to,
 
432
                              const unsigned char *from);
413
433
 
414
434
  virtual unsigned char *pack_key(unsigned char* to, const unsigned char *from,
415
435
                          uint32_t max_length, bool low_byte_first)
416
436
  {
417
437
    return pack(to, from, max_length, low_byte_first);
418
438
  }
419
 
  virtual unsigned char *pack_key_from_key_image(unsigned char* to, const unsigned char *from,
420
 
                                        uint32_t max_length, bool low_byte_first)
 
439
  virtual unsigned char *pack_key_from_key_image(unsigned char* to,
 
440
                                                 const unsigned char *from,
 
441
                                                 uint32_t max_length,
 
442
                                                 bool low_byte_first)
421
443
  {
422
444
    return pack(to, from, max_length, low_byte_first);
423
445
  }
424
 
  virtual const unsigned char *unpack_key(unsigned char* to, const unsigned char *from,
425
 
                                  uint32_t max_length, bool low_byte_first)
 
446
  virtual const unsigned char *unpack_key(unsigned char* to,
 
447
                                          const unsigned char *from,
 
448
                                          uint32_t max_length,
 
449
                                          bool low_byte_first)
426
450
  {
427
451
    return unpack(to, from, max_length, low_byte_first);
428
452
  }
429
 
  virtual uint32_t packed_col_length(const unsigned char *to __attribute__((unused)),
430
 
                                 uint32_t length)
431
 
  { return length;}
 
453
  virtual uint32_t packed_col_length(const unsigned char *to, uint32_t length);
432
454
  virtual uint32_t max_packed_col_length(uint32_t max_length)
433
455
  { return max_length;}
434
456
 
435
457
  virtual int pack_cmp(const unsigned char *a,const unsigned char *b,
436
 
                       uint32_t key_length_arg __attribute__((unused)),
437
 
                       bool insert_or_update __attribute__((unused)))
438
 
  { return cmp(a,b); }
 
458
                       uint32_t key_length_arg,
 
459
                       bool insert_or_update);
439
460
  virtual int pack_cmp(const unsigned char *b,
440
 
                       uint32_t key_length_arg __attribute__((unused)),
441
 
                       bool insert_or_update __attribute__((unused)))
442
 
  { return cmp(ptr,b); }
 
461
                       uint32_t key_length_arg,
 
462
                       bool insert_or_update);
 
463
 
443
464
  uint32_t offset(unsigned char *record)
444
465
  {
445
466
    return (uint32_t) (ptr - record);
451
472
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
452
473
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
453
474
  virtual bool has_charset(void) const { return false; }
454
 
  virtual void set_charset(const CHARSET_INFO * const charset_arg __attribute__((unused)))
 
475
  virtual void set_charset(const CHARSET_INFO * const)
455
476
  { }
456
477
  virtual enum Derivation derivation(void) const
457
478
  { return DERIVATION_IMPLICIT; }
458
 
  virtual void set_derivation(enum Derivation derivation_arg __attribute__((unused)))
 
479
  virtual void set_derivation(enum Derivation)
459
480
  { }
460
481
  bool set_warning(DRIZZLE_ERROR::enum_warning_level, unsigned int code,
461
482
                   int cuted_increment);
462
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
 
483
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code,
463
484
                            const char *str, uint32_t str_len,
464
485
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment);
465
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
 
486
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code,
466
487
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
467
488
                            int cuted_increment);
468
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, const uint32_t code, 
 
489
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, const uint32_t code,
469
490
                            double nr, enum enum_drizzle_timestamp_type ts_type);
470
491
  inline bool check_overflow(int op_result)
471
492
  {
472
493
    return (op_result == E_DEC_OVERFLOW);
473
494
  }
474
495
  int warn_if_overflow(int op_result);
475
 
  void init(Table *table_arg)
476
 
  {
477
 
    orig_table= table= table_arg;
478
 
    table_name= &table_arg->alias;
479
 
  }
 
496
  void init(Table *table_arg);
480
497
 
481
498
  /* maximum possible display length */
482
499
  virtual uint32_t max_display_length()= 0;
499
516
 
500
517
  /* Hash value */
501
518
  virtual void hash(uint32_t *nr, uint32_t *nr2);
502
 
  friend bool reopen_table(THD *,Table *,bool);
 
519
  friend bool reopen_table(Session *,Table *,bool);
503
520
  friend int cre_myisam(char * name, register Table *form, uint32_t options,
504
521
                        uint64_t auto_increment_value);
505
522
  friend class Copy_field;
539
556
 
540
557
   @returns 0 no bytes written.
541
558
*/
542
 
  virtual int do_save_field_metadata(unsigned char *metadata_ptr __attribute__((unused)))
 
559
  virtual int do_save_field_metadata(unsigned char *)
543
560
  { return 0; }
544
561
};
545
562
 
546
 
 
547
 
class Field_num :public Field {
548
 
public:
549
 
  const uint8_t dec;
550
 
  bool decimal_precision;       // Purify cannot handle bit fields & only for decimal type
551
 
  bool unsigned_flag;   // Purify cannot handle bit fields
552
 
  Field_num(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
553
 
            unsigned char null_bit_arg, utype unireg_check_arg,
554
 
            const char *field_name_arg,
555
 
            uint8_t dec_arg, bool zero_arg, bool unsigned_arg);
556
 
  Item_result result_type () const { return REAL_RESULT; }
557
 
  void add_unsigned(String &res) const;
558
 
  friend class Create_field;
559
 
  void make_field(Send_field *);
560
 
  uint32_t decimals() const { return (uint32_t) dec; }
561
 
  uint32_t size_of() const { return sizeof(*this); }
562
 
  bool eq_def(Field *field);
563
 
  int store_decimal(const my_decimal *);
564
 
  my_decimal *val_decimal(my_decimal *);
565
 
  uint32_t is_equal(Create_field *new_field);
566
 
  int check_int(const CHARSET_INFO * const cs, const char *str, int length,
567
 
                const char *int_end, int error);
568
 
  bool get_int(const CHARSET_INFO * const cs, const char *from, uint32_t len, 
569
 
               int64_t *rnd, uint64_t unsigned_max, 
570
 
               int64_t signed_min, int64_t signed_max);
571
 
};
572
 
 
573
 
/* base class for all string related classes */
574
 
 
575
 
class Field_str :public Field {
576
 
protected:
577
 
  const CHARSET_INFO *field_charset;
578
 
  enum Derivation field_derivation;
579
 
public:
580
 
  Field_str(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
581
 
            unsigned char null_bit_arg, utype unireg_check_arg,
582
 
            const char *field_name_arg, const CHARSET_INFO * const charset);
583
 
  Item_result result_type () const { return STRING_RESULT; }
584
 
  uint32_t decimals() const { return NOT_FIXED_DEC; }
585
 
  int  store(double nr);
586
 
  int  store(int64_t nr, bool unsigned_val)=0;
587
 
  int  store_decimal(const my_decimal *);
588
 
  int  store(const char *to,uint32_t length, const CHARSET_INFO * const cs)=0;
589
 
  uint32_t size_of() const { return sizeof(*this); }
590
 
  const CHARSET_INFO *charset(void) const { return field_charset; }
591
 
  void set_charset(const CHARSET_INFO * const charset_arg) { field_charset= charset_arg; }
592
 
  enum Derivation derivation(void) const { return field_derivation; }
593
 
  virtual void set_derivation(enum Derivation derivation_arg)
594
 
  { field_derivation= derivation_arg; }
595
 
  bool binary() const { return field_charset == &my_charset_bin; }
596
 
  uint32_t max_display_length() { return field_length; }
597
 
  friend class Create_field;
598
 
  my_decimal *val_decimal(my_decimal *);
599
 
  virtual bool str_needs_quotes() { return true; }
600
 
  bool compare_str_field_flags(Create_field *new_field, uint32_t flags);
601
 
  uint32_t is_equal(Create_field *new_field);
602
 
};
603
 
 
604
 
 
605
 
/* base class for Field_varstring and Field_blob */
606
 
 
607
 
class Field_longstr :public Field_str
608
 
{
609
 
protected:
610
 
  int report_if_important_data(const char *ptr, const char *end);
611
 
public:
612
 
  Field_longstr(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
613
 
                unsigned char null_bit_arg, utype unireg_check_arg,
614
 
                const char *field_name_arg, const CHARSET_INFO * const charset_arg)
615
 
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
616
 
               field_name_arg, charset_arg)
617
 
    {}
618
 
 
619
 
  int store_decimal(const my_decimal *d);
620
 
  uint32_t max_data_length() const;
621
 
};
622
 
 
623
 
/* base class for float and double and decimal (old one) */
624
 
class Field_real :public Field_num {
625
 
public:
626
 
  bool not_fixed;
627
 
 
628
 
  Field_real(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
629
 
             unsigned char null_bit_arg, utype unireg_check_arg,
630
 
             const char *field_name_arg,
631
 
             uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
632
 
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
633
 
               field_name_arg, dec_arg, zero_arg, unsigned_arg),
634
 
    not_fixed(dec_arg >= NOT_FIXED_DEC)
635
 
    {}
636
 
  int store_decimal(const my_decimal *);
637
 
  my_decimal *val_decimal(my_decimal *);
638
 
  int truncate(double *nr, double max_length);
639
 
  uint32_t max_display_length() { return field_length; }
640
 
  uint32_t size_of() const { return sizeof(*this); }
641
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
642
 
                              uint32_t param_data, bool low_byte_first);
643
 
  virtual unsigned char *pack(unsigned char* to, const unsigned char *from,
644
 
                      uint32_t max_length, bool low_byte_first);
645
 
};
646
 
 
647
 
 
648
 
class Field_tiny :public Field_num {
649
 
public:
650
 
  Field_tiny(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
651
 
             unsigned char null_bit_arg,
652
 
             enum utype unireg_check_arg, const char *field_name_arg,
653
 
             bool zero_arg, bool unsigned_arg)
654
 
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
655
 
               unireg_check_arg, field_name_arg,
656
 
               0, zero_arg,unsigned_arg)
657
 
    {}
658
 
  enum Item_result result_type () const { return INT_RESULT; }
659
 
  enum_field_types type() const { return DRIZZLE_TYPE_TINY;}
660
 
  enum ha_base_keytype key_type() const
661
 
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
662
 
  int store(const char *to,uint32_t length, const CHARSET_INFO * const charset);
663
 
  int store(double nr);
664
 
  int store(int64_t nr, bool unsigned_val);
665
 
  int reset(void) { ptr[0]=0; return 0; }
666
 
  double val_real(void);
667
 
  int64_t val_int(void);
668
 
  String *val_str(String*,String *);
669
 
  bool send_binary(Protocol *protocol);
670
 
  int cmp(const unsigned char *,const unsigned char *);
671
 
  void sort_string(unsigned char *buff,uint32_t length);
672
 
  uint32_t pack_length() const { return 1; }
673
 
  void sql_type(String &str) const;
674
 
  uint32_t max_display_length() { return 4; }
675
 
 
676
 
  virtual unsigned char *pack(unsigned char* to, const unsigned char *from,
677
 
                      uint32_t max_length __attribute__((unused)),
678
 
                      bool low_byte_first __attribute__((unused)))
679
 
  {
680
 
    *to= *from;
681
 
    return to + 1;
682
 
  }
683
 
 
684
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
685
 
                              uint32_t param_data __attribute__((unused)),
686
 
                              bool low_byte_first __attribute__((unused)))
687
 
  {
688
 
    *to= *from;
689
 
    return from + 1;
690
 
  }
691
 
};
692
 
 
693
563
/*
694
564
  Create field class for CREATE TABLE
695
565
*/
705
575
  enum  enum_field_types sql_type;
706
576
  /*
707
577
    At various stages in execution this can be length of field in bytes or
708
 
    max number of characters. 
 
578
    max number of characters.
709
579
  */
710
580
  uint32_t length;
711
581
  /*
724
594
 
725
595
  uint8_t row,col,sc_length,interval_id;        // For rea_create_table
726
596
  uint32_t      offset,pack_flag;
 
597
 
 
598
  /* Virtual column expression statement */
 
599
  virtual_column_info *vcol_info;
 
600
  /*
 
601
    Indication that the field is phycically stored in tables
 
602
    rather than just generated on SQL queries.
 
603
    As of now, FALSE can only be set for generated-only virtual columns.
 
604
  */
 
605
  bool is_stored;
 
606
 
727
607
  Create_field() :after(0) {}
728
608
  Create_field(Field *field, Field *orig_field);
729
609
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
742
622
                          uint32_t max_length, uint32_t decimals,
743
623
                          bool maybe_null, bool is_unsigned);
744
624
 
745
 
  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
 
625
  bool init(Session *session, char *field_name, enum_field_types type, char *length,
746
626
            char *decimals, uint32_t type_modifier, Item *default_value,
747
627
            Item *on_update_value, LEX_STRING *comment, char *change,
748
628
            List<String> *interval_list, const CHARSET_INFO * const cs,
749
629
            uint32_t uint_geom_type,
750
 
            enum column_format_type column_format);
 
630
            enum column_format_type column_format,
 
631
            virtual_column_info *vcol_info);
751
632
};
752
633
 
753
634
 
789
670
 
790
671
  Copy_field() {}
791
672
  ~Copy_field() {}
792
 
  void set(Field *to,Field *from,bool save);    // Field to field 
 
673
  void set(Field *to,Field *from,bool save);    // Field to field
793
674
  void set(unsigned char *to,Field *from);              // Field to string
794
675
  void (*do_copy)(Copy_field *);
795
676
  void (*do_copy2)(Copy_field *);               // Used to handle null values
797
678
 
798
679
 
799
680
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
800
 
                  unsigned char *null_pos, unsigned char null_bit,
801
 
                  uint32_t pack_flag, enum_field_types field_type,
802
 
                  const CHARSET_INFO * cs,
803
 
                  Field::utype unireg_check,
804
 
                  TYPELIB *interval, const char *field_name);
 
681
                  unsigned char *null_pos, unsigned char null_bit,
 
682
                  uint32_t pack_flag, enum_field_types field_type,
 
683
                  const CHARSET_INFO * cs,
 
684
                  Field::utype unireg_check,
 
685
                  TYPELIB *interval, const char *field_name);
 
686
 
805
687
uint32_t pack_length_to_packflag(uint32_t type);
806
688
enum_field_types get_blob_type_from_length(uint32_t length);
807
689
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
808
690
int set_field_to_null(Field *field);
809
691
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
810
692
 
 
693
 
811
694
bool
812
 
check_string_copy_error(Field_str *field,
813
 
                        const char *well_formed_error_pos,
814
 
                        const char *cannot_convert_error_pos,
815
 
                        const char *end,
816
 
                        const CHARSET_INFO * const cs);
817
 
 
818
 
/*
819
 
  Field subclasses
820
 
 */
821
 
#include <drizzled/field/blob.h>
822
 
#include <drizzled/field/enum.h>
823
 
#include <drizzled/field/null.h>
824
 
#include <drizzled/field/date.h>
825
 
#include <drizzled/field/fdecimal.h>
826
 
#include <drizzled/field/double.h>
827
 
#include <drizzled/field/long.h>
828
 
#include <drizzled/field/int64_t.h>
829
 
#include <drizzled/field/timetype.h>
830
 
#include <drizzled/field/timestamp.h>
831
 
#include <drizzled/field/datetime.h>
832
 
#include <drizzled/field/fstring.h>
833
 
#include <drizzled/field/varstring.h>
834
 
 
835
 
/*
836
 
  The following are for the interface with the .frm file
837
 
*/
838
 
 
839
 
#define FIELDFLAG_DECIMAL               1
840
 
#define FIELDFLAG_BINARY                1       // Shares same flag
841
 
#define FIELDFLAG_NUMBER                2
842
 
#define FIELDFLAG_DECIMAL_POSITION      4
843
 
#define FIELDFLAG_PACK                  120     // Bits used for packing
844
 
#define FIELDFLAG_INTERVAL              256     // mangled with decimals!
845
 
#define FIELDFLAG_BITFIELD              512     // mangled with decimals!
846
 
#define FIELDFLAG_BLOB                  1024    // mangled with decimals!
847
 
#define FIELDFLAG_GEOM                  2048    // mangled with decimals!
848
 
 
849
 
#define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */
850
 
 
851
 
#define FIELDFLAG_LEFT_FULLSCREEN       8192
852
 
#define FIELDFLAG_RIGHT_FULLSCREEN      16384
853
 
#define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
854
 
#define FIELDFLAG_NO_DEFAULT            16384   /* sql */
855
 
#define FIELDFLAG_SUM                   ((uint32_t) 32768)// predit: +#fieldflag
856
 
#define FIELDFLAG_MAYBE_NULL            ((uint32_t) 32768)// sql
857
 
#define FIELDFLAG_HEX_ESCAPE            ((uint32_t) 0x10000)
858
 
#define FIELDFLAG_PACK_SHIFT            3
859
 
#define FIELDFLAG_DEC_SHIFT             8
860
 
#define FIELDFLAG_MAX_DEC               31
861
 
#define FIELDFLAG_NUM_SCREEN_TYPE       0x7F01
862
 
#define FIELDFLAG_ALFA_SCREEN_TYPE      0x7800
863
 
 
864
 
#define MTYP_TYPENR(type) (type & 127)  /* Remove bits from type */
865
 
 
866
 
#define f_is_dec(x)             ((x) & FIELDFLAG_DECIMAL)
867
 
#define f_is_num(x)             ((x) & FIELDFLAG_NUMBER)
868
 
#define f_is_decimal_precision(x)       ((x) & FIELDFLAG_DECIMAL_POSITION)
869
 
#define f_is_packed(x)          ((x) & FIELDFLAG_PACK)
870
 
#define f_packtype(x)           (((x) >> FIELDFLAG_PACK_SHIFT) & 15)
871
 
#define f_decimals(x)           ((uint8_t) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
872
 
#define f_is_alpha(x)           (!f_is_num(x))
873
 
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
874
 
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
875
 
#define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
876
 
#define f_is_blob(x)            (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
877
 
#define f_is_equ(x)             ((x) & (1+2+FIELDFLAG_PACK+31*256))
878
 
#define f_settype(x)            (((int) x) << FIELDFLAG_PACK_SHIFT)
879
 
#define f_maybe_null(x)         (x & FIELDFLAG_MAYBE_NULL)
880
 
#define f_no_default(x)         (x & FIELDFLAG_NO_DEFAULT)
881
 
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
882
 
#define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)
883
 
 
 
695
test_if_important_data(const CHARSET_INFO * const cs,
 
696
                       const char *str,
 
697
                       const char *strend);
 
698
 
 
699
 
 
700
#endif /* DRIZZLED_FIELD_H */