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