1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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.
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.
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
24
Buffers to save and compare item values
28
#include <drizzled/cached_item.h>
29
#include <drizzled/sql_string.h>
30
#include <drizzled/session.h>
39
Create right type of Cached_item for an item.
42
Cached_item *new_Cached_item(Session *session, Item *item)
44
if (item->real_item()->type() == Item::FIELD_ITEM &&
45
!(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
47
Item_field *real_item= (Item_field *) item->real_item();
48
Field *cached_field= real_item->field;
49
return new Cached_item_field(cached_field);
52
switch (item->result_type()) {
54
return new Cached_item_str(session, (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);
69
Cached_item::~Cached_item() {}
72
Compare with old value and replace value with new value.
75
Return true if values have changed
78
Cached_item_str::Cached_item_str(Session *session, Item *arg)
79
:item(arg), value(min(arg->max_length,
80
(uint32_t)session->variables.max_sort_length))
83
bool Cached_item_str::cmp(void)
88
if ((res=item->val_str(&tmp_value)))
89
res->length(min(res->length(), value.alloced_length()));
91
if (null_value != item->null_value)
93
if ((null_value= item->null_value))
99
// new and old value was null
102
tmp= sortcmp(&value,res,item->collation.collation) != 0;
104
// Remember for next cmp
109
Cached_item_str::~Cached_item_str()
115
bool Cached_item_real::cmp(void)
117
double nr= item->val_real();
118
if (null_value != item->null_value || nr != value)
120
null_value= item->null_value;
127
bool Cached_item_int::cmp(void)
129
int64_t nr=item->val_int();
130
if (null_value != item->null_value || nr != value)
132
null_value= item->null_value;
140
Cached_item_field::Cached_item_field(Field *arg_field)
144
/* TODO: take the memory allocation below out of the constructor. */
145
buff= (unsigned char*) memory::sql_calloc(length= field->pack_length());
148
bool Cached_item_field::cmp(void)
150
// This is not a blob!
151
bool tmp= field->cmp_internal(buff) != 0;
154
field->get_image(buff,length,field->charset());
155
if (null_value != field->is_null())
157
null_value= !null_value;
164
Cached_item_decimal::Cached_item_decimal(Item *it)
167
my_decimal_set_zero(&value);
171
bool Cached_item_decimal::cmp()
174
my_decimal *ptmp= item->val_decimal(&tmp);
175
if (null_value != item->null_value ||
176
(!item->null_value && my_decimal_cmp(&value, ptmp)))
178
null_value= item->null_value;
179
/* Save only not null values */
182
my_decimal2decimal(ptmp, &value);
190
} /* namespace drizzled */