~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

Merge Stewart.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
22
22
  variables must declare the size_of() member function.
23
23
*/
24
24
 
25
 
 
26
 
 
27
25
#ifndef DRIZZLED_FIELD_H
28
26
#define DRIZZLED_FIELD_H
29
27
 
30
28
#include "drizzled/sql_error.h"
31
 
#include "drizzled/type/decimal.h"
 
29
#include "drizzled/my_decimal.h"
32
30
#include "drizzled/key_map.h"
 
31
#include "drizzled/sql_bitmap.h"
33
32
#include "drizzled/sql_list.h"
34
33
#include "drizzled/structs.h"
35
34
#include "drizzled/charset_info.h"
36
 
#include "drizzled/item_result.h"
37
 
#include "drizzled/charset_info.h"
38
35
 
39
36
#include <string>
40
37
#include <vector>
41
38
 
42
 
#include "drizzled/visibility.h"
43
 
 
44
39
namespace drizzled
45
40
{
46
41
 
48
43
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
49
44
 
50
45
#ifdef DEBUG
51
 
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
52
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!getTable() || (getTable()->write_set == NULL || isWriteSet()))
 
46
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!table || (table->read_set == NULL || isReadSet()))
 
47
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!table || (table->write_set == NULL || isWriteSet()))
53
48
#else
54
 
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
55
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
 
49
#define ASSERT_COLUMN_MARKED_FOR_READ
 
50
#define ASSERT_COLUMN_MARKED_FOR_WRITE
56
51
#endif
57
52
 
58
53
typedef struct st_typelib TYPELIB;
63
58
class CreateField;
64
59
class TableShare;
65
60
class Field;
66
 
struct CacheField;
 
61
struct st_cache_field;
67
62
 
68
63
int field_conv(Field *to,Field *from);
69
64
 
 
65
inline uint32_t get_enum_pack_length(int elements)
 
66
{
 
67
  return elements < 256 ? 1 : 2;
 
68
}
 
69
 
70
70
/**
71
71
 * Class representing a Field in a Table
72
72
 *
80
80
 * The store_xxx() methods take various input and convert
81
81
 * the input into the raw bytes stored in the ptr member variable.
82
82
 */
83
 
class DRIZZLED_API Field
 
