~drizzle-trunk/drizzle/development

642.1.6 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.6 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
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.6 by Lee
move functions from item.cc/item.h to item directory
22
#include <drizzled/session.h>
23
#include <drizzled/error.h>
670.4.1 by Monty Taylor
Removed recursive subdirs in drizzled/, which allowed the renaming of ifloat and istring. It's also faster.
24
#include <drizzled/item/string.h>
642.1.6 by Lee
move functions from item.cc/item.h to item directory
25
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
26
namespace drizzled
27
{
28
642.1.6 by Lee
move functions from item.cc/item.h to item directory
29
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
30
{
31
  Item_string *conv;
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
32
  size_t conv_errors;
642.1.6 by Lee
move functions from item.cc/item.h to item directory
33
  char *ptr;
34
  String tmp, cstr, *ostr= val_str(&tmp);
35
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
36
  if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(),
37
                                             cstr.charset(),
38
                                             collation.derivation)))
39
  {
40
    /*
41
      Safe conversion is not possible (or EOM).
42
      We could not convert a string into the requested character set
43
      without data loss. The target charset does not cover all the
44
      characters from the string. Operation cannot be done correctly.
45
    */
46
    return NULL;
47
  }
48
  if (!(ptr= current_session->strmake(cstr.ptr(), cstr.length())))
49
    return NULL;
50
  conv->str_value.set(ptr, cstr.length(), cstr.charset());
51
  /* Ensure that no one is going to change the result string */
52
  conv->str_value.mark_as_const();
53
  return conv;
54
}
55
56
57
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
58
{
59
  Item_string *conv;
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
60
  size_t conv_errors;
642.1.6 by Lee
move functions from item.cc/item.h to item directory
61
  String tmp, cstr, *ostr= val_str(&tmp);
62
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
63
  if (conv_errors ||
64
      !(conv= new Item_static_string_func(func_name,
65
                                          cstr.ptr(), cstr.length(),
66
                                          cstr.charset(),
67
                                          collation.derivation)))
68
  {
69
    /*
70
      Safe conversion is not possible (or EOM).
71
      We could not convert a string into the requested character set
72
      without data loss. The target charset does not cover all the
73
      characters from the string. Operation cannot be done correctly.
74
    */
75
    return NULL;
76
  }
77
  conv->str_value.copy();
78
  /* Ensure that no one is going to change the result string */
79
  conv->str_value.mark_as_const();
80
  return conv;
81
}
82
83
84
bool Item_string::eq(const Item *item, bool binary_cmp) const
85
{
86
  if (type() == item->type() && item->basic_const_item())
87
  {
88
    if (binary_cmp)
89
      return !stringcmp(&str_value, &item->str_value);
90
    return (collation.collation == item->collation.collation &&
91
            !sortcmp(&str_value, &item->str_value, collation.collation));
92
  }
93
  return 0;
94
}
95
96
void Item_string::print(String *str, enum_query_type query_type)
97
{
98
  if (query_type == QT_ORDINARY && is_cs_specified())
99
  {
100
    str->append('_');
101
    str->append(collation.collation->csname);
102
  }
103
104
  str->append('\'');
105
1054.2.11 by Monty Taylor
Removed copy_and_convert.
106
  str_value.print(str);
642.1.6 by Lee
move functions from item.cc/item.h to item directory
107
108
  str->append('\'');
109
}
110
111
double Item_string::val_real()
112
{
113
  assert(fixed == 1);
114
  int error;
115
  char *end, *org_end;
116
  double tmp;
117
  const CHARSET_INFO * const cs= str_value.charset();
118
119
  org_end= (char*) str_value.ptr() + str_value.length();
120
  tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
121
                  &error);
122
  if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
123
  {
124
    /*
125
      We can use str_value.ptr() here as Item_string is gurantee to put an
126
      end \0 here.
127
    */
128
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
129
                        ER_TRUNCATED_WRONG_VALUE,
130
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
131
                        str_value.ptr());
132
  }
133
  return tmp;
134
}
135
136
/**
137
  @todo
138
  Give error if we wanted a signed integer and we got an unsigned one
139
*/
140
int64_t Item_string::val_int()
141
{
142
  assert(fixed == 1);
143
  int err;
144
  int64_t tmp;
145
  char *end= (char*) str_value.ptr()+ str_value.length();
146
  char *org_end= end;
147
  const CHARSET_INFO * const cs= str_value.charset();
148
149
  tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
150
  /*
151
    TODO: Give error if we wanted a signed integer and we got an unsigned
152
    one
153
  */
154
  if (err > 0 ||
155
      (end != org_end && !check_if_only_end_space(cs, end, org_end)))
156
  {
157
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
158
                        ER_TRUNCATED_WRONG_VALUE,
159
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
160
                        str_value.ptr());
161
  }
162
  return tmp;
163
}
164
2030.1.4 by Brian Aker
Change my_decimal to Decimal
165
type::Decimal *Item_string::val_decimal(type::Decimal *decimal_value)
642.1.6 by Lee
move functions from item.cc/item.h to item directory
166
{
167
  return val_decimal_from_string(decimal_value);
168
}
169
170
int Item_string::save_in_field(Field *field, bool)
171
{
172
  String *result;
173
  result=val_str(&str_value);
174
  return save_str_value_in_field(field, result);
175
}
176
177
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
178
} /* namespace drizzled */