1 | /* |
2 | Copyright (c) 2000, 2010, 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 | |
18 | /** |
19 | @file |
20 | |
21 | @brief |
22 | Buffers to save and compare item values |
23 | */ |
24 | |
25 | #include "mariadb.h" |
26 | #include "sql_priv.h" |
27 | /* |
28 | It is necessary to include set_var.h instead of item.h because there |
29 | are dependencies on include order for set_var.h and item.h. This |
30 | will be resolved later. |
31 | */ |
32 | #include "sql_class.h" // THD |
33 | #include "set_var.h" // Cached_item, Cached_item_field, ... |
34 | |
35 | /** |
36 | Create right type of Cached_item for an item. |
37 | */ |
38 | |
39 | Cached_item *new_Cached_item(THD *thd, Item *item, bool pass_through_ref) |
40 | { |
41 | if (pass_through_ref && item->real_item()->type() == Item::FIELD_ITEM && |
42 | !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG)) |
43 | { |
44 | Item_field *real_item= (Item_field *) item->real_item(); |
45 | Field *cached_field= real_item->field; |
46 | return new (thd->mem_root) Cached_item_field(thd, cached_field); |
47 | } |
48 | switch (item->result_type()) { |
49 | case STRING_RESULT: |
50 | return new Cached_item_str(thd, (Item_field *) item); |
51 | case INT_RESULT: |
52 | return new Cached_item_int((Item_field *) item); |
53 | case REAL_RESULT: |
54 | return new Cached_item_real(item); |
55 | case DECIMAL_RESULT: |
56 | return new Cached_item_decimal(item); |
57 | case ROW_RESULT: |
58 | default: |
59 | DBUG_ASSERT(0); |
60 | return 0; |
61 | } |
62 | } |
63 | |
64 | Cached_item::~Cached_item() {} |
65 | |
66 | /** |
67 | Compare with old value and replace value with new value. |
68 | |
69 | @return |
70 | Return true if values have changed |
71 | */ |
72 | |
73 | Cached_item_str::Cached_item_str(THD *thd, Item *arg) |
74 | :Cached_item_item(arg), |
75 | value_max_length(MY_MIN(arg->max_length, thd->variables.max_sort_length)), |
76 | value(value_max_length) |
77 | {} |
78 | |
79 | bool Cached_item_str::cmp(void) |
80 | { |
81 | String *res; |
82 | bool tmp; |
83 | |
84 | if ((res=item->val_str(&tmp_value))) |
85 | res->length(MY_MIN(res->length(), value_max_length)); |
86 | if (null_value != item->null_value) |
87 | { |
88 | if ((null_value= item->null_value)) |
89 | return TRUE; // New value was null |
90 | tmp=TRUE; |
91 | } |
92 | else if (null_value) |
93 | return 0; // new and old value was null |
94 | else |
95 | tmp= sortcmp(&value,res,item->collation.collation) != 0; |
96 | if (tmp) |
97 | value.copy(*res); // Remember for next cmp |
98 | return tmp; |
99 | } |
100 | |
101 | |
102 | int Cached_item_str::cmp_read_only() |
103 | { |
104 | String *res= item->val_str(&tmp_value); |
105 | |
106 | if (null_value) |
107 | { |
108 | if (item->null_value) |
109 | return 0; |
110 | else |
111 | return -1; |
112 | } |
113 | if (item->null_value) |
114 | return 1; |
115 | |
116 | return sortcmp(&value, res, item->collation.collation); |
117 | } |
118 | |
119 | |
120 | Cached_item_str::~Cached_item_str() |
121 | { |
122 | item=0; // Safety |
123 | } |
124 | |
125 | bool Cached_item_real::cmp(void) |
126 | { |
127 | double nr= item->val_real(); |
128 | if (null_value != item->null_value || nr != value) |
129 | { |
130 | null_value= item->null_value; |
131 | value=nr; |
132 | return TRUE; |
133 | } |
134 | return FALSE; |
135 | } |
136 | |
137 | |
138 | int Cached_item_real::cmp_read_only() |
139 | { |
140 | double nr= item->val_real(); |
141 | if (null_value) |
142 | { |
143 | if (item->null_value) |
144 | return 0; |
145 | else |
146 | return -1; |
147 | } |
148 | if (item->null_value) |
149 | return 1; |
150 | return (nr == value)? 0 : ((nr < value)? 1: -1); |
151 | } |
152 | |
153 | |
154 | bool Cached_item_int::cmp(void) |
155 | { |
156 | longlong nr=item->val_int(); |
157 | if (null_value != item->null_value || nr != value) |
158 | { |
159 | null_value= item->null_value; |
160 | value=nr; |
161 | return TRUE; |
162 | } |
163 | return FALSE; |
164 | } |
165 | |
166 | |
167 | int Cached_item_int::cmp_read_only() |
168 | { |
169 | longlong nr= item->val_int(); |
170 | if (null_value) |
171 | { |
172 | if (item->null_value) |
173 | return 0; |
174 | else |
175 | return -1; |
176 | } |
177 | if (item->null_value) |
178 | return 1; |
179 | return (nr == value)? 0 : ((nr < value)? 1: -1); |
180 | } |
181 | |
182 | |
183 | bool Cached_item_field::cmp(void) |
184 | { |
185 | bool tmp= FALSE; // Value is identical |
186 | /* Note that field can't be a blob here ! */ |
187 | if (null_value != field->is_null()) |
188 | { |
189 | null_value= !null_value; |
190 | tmp= TRUE; // Value has changed |
191 | } |
192 | |
193 | /* |
194 | If value is not null and value changed (from null to not null or |
195 | becasue of value change), then copy the new value to buffer. |
196 | */ |
197 | if (! null_value && (tmp || (tmp= (field->cmp(buff) != 0)))) |
198 | field->get_image(buff,length,field->charset()); |
199 | return tmp; |
200 | } |
201 | |
202 | |
203 | int Cached_item_field::cmp_read_only() |
204 | { |
205 | if (null_value) |
206 | { |
207 | if (field->is_null()) |
208 | return 0; |
209 | else |
210 | return -1; |
211 | } |
212 | if (field->is_null()) |
213 | return 1; |
214 | |
215 | return field->cmp(buff); |
216 | } |
217 | |
218 | |
219 | Cached_item_decimal::Cached_item_decimal(Item *it) |
220 | :Cached_item_item(it) |
221 | { |
222 | my_decimal_set_zero(&value); |
223 | } |
224 | |
225 | |
226 | bool Cached_item_decimal::cmp() |
227 | { |
228 | my_decimal tmp; |
229 | my_decimal *ptmp= item->val_decimal(&tmp); |
230 | if (null_value != item->null_value || |
231 | (!item->null_value && my_decimal_cmp(&value, ptmp))) |
232 | { |
233 | null_value= item->null_value; |
234 | /* Save only not null values */ |
235 | if (!null_value) |
236 | { |
237 | my_decimal2decimal(ptmp, &value); |
238 | return TRUE; |
239 | } |
240 | return FALSE; |
241 | } |
242 | return FALSE; |
243 | } |
244 | |
245 | |
246 | int Cached_item_decimal::cmp_read_only() |
247 | { |
248 | my_decimal tmp; |
249 | my_decimal *ptmp= item->val_decimal(&tmp); |
250 | if (null_value) |
251 | { |
252 | if (item->null_value) |
253 | return 0; |
254 | else |
255 | return -1; |
256 | } |
257 | if (item->null_value) |
258 | return 1; |
259 | return my_decimal_cmp(&value, ptmp); |
260 | } |
261 | |
262 | |