~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Monty Taylor
  • Date: 2009-09-22 23:50:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090922235012-i0a3bs91f6krqduc
Fixed multi_malloc.h include guard.
Added include guard checking script.

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
41
#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()))
 
42
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!table || (table->read_set == NULL || isReadSet()))
 
43
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!table || (table->write_set == NULL || isWriteSet()))
48
44
#else
49
 
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
50
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
 
45
#define ASSERT_COLUMN_MARKED_FOR_READ
 
46
#define ASSERT_COLUMN_MARKED_FOR_WRITE
51
47
#endif
52
48
 
53
 
typedef struct st_typelib TYPELIB;
54
 
 
55
49
const uint32_t max_field_size= (uint32_t) 4294967295U;
56
50
 
57
51
class SendField;
58
52
class CreateField;
59
53
class TableShare;
60
54
class Field;
61
 
struct CacheField;
 
55
struct st_cache_field;
62
56
 
63
57
int field_conv(Field *to,Field *from);
64
58
 
 
59
inline uint32_t get_enum_pack_length(int elements)
 
60
{
 
61
  return elements < 256 ? 1 : 2;
 
62
}
 
63
 
65
64
/**
66
65
 * Class representing a Field in a Table
67
66
 *
68
67
 * @details
69
68
 *
70
 
 * The value stored in the Field object is stored in the
 
69
 * The value stored in the Field object is stored in the 
71
70
 * unsigned char pointer member variable called ptr.  The
72
 
 * val_xxx() methods retrieve this raw byte value and
 
71
 * val_xxx() methods retrieve this raw byte value and 
73
72
 * convert the byte into the appropriate output (int, decimal, etc).
74
73
 *
75
74
 * The store_xxx() methods take various input and convert
78
77
class Field
79
78
{
80
79
  /* Prevent use of these */
81
 
  Field(const Field&);
 
80
  Field(const Field&); 
82
81
  void operator=(Field &);
83
 
 
84
82
public:
85
83
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
86
84
  unsigned char *null_ptr; /**< Byte where null_bit is */
87
85
 
88
86
  /**
89
 
   * Pointer to the Table object containing this Field
 
87
   * Pointer to the Table object containing this Field 
90
88
   *
91
89
   * @note You can use table->in_use as replacement for current_session member
92
90
   * only inside of val_*() and store() members (e.g. you can't use it in cons)
93
91
   */
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
 
 
 
92
  Table *table; 
114
93
  Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
 
94
  const char **table_name; /**< Pointer to the name of the table. @TODO This is redundant with Table::table_name. */
115
95
  const char *field_name; /**< Name of the field */
116
96
  LEX_STRING comment; /**< A comment about the field */
117
97
 
122
102
  key_map part_of_sortkey;
123
103
 
124
104
  /*
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.
 
105
    We use three additional unireg types for TIMESTAMP to overcome limitation
 
106
    of current binary format of .frm file. We'd like to be able to support
 
107
    NOW() as default and on update value for such fields but unable to hold
 
108
    this info anywhere except unireg_check field. This issue will be resolved
 
109
    in more clean way with transition to new text based .frm format.
 
110
    See also comment for Field_timestamp::Field_timestamp().
130
111
  */
131
112
  enum utype
