~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
642.1.8 by Lee
move functions from item.cc/item.h to item directory
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
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>
2154.2.4 by Brian Aker
This fixes 716459
23
#include <drizzled/field.h>
24
#include <drizzled/item/hex_string.h>
642.1.63 by Lee
more header file cleanup
25
#include <drizzled/item/string.h>
2154.2.4 by Brian Aker
This fixes 716459
26
#include <drizzled/type/decimal.h>
642.1.8 by Lee
move functions from item.cc/item.h to item directory
27
1067.4.3 by Nathan Williams
All files in drizzled/item/ now use std::min instead of cmin.
28
#include <algorithm>
29
30
using namespace std;
31
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
namespace drizzled
33
{
34
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
35
static char _dig_vec_lower[] =
36
  "0123456789abcdefghijklmnopqrstuvwxyz";
37
642.1.8 by Lee
move functions from item.cc/item.h to item directory
38
inline uint32_t char_val(char X)
39
{
895 by Brian Aker
Completion (?) of uint conversion.
40
  return (uint32_t) (X >= '0' && X <= '9' ? X-'0' :
642.1.8 by Lee
move functions from item.cc/item.h to item directory
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;
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
48
  char *ptr=(char*) memory::sql_alloc(max_length+1);
642.1.8 by Lee
move functions from item.cc/item.h to item directory
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);
1067.4.3 by Nathan Williams
All files in drizzled/item/ now use std::min instead of cmin.
70
  char *end= (char*) str_value.ptr()+str_value.length(),
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
71
       *ptr= end - min(str_value.length(), sizeof(int64_t));
642.1.8 by Lee
move functions from item.cc/item.h to item directory
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
2030.1.4 by Brian Aker
Change my_decimal to Decimal
80
type::Decimal *Item_hex_string::val_decimal(type::Decimal *decimal_value)
642.1.8 by Lee
move functions from item.cc/item.h to item directory
81
{
82
  // following assert is redundant, because fixed=1 assigned in constructor
83
  assert(fixed == 1);
84
  uint64_t value= (uint64_t)val_int();
2030.1.3 by Brian Aker
Second pass through function names.
85
  int2_class_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
642.1.8 by Lee
move functions from item.cc/item.h to item directory
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(),
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
121
       *ptr= end - min(str_value.length(), sizeof(int64_t));
642.1.8 by Lee
move functions from item.cc/item.h to item directory
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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
155
} /* namespace drizzled */