Drizzled Public API Documentation

cache_str.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/item/cache_str.h>
00023 #include <drizzled/field.h>
00024 
00025 namespace drizzled
00026 {
00027 
00028 Item_cache_str::Item_cache_str(const Item *item) :
00029   Item_cache(), value(0),
00030   is_varbinary(item->type() == FIELD_ITEM &&
00031                ((const Item_field *) item)->field->type() ==
00032                DRIZZLE_TYPE_VARCHAR &&
00033                !((const Item_field *) item)->field->has_charset())
00034 {}
00035 
00036 void Item_cache_str::store(Item *item)
00037 {
00038   value_buff.set(buffer, sizeof(buffer), item->collation.collation);
00039   value= item->str_result(&value_buff);
00040   if ((null_value= item->null_value))
00041     value= 0;
00042   else if (value != &value_buff)
00043   {
00044     /*
00045       We copy string value to avoid changing value if 'item' is table field
00046       in queries like following (where t1.c is varchar):
00047       select a,
00048              (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
00049              (select c from t1 where a=t2.a)
00050         from t2;
00051     */
00052     value_buff.copy(*value);
00053     value= &value_buff;
00054   }
00055 }
00056 
00057 double Item_cache_str::val_real()
00058 {
00059   assert(fixed == 1);
00060   int err_not_used;
00061   char *end_not_used;
00062   if (value)
00063     return my_strntod(value->charset(), (char*) value->ptr(),
00064                       value->length(), &end_not_used, &err_not_used);
00065   return (double) 0;
00066 }
00067 
00068 
00069 int64_t Item_cache_str::val_int()
00070 {
00071   assert(fixed == 1);
00072   int err;
00073   if (value)
00074     return my_strntoll(value->charset(), value->ptr(),
00075                        value->length(), 10, (char**) 0, &err);
00076   else
00077     return (int64_t)0;
00078 }
00079 
00080 type::Decimal *Item_cache_str::val_decimal(type::Decimal *decimal_val)
00081 {
00082   assert(fixed == 1);
00083   if (value)
00084     decimal_val->store(E_DEC_FATAL_ERROR, value);
00085   else
00086     decimal_val= 0;
00087   return decimal_val;
00088 }
00089 
00090 int Item_cache_str::save_in_field(Field *field, bool no_conversions)
00091 {
00092   int res= Item_cache::save_in_field(field, no_conversions);
00093 
00094   return res;
00095 }
00096 
00097 } /* namespace drizzled */