MySQL  8.0.19
Source Code Documentation
sql_check_constraint.h
Go to the documentation of this file.
1 /* Copyright (c) 2019, 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
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef SQL_CHECK_CONSTRAINT_INCLUDED
24 #define SQL_CHECK_CONSTRAINT_INCLUDED
25 
26 #include "lex_string.h" // LEX_STRING
27 #include "sql/mem_root_array.h" // Mem_root_array
28 
29 class Alter_column;
30 class Alter_drop;
31 class Create_field;
32 class Item;
33 class String;
34 struct TABLE;
35 class THD;
36 class Value_generator;
37 
38 /**
39  Class to represent the check constraint specifications obtained from the SQL
40  statement parse.
41 */
43  public:
44  /**
45  Validate check constraint name, perform per item-type to check if the
46  expression is allowed for the check constraint. Check expression is
47  pre-validated at this stage. Validation of specific functions in expression
48  is done later in the method open_table_from_share.
49 
50  @retval false Success.
51  @retval true Failure.
52  */
53  bool pre_validate();
54 
55  /**
56  Write check constraint expression into a String with proper syntax.
57 
58  @param[in] thd Thread handle.
59  @param[out] out Check constraint expression.
60  */
61  void print_expr(THD *thd, String &out);
62 
63  /**
64  Method to check if column "column_name" referred in the check constraint
65  expression.
66 
67  @param[in] column_name Column name.
68 
69  @retval true If column name is referenced in the check expression.
70  @retval false Otherwise.
71  */
72  bool expr_refers_column(const char *column_name);
73 
74  public:
75  /// Name of the check constraint.
76  LEX_STRING name{nullptr, 0};
77 
78  /// Check constraint expression.
79  Item *check_expr{nullptr};
80 
81  /// Name of the column if check clause is defined at the column level.
82  LEX_STRING column_name{nullptr, 0};
83 
84  /// Check constraint state (enforced/not enforced)
85  bool is_enforced{true};
86 
87  /**
88  During ALTER TABLE operation, the state of the Sql_check_constraint_spec
89  instance(s) is set to alter mode in new table definition. In this
90  mode, alias_name is stored to data-dictionary tables to avoid name
91  conflicts. The name of the check constraint is updated to actual name after
92  older table version is either dropped or when new version of table is
93  renamed to actual table name.
94  */
95  bool is_alter_mode{false};
96 
97  /// Alias name for check constraints.
98  LEX_STRING alias_name{nullptr, 0};
99 };
100 
101 /**
102  Class to represent check constraint in the TABLE_SHARE.
103 
104  The instance of Sql_check_constraint_share contains information as name,
105  state and expression in string form. These informations are filled from
106  the data-dictionary. The check expression is not in itemized (materialized)
107  form here.
108 */
110  public:
111  Sql_check_constraint_share() = default;
112 
114  const LEX_CSTRING &expr_str, bool is_enforced)
116 
118  if (m_name.str != nullptr) delete m_name.str;
119  if (m_expr_str.str != nullptr) delete m_expr_str.str;
120  }
121 
122  /// Constraint name.
123  LEX_CSTRING &name() { return m_name; }
124  /// Check expression in string form.
126  /// Check constraint state (enforced / not enforced)
127  bool is_enforced() { return m_is_enforced; }
128 
129  private:
130  /// Check constraint name.
131  LEX_CSTRING m_name{nullptr, 0};
132 
133  /// Check constraint expression.
134  LEX_CSTRING m_expr_str{nullptr, 0};
135 
136  /// Check constraint state.
137  bool m_is_enforced{true};
138 
139  private:
140  /**
141  Delete default copy and assignment operator to avoid accidental destruction
142  of shallow copied Sql_table_check_constraint_share objects.
143  */
146  delete;
147 };
148 
149 /**
150  Class to represent check constraint in the TABLE instance.
151 
152  The Sql_table_check_constraint is a Sql_check_constraint_share with reference
153  to the parent TABLE instance and itemized (materialized) form of check
154  constraint expression.
155  Sql_table_check_constraint is prepared from the Sql_check_constraint_share of
156  TABLE_SHARE instance.
157 */
159  public:
160  Sql_table_check_constraint() = default;
161 
163  const LEX_CSTRING &expr_str, bool is_enforced,
164  Value_generator *val_gen, TABLE *table)
166  m_val_gen(val_gen),
167  m_table(table) {}
168 
170 
171  /// Value generator.
173  void set_value_generator(Value_generator *val_gen) { m_val_gen = val_gen; }
174 
175  /// Reference to owner table.
176  TABLE *table() const { return m_table; }
177 
178  private:
179  /// Value generator for the check constraint expression.
181 
182  /// Parent table reference.
183  TABLE *m_table{nullptr};
184 
185  private:
186  /**
187  Delete default copy and assignment operator to avoid accidental destruction
188  of shallow copied Sql_table_check_constraint objects.
189  */
192  delete;
193 };
194 
195 /// Type for the list of Sql_check_constraint_spec elements.
198 
199 /// Type for the list of Sql_check_constraint_share elements.
202 
203 /// Type for the list of Sql_table_check_constraint elements.
206 
207 /**
208  Method to check if server is a slave server and master server is on a
209  version not supporting check constraints feature. Check constraint support
210  is introduced in server version 80016.
211 
212  Method is used by methods prepare_check_constraints_for_create() and
213  prepare_check_constraints_for_alter(). Check constraints are not prepared
214  (and specification list is cleared) when this method returns to true.
215  In older versions, check constraint syntax was supported but check constraint
216  feature was not supported. So if master is on older version and slave gets
217  event with check constraint syntax then on slave supporting check constraint,
218  query is parsed but during prepare time the specifications are ignored
219  for the statement(event).
220 
221  @retval true if server is a slave server and master server is on a version
222  not supporting check constraints feature.
223  @retval false Otherwise.
224 */
226 
227 /**
228  Check if constraint expression refers to only "column_name" column of the
229  table.
230 
231  @param[in] check_expr Check constraint expression.
232  @param[in] column_name Column name.
233 
234  @retval true If expression refers to only "column_name".
235  @retval false If expression refers to more than one column
236  or if expression does not refers to "column_name".
237 */
239  const char *column_name);
240 
241 /**
242  Helper class to check if column being dropped or removed in ALTER statement
243  is in use by Check constraints.
244 */
246  public:
248  const Sql_check_constraint_spec_list &check_constraint_list)
249  : m_check_constraint_list(check_constraint_list) {}
250 
251  /**
252  Method to check if column being dropped is in use by check constraints.
253 
254  @param drop Instance of Alter_drop.
255 
256  @retval true If some check constraint uses the column being dropped.
257  @retval false Otherwise.
258  */
259  bool operator()(const Alter_drop *drop);
260 
261  /**
262  Method to check if column being renamed using RENAME COLUMN clause of the
263  ALTER TABLE statement is in use by check constraints.
264 
265  @param alter_column Instance of Alter_column.
266 
267  @retval true If some check constraint uses the column being renamed.
268  @retval false Otherwise.
269  */
270  bool operator()(const Alter_column *alter_column);
271 
272  /**
273  Method to check if column being renamed using CHANGE [COLUMN] clause of the
274  ALTER TABLE statement is in use by check constraints.
275 
276  @param fld Instance of Create_field.
277 
278  @retval true If some check constraint uses the column being renamed.
279  @retval false Otherwise.
280  */
281  bool operator()(const Create_field &fld);
282 
283  private:
284  /**
285  Check if any check constraint uses "column_name".
286 
287  @param column_name Column name.
288 
289  @retval true If column is used by the check constraint.
290  @retval false Otherwise.
291  */
292  bool any_check_constraint_uses_column(const char *column_name);
293 
294  private:
295  /// Check constraint specification list.
297 };
298 #endif // SQL_CHECK_CONSTRAINT_INCLUDED
MYSQL_LEX_CSTRING
Definition: mysql_lex_string.h:39
Check_constraint_column_dependency_checker
Helper class to check if column being dropped or removed in ALTER statement is in use by Check constr...
Definition: sql_check_constraint.h:245
Item
Definition: item.h:665
THD
Definition: sql_class.h:764
Sql_check_constraint_share::operator=
Sql_check_constraint_share & operator=(const Sql_check_constraint_share &)=delete
Mem_root_array< Sql_check_constraint_spec * >
Sql_check_constraint_share::m_is_enforced
bool m_is_enforced
Check constraint state.
Definition: sql_check_constraint.h:137
Sql_table_check_constraint
Class to represent check constraint in the TABLE instance.
Definition: sql_check_constraint.h:158
Sql_check_constraint_share::name
LEX_CSTRING & name()
Constraint name.
Definition: sql_check_constraint.h:123
dd::sdi::drop
bool drop(THD *thd, const Table *tp)
Remove SDI for a table.
Definition: sdi.cc:584
Sql_table_check_constraint::table
TABLE * table() const
Reference to owner table.
Definition: sql_check_constraint.h:176
Create_field
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:49
String
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:164
TABLE
Definition: table.h:1305
Sql_check_constraint_spec::pre_validate
bool pre_validate()
Validate check constraint name, perform per item-type to check if the expression is allowed for the c...
Definition: sql_check_constraint.cc:44
Sql_table_check_constraint::set_value_generator
void set_value_generator(Value_generator *val_gen)
Definition: sql_check_constraint.h:173
Sql_check_constraint_spec::expr_refers_column
bool expr_refers_column(const char *column_name)
Method to check if column "column_name" referred in the check constraint expression.
Definition: sql_check_constraint.cc:92
Sql_table_check_constraint::value_generator
Value_generator * value_generator()
Value generator.
Definition: sql_check_constraint.h:172
Sql_check_constraint_share::expr_str
LEX_CSTRING & expr_str()
Check expression in string form.
Definition: sql_check_constraint.h:125
Sql_check_constraint_spec
Class to represent the check constraint specifications obtained from the SQL statement parse.
Definition: sql_check_constraint.h:42
Sql_check_constraint_share
Class to represent check constraint in the TABLE_SHARE.
Definition: sql_check_constraint.h:109
Sql_check_constraint_spec::name
LEX_STRING name
Name of the check constraint.
Definition: sql_check_constraint.h:76
Sql_check_constraint_spec::column_name
LEX_STRING column_name
Name of the column if check clause is defined at the column level.
Definition: sql_check_constraint.h:82
lex_string.h
Check_constraint_column_dependency_checker::Check_constraint_column_dependency_checker
Check_constraint_column_dependency_checker(const Sql_check_constraint_spec_list &check_constraint_list)
Definition: sql_check_constraint.h:247
Sql_check_constraint_spec::is_alter_mode
bool is_alter_mode
During ALTER TABLE operation, the state of the Sql_check_constraint_spec instance(s) is set to alter ...
Definition: sql_check_constraint.h:95
Sql_check_constraint_spec::alias_name
LEX_STRING alias_name
Alias name for check constraints.
Definition: sql_check_constraint.h:98
Alter_column
Class representing SET DEFAULT, DROP DEFAULT and RENAME COLUMN clause in ALTER TABLE statement.
Definition: sql_alter.h:81
check_constraint_expr_refers_to_only_column
bool check_constraint_expr_refers_to_only_column(Item *check_expr, const char *column_name)
Check if constraint expression refers to only "column_name" column of the table.
Definition: sql_check_constraint.cc:118
Value_generator
Used for storing information associated with generated column, default values generated from expressi...
Definition: field.h:586
Sql_table_check_constraint::m_val_gen
Value_generator * m_val_gen
Value generator for the check constraint expression.
Definition: sql_check_constraint.h:180
Sql_table_check_constraint::m_table
TABLE * m_table
Parent table reference.
Definition: sql_check_constraint.h:183
Sql_check_constraint_spec::print_expr
void print_expr(THD *thd, String &out)
Write check constraint expression into a String with proper syntax.
Definition: sql_check_constraint.cc:85
Sql_check_constraint_spec::is_enforced
bool is_enforced
Check constraint state (enforced/not enforced)
Definition: sql_check_constraint.h:85
MYSQL_LEX_STRING
Definition: mysql_lex_string.h:34
Sql_check_constraint_spec::check_expr
Item * check_expr
Check constraint expression.
Definition: sql_check_constraint.h:79
Sql_check_constraint_share::is_enforced
bool is_enforced()
Check constraint state (enforced / not enforced)
Definition: sql_check_constraint.h:127
Sql_table_check_constraint::operator=
Sql_table_check_constraint & operator=(const Sql_table_check_constraint &)=delete
Sql_check_constraint_share::Sql_check_constraint_share
Sql_check_constraint_share(const LEX_CSTRING &name, const LEX_CSTRING &expr_str, bool is_enforced)
Definition: sql_check_constraint.h:113
Check_constraint_column_dependency_checker::any_check_constraint_uses_column
bool any_check_constraint_uses_column(const char *column_name)
Check if any check constraint uses "column_name".
Definition: sql_check_constraint.cc:139
Sql_table_check_constraint::Sql_table_check_constraint
Sql_table_check_constraint()=default
is_slave_with_master_without_check_constraints_support
bool is_slave_with_master_without_check_constraints_support(THD *thd)
Method to check if server is a slave server and master server is on a version not supporting check co...
Definition: sql_check_constraint.cc:111
Sql_check_constraint_share::m_expr_str
LEX_CSTRING m_expr_str
Check constraint expression.
Definition: sql_check_constraint.h:134
MYSQL_LEX_CSTRING::str
const char * str
Definition: mysql_lex_string.h:40
Check_constraint_column_dependency_checker::m_check_constraint_list
const Sql_check_constraint_spec_list & m_check_constraint_list
Check constraint specification list.
Definition: sql_check_constraint.h:296
Check_constraint_column_dependency_checker::operator()
bool operator()(const Alter_drop *drop)
Method to check if column being dropped is in use by check constraints.
Definition: sql_check_constraint.cc:154
Sql_table_check_constraint::~Sql_table_check_constraint
~Sql_table_check_constraint()
Definition: sql_check_constraint.cc:107
Alter_drop
Class representing DROP COLUMN, DROP KEY, DROP FOREIGN KEY, DROP CHECK CONSTRAINT and DROP CONSTRAINT...
Definition: sql_alter.h:64
Sql_check_constraint_share::m_name
LEX_CSTRING m_name
Check constraint name.
Definition: sql_check_constraint.h:131
Sql_check_constraint_share::Sql_check_constraint_share
Sql_check_constraint_share()=default
mem_root_array.h
Sql_table_check_constraint::Sql_table_check_constraint
Sql_table_check_constraint(const LEX_CSTRING &name, const LEX_CSTRING &expr_str, bool is_enforced, Value_generator *val_gen, TABLE *table)
Definition: sql_check_constraint.h:162
Sql_check_constraint_share::~Sql_check_constraint_share
~Sql_check_constraint_share()
Definition: sql_check_constraint.h:117