83
class Field
84
84
{
85
85
  /* Prevent use of these */
86
86
  Field(const Field&);
87
87
  void operator=(Field &);
88
 
 
89
88
public:
90
89
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
91
90
  unsigned char *null_ptr; /**< Byte where null_bit is */
96
95
   * @note You can use table->in_use as replacement for current_session member
97
96
   * only inside of val_*() and store() members (e.g. you can't use it in cons)
98
97
   */
99
 
private:
100
98
  Table *table;
101
 
 
102
 
public:
103
 
  Table *getTable()
104
 
  {
105
 
    assert(table);
106
 
    return table;
107
 
  }
108
 
 
109
 
  Table *getTable() const
110
 
  {
111
 
    assert(table);
112
 
    return table;
113
 
  }
114
 
 
115
 
  void setTable(Table *table_arg)
116
 
  {
117
 
    table= table_arg;
118
 
  }
119
 
 
120
99
  Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
 
100
  const char **table_name; /**< Pointer to the name of the table. @TODO This is redundant with Table::table_name. */
121
101
  const char *field_name; /**< Name of the field */
122
102
  LEX_STRING comment; /**< A comment about the field */
123
103
 
147
127
  utype unireg_check;
148
128
  uint32_t field_length; /**< Length of this field in bytes */
149
129
  uint32_t flags;
150
 
 
151
 
  bool isUnsigned() const
152
 
  {
153
 
    return flags & UNSIGNED_FLAG;
154
 
  }
155
 
 
156
 
private:
157
130
  uint16_t field_index; /**< Index of this Field in Table::fields array */
158
 
 
159
 
public:
160
 
 
161
 
  uint16_t position() const
162
 
  {
163
 
    return field_index;
164
 
  }
165
 
 
166
 
  void setPosition(uint32_t arg)
167
 
  {
168
 
    field_index= arg;
169
 
  }
170
 
 
171
131
  unsigned char null_bit; /**< Bit used to test null bit */
172
132
  /**
173
133
     If true, this field was created in create_tmp_field_from_item from a NULL
183
143
  static void *operator new(size_t size, memory::Root *mem_root);
184
144
  static void operator delete(void *, size_t)
185
145
  { }
186
 
  static void operator delete(void *, memory::Root *)
187
 
  { }
188
146
 
189
147
  Field(unsigned char *ptr_arg,
190
148
        uint32_t length_arg,
193
151
        utype unireg_check_arg,
194
152
        const char *field_name_arg);
195
153
  virtual ~Field() {}
196
 
 
197
 
  bool hasDefault() const
198
 
  {
199
 
    return not (flags & NO_DEFAULT_VALUE_FLAG);
200
 
  }
201
 
 
202
154
  /* Store functions returns 1 on overflow and -1 on fatal error */
203
155
  virtual int store(const char *to,
204
156
                    uint32_t length,
205
157
                    const CHARSET_INFO * const cs)=0;
206
158
  virtual int store(double nr)=0;
207
159
  virtual int store(int64_t nr, bool unsigned_val)=0;
208
 
  virtual int store_decimal(const type::Decimal *d)=0;
209
 
  int store_and_check(enum_check_fields check_level,
210
 
                      const char *to,
211
 
                      uint32_t length,
212
 
                      const CHARSET_INFO * const cs);
 
160
  virtual int store_decimal(const my_decimal *d)=0;
 
161
  int store(const char *to,
 
162
            uint32_t length,
 
163
            const CHARSET_INFO * const cs,
 
164
            enum_check_fields check_level);
213
165
  /**
214
166
    This is called when storing a date in a string.
215
167
 
216
168
    @note
217
169
      Needs to be changed if/when we want to support different time formats.
218
170
  */
219
 
  virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
220
 
  virtual double val_real()=0;
221
 
  virtual int64_t val_int()=0;
222
 
  virtual type::Decimal *val_decimal(type::Decimal *);
223
 
  String *val_str_internal(String *str)
 
171
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
 
172
  virtual double val_real(void)=0;
 
173
  virtual int64_t val_int(void)=0;
 
174
  virtual my_decimal *val_decimal(my_decimal *);
 
175
  inline String *val_str(String *str)
224
176
  {
225
177
    return val_str(str, str);
226
178
  }
227
 
 
228
179
  /*
229
180
     val_str(buf1, buf2) gets two buffers and should use them as follows:
230
181
     if it needs a temp buffer to convert result to string - use buf1
238
189
     This trickery is used to decrease a number of malloc calls.
239
190
  */
240
191
  virtual String *val_str(String*, String *)=0;
241
 
 
 
192
  /**
 
193
   * Interpret field value as an integer but return the result as a string.
 
194
   *
 
195
   * This is used for printing bit_fields as numbers while debugging.
 
196
   */
 
197
  String *val_int_as_str(String *val_buffer, bool unsigned_flag);
242
198
  /*
243
199
   str_needs_quotes() returns true if the value returned by val_str() needs
244
200
   to be quoted when used in constructing an SQL query.
247
203
  virtual Item_result result_type () const=0;
248
204
  virtual Item_result cmp_type () const { return result_type(); }
249
205
  virtual Item_result cast_to_int_type () const { return result_type(); }
250
 
 
251
206
  /**
252
207
     Check whether a field type can be partially indexed by a key.
253
208
 
294
249
   */
295
250
  virtual bool eq_def(Field *field);
296
251
 
297
 
  virtual bool is_timestamp() const
298
 
  {
299
 
    return false;
300
 
  }
301
 
 
302
252
  /**
303
253
   * Returns size (in bytes) used to store field data in memory
304
254
   * (i.e. it returns the maximum size of the field in a row of the table,
312
262
   * table, which is located on disk).
313
263
   */
314
264
  virtual uint32_t pack_length_in_rec() const;
 
265
  /**
 
266
    Check to see if field size is compatible with destination.
 
267
 
 
268
    This method is used in row-based replication to verify that the slave's
 
269
    field size is less than or equal to the master's field size. The
 
270
    encoded field metadata (from the master or source) is decoded and compared
 
271
    to the size of this field (the slave or destination).
 
272
 
 
273
    @param   field_metadata   Encoded size in field metadata
 
274
 
 
275
    @retval 0 if this field's size is < the source field's size
 
276
    @retval 1 if this field's size is >= the source field's size
 
277
  */
 
278
  virtual int compatible_field_size(uint32_t field_metadata);
 
279
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata);
 
280
 
 
281
  /*
 
282
    This method is used to return the size of the data in a row-based
 
283
    replication row record. The default implementation of returning 0 is
 
284
    designed to allow fields that do not use metadata to return true (1)
 
285
    from compatible_field_size() which uses this function in the comparison.
 
286
    The default value for field metadata for fields that do not have
 
287
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
 
288
 
 
289
    Note: While most classes that override this method return pack_length(),
 
290
    the classes Field_varstring, and Field_blob return
 
291
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
 
292
  */
 
293
  virtual uint32_t row_pack_length();
315
294
 
316
295
  /**
317
296
   * Return the "real size" of the data in memory.
342
321
  virtual uint32_t key_length() const;
343
322
  virtual enum_field_types type() const =0;
344
323
  virtual enum_field_types real_type() const;
345
 
  virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
 
324
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
 
325
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
 
326
                      uint32_t max_len);
346
327
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
347
 
  int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
348
328
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
349
329
                         uint32_t max_length=UINT32_MAX);
350
330
  virtual int cmp_offset(uint32_t row_offset);
396
376
                               uint32_t new_null_bit);
397
377
  /** This is used to generate a field in Table from TableShare */
398
378
  Field *clone(memory::Root *mem_root, Table *new_table);
399
 
  void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
 
379
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
400
380
  {
401
381
    ptr= ptr_arg;
402
382
    null_ptr= null_ptr_arg;
403
383
    null_bit= null_bit_arg;
404
384
  }
405
 
  void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
 
385
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
406
386
  virtual void move_field_offset(ptrdiff_t ptr_diff)
407
387
  {
408
388
    ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
460
440
  {
461
441
    set_image(buff,length, &my_charset_bin);
462
442
  }
463
 
  int64_t val_int_offset(uint32_t row_offset)
 
443
  inline int64_t val_int_offset(uint32_t row_offset)
464
444
  {
465
445
    ptr+=row_offset;
466
446
    int64_t tmp=val_int();
468
448
    return tmp;
469
449
  }
470
450
 
471
 
  int64_t val_int_internal(const unsigned char *new_ptr)
 
451
  inline int64_t val_int(const unsigned char *new_ptr)
472
452
  {
473
453
    unsigned char *old_ptr= ptr;
474
454
    int64_t return_value;
477
457
    ptr= old_ptr;
478
458
    return return_value;
479
459
  }
480
 
 
481
 
  String *val_str_internal(String *str, const unsigned char *new_ptr)
 
460
  inline String *val_str(String *str, const unsigned char *new_ptr)
482
461
  {
483
462
    unsigned char *old_ptr= ptr;
484
463
    ptr= const_cast<unsigned char*>(new_ptr);
485
 
    val_str_internal(str);
 
464
    val_str(str);
486
465
    ptr= old_ptr;
487
466
    return str;
488
467
  }
591
570
    return max_length;
592
571
  }
593
572
 
594
 
  uint32_t offset(const unsigned char *record)
 
573
  inline uint32_t offset(unsigned char *record)
595
574
  {
596
575
    return (uint32_t) (ptr - record);
597
576
  }
598
577
  void copy_from_tmp(int offset);
599
 
  uint32_t fill_cache_field(CacheField *copy);
600
 
  virtual bool get_date(type::Time &ltime,uint32_t fuzzydate);
601
 
  virtual bool get_time(type::Time &ltime);
 
578
  uint32_t fill_cache_field(struct st_cache_field *copy);
 
579
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
 
580
  virtual bool get_time(DRIZZLE_TIME *ltime);
602
581
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
603
582
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
604
583
  virtual bool has_charset(void) const { return false; }
630
609
      0 otherwise
631
610
  */
632
611
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
633
 
                   drizzled::error_t code,
 
612
                   unsigned int code,
634
613
                   int cuted_increment);
