00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <drizzled/session.h>
00023 #include <drizzled/error.h>
00024 #include <drizzled/item/string.h>
00025
00026 namespace drizzled
00027 {
00028
00029 Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
00030 {
00031 Item_string *conv;
00032 size_t conv_errors;
00033 char *ptr;
00034 String tmp, cstr, *ostr= val_str(&tmp);
00035 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
00036 if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(),
00037 cstr.charset(),
00038 collation.derivation)))
00039 {
00040
00041
00042
00043
00044
00045
00046 return NULL;
00047 }
00048
00049 if (!(ptr= getSession().strmake(cstr.ptr(), cstr.length())))
00050 return NULL;
00051
00052 conv->str_value.set(ptr, cstr.length(), cstr.charset());
00053
00054 conv->str_value.mark_as_const();
00055 return conv;
00056 }
00057
00058
00059 Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
00060 {
00061 Item_string *conv;
00062 size_t conv_errors;
00063 String tmp, cstr, *ostr= val_str(&tmp);
00064 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
00065 if (conv_errors ||
00066 !(conv= new Item_static_string_func(func_name,
00067 cstr.ptr(), cstr.length(),
00068 cstr.charset(),
00069 collation.derivation)))
00070 {
00071
00072
00073
00074
00075
00076
00077 return NULL;
00078 }
00079 conv->str_value.copy();
00080
00081 conv->str_value.mark_as_const();
00082 return conv;
00083 }
00084
00085
00086 bool Item_string::eq(const Item *item, bool binary_cmp) const
00087 {
00088 if (type() == item->type() && item->basic_const_item())
00089 {
00090 if (binary_cmp)
00091 return !stringcmp(&str_value, &item->str_value);
00092 return (collation.collation == item->collation.collation &&
00093 !sortcmp(&str_value, &item->str_value, collation.collation));
00094 }
00095 return 0;
00096 }
00097
00098 void Item_string::print(String *str)
00099 {
00100 if (is_cs_specified())
00101 {
00102 str->append('_');
00103 str->append(collation.collation->csname);
00104 }
00105
00106 str->append('\'');
00107
00108 str_value.print(str);
00109
00110 str->append('\'');
00111 }
00112
00113 double Item_string::val_real()
00114 {
00115 assert(fixed == 1);
00116 int error;
00117 char *end, *org_end;
00118 double tmp;
00119 const CHARSET_INFO * const cs= str_value.charset();
00120
00121 org_end= (char*) str_value.ptr() + str_value.length();
00122 tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
00123 &error);
00124 if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
00125 {
00126
00127
00128
00129
00130 push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
00131 ER_TRUNCATED_WRONG_VALUE,
00132 ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
00133 str_value.ptr());
00134 }
00135 return tmp;
00136 }
00137
00142 int64_t Item_string::val_int()
00143 {
00144 assert(fixed == 1);
00145 int err;
00146 int64_t tmp;
00147 char *end= (char*) str_value.ptr()+ str_value.length();
00148 char *org_end= end;
00149 const CHARSET_INFO * const cs= str_value.charset();
00150
00151 tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
00152
00153
00154
00155
00156 if (err > 0 ||
00157 (end != org_end && !check_if_only_end_space(cs, end, org_end)))
00158 {
00159 push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
00160 ER_TRUNCATED_WRONG_VALUE,
00161 ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
00162 str_value.ptr());
00163 }
00164 return tmp;
00165 }
00166
00167 type::Decimal *Item_string::val_decimal(type::Decimal *decimal_value)
00168 {
00169 return val_decimal_from_string(decimal_value);
00170 }
00171
00172 int Item_string::save_in_field(Field *field, bool)
00173 {
00174 String *result;
00175 result=val_str(&str_value);
00176 return save_str_value_in_field(field, result);
00177 }
00178
00179
00180 }