Drizzled Public API Documentation

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 
28 namespace drizzled {
29 
30 bool 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 
37 bool 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(def_field, field_arg->field, field_arg->field->size_of());
65  def_field->move_field_offset((ptrdiff_t)(def_field->getTable()->getDefaultValues() - def_field->getTable()->record[0]));
66  set_field(def_field);
67  return false;
68 }
69 
70 
72 {
73  if (!arg)
74  {
75  str->append(STRING_WITH_LEN("default"));
76  return;
77  }
78  str->append(STRING_WITH_LEN("default("));
79  arg->print(str);
80  str->append(')');
81 }
82 
83 
84 int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
85 {
86  if (!arg)
87  {
88  if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
89  {
90  if (field_arg->reset())
91  {
92  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT, ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
93  return -1;
94  }
95  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);
96  return 1;
97  }
98  field_arg->set_default();
99  return 0;
100  }
101  return Item_field::save_in_field(field_arg, no_conversions);
102 }
103 
104 
110 Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
111 {
112  Item *new_item= arg->transform(transformer, args);
113  if (!new_item)
114  return NULL;
115  arg= new_item;
116  return (this->*transformer)(args);
117 }
118 
119 
120 } /* namespace drizzled */
const char * name
Definition: item.h:110
bool fixed
Definition: item.h:120
bool eq(const Item *item, bool binary_cmp) const
virtual void print(String *str)
Item * transform(Item_transformer transformer, unsigned char *args)
virtual Item * transform(Item_transformer transformer, unsigned char *arg)
Definition: item.cc:387
Session * in_use
Definition: table.h:123
virtual void print(String *str)
Definition: item.cc:362
const char * field_name
Definition: field.h:102