635
614
  /**
636
615
    Produce warning or note about datetime string data saved into field.
648
627
      thread.
649
628
  */
650
629
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
651
 
                            drizzled::error_t code,
 
630
                            uint32_t code,
652
631
                            const char *str,
653
632
                            uint32_t str_len,
654
 
                            type::timestamp_t ts_type,
 
633
                            enum enum_drizzle_timestamp_type ts_type,
655
634
                            int cuted_increment);
656
635
  /**
657
636
    Produce warning or note about integer datetime value saved into field.
668
647
      thread.
669
648
  */
670
649
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
671
 
                            drizzled::error_t code,
 
650
                            uint32_t code,
672
651
                            int64_t nr,
673
 
                            type::timestamp_t ts_type,
 
652
                            enum enum_drizzle_timestamp_type ts_type,
674
653
                            int cuted_increment);
675
654
  /**
676
655
    Produce warning or note about double datetime data saved into field.
686
665
      thread.
687
666
  */
688
667
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
689
 
                            const drizzled::error_t code,
 
668
                            const uint32_t code,
690
669
                            double nr,
691
 
                            type::timestamp_t ts_type);
692
 
  bool check_overflow(int op_result)
 
670
                            enum enum_drizzle_timestamp_type ts_type);
 
671
  inline bool check_overflow(int op_result)
