~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/default_value.cc

Cleanup around SAFEMALLOC

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
 
 
22
 
#include <drizzled/error.h>
23
 
#include <drizzled/name_resolution_context.h>
24
 
#include <drizzled/table.h>
25
 
#include <drizzled/session.h>
26
 
#include <drizzled/current_session.h>
27
 
#include <drizzled/item/default_value.h>
28
 
 
29
 
namespace drizzled
30
 
{
31
 
 
32
 
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
33
 
{
34
 
  return item->type() == DEFAULT_VALUE_ITEM &&
35
 
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
36
 
}
37
 
 
38
 
 
39
 
bool Item_default_value::fix_fields(Session *session, Item **)
40
 
{
41
 
  Item *real_arg;
42
 
  Item_field *field_arg;
43
 
  Field *def_field;
44
 
  assert(fixed == 0);
45
 
 
46
 
  if (!arg)
47
 
  {
48
 
    fixed= 1;
49
 
    return false;
50
 
  }
51
 
  if (!arg->fixed && arg->fix_fields(session, &arg))
52
 
    goto error;
53
 
 
54
 
 
55
 
  real_arg= arg->real_item();
56
 
  if (real_arg->type() != FIELD_ITEM)
57
 
  {
58
 
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
59
 
    goto error;
60
 
  }
61
 
 
62
 
  field_arg= (Item_field *)real_arg;
63
 
  if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
64
 
  {
65
 
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
66
 
    goto error;
67
 
  }
68
 
  if (!(def_field= (Field*) memory::sql_alloc(field_arg->field->size_of())))
69
 
    goto error;
70
 
  memcpy(def_field, field_arg->field, field_arg->field->size_of());
71
 
  def_field->move_field_offset((ptrdiff_t)
72
 
                               (def_field->table->s->default_values -
73
 
                                def_field->table->record[0]));
74
 
  set_field(def_field);
75
 
  return false;
76
 
 
77
 
error:
78
 
  context->process_error(session);
79
 
  return true;
80
 
}
81
 
 
82
 
 
83
 
void Item_default_value::print(String *str, enum_query_type query_type)
84
 
{
85
 
  if (!arg)
86
 
  {
87
 
    str->append(STRING_WITH_LEN("default"));
88
 
    return;
89
 
  }
90
 
  str->append(STRING_WITH_LEN("default("));
91
 
  arg->print(str, query_type);
92
 
  str->append(')');
93
 
}
94
 
 
95
 
 
96
 
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
97
 
{
98
 
  if (!arg)
99
 
  {
100
 
    if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
101
 
    {
102
 
      if (field_arg->reset())
103
 
      {
104
 
        my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
105
 
                   ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
106
 
        return -1;
107
 
      }
108
 
 
109
 
      {
110
 
        push_warning_printf(field_arg->table->in_use,
111
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
112
 
                            ER_NO_DEFAULT_FOR_FIELD,
113
 
                            ER(ER_NO_DEFAULT_FOR_FIELD),
114
 
                            field_arg->field_name);
115
 
      }
116
 
      return 1;
117
 
    }
118
 
    field_arg->set_default();
119
 
    return 0;
120
 
  }
121
 
  return Item_field::save_in_field(field_arg, no_conversions);
122
 
}
123
 
 
124
 
 
125
 
/**
126
 
  This method like the walk method traverses the item tree, but at the
127
 
  same time it can replace some nodes in the tree.
128
 
*/
129
 
 
130
 
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
131
 
{
132
 
  Item *new_item= arg->transform(transformer, args);
133
 
  if (!new_item)
134
 
    return NULL;
135
 
 
136
 
  /*
137
 
    Session::change_item_tree() should be called only if the tree was
138
 
    really transformed, i.e. when a new item has been created.
139
 
    Otherwise we'll be allocating a lot of unnecessary memory for
140
 
    change records at each execution.
141
 
  */
142
 
  if (arg != new_item)
143
 
    current_session->change_item_tree(&arg, new_item);
144
 
  return (this->*transformer)(args);
145
 
}
146
 
 
147
 
 
148
 
} /* namespace drizzled */