MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_data_change.cc
Go to the documentation of this file.
1 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 
25 #include "sql_data_change.h"
26 #include "sql_class.h"
27 
39 static bool allocate_column_bitmap(TABLE *table, MY_BITMAP **bitmap)
40 {
41  DBUG_ENTER("allocate_column_bitmap");
42  const uint number_bits= table->s->fields;
43  MY_BITMAP *the_struct;
44  my_bitmap_map *the_bits;
45 
46  DBUG_ASSERT(current_thd == table->in_use);
47  if (multi_alloc_root(table->in_use->mem_root,
48  &the_struct, sizeof(MY_BITMAP),
49  &the_bits, bitmap_buffer_size(number_bits),
50  NULL) == NULL)
51  DBUG_RETURN(true);
52 
53  if (bitmap_init(the_struct, the_bits, number_bits, FALSE) != 0)
54  DBUG_RETURN(true);
55 
56  *bitmap= the_struct;
57 
58  DBUG_RETURN(false);
59 }
60 
61 
63 {
64  DBUG_ENTER("COPY_INFO::get_function_default_columns");
65 
66  if (m_function_default_columns != NULL)
67  DBUG_RETURN(false);
68 
69  if (allocate_column_bitmap(table, &m_function_default_columns))
70  DBUG_RETURN(true);
71 
72  if (!m_manage_defaults)
73  DBUG_RETURN(false); // leave bitmap full of zeroes
74 
75  /*
76  Find columns with function default on insert or update, mark them in
77  bitmap.
78  */
79  for (uint i= 0; i < table->s->fields; ++i)
80  {
81  Field *f= table->field[i];
82  if ((m_optype == INSERT_OPERATION && f->has_insert_default_function()) ||
83  (m_optype == UPDATE_OPERATION && f->has_update_default_function()))
84  bitmap_set_bit(m_function_default_columns, f->field_index);
85  }
86 
87  if (bitmap_is_clear_all(m_function_default_columns))
88  DBUG_RETURN(false); // no bit set, next step unneeded
89 
90  /*
91  Remove explicitly assigned columns from the bitmap. The assignment
92  target (lvalue) may not always be a column (Item_field), e.g. we could
93  be inserting into a view, whose column is actually a base table's column
94  converted with COLLATE: the lvalue would then be an
95  Item_func_set_collation.
96  If the lvalue is an expression tree, we clear all columns in it from the
97  bitmap.
98  */
99  List<Item> *all_changed_columns[2]=
100  { m_changed_columns, m_changed_columns2 };
101  for (uint i= 0; i < 2; i++)
102  {
103  if (all_changed_columns[i] != NULL)
104  {
105  List_iterator<Item> lvalue_it(*all_changed_columns[i]);
106  Item *lvalue_item;
107  while ((lvalue_item= lvalue_it++) != NULL)
108  lvalue_item->walk(&Item::remove_column_from_bitmap,
109  true,
110  reinterpret_cast<uchar*>(m_function_default_columns));
111  }
112  }
113 
114  DBUG_RETURN(false);
115 }
116 
117 
119 {
120  DBUG_ENTER("COPY_INFO::set_function_defaults");
121 
122  DBUG_ASSERT(m_function_default_columns != NULL);
123 
124  /* Quick reject test for checking the case when no defaults are invoked. */
125  if (bitmap_is_clear_all(m_function_default_columns))
126  DBUG_VOID_RETURN;
127 
128  for (uint i= 0; i < table->s->fields; ++i)
129  if (bitmap_is_set(m_function_default_columns, i))
130  {
131  DBUG_ASSERT(bitmap_is_set(table->write_set, i));
132  switch (m_optype)
133  {
134  case INSERT_OPERATION:
135  table->field[i]->evaluate_insert_default_function();
136  break;
137  case UPDATE_OPERATION:
138  table->field[i]->evaluate_update_default_function();
139  break;
140  }
141  }
142  DBUG_VOID_RETURN;
143 }
144 
145 
146 bool COPY_INFO::ignore_last_columns(TABLE *table, uint count)
147 {
148  if (get_function_default_columns(table))
149  return true;
150  for (uint i= 0; i < count; i++)
151  bitmap_clear_bit(m_function_default_columns,
152  table->s->fields - 1 - i);
153  return false;
154 }