1
/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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.
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.
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 */
21
Buffers to save and compare item values
26
It is necessary to include set_var.h instead of item.h because there
27
are dependencies on include order for set_var.h and item.h. This
28
will be resolved later.
30
#include "sql_class.h" // THD
31
#include "set_var.h" // Cached_item, Cached_item_field, ...
37
Create right type of Cached_item for an item.
40
Cached_item *new_Cached_item(THD *thd, Item *item, bool use_result_field)
42
if (item->real_item()->type() == Item::FIELD_ITEM &&
43
!(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
45
Item_field *real_item= (Item_field *) item->real_item();
46
Field *cached_field= use_result_field ? real_item->result_field :
48
return new Cached_item_field(cached_field);
50
switch (item->result_type()) {
52
if (item->is_temporal())
53
return new Cached_item_temporal((Item_field *) item);
54
return new Cached_item_str(thd, (Item_field *) item);
56
return new Cached_item_int((Item_field *) item);
58
return new Cached_item_real(item);
60
return new Cached_item_decimal(item);
68
Cached_item::~Cached_item() {}
71
Compare with old value and replace value with new value.
74
Return true if values have changed
77
Cached_item_str::Cached_item_str(THD *thd, Item *arg)
79
value_max_length(min<uint32>(arg->max_length, thd->variables.max_sort_length)),
80
value(value_max_length)
83
bool Cached_item_str::cmp(void)
88
DBUG_ENTER("Cached_item_str::cmp");
89
DBUG_ASSERT(!item->is_temporal());
90
if ((res=item->val_str(&tmp_value)))
91
res->length(min(res->length(), value_max_length));
92
DBUG_PRINT("info", ("old: %s, new: %s",
93
value.c_ptr_safe(), res ? res->c_ptr_safe() : ""));
94
if (null_value != item->null_value)
96
if ((null_value= item->null_value))
97
DBUG_RETURN(TRUE); // New value was null
101
DBUG_RETURN(0); // new and old value was null
103
tmp= sortcmp(&value,res,item->collation.collation) != 0;
105
value.copy(*res); // Remember for next cmp
109
Cached_item_str::~Cached_item_str()
114
bool Cached_item_real::cmp(void)
116
DBUG_ENTER("Cached_item_real::cmp");
117
double nr= item->val_real();
118
DBUG_PRINT("info", ("old: %f, new: %f", value, nr));
119
if (null_value != item->null_value || nr != value)
121
null_value= item->null_value;
128
bool Cached_item_int::cmp(void)
130
DBUG_ENTER("Cached_item_int::cmp");
131
longlong nr=item->val_int();
132
DBUG_PRINT("info", ("old: %lld, new: %lld", value, nr));
133
if (null_value != item->null_value || nr != value)
135
null_value= item->null_value;
143
bool Cached_item_temporal::cmp(void)
145
DBUG_ENTER("Cached_item_temporal::cmp");
146
longlong nr= item->val_temporal_by_field_type();
147
DBUG_PRINT("info", ("old: %lld, new: %lld", value, nr));
148
if (null_value != item->null_value || nr != value)
150
null_value= item->null_value;
158
bool Cached_item_field::cmp(void)
160
DBUG_ENTER("Cached_item_field::cmp");
161
DBUG_EXECUTE("info", dbug_print(););
163
bool different= false;
165
if (field->is_null())
179
field->get_image(buff, length, field->charset());
181
else if (field->cmp(buff)) // Not a blob: cmp() is OK
184
field->get_image(buff, length, field->charset());
188
DBUG_RETURN(different);
192
Cached_item_decimal::Cached_item_decimal(Item *it)
195
my_decimal_set_zero(&value);
199
bool Cached_item_decimal::cmp()
202
my_decimal *ptmp= item->val_decimal(&tmp);
203
if (null_value != item->null_value ||
204
(!item->null_value && my_decimal_cmp(&value, ptmp)))
206
null_value= item->null_value;
207
/* Save only not null values */
210
my_decimal2decimal(ptmp, &value);