~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
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.
6
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.
11
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
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/**
18
  @file
19
20
  @brief
21
  Buffers to save and compare item values
22
*/
23
24
#include "mysql_priv.h"
25
26
/**
27
  Create right type of Cached_item for an item.
28
*/
29
30
Cached_item *new_Cached_item(THD *thd, Item *item, bool use_result_field)
31
{
32
  if (item->real_item()->type() == Item::FIELD_ITEM &&
33
      !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
34
  {
35
    Item_field *real_item= (Item_field *) item->real_item();
36
    Field *cached_field= use_result_field ? real_item->result_field :
37
                                            real_item->field;
38
    return new Cached_item_field(cached_field);
39
  }
40
  switch (item->result_type()) {
41
  case STRING_RESULT:
42
    return new Cached_item_str(thd, (Item_field *) item);
43
  case INT_RESULT:
44
    return new Cached_item_int((Item_field *) item);
45
  case REAL_RESULT:
46
    return new Cached_item_real(item);
47
  case DECIMAL_RESULT:
48
    return new Cached_item_decimal(item);
49
  case ROW_RESULT:
50
  default:
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
51
    assert(0);
1 by brian
clean slate
52
    return 0;
53
  }
54
}
55
56
Cached_item::~Cached_item() {}
57
58
/**
59
  Compare with old value and replace value with new value.
60
61
  @return
62
    Return true if values have changed
63
*/
64
65
Cached_item_str::Cached_item_str(THD *thd, Item *arg)
66
  :item(arg), value(min(arg->max_length, thd->variables.max_sort_length))
67
{}
68
69
bool Cached_item_str::cmp(void)
70
{
71
  String *res;
72
  bool tmp;
73
74
  if ((res=item->val_str(&tmp_value)))
75
    res->length(min(res->length(), value.alloced_length()));
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
76
1 by brian
clean slate
77
  if (null_value != item->null_value)
78
  {
79
    if ((null_value= item->null_value))
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
80
      return(true);			// New value was null
81
    tmp=true;
1 by brian
clean slate
82
  }
83
  else if (null_value)
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
84
    return(0);				// new and old value was null
1 by brian
clean slate
85
  else
86
    tmp= sortcmp(&value,res,item->collation.collation) != 0;
87
  if (tmp)
88
    value.copy(*res);				// Remember for next cmp
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
89
  return(tmp);
1 by brian
clean slate
90
}
91
92
Cached_item_str::~Cached_item_str()
93
{
94
  item=0;					// Safety
95
}
96
97
bool Cached_item_real::cmp(void)
98
{
99
  double nr= item->val_real();
100
  if (null_value != item->null_value || nr != value)
101
  {
102
    null_value= item->null_value;
103
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
104
    return(true);
1 by brian
clean slate
105
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
106
  return(false);
1 by brian
clean slate
107
}
108
109
bool Cached_item_int::cmp(void)
110
{
152 by Brian Aker
longlong replacement
111
  int64_t nr=item->val_int();
1 by brian
clean slate
112
  if (null_value != item->null_value || nr != value)
113
  {
114
    null_value= item->null_value;
115
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
116
    return(true);
1 by brian
clean slate
117
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
118
  return(false);
1 by brian
clean slate
119
}
120
121
122
bool Cached_item_field::cmp(void)
123
{
124
  bool tmp= field->cmp(buff) != 0;		// This is not a blob!
125
  if (tmp)
126
    field->get_image(buff,length,field->charset());
127
  if (null_value != field->is_null())
128
  {
129
    null_value= !null_value;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
130
    tmp=true;
1 by brian
clean slate
131
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
132
  return(tmp);
1 by brian
clean slate
133
}
134
135
136
Cached_item_decimal::Cached_item_decimal(Item *it)
137
  :item(it)
138
{
139
  my_decimal_set_zero(&value);
140
}
141
142
143
bool Cached_item_decimal::cmp()
144
{
145
  my_decimal tmp;
146
  my_decimal *ptmp= item->val_decimal(&tmp);
147
  if (null_value != item->null_value ||
148
      (!item->null_value && my_decimal_cmp(&value, ptmp)))
149
  {
150
    null_value= item->null_value;
151
    /* Save only not null values */
152
    if (!null_value)
153
    {
154
      my_decimal2decimal(ptmp, &value);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
155
      return true;
1 by brian
clean slate
156
    }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
157
    return false;
1 by brian
clean slate
158
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
159
  return false;
1 by brian
clean slate
160
}
161
162
163
/*****************************************************************************
164
** Instansiate templates
165
*****************************************************************************/
166
167
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
168
template class List<Cached_item>;
169
template class List_iterator<Cached_item>;
170
#endif