~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/item_buff.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
31
{
 
32
  if (item->real_item()->type() == Item::FIELD_ITEM &&
 
33
      !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
 
34
    return new Cached_item_field((Item_field *) (item->real_item()));
 
35
  switch (item->result_type()) {
 
36
  case STRING_RESULT:
 
37
    return new Cached_item_str(thd, (Item_field *) item);
 
38
  case INT_RESULT:
 
39
    return new Cached_item_int((Item_field *) item);
 
40
  case REAL_RESULT:
 
41
    return new Cached_item_real(item);
 
42
  case DECIMAL_RESULT:
 
43
    return new Cached_item_decimal(item);
 
44
  case ROW_RESULT:
 
45
  default:
 
46
    DBUG_ASSERT(0);
 
47
    return 0;
 
48
  }
 
49
}
 
50
 
 
51
Cached_item::~Cached_item() {}
 
52
 
 
53
/**
 
54
  Compare with old value and replace value with new value.
 
55
 
 
56
  @return
 
57
    Return true if values have changed
 
58
*/
 
59
 
 
60
Cached_item_str::Cached_item_str(THD *thd, Item *arg)
 
61
  :item(arg), value(min(arg->max_length, thd->variables.max_sort_length))
 
62
{}
 
63
 
 
64
bool Cached_item_str::cmp(void)
 
65
{
 
66
  String *res;
 
67
  bool tmp;
 
68
 
 
69
  if ((res=item->val_str(&tmp_value)))
 
70
    res->length(min(res->length(), value.alloced_length()));
 
71
  if (null_value != item->null_value)
 
72
  {
 
73
    if ((null_value= item->null_value))
 
74
      return TRUE;                              // New value was null
 
75
    tmp=TRUE;
 
76
  }
 
77
  else if (null_value)
 
78
    return 0;                                   // new and old value was null
 
79
  else
 
80
    tmp= sortcmp(&value,res,item->collation.collation) != 0;
 
81
  if (tmp)
 
82
    value.copy(*res);                           // Remember for next cmp
 
83
  return tmp;
 
84
}
 
85
 
 
86
Cached_item_str::~Cached_item_str()
 
87
{
 
88
  item=0;                                       // Safety
 
89
}
 
90
 
 
91
bool Cached_item_real::cmp(void)
 
92
{
 
93
  double nr= item->val_real();
 
94
  if (null_value != item->null_value || nr != value)
 
95
  {
 
96
    null_value= item->null_value;
 
97
    value=nr;
 
98
    return TRUE;
 
99
  }
 
100
  return FALSE;
 
101
}
 
102
 
 
103
bool Cached_item_int::cmp(void)
 
104
{
 
105
  longlong nr=item->val_int();
 
106
  if (null_value != item->null_value || nr != value)
 
107
  {
 
108
    null_value= item->null_value;
 
109
    value=nr;
 
110
    return TRUE;
 
111
  }
 
112
  return FALSE;
 
113
}
 
114
 
 
115
 
 
116
bool Cached_item_field::cmp(void)
 
117
{
 
118
  bool tmp= field->cmp(buff) != 0;              // This is not a blob!
 
119
  if (tmp)
 
120
    field->get_image(buff,length,field->charset());
 
121
  if (null_value != field->is_null())
 
122
  {
 
123
    null_value= !null_value;
 
124
    tmp=TRUE;
 
125
  }
 
126
  return tmp;
 
127
}
 
128
 
 
129
 
 
130
Cached_item_decimal::Cached_item_decimal(Item *it)
 
131
  :item(it)
 
132
{
 
133
  my_decimal_set_zero(&value);
 
134
}
 
135
 
 
136
 
 
137
bool Cached_item_decimal::cmp()
 
138
{
 
139
  my_decimal tmp;
 
140
  my_decimal *ptmp= item->val_decimal(&tmp);
 
141
  if (null_value != item->null_value ||
 
142
      (!item->null_value && my_decimal_cmp(&value, ptmp)))
 
143
  {
 
144
    null_value= item->null_value;
 
145
    /* Save only not null values */
 
146
    if (!null_value)
 
147
    {
 
148
      my_decimal2decimal(ptmp, &value);
 
149
      return TRUE;
 
150
    }
 
151
    return FALSE;
 
152
  }
 
153
  return FALSE;
 
154
}
 
155
 
 
156
 
 
157
/*****************************************************************************
 
158
** Instansiate templates
 
159
*****************************************************************************/
 
160
 
 
161
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
162
template class List<Cached_item>;
 
163
template class List_iterator<Cached_item>;
 
164
#endif