Drizzled Public API Documentation

insert_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 #include <drizzled/error.h>
00022 #include <drizzled/name_resolution_context.h>
00023 #include <drizzled/table.h>
00024 #include <drizzled/item/insert_value.h>
00025 #include <drizzled/item/ref.h>
00026 #include <drizzled/item/copy_string.h>
00027 #include <drizzled/item/default_value.h>
00028 #include <drizzled/field/null.h>
00029 
00030 namespace drizzled {
00031 
00032 bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
00033 {
00034   return item->type() == INSERT_VALUE_ITEM &&
00035     ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
00036 }
00037 
00038 bool Item_insert_value::fix_fields(Session *session, Item **)
00039 {
00040   assert(fixed == 0);
00041   /* We should only check that arg is in first table */
00042   if (!arg->fixed)
00043   {
00044     TableList *orig_next_table= context->last_name_resolution_table;
00045     context->last_name_resolution_table= context->first_name_resolution_table;
00046     bool res= arg->fix_fields(session, &arg);
00047     context->last_name_resolution_table= orig_next_table;
00048     if (res)
00049       return true;
00050   }
00051 
00052   if (arg->type() == REF_ITEM)
00053   {
00054     Item_ref *ref= (Item_ref *)arg;
00055     if (ref->ref[0]->type() != FIELD_ITEM)
00056     {
00057       my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
00058       return true;
00059     }
00060     arg= ref->ref[0];
00061   }
00062   /*
00063     According to our SQL grammar, VALUES() function can reference
00064     only to a column.
00065   */
00066   assert(arg->type() == FIELD_ITEM);
00067 
00068   Item_field *field_arg= (Item_field *)arg;
00069 
00070   if (field_arg->field->getTable()->insert_values.size())
00071   {
00072     Field *def_field= (Field*) memory::sql_alloc(field_arg->field->size_of());
00073     memcpy(def_field, field_arg->field, field_arg->field->size_of());
00074     def_field->move_field_offset((ptrdiff_t)
00075                                  (&def_field->getTable()->insert_values[0] - def_field->getTable()->record[0]));
00076     set_field(def_field);
00077   }
00078   else
00079   {
00080     /* charset doesn't matter here, it's to avoid sigsegv only */
00081     Field* tmp_field= new Field_null(0, 0, field_arg->field->field_name);
00082     tmp_field->init(field_arg->field->getTable());
00083     set_field(tmp_field);
00084   }
00085   return false;
00086 }
00087 
00088 
00089 void Item_insert_value::print(String *str)
00090 {
00091   str->append(STRING_WITH_LEN("values("));
00092   arg->print(str);
00093   str->append(')');
00094 }
00095 
00096 
00097 } /* namespace drizzled */