~drizzle-trunk/drizzle/development

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