~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: mordred
  • Date: 2008-11-01 00:46:20 UTC
  • mto: (572.1.1 devel) (575.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: mordred@opensolaris-20081101004620-vd0kzsl9k40hvf4p
Some updates to dtrace support.

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
 
#ifdef USE_PRAGMA_INTERFACE
26
 
#pragma interface                       /* gcc class implementation */
27
 
#endif
 
25
#ifndef DRIZZLED_FIELD_H
 
26
#define DRIZZLED_FIELD_H
 
27
 
 
28
#include <drizzled/sql_error.h>
 
29
#include <drizzled/my_decimal.h>
28
30
 
29
31
#define DATETIME_DEC                     6
30
32
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
48
50
  return len > 4 ? 8 : len;
49
51
}
50
52
 
 
53
class virtual_column_info: public Sql_alloc
 
54
{
 
55
public:
 
56
  Item *expr_item;
 
57
  LEX_STRING expr_str;
 
58
  Item *item_free_list;
 
59
  virtual_column_info() 
 
60
  : expr_item(0), item_free_list(0),
 
61
    field_type(DRIZZLE_TYPE_VIRTUAL),
 
62
    is_stored(false), data_inited(false)
 
63
  {
 
64
    expr_str.str= NULL;
 
65
    expr_str.length= 0;
 
66
  };
 
67
  ~virtual_column_info() {}
 
68
  enum_field_types get_real_type()
 
69
  {
 
70
    assert(data_inited);
 
71
    return data_inited ? field_type : DRIZZLE_TYPE_VIRTUAL;
 
72
  }
 
73
  void set_field_type(enum_field_types fld_type)
 
74
  {
 
75
    /* Calling this function can only be done once. */
 
76
    assert(not data_inited);
 
77
    data_inited= true;
 
78
    field_type= fld_type;
 
79
  }
 
80
  bool get_field_stored()
 
81
  {
 
82
    assert(data_inited);
 
83
    return data_inited ? is_stored : true;
 
84
  }
 
85
  void set_field_stored(bool stored)
 
86
  {
 
87
    is_stored= stored;
 
88
  }
 
89
private:
 
90
  /*
 
91
    The following data is only updated by the parser and read
 
92
    when a Create_field object is created/initialized.
 
93
  */
 
94
  enum_field_types field_type;   /* Real field type*/
 
95
  bool is_stored;             /* Indication that the field is 
 
96
                                    phisically stored in the database*/
 
97
  /*
 
98
    This flag is used to prevent other applications from
 
99
    reading and using incorrect data.
 
100
  */
 
101
  bool data_inited; 
 
102
};
 
103
 
51
104
class Field
52
105
{
53
106
  Field(const Item &);                          /* Prevent use of these */
61
114
  unsigned char         *ptr;                   // Position to field in record
62
115
  unsigned char         *null_ptr;              // Byte where null_bit is
63
116
  /*
64
 
    Note that you can use table->in_use as replacement for current_thd member 
 
117
    Note that you can use table->in_use as replacement for current_session member 
65
118
    only inside of val_*() and store() members (e.g. you can't use it in cons)
66
119
  */
67
120
  Table *table;         // Pointer for table
101
154
   */
102
155
  bool is_created_from_null_item;
103
156
 
 
157
  /* Virtual column data */
 
158
  virtual_column_info *vcol_info;
 
159
  /*
 
160
    Indication that the field is phycically stored in tables 
 
161
    rather than just generated on SQL queries.
 
162
    As of now, false can only be set for generated-only virtual columns.
 
163
  */
 
164
  bool is_stored;
 
165
 
104
166
  Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
105
167
        unsigned char null_bit_arg, utype unireg_check_arg,
106
168
        const char *field_name_arg);
499
561
 
500
562
  /* Hash value */
501
563
  virtual void hash(uint32_t *nr, uint32_t *nr2);
502
 
  friend bool reopen_table(THD *,Table *,bool);
 
564
  friend bool reopen_table(Session *,Table *,bool);
503
565
  friend int cre_myisam(char * name, register Table *form, uint32_t options,
504
566
                        uint64_t auto_increment_value);
505
567
  friend class Copy_field;
