~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Stewart Smith
  • Date: 2009-06-16 06:55:11 UTC
  • mto: This revision was merged to the branch mainline in revision 1094.
  • Revision ID: stewart@flamingspork.com-20090616065511-ps3ewfxj7918lwy3
rollback.test for MyISAM temp only.
- rename to myisam_rollback to reflect what it's testing
- just use create temporary table

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#define DRIZZLED_FIELD_H
27
27
 
28
28
#include "drizzled/sql_error.h"
29
 
#include "drizzled/decimal.h"
 
29
#include "drizzled/my_decimal.h"
30
30
#include "drizzled/key_map.h"
 
31
#include "drizzled/sql_bitmap.h"
31
32
#include "drizzled/sql_list.h"
32
33
#include "drizzled/structs.h"
33
 
#include "drizzled/charset_info.h"
34
 
#include "drizzled/item_result.h"
35
34
 
36
35
#include <string>
37
36
#include <vector>
38
37
 
39
 
namespace drizzled
40
 
{
41
 
 
42
38
#define DATETIME_DEC                     6
43
39
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
44
40
 
45
 
#ifdef DEBUG
46
 
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
47
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!getTable() || (getTable()->write_set == NULL || isWriteSet()))
48
 
#else
49
 
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
50
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
51
 
#endif
52
 
 
53
 
typedef struct st_typelib TYPELIB;
54
 
 
55
41
const uint32_t max_field_size= (uint32_t) 4294967295U;
56
42
 
57
43
class SendField;
58
44
class CreateField;
59
45
class TableShare;
60
46
class Field;
61
 
struct CacheField;
 
47
struct st_cache_field;
62
48
 
63
49
int field_conv(Field *to,Field *from);
64
50
 
 
51
inline uint32_t get_enum_pack_length(int elements)
 
52
{
 
53
  return elements < 256 ? 1 : 2;
 
54
}
 
55
 
65
56
/**
66
57
 * Class representing a Field in a Table
67
58
 *
68
59
 * @details
69
60
 *
70
 
 * The value stored in the Field object is stored in the
 
61
 * The value stored in the Field object is stored in the 
71
62
 * unsigned char pointer member variable called ptr.  The
72
 
 * val_xxx() methods retrieve this raw byte value and
 
63
 * val_xxx() methods retrieve this raw byte value and 
73
64
 * convert the byte into the appropriate output (int, decimal, etc).
74
65
 *
75
66
 * The store_xxx() methods take various input and convert
78
69
class Field
79
70
{
80
71
  /* Prevent use of these */
81
 
  Field(const Field&);
 
72
  Field(const Item &); 
82
73
  void operator=(Field &);
83
 
 
84
74
public:
85
75
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
86
76
  unsigned char *null_ptr; /**< Byte where null_bit is */
87
77
 
88
78
  /**
89
 
   * Pointer to the Table object containing this Field
 
79
   * Pointer to the Table object containing this Field 
90
80
   *
91
81
   * @note You can use table->in_use as replacement for current_session member
92
82
   * only inside of val_*() and store() members (e.g. you can't use it in cons)
93
83
   */
94
 
private:
95
 
  Table *table;
96
 
public:
97
 
  Table *getTable()
98
 
  {
99
 
    assert(table);
100
 
    return table;
101
 
  }
102
 
 
103
 
  Table *getTable() const
104
 
  {
105
 
    assert(table);
106
 
    return table;
107
 
  }
108
 
 
109
 
  void setTable(Table *table_arg)
110
 
  {
111
 
    table= table_arg;
112
 
  }
113
 
 
 
84
  Table *table; 
114
85
  Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
 
86
  const char **table_name; /**< Pointer to the name of the table. @TODO This is redundant with Table::table_name. */
115
87
  const char *field_name; /**< Name of the field */
116
88
  LEX_STRING comment; /**< A comment about the field */
117
89
 
122
94
  key_map part_of_sortkey;
123
95
 
124
96
  /*
125
 
    We use three additional unireg types for TIMESTAMP for hysterical
126
 
    raisins and limitations in the MySQL FRM file format.
127
 
 
128
 
    A good TODO is to clean this up as we can support just about
129
 
    anything in the table proto message now.
 
97
    We use three additional unireg types for TIMESTAMP to overcome limitation
 
98
    of current binary format of .frm file. We'd like to be able to support
 
99
    NOW() as default and on update value for such fields but unable to hold
 
100
    this info anywhere except unireg_check field. This issue will be resolved
 
101
    in more clean way with transition to new text based .frm format.
 
102
    See also comment for Field_timestamp::Field_timestamp().
130
103
  */
