~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
    DBUG_ASSERT(0);
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
  DBUG_ENTER("Cached_item_str::cmp");
75
  if ((res=item->val_str(&tmp_value)))
76
    res->length(min(res->length(), value.alloced_length()));
77
  DBUG_PRINT("info", ("old: %s, new: %s",
78
                      value.c_ptr_safe(), res ? res->c_ptr_safe() : ""));
79
  if (null_value != item->null_value)
80
  {
81
    if ((null_value= item->null_value))
82
      DBUG_RETURN(TRUE);			// New value was null
83
    tmp=TRUE;
84
  }
85
  else if (null_value)
86
    DBUG_RETURN(0);				// new and old value was null
87
  else
88
    tmp= sortcmp(&value,res,item->collation.collation) != 0;
89
  if (tmp)
90
    value.copy(*res);				// Remember for next cmp
91
  DBUG_RETURN(tmp);
92
}
93
94
Cached_item_str::~Cached_item_str()
95
{
96
  item=0;					// Safety
97
}
98
99
bool Cached_item_real::cmp(void)
100
{
101
  DBUG_ENTER("Cached_item_real::cmp");
102
  double nr= item->val_real();
103
  DBUG_PRINT("info", ("old: %f, new: %f", value, nr));
104
  if (null_value != item->null_value || nr != value)
105
  {
106
    null_value= item->null_value;
107
    value=nr;
108
    DBUG_RETURN(TRUE);
109
  }
110
  DBUG_RETURN(FALSE);
111
}
112
113
bool Cached_item_int::cmp(void)
114
{
115
  DBUG_ENTER("Cached_item_int::cmp");
116
  longlong nr=item->val_int();
117
  DBUG_PRINT("info", ("old: %Ld, new: %Ld", value, nr));
118
  if (null_value != item->null_value || nr != value)
119
  {
120
    null_value= item->null_value;
121
    value=nr;
122
    DBUG_RETURN(TRUE);
123
  }
124
  DBUG_RETURN(FALSE);
125
}
126
127
128
bool Cached_item_field::cmp(void)
129
{
130
  DBUG_ENTER("Cached_item_field::cmp");
131
  bool tmp= field->cmp(buff) != 0;		// This is not a blob!
132
  DBUG_EXECUTE("info", dbug_print(););
133
  if (tmp)
134
    field->get_image(buff,length,field->charset());
135
  if (null_value != field->is_null())
136
  {
137
    null_value= !null_value;
138
    tmp=TRUE;
139
  }
140
  DBUG_RETURN(tmp);
141
}
142
143
144
Cached_item_decimal::Cached_item_decimal(Item *it)
145
  :item(it)
146
{
147
  my_decimal_set_zero(&value);
148
}
149
150
151
bool Cached_item_decimal::cmp()
152
{
153
  my_decimal tmp;
154
  my_decimal *ptmp= item->val_decimal(&tmp);
155
  if (null_value != item->null_value ||
156
      (!item->null_value && my_decimal_cmp(&value, ptmp)))
157
  {
158
    null_value= item->null_value;
159
    /* Save only not null values */
160
    if (!null_value)
161
    {
162
      my_decimal2decimal(ptmp, &value);
163
      return TRUE;
164
    }
165
    return FALSE;
166
  }
167
  return FALSE;
168
}
169
170
171
/*****************************************************************************
172
** Instansiate templates
173
*****************************************************************************/
174
175
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
176
template class List<Cached_item>;
177
template class List_iterator<Cached_item>;
178
#endif