Drizzled Public API Documentation

make_set.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/function/str/make_set.h>
00023 #include <drizzled/session.h>
00024 
00025 namespace drizzled
00026 {
00027 
00028 void Item_func_make_set::update_used_tables()
00029 {
00030   Item_func::update_used_tables();
00031   item->update_used_tables();
00032   used_tables_cache|=item->used_tables();
00033   const_item_cache&=item->const_item();
00034 }
00035 
00036 
00037 void Item_func_make_set::split_sum_func(Session *session_arg, Item **ref_pointer_array,
00038           List<Item> &fields)
00039 {
00040   item->split_sum_func(session_arg, ref_pointer_array, fields, &item, true);
00041   Item_str_func::split_sum_func(session_arg, ref_pointer_array, fields);
00042 }
00043 
00044 
00045 void Item_func_make_set::fix_length_and_dec()
00046 {
00047   max_length=arg_count-1;
00048 
00049   if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
00050     return;
00051 
00052   for (uint32_t i=0 ; i < arg_count ; i++)
00053     max_length+=args[i]->max_length;
00054 
00055   used_tables_cache|=   item->used_tables();
00056   not_null_tables_cache&= item->not_null_tables();
00057   const_item_cache&=    item->const_item();
00058   with_sum_func= with_sum_func || item->with_sum_func;
00059 }
00060 
00061 String *Item_func_make_set::val_str(String *str)
00062 {
00063   assert(fixed == 1);
00064   uint64_t bits;
00065   bool first_found=0;
00066   Item **ptr=args;
00067   String *result=&my_empty_string;
00068 
00069   bits=item->val_int();
00070   if ((null_value=item->null_value))
00071     return NULL;
00072 
00073   if (arg_count < 64)
00074     bits &= ((uint64_t) 1 << arg_count)-1;
00075 
00076   for (; bits; bits >>= 1, ptr++)
00077   {
00078     if (bits & 1)
00079     {
00080       String *res= (*ptr)->val_str(str);
00081       if (res)          // Skip nulls
00082       {
00083   if (!first_found)
00084   {         // First argument
00085     first_found=1;
00086     if (res != str)
00087       result=res;       // Use original string
00088     else
00089     {
00090       if (tmp_str.copy(*res))   // Don't use 'str'
00091         return &my_empty_string;
00092       result= &tmp_str;
00093     }
00094   }
00095   else
00096   {
00097     if (result != &tmp_str)
00098     {         // Copy data to tmp_str
00099       if (tmp_str.alloc(result->length()+res->length()+1) ||
00100     tmp_str.copy(*result))
00101         return &my_empty_string;
00102       result= &tmp_str;
00103     }
00104     if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
00105       return &my_empty_string;
00106   }
00107       }
00108     }
00109   }
00110   return result;
00111 }
00112 
00113 
00114 Item *Item_func_make_set::transform(Item_transformer transformer, unsigned char *arg)
00115 {
00116   Item *new_item= item->transform(transformer, arg);
00117   if (!new_item)
00118     return 0;
00119   item= new_item;
00120   return Item_str_func::transform(transformer, arg);
00121 }
00122 
00123 
00124 void Item_func_make_set::print(String *str)
00125 {
00126   str->append(STRING_WITH_LEN("make_set("));
00127   item->print(str);
00128   if (arg_count)
00129   {
00130     str->append(',');
00131     print_args(str, 0);
00132   }
00133   str->append(')');
00134 }
00135 
00136 } /* namespace drizzled */