Drizzled Public API Documentation

string.cc
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, Inc.
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 
20 #include <config.h>
21 
22 #include <drizzled/session.h>
23 #include <drizzled/error.h>
24 #include <drizzled/item/string.h>
25 
26 namespace drizzled {
27 
28 Item *Item_string::safe_charset_converter(const charset_info_st * const tocs)
29 {
30  String tmp, cstr, *ostr= val_str(&tmp);
31  cstr.copy(ostr->ptr(), ostr->length(), tocs);
32  Item_string* conv= new Item_string(cstr.ptr(), cstr.length(), cstr.charset(), collation.derivation);
33  char* ptr= getSession().mem.strdup(cstr);
34 
35  conv->str_value.set(ptr, cstr.length(), cstr.charset());
36  /* Ensure that no one is going to change the result string */
37  conv->str_value.mark_as_const();
38  return conv;
39 }
40 
41 
42 Item *Item_static_string_func::safe_charset_converter(const charset_info_st * const tocs)
43 {
44  String tmp, cstr, *ostr= val_str(&tmp);
45  cstr.copy(ostr->ptr(), ostr->length(), tocs);
46  Item_string* conv= new Item_static_string_func(func_name, cstr, cstr.charset(), collation.derivation);
47  conv->str_value.copy();
48  /* Ensure that no one is going to change the result string */
49  conv->str_value.mark_as_const();
50  return conv;
51 }
52 
53 
54 bool Item_string::eq(const Item *item, bool binary_cmp) const
55 {
56  if (type() == item->type() && item->basic_const_item())
57  {
58  if (binary_cmp)
59  return !stringcmp(&str_value, &item->str_value);
60  return (collation.collation == item->collation.collation &&
61  !sortcmp(&str_value, &item->str_value, collation.collation));
62  }
63  return 0;
64 }
65 
67 {
68  str->append('\'');
69  str_value.print(*str);
70  str->append('\'');
71 }
72 
74 {
75  assert(fixed == 1);
76  int error;
77  char *end;
78  const charset_info_st* cs= str_value.charset();
79 
80  char* org_end= (char*) str_value.ptr() + str_value.length();
81  double tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end, &error);
82  if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
83  {
84  /*
85  We can use str_value.ptr() here as Item_string is gurantee to put an
86  end \0 here.
87  */
88  push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_TRUNCATED_WRONG_VALUE, ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE", str_value.ptr());
89  }
90  return tmp;
91 }
92 
98 {
99  assert(fixed == 1);
100  int err;
101  char *end= (char*) str_value.ptr()+ str_value.length();
102  char *org_end= end;
103  const charset_info_st * const cs= str_value.charset();
104 
105  int64_t tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
106  /*
107  TODO: Give error if we wanted a signed integer and we got an unsigned
108  one
109  */
110  if (err > 0 || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
111  {
112  push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_TRUNCATED_WRONG_VALUE, ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER", str_value.ptr());
113  }
114  return tmp;
115 }
116 
118 {
119  return val_decimal_from_string(decimal_value);
120 }
121 
122 int Item_string::save_in_field(Field *field, bool)
123 {
124  String* result=val_str(&str_value);
125  return save_str_value_in_field(field, result);
126 }
127 
128 
129 } /* namespace drizzled */
virtual bool basic_const_item() const
Definition: item.h:474
bool fixed
Definition: item.h:120
TODO: Rename this file - func.h is stupid.
int64_t val_int()
Definition: string.cc:97
int save_str_value_in_field(Field *field, String *result)
Definition: item.cc:272
virtual void print(String *str)
Definition: string.cc:66
bool eq(const Item *item, bool binary_cmp) const
Definition: string.cc:54
String * val_str(String *)
Definition: string.h:92
type::Decimal * val_decimal(type::Decimal *)
Definition: string.cc:117
double val_real()
Definition: string.cc:73
String str_value
Definition: item.h:107
char * strdup(const char *)
Duplicate a null-terminated string into memory allocated from within the specified Root...
Definition: root.cc:328