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
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
21
#include <drizzled/session.h>
22
#include "drizzled/internal/m_string.h"
27
/** Get the value of a variable as a double. */
29
double user_var_entry::val_real(bool *null_value)
31
if ((*null_value= (value == 0)))
36
return *(double*) value;
39
return (double) *(int64_t*) value;
44
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
49
return internal::my_atof(value); // This is null terminated
52
assert(1); // Impossible
55
return 0.0; // Impossible
59
/** Get the value of a variable as an integer. */
61
int64_t user_var_entry::val_int(bool *null_value) const
63
if ((*null_value= (value == 0)))
68
return (int64_t) *(double*) value;
71
return *(int64_t*) value;
76
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
83
return internal::my_strtoll10(value, (char**) 0, &error);// String is null terminated
87
assert(1); // Impossible
91
return 0L; // Impossible
95
/** Get the value of a variable as a string. */
97
String *user_var_entry::val_str(bool *null_value, String *str,
100
if ((*null_value= (value == 0)))
105
str->set_real(*(double*) value, decimals, &my_charset_bin);
110
str->set(*(int64_t*) value, &my_charset_bin);
112
str->set(*(uint64_t*) value, &my_charset_bin);
116
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
120
if (str->copy(value, length, collation.collation))
124
assert(1); // Impossible
131
/** Get the value of a variable as a decimal. */
133
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
135
if ((*null_value= (value == 0)))
140
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
144
int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
148
val= (my_decimal *)value;
152
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
156
assert(1); // Impossible
164
Set value to user variable.
166
@param entry pointer to structure representing variable
167
@param set_null should we set NULL value ?
168
@param ptr pointer to buffer with new value
169
@param length length of new value
170
@param type type of new value
171
@param cs charset info for new value
172
@param dv derivation for new value
173
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
175
@note Sets error and fatal error if allocation fails.
183
bool user_var_entry::update_hash(bool set_null, void *ptr, uint32_t arg_length,
184
Item_result arg_type, const CHARSET_INFO * const cs, Derivation dv,
191
assert(length && size);
200
size_t needed_size= arg_length + ((arg_type == STRING_RESULT) ? 1 : 0);
202
if (needed_size > size)
206
new_ptr= (char *)realloc(value, needed_size);
215
if (arg_type == STRING_RESULT)
216
value[arg_length]= 0; // Store end \0
218
memcpy(value, ptr, arg_length);
219
if (arg_type == DECIMAL_RESULT)
220
((my_decimal*)value)->fix_buffer_pointer();
222
collation.set(cs, dv);
223
unsigned_flag= unsigned_arg;
230
} /* namespace drizzled */