~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/hex_string.cc

move functions from item.cc/item.h to item directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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 <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/error.h>
 
23
#include <drizzled/item/hex_string.h>
 
24
 
 
25
inline uint32_t char_val(char X)
 
26
{
 
27
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
 
28
                 X >= 'A' && X <= 'Z' ? X-'A'+10 :
 
29
                 X-'a'+10);
 
30
}
 
31
 
 
32
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
 
33
{
 
34
  max_length=(str_length+1)/2;
 
35
  char *ptr=(char*) sql_alloc(max_length+1);
 
36
  if (!ptr)
 
37
    return;
 
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
 
42
  while (ptr != end)
 
43
  {
 
44
    *ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
 
45
    str+=2;
 
46
  }
 
47
  *ptr=0;                                       // Keep purify happy
 
48
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
 
49
  fixed= 1;
 
50
  unsigned_flag= 1;
 
51
}
 
52
 
 
53
int64_t Item_hex_string::val_int()
 
54
{
 
55
  // following assert is redundant, because fixed=1 assigned in constructor
 
56
  assert(fixed == 1);
 
57
  char *end=(char*) str_value.ptr()+str_value.length(),
 
58
       *ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
 
59
 
 
60
  uint64_t value=0;
 
61
  for (; ptr != end ; ptr++)
 
62
    value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
 
63
  return (int64_t) value;
 
64
}
 
65
 
 
66
 
 
67
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
 
68
{
 
69
  // following assert is redundant, because fixed=1 assigned in constructor
 
70
  assert(fixed == 1);
 
71
  uint64_t value= (uint64_t)val_int();
 
72
  int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
 
73
  return (decimal_value);
 
74
}
 
75
 
 
76
int Item_hex_string::save_in_field(Field *field, bool)
 
77
{
 
78
  field->set_notnull();
 
79
  if (field->result_type() == STRING_RESULT)
 
80
    return field->store(str_value.ptr(), str_value.length(),
 
81
                        collation.collation);
 
82
 
 
83
  uint64_t nr;
 
84
  uint32_t length= str_value.length();
 
85
  if (length > 8)
 
86
  {
 
87
    nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
 
88
    goto warn;
 
89
  }
 
90
  nr= (uint64_t) val_int();
 
91
  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
 
92
  {
 
93
    nr= INT64_MAX;
 
94
    goto warn;
 
95
  }
 
96
  return field->store((int64_t) nr, true);  // Assume hex numbers are unsigned
 
97
 
 
98
warn:
 
99
  if (!field->store((int64_t) nr, true))
 
100
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
 
101
                       1);
 
102
  return 1;
 
103
}
 
104
 
 
105
void Item_hex_string::print(String *str, enum_query_type)
 
106
{
 
107
  char *end= (char*) str_value.ptr() + str_value.length(),
 
108
       *ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
 
109
  str->append("0x");
 
110
  for (; ptr != end ; ptr++)
 
111
  {
 
112
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
 
113
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
 
114
  }
 
115
}
 
116
 
 
117
 
 
118
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
 
119
{
 
120
  if (arg->basic_const_item() && arg->type() == type())
 
121
  {
 
122
    if (binary_cmp)
 
123
      return !stringcmp(&str_value, &arg->str_value);
 
124
    return !sortcmp(&str_value, &arg->str_value, collation.collation);
 
125
  }
 
126
  return false;
 
127
}
 
128
 
 
129
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
 
130
{
 
131
  Item_string *conv;
 
132
  String tmp, *str= val_str(&tmp);
 
133
 
 
134
  if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
 
135
    return NULL;
 
136
  conv->str_value.copy();
 
137
  conv->str_value.mark_as_const();
 
138
  return conv;
 
139
}
 
140
 
 
141