Drizzled Public API Documentation

cached_item.cc
Go to the documentation of this file.
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 
00027 #include <config.h>
00028 #include <drizzled/cached_item.h>
00029 #include <drizzled/field.h>
00030 #include <drizzled/sql_string.h>
00031 #include <drizzled/session.h>
00032 #include <drizzled/item/field.h>
00033 #include <algorithm>
00034 
00035 using namespace std;
00036 
00037 namespace drizzled
00038 {
00039 
00044 Cached_item *new_Cached_item(Session *session, Item *item)
00045 {
00046   if (item->real_item()->type() == Item::FIELD_ITEM &&
00047       !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
00048   {
00049     Item_field *real_item= (Item_field *) item->real_item();
00050     Field *cached_field= real_item->field;
00051     return new Cached_item_field(cached_field);
00052   }
00053 
00054   switch (item->result_type()) {
00055   case STRING_RESULT:
00056     return new Cached_item_str(session, (Item_field *) item);
00057   case INT_RESULT:
00058     return new Cached_item_int((Item_field *) item);
00059   case REAL_RESULT:
00060     return new Cached_item_real(item);
00061   case DECIMAL_RESULT:
00062     return new Cached_item_decimal(item);
00063   case ROW_RESULT:
00064     assert(0);
00065     return 0;
00066   }
00067 
00068   abort();
00069 }
00070 
00071 Cached_item::~Cached_item() {}
00072 
00080 Cached_item_str::Cached_item_str(Session *session, Item *arg)
00081   :item(arg), value(min(arg->max_length,
00082                         (uint32_t)session->variables.max_sort_length))
00083 {}
00084 
00085 bool Cached_item_str::cmp(void)
00086 {
00087   String *res;
00088   bool tmp;
00089 
00090   if ((res=item->val_str(&tmp_value)))
00091     res->length(min(res->length(), value.alloced_length()));
00092 
00093   if (null_value != item->null_value)
00094   {
00095     if ((null_value= item->null_value))
00096       // New value was null
00097       return(true);
00098     tmp=true;
00099   }
00100   else if (null_value)
00101     // new and old value was null
00102     return(0);
00103   else
00104     tmp= sortcmp(&value,res,item->collation.collation) != 0;
00105   if (tmp)
00106     // Remember for next cmp
00107     value.copy(*res);
00108   return(tmp);
00109 }
00110 
00111 Cached_item_str::~Cached_item_str()
00112 {
00113   // Safety
00114   item=0;
00115 }
00116 
00117 bool Cached_item_real::cmp(void)
00118 {
00119   double nr= item->val_real();
00120   if (null_value != item->null_value || nr != value)
00121   {
00122     null_value= item->null_value;
00123     value=nr;
00124     return(true);
00125   }
00126   return(false);
00127 }
00128 
00129 bool Cached_item_int::cmp(void)
00130 {
00131   int64_t nr=item->val_int();
00132   if (null_value != item->null_value || nr != value)
00133   {
00134     null_value= item->null_value;
00135     value=nr;
00136     return(true);
00137   }
00138   return(false);
00139 }
00140 
00141 
00142 Cached_item_field::Cached_item_field(Field *arg_field) 
00143   : 
00144     field(arg_field)
00145 {
00146   /* TODO: take the memory allocation below out of the constructor. */
00147   buff= (unsigned char*) memory::sql_calloc(length= field->pack_length());
00148 }
00149 
00150 bool Cached_item_field::cmp(void)
00151 {
00152   // This is not a blob!
00153   bool tmp= field->cmp_internal(buff) != 0;
00154 
00155   if (tmp)
00156     field->get_image(buff,length,field->charset());
00157   if (null_value != field->is_null())
00158   {
00159     null_value= !null_value;
00160     tmp=true;
00161   }
00162   return(tmp);
00163 }
00164 
00165 
00166 Cached_item_decimal::Cached_item_decimal(Item *it)
00167   :item(it)
00168 {
00169   value.set_zero();
00170 }
00171 
00172 
00173 bool Cached_item_decimal::cmp()
00174 {
00175   type::Decimal tmp;
00176   type::Decimal *ptmp= item->val_decimal(&tmp);
00177   if (null_value != item->null_value ||
00178       (!item->null_value && class_decimal_cmp(&value, ptmp)))
00179   {
00180     null_value= item->null_value;
00181     /* Save only not null values */
00182     if (!null_value)
00183     {
00184       class_decimal2decimal(ptmp, &value);
00185       return true;
00186     }
00187     return false;
00188   }
00189   return false;
00190 }
00191 
00192 } /* namespace drizzled */