693
672
  {
694
673
    return (op_result == E_DEC_OVERFLOW);
695
674
  }
723
702
    @return
724
703
      value converted from val
725
704
  */
726
 
  int64_t convert_decimal2int64_t(const type::Decimal *val,
 
705
  int64_t convert_decimal2int64_t(const my_decimal *val,
727
706
                                  bool unsigned_flag,
728
707
                                  int *err);
729
708
  /* The max. number of characters */
730
 
  uint32_t char_length() const
 
709
  inline uint32_t char_length() const
731
710
  {
732
711
    return field_length / charset()->mbmaxlen;
733
712
  }
734
713
 
735
 
  enum column_format_type column_format() const
 
714
  inline enum column_format_type column_format() const
736
715
  {
737
716
    return (enum column_format_type)
738
717
      ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
759
738
  bool isWriteSet();
760
739
  void setReadSet(bool arg= true);
761
740
  void setWriteSet(bool arg= true);
762
 
 
763
 
protected:
764
 
 
765
 
  void pack_num(uint64_t arg, unsigned char *destination= NULL);
766
 
  void pack_num(uint32_t arg, unsigned char *destination= NULL);
767
 
  uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
768
 
  uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
769
741
};
770
742
 
771
 
std::ostream& operator<<(std::ostream& output, const Field &field);
772
 
 
773
743
} /* namespace drizzled */
774
744
 
775
745
/** @TODO Why is this in the middle of the file???*/
802
772
  SendField() {}
803
773
};
804
774
 
 
775
/**
 
776
 * A class for quick copying data to fields
 
777
 */
 
778
class CopyField :public memory::SqlAlloc
 
779
{
 
780
  /**
 
781
    Convenience definition of a copy function returned by
 
782
    get_copy_func.
 
783
  */
 
784
  typedef void Copy_func(CopyField*);
 
785
  Copy_func *get_copy_func(Field *to, Field *from);
 
786
public:
 
787
  unsigned char *from_ptr;
 
788
  unsigned char *to_ptr;
 
789
  unsigned char *from_null_ptr;
 
790
  unsigned char *to_null_ptr;
 
791
  bool *null_row;
 
792
  uint32_t from_bit;
 
793
  uint32_t to_bit;
 
794
  uint32_t from_length;
 
795
  uint32_t to_length;
 
796
  Field *from_field;
 
797
  Field *to_field;
 
798
  String tmp;                                   // For items
 
799
 
 
800
  CopyField() {}
 
801
  ~CopyField() {}
 
802
  void set(Field *to,Field *from,bool save);    // Field to field
 
803
  void set(unsigned char *to,Field *from);              // Field to string
 
804
  void (*do_copy)(CopyField *);
 
805
  void (*do_copy2)(CopyField *);                // Used to handle null values
 
806
};
 
807
 
 
808
Field *make_field(TableShare *share,
 
809
                  memory::Root *root,
 
810
                  unsigned char *ptr,
 
811
                  uint32_t field_length,
 
812
                  bool is_nullable,
 
813
                  unsigned char *null_pos,
 
814
                  unsigned char null_bit,
 
815
                  uint8_t decimals,
 
816
                  enum_field_types field_type,
 
817
                  const CHARSET_INFO * cs,
 
818
                  Field::utype unireg_check,
 
819
                  TYPELIB *interval,
 
820
                  const char *field_name);
 
821
 
805
822
uint32_t pack_length_to_packflag(uint32_t type);
806
823
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
807
824
int set_field_to_null(Field *field);