~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Olaf van der Spek
  • Date: 2011-03-28 14:32:36 UTC
  • mto: (2257.1.1 build) (2276.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2258.
  • Revision ID: olafvdspek@gmail.com-20110328143236-4ge1d793iqaktfq0
Common fwd

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
 
#ifndef DRIZZLED_FIELD_H
26
 
#define DRIZZLED_FIELD_H
27
 
 
28
 
#include "drizzled/sql_error.h"
29
 
#include "drizzled/decimal.h"
30
 
#include "drizzled/key_map.h"
31
 
#include "drizzled/sql_list.h"
32
 
#include "drizzled/structs.h"
33
 
#include "drizzled/charset_info.h"
34
 
#include "drizzled/item_result.h"
 
25
 
 
26
 
 
27
#pragma once
 
28
 
 
29
#include <drizzled/common_fwd.h>
 
30
#include <drizzled/sql_error.h>
 
31
#include <drizzled/type/decimal.h>
 
32
#include <drizzled/key_map.h>
 
33
#include <drizzled/sql_list.h>
 
34
#include <drizzled/structs.h>
 
35
#include <drizzled/charset_info.h>
 
36
#include <drizzled/item_result.h>
 
37
#include <drizzled/charset_info.h>
35
38
 
36
39
#include <string>
37
40
#include <vector>
38
41
 
 
42
#include <drizzled/visibility.h>
 
43
 
39
44
namespace drizzled
40
45
{
41
46
 
50
55
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
51
56
#endif
52
57
 
53
 
typedef struct st_typelib TYPELIB;
54
 
 
55
58
const uint32_t max_field_size= (uint32_t) 4294967295U;
56
59
 
57
60
class SendField;
75
78
 * The store_xxx() methods take various input and convert
76
79
 * the input into the raw bytes stored in the ptr member variable.
77
80
 */
78
 
class Field
 
81
class DRIZZLED_API Field
79
82
{
80
83
  /* Prevent use of these */
81
84
  Field(const Field&);
93
96
   */
94
97
private:
95
98
  Table *table;
 
99
 
96
100
public:
97
101
  Table *getTable()
98
102
  {
141
145
  utype unireg_check;
142
146
  uint32_t field_length; /**< Length of this field in bytes */
143
147
  uint32_t flags;
 
148
 
 
149
  bool isUnsigned() const
 
150
  {
 
151
    return flags & UNSIGNED_FLAG;
 
152
  }
 
153
 
144
154
private:
145
155
  uint16_t field_index; /**< Index of this Field in Table::fields array */
146
156
 
190
200
  /* Store functions returns 1 on overflow and -1 on fatal error */
191
201
  virtual int store(const char *to,
192
202
                    uint32_t length,
193
 
                    const CHARSET_INFO * const cs)=0;
 
203
                    const charset_info_st * const cs)=0;
194
204
  virtual int store(double nr)=0;
195
205
  virtual int store(int64_t nr, bool unsigned_val)=0;
196
 
  virtual int store_decimal(const my_decimal *d)=0;
 
206
  virtual int store_decimal(const type::Decimal *d)=0;
197
207
  int store_and_check(enum_check_fields check_level,
198
208
                      const char *to,
199
209
                      uint32_t length,
200
 
                      const CHARSET_INFO * const cs);
 
210
                      const charset_info_st * const cs);
201
211
  /**
202
212
    This is called when storing a date in a string.
203
213
 
204
214
    @note
205
215
      Needs to be changed if/when we want to support different time formats.
206
216
  */
207
 
  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;
210
 
  virtual my_decimal *val_decimal(my_decimal *);
211
 
  String *val_str_internal(String *str)
 
217
  virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
 
218
  virtual double val_real() const=0;
 
219
  virtual int64_t val_int() const =0;
 
220
  virtual type::Decimal *val_decimal(type::Decimal *) const;
 
221
  String *val_str_internal(String *str) const
212
222
  {
213
223
    return val_str(str, str);
214
224
  }
 
225
 
215
226
  /*
216
227
     val_str(buf1, buf2) gets two buffers and should use them as follows:
217
228
     if it needs a temp buffer to convert result to string - use buf1
224
235
     an unnecessary free (and later, may be an alloc).
225
236
     This trickery is used to decrease a number of malloc calls.
226
237
  */
227
 
  virtual String *val_str(String*, String *)=0;
 
238
  virtual String *val_str(String*, String *) const =0;
 
239
 
228
240
  /*
229
241
   str_needs_quotes() returns true if the value returned by val_str() needs
230
242
   to be quoted when used in constructing an SQL query.
233
245
  virtual Item_result result_type () const=0;
234
246
  virtual Item_result cmp_type () const { return result_type(); }
235
247
  virtual Item_result cast_to_int_type () const { return result_type(); }
 
248
 
236
249
  /**
237
250
     Check whether a field type can be partially indexed by a key.
238
251
 
279
292
   */
280
293
  virtual bool eq_def(Field *field);
281
294
 
 
295
  virtual bool is_timestamp() const
 
296
  {
 
297
    return false;
 
298
  }
 
299
 
282
300
  /**
283
301
   * Returns size (in bytes) used to store field data in memory
284
302
   * (i.e. it returns the maximum size of the field in a row of the table,
343
361
  // For new field
344
362
  virtual uint32_t size_of() const =0;
345
363
 
346
 
  bool is_null(ptrdiff_t row_offset= 0);
347
 
  bool is_real_null(ptrdiff_t row_offset= 0);
348
 
  bool is_null_in_record(const unsigned char *record);
349
 
  bool is_null_in_record_with_offset(ptrdiff_t offset);
 
364
  bool is_null(ptrdiff_t row_offset= 0) const;
 
365
  bool is_real_null(ptrdiff_t row_offset= 0) const;
 
366
  bool is_null_in_record(const unsigned char *record) const;
 
367
  bool is_null_in_record_with_offset(ptrdiff_t offset) const;
350
368
  void set_null(ptrdiff_t row_offset= 0);
351
369
  void set_notnull(ptrdiff_t row_offset= 0);
352
 
  bool maybe_null(void);
353
 
  bool real_maybe_null(void);
 
370
  bool maybe_null(void) const;
 
371
  bool real_maybe_null(void) const;
354
372
 
355
373
  virtual void make_field(SendField *);
356
374
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
389
407
    if (null_ptr)
390
408
      null_ptr= ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
391
409
  }
392
 
  virtual void get_image(unsigned char *buff, uint32_t length, const CHARSET_INFO * const)
 
410
  virtual void get_image(unsigned char *buff, uint32_t length, const charset_info_st * const)
393
411
  {
394
412
    memcpy(buff,ptr,length);
395
413
  }
396
 
  virtual void get_image(std::basic_string<unsigned char> &buff, uint32_t length, const CHARSET_INFO * const)
 
414
  virtual void get_image(std::basic_string<unsigned char> &buff, uint32_t length, const charset_info_st * const)
397
415
  {
398
416
    buff.append(ptr,length);
399
417
  }
400
 
  virtual void set_image(const unsigned char *buff,uint32_t length, const CHARSET_INFO * const)
 
418
  virtual void set_image(const unsigned char *buff,uint32_t length, const charset_info_st * const)
401
419
  {
402
420
    memcpy(ptr,buff,length);
403
421
  }
451
469
  int64_t val_int_internal(const unsigned char *new_ptr)
452
470
  {
453
471
    unsigned char *old_ptr= ptr;
454
 
    int64_t return_value;
455
472
    ptr= const_cast<unsigned char*>(new_ptr);
456
 
    return_value= val_int();
 
473
    int64_t return_value= val_int();
457
474
    ptr= old_ptr;
458
475
    return return_value;
459
476
  }
577
594
  }
578
595
  void copy_from_tmp(int offset);
579
596
  uint32_t fill_cache_field(CacheField *copy);
580
 
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
581
 
  virtual bool get_time(DRIZZLE_TIME *ltime);
582
 
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
583
 
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
 
597
  virtual bool get_date(type::Time &ltime,uint32_t fuzzydate) const;
 
598
  virtual bool get_time(type::Time &ltime) const;
 
599
  virtual const charset_info_st *charset(void) const { return &my_charset_bin; }
 
600
  virtual const charset_info_st *sort_charset(void) const { return charset(); }
584
601
  virtual bool has_charset(void) const { return false; }
585
 
  virtual void set_charset(const CHARSET_INFO * const)
 
602
  virtual void set_charset(const charset_info_st * const)
586
603
  {}
587
604
  virtual enum Derivation derivation(void) const
588
605
  {
610
627
      0 otherwise
611
628
  */
612
629
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
613
 
                   unsigned int code,
 
630
                   drizzled::error_t code,
614
631
                   int cuted_increment);
615
632
  /**
616
633
    Produce warning or note about datetime string data saved into field.
628
645
      thread.
629
646
  */
630
647
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
631
 
                            uint32_t code,
 
648
                            drizzled::error_t code,
632
649
                            const char *str,
633
650
                            uint32_t str_len,
634
 
                            enum enum_drizzle_timestamp_type ts_type,
 
651
                            type::timestamp_t ts_type,
635
652
                            int cuted_increment);
