Drizzled Public API Documentation

field.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 /*
00021   Because of the function new_field() all field classes that have static
00022   variables must declare the size_of() member function.
00023 */
00024 
00025 
00026 
00027 #pragma once
00028 
00029 #include <drizzled/sql_error.h>
00030 #include <drizzled/type/decimal.h>
00031 #include <drizzled/key_map.h>
00032 #include <drizzled/sql_list.h>
00033 #include <drizzled/structs.h>
00034 #include <drizzled/charset_info.h>
00035 #include <drizzled/item_result.h>
00036 #include <drizzled/charset_info.h>
00037 
00038 #include <string>
00039 #include <vector>
00040 
00041 #include <drizzled/visibility.h>
00042 
00043 namespace drizzled
00044 {
00045 
00046 #define DATETIME_DEC                     6
00047 #define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
00048 
00049 #ifdef DEBUG
00050 #define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
00051 #define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!getTable() || (getTable()->write_set == NULL || isWriteSet()))
00052 #else
00053 #define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
00054 #define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
00055 #endif
00056 
00057 typedef struct st_typelib TYPELIB;
00058 
00059 const uint32_t max_field_size= (uint32_t) 4294967295U;
00060 
00061 class SendField;
00062 class CreateField;
00063 class TableShare;
00064 class Field;
00065 struct CacheField;
00066 
00067 int field_conv(Field *to,Field *from);
00068 
00082 class DRIZZLED_API Field
00083 {
00084   /* Prevent use of these */
00085   Field(const Field&);
00086   void operator=(Field &);
00087 
00088 public:
00089   unsigned char *ptr; 
00090   unsigned char *null_ptr; 
00098 private:
00099   Table *table;
00100 
00101 public:
00102   Table *getTable()
00103   {
00104     assert(table);
00105     return table;
00106   }
00107 
00108   Table *getTable() const
00109   {
00110     assert(table);
00111     return table;
00112   }
00113 
00114   void setTable(Table *table_arg)
00115   {
00116     table= table_arg;
00117   }
00118 
00119   Table *orig_table; 
00120   const char *field_name; 
00121   LEX_STRING comment; 
00124   key_map key_start;
00125   key_map part_of_key;
00126   key_map part_of_key_not_clustered;
00127   key_map part_of_sortkey;
00128 
00129   /*
00130     We use three additional unireg types for TIMESTAMP for hysterical
00131     raisins and limitations in the MySQL FRM file format.
00132 
00133     A good TODO is to clean this up as we can support just about
00134     anything in the table proto message now.
00135   */
00136   enum utype
00137   {
00138     NONE,
00139     NEXT_NUMBER,
00140     TIMESTAMP_OLD_FIELD,
00141     TIMESTAMP_DN_FIELD,
00142     TIMESTAMP_UN_FIELD,
00143     TIMESTAMP_DNUN_FIELD
00144   };
00145 
00146   utype unireg_check;
00147   uint32_t field_length; 
00148   uint32_t flags;
00149 
00150   bool isUnsigned() const
00151   {
00152     return flags & UNSIGNED_FLAG;
00153   }
00154 
00155 private:
00156   uint16_t field_index; 
00158 public:
00159 
00160   uint16_t position() const
00161   {
00162     return field_index;
00163   }
00164 
00165   void setPosition(uint32_t arg)
00166   {
00167     field_index= arg;
00168   }
00169 
00170   unsigned char null_bit; 
00179   bool is_created_from_null_item;
00180 
00181   static void *operator new(size_t size);
00182   static void *operator new(size_t size, memory::Root *mem_root);
00183   static void operator delete(void *, size_t)
00184   { }
00185   static void operator delete(void *, memory::Root *)
00186   { }
00187 
00188   Field(unsigned char *ptr_arg,
00189         uint32_t length_arg,
00190         unsigned char *null_ptr_arg,
00191         unsigned char null_bit_arg,
00192         utype unireg_check_arg,
00193         const char *field_name_arg);
00194   virtual ~Field() {}
00195 
00196   bool hasDefault() const
00197   {
00198     return not (flags & NO_DEFAULT_VALUE_FLAG);
00199   }
00200 
00201   /* Store functions returns 1 on overflow and -1 on fatal error */
00202   virtual int store(const char *to,
00203                     uint32_t length,
00204                     const CHARSET_INFO * const cs)=0;
00205   virtual int store(double nr)=0;
00206   virtual int store(int64_t nr, bool unsigned_val)=0;
00207   virtual int store_decimal(const type::Decimal *d)=0;
00208   int store_and_check(enum_check_fields check_level,
00209                       const char *to,
00210                       uint32_t length,
00211                       const CHARSET_INFO * const cs);
00218   virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
00219   virtual double val_real() const=0;
00220   virtual int64_t val_int() const =0;
00221   virtual type::Decimal *val_decimal(type::Decimal *) const;
00222   String *val_str_internal(String *str) const
00223   {
00224     return val_str(str, str);
00225   }
00226 
00227   /*
00228      val_str(buf1, buf2) gets two buffers and should use them as follows:
00229      if it needs a temp buffer to convert result to string - use buf1
00230        example Field_tiny::val_str()
00231      if the value exists as a string already - use buf2
00232        example Field_varstring::val_str() (???)
00233      consequently, buf2 may be created as 'String buf;' - no memory
00234      will be allocated for it. buf1 will be allocated to hold a
00235      value if it's too small. Using allocated buffer for buf2 may result in
00236      an unnecessary free (and later, may be an alloc).
00237      This trickery is used to decrease a number of malloc calls.
00238   */
00239   virtual String *val_str(String*, String *) const =0;
00240 
00241   /*
00242    str_needs_quotes() returns true if the value returned by val_str() needs
00243    to be quoted when used in constructing an SQL query.
00244   */
00245   virtual bool str_needs_quotes() { return false; }
00246   virtual Item_result result_type () const=0;
00247   virtual Item_result cmp_type () const { return result_type(); }
00248   virtual Item_result cast_to_int_type () const { return result_type(); }
00249 
00263   static bool type_can_have_key_part(enum_field_types);
00273   static enum_field_types field_type_merge(enum_field_types, enum_field_types);
00274 
00283   static Item_result result_merge_type(enum_field_types);
00284 
00285   virtual bool eq(Field *field);
00294   virtual bool eq_def(Field *field);
00295 
00296   virtual bool is_timestamp() const
00297   {
00298     return false;
00299   }
00300 
00306   virtual uint32_t pack_length() const;
00307 
00313   virtual uint32_t pack_length_in_rec() const;
00314 
00319   virtual uint32_t data_length();
00325   virtual uint32_t used_length();
00326   virtual uint32_t sort_length() const;
00327 
00334   virtual uint32_t max_data_length() const;
00335   virtual int reset(void);
00336   virtual void reset_fields();
00337   virtual void set_default();
00338   virtual bool binary() const;
00339   virtual bool zero_pack() const;
00340   virtual enum ha_base_keytype key_type() const;
00341   virtual uint32_t key_length() const;
00342   virtual enum_field_types type() const =0;
00343   virtual enum_field_types real_type() const;
00344   virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
00345   virtual int cmp(const unsigned char *,const unsigned char *)=0;
00346   int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
00347   virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
00348                          uint32_t max_length=UINT32_MAX);
00349   virtual int cmp_offset(uint32_t row_offset);
00350   virtual int cmp_binary_offset(uint32_t row_offset);
00351   virtual int key_cmp(const unsigned char *a,const unsigned char *b);
00352   virtual int key_cmp(const unsigned char *str, uint32_t length);
00353   virtual uint32_t decimals() const;
00354 
00355   /*
00356     Caller beware: sql_type can change str.Ptr, so check
00357     ptr() to see if it changed if you are using your own buffer
00358     in str and restore it with set() if needed
00359   */
00360   virtual void sql_type(String &str) const =0;
00361 
00362   // For new field
00363   virtual uint32_t size_of() const =0;
00364 
00365   bool is_null(ptrdiff_t row_offset= 0) const;
00366   bool is_real_null(ptrdiff_t row_offset= 0) const;
00367   bool is_null_in_record(const unsigned char *record) const;
00368   bool is_null_in_record_with_offset(ptrdiff_t offset) const;
00369   void set_null(ptrdiff_t row_offset= 0);
00370   void set_notnull(ptrdiff_t row_offset= 0);
00371   bool maybe_null(void) const;
00372   bool real_maybe_null(void) const;
00373 
00374   virtual void make_field(SendField *);
00375   virtual void sort_string(unsigned char *buff,uint32_t length)=0;
00376   virtual bool optimize_range(uint32_t idx, uint32_t part);
00384   virtual bool can_be_compared_as_int64_t() const
00385   {
00386     return false;
00387   }
00388   virtual void free() {}
00389   virtual Field *new_field(memory::Root *root,
00390                            Table *new_table,
00391                            bool keep_type);
00392   virtual Field *new_key_field(memory::Root *root, Table *new_table,
00393                                unsigned char *new_ptr,
00394                                unsigned char *new_null_ptr,
00395                                uint32_t new_null_bit);
00397   Field *clone(memory::Root *mem_root, Table *new_table);
00398   void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
00399   {
00400     ptr= ptr_arg;
00401     null_ptr= null_ptr_arg;
00402     null_bit= null_bit_arg;
00403   }
00404   void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
00405   virtual void move_field_offset(ptrdiff_t ptr_diff)
00406   {
00407     ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
00408     if (null_ptr)
00409       null_ptr= ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
00410   }
00411   virtual void get_image(unsigned char *buff, uint32_t length, const CHARSET_INFO * const)
00412   {
00413     memcpy(buff,ptr,length);
00414   }
00415   virtual void get_image(std::basic_string<unsigned char> &buff, uint32_t length, const CHARSET_INFO * const)
00416   {
00417     buff.append(ptr,length);
00418   }
00419   virtual void set_image(const unsigned char *buff,uint32_t length, const CHARSET_INFO * const)
00420   {
00421     memcpy(ptr,buff,length);
00422   }
00423 
00448   virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
00449   {
00450     get_image(buff, length, &my_charset_bin);
00451     return length;
00452   }
00453   virtual uint32_t get_key_image(std::basic_string<unsigned char> &buff, uint32_t length)
00454   {
00455     get_image(buff, length, &my_charset_bin);
00456     return length;
00457   }
00458   virtual void set_key_image(const unsigned char *buff,uint32_t length)
00459   {
00460     set_image(buff,length, &my_charset_bin);
00461   }
00462   int64_t val_int_offset(uint32_t row_offset)
00463   {
00464     ptr+=row_offset;
00465     int64_t tmp=val_int();
00466     ptr-=row_offset;
00467     return tmp;
00468   }
00469 
00470   int64_t val_int_internal(const unsigned char *new_ptr)
00471   {
00472     unsigned char *old_ptr= ptr;
00473     ptr= const_cast<unsigned char*>(new_ptr);
00474     int64_t return_value= val_int();
00475     ptr= old_ptr;
00476     return return_value;
00477   }
00478 
00479   String *val_str_internal(String *str, const unsigned char *new_ptr)
00480   {
00481     unsigned char *old_ptr= ptr;
00482     ptr= const_cast<unsigned char*>(new_ptr);
00483     val_str_internal(str);
00484     ptr= old_ptr;
00485     return str;
00486   }
00487 
00525   virtual unsigned char *pack(unsigned char *to,
00526                               const unsigned char *from,
00527                               uint32_t max_length,
00528                               bool low_byte_first);
00529 
00530   unsigned char *pack(unsigned char *to, const unsigned char *from);
00531 
00562   virtual const unsigned char *unpack(unsigned char* to,
00563                                       const unsigned char *from,
00564                                       uint32_t param_data,
00565                                       bool low_byte_first);
00570   const unsigned char *unpack(unsigned char* to,
00571                               const unsigned char *from);
00572 
00573   virtual unsigned char *pack_key(unsigned char* to,
00574                                   const unsigned char *from,
00575                                   uint32_t max_length,
00576                                   bool low_byte_first)
00577   {
00578     return pack(to, from, max_length, low_byte_first);
00579   }
00580   virtual const unsigned char *unpack_key(unsigned char* to,
00581                                           const unsigned char *from,
00582                                           uint32_t max_length,
00583                                           bool low_byte_first)
00584   {
00585     return unpack(to, from, max_length, low_byte_first);
00586   }
00587   virtual uint32_t max_packed_col_length(uint32_t max_length)
00588   {
00589     return max_length;
00590   }
00591 
00592   uint32_t offset(const unsigned char *record)
00593   {
00594     return (uint32_t) (ptr - record);
00595   }
00596   void copy_from_tmp(int offset);
00597   uint32_t fill_cache_field(CacheField *copy);
00598   virtual bool get_date(type::Time &ltime,uint32_t fuzzydate) const;
00599   virtual bool get_time(type::Time &ltime) const;
00600   virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
00601   virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
00602   virtual bool has_charset(void) const { return false; }
00603   virtual void set_charset(const CHARSET_INFO * const)
00604   {}
00605   virtual enum Derivation derivation(void) const
00606   {
00607     return DERIVATION_IMPLICIT;
00608   }
00609   virtual void set_derivation(enum Derivation)
00610   {}
00630   bool set_warning(DRIZZLE_ERROR::enum_warning_level,
00631                    drizzled::error_t code,
00632                    int cuted_increment);
00648   void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
00649                             drizzled::error_t code,
00650                             const char *str,
00651                             uint32_t str_len,
00652                             type::timestamp_t ts_type,
00653                             int cuted_increment);
00668   void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
00669                             drizzled::error_t code,
00670                             int64_t nr,
00671                             type::timestamp_t ts_type,
00672                             int cuted_increment);
00686   void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
00687                             const drizzled::error_t code,
00688                             double nr,
00689                             type::timestamp_t ts_type);
00690   bool check_overflow(int op_result)
00691   {
00692     return (op_result == E_DEC_OVERFLOW);
00693   }
00706   int warn_if_overflow(int op_result);
00707   void init(Table *table_arg);
00708 
00709   /* maximum possible display length */
00710   virtual uint32_t max_display_length()= 0;
00711 
00712   virtual uint32_t is_equal(CreateField *new_field);
00724   int64_t convert_decimal2int64_t(const type::Decimal *val,
00725                                   bool unsigned_flag,
00726                                   int *err);
00727   /* The max. number of characters */
00728   uint32_t char_length() const
00729   {
00730     return field_length / charset()->mbmaxlen;
00731   }
00732 
00733   enum column_format_type column_format() const
00734   {
00735     return (enum column_format_type)
00736       ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
00737   }
00738 
00739   /* Hash value */
00740   virtual void hash(uint32_t *nr, uint32_t *nr2) const;
00741   friend bool reopen_table(Session *,Table *,bool);
00742 
00743   friend class CopyField;
00744   friend class Item_avg_field;
00745   friend class Item_std_field;
00746   friend class Item_sum_num;
00747   friend class Item_sum_sum;
00748   friend class Item_sum_str;
00749   friend class Item_sum_count;
00750   friend class Item_sum_avg;
00751   friend class Item_sum_std;
00752   friend class Item_sum_min;
00753   friend class Item_sum_max;
00754   friend class Item_func_group_concat;
00755 
00756   bool isReadSet() const;
00757   bool isWriteSet();
00758   void setReadSet(bool arg= true);
00759   void setWriteSet(bool arg= true);
00760 
00761 protected:
00762 
00763   void pack_num(uint64_t arg, unsigned char *destination= NULL);
00764   void pack_num(uint32_t arg, unsigned char *destination= NULL);
00765   uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
00766   uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
00767 };
00768 
00769 namespace field {
00770 
00771 inline bool isDateTime(const enum_field_types &arg)
00772 {
00773   switch (arg)
00774   {
00775   case DRIZZLE_TYPE_DATE:
00776   case DRIZZLE_TYPE_DATETIME:
00777   case DRIZZLE_TYPE_MICROTIME:
00778   case DRIZZLE_TYPE_TIME:
00779   case DRIZZLE_TYPE_TIMESTAMP:
00780     return true;
00781 
00782   case DRIZZLE_TYPE_BLOB:
00783   case DRIZZLE_TYPE_BOOLEAN:
00784   case DRIZZLE_TYPE_DECIMAL:
00785   case DRIZZLE_TYPE_DOUBLE:
00786   case DRIZZLE_TYPE_ENUM:
00787   case DRIZZLE_TYPE_LONG:
00788   case DRIZZLE_TYPE_LONGLONG:
00789   case DRIZZLE_TYPE_NULL:
00790   case DRIZZLE_TYPE_UUID:
00791   case DRIZZLE_TYPE_VARCHAR:
00792     return false;
00793   }
00794 
00795   assert(0);
00796   abort();
00797 }
00798 
00799 } // namespace field
00800 
00801 std::ostream& operator<<(std::ostream& output, const Field &field);
00802 
00803 } /* namespace drizzled */
00804 
00806 #include <drizzled/create_field.h>
00807 
00808 namespace drizzled
00809 {
00810 
00819 class SendField
00820 {
00821 public:
00822   const char *db_name;
00823   const char *table_name;
00824   const char *org_table_name;
00825   const char *col_name;
00826   const char *org_col_name;
00827   uint32_t length;
00828   uint32_t charsetnr;
00829   uint32_t flags;
00830   uint32_t decimals;
00831   enum_field_types type;
00832   SendField() {}
00833 };
00834 
00835 uint32_t pack_length_to_packflag(uint32_t type);
00836 uint32_t calc_pack_length(enum_field_types type,uint32_t length);
00837 int set_field_to_null(Field *field);
00838 int set_field_to_null_with_conversions(Field *field, bool no_conversions);
00839 
00853 bool test_if_important_data(const CHARSET_INFO * const cs,
00854                             const char *str,
00855                             const char *strend);
00856 
00857 } /* namespace drizzled */
00858