1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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.
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.
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
22
#include <drizzled/session.h>
23
#include <drizzled/internal/m_string.h>
24
#include <drizzled/user_var_entry.h>
29
/** Get the value of a variable as a double. */
31
double user_var_entry::val_real(bool *null_value)
33
if ((*null_value= (value == 0)))
38
return *(double*) value;
41
return (double) *(int64_t*) value;
46
class_decimal2double(E_DEC_FATAL_ERROR, (type::Decimal *)value, &result);
51
return internal::my_atof(value); // This is null terminated
54
assert(1); // Impossible
57
return 0.0; // Impossible
61
/** Get the value of a variable as an integer. */
63
int64_t user_var_entry::val_int(bool *null_value) const
65
if ((*null_value= (value == 0)))
70
return (int64_t) *(double*) value;
73
return *(int64_t*) value;
78
((type::Decimal *)(value))->val_int32(E_DEC_FATAL_ERROR, 0, &result);
85
return internal::my_strtoll10(value, (char**) 0, &error);// String is null terminated
89
assert(1); // Impossible
93
return 0L; // Impossible
97
/** Get the value of a variable as a string. */
99
String *user_var_entry::val_str(bool *null_value, String *str,
102
if ((*null_value= (value == 0)))
107
str->set_real(*(double*) value, decimals, &my_charset_bin);
112
str->set(*(int64_t*) value, &my_charset_bin);
114
str->set(*(uint64_t*) value, &my_charset_bin);
118
class_decimal2string((type::Decimal *)value, 0, str);
122
if (str->copy(value, length, collation.collation))
126
assert(1); // Impossible
133
/** Get the value of a variable as a decimal. */
135
type::Decimal *user_var_entry::val_decimal(bool *null_value, type::Decimal *val)
137
if ((*null_value= (value == 0)))
142
double2_class_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
146
int2_class_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
150
val= (type::Decimal *)value;
154
val->store(E_DEC_FATAL_ERROR, value, length, collation.collation);
158
assert(1); // Impossible
166
Set value to user variable.
168
@param entry pointer to structure representing variable
169
@param set_null should we set NULL value ?
170
@param ptr pointer to buffer with new value
171
@param length length of new value
172
@param type type of new value
173
@param cs charset info for new value
174
@param dv derivation for new value
175
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
177
@note Sets error and fatal error if allocation fails.
185
bool user_var_entry::update_hash(bool set_null, void *ptr, uint32_t arg_length,
186
Item_result arg_type, const CHARSET_INFO * const cs, Derivation dv,
193
assert(length && size);
202
size_t needed_size= arg_length + ((arg_type == STRING_RESULT) ? 1 : 0);
204
if (needed_size > size)
208
new_ptr= (char *)realloc(value, needed_size);
217
if (arg_type == STRING_RESULT)
218
value[arg_length]= 0; // Store end \0
220
memcpy(value, ptr, arg_length);
221
if (arg_type == DECIMAL_RESULT)
222
((type::Decimal*)value)->fix_buffer_pointer();
224
collation.set(cs, dv);
225
unsigned_flag= unsigned_arg;
232
} /* namespace drizzled */