132
 
  {
 
113
  { 
133
114
    NONE,
134
115
    NEXT_NUMBER,
135
116
    TIMESTAMP_OLD_FIELD,
141
122
  utype unireg_check;
142
123
  uint32_t field_length; /**< Length of this field in bytes */
143
124
  uint32_t flags;
144
 
private:
145
125
  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
126
  unsigned char null_bit; /**< Bit used to test null bit */
160
127
  /**
161
128
     If true, this field was created in create_tmp_field_from_item from a NULL
167
134
   */
168
135
  bool is_created_from_null_item;
169
136
 
170
 
  static void *operator new(size_t size);
171
 
  static void *operator new(size_t size, memory::Root *mem_root);
 
137
  static void *operator new(size_t size) {return sql_alloc(size); }
 
138
  static void *operator new(size_t size, MEM_ROOT *mem_root)
 
139
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
172
140
  static void operator delete(void *, size_t)
173
 
  { }
174
 
  static void operator delete(void *, memory::Root *)
175
 
  { }
 
141
  { TRASH(ptr_arg, size); }
176
142
 
177
143
  Field(unsigned char *ptr_arg,
178
144
        uint32_t length_arg,
181
147
        utype unireg_check_arg,
182
148
        const char *field_name_arg);
183
149
  virtual ~Field() {}
184
 
 
185
 
  bool hasDefault() const
186
 
  {
187
 
    return not (flags & NO_DEFAULT_VALUE_FLAG);
188
 
  }
189
 
 
190
150
  /* Store functions returns 1 on overflow and -1 on fatal error */
191
 
  virtual int store(const char *to,
192
 
                    uint32_t length,
 
151
  virtual int store(const char *to, 
 
152
                    uint32_t length, 
193
153
                    const CHARSET_INFO * const cs)=0;
194
154
  virtual int store(double nr)=0;
195
155
  virtual int store(int64_t nr, bool unsigned_val)=0;
196
156
  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);
 
157
  int store(const char *to, 
 
158
            uint32_t length,
 
159
            const CHARSET_INFO * const cs,
 
160
            enum_check_fields check_level);
201
161
  /**
202
162
    This is called when storing a date in a string.
203
163
 
205
165
      Needs to be changed if/when we want to support different time formats.
206
166
  */
207
167
  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;
 
168
  virtual double val_real(void)=0;
 
169
  virtual int64_t val_int(void)=0;
210
170
  virtual my_decimal *val_decimal(my_decimal *);
211
 
  String *val_str_internal(String *str)
 
171
  inline String *val_str(String *str) 
212
172
  {
213
173
    return val_str(str, str);
214
174
  }
225
185
     This trickery is used to decrease a number of malloc calls.
226
186
  */
227
187
  virtual String *val_str(String*, String *)=0;
 
188
  /**
 
189
   * Interpret field value as an integer but return the result as a string.
 
190
   *
 
191
   * This is used for printing bit_fields as numbers while debugging.
 
192
   */
 
193
  String *val_int_as_str(String *val_buffer, bool unsigned_flag);
228
194
  /*
229
195
   str_needs_quotes() returns true if the value returned by val_str() needs
230
196
   to be quoted when used in constructing an SQL query.
237
203
     Check whether a field type can be partially indexed by a key.
238
204
 
239
205
     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().
 
206
     to check the type of a non-Field in mysql_alter_table().
241
207
 
242
208
     @param type  field type
243
209
 
292
258
   * table, which is located on disk).
293
259
   */
294
260
  virtual uint32_t pack_length_in_rec() const;
295
 
 
296
 
  /**
297
 
   * Return the "real size" of the data in memory.
 
261
  /**
 
262
    Check to see if field size is compatible with destination.
 
263
 
 
264
    This method is used in row-based replication to verify that the slave's
 
265
    field size is less than or equal to the master's field size. The
 
266
    encoded field metadata (from the master or source) is decoded and compared
 
267
    to the size of this field (the slave or destination).
 
268
 
 
269
    @param   field_metadata   Encoded size in field metadata
 
270
 
 
271
    @retval 0 if this field's size is < the source field's size
 
272
    @retval 1 if this field's size is >= the source field's size
 
273
  */
 
274
  virtual int compatible_field_size(uint32_t field_metadata);
 
275
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata);
 
276
 
 
277
  /*
 
278
    This method is used to return the size of the data in a row-based
 
279
    replication row record. The default implementation of returning 0 is
 
280
    designed to allow fields that do not use metadata to return true (1)
 
281
    from compatible_field_size() which uses this function in the comparison.
 
282
    The default value for field metadata for fields that do not have
 
283
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
 
284
 
 
285
    Note: While most classes that override this method return pack_length(),
 
286
    the classes Field_varstring, and Field_blob return
 
287
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
 
288
  */
 
289
  virtual uint32_t row_pack_length();
 
290
  virtual int save_field_metadata(unsigned char *first_byte);
 
291
 
 
292
  /**
 
293
   * Return the "real size" of the data in memory. 
298
294
   * For varstrings, this does _not_ include the length bytes.
299
295
   */
300
296
  virtual uint32_t data_length();
322
318
  virtual uint32_t key_length() const;
323
319
  virtual enum_field_types type() const =0;
324
320
  virtual enum_field_types real_type() const;
325
 
  virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
 
321
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
 
322
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
 
323
                      uint32_t max_len);
326
324
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
327
 
  int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
328
325
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
329
326
                         uint32_t max_length=UINT32_MAX);
330
327
  virtual int cmp_offset(uint32_t row_offset);
362
359
   * use field->val_int() for comparison.  Used to optimize clauses like
363
360
   * 'a_column BETWEEN date_const AND date_const'.
364
361
   */
365
 
  virtual bool can_be_compared_as_int64_t() const
 
362
  virtual bool can_be_compared_as_int64_t() const 
366
363
  {
367
364
    return false;
368
365
  }
369
366
  virtual void free() {}
