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