~drizzle-trunk/drizzle/development

584.4.10 by Monty Taylor
Broke out cached_item.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
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.
9
 *
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.
14
 *
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
18
 */
1 by brian
clean slate
19
20
/**
21
  @file
22
23
  @brief
24
  Buffers to save and compare item values
25
*/
26
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
27
#include <drizzled/server_includes.h>
584.4.10 by Monty Taylor
Broke out cached_item.
28
#include <drizzled/cached_item.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
29
#include <drizzled/sql_string.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
30
#include <drizzled/session.h>
1 by brian
clean slate
31
32
/**
33
  Create right type of Cached_item for an item.
34
*/
35
584.4.10 by Monty Taylor
Broke out cached_item.
36
Cached_item *new_Cached_item(Session *session, Item *item,
37
                             bool use_result_field)
1 by brian
clean slate
38
{
39
  if (item->real_item()->type() == Item::FIELD_ITEM &&
40
      !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
41
  {
42
    Item_field *real_item= (Item_field *) item->real_item();
43
    Field *cached_field= use_result_field ? real_item->result_field :
44
                                            real_item->field;
45
    return new Cached_item_field(cached_field);
46
  }
47
  switch (item->result_type()) {
48
  case STRING_RESULT:
520.1.22 by Brian Aker
Second pass of thd cleanup
49
    return new Cached_item_str(session, (Item_field *) item);
1 by brian
clean slate
50
  case INT_RESULT:
51
    return new Cached_item_int((Item_field *) item);
52
  case REAL_RESULT:
53
    return new Cached_item_real(item);
54
  case DECIMAL_RESULT:
55
    return new Cached_item_decimal(item);
56
  case ROW_RESULT:
57
  default:
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
58
    assert(0);
1 by brian
clean slate
59
    return 0;
60
  }
61
}
62
63
Cached_item::~Cached_item() {}
64
65
/**
66
  Compare with old value and replace value with new value.
67
68
  @return
69
    Return true if values have changed
70
*/
71
520.1.22 by Brian Aker
Second pass of thd cleanup
72
Cached_item_str::Cached_item_str(Session *session, Item *arg)
584.4.10 by Monty Taylor
Broke out cached_item.
73
  :item(arg), value(cmin(arg->max_length,
74
                         (uint32_t)session->variables.max_sort_length))
1 by brian
clean slate
75
{}
76
77
bool Cached_item_str::cmp(void)
78
{
79
  String *res;
80
  bool tmp;
81
82
  if ((res=item->val_str(&tmp_value)))
398.1.4 by Monty Taylor
Renamed max/min.
83
    res->length(cmin(res->length(), value.alloced_length()));
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
84
1 by brian
clean slate
85
  if (null_value != item->null_value)
86
  {
87
    if ((null_value= item->null_value))
584.4.10 by Monty Taylor
Broke out cached_item.
88
      // New value was null
89
      return(true);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
90
    tmp=true;
1 by brian
clean slate
91
  }
92
  else if (null_value)
584.4.10 by Monty Taylor
Broke out cached_item.
93
    // new and old value was null
94
    return(0);
1 by brian
clean slate
95
  else
96
    tmp= sortcmp(&value,res,item->collation.collation) != 0;
97
  if (tmp)
584.4.10 by Monty Taylor
Broke out cached_item.
98
    // Remember for next cmp
99
    value.copy(*res);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
100
  return(tmp);
1 by brian
clean slate
101
}
102
103
Cached_item_str::~Cached_item_str()
104
{
584.4.10 by Monty Taylor
Broke out cached_item.
105
  // Safety
106
  item=0;
1 by brian
clean slate
107
}
108
109
bool Cached_item_real::cmp(void)
110
{
111
  double nr= item->val_real();
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
bool Cached_item_int::cmp(void)
122
{
152 by Brian Aker
longlong replacement
123
  int64_t nr=item->val_int();
1 by brian
clean slate
124
  if (null_value != item->null_value || nr != value)
125
  {
126
    null_value= item->null_value;
127
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
128
    return(true);
1 by brian
clean slate
129
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
130
  return(false);
1 by brian
clean slate
131
}
132
133
134
bool Cached_item_field::cmp(void)
135
{
584.4.10 by Monty Taylor
Broke out cached_item.
136
  // This is not a blob!
137
  bool tmp= field->cmp(buff) != 0;
138
1 by brian
clean slate
139
  if (tmp)
140
    field->get_image(buff,length,field->charset());
141
  if (null_value != field->is_null())
142
  {
143
    null_value= !null_value;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
144
    tmp=true;
1 by brian
clean slate
145
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
146
  return(tmp);
1 by brian
clean slate
147
}
148
149
150
Cached_item_decimal::Cached_item_decimal(Item *it)
151
  :item(it)
152
{
153
  my_decimal_set_zero(&value);
154
}
155
156
157
bool Cached_item_decimal::cmp()
158
{
159
  my_decimal tmp;
160
  my_decimal *ptmp= item->val_decimal(&tmp);
161
  if (null_value != item->null_value ||
162
      (!item->null_value && my_decimal_cmp(&value, ptmp)))
163
  {
164
    null_value= item->null_value;
165
    /* Save only not null values */
166
    if (!null_value)
167
    {
168
      my_decimal2decimal(ptmp, &value);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
169
      return true;
1 by brian
clean slate
170
    }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
171
    return false;
1 by brian
clean slate
172
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
173
  return false;
1 by brian
clean slate
174
}
175
176