543
605
  { return 0; }
544
606
};
545
607
 
546
 
 
547
 
class Field_num :public Field {
548
 
public:
549
 
  const uint8_t dec;
550
 
  bool decimal_precision;       // Purify cannot handle bit fields & only for decimal type
551
 
  bool unsigned_flag;   // Purify cannot handle bit fields
552
 
  Field_num(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
553
 
            unsigned char null_bit_arg, utype unireg_check_arg,
554
 
            const char *field_name_arg,
555
 
            uint8_t dec_arg, bool zero_arg, bool unsigned_arg);
556
 
  Item_result result_type () const { return REAL_RESULT; }
557
 
  void add_unsigned(String &res) const;
558
 
  friend class Create_field;
559
 
  void make_field(Send_field *);
560
 
  uint32_t decimals() const { return (uint32_t) dec; }
561
 
  uint32_t size_of() const { return sizeof(*this); }
562
 
  bool eq_def(Field *field);
563
 
  int store_decimal(const my_decimal *);
564
 
  my_decimal *val_decimal(my_decimal *);
565
 
  uint32_t is_equal(Create_field *new_field);
566
 
  int check_int(const CHARSET_INFO * const cs, const char *str, int length,
567
 
                const char *int_end, int error);
568
 
  bool get_int(const CHARSET_INFO * const cs, const char *from, uint32_t len, 
569
 
               int64_t *rnd, uint64_t unsigned_max, 
570
 
               int64_t signed_min, int64_t signed_max);
571
 
};
572
 
 
573
 
/* base class for all string related classes */
574
 
 
575
 
class Field_str :public Field {
576
 
protected:
577
 
  const CHARSET_INFO *field_charset;
578
 
  enum Derivation field_derivation;
579
 
public:
580
 
  Field_str(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
581
 
            unsigned char null_bit_arg, utype unireg_check_arg,
582
 
            const char *field_name_arg, const CHARSET_INFO * const charset);
583
 
  Item_result result_type () const { return STRING_RESULT; }
584
 
  uint32_t decimals() const { return NOT_FIXED_DEC; }
585
 
  int  store(double nr);
586
 
  int  store(int64_t nr, bool unsigned_val)=0;
587
 
  int  store_decimal(const my_decimal *);
588
 
  int  store(const char *to,uint32_t length, const CHARSET_INFO * const cs)=0;
589
 
  uint32_t size_of() const { return sizeof(*this); }
590
 
  const CHARSET_INFO *charset(void) const { return field_charset; }
591
 
  void set_charset(const CHARSET_INFO * const charset_arg) { field_charset= charset_arg; }
592
 
  enum Derivation derivation(void) const { return field_derivation; }
593
 
  virtual void set_derivation(enum Derivation derivation_arg)
594
 
  { field_derivation= derivation_arg; }
595
 
  bool binary() const { return field_charset == &my_charset_bin; }
596
 
  uint32_t max_display_length() { return field_length; }
597
 
  friend class Create_field;
598
 
  my_decimal *val_decimal(my_decimal *);
599
 
  virtual bool str_needs_quotes() { return true; }
600
 
  bool compare_str_field_flags(Create_field *new_field, uint32_t flags);
601
 
  uint32_t is_equal(Create_field *new_field);
602
 
};
603
 
 
604
 
 
605
 
/* base class for Field_varstring and Field_blob */
606
 
 
607
 
class Field_longstr :public Field_str
608
 
{
609
 
protected:
610
 
  int report_if_important_data(const char *ptr, const char *end);
611
 
public:
612
 
  Field_longstr(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
613
 
                unsigned char null_bit_arg, utype unireg_check_arg,
614
 
                const char *field_name_arg, const CHARSET_INFO * const charset_arg)
615
 
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
616
 
               field_name_arg, charset_arg)
617
 
    {}
618
 
 
619
 
  int store_decimal(const my_decimal *d);
620
 
  uint32_t max_data_length() const;
621
 
};
622
 
 
623
 
/* base class for float and double and decimal (old one) */
624
 
class Field_real :public Field_num {
625
 
public:
626
 
  bool not_fixed;
627
 
 
628
 
