~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to drizzled/item/insert_value.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include <drizzled/error.h>
 
22
#include <drizzled/name_resolution_context.h>
 
23
#include <drizzled/table.h>
 
24
#include <drizzled/item/insert_value.h>
 
25
#include <drizzled/item/ref.h>
 
26
#include <drizzled/item/copy_string.h>
 
27
#include <drizzled/item/default_value.h>
 
28
#include <drizzled/field/null.h>
 
29
 
 
30
namespace drizzled
 
31
{
 
32
 
 
33
bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
 
34
{
 
35
  return item->type() == INSERT_VALUE_ITEM &&
 
36
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
 
37
}
 
38
 
 
39
bool Item_insert_value::fix_fields(Session *session, Item **)
 
40
{
 
41
  assert(fixed == 0);
 
42
  /* We should only check that arg is in first table */
 
43
  if (!arg->fixed)
 
44
  {
 
45
    bool res;
 
46
    TableList *orig_next_table= context->last_name_resolution_table;
 
47
    context->last_name_resolution_table= context->first_name_resolution_table;
 
48
    res= arg->fix_fields(session, &arg);
 
49
    context->last_name_resolution_table= orig_next_table;
 
50
    if (res)
 
51
      return true;
 
52
  }
 
53
 
 
54
  if (arg->type() == REF_ITEM)
 
55
  {
 
56
    Item_ref *ref= (Item_ref *)arg;
 
57
    if (ref->ref[0]->type() != FIELD_ITEM)
 
58
    {
 
59
      my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
 
60
      return true;
 
61
    }
 
62
    arg= ref->ref[0];
 
63
  }
 
64
  /*
 
65
    According to our SQL grammar, VALUES() function can reference
 
66
    only to a column.
 
67
  */
 
68
  assert(arg->type() == FIELD_ITEM);
 
69
 
 
70
  Item_field *field_arg= (Item_field *)arg;
 
71
 
 
72
  if (field_arg->field->table->insert_values)
 
73
  {
 
74
    Field *def_field= (Field*) memory::sql_alloc(field_arg->field->size_of());
 
75
    if (!def_field)
 
76
      return true;
 
77
    memcpy(def_field, field_arg->field, field_arg->field->size_of());
 
78
    def_field->move_field_offset((ptrdiff_t)
 
79
                                 (def_field->table->insert_values -
 
80
                                  def_field->table->record[0]));
 
81
    set_field(def_field);
 
82
  }
 
83
  else
 
84
  {
 
85
    Field *tmp_field= field_arg->field;
 
86
    /* charset doesn't matter here, it's to avoid sigsegv only */
 
87
    tmp_field= new Field_null(0, 0, field_arg->field->field_name, &my_charset_bin);
 
88
    if (tmp_field)
 
89
    {
 
90
      tmp_field->init(field_arg->field->table);
 
91
      set_field(tmp_field);
 
92
    }
 
93
  }
 
94
  return false;
 
95
}
 
96
 
 
97
 
 
98
void Item_insert_value::print(String *str, enum_query_type query_type)
 
99
{
 
100
  str->append(STRING_WITH_LEN("values("));
 
101
  arg->print(str, query_type);
 
102
  str->append(')');
 
103
}
 
104
 
 
105
 
 
106
} /* namespace drizzled */