Drizzled Public API Documentation

cached_item.cc
Go to the documentation of this file.
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, Inc.
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  */
19 
27 #include <config.h>
28 #include <drizzled/cached_item.h>
29 #include <drizzled/field.h>
30 #include <drizzled/sql_string.h>
31 #include <drizzled/session.h>
32 #include <drizzled/item/field.h>
33 #include <drizzled/system_variables.h>
34 #include <algorithm>
35 
36 using namespace std;
37 
38 namespace drizzled {
39 
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();
50  Field *cached_field= real_item->field;
51  return new Cached_item_field(cached_field);
52  }
53 
54  switch (item->result_type()) {
55  case STRING_RESULT:
56  return new Cached_item_str(session, (Item_field *) item);
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:
64  assert(0);
65  return 0;
66  }
67 
68  abort();
69 }
70 
71 Cached_item::~Cached_item() {}
72 
80 Cached_item_str::Cached_item_str(Session *session, Item *arg)
81  :item(arg), value(min(arg->max_length,
82  (uint32_t)session->variables.max_sort_length))
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)))
91  res->length(min(res->length(), value.alloced_length()));
92 
93  if (null_value != item->null_value)
94  {
95  if ((null_value= item->null_value))
96  // New value was null
97  return true;
98  tmp=true;
99  }
100  else if (null_value)
101  // new and old value was null
102  return 0;
103  else
104  tmp= sortcmp(&value,res,item->collation.collation) != 0;
105  if (tmp)
106  // Remember for next cmp
107  value.copy(*res);
108  return(tmp);
109 }
110 
111 Cached_item_str::~Cached_item_str()
112 {
113  // Safety
114  item=0;
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;
124  return true;
125  }
126  return false;
127 }
128 
129 bool Cached_item_int::cmp(void)
130 {
131  int64_t nr=item->val_int();
132  if (null_value != item->null_value || nr != value)
133  {
134  null_value= item->null_value;
135  value=nr;
136  return true;
137  }
138  return false;
139 }
140 
141 
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. */
147  buff= (unsigned char*) memory::sql_calloc(length= field->pack_length());
148 }
149 
150 bool Cached_item_field::cmp(void)
151 {
152  // This is not a blob!
153  bool tmp= field->cmp_internal(buff) != 0;
154 
155  if (tmp)
156  field->get_image(buff,length,field->charset());
157  if (null_value != field->is_null())
158  {
159  null_value= !null_value;
160  tmp=true;
161  }
162  return(tmp);
163 }
164 
165 
166 Cached_item_decimal::Cached_item_decimal(Item *it)
167  :item(it)
168 {
169  value.set_zero();
170 }
171 
172 
173 bool Cached_item_decimal::cmp()
174 {
175  type::Decimal tmp;
176  type::Decimal *ptmp= item->val_decimal(&tmp);
177  if (null_value != item->null_value ||
178  (!item->null_value && class_decimal_cmp(&value, ptmp)))
179  {
180  null_value= item->null_value;
181  /* Save only not null values */
182  if (!null_value)
183  {
184  class_decimal2decimal(ptmp, &value);
185  return true;
186  }
187  return false;
188  }
189  return false;
190 }
191 
192 } /* namespace drizzled */
Cached_item * new_Cached_item(Session *session, Item *item)
Definition: cached_item.cc:44
virtual int64_t val_int()=0
int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)
Definition: decimal.h:425
bool null_value
Definition: item.h:122
virtual double val_real()=0
virtual String * val_str(String *str)=0
virtual type::Decimal * val_decimal(type::Decimal *decimal_buffer)=0