370
 
  virtual Field *new_field(memory::Root *root,
 
367
  virtual Field *new_field(MEM_ROOT *root, 
371
368
                           Table *new_table,
372
369
                           bool keep_type);
373
 
  virtual Field *new_key_field(memory::Root *root, Table *new_table,
374
 
                               unsigned char *new_ptr,
 
370
  virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
 
371
                               unsigned char *new_ptr, 
375
372
                               unsigned char *new_null_ptr,
376
373
                               uint32_t new_null_bit);
377
374
  /** 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)
 
375
  Field *clone(MEM_ROOT *mem_root, Table *new_table);
 
376
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
380
377
  {
381
378
    ptr= ptr_arg;
382
379
    null_ptr= null_ptr_arg;
383
380
    null_bit= null_bit_arg;
384
381
  }
385
 
  void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
 
382
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
386
383
  virtual void move_field_offset(ptrdiff_t ptr_diff)
387
384
  {
388
385
    ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
422
419
   * characters have maximal possible size (mbmaxlen). In the other words,
423
420
   * "length" parameter is a number of characters multiplied by
424
421
   * field_charset->mbmaxlen.
425
 
   *
 
422
   * 
426
423
   * @retval
427
424
   *   Number of copied bytes (excluding padded zero bytes -- see above).
428
425
   */
440
437
  {
441
438
    set_image(buff,length, &my_charset_bin);
442
439
  }
443
 
  int64_t val_int_offset(uint32_t row_offset)
 
440
  inline int64_t val_int_offset(uint32_t row_offset)
444
441
  {
445
442
    ptr+=row_offset;
446
443
    int64_t tmp=val_int();
448
445
    return tmp;
449
446
  }
450
447
 
451
 
  int64_t val_int_internal(const unsigned char *new_ptr)
 
448
  inline int64_t val_int(const unsigned char *new_ptr)
452
449
  {
453
450
    unsigned char *old_ptr= ptr;
454
451
    int64_t return_value;
455
 
    ptr= const_cast<unsigned char*>(new_ptr);
 
452
    ptr= (unsigned char*) new_ptr;
456
453
    return_value= val_int();
457
454
    ptr= old_ptr;
458
455
    return return_value;
459
456
  }
460
 
 
461
 
  String *val_str_internal(String *str, const unsigned char *new_ptr)
 
457
  inline String *val_str(String *str, const unsigned char *new_ptr)
462
458
  {
463
459
    unsigned char *old_ptr= ptr;
464
 
    ptr= const_cast<unsigned char*>(new_ptr);
465
 
    val_str_internal(str);
 
460
    ptr= (unsigned char*) new_ptr;
 
461
    val_str(str);
466
462
    ptr= old_ptr;
467
463
    return str;
468
464
  }
554
550
 
555
551
  virtual unsigned char *pack_key(unsigned char* to,
556
552
                                  const unsigned char *from,
557
 
                                  uint32_t max_length,
 
553
                                  uint32_t max_length, 
558
554
                                  bool low_byte_first)
559
555
  {
560
556
    return pack(to, from, max_length, low_byte_first);
561
557
  }
 
558
  virtual unsigned char *pack_key_from_key_image(unsigned char* to,
 
559
                                                 const unsigned char *from,
 
560
                                                 uint32_t max_length,
 
561
                                                 bool low_byte_first)
 
562
  {
 
563
    return pack(to, from, max_length, low_byte_first);
 
564
  }
562
565
  virtual const unsigned char *unpack_key(unsigned char* to,
563
566
                                          const unsigned char *from,
564
567
                                          uint32_t max_length,
566
569
  {
567
570
    return unpack(to, from, max_length, low_byte_first);
568
571
  }
 
572
  virtual uint32_t packed_col_length(const unsigned char *to, uint32_t length);
569
573
  virtual uint32_t max_packed_col_length(uint32_t max_length)
570
574
  {
571
575
    return max_length;
572
576
  }
573
577
 
574
 
  uint32_t offset(const unsigned char *record)
 
578
  virtual int pack_cmp(const unsigned char *a,
 
579
                       const unsigned char *b,
 
580
                       uint32_t key_length_arg,
 
581
                       bool insert_or_update);
 
582
  virtual int pack_cmp(const unsigned char *b,
 
583
                       uint32_t key_length_arg,
 
584
                       bool insert_or_update);
 
585
 
 
586
  inline uint32_t offset(unsigned char *record)
575
587
  {
576
588
    return (uint32_t) (ptr - record);
577
589
  }
578
590
  void copy_from_tmp(int offset);
579
 
  uint32_t fill_cache_field(CacheField *copy);
 
591
  uint32_t fill_cache_field(struct st_cache_field *copy);
580
592
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
581
593
  virtual bool get_time(DRIZZLE_TIME *ltime);
582
594
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
609
621
    @retval
610
622
      0 otherwise
611
623
  */
612
 
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
 
624
  bool set_warning(DRIZZLE_ERROR::enum_warning_level, 
613
625
                   unsigned int code,
614
626
                   int cuted_increment);
615
627
  /**
627
639
      fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
628
640
      thread.
629
641
  */
630
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
642
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
631
643
                            uint32_t code,
632
 
                            const char *str,
 
644
                            const char *str, 
633
645
                            uint32_t str_len,
634
 
                            enum enum_drizzle_timestamp_type ts_type,
 
646
                            enum enum_drizzle_timestamp_type ts_type, 
635
647
                            int cuted_increment);
636
648
  /**
637
649
    Produce warning or note about integer datetime value saved into field.
647
659
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
648
660
      thread.
649
661
  */
650
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
662
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
651
663
                            uint32_t code,
652
 
                            int64_t nr,
 
664
                            int64_t nr, 
653
665
                            enum enum_drizzle_timestamp_type ts_type,
654
666
                            int cuted_increment);
