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
20
#include <drizzled/server_includes.h>
22
#include <drizzled/error.h>
23
#include <drizzled/item/hex_string.h>
25
inline uint32_t char_val(char X)
27
return (uint) (X >= '0' && X <= '9' ? X-'0' :
28
X >= 'A' && X <= 'Z' ? X-'A'+10 :
32
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
34
max_length=(str_length+1)/2;
35
char *ptr=(char*) sql_alloc(max_length+1);
38
str_value.set(ptr,max_length,&my_charset_bin);
39
char *end=ptr+max_length;
40
if (max_length*2 != str_length)
41
*ptr++=char_val(*str++); // Not even, assume 0 prefix
44
*ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
47
*ptr=0; // Keep purify happy
48
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
53
int64_t Item_hex_string::val_int()
55
// following assert is redundant, because fixed=1 assigned in constructor
57
char *end=(char*) str_value.ptr()+str_value.length(),
58
*ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
61
for (; ptr != end ; ptr++)
62
value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
63
return (int64_t) value;
67
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
69
// following assert is redundant, because fixed=1 assigned in constructor
71
uint64_t value= (uint64_t)val_int();
72
int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
73
return (decimal_value);
76
int Item_hex_string::save_in_field(Field *field, bool)
79
if (field->result_type() == STRING_RESULT)
80
return field->store(str_value.ptr(), str_value.length(),
84
uint32_t length= str_value.length();
87
nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
90
nr= (uint64_t) val_int();
91
if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
96
return field->store((int64_t) nr, true); // Assume hex numbers are unsigned
99
if (!field->store((int64_t) nr, true))
100
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
105
void Item_hex_string::print(String *str, enum_query_type)
107
char *end= (char*) str_value.ptr() + str_value.length(),
108
*ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
110
for (; ptr != end ; ptr++)
112
str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
113
str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
118
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
120
if (arg->basic_const_item() && arg->type() == type())
123
return !stringcmp(&str_value, &arg->str_value);
124
return !sortcmp(&str_value, &arg->str_value, collation.collation);
129
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
132
String tmp, *str= val_str(&tmp);
134
if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
136
conv->str_value.copy();
137
conv->str_value.mark_as_const();