~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-13 01:03:01 UTC
  • mto: (1126.9.2 captain-20090915-01)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: osullivan.padraig@gmail.com-20090913010301-tcvvezipx1124acy
Added calls to the dtrace delete begin/end probes.

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
31
#include "drizzled/sql_bitmap.h"
32
32
#include "drizzled/sql_list.h"
33
33
#include "drizzled/structs.h"
34
 
#include "drizzled/charset_info.h"
35
 
#include "drizzled/item_result.h"
36
34
 
37
35
#include <string>
38
36
#include <vector>
39
37
 
40
 
namespace drizzled
41
 
{
42
 
 
43
38
#define DATETIME_DEC                     6
44
39
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
45
40
 
51
46
#define ASSERT_COLUMN_MARKED_FOR_WRITE
52
47
#endif
53
48
 
54
 
typedef struct st_typelib TYPELIB;
55
 
 
56
49
const uint32_t max_field_size= (uint32_t) 4294967295U;
57
50
 
58
51
class SendField;
73
66
 *
74
67
 * @details
75
68
 *
76
 
 * The value stored in the Field object is stored in the
 
69
 * The value stored in the Field object is stored in the 
77
70
 * unsigned char pointer member variable called ptr.  The
78
 
 * val_xxx() methods retrieve this raw byte value and
 
71
 * val_xxx() methods retrieve this raw byte value and 
79
72
 * convert the byte into the appropriate output (int, decimal, etc).
80
73
 *
81
74
 * The store_xxx() methods take various input and convert
84
77
class Field
85
78
{
86
79
  /* Prevent use of these */
87
 
  Field(const Field&);
 
80
  Field(const Field&); 
88
81
  void operator=(Field &);
89
82
public:
90
83
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
91
84
  unsigned char *null_ptr; /**< Byte where null_bit is */
92
85
 
93
86
  /**
94
 
   * Pointer to the Table object containing this Field
 
87
   * Pointer to the Table object containing this Field 
95
88
   *
96
89
   * @note You can use table->in_use as replacement for current_session member
97
90
   * only inside of val_*() and store() members (e.g. you can't use it in cons)
98
91
   */
99
 
  Table *table;
 
92
  Table *table; 
100
93
  Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
101
94
  const char **table_name; /**< Pointer to the name of the table. @TODO This is redundant with Table::table_name. */
102
95
  const char *field_name; /**< Name of the field */
109
102
  key_map part_of_sortkey;
110
103
 
111
104
  /*
112
 
    We use three additional unireg types for TIMESTAMP for hysterical
113
 
    raisins and limitations in the MySQL FRM file format.
114
 
 
115
 
    A good TODO is to clean this up as we can support just about
116
 
    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().
117
111
  */
118
112
  enum utype
119
 
  {
 
113
  { 
120
114
    NONE,
121
115
    NEXT_NUMBER,
122
116
    TIMESTAMP_OLD_FIELD,
140
134
   */
141
135
  bool is_created_from_null_item;
142
136
 
143
 
  static void *operator new(size_t size);
144
 
  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); }
145
140
  static void operator delete(void *, size_t)
146
 
  { }
 
141
  { TRASH(ptr_arg, size); }
147
142
 
148
143
  Field(unsigned char *ptr_arg,
149
144
        uint32_t length_arg,
153
148
        const char *field_name_arg);
154
149
  virtual ~Field() {}
155
150
  /* Store functions returns 1 on overflow and -1 on fatal error */
156
 
  virtual int store(const char *to,
157
 
                    uint32_t length,
 
151
  virtual int store(const char *to, 
 
152
                    uint32_t length, 
158
153
                    const CHARSET_INFO * const cs)=0;
159
154
  virtual int store(double nr)=0;
160
155
  virtual int store(int64_t nr, bool unsigned_val)=0;
161
156
  virtual int store_decimal(const my_decimal *d)=0;
162
 
  int store(const char *to,
 
157
  int store(const char *to, 
163
158
            uint32_t length,
164
159
            const CHARSET_INFO * const cs,
165
160
            enum_check_fields check_level);
173
168
  virtual double val_real(void)=0;
174
169
  virtual int64_t val_int(void)=0;
175
170
  virtual my_decimal *val_decimal(my_decimal *);
176
 
  inline String *val_str(String *str)
 
171
  inline String *val_str(String *str) 
177
172
  {
178
173
    return val_str(str, str);
179
174
  }
190
185
     This trickery is used to decrease a number of malloc calls.
191
186
  */
192
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);
193
194
  /*
194
195
   str_needs_quotes() returns true if the value returned by val_str() needs
195
196
   to be quoted when used in constructing an SQL query.
202
203
     Check whether a field type can be partially indexed by a key.
203
204
 
204
205
     This is a static method, rather than a virtual function, because we need
205
 
     to check the type of a non-Field in alter_table().
 
206
     to check the type of a non-Field in mysql_alter_table().
206
207
 
207
208
     @param type  field type
208
209
 
257
258
   * table, which is located on disk).
258
259
   */
259
260
  virtual uint32_t pack_length_in_rec() const;
260
 
 
261
 
  /**
262
 
   * 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. 
263
294
   * For varstrings, this does _not_ include the length bytes.
264
295
   */
265
296
  virtual uint32_t data_length();
309
340
  // For new field
310
341
  virtual uint32_t size_of() const =0;
311
342
 
312
 
  bool is_null(ptrdiff_t row_offset= 0);
313
 
  bool is_real_null(ptrdiff_t row_offset= 0);
 
343
  bool is_null(my_ptrdiff_t row_offset= 0);
 
344
  bool is_real_null(my_ptrdiff_t row_offset= 0);
314
345
  bool is_null_in_record(const unsigned char *record);
315
346
  bool is_null_in_record_with_offset(ptrdiff_t offset);
316
347
  void set_null(ptrdiff_t row_offset= 0);
328
359
   * use field->val_int() for comparison.  Used to optimize clauses like
329
360
   * 'a_column BETWEEN date_const AND date_const'.
330
361
   */
331
 
  virtual bool can_be_compared_as_int64_t() const
 
362
  virtual bool can_be_compared_as_int64_t() const 
332
363
  {
333
364
    return false;
334
365
  }
335
366
  virtual void free() {}
336
 
  virtual Field *new_field(memory::Root *root,
 
367
  virtual Field *new_field(MEM_ROOT *root, 
337
368
                           Table *new_table,
338
369
                           bool keep_type);
339
 
  virtual Field *new_key_field(memory::Root *root, Table *new_table,
340
 
                               unsigned char *new_ptr,
 
370
  virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
 
371
                               unsigned char *new_ptr, 
341
372
                               unsigned char *new_null_ptr,
342
373
                               uint32_t new_null_bit);
343
374
  /** This is used to generate a field in Table from TableShare */
344
 
  Field *clone(memory::Root *mem_root, Table *new_table);
 
375
  Field *clone(MEM_ROOT *mem_root, Table *new_table);
345
376
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
346
377
  {
347
378
    ptr= ptr_arg;
388
419
   * characters have maximal possible size (mbmaxlen). In the other words,
389
420
   * "length" parameter is a number of characters multiplied by
390
421
   * field_charset->mbmaxlen.
391
 
   *
 
422
   * 
392
423
   * @retval
393
424
   *   Number of copied bytes (excluding padded zero bytes -- see above).
394
425
   */
418
449
  {
419
450
    unsigned char *old_ptr= ptr;
420
451
    int64_t return_value;
421
 
    ptr= const_cast<unsigned char*>(new_ptr);
 
452
    ptr= (unsigned char*) new_ptr;
422
453
    return_value= val_int();
423
454
    ptr= old_ptr;
424
455
    return return_value;
426
457
  inline String *val_str(String *str, const unsigned char *new_ptr)
427
458
  {
428
459
    unsigned char *old_ptr= ptr;
429
 
    ptr= const_cast<unsigned char*>(new_ptr);
 
460
    ptr= (unsigned char*) new_ptr;
430
461
    val_str(str);
431
462
    ptr= old_ptr;
432
463
    return str;
519
550
 
520
551
  virtual unsigned char *pack_key(unsigned char* to,
521
552
                                  const unsigned char *from,
522
 
                                  uint32_t max_length,
 
553
                                  uint32_t max_length, 
523
554
                                  bool low_byte_first)
524
555
  {
525
556
    return pack(to, from, max_length, low_byte_first);
526
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
  }
527
565
  virtual const unsigned char *unpack_key(unsigned char* to,
528
566
                                          const unsigned char *from,
529
567
                                          uint32_t max_length,
531
569
  {
532
570
    return unpack(to, from, max_length, low_byte_first);
533
571
  }
 
572
  virtual uint32_t packed_col_length(const unsigned char *to, uint32_t length);
534
573
  virtual uint32_t max_packed_col_length(uint32_t max_length)
535
574
  {
536
575
    return max_length;
537
576
  }
538
577
 
 
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
 
539
586
  inline uint32_t offset(unsigned char *record)
540
587
  {
541
588
    return (uint32_t) (ptr - record);
574
621
    @retval
575
622
      0 otherwise
576
623
  */
577
 
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
 
624
  bool set_warning(DRIZZLE_ERROR::enum_warning_level, 
578
625
                   unsigned int code,
579
626
                   int cuted_increment);
580
627
  /**
592
639
      fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
593
640
      thread.
594
641
  */
595
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
642
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
596
643
                            uint32_t code,
597
 
                            const char *str,
 
644
                            const char *str, 
598
645
                            uint32_t str_len,
599
 
                            enum enum_drizzle_timestamp_type ts_type,
 
646
                            enum enum_drizzle_timestamp_type ts_type, 
600
647
                            int cuted_increment);
601
648
  /**
602
649
    Produce warning or note about integer datetime value saved into field.
612
659
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
613
660
      thread.
614
661
  */
615
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
662
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
616
663
                            uint32_t code,
617
 
                            int64_t nr,
 
664
                            int64_t nr, 
618
665
                            enum enum_drizzle_timestamp_type ts_type,
619
666
                            int cuted_increment);
620
667
  /**
630
677
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
631
678
      thread.
632
679
  */
633
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
 
680
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, 
634
681
                            const uint32_t code,
635
 
                            double nr,
 
682
                            double nr, 
636
683
                            enum enum_drizzle_timestamp_type ts_type);
637
684
  inline bool check_overflow(int op_result)
638
685
  {
668
715
    @return
669
716
      value converted from val
670
717
  */
671
 
  int64_t convert_decimal2int64_t(const my_decimal *val,
 
718
  int64_t convert_decimal2int64_t(const my_decimal *val, 
672
719
                                  bool unsigned_flag,
673
720
                                  int *err);
674
721
  /* The max. number of characters */
704
751
  bool isWriteSet();
705
752
  void setReadSet(bool arg= true);
706
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
  }
707
771
};
708
772
 
709
 
} /* namespace drizzled */
710
 
 
711
 
/** @TODO Why is this in the middle of the file???*/
712
773
#include "drizzled/create_field.h"
713
774
 
714
 
namespace drizzled
715
 
{
716
 
 
717
775
/**
718
776
 * A class for sending field information to a client.
719
777
 *
722
780
 * Send_field is basically a stripped-down POD class for
723
781
 * representing basic information about a field...
724
782
 */
725
 
class SendField
 
783
class SendField 
726
784
{
727
785
public:
728
786
  const char *db_name;
741
799
/**
742
800
 * A class for quick copying data to fields
743
801
 */
744
 
class CopyField :public memory::SqlAlloc
 
802
class CopyField :public Sql_alloc
745
803
{
746
804
  /**
747
805
    Convenience definition of a copy function returned by
772
830
};
773
831
 
774
832
Field *make_field(TableShare *share,
775
 
                  memory::Root *root,
 
833
                  MEM_ROOT *root,
776
834
                  unsigned char *ptr,
777
835
                  uint32_t field_length,
778
 
                  bool is_nullable,
779
836
                  unsigned char *null_pos,
780
837
                  unsigned char null_bit,
781
 
                  uint8_t decimals,
 
838
                  uint32_t pack_flag,
782
839
                  enum_field_types field_type,
783
840
                  const CHARSET_INFO * cs,
784
841
                  Field::utype unireg_check,
807
864
                            const char *str,
808
865
                            const char *strend);
809
866
 
810
 
} /* namespace drizzled */
811
 
 
812
867
#endif /* DRIZZLED_FIELD_H */