~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <drizzled/session.h>
#include <drizzled/error.h>
#include <drizzled/item/string.h>

namespace drizzled {

Item *Item_string::safe_charset_converter(const charset_info_st * const tocs)
{
  String tmp, cstr, *ostr= val_str(&tmp);
  cstr.copy(ostr->ptr(), ostr->length(), tocs);
  Item_string* conv= new Item_string(cstr.ptr(), cstr.length(), cstr.charset(), collation.derivation);
  char* ptr= getSession().mem.strdup(cstr);

  conv->str_value.set(ptr, cstr.length(), cstr.charset());
  /* Ensure that no one is going to change the result string */
  conv->str_value.mark_as_const();
  return conv;
}


Item *Item_static_string_func::safe_charset_converter(const charset_info_st * const tocs)
{
  String tmp, cstr, *ostr= val_str(&tmp);
  cstr.copy(ostr->ptr(), ostr->length(), tocs);
  Item_string* conv= new Item_static_string_func(func_name, cstr, cstr.charset(), collation.derivation);
  conv->str_value.copy();
  /* Ensure that no one is going to change the result string */
  conv->str_value.mark_as_const();
  return conv;
}


bool Item_string::eq(const Item *item, bool binary_cmp) const
{
  if (type() == item->type() && item->basic_const_item())
  {
    if (binary_cmp)
      return !stringcmp(&str_value, &item->str_value);
    return (collation.collation == item->collation.collation &&
            !sortcmp(&str_value, &item->str_value, collation.collation));
  }
  return 0;
}

void Item_string::print(String *str)
{
  str->append('\'');
  str_value.print(*str);
  str->append('\'');
}

double Item_string::val_real()
{
  assert(fixed == 1);
  int error;
  char *end;
  const charset_info_st* cs= str_value.charset();

  char* org_end= (char*) str_value.ptr() + str_value.length();
  double tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end, &error);
  if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
  {
    /*
      We can use str_value.ptr() here as Item_string is gurantee to put an
      end \0 here.
    */
    push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_TRUNCATED_WRONG_VALUE, ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE", str_value.ptr());
  }
  return tmp;
}

/**
  @todo
  Give error if we wanted a signed integer and we got an unsigned one
*/
int64_t Item_string::val_int()
{
  assert(fixed == 1);
  int err;
  char *end= (char*) str_value.ptr()+ str_value.length();
  char *org_end= end;
  const charset_info_st * const cs= str_value.charset();

  int64_t tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
  /*
    TODO: Give error if we wanted a signed integer and we got an unsigned
    one
  */
  if (err > 0 || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
  {
    push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_TRUNCATED_WRONG_VALUE, ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER", str_value.ptr());
  }
  return tmp;
}

type::Decimal *Item_string::val_decimal(type::Decimal *decimal_value)
{
  return val_decimal_from_string(decimal_value);
}

int Item_string::save_in_field(Field *field, bool)
{
  String* result=val_str(&str_value);
  return save_str_value_in_field(field, result);
}


} /* namespace drizzled */