drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
default_value.cc
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, Inc.
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/item/default_value.h>
27
28namespace drizzled {
29
30bool Item_default_value::eq(const Item *item, bool binary_cmp) const
31{
32 return item->type() == DEFAULT_VALUE_ITEM &&
33 ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
34}
35
36
37bool Item_default_value::fix_fields(Session *session, Item **)
38{
39 assert(fixed == 0);
40
41 if (!arg)
42 {
43 fixed= 1;
44 return false;
45 }
46 if (!arg->fixed && arg->fix_fields(session, &arg))
47 return true;
48
49
50 Item* real_arg= arg->real_item();
51 if (real_arg->type() != FIELD_ITEM)
52 {
53 my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
54 return true;
55 }
56
57 Item_field* field_arg= (Item_field *)real_arg;
58 if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
59 {
60 my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
61 return true;
62 }
63 Field* def_field= (Field*) memory::sql_alloc(field_arg->field->size_of());
64 memcpy(static_cast<void *>(def_field),
65 static_cast<const void *>(field_arg->field),
66 field_arg->field->size_of());
67 def_field->move_field_offset((ptrdiff_t)(def_field->getTable()->getDefaultValues() - def_field->getTable()->record[0]));
68 set_field(def_field);
69 return false;
70}
71
72
74{
75 if (!arg)
76 {
77 str->append(STRING_WITH_LEN("default"));
78 return;
79 }
80 str->append(STRING_WITH_LEN("default("));
81 arg->print(str);
82 str->append(')');
83}
84
85
86int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
87{
88 if (!arg)
89 {
90 if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
91 {
92 if (field_arg->reset())
93 {
94 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT, ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
95 return -1;
96 }
97 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);
98 return 1;
99 }
100 field_arg->set_default();
101 return 0;
102 }
103 return Item_field::save_in_field(field_arg, no_conversions);
104}
105
106
111
112Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
113{
114 Item *new_item= arg->transform(transformer, args);
115 if (!new_item)
116 return NULL;
117 arg= new_item;
118 return (this->*transformer)(args);
119}
120
121
122} /* namespace drizzled */
const char * field_name
Definition field.h:104
Item * transform(Item_transformer transformer, unsigned char *args)
virtual void print(String *str)
bool eq(const Item *item, bool binary_cmp) const
bool fixed
Definition item.h:122
virtual Item * transform(Item_transformer transformer, unsigned char *arg)
Definition item.cc:387
const char * name
Definition item.h:112
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.