131
104
  enum utype
132
 
  {
 
105
  { 
133
106
    NONE,
134
107
    NEXT_NUMBER,
135
108
    TIMESTAMP_OLD_FIELD,
141
114
  utype unireg_check;
142
115
  uint32_t field_length; /**< Length of this field in bytes */
143
116
  uint32_t flags;
144
 
private:
145
117
  uint16_t field_index; /**< Index of this Field in Table::fields array */
146
 
 
147
 
public:
148
 
 
149
 
  uint16_t position() const
150
 
  {
151
 
    return field_index;
152
 
  }
153
 
 
154
 
  void setPosition(uint32_t arg)
155
 
  {
156
 
    field_index= arg;
157
 
  }
158
 
 
159
118
  unsigned char null_bit; /**< Bit used to test null bit */
160
119
  /**
161
120
     If true, this field was created in create_tmp_field_from_item from a NULL
167
126
   */
168
127
  bool is_created_from_null_item;
169
128
 
170
 
  static void *operator new(size_t size);
171
 
  static void *operator new(size_t size, memory::Root *mem_root);
 
129
  static void *operator new(size_t size) {return sql_alloc(size); }
 
130
  static void *operator new(size_t size, MEM_ROOT *mem_root)
 
131
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
172
132
  static void operator delete(void *, size_t)
173
 
  { }
174
 
  static void operator delete(void *, memory::Root *)
175
 
  { }
 
133
  { TRASH(ptr_arg, size); }
176
134
 
177
135
  Field(unsigned char *ptr_arg,
178
136
        uint32_t length_arg,
181
139
        utype unireg_check_arg,
182
140
        const char *field_name_arg);
183
141
  virtual ~Field() {}
184
 
 
185
 
  bool hasDefault() const
186
 
  {
187
 
    return not (flags & NO_DEFAULT_VALUE_FLAG);
188
 
  }
189
 
 
190
142
  /* Store functions returns 1 on overflow and -1 on fatal error */
191
 
  virtual int store(const char *to,
192
 
                    uint32_t length,
 
143
  virtual int store(const char *to, 
 
144
                    uint32_t length, 
193
145
                    const CHARSET_INFO * const cs)=0;
194
146
  virtual int store(double nr)=0;
195
147
  virtual int store(int64_t nr, bool unsigned_val)=0;
196
148
  virtual int store_decimal(const my_decimal *d)=0;
197
 
  int store_and_check(enum_check_fields check_level,
198
 
                      const char *to,
199
 
                      uint32_t length,
200
 
                      const CHARSET_INFO * const cs);
 
149
  int store(const char *to, 
 
150
            uint32_t length,
 
151
            const CHARSET_INFO * const cs,
 
152
            enum_check_fields check_level);
201
153
  /**
202
154
    This is called when storing a date in a string.
203
155
 
205
157
      Needs to be changed if/when we want to support different time formats.
206
158
  */
207
159
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
208
 
  virtual double val_real()=0;
209
 
  virtual int64_t val_int()=0;
 
160
  virtual double val_real(void)=0;
 
161
  virtual int64_t val_int(void)=0;
210
162
  virtual my_decimal *val_decimal(my_decimal *);
211
 
  String *val_str_internal(String *str)
 
163
  inline String *val_str(String *str) 
212
164
  {
213
165
    return val_str(str, str);
214
166
  }
225
177
     This trickery is used to decrease a number of malloc calls.
226
178
  */
227
179
  virtual String *val_str(String*, String *)=0;
 
180
  /**
 
181
   * Interpret field value as an integer but return the result as a string.
 
182
   *
 
183
   * This is used for printing bit_fields as numbers while debugging.
 
184
   */
 
185
  String *val_int_as_str(String *val_buffer, bool unsigned_flag);
228
186
  /*
229
187
   str_needs_quotes() returns true if the value returned by val_str() needs
230
188
   to be quoted when used in constructing an SQL query.
237
195
     Check whether a field type can be partially indexed by a key.
238
196
 
239
197
     This is a static method, rather than a virtual function, because we need
240
 
     to check the type of a non-Field in alter_table().
 
198
     to check the type of a non-Field in mysql_alter_table().
241
199
 
242
200
     @param type  field type
243
201
 
292
250
   * table, which is located on disk).
293
251
   */
294
252
  virtual uint32_t pack_length_in_rec() const;
295
 
 
296
 
  /**
297
 
   * Return the "real size" of the data in memory.
 
253
  /**
 
254
    Check to see if field size is compatible with destination.
 
255
 
 
256
    This method is used in row-based replication to verify that the slave's
 
257
    field size is less than or equal to the master's field size. The
 
258
    encoded field metadata (from the master or source) is decoded and compared
 
259
    to the size of this field (the slave or destination).
 
260
 
 
261
    @param   field_metadata   Encoded size in field metadata
 
262
 
 
263
    @retval 0 if this field's size is < the source field's size
 
264
    @retval 1 if this field's size is >= the source field's size
 
265
  */
 
266
  virtual int compatible_field_size(uint32_t field_metadata);
 
267
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata);
 
268
 
 
269
  /*
 
270
    This method is used to return the size of the data in a row-based
 
271
    replication row record. The default implementation of returning 0 is
 
272
    designed to allow fields that do not use metadata to return true (1)
 
273
    from compatible_field_size() which uses this function in the comparison.
 
274
    The default value for field metadata for fields that do not have
 
275
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
 
276
 
 
277
    Note: While most classes that override this method return pack_length(),
 
278
    the classes Field_varstring, and Field_blob return
 
279
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
 
280
  */
 
281
  virtual uint32_t row_pack_length();
 
282
  virtual int save_field_metadata(unsigned char *first_byte);
 
283
 
 
284
  /**
 
285
   * Return the "real size" of the data in memory. 
298
286
   * For varstrings, this does _not_ include the length bytes.
299
287
   */
300
288
  virtual uint32_t data_length();
322
310
  virtual uint32_t key_length() const;
323
311
  virtual enum_field_types type() const =0;
324
312
  virtual enum_field_types real_type() const;
325
 
  virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
 
313
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
 
314
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
 
315
                      uint32_t max_len);
326
316
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
327
 
  int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
328
317
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
329
318
                         uint32_t max_length=UINT32_MAX);
