1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 
34 #include "sql_class.h"  // THD
35 #include "table.h"      // TABLE
36 
37 /**
38    Allocates and initializes a MY_BITMAP bitmap, containing one bit per column
39    in the table. The table THD's MEM_ROOT is used to allocate memory.
40 
41    @param      table   The table whose columns should be used as a template
42                        for the bitmap.
43    @param[out] bitmap  A pointer to the allocated bitmap.
44 
45    @retval false Success.
46    @retval true Memory allocation error.
47 */
allocate_column_bitmap(TABLE * table,MY_BITMAP ** bitmap)48 static bool allocate_column_bitmap(TABLE *table, MY_BITMAP **bitmap)
49 {
50   DBUG_ENTER("allocate_column_bitmap");
51   const uint number_bits= table->s->fields;
52   MY_BITMAP *the_struct;
53   my_bitmap_map *the_bits;
54 
55   assert(current_thd == table->in_use);
56   if (multi_alloc_root(table->in_use->mem_root,
57                        &the_struct, sizeof(MY_BITMAP),
58                        &the_bits, bitmap_buffer_size(number_bits),
59                        NULL) == NULL)
60     DBUG_RETURN(true);
61 
62   if (bitmap_init(the_struct, the_bits, number_bits, FALSE) != 0)
63     DBUG_RETURN(true);
64 
65   *bitmap= the_struct;
66 
67   DBUG_RETURN(false);
68 }
69 
70 
get_function_default_columns(TABLE * table)71 bool COPY_INFO::get_function_default_columns(TABLE *table)
72 {
73   DBUG_ENTER("COPY_INFO::get_function_default_columns");
74 
75   if (m_function_default_columns != NULL)
76     DBUG_RETURN(false);
77 
78   if (allocate_column_bitmap(table, &m_function_default_columns))
79     DBUG_RETURN(true);
80 
81   if (!m_manage_defaults)
82     DBUG_RETURN(false); // leave bitmap full of zeroes
83 
84   /*
85     Find columns with function default on insert or update, mark them in
86     bitmap.
87   */
88   for (uint i= 0; i < table->s->fields; ++i)
89   {
90     Field *f= table->field[i];
91     if ((m_optype == INSERT_OPERATION && f->has_insert_default_function()) ||
92         (m_optype == UPDATE_OPERATION && f->has_update_default_function()))
93       bitmap_set_bit(m_function_default_columns, f->field_index);
94   }
95 
96   if (bitmap_is_clear_all(m_function_default_columns))
97     DBUG_RETURN(false); // no bit set, next step unneeded
98 
99   /*
100     Remove explicitly assigned columns from the bitmap. The assignment
101     target (lvalue) may not always be a column (Item_field), e.g. we could
102     be inserting into a view, whose column is actually a base table's column
103     converted with COLLATE: the lvalue would then be an
104     Item_func_set_collation.
105     If the lvalue is an expression tree, we clear all columns in it from the
106     bitmap.
107   */
108   List<Item> *all_changed_columns[2]=
109     { m_changed_columns, m_changed_columns2 };
110   for (uint i= 0; i < 2; i++)
111   {
112     if (all_changed_columns[i] != NULL)
113     {
114       List_iterator<Item> lvalue_it(*all_changed_columns[i]);
115       Item *lvalue_item;
116       while ((lvalue_item= lvalue_it++) != NULL)
117         lvalue_item->walk(&Item::remove_column_from_bitmap,
118                       Item::enum_walk(Item::WALK_POSTFIX | Item::WALK_SUBQUERY),
119                       reinterpret_cast<uchar*>(m_function_default_columns));
120     }
121   }
122 
123   DBUG_RETURN(false);
124 }
125 
126 
set_function_defaults(TABLE * table)127 void COPY_INFO::set_function_defaults(TABLE *table)
128 {
129   DBUG_ENTER("COPY_INFO::set_function_defaults");
130 
131   assert(m_function_default_columns != NULL);
132 
133   /* Quick reject test for checking the case when no defaults are invoked. */
134   if (bitmap_is_clear_all(m_function_default_columns))
135     DBUG_VOID_RETURN;
136 
137   for (uint i= 0; i < table->s->fields; ++i)
138     if (bitmap_is_set(m_function_default_columns, i))
139     {
140       assert(bitmap_is_set(table->write_set, i));
141       switch (m_optype)
142       {
143       case INSERT_OPERATION:
144         table->field[i]->evaluate_insert_default_function();
145         break;
146       case UPDATE_OPERATION:
147         table->field[i]->evaluate_update_default_function();
148         break;
149       }
150     }
151 
152   /**
153     @todo combine this call to update_generated_write_fields() with the
154     one in fill_record() to avoid updating virtual generated fields twice.
155     blobs_need_not_keep_old_value() is called to unset the m_keep_old_value
156     flag. Allowing this flag to remain might interfere with the way the old
157     BLOB value is handled. When update_generated_write_fields() is removed,
158     blobs_need_not_keep_old_value() can also be removed.
159   */
160   if (table->has_gcol())
161   {
162     table->blobs_need_not_keep_old_value();
163     update_generated_write_fields(table->write_set, table);
164   }
165 
166   DBUG_VOID_RETURN;
167 }
168 
169 
ignore_last_columns(TABLE * table,uint count)170 bool COPY_INFO::ignore_last_columns(TABLE *table, uint count)
171 {
172   if (get_function_default_columns(table))
173     return true;
174   for (uint i= 0; i < count; i++)
175     bitmap_clear_bit(m_function_default_columns,
176                      table->s->fields - 1 - i);
177   return false;
178 }
179