Drizzled Public API Documentation

string.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 Item *Item_string::safe_charset_converter(const charset_info_st * const tocs)
00029 {
00030   String tmp, cstr, *ostr= val_str(&tmp);
00031   cstr.copy(ostr->ptr(), ostr->length(), tocs);
00032   Item_string* conv= new Item_string(cstr.ptr(), cstr.length(), cstr.charset(), collation.derivation);
00033   char* ptr= getSession().mem.strdup(cstr);
00034 
00035   conv->str_value.set(ptr, cstr.length(), cstr.charset());
00036   /* Ensure that no one is going to change the result string */
00037   conv->str_value.mark_as_const();
00038   return conv;
00039 }
00040 
00041 
00042 Item *Item_static_string_func::safe_charset_converter(const charset_info_st * const tocs)
00043 {
00044   String tmp, cstr, *ostr= val_str(&tmp);
00045   cstr.copy(ostr->ptr(), ostr->length(), tocs);
00046   Item_string* conv= new Item_static_string_func(func_name, cstr, cstr.charset(), collation.derivation);
00047   conv->str_value.copy();
00048   /* Ensure that no one is going to change the result string */
00049   conv->str_value.mark_as_const();
00050   return conv;
00051 }
00052 
00053 
00054 bool Item_string::eq(const Item *item, bool binary_cmp) const
00055 {
00056   if (type() == item->type() && item->basic_const_item())
00057   {
00058     if (binary_cmp)
00059       return !stringcmp(&str_value, &item->str_value);
00060     return (collation.collation == item->collation.collation &&
00061             !sortcmp(&str_value, &item->str_value, collation.collation));
00062   }
00063   return 0;
00064 }
00065 
00066 void Item_string::print(String *str)
00067 {
00068   str->append('\'');
00069   str_value.print(*str);
00070   str->append('\'');
00071 }
00072 
00073 double Item_string::val_real()
00074 {
00075   assert(fixed == 1);
00076   int error;
00077   char *end;
00078   const charset_info_st* cs= str_value.charset();
00079 
00080   char* org_end= (char*) str_value.ptr() + str_value.length();
00081   double tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end, &error);
00082   if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
00083   {
00084     /*
00085       We can use str_value.ptr() here as Item_string is gurantee to put an
00086       end \0 here.
00087     */
00088     push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_TRUNCATED_WRONG_VALUE, ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE", str_value.ptr());
00089   }
00090   return tmp;
00091 }
00092 
00097 int64_t Item_string::val_int()
00098 {
00099   assert(fixed == 1);
00100   int err;
00101   char *end= (char*) str_value.ptr()+ str_value.length();
00102   char *org_end= end;
00103   const charset_info_st * const cs= str_value.charset();
00104 
00105   int64_t tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
00106   /*
00107     TODO: Give error if we wanted a signed integer and we got an unsigned
00108     one
00109   */
00110   if (err > 0 || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
00111   {
00112     push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_TRUNCATED_WRONG_VALUE, ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER", str_value.ptr());
00113   }
00114   return tmp;
00115 }
00116 
00117 type::Decimal *Item_string::val_decimal(type::Decimal *decimal_value)
00118 {
00119   return val_decimal_from_string(decimal_value);
00120 }
00121 
00122 int Item_string::save_in_field(Field *field, bool)
00123 {
00124   String* result=val_str(&str_value);
00125   return save_str_value_in_field(field, result);
00126 }
00127 
00128 
00129 } /* namespace drizzled */