~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
2318.7.19 by Olaf van der Spek
Refactor Items
32
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
33
2318.7.19 by Olaf van der Spek
Refactor Items
34
static char _dig_vec_lower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
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(),
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
69
       *ptr= end - min(str_value.length(), 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
2030.1.4 by Brian Aker
Change my_decimal to Decimal
78
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
79
{
80
  // following assert is redundant, because fixed=1 assigned in constructor
81
  assert(fixed == 1);
82
  uint64_t value= (uint64_t)val_int();
2030.1.3 by Brian Aker
Second pass through function names.
83
  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
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)
2318.7.19 by Olaf van der Spek
Refactor Items
91
    return field->store(str_value.ptr(), str_value.length(), collation.collation);
642.1.8 by Lee
move functions from item.cc/item.h to item directory
92
93
  uint64_t nr;
94
  uint32_t length= str_value.length();
95
  if (length > 8)
96
  {
97
    nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
98
    goto warn;
99
  }
100
  nr= (uint64_t) val_int();
101
  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
102
  {
103
    nr= INT64_MAX;
104
    goto warn;
105
  }
106
  return field->store((int64_t) nr, true);  // Assume hex numbers are unsigned
107
108
warn:
109
  if (!field->store((int64_t) nr, true))
2318.7.19 by Olaf van der Spek
Refactor Items
110
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
642.1.8 by Lee
move functions from item.cc/item.h to item directory
111
  return 1;
112
}
113
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
114
void Item_hex_string::print(String *str)
642.1.8 by Lee
move functions from item.cc/item.h to item directory
115
{
116
  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).
117
       *ptr= end - min(str_value.length(), sizeof(int64_t));
642.1.8 by Lee
move functions from item.cc/item.h to item directory
118
  str->append("0x");
119
  for (; ptr != end ; ptr++)
120
  {
121
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
122
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
123
  }
124
}
125
126
127
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
128
{
129
  if (arg->basic_const_item() && arg->type() == type())
130
  {
131
    if (binary_cmp)
132
      return !stringcmp(&str_value, &arg->str_value);
133
    return !sortcmp(&str_value, &arg->str_value, collation.collation);
134
  }
135
  return false;
136
}
137
2318.7.19 by Olaf van der Spek
Refactor Items
138
Item *Item_hex_string::safe_charset_converter(const charset_info_st* tocs)
642.1.8 by Lee
move functions from item.cc/item.h to item directory
139
{
140
  String tmp, *str= val_str(&tmp);
2318.7.14 by Olaf van der Spek
Refactor
141
  Item_string* conv= new Item_string(str->ptr(), str->length(), tocs);
642.1.8 by Lee
move functions from item.cc/item.h to item directory
142
  conv->str_value.copy();
143
  conv->str_value.mark_as_const();
144
  return conv;
145
}
146
147
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
148
} /* namespace drizzled */