~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
584.4.10 by Monty Taylor
Broke out cached_item.
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
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
51
1 by brian
clean slate
52
  switch (item->result_type()) {
53
  case STRING_RESULT:
520.1.22 by Brian Aker
Second pass of thd cleanup
54
    return new Cached_item_str(session, (Item_field *) item);
1 by brian
clean slate
55
  case INT_RESULT:
56
    return new Cached_item_int((Item_field *) item);
57
  case REAL_RESULT:
58
    return new Cached_item_real(item);
59
  case DECIMAL_RESULT:
60
    return new Cached_item_decimal(item);
61
  case ROW_RESULT:
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
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
65
66
  abort();
1 by brian
clean slate
67
}
68
69
Cached_item::~Cached_item() {}
70
71
/**
72
  Compare with old value and replace value with new value.
73
74
  @return
75
    Return true if values have changed
76
*/
77
520.1.22 by Brian Aker
Second pass of thd cleanup
78
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.
79
  :item(arg), value(min(arg->max_length,
80
                        (uint32_t)session->variables.max_sort_length))
1 by brian
clean slate
81
{}
82
83
bool Cached_item_str::cmp(void)
84
{
85
  String *res;
86
  bool tmp;
87
88
  if ((res=item->val_str(&tmp_value)))
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
89
    res->length(min(res->length(), value.alloced_length()));
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
90
1 by brian
clean slate
91
  if (null_value != item->null_value)
92
  {
93
    if ((null_value= item->null_value))
584.4.10 by Monty Taylor
Broke out cached_item.
94
      // New value was null
95
      return(true);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
96
    tmp=true;
1 by brian
clean slate
97
  }
98
  else if (null_value)
584.4.10 by Monty Taylor
Broke out cached_item.
99
    // new and old value was null
100
    return(0);
1 by brian
clean slate
101
  else
102
    tmp= sortcmp(&value,res,item->collation.collation) != 0;
103
  if (tmp)
584.4.10 by Monty Taylor
Broke out cached_item.
104
    // Remember for next cmp
105
    value.copy(*res);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
106
  return(tmp);
1 by brian
clean slate
107
}
108
109
Cached_item_str::~Cached_item_str()
110
{
584.4.10 by Monty Taylor
Broke out cached_item.
111
  // Safety
112
  item=0;
1 by brian
clean slate
113
}
114
115
bool Cached_item_real::cmp(void)
116
{
117
  double nr= item->val_real();
118
  if (null_value != item->null_value || nr != value)
119
  {
120
    null_value= item->null_value;
121
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
122
    return(true);
1 by brian
clean slate
123
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
124
  return(false);
1 by brian
clean slate
125
}
126
127
bool Cached_item_int::cmp(void)
128
{
152 by Brian Aker
longlong replacement
129
  int64_t nr=item->val_int();
1 by brian
clean slate
130
  if (null_value != item->null_value || nr != value)
131
  {
132
    null_value= item->null_value;
133
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
134
    return(true);
1 by brian
clean slate
135
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
136
  return(false);
1 by brian
clean slate
137
}
138
139
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
140
Cached_item_field::Cached_item_field(Field *arg_field) 
141
  : 
142
    field(arg_field)
143
{
144
  /* TODO: take the memory allocation below out of the constructor. */
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
145
  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.
146
}
147
1 by brian
clean slate
148
bool Cached_item_field::cmp(void)
149
{
584.4.10 by Monty Taylor
Broke out cached_item.
150
  // This is not a blob!
1996.2.1 by Brian Aker
uuid type code.
151
  bool tmp= field->cmp_internal(buff) != 0;
584.4.10 by Monty Taylor
Broke out cached_item.
152
1 by brian
clean slate
153
  if (tmp)
154
    field->get_image(buff,length,field->charset());
155
  if (null_value != field->is_null())
156
  {
157
    null_value= !null_value;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
158
    tmp=true;
1 by brian
clean slate
159
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
160
  return(tmp);
1 by brian
clean slate
161
}
162
163
164
Cached_item_decimal::Cached_item_decimal(Item *it)
165
  :item(it)
166
{
2034.2.4 by Brian Aker
Further encapsulation for DECIMAL.
167
  value.set_zero();
1 by brian
clean slate
168
}
169
170
171
bool Cached_item_decimal::cmp()
172
{
2030.1.4 by Brian Aker
Change my_decimal to Decimal
173
  type::Decimal tmp;
174
  type::Decimal *ptmp= item->val_decimal(&tmp);
1 by brian
clean slate
175
  if (null_value != item->null_value ||
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
176
      (!item->null_value && class_decimal_cmp(&value, ptmp)))
1 by brian
clean slate
177
  {
178
    null_value= item->null_value;
179
    /* Save only not null values */
180
    if (!null_value)
181
    {
2030.1.3 by Brian Aker
Second pass through function names.
182
      class_decimal2decimal(ptmp, &value);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
183
      return true;
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
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
187
  return false;
1 by brian
clean slate
188
}
189
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
190
} /* namespace drizzled */