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
22
#include <drizzled/error.h>
23
#include <drizzled/item/string.h>
24
#include <drizzled/item/hex_string.h>
33
static char _dig_vec_lower[] =
34
"0123456789abcdefghijklmnopqrstuvwxyz";
36
inline uint32_t char_val(char X)
38
return (uint32_t) (X >= '0' && X <= '9' ? X-'0' :
39
X >= 'A' && X <= 'Z' ? X-'A'+10 :
43
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
45
max_length=(str_length+1)/2;
46
char *ptr=(char*) memory::sql_alloc(max_length+1);
49
str_value.set(ptr,max_length,&my_charset_bin);
50
char *end=ptr+max_length;
51
if (max_length*2 != str_length)
52
*ptr++=char_val(*str++); // Not even, assume 0 prefix
55
*ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
58
*ptr=0; // Keep purify happy
59
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
64
int64_t Item_hex_string::val_int()
66
// following assert is redundant, because fixed=1 assigned in constructor
68
char *end= (char*) str_value.ptr()+str_value.length(),
69
*ptr= end - min(str_value.length(),(uint32_t)sizeof(int64_t));
72
for (; ptr != end ; ptr++)
73
value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
74
return (int64_t) value;
78
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
80
// following assert is redundant, because fixed=1 assigned in constructor
82
uint64_t value= (uint64_t)val_int();
83
int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
84
return (decimal_value);
87
int Item_hex_string::save_in_field(Field *field, bool)
90
if (field->result_type() == STRING_RESULT)
91
return field->store(str_value.ptr(), str_value.length(),
95
uint32_t length= str_value.length();
98
nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
101
nr= (uint64_t) val_int();
102
if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
107
return field->store((int64_t) nr, true); // Assume hex numbers are unsigned
110
if (!field->store((int64_t) nr, true))
111
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
116
void Item_hex_string::print(String *str, enum_query_type)
118
char *end= (char*) str_value.ptr() + str_value.length(),
119
*ptr= end - min(str_value.length(), (uint32_t)sizeof(int64_t));
121
for (; ptr != end ; ptr++)
123
str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
124
str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
129
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
131
if (arg->basic_const_item() && arg->type() == type())
134
return !stringcmp(&str_value, &arg->str_value);
135
return !sortcmp(&str_value, &arg->str_value, collation.collation);
140
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
143
String tmp, *str= val_str(&tmp);
145
if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
147
conv->str_value.copy();
148
conv->str_value.mark_as_const();
153
} /* namespace drizzled */