636
653
  /**
637
654
    Produce warning or note about integer datetime value saved into field.
648
665
      thread.
649
666
  */
650
667
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
651
 
                            uint32_t code,
 
668
                            drizzled::error_t code,
652
669
                            int64_t nr,
653
 
                            enum enum_drizzle_timestamp_type ts_type,
 
670
                            type::timestamp_t ts_type,
654
671
                            int cuted_increment);
655
672
  /**
656
673
    Produce warning or note about double datetime data saved into field.
666
683
      thread.
667
684
  */
668
685
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
669
 
                            const uint32_t code,
 
686
                            const drizzled::error_t code,
670
687
                            double nr,
671
 
                            enum enum_drizzle_timestamp_type ts_type);
 
688
                            type::timestamp_t ts_type);
672
689
  bool check_overflow(int op_result)
673
690
  {
674
691
    return (op_result == E_DEC_OVERFLOW);
703
720
    @return
704
721
      value converted from val
705
722
  */
706
 
  int64_t convert_decimal2int64_t(const my_decimal *val,
 
723
  int64_t convert_decimal2int64_t(const type::Decimal *val,
707
724
                                  bool unsigned_flag,
708
725
                                  int *err);
709
726
  /* The max. number of characters */
719
736
  }
720
737
 
721
738
  /* Hash value */
722
 
  virtual void hash(uint32_t *nr, uint32_t *nr2);
 
739
  virtual void hash(uint32_t *nr, uint32_t *nr2) const;
723
740
  friend bool reopen_table(Session *,Table *,bool);
724
741
 
725
742
  friend class CopyField;
735
752
  friend class Item_sum_max;
736
753
  friend class Item_func_group_concat;
737
754
 
738
 
  bool isReadSet();
 
755
  bool isReadSet() const;
739
756
  bool isWriteSet();
740
757
  void setReadSet(bool arg= true);
741
758
  void setWriteSet(bool arg= true);
 
759
 
 
760
protected:
 
761
 
 
762
  void pack_num(uint64_t arg, unsigned char *destination= NULL);
 
763
  void pack_num(uint32_t arg, unsigned char *destination= NULL);
 
764
  uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
 
765
  uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
742
766
};
743
767
 
 
768
namespace field {
 
769
 
 
770
inline bool isDateTime(const enum_field_types &arg)
 
771
{
 
772
  switch (arg)
 
773
  {
 
774
  case DRIZZLE_TYPE_DATE:
 
775
  case DRIZZLE_TYPE_DATETIME:
 
776
  case DRIZZLE_TYPE_MICROTIME:
 
777
  case DRIZZLE_TYPE_TIME:
 
778
  case DRIZZLE_TYPE_TIMESTAMP:
 
779
    return true;
 
780
 
 
781
  case DRIZZLE_TYPE_BLOB:
 
782
  case DRIZZLE_TYPE_BOOLEAN:
 
783
  case DRIZZLE_TYPE_DECIMAL:
 
784
  case DRIZZLE_TYPE_DOUBLE:
 
785
  case DRIZZLE_TYPE_ENUM:
 
786
  case DRIZZLE_TYPE_LONG:
 
787
  case DRIZZLE_TYPE_LONGLONG:
 
788
  case DRIZZLE_TYPE_NULL:
 
789
  case DRIZZLE_TYPE_UUID:
 
790
  case DRIZZLE_TYPE_VARCHAR:
 
791
    return false;
 
792
  }
 
793
 
 
794
  assert(0);
 
795
  abort();
 
796
}
 
797
 
 
798
} // namespace field
 
