~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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
27
#include <config.h>
584.4.10 by Monty Taylor
Broke out cached_item.
28
#include <drizzled/cached_item.h>
2234.1.3 by Olaf van der Spek
Refactor includes
29
#include <drizzled/field.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
30
#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.
31
#include <drizzled/session.h>
2198.1.2 by Olaf van der Spek
Refactor includes
32
#include <drizzled/item/field.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
33
#include <drizzled/system_variables.h>
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
34
#include <algorithm>
35
36
using namespace std;
1 by brian
clean slate
37
2241.3.2 by Olaf van der Spek
Refactor Session::variables
38
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
39
1 by brian
clean slate
40
/**
41
  Create right type of Cached_item for an item.
42
*/
43
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.
44
Cached_item *new_Cached_item(Session *session, Item *item)
1 by brian
clean slate
45
{
46
  if (item->real_item()->type() == Item::FIELD_ITEM &&
47
      !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
48
  {
49
    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.
50
    Field *cached_field= real_item->field;
1 by brian
clean slate
51
    return new Cached_item_field(cached_field);
52
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
53
1 by brian
clean slate
54
  switch (item->result_type()) {
55
  case STRING_RESULT:
520.1.22 by Brian Aker
Second pass of thd cleanup
56
    return new Cached_item_str(session, (Item_field *) item);
1 by brian
clean slate
57
  case INT_RESULT:
58
    return new Cached_item_int((Item_field *) item);
59
  case REAL_RESULT:
60
    return new Cached_item_real(item);
61
  case DECIMAL_RESULT:
62
    return new Cached_item_decimal(item);
63
  case ROW_RESULT:
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
64
    assert(0);
1 by brian
clean slate
65
    return 0;
66
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
67
68
  abort();
1 by brian
clean slate
69
}
70
71
Cached_item::~Cached_item() {}
72
73
/**
74
  Compare with old value and replace value with new value.
75
76
  @return
77
    Return true if values have changed
78
*/
79
520.1.22 by Brian Aker
Second pass of thd cleanup
80
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.
81
  :item(arg), value(min(arg->max_length,
82
                        (uint32_t)session->variables.max_sort_length))
1 by brian
clean slate
83
{}
84
85
bool Cached_item_str::cmp(void)
86
{
87
  String *res;
88
  bool tmp;
89
90
  if ((res=item->val_str(&tmp_value)))
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
91
    res->length(min(res->length(), value.alloced_length()));
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
92
1 by brian
clean slate
93
  if (null_value != item->null_value)
94
  {
95
    if ((null_value= item->null_value))
584.4.10 by Monty Taylor
Broke out cached_item.
96
      // New value was null
97
      return(true);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
98
    tmp=true;
1 by brian
clean slate
99
  }
100
  else if (null_value)
584.4.10 by Monty Taylor
Broke out cached_item.
101
    // new and old value was null
102
    return(0);
1 by brian
clean slate
103
  else
104
    tmp= sortcmp(&value,res,item->collation.collation) != 0;
105
  if (tmp)
584.4.10 by Monty Taylor
Broke out cached_item.
106
    // Remember for next cmp
107
    value.copy(*res);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
108
  return(tmp);
1 by brian
clean slate
109
}
110
111
Cached_item_str::~Cached_item_str()
112
{
584.4.10 by Monty Taylor
Broke out cached_item.
113
  // Safety
114
  item=0;
1 by brian
clean slate
115
}
116
117
bool Cached_item_real::cmp(void)
118
{
119
  double nr= item->val_real();
120
  if (null_value != item->null_value || nr != value)
121
  {
122
    null_value= item->null_value;
123
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
124
    return(true);
1 by brian
clean slate
125
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
126
  return(false);
1 by brian
clean slate
127
}
128
129
bool Cached_item_int::cmp(void)
130
{
152 by Brian Aker
longlong replacement
131
  int64_t nr=item->val_int();
1 by brian
clean slate
132
  if (null_value != item->null_value || nr != value)
133
  {
134
    null_value= item->null_value;
135
    value=nr;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
136
    return(true);
1 by brian
clean slate
137
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
138
  return(false);
1 by brian
clean slate
139
}
140
141
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
142
Cached_item_field::Cached_item_field(Field *arg_field) 
143
  : 
144
    field(arg_field)
145
{
146
  /* TODO: take the memory allocation below out of the constructor. */
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
147
  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.
148
}
149
1 by brian
clean slate
150
bool Cached_item_field::cmp(void)
151
{
584.4.10 by Monty Taylor
Broke out cached_item.
152
  // This is not a blob!
1996.2.1 by Brian Aker
uuid type code.
153
  bool tmp= field->cmp_internal(buff) != 0;
584.4.10 by Monty Taylor
Broke out cached_item.
154
1 by brian
clean slate
155
  if (tmp)
156
    field->get_image(buff,length,field->charset());
157
  if (null_value != field->is_null())
158
  {
159
    null_value= !null_value;
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
160
    tmp=true;
1 by brian
clean slate
161
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
162
  return(tmp);
1 by brian
clean slate
163
}
164
165
166
Cached_item_decimal::Cached_item_decimal(Item *it)
167
  :item(it)
168
{
2034.2.4 by Brian Aker
Further encapsulation for DECIMAL.
169
  value.set_zero();
1 by brian
clean slate
170
}
171
172
173
bool Cached_item_decimal::cmp()
174
{
2030.1.4 by Brian Aker
Change my_decimal to Decimal
175
  type::Decimal tmp;
176
  type::Decimal *ptmp= item->val_decimal(&tmp);
1 by brian
clean slate
177
  if (null_value != item->null_value ||
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
178
      (!item->null_value && class_decimal_cmp(&value, ptmp)))
1 by brian
clean slate
179
  {
180
    null_value= item->null_value;
181
    /* Save only not null values */
182
    if (!null_value)
183
    {
2030.1.3 by Brian Aker
Second pass through function names.
184
      class_decimal2decimal(ptmp, &value);
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
185
      return true;
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
  }
51.1.14 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
189
  return false;
1 by brian
clean slate
190
}
191
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
192
} /* namespace drizzled */