~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/hex_string.cc

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

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