Drizzled Public API Documentation

export_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/export_set.h>
00023 
00024 #include <algorithm>
00025 
00026 using namespace std;
00027 
00028 namespace drizzled {
00029 
00030 String* Item_func_export_set::val_str(String* str)
00031 {
00032   assert(fixed == 1);
00033   uint64_t the_set = (uint64_t) args[0]->val_int();
00034   String yes_buf, *yes;
00035   yes = args[1]->val_str(&yes_buf);
00036   String no_buf, *no;
00037   no = args[2]->val_str(&no_buf);
00038   String *sep = NULL, sep_buf ;
00039 
00040   uint32_t num_set_values = 64;
00041   uint64_t mask = 0x1;
00042   str->length(0);
00043   str->set_charset(collation.collation);
00044 
00045   /* Check if some argument is a NULL value */
00046   if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
00047   {
00048     null_value=1;
00049     return 0;
00050   }
00051   /*
00052     Arg count can only be 3, 4 or 5 here. This is guaranteed from the
00053     grammar for EXPORT_SET()
00054   */
00055   switch(arg_count) {
00056   case 5:
00057     num_set_values = (uint) args[4]->val_int();
00058     if (num_set_values > 64)
00059       num_set_values=64;
00060     if (args[4]->null_value)
00061     {
00062       null_value=1;
00063       return 0;
00064     }
00065     /* Fall through */
00066   case 4:
00067     if (!(sep = args[3]->val_str(&sep_buf)))  // Only true if NULL
00068     {
00069       null_value=1;
00070       return 0;
00071     }
00072     break;
00073   case 3:
00074     {
00075       sep_buf.copy(STRING_WITH_LEN(","), collation.collation);
00076       sep = &sep_buf;
00077     }
00078     break;
00079   default:
00080     assert(0); // cannot happen
00081   }
00082   null_value=0;
00083 
00084   for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
00085   {
00086     if (the_set & mask)
00087       str->append(*yes);
00088     else
00089       str->append(*no);
00090     if (i != num_set_values - 1)
00091       str->append(*sep);
00092   }
00093   return str;
00094 }
00095 
00096 void Item_func_export_set::fix_length_and_dec()
00097 {
00098   uint32_t length= max(args[1]->max_length,args[2]->max_length);
00099   uint32_t sep_length= (arg_count > 3 ? args[3]->max_length : 1);
00100   max_length= length*64+sep_length*63;
00101 
00102   if (agg_arg_charsets(collation, args+1, min(4U,arg_count)-1,
00103                        MY_COLL_ALLOW_CONV, 1))
00104     return;
00105 }
00106 
00107 } /* namespace drizzled */