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