655
667
  /**
665
677
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
666
678
      thread.
667
679
  */
668
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
680
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
669
681
                            const uint32_t code,
670
 
                            double nr,
 
682
                            double nr, 
671
683
                            enum enum_drizzle_timestamp_type ts_type);
672
 
  bool check_overflow(int op_result)
 
684
  inline bool check_overflow(int op_result)
673
685
  {
674
686
    return (op_result == E_DEC_OVERFLOW);
675
687
  }
703
715
    @return
704
716
      value converted from val
705
717
  */
706
 
  int64_t convert_decimal2int64_t(const my_decimal *val,
 
718
  int64_t convert_decimal2int64_t(const my_decimal *val, 
707
719
                                  bool unsigned_flag,
708
720
                                  int *err);
709
721
  /* The max. number of characters */
710
 
  uint32_t char_length() const
 
722
  inline uint32_t char_length() const
711
723
  {
712
724
    return field_length / charset()->mbmaxlen;
713
725
  }
714
726
 
715
 
  enum column_format_type column_format() const
 
727
  inline enum column_format_type column_format() const
716
728
  {
717
729
    return (enum column_format_type)
718
730
      ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
739
751
  bool isWriteSet();
740
752
  void setReadSet(bool arg= true);
741
753
  void setWriteSet(bool arg= true);
 
754
 
 
755
private:
 
756
 
 
757
  /**
 
758
    Retrieve the field metadata for fields.
 
759
 
 
760
    This default implementation returns 0 and saves 0 in the metadata_ptr
 
761
    value.
 
762
 
 
763
    @param   metadata_ptr   First byte of field metadata
 
764
 
 
765
    @returns 0 no bytes written.
 
766
  */
 
767
  virtual int do_save_field_metadata(unsigned char *)
 
768
  {
 
769
    return 0;
 
770
  }
742
771
};
743
772
 
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
773
#include "drizzled/create_field.h"
750
774
 
751
 
namespace drizzled
752
 
{
753
 
 
754
775
/**
755
776
 * A class for sending field information to a client.
756
777
 *
759
780
 * Send_field is basically a stripped-down POD class for
760
781
 * representing basic information about a field...
761
782
 */
762
 
class SendField
 
783
class SendField 
763
784
{
764
785
public:
765
786
  const char *db_name;
778
799
/**
779
800
 * A class for quick copying data to fields
780
801
 */
781
 
class CopyField :public memory::SqlAlloc
 
802
class CopyField :public Sql_alloc
782
803
{
783
804
  /**
784
805
    Convenience definition of a copy function returned by
808
829
  void (*do_copy2)(CopyField *);                // Used to handle null values
809
830
};
810
831
 
 
832
Field *make_field(TableShare *share,
 
833
                  MEM_ROOT *root,
 
834
                  unsigned char *ptr,
 
835
                  uint32_t field_length,
 
836
                  unsigned char *null_pos,
 
837
                  unsigned char null_bit,
 
838
                  uint32_t pack_flag,
 
839
                  enum_field_types field_type,
 
840
                  const CHARSET_INFO * cs,
 
841
                  Field::utype unireg_check,
 
842
                  TYPELIB *interval,
 
843
                  const char *field_name);
 
844
 
811
845
uint32_t pack_length_to_packflag(uint32_t type);
812
846
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
813
847
int set_field_to_null(Field *field);
830
864
                            const char *str,
831
865
                            const char *strend);
832
866
 
833
 
} /* namespace drizzled */
834
 
 
835
867
#endif /* DRIZZLED_FIELD_H */