mariadb/sql/item_buff.cc

165 lines
3.8 KiB
C++
Raw Normal View History

/* Copyright (C) 2000-2006 MySQL AB
2000-07-31 21:29:14 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
2000-07-31 21:29:14 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2000-07-31 21:29:14 +02:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
2007-10-11 19:29:09 +02:00
/**
@file
@brief
Buffers to save and compare item values
*/
2000-07-31 21:29:14 +02:00
#include "mysql_priv.h"
2007-10-11 19:29:09 +02:00
/**
Create right type of Cached_item for an item.
2000-07-31 21:29:14 +02:00
*/
Cached_item *new_Cached_item(THD *thd, Item *item)
2000-07-31 21:29:14 +02:00
{
if (item->real_item()->type() == Item::FIELD_ITEM &&
!(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
return new Cached_item_field((Item_field *) (item->real_item()));
switch (item->result_type()) {
2005-02-08 23:50:45 +01:00
case STRING_RESULT:
return new Cached_item_str(thd, (Item_field *) item);
2005-02-08 23:50:45 +01:00
case INT_RESULT:
return new Cached_item_int((Item_field *) item);
2005-02-08 23:50:45 +01:00
case REAL_RESULT:
return new Cached_item_real(item);
2005-02-08 23:50:45 +01:00
case DECIMAL_RESULT:
return new Cached_item_decimal(item);
2005-02-08 23:50:45 +01:00
case ROW_RESULT:
default:
DBUG_ASSERT(0);
return 0;
}
2000-07-31 21:29:14 +02:00
}
Cached_item::~Cached_item() {}
2000-07-31 21:29:14 +02:00
2007-10-11 19:29:09 +02:00
/**
Compare with old value and replace value with new value.
@return
Return true if values have changed
2000-07-31 21:29:14 +02:00
*/
Cached_item_str::Cached_item_str(THD *thd, Item *arg)
:item(arg), value(min(arg->max_length, thd->variables.max_sort_length))
{}
bool Cached_item_str::cmp(void)
2000-07-31 21:29:14 +02:00
{
String *res;
bool tmp;
if ((res=item->val_str(&tmp_value)))
res->length(min(res->length(), value.alloced_length()));
2000-07-31 21:29:14 +02:00
if (null_value != item->null_value)
{
if ((null_value= item->null_value))
return TRUE; // New value was null
tmp=TRUE;
}
else if (null_value)
return 0; // new and old value was null
else
tmp= sortcmp(&value,res,item->collation.collation) != 0;
2000-07-31 21:29:14 +02:00
if (tmp)
value.copy(*res); // Remember for next cmp
return tmp;
}
Cached_item_str::~Cached_item_str()
2000-07-31 21:29:14 +02:00
{
item=0; // Safety
}
bool Cached_item_real::cmp(void)
2000-07-31 21:29:14 +02:00
{
2004-11-11 19:39:35 +01:00
double nr= item->val_real();
2000-07-31 21:29:14 +02:00
if (null_value != item->null_value || nr != value)
{
null_value= item->null_value;
value=nr;
return TRUE;
}
return FALSE;
}
bool Cached_item_int::cmp(void)
2000-07-31 21:29:14 +02:00
{
longlong nr=item->val_int();
if (null_value != item->null_value || nr != value)
{
null_value= item->null_value;
value=nr;
return TRUE;
}
return FALSE;
}
bool Cached_item_field::cmp(void)
2000-07-31 21:29:14 +02:00
{
bool tmp= field->cmp(buff) != 0; // This is not a blob!
if (tmp)
2002-12-19 12:27:46 +01:00
field->get_image(buff,length,field->charset());
2000-07-31 21:29:14 +02:00
if (null_value != field->is_null())
{
null_value= !null_value;
tmp=TRUE;
}
return tmp;
}
Cached_item_decimal::Cached_item_decimal(Item *it)
2005-02-08 23:50:45 +01:00
:item(it)
{
my_decimal_set_zero(&value);
}
bool Cached_item_decimal::cmp()
2005-02-08 23:50:45 +01:00
{
my_decimal tmp;
my_decimal *ptmp= item->val_decimal(&tmp);
if (null_value != item->null_value ||
(!item->null_value && my_decimal_cmp(&value, ptmp)))
2005-02-08 23:50:45 +01:00
{
null_value= item->null_value;
/* Save only not null values */
if (!null_value)
{
my_decimal2decimal(ptmp, &value);
return TRUE;
}
return FALSE;
2005-02-08 23:50:45 +01:00
}
return FALSE;
}
2000-07-31 21:29:14 +02:00
/*****************************************************************************
** Instansiate templates
*****************************************************************************/
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
template class List<Cached_item>;
template class List_iterator<Cached_item>;
2000-07-31 21:29:14 +02:00
#endif