Drizzled Public API Documentation

user_var_entry.cc
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 
20 #include <config.h>
21 
22 #include <drizzled/session.h>
23 #include <drizzled/internal/m_string.h>
24 #include <drizzled/user_var_entry.h>
25 #include <drizzled/type/decimal.h>
26 #include <drizzled/charset.h>
27 
28 namespace drizzled {
29 
32 double user_var_entry::val_real(bool *null_value) const
33 {
34  if ((*null_value= not value))
35  return 0.0;
36 
37  switch (type)
38  {
39  case REAL_RESULT:
40  return *(double*) value;
41 
42  case INT_RESULT:
43  return (double) *(int64_t*) value;
44 
45  case DECIMAL_RESULT:
46  {
47  double result;
48  class_decimal2double(E_DEC_FATAL_ERROR, (type::Decimal *)value, &result);
49  return result;
50  }
51 
52  case STRING_RESULT:
53  return internal::my_atof(value); // This is null terminated
54 
55  case ROW_RESULT:
56  assert(false); // Impossible
57  break;
58  }
59  return 0.0; // Impossible
60 }
61 
62 
65 int64_t user_var_entry::val_int(bool *null_value) const
66 {
67  if ((*null_value= not value))
68  return 0;
69 
70  switch (type)
71  {
72  case REAL_RESULT:
73  return (int64_t) *(double*) value;
74 
75  case INT_RESULT:
76  return *(int64_t*) value;
77 
78  case DECIMAL_RESULT:
79  {
80  int64_t result;
81  ((type::Decimal *)(value))->val_int32(E_DEC_FATAL_ERROR, 0, &result);
82  return result;
83  }
84 
85  case STRING_RESULT:
86  {
87  int error;
88  return internal::my_strtoll10(value, (char**) 0, &error);// String is null terminated
89  }
90 
91  case ROW_RESULT:
92  assert(false); // Impossible
93  break;
94  }
95 
96  assert(false);
97  return 0; // Impossible
98 }
99 
100 
103 String *user_var_entry::val_str(bool *null_value, String *str, uint32_t decimals) const
104 {
105  if ((*null_value= not value))
106  return NULL;
107 
108  switch (type)
109  {
110  case REAL_RESULT:
111  str->set_real(*(double*) value, decimals, &my_charset_bin);
112  break;
113 
114  case INT_RESULT:
115  if (!unsigned_flag)
116  str->set(*(int64_t*) value, &my_charset_bin);
117  else
118  str->set(*(uint64_t*) value, &my_charset_bin);
119  break;
120 
121  case DECIMAL_RESULT:
122  class_decimal2string((type::Decimal *)value, 0, str);
123  break;
124 
125  case STRING_RESULT:
126  str->copy(value, length, collation.collation);
127  break;
128 
129  case ROW_RESULT:
130  assert(false); // Impossible
131  break;
132  }
133 
134  return str;
135 }
136 
140 {
141  if ((*null_value= not value))
142  return 0;
143 
144  switch (type)
145  {
146  case REAL_RESULT:
147  double2_class_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
148  break;
149 
150  case INT_RESULT:
151  int2_class_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
152  break;
153 
154  case DECIMAL_RESULT:
155  val= (type::Decimal *)value;
156  break;
157 
158  case STRING_RESULT:
159  val->store(E_DEC_FATAL_ERROR, value, length, collation.collation);
160  break;
161 
162  case ROW_RESULT:
163  assert(false); // Impossible
164  break;
165  }
166 
167  return val;
168 }
169 
190 void user_var_entry::update_hash(bool set_null, data_ref data, Item_result arg_type, const charset_info_st* cs, Derivation dv, bool unsigned_arg)
191 {
192  if (set_null)
193  {
194  if (value)
195  {
196  assert(length && size);
197  free(value);
198  value= NULL;
199  length= 0;
200  size= 0;
201  }
202  }
203  else
204  {
205  size_t needed_size= data.size() + ((arg_type == STRING_RESULT) ? 1 : 0);
206 
207  if (needed_size > size)
208  {
209  value= (char *)realloc(value, needed_size);
210  size= needed_size;
211  }
212 
213  if (arg_type == STRING_RESULT)
214  value[data.size()]= 0; // Store end \0
215 
216  memcpy(value, data.data(), data.size());
217  if (arg_type == DECIMAL_RESULT)
218  ((type::Decimal*)value)->fix_buffer_pointer();
219  length= data.size();
220  collation.set(cs, dv);
221  unsigned_flag= unsigned_arg;
222  }
223  type= arg_type;
224 }
225 
226 } /* namespace drizzled */