  Field_real(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
629
 
             unsigned char null_bit_arg, utype unireg_check_arg,
630
 
             const char *field_name_arg,
631
 
             uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
632
 
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
633
 
               field_name_arg, dec_arg, zero_arg, unsigned_arg),
634
 
    not_fixed(dec_arg >= NOT_FIXED_DEC)
635
 
    {}
636
 
  int store_decimal(const my_decimal *);
637
 
  my_decimal *val_decimal(my_decimal *);
638
 
  int truncate(double *nr, double max_length);
639
 
  uint32_t max_display_length() { return field_length; }
640
 
  uint32_t size_of() const { return sizeof(*this); }
641
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
642
 
                              uint32_t param_data, bool low_byte_first);
643
 
  virtual unsigned char *pack(unsigned char* to, const unsigned char *from,
644
 
                      uint32_t max_length, bool low_byte_first);
645
 
};
646
 
 
647
 
 
648
 
class Field_tiny :public Field_num {
649
 
public:
650
 
  Field_tiny(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
651
 
             unsigned char null_bit_arg,
652
 
             enum utype unireg_check_arg, const char *field_name_arg,
653
 
             bool zero_arg, bool unsigned_arg)
654
 
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
655
 
               unireg_check_arg, field_name_arg,
656
 
               0, zero_arg,unsigned_arg)
657
 
    {}
658
 
  enum Item_result result_type () const { return INT_RESULT; }
659
 
  enum_field_types type() const { return DRIZZLE_TYPE_TINY;}
660
 
  enum ha_base_keytype key_type() const
661
 
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
662
 
  int store(const char *to,uint32_t length, const CHARSET_INFO * const charset);
663
 
  int store(double nr);
664
 
  int store(int64_t nr, bool unsigned_val);
665
 
  int reset(void) { ptr[0]=0; return 0; }
666
 
  double val_real(void);
667
 
  int64_t val_int(void);
668
 
  String *val_str(String*,String *);
669
 
  bool send_binary(Protocol *protocol);
670
 
  int cmp(const unsigned char *,const unsigned char *);
671
 
  void sort_string(unsigned char *buff,uint32_t length);
672
 
  uint32_t pack_length() const { return 1; }
673
 
  void sql_type(String &str) const;
674
 
  uint32_t max_display_length() { return 4; }
675
 
 
676
 
  virtual unsigned char *pack(unsigned char* to, const unsigned char *from,
677
 
                      uint32_t max_length __attribute__((unused)),
678
 
                      bool low_byte_first __attribute__((unused)))
679
 
  {
680
 
    *to= *from;
681
 
    return to + 1;
682
 
  }
683
 
 
684
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
685
 
                              uint32_t param_data __attribute__((unused)),
686
 
                              bool low_byte_first __attribute__((unused)))
687
 
  {
688
 
    *to= *from;
689
 
    return from + 1;
690
 
  }
691
 
};
692
 
 
693
608
/*
694
609
  Create field class for CREATE TABLE
695
610
*/
724
639
 
725
640
  uint8_t row,col,sc_length,interval_id;        // For rea_create_table
726
641
  uint32_t      offset,pack_flag;
 
642
 
 
643
  /* Virtual column expression statement */
 
644
  virtual_column_info *vcol_info;
 
645
  /*
 
646
    Indication that the field is phycically stored in tables 
 
647
    rather than just generated on SQL queries.
 
648
    As of now, FALSE can only be set for generated-only virtual columns.
 
649
  */
 
650
  bool is_stored;
 
651
 
727
652
  Create_field() :after(0) {}
728
653
  Create_field(Field *field, Field *orig_field);
729
654
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
742
667
                          uint32_t max_length, uint32_t decimals,
743
668
                          bool maybe_null, bool is_unsigned);
744
669
 
745
 
  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
 
670
  bool init(Session *session, char *field_name, enum_field_types type, char *length,
746
671
            char *decimals, uint32_t type_modifier, Item *default_value,
747
672
            Item *on_update_value, LEX_STRING *comment, char *change,
748
673
            List<String> *interval_list, const CHARSET_INFO * const cs,
749
674
            uint32_t uint_geom_type,
750
 
            enum column_format_type column_format);
 