330
319
  virtual int cmp_offset(uint32_t row_offset);
343
332
  // For new field
344
333
  virtual uint32_t size_of() const =0;
345
334
 
346
 
  bool is_null(ptrdiff_t row_offset= 0);
347
 
  bool is_real_null(ptrdiff_t row_offset= 0);
 
335
  bool is_null(my_ptrdiff_t row_offset= 0);
 
336
  bool is_real_null(my_ptrdiff_t row_offset= 0);
348
337
  bool is_null_in_record(const unsigned char *record);
349
338
  bool is_null_in_record_with_offset(ptrdiff_t offset);
350
339
  void set_null(ptrdiff_t row_offset= 0);
362
351
   * use field->val_int() for comparison.  Used to optimize clauses like
363
352
   * 'a_column BETWEEN date_const AND date_const'.
364
353
   */
365
 
  virtual bool can_be_compared_as_int64_t() const
 
354
  virtual bool can_be_compared_as_int64_t() const 
366
355
  {
367
356
    return false;
368
357
  }
369
358
  virtual void free() {}
370
 
  virtual Field *new_field(memory::Root *root,
 
359
  virtual Field *new_field(MEM_ROOT *root, 
371
360
                           Table *new_table,
372
361
                           bool keep_type);
373
 
  virtual Field *new_key_field(memory::Root *root, Table *new_table,
374
 
                               unsigned char *new_ptr,
 
362
  virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
 
363
                               unsigned char *new_ptr, 
375
364
                               unsigned char *new_null_ptr,
376
365
                               uint32_t new_null_bit);
377
366
  /** This is used to generate a field in Table from TableShare */
378
 
  Field *clone(memory::Root *mem_root, Table *new_table);
379
 
  void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
 
367
  Field *clone(MEM_ROOT *mem_root, Table *new_table);
 
368
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
380
369
  {
381
370
    ptr= ptr_arg;
382
371
    null_ptr= null_ptr_arg;
383
372
    null_bit= null_bit_arg;
384
373
  }
385
 
  void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
 
374
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
386
375
  virtual void move_field_offset(ptrdiff_t ptr_diff)
387
376
  {
388
377
    ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
422
411
   * characters have maximal possible size (mbmaxlen). In the other words,
423
412
   * "length" parameter is a number of characters multiplied by
424
413
   * field_charset->mbmaxlen.
425
 
   *
 
414
   * 
426
415
   * @retval
427
416
   *   Number of copied bytes (excluding padded zero bytes -- see above).
428
417
   */
440
429
  {
441
430
    set_image(buff,length, &my_charset_bin);
442
431
  }
443
 
  int64_t val_int_offset(uint32_t row_offset)
 
432
  inline int64_t val_int_offset(uint32_t row_offset)
444
433
  {
445
434
    ptr+=row_offset;
446
435
    int64_t tmp=val_int();
448
437
    return tmp;
449
438
  }
450
439
 
451
 
  int64_t val_int_internal(const unsigned char *new_ptr)
 
440
  inline int64_t val_int(const unsigned char *new_ptr)
452
441
  {
453
442
    unsigned char *old_ptr= ptr;
454
443
    int64_t return_value;
455
 
    ptr= const_cast<unsigned char*>(new_ptr);
 
444
    ptr= (unsigned char*) new_ptr;
456
445
    return_value= val_int();
457
446
    ptr= old_ptr;
458
447
    return return_value;
459
448
  }
460
 
 
461
 
  String *val_str_internal(String *str, const unsigned char *new_ptr)
 
449
  inline String *val_str(String *str, const unsigned char *new_ptr)
462
450
  {
463
451
    unsigned char *old_ptr= ptr;
464
 
    ptr= const_cast<unsigned char*>(new_ptr);
465
 
    val_str_internal(str);
 
452
    ptr= (unsigned char*) new_ptr;
 
453
    val_str(str);
466
454
    ptr= old_ptr;
467
455
    return str;
468
456
  }
554
542
 
555
543
  virtual unsigned char *pack_key(unsigned char* to,
556
544
                                  const unsigned char *from,
557
 
                                  uint32_t max_length,
 
545
                                  uint32_t max_length, 
558
546
                                  bool low_byte_first)
559
547
  {
560
548
    return pack(to, from, max_length, low_byte_first);
561
549
  }
 
550
  virtual unsigned char *pack_key_from_key_image(unsigned char* to,
 
551
                                                 const unsigned char *from,
 
552
                                                 uint32_t max_length,
 
553
                                                 bool low_byte_first)
 
554
  {
 
555
    return pack(to, from, max_length, low_byte_first);
 
556
  }
562
557
  virtual const unsigned char *unpack_key(unsigned char* to,
563
558
                                          const unsigned char *from,
564
559
                                          uint32_t max_length,
566
561
  {
567
562
    return unpack(to, from, max_length, low_byte_first);
568
563
  }
 
564
  virtual uint32_t packed_col_length(const unsigned char *to, uint32_t length);
569
565
  virtual uint32_t max_packed_col_length(uint32_t max_length)
570
566
  {
571
567
    return max_length;
572
568
  }
573
569
 
574
 
  uint32_t offset(const unsigned char *record)
 
570
  virtual int pack_cmp(const unsigned char *a,
 
571
                       const unsigned char *b,
 
572
                       uint32_t key_length_arg,
 
573
                       bool insert_or_update);
 
574
  virtual int pack_cmp(const unsigned char *b,
 
575
                       uint32_t key_length_arg,
 
576
                       bool insert_or_update);
 
577
 
 
578
  inline uint32_t offset(unsigned char *record)
575
579
  {
576
580
    return (uint32_t) (ptr - record);
577
581
  }
578
582
  void copy_from_tmp(int offset);
579
 
  uint32_t fill_cache_field(CacheField *copy);
 
583
  uint32_t fill_cache_field(struct st_cache_field *copy);
580
584
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
581
585
  virtual bool get_time(DRIZZLE_TIME *ltime);
582
586
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
609
613
    @retval
610
614
      0 otherwise
611
615
  */
612
 
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
 
616
  bool set_warning(DRIZZLE_ERROR::enum_warning_level, 
613
617
                   unsigned int code,
614
618
                   int cuted_increment);
615
619
  /**
627
631
      fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
628
632
      thread.
629
633
  */
630
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
634
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
631
635
                            uint32_t code,
632
 
                            const char *str,
 
636
                            const char *str, 
633
637
                            uint32_t str_len,
634
 
                            enum enum_drizzle_timestamp_type ts_type,
 
638
                            enum enum_drizzle_timestamp_type ts_type, 
635
639
                            int cuted_increment);
636
640
  /**
637
641
    Produce warning or note about integer datetime value saved into field.
647
651
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
648
652
      thread.
649
653
  */
650
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
654
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
651
655
                            uint32_t code,
652
 
                            int64_t nr,
 
656
                            int64_t nr, 
653
657
                            enum enum_drizzle_timestamp_type ts_type,
654
658
                            int cuted_increment);
655
659
  /**
665
669
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
666
670
      thread.
667
671
  */
668
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
672
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
669
673
                            const uint32_t code,
670
 
                            double nr,
 
674
                            double nr, 
671
675
                            enum enum_drizzle_timestamp_type ts_type);
