MySQL 9.7.0
Source Code Documentation
sql_check_constraint.h
Go to the documentation of this file.
1/* Copyright (c) 2019, 2026, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_CHECK_CONSTRAINT_INCLUDED
25#define SQL_CHECK_CONSTRAINT_INCLUDED
26
27#include "lex_string.h" // LEX_STRING
28#include "sql/mem_root_array.h" // Mem_root_array
29
30class Alter_column;
31class Alter_drop;
32class Create_field;
33class Item;
34class String;
35struct TABLE;
36class THD;
37class Value_generator;
38
39/**
40 Class to represent the check constraint specifications obtained from the SQL
41 statement parse.
42*/
44 public:
45 /**
46 Validate check constraint name, perform per item-type to check if the
47 expression is allowed for the check constraint. Check expression is
48 pre-validated at this stage. Validation of specific functions in expression
49 is done later in the method open_table_from_share.
50
51 @retval false Success.
52 @retval true Failure.
53 */
54 bool pre_validate();
55
56 /**
57 Write check constraint expression into a String with proper syntax.
58
59 @param[in] thd Thread handle.
60 @param[out] out Check constraint expression.
61 */
62 void print_expr(THD *thd, String &out);
63
64 /**
65 Method to check if column "column_name" referred in the check constraint
66 expression.
67
68 @param[in] column_name Column name.
69
70 @retval true If column name is referenced in the check expression.
71 @retval false Otherwise.
72 */
73 bool expr_refers_column(const char *column_name);
74
75 public:
76 /// Name of the check constraint.
77 LEX_STRING name{nullptr, 0};
78
79 /// Check constraint expression.
80 Item *check_expr{nullptr};
81
82 /// Name of the column if check clause is defined at the column level.
84
85 /// Check constraint state (enforced/not enforced)
86 bool is_enforced{true};
87
88 /**
89 During ALTER TABLE operation, the state of the Sql_check_constraint_spec
90 instance(s) is set to alter mode in new table definition. In this
91 mode, alias_name is stored to data-dictionary tables to avoid name
92 conflicts. The name of the check constraint is updated to actual name after
93 older table version is either dropped or when new version of table is
94 renamed to actual table name.
95 */
96 bool is_alter_mode{false};
97
98 /// Alias name for check constraints.
100};
101
102/**
103 Class to represent check constraint in the TABLE_SHARE.
104
105 The instance of Sql_check_constraint_share contains information as name,
106 state and expression in string form. This information is obtained from
107 the data-dictionary. The check expression is not in itemized (materialized)
108 form here.
109*/
111 public:
113
115 const LEX_CSTRING &expr_str, bool is_enforced)
117
118 /// Constraint name.
119 LEX_CSTRING &name() { return m_name; }
120 /// Check expression in string form.
122 /// Check constraint state (enforced / not enforced)
123 bool is_enforced() { return m_is_enforced; }
124
125 private:
126 /// Check constraint name.
127 LEX_CSTRING m_name{nullptr, 0};
128
129 /// Check constraint expression.
131
132 /// Check constraint state.
133 bool m_is_enforced{true};
134};
135
136/**
137 Class to represent check constraint in the TABLE instance.
138
139 The Sql_table_check_constraint is a Sql_check_constraint_share with reference
140 to the parent TABLE instance and itemized (materialized) form of check
141 constraint expression.
142 Sql_table_check_constraint is prepared from the Sql_check_constraint_share of
143 TABLE_SHARE instance.
144*/
146 public:
148
150 const LEX_CSTRING &expr_str, bool is_enforced,
151 Value_generator *val_gen, TABLE *table)
153 m_val_gen(val_gen),
154 m_table(table) {}
155
156 /// Value generator.
158 void set_value_generator(Value_generator *val_gen) { m_val_gen = val_gen; }
159
160 /// Reference to owner table.
161 TABLE *table() const { return m_table; }
162
163 private:
164 /// Value generator for the check constraint expression.
166
167 /// Parent table reference.
168 TABLE *m_table{nullptr};
169};
170
171/// Type for the list of Sql_check_constraint_spec elements.
174
175/// Type for the list of Sql_check_constraint_share elements.
178
179/// Type for the list of Sql_table_check_constraint elements.
182
183/**
184 Method to check if server is a slave server and master server is on a
185 version not supporting check constraints feature. Check constraint support
186 is introduced in server version 80016.
187
188 Method is used by methods prepare_check_constraints_for_create() and
189 prepare_check_constraints_for_alter(). Check constraints are not prepared
190 (and specification list is cleared) when this method returns to true.
191 In older versions, check constraint syntax was supported but check constraint
192 feature was not supported. So if master is on older version and slave gets
193 event with check constraint syntax then on slave supporting check constraint,
194 query is parsed but during prepare time the specifications are ignored
195 for the statement(event).
196
197 @retval true if server is a slave server and master server is on a version
198 not supporting check constraints feature.
199 @retval false Otherwise.
200*/
202
203/**
204 Check if constraint expression refers to only "column_name" column of the
205 table.
206
207 @param[in] check_expr Check constraint expression.
208 @param[in] column_name Column name.
209
210 @retval true If expression refers to only "column_name".
211 @retval false If expression refers to more than one column
212 or if expression does not refers to "column_name".
213*/
215 const char *column_name);
216
217/**
218 Helper class to check if an ALTER statement performs a disallowed
219 modification on a column depended on by a Check constraint. Disallowed
220 modifications include:
221
222 - DROP COLUMN
223 - RENAME COLUMN
224 - SET MASKING POLICY
225*/
227 public:
229 const Sql_check_constraint_spec_list &check_constraint_list)
230 : m_check_constraint_list(check_constraint_list) {}
231
232 /**
233 Method to check if column being dropped is in use by check constraints.
234
235 @param drop Instance of Alter_drop.
236
237 @retval true If some check constraint uses the column being dropped.
238 @retval false Otherwise.
239 */
240 bool operator()(const Alter_drop *drop);
241
242 /**
243 Method to check if a column being altered with the RENAME COLUMN or SET
244 MASKING POLICY clause of the ALTER TABLE statement is in use by check
245 constraints.
246
247 @param alter_column Instance of Alter_column.
248
249 @retval true If some check constraint uses the column being renamed.
250 @retval false Otherwise.
251 */
252 bool operator()(const Alter_column *alter_column);
253
254 /**
255 Method to check if the CHANGE COLUMN or MODIFY COLUMN clause of the ALTER
256 TABLE statement renames a column or assigns a masking policy to a column
257 which is in use by check constraints.
258
259 @param fld Instance of Create_field.
260
261 @retval true If some check constraint uses the column being renamed.
262 @retval false Otherwise.
263 */
264 bool operator()(const Create_field &fld);
265
266 private:
268
269 /**
270 Check if any check constraint uses "column_name".
271
272 @param column_name Column name.
273 @param operation Which operation is performed on the column.
274
275 @retval true If column is used by the check constraint.
276 @retval false Otherwise.
277 */
278 bool any_check_constraint_uses_column(const char *column_name,
279 Operation operation);
280
281 /// Check constraint specification list.
283};
284#endif // SQL_CHECK_CONSTRAINT_INCLUDED
Class representing SET DEFAULT, DROP DEFAULT, RENAME COLUMN, SET VISIBLE and SET INVISIBLE clause in ...
Definition: sql_alter.h:82
Class representing DROP COLUMN, DROP KEY, DROP FOREIGN KEY, DROP CHECK CONSTRAINT and DROP CONSTRAINT...
Definition: sql_alter.h:65
Helper class to check if an ALTER statement performs a disallowed modification on a column depended o...
Definition: sql_check_constraint.h:226
const Sql_check_constraint_spec_list & m_check_constraint_list
Check constraint specification list.
Definition: sql_check_constraint.h:282
Operation
Definition: sql_check_constraint.h:267
bool operator()(const Alter_drop *drop)
Method to check if column being dropped is in use by check constraints.
Definition: sql_check_constraint.cc:153
Check_constraint_column_dependency_checker(const Sql_check_constraint_spec_list &check_constraint_list)
Definition: sql_check_constraint.h:228
bool any_check_constraint_uses_column(const char *column_name, Operation operation)
Check if any check constraint uses "column_name".
Definition: sql_check_constraint.cc:132
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:51
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:929
Class to represent check constraint in the TABLE_SHARE.
Definition: sql_check_constraint.h:110
bool m_is_enforced
Check constraint state.
Definition: sql_check_constraint.h:133
bool is_enforced()
Check constraint state (enforced / not enforced)
Definition: sql_check_constraint.h:123
Sql_check_constraint_share(const LEX_CSTRING &name, const LEX_CSTRING &expr_str, bool is_enforced)
Definition: sql_check_constraint.h:114
LEX_CSTRING m_name
Check constraint name.
Definition: sql_check_constraint.h:127
Sql_check_constraint_share()=default
LEX_CSTRING & name()
Constraint name.
Definition: sql_check_constraint.h:119
LEX_CSTRING & expr_str()
Check expression in string form.
Definition: sql_check_constraint.h:121
LEX_CSTRING m_expr_str
Check constraint expression.
Definition: sql_check_constraint.h:130
Class to represent the check constraint specifications obtained from the SQL statement parse.
Definition: sql_check_constraint.h:43
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:96
LEX_STRING alias_name
Alias name for check constraints.
Definition: sql_check_constraint.h:99
Item * check_expr
Check constraint expression.
Definition: sql_check_constraint.h:80
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:93
void print_expr(THD *thd, String &out)
Write check constraint expression into a String with proper syntax.
Definition: sql_check_constraint.cc:86
LEX_STRING column_name
Name of the column if check clause is defined at the column level.
Definition: sql_check_constraint.h:83
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:45
bool is_enforced
Check constraint state (enforced/not enforced)
Definition: sql_check_constraint.h:86
LEX_STRING name
Name of the check constraint.
Definition: sql_check_constraint.h:77
Class to represent check constraint in the TABLE instance.
Definition: sql_check_constraint.h:145
TABLE * m_table
Parent table reference.
Definition: sql_check_constraint.h:168
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:149
Value_generator * m_val_gen
Value generator for the check constraint expression.
Definition: sql_check_constraint.h:165
Value_generator * value_generator()
Value generator.
Definition: sql_check_constraint.h:157
TABLE * table() const
Reference to owner table.
Definition: sql_check_constraint.h:161
Sql_table_check_constraint()=default
void set_value_generator(Value_generator *val_gen)
Definition: sql_check_constraint.h:158
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Used for storing information associated with generated column, default values generated from expressi...
Definition: field.h:481
bool drop(THD *thd, const Table *tp)
Remove SDI for a table.
Definition: sdi.cc:639
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:113
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:106
Definition: mysql_lex_string.h:40
Definition: mysql_lex_string.h:35
Definition: table.h:1456