675
            enum column_format_type column_format,
 
676
            virtual_column_info *vcol_info);
751
677
};
752
678
 
753
679
 
808
734
int set_field_to_null(Field *field);
809
735
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
810
736
 
 
737
 
811
738
bool
812
 
check_string_copy_error(Field_str *field,
813
 
                        const char *well_formed_error_pos,
814
 
                        const char *cannot_convert_error_pos,
815
 
                        const char *end,
816
 
                        const CHARSET_INFO * const cs);
 
739
test_if_important_data(const CHARSET_INFO * const cs, 
 
740
                       const char *str,
 
741
                       const char *strend);
817
742
 
818
743
/*
819
744
  Field subclasses
820
745
 */
 
746
#include <drizzled/field/str.h>
 
747
#include <drizzled/field/longstr.h>
 
748
#include <drizzled/field/num.h>
821
749
#include <drizzled/field/blob.h>
822
750
#include <drizzled/field/enum.h>
823
751
#include <drizzled/field/null.h>
824
752
#include <drizzled/field/date.h>
825
753
#include <drizzled/field/fdecimal.h>
 
754
#include <drizzled/field/real.h>
826
755
#include <drizzled/field/double.h>
827
756
#include <drizzled/field/long.h>
828
757
#include <drizzled/field/int64_t.h>
 
758
#include <drizzled/field/num.h>
829
759
#include <drizzled/field/timetype.h>
830
760
#include <drizzled/field/timestamp.h>
831
761
#include <drizzled/field/datetime.h>
842
772
#define FIELDFLAG_DECIMAL_POSITION      4
843
773
#define FIELDFLAG_PACK                  120     // Bits used for packing
844
774
#define FIELDFLAG_INTERVAL              256     // mangled with decimals!
845
 
#define FIELDFLAG_BITFIELD              512     // mangled with decimals!
846
775
#define FIELDFLAG_BLOB                  1024    // mangled with decimals!
847
 
#define FIELDFLAG_GEOM                  2048    // mangled with decimals!
848
 
 
849
 
#define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */
850
 
 
851
 
#define FIELDFLAG_LEFT_FULLSCREEN       8192
852
 
#define FIELDFLAG_RIGHT_FULLSCREEN      16384
853
 
#define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
 
776
 
854
777
#define FIELDFLAG_NO_DEFAULT            16384   /* sql */
855
778
#define FIELDFLAG_SUM                   ((uint32_t) 32768)// predit: +#fieldflag
856
779
#define FIELDFLAG_MAYBE_NULL            ((uint32_t) 32768)// sql
858
781
#define FIELDFLAG_PACK_SHIFT            3
859
782
#define FIELDFLAG_DEC_SHIFT             8
860
783
#define FIELDFLAG_MAX_DEC               31
861
 
#define FIELDFLAG_NUM_SCREEN_TYPE       0x7F01
862
 
#define FIELDFLAG_ALFA_SCREEN_TYPE      0x7800
863
784
 
864
785
#define MTYP_TYPENR(type) (type & 127)  /* Remove bits from type */
865
786
 
872
793
#define f_is_alpha(x)           (!f_is_num(x))
873
794
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
874
795
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
875
 
#define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
876
796
#define f_is_blob(x)            (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
877
797
#define f_is_equ(x)             ((x) & (1+2+FIELDFLAG_PACK+31*256))
878
 
#define f_settype(x)            (((int) x) << FIELDFLAG_PACK_SHIFT)
 
798
#define f_settype(x)   (((int) x) << FIELDFLAG_PACK_SHIFT)
879
799
#define f_maybe_null(x)         (x & FIELDFLAG_MAYBE_NULL)
880
800
#define f_no_default(x)         (x & FIELDFLAG_NO_DEFAULT)
881
 
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
882
801
#define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)
883
802
 
 
803
bool
 
804
check_string_copy_error(Field_str *field,
 
805
                        const char *well_formed_error_pos,
 
806
                        const char *cannot_convert_error_pos,
 
807
                        const char *end,
 
808
                        const CHARSET_INFO * const cs);
 
809
 
 
810
#endif /* DRIZZLED_FIELD_H */