MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
item_row.cc
1 /* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 #include "sql_priv.h"
17 /*
18  It is necessary to include set_var.h instead of item.h because there
19  are dependencies on include order for set_var.h and item.h. This
20  will be resolved later.
21 */
22 #include "sql_class.h" // THD, set_var.h: THD
23 #include "set_var.h"
24 
40  Item(), used_tables_cache(0), not_null_tables_cache(0),
41  const_item_cache(1), with_null(0)
42 {
43 
44  //TODO: think placing 2-3 component items in item (as it done for function)
45  if ((arg_count= arg.elements))
46  items= (Item**) sql_alloc(sizeof(Item*)*arg_count);
47  else
48  items= 0;
49  List_iterator<Item> li(arg);
50  uint i= 0;
51  Item *item;
52  while ((item= li++))
53  {
54  items[i]= item;
55  i++;
56  }
57 }
58 
59 void Item_row::illegal_method_call(const char *method)
60 {
61  DBUG_ENTER("Item_row::illegal_method_call");
62  DBUG_PRINT("error", ("!!! %s method was called for row item", method));
63  DBUG_ASSERT(0);
64  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
65  DBUG_VOID_RETURN;
66 }
67 
68 bool Item_row::fix_fields(THD *thd, Item **ref)
69 {
70  DBUG_ASSERT(fixed == 0);
71  null_value= 0;
72  maybe_null= 0;
73  Item **arg, **arg_end;
74  for (arg= items, arg_end= items+arg_count; arg != arg_end ; arg++)
75  {
76  if ((*arg)->fix_fields(thd, arg))
77  return TRUE;
78  // we can't assign 'item' before, because fix_fields() can change arg
79  Item *item= *arg;
80  used_tables_cache |= item->used_tables();
81  const_item_cache&= item->const_item() && !with_null;
82  not_null_tables_cache|= item->not_null_tables();
83 
84  if (const_item_cache)
85  {
86  if (item->cols() > 1)
87  with_null|= item->null_inside();
88  else
89  {
90  if (item->is_null())
91  with_null|= 1;
92  }
93  }
94  maybe_null|= item->maybe_null;
95  with_sum_func|= item->with_sum_func;
96  with_subselect|= item->has_subquery();
97  }
98  fixed= 1;
99  return FALSE;
100 }
101 
102 
103 void Item_row::cleanup()
104 {
105  DBUG_ENTER("Item_row::cleanup");
106 
107  Item::cleanup();
108  /* Reset to the original values */
109  used_tables_cache= 0;
110  const_item_cache= 1;
111  with_null= 0;
112 
113  DBUG_VOID_RETURN;
114 }
115 
116 
117 void Item_row::split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
118  List<Item> &fields)
119 {
120  Item **arg, **arg_end;
121  for (arg= items, arg_end= items+arg_count; arg != arg_end ; arg++)
122  (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, TRUE);
123 }
124 
125 
126 void Item_row::update_used_tables()
127 {
128  used_tables_cache= 0;
129  const_item_cache= 1;
130  with_subselect= false;
131  with_stored_program= false;
132  for (uint i= 0; i < arg_count; i++)
133  {
134  items[i]->update_used_tables();
135  used_tables_cache|= items[i]->used_tables();
136  const_item_cache&= items[i]->const_item();
137  with_subselect|= items[i]->has_subquery();
138  with_stored_program|= items[i]->has_stored_program();
139  }
140 }
141 
142 void Item_row::fix_after_pullout(st_select_lex *parent_select,
143  st_select_lex *removed_select)
144 {
145  used_tables_cache= 0;
146  const_item_cache= 1;
147  for (uint i= 0; i < arg_count; i++)
148  {
149  items[i]->fix_after_pullout(parent_select, removed_select);
150  used_tables_cache|= items[i]->used_tables();
151  const_item_cache&= items[i]->const_item();
152  }
153 }
154 
155 bool Item_row::check_cols(uint c)
156 {
157  if (c != arg_count)
158  {
159  my_error(ER_OPERAND_COLUMNS, MYF(0), c);
160  return 1;
161  }
162  return 0;
163 }
164 
165 void Item_row::print(String *str, enum_query_type query_type)
166 {
167  str->append('(');
168  for (uint i= 0; i < arg_count; i++)
169  {
170  if (i)
171  str->append(',');
172  items[i]->print(str, query_type);
173  }
174  str->append(')');
175 }
176 
177 
178 bool Item_row::walk(Item_processor processor, bool walk_subquery, uchar *arg)
179 {
180  for (uint i= 0; i < arg_count; i++)
181  {
182  if (items[i]->walk(processor, walk_subquery, arg))
183  return 1;
184  }
185  return (this->*processor)(arg);
186 }
187 
188 
189 Item *Item_row::transform(Item_transformer transformer, uchar *arg)
190 {
191  DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
192 
193  for (uint i= 0; i < arg_count; i++)
194  {
195  Item *new_item= items[i]->transform(transformer, arg);
196  if (!new_item)
197  return 0;
198 
199  /*
200  THD::change_item_tree() should be called only if the tree was
201  really transformed, i.e. when a new item has been created.
202  Otherwise we'll be allocating a lot of unnecessary memory for
203  change records at each execution.
204  */
205  if (items[i] != new_item)
206  current_thd->change_item_tree(&items[i], new_item);
207  }
208  return (this->*transformer)(arg);
209 }
210 
211 void Item_row::bring_value()
212 {
213  for (uint i= 0; i < arg_count; i++)
214  items[i]->bring_value();
215 }