~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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
27
#include "config.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>
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
31
#include <algorithm>
32
33
using namespace std;
1 by brian
clean slate
34
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
35
namespace drizzled
36
{
37
1 by brian
clean slate
38
/**
39
  Create right type of Cached_item for an item.
40
*/
41
1221.1.1 by Jay Pipes
Fixes some valgrind warnings regarding conditionals depending on unintialized variables. Use initializer lists properly, dang it. :) Also, removed the new_Cached_item() function's use_result_field, as this was only used for views and was producing a valgrind warning unnecessarily.
42
Cached_item *new_Cached_item(Session *session, Item *item)
1 by brian
clean slate
43
{
44
  if (item->real_item()->type() == Item::FIELD_ITEM &&
45
      !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
46
  {
47
    Item_field *real_item= (Item_field *) item->real_item();
1221.1.1 by Jay Pipes
Fixes some valgrind warnings regarding conditionals depending on unintialized variables. Use initializer lists properly, dang it. :) Also, removed the new_Cached_item() function's use_result_field, as this was only used for views and was producing a valgrind warning unnecessarily.
48
    Field *cached_field= real_item->field;
1 by brian
clean slate
49
    return new Cached_item_field(cached_field);
50
  }
51
  switch (item->result_type()) {
52
  case STRING_RESULT:
520.1.22 by Brian Aker
Second pass of thd cleanup
53
    return new Cached_item_str(session, (Item_field *) item);
1 by brian
clean slate
54
  case INT_RESULT:
55
    return new Cached_item_int((Item_field *) item);
56
  case REAL_RESULT:
57
    return new Cached_item_real(item);
58
  case DECIMAL_RESULT:
59
    return new Cached_item_decimal(item);
60
  case ROW_RESULT:
61
  default:
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
62
    assert(0);
1 by brian
clean slate
63
    return 0;
64
  }
65
}
66
67
Cached_item::~Cached_item() {}
68
69
/**
70
  Compare with old value and replace value with new value.
71
72
  @return
73
    Return true if values have changed
74
*/
75
520.1.22 by Brian Aker
Second pass of thd cleanup
76
Cached_item_str::Cached_item_str(Session *session, Item *arg)
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
77
  :item(arg), value(min(arg->max_length,
78
                        (uint32_t)session->variables.max_sort_length))
1 by brian
clean slate
79
{}
80
81
bool Cached_item_str::cmp(void)
82
{
83
  String *res;
84
  bool tmp;
85
86
  if ((res=item->val_str(&tmp_value)))
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
87
    res->length(min(res->length(), value.alloced_length()));
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
88
1 by brian
clean slate
89
  if (null_value != item->null_value)
90
  {
91
    if ((null_value= item->null_value))
584.4.10 by Monty Taylor
Broke out cached_item.
92
      // New value was null
93
      return(true);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
94
    tmp=true;
1 by brian
clean slate
95
  }
96
  else if (null_value)
584.4.10 by Monty Taylor
Broke out cached_item.
97
    // new and old value was null
98
    return(0);
1 by brian
clean slate
99
  else
100
    tmp= sortcmp(&value,res,item->collation.collation) != 0;
101
  if (tmp)
584.4.10 by Monty Taylor
Broke out cached_item.
102
    // Remember for next cmp
103
    value.copy(*res);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
104
  return(tmp);
1 by brian
clean slate
105
}
106
107
Cached_item_str::~Cached_item_str()
108
{
584.4.10 by Monty Taylor
Broke out cached_item.
109
  // Safety
110
  item=0;
1 by brian
clean slate
111
}
112
113
bool Cached_item_real::cmp(void)
114
{
115
  double nr= item->val_real();
116
  if (null_value != item->null_value || nr != value)
117
  {
118
    null_value= item->null_value;
119
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
120
    return(true);
1 by brian
clean slate
121
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
122
  return(false);
1 by brian
clean slate
123
}
124
125
bool Cached_item_int::cmp(void)
126
{
152 by Brian Aker
longlong replacement
127
  int64_t nr=item->val_int();
1 by brian
clean slate
128
  if (null_value != item->null_value || nr != value)
129
  {
130
    null_value= item->null_value;
131
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
132
    return(true);
1 by brian
clean slate
133
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
134
  return(false);
1 by brian
clean slate
135
}
136
137
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
138
Cached_item_field::Cached_item_field(Field *arg_field) 
139
  : 
140
    field(arg_field)
141
{
142
  /* TODO: take the memory allocation below out of the constructor. */
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
143
  buff= (unsigned char*) memory::sql_calloc(length= field->pack_length());
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
144
}
145
1 by brian
clean slate
146
bool Cached_item_field::cmp(void)
147
{
584.4.10 by Monty Taylor
Broke out cached_item.
148
  // This is not a blob!
149
  bool tmp= field->cmp(buff) != 0;
150
1 by brian
clean slate
151
  if (tmp)
152
    field->get_image(buff,length,field->charset());
153
  if (null_value != field->is_null())
154
  {
155
    null_value= !null_value;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
156
    tmp=true;
1 by brian
clean slate
157
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
158
  return(tmp);
1 by brian
clean slate
159
}
160
161
162
Cached_item_decimal::Cached_item_decimal(Item *it)
163
  :item(it)
164
{
165
  my_decimal_set_zero(&value);
166
}
167
168
169
bool Cached_item_decimal::cmp()
170
{
171
  my_decimal tmp;
172
  my_decimal *ptmp= item->val_decimal(&tmp);
173
  if (null_value != item->null_value ||
174
      (!item->null_value && my_decimal_cmp(&value, ptmp)))
175
  {
176
    null_value= item->null_value;
177
    /* Save only not null values */
178
    if (!null_value)
179
    {
180
      my_decimal2decimal(ptmp, &value);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
181
      return true;
1 by brian
clean slate
182
    }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
183
    return false;
1 by brian
clean slate
184
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
185
  return false;
1 by brian
clean slate
186
}
187
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
188
} /* namespace drizzled */