Drizzled Public API Documentation

item.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 
00022 #pragma once
00023 
00024 #include <drizzled/dtcollation.h>
00025 #include <drizzled/global_charset_info.h>
00026 #include <drizzled/item_result.h>
00027 #include <drizzled/memory/sql_alloc.h>
00028 #include <drizzled/sql_list.h>
00029 #include <drizzled/sql_string.h>
00030 
00031 #include <drizzled/visibility.h>
00032 
00033 namespace drizzled
00034 {
00035 
00036 class Field;
00037 class Item_equal;
00038 class Item_field;
00039 class Item_ident;
00040 class Item_in_subselect;
00041 class Item_sum;
00042 class Select_Lex;
00043 class SendField;
00044 class Table;
00045 class user_var_entry;
00046 
00047 namespace plugin { class Client; }
00048 namespace type { class Time; }
00049 namespace type { class Decimal; }
00050 
00057 void dummy_error_processor(Session *session, void *data);
00058 
00059 /*
00060   Analyzer function
00061     SYNOPSIS
00062       argp   in/out IN:  Analysis parameter
00063                     OUT: Parameter to be passed to the transformer
00064 
00065      RETURN
00066       true   Invoke the transformer
00067       false  Don't do it
00068 
00069 */
00070 typedef bool (Item::*Item_analyzer) (unsigned char **argp);
00071 typedef Item* (Item::*Item_transformer) (unsigned char *arg);
00072 typedef void (*Cond_traverser) (const Item *item, void *arg);
00073 typedef bool (Item::*Item_processor) (unsigned char *arg);
00074 
00080 class DRIZZLED_API Item : public memory::SqlAlloc
00081 {
00082   /* Prevent use of these */
00083   Item(const Item &);
00084   void operator=(Item &);
00085 
00086   /* Cache of the result of is_expensive(). */
00087   int8_t is_expensive_cache;
00088   virtual bool is_expensive_processor(unsigned char *arg);
00089 
00090 public:
00091 
00092   enum Type
00093   {
00094     FIELD_ITEM= 0,
00095     FUNC_ITEM,
00096     SUM_FUNC_ITEM,
00097     STRING_ITEM,
00098     INT_ITEM,
00099     REAL_ITEM,
00100     NULL_ITEM,
00101     VARBIN_ITEM,
00102     COPY_STR_ITEM,
00103     FIELD_AVG_ITEM,
00104     DEFAULT_VALUE_ITEM,
00105     PROC_ITEM,
00106     COND_ITEM,
00107     REF_ITEM,
00108     FIELD_STD_ITEM,
00109     FIELD_VARIANCE_ITEM,
00110     INSERT_VALUE_ITEM,
00111     SUBSELECT_ITEM,
00112     ROW_ITEM, CACHE_ITEM,
00113     TYPE_HOLDER,
00114     PARAM_ITEM,
00115     BOOLEAN_ITEM,
00116     DECIMAL_ITEM
00117   };
00118   enum traverse_order
00119   {
00120     T_POSTFIX,
00121     T_PREFIX
00122   };
00123   enum cond_result
00124   {
00125     COND_UNDEF,
00126     COND_OK,
00127     COND_TRUE,
00128     COND_FALSE
00129   };
00130 
00135   String str_value;
00136 
00138   char *name;
00139 
00141   uint32_t name_length;
00142 
00144   char *orig_name;
00145   Item *next;
00146   uint32_t max_length;
00147 
00148   int8_t marker;
00149   uint8_t decimals;
00150   bool fixed; 
00151   bool maybe_null; 
00152   bool null_value; 
00153   bool unsigned_flag;
00154 
00155   bool is_unsigned() const
00156   {
00157     return unsigned_flag;
00158   }
00159 
00160   virtual bool negative() const
00161   {
00162     return false;
00163   }
00164 
00165   bool with_sum_func;
00166   bool is_autogenerated_name; 
00172   bool with_subselect;
00173   DTCollation collation;
00174   Item_result cmp_context; 
00183   Item();
00184 
00195   Item(Session *session, Item *item);
00196 
00197   virtual ~Item()
00198   {
00199 #ifdef EXTRA_DEBUG
00200     name= NULL;
00201 #endif
00202   }
00203 
00204   void set_name(const std::string &arg)
00205   {
00206     set_name(arg.c_str(), arg.length(), system_charset_info);
00207   }
00208 
00209   void set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs= system_charset_info);
00215   void rename(char *new_name);
00216   void init_make_field(SendField *tmp_field,enum enum_field_types type);
00217   virtual void cleanup();
00218   virtual void make_field(SendField *field);
00228   Field *make_string_field(Table *table);
00229   virtual bool fix_fields(Session *, Item **);
00230 
00235   virtual void fix_after_pullout(Select_Lex *new_parent, Item **ref);
00236 
00241   inline void quick_fix_field()
00242   {
00243     fixed= true;
00244   }
00245 
00246   virtual int save_in_field(Field *field, bool no_conversions);
00247   virtual void save_org_in_field(Field *field)
00248   {
00249     (void) save_in_field(field, true);
00250   }
00251   virtual int save_safe_in_field(Field *field)
00252   {
00253     return save_in_field(field, true);
00254   }
00258   virtual bool send(plugin::Client *client, String *str);
00269   virtual bool eq(const Item *, bool binary_cmp) const;
00270   virtual Item_result result_type() const
00271   {
00272     return REAL_RESULT;
00273   }
00274   virtual Item_result cast_to_int_type() const
00275   {
00276     return result_type();
00277   }
00278   virtual enum_field_types string_field_type() const;
00279   virtual enum_field_types field_type() const;
00280   virtual enum Type type() const =0;
00281 
00318   virtual int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
00319 
00320   /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
00329   virtual double val_real()=0;
00338   virtual int64_t val_int()=0;
00343   inline uint64_t val_uint()
00344   {
00345     return (uint64_t) val_int();
00346   }
00382   virtual String *val_str(String *str)=0;
00383 
00402   virtual type::Decimal *val_decimal(type::Decimal *decimal_buffer)= 0;
00403 
00412   virtual bool val_bool();
00413 
00414   /* Helper functions, see item_sum.cc */
00415   String *val_string_from_real(String *str);
00416   String *val_string_from_int(String *str);
00417   String *val_string_from_decimal(String *str);
00418   type::Decimal *val_decimal_from_real(type::Decimal *decimal_value);
00419   type::Decimal *val_decimal_from_int(type::Decimal *decimal_value);
00420   type::Decimal *val_decimal_from_string(type::Decimal *decimal_value);
00421   type::Decimal *val_decimal_from_date(type::Decimal *decimal_value);
00422   type::Decimal *val_decimal_from_time(type::Decimal *decimal_value);
00423   int64_t val_int_from_decimal();
00424   double val_real_from_decimal();
00425 
00426   bool save_time_in_field(Field *field);
00427   bool save_date_in_field(Field *field);
00428 
00446   int save_str_value_in_field(Field *field, String *result);
00447 
00448   virtual Field *get_tmp_table_field(void)
00449   {
00450     return NULL;
00451   }
00452   /* This is also used to create fields in CREATE ... SELECT: */
00453   virtual Field *tmp_table_field(Table *t_arg);
00454   virtual const char *full_name(void) const;
00455 
00456   /*
00457     *result* family of methods is analog of *val* family (see above) but
00458     return value of result_field of item if it is present. If Item have not
00459     result field, it return val(). This methods set null_value flag in same
00460     way as *val* methods do it.
00461   */
00462   virtual double  val_result() 
00463   {
00464     return val_real();
00465   }
00466   virtual int64_t val_int_result()
00467   {
00468     return val_int();
00469   }
00470   virtual String *str_result(String* tmp)
00471   {
00472     return val_str(tmp);
00473   }
00474   virtual type::Decimal *val_decimal_result(type::Decimal *val)
00475   {
00476     return val_decimal(val);
00477   }
00478   virtual bool val_bool_result()
00479   {
00480     return val_bool();
00481   }
00485   virtual table_map used_tables() const
00486   {
00487     return (table_map) 0L;
00488   }
00500   virtual table_map not_null_tables() const
00501   {
00502     return used_tables();
00503   }
00508   virtual bool basic_const_item() const
00509   {
00510     return false;
00511   }
00512   /* cloning of constant items (NULL if it is not const) */
00513   virtual Item *clone_item()
00514   {
00515     return NULL;
00516   }
00517   virtual cond_result eq_cmp_result() const
00518   {
00519     return COND_OK;
00520   }
00521   uint32_t float_length(uint32_t decimals_par) const;
00522   virtual uint32_t decimal_precision() const;
00523   int decimal_int_part() const;
00524 
00529   virtual bool const_item() const
00530   {
00531     return used_tables() == 0;
00532   }
00537   virtual bool const_during_execution() const
00538   {
00539     return (used_tables() & ~PARAM_TABLE_BIT) == 0;
00540   }
00541 
00553   virtual void print(String *str);
00554 
00555   void print_item_w_name(String *);
00556   virtual void update_used_tables() {}
00557   virtual void split_sum_func(Session *session, 
00558                               Item **ref_pointer_array,
00559                               List<Item> &fields);
00578   void split_sum_func(Session *session, 
00579                       Item **ref_pointer_array,
00580                       List<Item> &fields,
00581                       Item **ref, 
00582                       bool skip_registered);
00583 
00588   virtual bool get_date(type::Time &ltime, uint32_t fuzzydate);
00594   virtual bool get_time(type::Time &ltime);
00595   virtual bool get_date_result(type::Time &ltime,uint32_t fuzzydate);
00596 
00604   virtual bool is_null();
00605 
00607   virtual void update_null_value ();
00608 
00620   virtual void top_level_item(void);
00625   virtual void set_result_field(Field *field);
00626   virtual bool is_result_field(void);
00627   virtual bool is_bool_func(void);
00628   virtual void save_in_result_field(bool no_conversions);
00629 
00633   virtual void no_rows_in_result(void);
00634   virtual Item *copy_or_same(Session *session);
00635 
00636   virtual Item *copy_andor_structure(Session *session);
00637 
00638   virtual Item *real_item(void);
00639   virtual const Item *real_item(void) const;
00640   virtual Item *get_tmp_table_item(Session *session);
00641 
00642   static const CHARSET_INFO *default_charset();
00643   virtual const CHARSET_INFO *compare_collation();
00644 
00645   virtual bool walk(Item_processor processor,
00646                     bool walk_subquery,
00647                     unsigned char *arg);
00648 
00663   virtual Item* transform(Item_transformer transformer, unsigned char *arg);
00664 
00683   virtual Item* compile(Item_analyzer analyzer, 
00684                         unsigned char **arg_p,
00685                         Item_transformer transformer, 
00686                         unsigned char *arg_t);
00687 
00688   virtual void traverse_cond(Cond_traverser traverser,
00689                              void *arg,
00690                              traverse_order order);
00691 
00692   virtual bool remove_dependence_processor(unsigned char * arg);
00693   virtual bool remove_fixed(unsigned char * arg);
00694   virtual bool collect_item_field_processor(unsigned char * arg);
00695   virtual bool find_item_in_field_list_processor(unsigned char *arg);
00696   virtual bool change_context_processor(unsigned char *context);
00697   virtual bool register_field_in_read_map(unsigned char *arg);
00698   virtual bool subst_argument_checker(unsigned char **arg);
00699 
00700   virtual bool cache_const_expr_analyzer(unsigned char **arg);
00701   virtual Item* cache_const_expr_transformer(unsigned char *arg);
00702 
00703   virtual Item *equal_fields_propagator(unsigned char * arg);
00704   virtual bool set_no_const_sub(unsigned char *arg);
00705   virtual Item *replace_equal_field(unsigned char * arg);
00706 
00707   // Row emulation
00708   virtual uint32_t cols();
00709   virtual Item* element_index(uint32_t i);
00710   virtual Item** addr(uint32_t i);
00711   virtual bool check_cols(uint32_t c);
00712   // It is not row => null inside is impossible
00713   virtual bool null_inside();
00714   // used in row subselects to get value of elements
00715   virtual void bring_value();
00716 
00728   Field *tmp_table_field_from_field_type(Table *table, bool fixed_length);
00729 
00730   virtual Item *neg_transformer(Session *session);
00731   virtual Item *update_value_transformer(unsigned char *select_arg);
00732   virtual Item *safe_charset_converter(const CHARSET_INFO * const tocs);
00733   void delete_self();
00734 
00741   virtual bool result_as_int64_t();
00742   bool is_datetime();
00743 
00762   virtual bool is_expensive();
00763 
00764   String *check_well_formed_result(String *str, bool send_error= 0);
00785   bool eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs);
00786 
00787   inline uint32_t char_to_byte_length_safe(uint32_t char_length_arg, uint32_t mbmaxlen_arg)
00788   { 
00789     uint64_t tmp= ((uint64_t) char_length_arg) * mbmaxlen_arg;
00790     return (tmp > UINT32_MAX) ? (uint32_t) UINT32_MAX : (uint32_t) tmp;
00791   } 
00792 
00793   uint32_t max_char_length() const;
00794 
00795   void fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs);
00796   void fix_char_length(uint32_t max_char_length_arg);
00797   void fix_char_length_uint64_t(uint64_t max_char_length_arg);
00798   void fix_length_and_charset_datetime(uint32_t max_char_length_arg);
00799 
00800 protected:
00801   Session &getSession()
00802   {
00803     return _session;
00804   }
00805 
00806 private:
00807   Session &_session;
00808 };
00809 
00810 namespace display {
00811 const std::string &type(Item::Type type);
00812 } /* namespace display */
00813 
00814 std::ostream& operator<<(std::ostream& output, const Item &item);
00815 
00816 } /* namespace drizzled */
00817 
00819 #include <drizzled/item/ident.h>
00820 
00821 namespace drizzled
00822 {
00823 
00835 void mark_as_dependent(Session *session,
00836            Select_Lex *last,
00837                        Select_Lex *current,
00838                        Item_ident *resolved_item,
00839                        Item_ident *mark_item);
00840 
00876 Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select);
00877 
00897 void mark_select_range_as_dependent(Session *session,
00898                                     Select_Lex *last_select,
00899                                     Select_Lex *current_sel,
00900                                     Field *found_field, Item *found_item,
00901                                     Item_ident *resolved_item);
00902 
00903 extern void resolve_const_item(Session *session, Item **ref, Item *cmp_item);
00911 extern bool field_is_equal_to_item(Field *field,Item *item);
00912 
00946 Field *create_tmp_field(Session *session,
00947                         Table *table,
00948                         Item *item,
00949                         Item::Type type,
00950                         Item ***copy_func,
00951                         Field **from_field,
00952                         Field **def_field,
00953                         bool group,
00954                         bool modify_item,
00955                         bool make_copy_field,
00956                         uint32_t convert_blob_length);
00957 
00958 } /* namespace drizzled */
00959