672
 
  bool check_overflow(int op_result)
 
676
  inline bool check_overflow(int op_result)
673
677
  {
674
678
    return (op_result == E_DEC_OVERFLOW);
675
679
  }
703
707
    @return
704
708
      value converted from val
705
709
  */
706
 
  int64_t convert_decimal2int64_t(const my_decimal *val,
 
710
  int64_t convert_decimal2int64_t(const my_decimal *val, 
707
711
                                  bool unsigned_flag,
708
712
                                  int *err);
709
713
  /* The max. number of characters */
710
 
  uint32_t char_length() const
 
714
  inline uint32_t char_length() const
711
715
  {
712
716
    return field_length / charset()->mbmaxlen;
713
717
  }
714
718
 
715
 
  enum column_format_type column_format() const
 
719
  inline enum column_format_type column_format() const
716
720
  {
717
721
    return (enum column_format_type)
718
722
      ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
737
741
 
738
742
  bool isReadSet();
739
743
  bool isWriteSet();
740
 
  void setReadSet(bool arg= true);
741
 
  void setWriteSet(bool arg= true);
 
744
 
 
745
private:
 
746
 
 
747
  /**
 
748
    Retrieve the field metadata for fields.
 
749
 
 
750
    This default implementation returns 0 and saves 0 in the metadata_ptr
 
751
    value.
 
752
 
 
753
    @param   metadata_ptr   First byte of field metadata
 
754
 
 
755
    @returns 0 no bytes written.
 
756
  */
 
757
  virtual int do_save_field_metadata(unsigned char *)
 
758
  {
 
759
    return 0;
 
760
  }
742
761
};
743
762
 
744
 
std::ostream& operator<<(std::ostream& output, const Field &field);
745
 
 
746
 
} /* namespace drizzled */
747
 
 
748
 
