Drizzled Public API Documentation

item.h
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 
21 
22 #pragma once
23 
24 #include <drizzled/dtcollation.h>
25 #include <drizzled/charset.h>
26 #include <drizzled/item_result.h>
27 #include <drizzled/memory/sql_alloc.h>
28 #include <drizzled/sql_list.h>
29 #include <drizzled/sql_string.h>
30 
31 #include <drizzled/visibility.h>
32 
33 namespace drizzled {
34 
35 /*
36  Analyzer function
37  SYNOPSIS
38  argp in/out IN: Analysis parameter
39  OUT: Parameter to be passed to the transformer
40 
41  RETURN
42  true Invoke the transformer
43  false Don't do it
44 
45 */
46 typedef bool (Item::*Item_analyzer) (unsigned char **argp);
47 typedef Item* (Item::*Item_transformer) (unsigned char *arg);
48 typedef void (*Cond_traverser) (const Item *item, void *arg);
49 typedef bool (Item::*Item_processor) (unsigned char *arg);
50 
56 class DRIZZLED_API Item : public memory::SqlAlloc, boost::noncopyable
57 {
58  /* Cache of the result of is_expensive(). */
59  int8_t is_expensive_cache;
60  virtual bool is_expensive_processor(unsigned char *arg);
61 
62 public:
63 
64  enum Type
65  {
66  FIELD_ITEM= 0,
67  FUNC_ITEM,
68  SUM_FUNC_ITEM,
69  STRING_ITEM,
70  INT_ITEM,
71  REAL_ITEM,
72  NULL_ITEM,
73  VARBIN_ITEM,
74  COPY_STR_ITEM,
75  FIELD_AVG_ITEM,
76  DEFAULT_VALUE_ITEM,
77  PROC_ITEM,
78  COND_ITEM,
79  REF_ITEM,
80  FIELD_STD_ITEM,
81  FIELD_VARIANCE_ITEM,
82  INSERT_VALUE_ITEM,
83  SUBSELECT_ITEM,
84  ROW_ITEM, CACHE_ITEM,
85  TYPE_HOLDER,
86  PARAM_ITEM,
87  BOOLEAN_ITEM,
88  DECIMAL_ITEM
89  };
90  enum traverse_order
91  {
92  T_POSTFIX,
93  T_PREFIX
94  };
95  enum cond_result
96  {
97  COND_UNDEF,
98  COND_OK,
99  COND_TRUE,
100  COND_FALSE
101  };
102 
108 
110  const char *name;
111 
113  uint32_t name_length;
114 
115  Item *next;
116  uint32_t max_length;
117 
118  int8_t marker;
119  uint8_t decimals;
120  bool fixed;
121  bool maybe_null;
122  bool null_value;
123  bool unsigned_flag;
124 
125  bool is_unsigned() const
126  {
127  return unsigned_flag;
128  }
129 
130  virtual bool negative() const
131  {
132  return false;
133  }
134 
135  bool with_sum_func;
143  DTCollation collation;
144  Item_result cmp_context;
153  Item();
154 
165  Item(Session *session, Item *item);
166 
167  virtual ~Item()
168  {
169  }
170 
171  void set_name(str_ref arg)
172  {
173  set_name(arg.data(), arg.size());
174  }
175 
176  void set_name(const char* arg)
177  {
178  set_name(str_ref(arg));
179  }
180 
181  void set_name(const char *str, uint32_t length, const charset_info_st* cs= system_charset_info);
182  void init_make_field(SendField *tmp_field, enum_field_types type);
183  virtual void cleanup();
184  virtual void make_field(SendField *field);
194  Field *make_string_field(Table *table);
195  virtual bool fix_fields(Session *, Item **);
196 
201  virtual void fix_after_pullout(Select_Lex *new_parent, Item **ref);
202 
207  inline void quick_fix_field()
208  {
209  fixed= true;
210  }
211 
212  virtual int save_in_field(Field *field, bool no_conversions);
213  virtual void save_org_in_field(Field *field)
214  {
215  (void) save_in_field(field, true);
216  }
217  virtual int save_safe_in_field(Field *field)
218  {
219  return save_in_field(field, true);
220  }
224  virtual void send(plugin::Client *client, String *str);
235  virtual bool eq(const Item *, bool binary_cmp) const;
236  virtual Item_result result_type() const
237  {
238  return REAL_RESULT;
239  }
240  virtual Item_result cast_to_int_type() const
241  {
242  return result_type();
243  }
244  virtual enum_field_types string_field_type() const;
245  virtual enum_field_types field_type() const;
246  virtual enum Type type() const =0;
247 
284  virtual int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
285 
286  /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
295  virtual double val_real()=0;
304  virtual int64_t val_int()=0;
309  inline uint64_t val_uint()
310  {
311  return (uint64_t) val_int();
312  }
348  virtual String *val_str(String *str)=0;
349 
368  virtual type::Decimal *val_decimal(type::Decimal *decimal_buffer)= 0;
369 
378  virtual bool val_bool();
379 
380  /* Helper functions, see item_sum.cc */
381  String *val_string_from_real(String *str);
382  String *val_string_from_int(String *str);
383  String *val_string_from_decimal(String *str);
384  type::Decimal *val_decimal_from_real(type::Decimal *decimal_value);
385  type::Decimal *val_decimal_from_int(type::Decimal *decimal_value);
386  type::Decimal *val_decimal_from_string(type::Decimal *decimal_value);
387  type::Decimal *val_decimal_from_date(type::Decimal *decimal_value);
388  type::Decimal *val_decimal_from_time(type::Decimal *decimal_value);
389  int64_t val_int_from_decimal();
390  double val_real_from_decimal();
391 
392  bool save_time_in_field(Field *field);
393  bool save_date_in_field(Field *field);
394 
412  int save_str_value_in_field(Field *field, String *result);
413 
414  virtual Field *get_tmp_table_field(void)
415  {
416  return NULL;
417  }
418  /* This is also used to create fields in CREATE ... SELECT: */
419  virtual Field *tmp_table_field(Table *t_arg);
420  virtual const char *full_name(void) const;
421 
422  /*
423  *result* family of methods is analog of *val* family (see above) but
424  return value of result_field of item if it is present. If Item have not
425  result field, it return val(). This methods set null_value flag in same
426  way as *val* methods do it.
427  */
428  virtual double val_result()
429  {
430  return val_real();
431  }
432  virtual int64_t val_int_result()
433  {
434  return val_int();
435  }
436  virtual String *str_result(String* tmp)
437  {
438  return val_str(tmp);
439  }
440  virtual type::Decimal *val_decimal_result(type::Decimal *val)
441  {
442  return val_decimal(val);
443  }
444  virtual bool val_bool_result()
445  {
446  return val_bool();
447  }
451  virtual table_map used_tables() const
452  {
453  return (table_map) 0L;
454  }
466  virtual table_map not_null_tables() const
467  {
468  return used_tables();
469  }
474  virtual bool basic_const_item() const
475  {
476  return false;
477  }
478  /* cloning of constant items (NULL if it is not const) */
479  virtual Item *clone_item()
480  {
481  return NULL;
482  }
483  virtual cond_result eq_cmp_result() const
484  {
485  return COND_OK;
486  }
487  uint32_t float_length(uint32_t decimals_par) const;
488  virtual uint32_t decimal_precision() const;
489  int decimal_int_part() const;
490 
495  virtual bool const_item() const
496  {
497  return used_tables() == 0;
498  }
503  virtual bool const_during_execution() const
504  {
505  return (used_tables() & ~PARAM_TABLE_BIT) == 0;
506  }
507 
519  virtual void print(String *str);
520 
521  void print_item_w_name(String *);
522  virtual void update_used_tables() {}
523  virtual void split_sum_func(Session *session,
524  Item **ref_pointer_array,
525  List<Item> &fields);
544  void split_sum_func(Session *session,
545  Item **ref_pointer_array,
546  List<Item> &fields,
547  Item **ref,
548  bool skip_registered);
549 
554  virtual bool get_date(type::Time &ltime, uint32_t fuzzydate);
560  virtual bool get_time(type::Time &ltime);
561  virtual bool get_date_result(type::Time &ltime,uint32_t fuzzydate);
562 
570  virtual bool is_null();
571 
573  virtual void update_null_value ();
574 
586  virtual void top_level_item(void);
591  virtual void set_result_field(Field *field);
592  virtual bool is_result_field(void);
593  virtual bool is_bool_func(void);
594  virtual void save_in_result_field(bool no_conversions);
595 
599  virtual void no_rows_in_result(void);
600  virtual Item *copy_or_same(Session *session);
601 
602  virtual Item *copy_andor_structure(Session *session);
603 
604  virtual Item *real_item(void);
605  virtual const Item *real_item(void) const;
606  virtual Item *get_tmp_table_item(Session *session);
607 
608  static const charset_info_st *default_charset();
609  virtual const charset_info_st *compare_collation();
610 
611  virtual bool walk(Item_processor processor,
612  bool walk_subquery,
613  unsigned char *arg);
614 
629  virtual Item* transform(Item_transformer transformer, unsigned char *arg);
630 
649  virtual Item* compile(Item_analyzer analyzer,
650  unsigned char **arg_p,
651  Item_transformer transformer,
652  unsigned char *arg_t);
653 
654  virtual void traverse_cond(Cond_traverser traverser,
655  void *arg,
656  traverse_order order);
657 
658  virtual bool remove_dependence_processor(unsigned char * arg);
659  virtual bool collect_item_field_processor(unsigned char * arg);
660  virtual bool find_item_in_field_list_processor(unsigned char *arg);
661  virtual bool change_context_processor(unsigned char *context);
662  virtual bool register_field_in_read_map(unsigned char *arg);
663  virtual bool subst_argument_checker(unsigned char **arg);
664 
665  virtual bool cache_const_expr_analyzer(unsigned char **arg);
666  virtual Item* cache_const_expr_transformer(unsigned char *arg);
667 
668  virtual Item *equal_fields_propagator(unsigned char * arg);
669  virtual bool set_no_const_sub(unsigned char *arg);
670  virtual Item *replace_equal_field(unsigned char * arg);
671 
672  // Row emulation
673  virtual uint32_t cols();
674  virtual Item* element_index(uint32_t i);
675  virtual Item** addr(uint32_t i);
676  virtual bool check_cols(uint32_t c);
677  // It is not row => null inside is impossible
678  virtual bool null_inside();
679  // used in row subselects to get value of elements
680  virtual void bring_value();
681 
693  Field *tmp_table_field_from_field_type(Table *table, bool fixed_length);
694 
695  virtual Item *neg_transformer(Session *session);
696  virtual Item *update_value_transformer(unsigned char *select_arg);
697  virtual Item *safe_charset_converter(const charset_info_st * const tocs);
698  void delete_self();
699 
706  virtual bool result_as_int64_t();
707  bool is_datetime();
708 
727  virtual bool is_expensive();
728 
729  String *check_well_formed_result(String *str, bool send_error= 0);
750  bool eq_by_collation(Item *item, bool binary_cmp, const charset_info_st * const cs);
751 
752  inline uint32_t char_to_byte_length_safe(uint32_t char_length_arg, uint32_t mbmaxlen_arg)
753  {
754  uint64_t tmp= ((uint64_t) char_length_arg) * mbmaxlen_arg;
755  return (tmp > UINT32_MAX) ? (uint32_t) UINT32_MAX : (uint32_t) tmp;
756  }
757 
758  uint32_t max_char_length() const;
759 
760  void fix_char_length(uint32_t max_char_length_arg);
761 
762 protected:
763  Session &getSession()
764  {
765  return _session;
766  }
767 
768 private:
769  Session &_session;
770 };
771 
772 namespace display {
773 const std::string &type(Item::Type type);
774 } /* namespace display */
775 
776 std::ostream& operator<<(std::ostream& output, const Item &item);
777 
778 } /* namespace drizzled */
779 
781 #include <drizzled/item/ident.h>
782 
783 namespace drizzled
784 {
785 
797 void mark_as_dependent(Session *session,
798  Select_Lex *last,
799  Select_Lex *current,
800  Item_ident *resolved_item,
801  Item_ident *mark_item);
802 
838 Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select);
839 
859 void mark_select_range_as_dependent(Session *session,
860  Select_Lex *last_select,
861  Select_Lex *current_sel,
862  Field *found_field, Item *found_item,
863  Item_ident *resolved_item);
864 
865 extern void resolve_const_item(Session *session, Item **ref, Item *cmp_item);
873 extern bool field_is_equal_to_item(Field *field,Item *item);
874 
908 Field *create_tmp_field(Session *session,
909  Table *table,
910  Item *item,
911  Item::Type type,
912  Item ***copy_func,
913  Field **from_field,
914  Field **def_field,
915  bool group,
916  bool modify_item,
917  bool make_copy_field,
918  uint32_t convert_blob_length);
919 
920 } /* namespace drizzled */
921