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/charset.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   Analyzer function
00037     SYNOPSIS
00038       argp   in/out IN:  Analysis parameter
00039                     OUT: Parameter to be passed to the transformer
00040 
00041      RETURN
00042       true   Invoke the transformer
00043       false  Don't do it
00044 
00045 */
00046 typedef bool (Item::*Item_analyzer) (unsigned char **argp);
00047 typedef Item* (Item::*Item_transformer) (unsigned char *arg);
00048 typedef void (*Cond_traverser) (const Item *item, void *arg);
00049 typedef bool (Item::*Item_processor) (unsigned char *arg);
00050 
00056 class DRIZZLED_API Item : public memory::SqlAlloc, boost::noncopyable
00057 {
00058   /* Cache of the result of is_expensive(). */
00059   int8_t is_expensive_cache;
00060   virtual bool is_expensive_processor(unsigned char *arg);
00061 
00062 public:
00063 
00064   enum Type
00065   {
00066     FIELD_ITEM= 0,
00067     FUNC_ITEM,
00068     SUM_FUNC_ITEM,
00069     STRING_ITEM,
00070     INT_ITEM,
00071     REAL_ITEM,
00072     NULL_ITEM,
00073     VARBIN_ITEM,
00074     COPY_STR_ITEM,
00075     FIELD_AVG_ITEM,
00076     DEFAULT_VALUE_ITEM,
00077     PROC_ITEM,
00078     COND_ITEM,
00079     REF_ITEM,
00080     FIELD_STD_ITEM,
00081     FIELD_VARIANCE_ITEM,
00082     INSERT_VALUE_ITEM,
00083     SUBSELECT_ITEM,
00084     ROW_ITEM, CACHE_ITEM,
00085     TYPE_HOLDER,
00086     PARAM_ITEM,
00087     BOOLEAN_ITEM,
00088     DECIMAL_ITEM
00089   };
00090   enum traverse_order
00091   {
00092     T_POSTFIX,
00093     T_PREFIX
00094   };
00095   enum cond_result
00096   {
00097     COND_UNDEF,
00098     COND_OK,
00099     COND_TRUE,
00100     COND_FALSE
00101   };
00102 
00107   String str_value;
00108 
00110   const char *name;
00111 
00113   uint32_t name_length;
00114 
00115   Item *next;
00116   uint32_t max_length;
00117 
00118   int8_t marker;
00119   uint8_t decimals;
00120   bool fixed; 
00121   bool maybe_null; 
00122   bool null_value; 
00123   bool unsigned_flag;
00124 
00125   bool is_unsigned() const
00126   {
00127     return unsigned_flag;
00128   }
00129 
00130   virtual bool negative() const
00131   {
00132     return false;
00133   }
00134 
00135   bool with_sum_func;
00136   bool is_autogenerated_name; 
00142   bool with_subselect;
00143   DTCollation collation;
00144   Item_result cmp_context; 
00153   Item();
00154 
00165   Item(Session *session, Item *item);
00166 
00167   virtual ~Item()
00168   {
00169   }
00170 
00171   void set_name(str_ref arg)
00172   {
00173     set_name(arg.data(), arg.size());
00174   }
00175 
00176   void set_name(const char* arg)
00177   {
00178     set_name(str_ref(arg));
00179   }
00180 
00181   void set_name(const char *str, uint32_t length, const charset_info_st* cs= system_charset_info);
00182   void init_make_field(SendField *tmp_field, enum_field_types type);
00183   virtual void cleanup();
00184   virtual void make_field(SendField *field);
00194   Field *make_string_field(Table *table);
00195   virtual bool fix_fields(Session *, Item **);
00196 
00201   virtual void fix_after_pullout(Select_Lex *new_parent, Item **ref);
00202 
00207   inline void quick_fix_field()
00208   {
00209     fixed= true;
00210   }
00211 
00212   virtual int save_in_field(Field *field, bool no_conversions);
00213   virtual void save_org_in_field(Field *field)
00214   {
00215     (void) save_in_field(field, true);
00216   }
00217   virtual int save_safe_in_field(Field *field)
00218   {
00219     return save_in_field(field, true);
00220   }
00224   virtual void send(plugin::Client *client, String *str);
00235   virtual bool eq(const Item *, bool binary_cmp) const;
00236   virtual Item_result result_type() const
00237   {
00238     return REAL_RESULT;
00239   }
00240   virtual Item_result cast_to_int_type() const
00241   {
00242     return result_type();
00243   }
00244   virtual enum_field_types string_field_type() const;
00245   virtual enum_field_types field_type() const;
00246   virtual enum Type type() const =0;
00247 
00284   virtual int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
00285 
00286   /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
00295   virtual double val_real()=0;
00304   virtual int64_t val_int()=0;
00309   inline uint64_t val_uint()
00310   {
00311     return (uint64_t) val_int();
00312   }
00348   virtual String *val_str(String *str)=0;
00349 
00368   virtual type::Decimal *val_decimal(type::Decimal *decimal_buffer)= 0;
00369 
00378   virtual bool val_bool();
00379 
00380   /* Helper functions, see item_sum.cc */
00381   String *val_string_from_real(String *str);
00382   String *val_string_from_int(String *str);
00383   String *val_string_from_decimal(String *str);
00384   type::Decimal *val_decimal_from_real(type::Decimal *decimal_value);
00385   type::Decimal *val_decimal_from_int(type::Decimal *decimal_value);
00386   type::Decimal *val_decimal_from_string(type::Decimal *decimal_value);
00387   type::Decimal *val_decimal_from_date(type::Decimal *decimal_value);
00388   type::Decimal *val_decimal_from_time(type::Decimal *decimal_value);
00389   int64_t val_int_from_decimal();
00390   double val_real_from_decimal();
00391 
00392   bool save_time_in_field(Field *field);
00393   bool save_date_in_field(Field *field);
00394 
00412   int save_str_value_in_field(Field *field, String *result);
00413 
00414   virtual Field *get_tmp_table_field(void)
00415   {
00416     return NULL;
00417   }
00418   /* This is also used to create fields in CREATE ... SELECT: */
00419   virtual Field *tmp_table_field(Table *t_arg);
00420   virtual const char *full_name(void) const;
00421 
00422   /*
00423     *result* family of methods is analog of *val* family (see above) but
00424     return value of result_field of item if it is present. If Item have not
00425     result field, it return val(). This methods set null_value flag in same
00426     way as *val* methods do it.
00427   */
00428   virtual double  val_result() 
00429   {
00430     return val_real();
00431   }
00432   virtual int64_t val_int_result()
00433   {
00434     return val_int();
00435   }
00436   virtual String *str_result(String* tmp)
00437   {
00438     return val_str(tmp);
00439   }
00440   virtual type::Decimal *val_decimal_result(type::Decimal *val)
00441   {
00442     return val_decimal(val);
00443   }
00444   virtual bool val_bool_result()
00445   {
00446     return val_bool();
00447   }
00451   virtual table_map used_tables() const
00452   {
00453     return (table_map) 0L;
00454   }
00466   virtual table_map not_null_tables() const
00467   {
00468     return used_tables();
00469   }
00474   virtual bool basic_const_item() const
00475   {
00476     return false;
00477   }
00478   /* cloning of constant items (NULL if it is not const) */
00479   virtual Item *clone_item()
00480   {
00481     return NULL;
00482   }
00483   virtual cond_result eq_cmp_result() const
00484   {
00485     return COND_OK;
00486   }
00487   uint32_t float_length(uint32_t decimals_par) const;
00488   virtual uint32_t decimal_precision() const;
00489   int decimal_int_part() const;
00490 
00495   virtual bool const_item() const
00496   {
00497     return used_tables() == 0;
00498   }
00503   virtual bool const_during_execution() const
00504   {
00505     return (used_tables() & ~PARAM_TABLE_BIT) == 0;
00506   }
00507 
00519   virtual void print(String *str);
00520 
00521   void print_item_w_name(String *);
00522   virtual void update_used_tables() {}
00523   virtual void split_sum_func(Session *session, 
00524                               Item **ref_pointer_array,
00525                               List<Item> &fields);
00544   void split_sum_func(Session *session, 
00545                       Item **ref_pointer_array,
00546                       List<Item> &fields,
00547                       Item **ref, 
00548                       bool skip_registered);
00549 
00554   virtual bool get_date(type::Time &ltime, uint32_t fuzzydate);
00560   virtual bool get_time(type::Time &ltime);
00561   virtual bool get_date_result(type::Time &ltime,uint32_t fuzzydate);
00562 
00570   virtual bool is_null();
00571 
00573   virtual void update_null_value ();
00574 
00586   virtual void top_level_item(void);
00591   virtual void set_result_field(Field *field);
00592   virtual bool is_result_field(void);
00593   virtual bool is_bool_func(void);
00594   virtual void save_in_result_field(bool no_conversions);
00595 
00599   virtual void no_rows_in_result(void);
00600   virtual Item *copy_or_same(Session *session);
00601 
00602   virtual Item *copy_andor_structure(Session *session);
00603 
00604   virtual Item *real_item(void);
00605   virtual const Item *real_item(void) const;
00606   virtual Item *get_tmp_table_item(Session *session);
00607 
00608   static const charset_info_st *default_charset();
00609   virtual const charset_info_st *compare_collation();
00610 
00611   virtual bool walk(Item_processor processor,
00612                     bool walk_subquery,
00613                     unsigned char *arg);
00614 
00629   virtual Item* transform(Item_transformer transformer, unsigned char *arg);
00630 
00649   virtual Item* compile(Item_analyzer analyzer, 
00650                         unsigned char **arg_p,
00651                         Item_transformer transformer, 
00652                         unsigned char *arg_t);
00653 
00654   virtual void traverse_cond(Cond_traverser traverser,
00655                              void *arg,
00656                              traverse_order order);
00657 
00658   virtual bool remove_dependence_processor(unsigned char * arg);
00659   virtual bool collect_item_field_processor(unsigned char * arg);
00660   virtual bool find_item_in_field_list_processor(unsigned char *arg);
00661   virtual bool change_context_processor(unsigned char *context);
00662   virtual bool register_field_in_read_map(unsigned char *arg);
00663   virtual bool subst_argument_checker(unsigned char **arg);
00664 
00665   virtual bool cache_const_expr_analyzer(unsigned char **arg);
00666   virtual Item* cache_const_expr_transformer(unsigned char *arg);
00667 
00668   virtual Item *equal_fields_propagator(unsigned char * arg);
00669   virtual bool set_no_const_sub(unsigned char *arg);
00670   virtual Item *replace_equal_field(unsigned char * arg);
00671 
00672   // Row emulation
00673   virtual uint32_t cols();
00674   virtual Item* element_index(uint32_t i);
00675   virtual Item** addr(uint32_t i);
00676   virtual bool check_cols(uint32_t c);
00677   // It is not row => null inside is impossible
00678   virtual bool null_inside();
00679   // used in row subselects to get value of elements
00680   virtual void bring_value();
00681 
00693   Field *tmp_table_field_from_field_type(Table *table, bool fixed_length);
00694 
00695   virtual Item *neg_transformer(Session *session);
00696   virtual Item *update_value_transformer(unsigned char *select_arg);
00697   virtual Item *safe_charset_converter(const charset_info_st * const tocs);
00698   void delete_self();
00699 
00706   virtual bool result_as_int64_t();
00707   bool is_datetime();
00708 
00727   virtual bool is_expensive();
00728 
00729   String *check_well_formed_result(String *str, bool send_error= 0);
00750   bool eq_by_collation(Item *item, bool binary_cmp, const charset_info_st * const cs);
00751 
00752   inline uint32_t char_to_byte_length_safe(uint32_t char_length_arg, uint32_t mbmaxlen_arg)
00753   { 
00754     uint64_t tmp= ((uint64_t) char_length_arg) * mbmaxlen_arg;
00755     return (tmp > UINT32_MAX) ? (uint32_t) UINT32_MAX : (uint32_t) tmp;
00756   } 
00757 
00758   uint32_t max_char_length() const;
00759 
00760   void fix_char_length(uint32_t max_char_length_arg);
00761 
00762 protected:
00763   Session &getSession()
00764   {
00765     return _session;
00766   }
00767 
00768 private:
00769   Session &_session;
00770 };
00771 
00772 namespace display {
00773 const std::string &type(Item::Type type);
00774 } /* namespace display */
00775 
00776 std::ostream& operator<<(std::ostream& output, const Item &item);
00777 
00778 } /* namespace drizzled */
00779 
00781 #include <drizzled/item/ident.h>
00782 
00783 namespace drizzled
00784 {
00785 
00797 void mark_as_dependent(Session *session,
00798            Select_Lex *last,
00799                        Select_Lex *current,
00800                        Item_ident *resolved_item,
00801                        Item_ident *mark_item);
00802 
00838 Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select);
00839 
00859 void mark_select_range_as_dependent(Session *session,
00860                                     Select_Lex *last_select,
00861                                     Select_Lex *current_sel,
00862                                     Field *found_field, Item *found_item,
00863                                     Item_ident *resolved_item);
00864 
00865 extern void resolve_const_item(Session *session, Item **ref, Item *cmp_item);
00873 extern bool field_is_equal_to_item(Field *field,Item *item);
00874 
00908 Field *create_tmp_field(Session *session,
00909                         Table *table,
00910                         Item *item,
00911                         Item::Type type,
00912                         Item ***copy_func,
00913                         Field **from_field,
00914                         Field **def_field,
00915                         bool group,
00916                         bool modify_item,
00917                         bool make_copy_field,
00918                         uint32_t convert_blob_length);
00919 
00920 } /* namespace drizzled */
00921