/** @TODO Why is this in the middle of the file???*/
749
763
#include "drizzled/create_field.h"
750
764
 
751
 
namespace drizzled
752
 
{
753
 
 
754
765
/**
755
766
 * A class for sending field information to a client.
756
767
 *
759
770
 * Send_field is basically a stripped-down POD class for
760
771
 * representing basic information about a field...
761
772
 */
762
 
class SendField
 
773
class SendField 
763
774
{
764
775
public:
765
776
  const char *db_name;
778
789
/**
779
790
 * A class for quick copying data to fields
780
791
 */
781
 
class CopyField :public memory::SqlAlloc
 
792
class CopyField :public Sql_alloc
782
793
{
783
794
  /**
784
795
    Convenience definition of a copy function returned by
808
819
  void (*do_copy2)(CopyField *);                // Used to handle null values
809
820
};
810
821
 
 
822
Field *make_field(TableShare *share,
 
823
                  MEM_ROOT *root,
 
824
                  unsigned char *ptr,
 
825
                  uint32_t field_length,
 
826
                  unsigned char *null_pos,
 
827
                  unsigned char null_bit,
 
828
                  uint32_t pack_flag,
 
829
                  enum_field_types field_type,
 
830
                  const CHARSET_INFO * cs,
 
831
                  Field::utype unireg_check,
 
832
                  TYPELIB *interval,
 
833
                  const char *field_name);
 
834
 
811
835
uint32_t pack_length_to_packflag(uint32_t type);
812
836
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
813
837
int set_field_to_null(Field *field);
830
854
                            const char *str,
831
855
                            const char *strend);
832
856
 
833
 
} /* namespace drizzled */
834
 
 
835
857
#endif /* DRIZZLED_FIELD_H */