1/*
2 Copyright (c) 2002, 2011, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17#include "mariadb.h"
18#include "sql_priv.h"
19/*
20 It is necessary to include set_var.h instead of item.h because there
21 are dependencies on include order for set_var.h and item.h. This
22 will be resolved later.
23*/
24#include "sql_class.h" // THD, set_var.h: THD
25#include "set_var.h"
26
27void Item_row::illegal_method_call(const char *method)
28{
29 DBUG_ENTER("Item_row::illegal_method_call");
30 DBUG_PRINT("error", ("!!! %s method was called for row item", method));
31 DBUG_ASSERT(0);
32 my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
33 DBUG_VOID_RETURN;
34}
35
36bool Item_row::fix_fields(THD *thd, Item **ref)
37{
38 DBUG_ASSERT(fixed == 0);
39 null_value= 0;
40 maybe_null= 0;
41 Item **arg, **arg_end;
42 for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
43 {
44 if (!(*arg)->fixed &&
45 (*arg)->fix_fields(thd, arg))
46 return TRUE;
47 // we can't assign 'item' before, because fix_fields() can change arg
48 Item *item= *arg;
49 used_tables_cache |= item->used_tables();
50 const_item_cache&= item->const_item() && !with_null;
51 not_null_tables_cache|= item->not_null_tables();
52
53 if (const_item_cache)
54 {
55 if (item->cols() > 1)
56 with_null|= item->null_inside();
57 else
58 {
59 if (item->is_null())
60 with_null|= 1;
61 }
62 }
63 maybe_null|= item->maybe_null;
64 with_sum_func= with_sum_func || item->with_sum_func;
65 with_window_func = with_window_func || item->with_window_func;
66 with_field= with_field || item->with_field;
67 m_with_subquery|= item->with_subquery();
68 with_param|= item->with_param;
69 }
70 fixed= 1;
71 return FALSE;
72}
73
74
75bool
76Item_row::eval_not_null_tables(void *opt_arg)
77{
78 Item **arg,**arg_end;
79 not_null_tables_cache= 0;
80 if (arg_count)
81 {
82 for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
83 {
84 not_null_tables_cache|= (*arg)->not_null_tables();
85 }
86 }
87 return FALSE;
88}
89
90
91void Item_row::cleanup()
92{
93 DBUG_ENTER("Item_row::cleanup");
94
95 Item::cleanup();
96 /* Reset to the original values */
97 used_tables_and_const_cache_init();
98 with_null= 0;
99
100 DBUG_VOID_RETURN;
101}
102
103
104void Item_row::split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
105 List<Item> &fields, uint flags)
106{
107 Item **arg, **arg_end;
108 for (arg= args, arg_end= args + arg_count; arg != arg_end ; arg++)
109 (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg,
110 flags | SPLIT_SUM_SKIP_REGISTERED);
111}
112
113
114void Item_row::fix_after_pullout(st_select_lex *new_parent, Item **ref,
115 bool merge)
116{
117 used_tables_and_const_cache_init();
118 not_null_tables_cache= 0;
119 for (uint i= 0; i < arg_count; i++)
120 {
121 args[i]->fix_after_pullout(new_parent, &args[i], merge);
122 used_tables_and_const_cache_join(args[i]);
123 not_null_tables_cache|= args[i]->not_null_tables();
124 }
125}
126
127
128bool Item_row::check_cols(uint c)
129{
130 if (c != arg_count)
131 {
132 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
133 return 1;
134 }
135 return 0;
136}
137
138void Item_row::print(String *str, enum_query_type query_type)
139{
140 str->append('(');
141 for (uint i= 0; i < arg_count; i++)
142 {
143 if (i)
144 str->append(',');
145 args[i]->print(str, query_type);
146 }
147 str->append(')');
148}
149
150
151Item *Item_row::transform(THD *thd, Item_transformer transformer, uchar *arg)
152{
153 DBUG_ASSERT(!thd->stmt_arena->is_stmt_prepare());
154
155 if (transform_args(thd, transformer, arg))
156 return 0;
157 return (this->*transformer)(thd, arg);
158}
159
160void Item_row::bring_value()
161{
162 for (uint i= 0; i < arg_count; i++)
163 args[i]->bring_value();
164}
165
166
167Item* Item_row::build_clone(THD *thd)
168{
169 Item_row *copy= (Item_row *) get_copy(thd);
170 if (!copy)
171 return 0;
172 copy->args= (Item**) alloc_root(thd->mem_root, sizeof(Item*) * arg_count);
173 for (uint i= 0; i < arg_count; i++)
174 {
175 Item *arg_clone= args[i]->build_clone(thd);
176 if (!arg_clone)
177 return 0;
178 copy->args[i]= arg_clone;
179 }
180 return copy;
181}
182