Drizzled Public API Documentation

default_value.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/error.h>
00023 #include <drizzled/name_resolution_context.h>
00024 #include <drizzled/table.h>
00025 #include <drizzled/session.h>
00026 #include <drizzled/item/default_value.h>
00027 
00028 namespace drizzled
00029 {
00030 
00031 bool Item_default_value::eq(const Item *item, bool binary_cmp) const
00032 {
00033   return item->type() == DEFAULT_VALUE_ITEM &&
00034     ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
00035 }
00036 
00037 
00038 bool Item_default_value::fix_fields(Session *session, Item **)
00039 {
00040   Item *real_arg;
00041   Item_field *field_arg;
00042   Field *def_field;
00043   assert(fixed == 0);
00044 
00045   if (!arg)
00046   {
00047     fixed= 1;
00048     return false;
00049   }
00050   if (!arg->fixed && arg->fix_fields(session, &arg))
00051     goto error;
00052 
00053 
00054   real_arg= arg->real_item();
00055   if (real_arg->type() != FIELD_ITEM)
00056   {
00057     my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
00058     goto error;
00059   }
00060 
00061   field_arg= (Item_field *)real_arg;
00062   if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
00063   {
00064     my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
00065     goto error;
00066   }
00067   if (!(def_field= (Field*) memory::sql_alloc(field_arg->field->size_of())))
00068     goto error;
00069   memcpy(def_field, field_arg->field, field_arg->field->size_of());
00070   def_field->move_field_offset((ptrdiff_t)
00071                                (def_field->getTable()->getDefaultValues() - def_field->getTable()->record[0]));
00072   set_field(def_field);
00073   return false;
00074 
00075 error:
00076   context->process_error(session);
00077   return true;
00078 }
00079 
00080 
00081 void Item_default_value::print(String *str)
00082 {
00083   if (!arg)
00084   {
00085     str->append(STRING_WITH_LEN("default"));
00086     return;
00087   }
00088   str->append(STRING_WITH_LEN("default("));
00089   arg->print(str);
00090   str->append(')');
00091 }
00092 
00093 
00094 int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
00095 {
00096   if (!arg)
00097   {
00098     if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
00099     {
00100       if (field_arg->reset())
00101       {
00102         my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
00103                    ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
00104         return -1;
00105       }
00106 
00107       {
00108         push_warning_printf(field_arg->getTable()->in_use,
00109                             DRIZZLE_ERROR::WARN_LEVEL_WARN,
00110                             ER_NO_DEFAULT_FOR_FIELD,
00111                             ER(ER_NO_DEFAULT_FOR_FIELD),
00112                             field_arg->field_name);
00113       }
00114       return 1;
00115     }
00116     field_arg->set_default();
00117     return 0;
00118   }
00119   return Item_field::save_in_field(field_arg, no_conversions);
00120 }
00121 
00122 
00128 Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
00129 {
00130   Item *new_item= arg->transform(transformer, args);
00131   if (!new_item)
00132     return NULL;
00133   arg= new_item;
00134   return (this->*transformer)(args);
00135 }
00136 
00137 
00138 } /* namespace drizzled */