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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 
24 /**
25   @file sql_data_change.cc
26 
27   Contains classes representing SQL-data change statements. Currently
28   the only data change functionality implemented here is function
29   defaults.
30 */
31 
32 #include "sql_data_change.h"
33 #include "sql_class.h"
34 
35 /**
36    Allocates and initializes a MY_BITMAP bitmap, containing one bit per column
37    in the table. The table THD's MEM_ROOT is used to allocate memory.
38 
39    @param      table   The table whose columns should be used as a template
40                        for the bitmap.
41    @param[out] bitmap  A pointer to the allocated bitmap.
42 
43    @retval false Success.
44    @retval true Memory allocation error.
45 */
allocate_column_bitmap(TABLE * table,MY_BITMAP ** bitmap)46 static bool allocate_column_bitmap(TABLE *table, MY_BITMAP **bitmap)
47 {
48   DBUG_ENTER("allocate_column_bitmap");
49   const uint number_bits= table->s->fields;
50   MY_BITMAP *the_struct;
51   my_bitmap_map *the_bits;
52 
53   DBUG_ASSERT(current_thd == table->in_use);
54   if (multi_alloc_root(table->in_use->mem_root,
55                        &the_struct, sizeof(MY_BITMAP),
56                        &the_bits, bitmap_buffer_size(number_bits),
57                        NULL) == NULL)
58     DBUG_RETURN(true);
59 
60   if (bitmap_init(the_struct, the_bits, number_bits, FALSE) != 0)
61     DBUG_RETURN(true);
62 
63   *bitmap= the_struct;
64 
65   DBUG_RETURN(false);
66 }
67 
68 
get_function_default_columns(TABLE * table)69 bool COPY_INFO::get_function_default_columns(TABLE *table)
70 {
71   DBUG_ENTER("COPY_INFO::get_function_default_columns");
72 
73   if (m_function_default_columns != NULL)
74     DBUG_RETURN(false);
75 
76   if (allocate_column_bitmap(table, &m_function_default_columns))
77     DBUG_RETURN(true);
78 
79   if (!m_manage_defaults)
80     DBUG_RETURN(false); // leave bitmap full of zeroes
81 
82   /*
83     Find columns with function default on insert or update, mark them in
84     bitmap.
85   */
86   for (uint i= 0; i < table->s->fields; ++i)
87   {
88     Field *f= table->field[i];
89     if ((m_optype == INSERT_OPERATION && f->has_insert_default_function()) ||
90         (m_optype == UPDATE_OPERATION && f->has_update_default_function()))
91       bitmap_set_bit(m_function_default_columns, f->field_index);
92   }
93 
94   if (bitmap_is_clear_all(m_function_default_columns))
95     DBUG_RETURN(false); // no bit set, next step unneeded
96 
97   /*
98     Remove explicitly assigned columns from the bitmap. The assignment
99     target (lvalue) may not always be a column (Item_field), e.g. we could
100     be inserting into a view, whose column is actually a base table's column
101     converted with COLLATE: the lvalue would then be an
102     Item_func_set_collation.
103     If the lvalue is an expression tree, we clear all columns in it from the
104     bitmap.
105   */
106   List<Item> *all_changed_columns[2]=
107     { m_changed_columns, m_changed_columns2 };
108   for (uint i= 0; i < 2; i++)
109   {
110     if (all_changed_columns[i] != NULL)
111     {
112       List_iterator<Item> lvalue_it(*all_changed_columns[i]);
113       Item *lvalue_item;
114       while ((lvalue_item= lvalue_it++) != NULL)
115         lvalue_item->walk(&Item::remove_column_from_bitmap,
116                           true,
117                           reinterpret_cast<uchar*>(m_function_default_columns));
118     }
119   }
120 
121   DBUG_RETURN(false);
122 }
123 
124 
set_function_defaults(TABLE * table)125 void COPY_INFO::set_function_defaults(TABLE *table)
126 {
127   DBUG_ENTER("COPY_INFO::set_function_defaults");
128 
129   DBUG_ASSERT(m_function_default_columns != NULL);
130 
131   /* Quick reject test for checking the case when no defaults are invoked. */
132   if (bitmap_is_clear_all(m_function_default_columns))
133     DBUG_VOID_RETURN;
134 
135   for (uint i= 0; i < table->s->fields; ++i)
136     if (bitmap_is_set(m_function_default_columns, i))
137     {
138       DBUG_ASSERT(bitmap_is_set(table->write_set, i));
139       switch (m_optype)
140       {
141       case INSERT_OPERATION:
142         table->field[i]->evaluate_insert_default_function();
143         break;
144       case UPDATE_OPERATION:
145         table->field[i]->evaluate_update_default_function();
146         break;
147       }
148     }
149   DBUG_VOID_RETURN;
150 }
151 
152 
ignore_last_columns(TABLE * table,uint count)153 bool COPY_INFO::ignore_last_columns(TABLE *table, uint count)
154 {
155   if (get_function_default_columns(table))
156     return true;
157   for (uint i= 0; i < count; i++)
158     bitmap_clear_bit(m_function_default_columns,
159                      table->s->fields - 1 - i);
160   return false;
161 }
162