799
 
744
800
std::ostream& operator<<(std::ostream& output, const Field &field);
745
801
 
746
802
} /* namespace drizzled */
747
803
 
748
804
/** @TODO Why is this in the middle of the file???*/
749
 
#include "drizzled/create_field.h"
 
805
#include <drizzled/create_field.h>
750
806
 
751
807
namespace drizzled
752
808
{
775
831
  SendField() {}
776
832
};
777
833
 
778
 
/**
779
 
 * A class for quick copying data to fields
780
 
 */
781
 
class CopyField :public memory::SqlAlloc
782
 
{
783
 
  /**
784
 
    Convenience definition of a copy function returned by
785
 
    get_copy_func.
786
 
  */
787
 
  typedef void Copy_func(CopyField*);
788
 
  Copy_func *get_copy_func(Field *to, Field *from);
789
 
public:
790
 
  unsigned char *from_ptr;
791
 
  unsigned char *to_ptr;
792
 
  unsigned char *from_null_ptr;
793
 
  unsigned char *to_null_ptr;
794
 
  bool *null_row;
795
 
  uint32_t from_bit;
796
 
  uint32_t to_bit;
797
 
  uint32_t from_length;
798
 
  uint32_t to_length;
799
 
  Field *from_field;
800
 
  Field *to_field;
801
 
  String tmp;                                   // For items
802
 
 
803
 
  CopyField() {}
804
 
  ~CopyField() {}
805
 
  void set(Field *to,Field *from,bool save);    // Field to field
806
 
  void set(unsigned char *to,Field *from);              // Field to string
807
 
  void (*do_copy)(CopyField *);
808
 
  void (*do_copy2)(CopyField *);                // Used to handle null values
809
 
};
810
 
 
811
834
uint32_t pack_length_to_packflag(uint32_t type);
812
835
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
813
836
int set_field_to_null(Field *field);
826
849
 * @retval
827
850
 *  true  - If string has some important data
828
851
 */
829
 
bool test_if_important_data(const CHARSET_INFO * const cs,
 
852
bool test_if_important_data(const charset_info_st * const cs,
830
853
                            const char *str,
831
854
                            const char *strend);
832
855
 
833
856
} /* namespace drizzled */
834
857
 
835
 
#endif /* DRIZZLED_FIELD_H */