Drizzled Public API Documentation

field.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/field.h>
00023 #include <drizzled/item/cmpfunc.h>
00024 
00025 // Conversion functions
00026 
00027 namespace drizzled
00028 {
00029 
00030 int64_t Item_func_field::val_int()
00031 {
00032   assert(fixed == 1);
00033 
00034   if (cmp_type == STRING_RESULT)
00035   {
00036     String *field;
00037     if (!(field= args[0]->val_str(&value)))
00038       return 0;
00039     for (uint32_t i=1 ; i < arg_count ; i++)
00040     {
00041       String *tmp_value=args[i]->val_str(&tmp);
00042       if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
00043         return (int64_t) (i);
00044     }
00045   }
00046   else if (cmp_type == INT_RESULT)
00047   {
00048     int64_t val= args[0]->val_int();
00049     if (args[0]->null_value)
00050       return 0;
00051     for (uint32_t i=1; i < arg_count ; i++)
00052     {
00053       if (val == args[i]->val_int() && !args[i]->null_value)
00054         return (int64_t) (i);
00055     }
00056   }
00057   else if (cmp_type == DECIMAL_RESULT)
00058   {
00059     type::Decimal dec_arg_buf, *dec_arg,
00060                dec_buf, *dec= args[0]->val_decimal(&dec_buf);
00061     if (args[0]->null_value)
00062       return 0;
00063     for (uint32_t i=1; i < arg_count; i++)
00064     {
00065       dec_arg= args[i]->val_decimal(&dec_arg_buf);
00066       if (!args[i]->null_value && !class_decimal_cmp(dec_arg, dec))
00067         return (int64_t) (i);
00068     }
00069   }
00070   else
00071   {
00072     double val= args[0]->val_real();
00073     if (args[0]->null_value)
00074       return 0;
00075     for (uint32_t i=1; i < arg_count ; i++)
00076     {
00077       if (val == args[i]->val_real() && !args[i]->null_value)
00078         return (int64_t) (i);
00079     }
00080   }
00081   return 0;
00082 }
00083 
00084 void Item_func_field::fix_length_and_dec()
00085 {
00086   maybe_null=0; max_length=3;
00087   cmp_type= args[0]->result_type();
00088   for (uint32_t i=1; i < arg_count ; i++)
00089     cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
00090   if (cmp_type == STRING_RESULT)
00091     agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
00092 }
00093 
00094 } /* namespace drizzled */