MySQL 8.4.3
Source Code Documentation
sql_constraint.h
Go to the documentation of this file.
1/* Copyright (c) 2019, 2024, 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_CONSTRAINT_INCLUDED
25#define SQL_CONSTRAINT_INCLUDED
26
27#include "my_inttypes.h" // ulonglong
28
30class Alter_drop;
31class Alter_info;
32class KEY;
33struct TABLE;
34class THD;
35namespace dd {
36class Table;
37}
38
39/**
40 Base class to resolve constraints type for the constraints specified in the
41 ALTER TABLE ... DROP/ALTER CONSTRAINT operation.
42
43 For a constraint specified in the DROP/ALTER CONSTRAINT operations of a ALTER
44 TABLE statement, actual constraint type is unknown. This is the class to
45 resolve the actual constraint type (PRIMARY, UNIQUE, FOREIGN KEY or CHECK) of
46 a constraint by name. Changes to Alter_info instance are reverted in the
47 destructor to make operation safe for re-execution of a stored routines and
48 prepared statements.
49*/
51 public:
53 : m_alter_info(alter_info) {}
54 virtual ~Constraint_type_resolver() = default;
55
56 /**
57 Method to check if constraint type resolution is needed.
58
59 @retval true if ALTER TABLE statement has DROP/ALTER CONSTRAINT operations.
60 @retval false Otherwise.
61 */
62 virtual bool is_type_resolution_needed() const = 0;
63
64 /**
65 Method to resolve constraints type.
66
67 @param thd Thread handle.
68 @param src_table TABLE instance of a source table.
69 @param dd_src_table Data-dictionary table instance of
70 a source table.
71
72 @retval false Success.
73 @retval true Failure.
74 */
75 virtual bool resolve_constraints_type(THD *thd, const TABLE *src_table,
76 const dd::Table *dd_src_table) = 0;
77
78 protected:
79 /**
80 Helper method to check if "name" type is PRIMARY or UNIQUE constraint.
81
82 @param src_table TABLE instance of a source table.
83 @param name Constraint name.
84
85 @retval non-nullptr Returns pointer to KEY instance if "name" is PRIMARY
86 or UNIQUE constraint.
87 @retval nullptr Otherwise.
88 */
90 const char *name);
91
92 /**
93 Helper method to check if "name" type is REFERENTIAL constraint.
94
95 @param dd_src_table Data-dictionary table instance of a source table.
96 @param name Constraint name.
97
98 @retval true If "name" type is REFERENTIAL.
99 @retval false Otherwise.
100 */
101 bool is_referential_constraint(const dd::Table *dd_src_table,
102 const char *name);
103
104 /**
105 Helper method to check if "name" type is CHECK constraint.
106
107 @param src_table TABLE instance of a source table.
108 @param name Constraint name.
109
110 @retval true If "name" type is CHECK.
111 @retval false Otherwise.
112 */
113 bool is_check_constraint(const TABLE *src_table, const char *name);
114
115 protected:
116 /// Alter_info instance describing table being altered.
118};
119
120/**
121 Class to resolve constraints type for the constraints specified in the
122 ALTER TABLE ... DROP CONSTRAINT operation.
123
124 For a constraint specified in the DROP CONSTRAINT operations of a ALTER TABLE
125 statement, actual constraint type is unknown. This is the class to resolve
126 actual constraint type (PRIMARY, UNIQUE, FOREIGN KEY or CHECK) by constraint
127 name. Alter_drop elements with actual constraint type are added to the
128 Alter_info::Alter_drop list. To make changes safe for re-execution of a stored
129 routines and prepared statements, elements added to Alter_drop list are
130 removed in the destructor.
131*/
133 public:
135 : Constraint_type_resolver(alter_info) {}
137
138 /**
139 Method to check if constraint type resolution is needed.
140
141 @retval true if ALTER TABLE statement has DROP CONSTRAINT operations.
142 @retval false Otherwise.
143 */
144 bool is_type_resolution_needed() const override;
145
146 /**
147 Method to resolve constraints type.
148
149 @param thd Thread handle.
150 @param src_table TABLE instance of a source table.
151 @param dd_src_table Data-dictionary table instance of
152 a source table.
153
154 @retval false Success.
155 @retval true Failure.
156 */
157 bool resolve_constraints_type(THD *thd, const TABLE *src_table,
158 const dd::Table *dd_src_table) override;
159
160 private:
161 /**
162 Method to resolve constraint type.
163
164 @param thd Thread handle.
165 @param src_table TABLE instance of a source table.
166 @param dd_src_table Data-dictionary table instance of
167 a source table.
168 @param drop Alter_drop instance for which
169 type should be resolved.
170
171 @retval false Success.
172 @retval true Failure.
173 */
174 bool resolve_constraint_type(THD *thd, const TABLE *src_table,
175 const dd::Table *dd_src_table,
176 const Alter_drop *drop);
177
178 private:
179 /// Flags set in Alter_info::flags while fixing type for constraint.
181
182 /*
183 Alter_drop element with actual type is added to the Alter_info::drop_list
184 for each DROP CONSTRAINT operation. Member holds the position of the first
185 Alter_drop element with actual type in the Alter_info::drop_list list.
186 */
188};
189
190/**
191 Class to resolve constraints type for the constraints specified in the
192 ALTER TABLE ... ALTER CONSTRAINT operation.
193
194 For a constraint specified in the ALTER CONSTRAINT operations of a ALTER TABLE
195 statement, actual constraint type is unknown. This is the class to resolve
196 actual constraint type (PRIMARY, UNIQUE, FOREIGN KEY or CHECK) by constraint
197 name. Alter_constraint_enforcement elements with actual constraint type are
198 added to the alter_constraint_enforcement_list. To make changes safe for
199 re-execution of stored routines and prepared statements, elements added to
200 list are removed in the destructor.
201*/
203 public:
205 : Constraint_type_resolver(alter_info) {}
207
208 /**
209 Method to check if constraint type resolution is needed.
210
211 @retval true if ALTER TABLE statement has ALTER CONSTRAINT operations.
212 @retval false Otherwise.
213 */
214 bool is_type_resolution_needed() const override;
215
216 /**
217 Method to resolve constraints type.
218
219 @param thd Thread handle.
220 @param src_table TABLE instance of a source table.
221 @param dd_src_table Data-dictionary table instance of
222 a source table.
223
224 @retval false Success.
225 @retval true Failure.
226 */
227 bool resolve_constraints_type(THD *thd, const TABLE *src_table,
228 const dd::Table *dd_src_table) override;
229
230 private:
231 /**
232 Method to resolve constraint type.
233
234 @param thd Thread handle.
235 @param src_table TABLE instance of a source table.
236 @param dd_src_table Data-dictionary table instance of
237 a source table.
238 @param alter_constraint Alter_constraint_enforcement
239 instance for which type should be
240 resolved.
241
242 @retval false Success.
243 @retval true Failure.
244 */
246 THD *thd, const TABLE *src_table, const dd::Table *dd_src_table,
247 const Alter_constraint_enforcement *alter_constraint);
248
249 private:
250 /// Flags set in Alter_info::flags while fixing type for check constraint.
252
253 /*
254 Alter_constraint_enforcement element with actual type is added to the
255 Alter_info::alter_constraint_enforcement_list for each ALTER CONSTRAINT
256 operation. Member holds position of the first Alter_constraint_enforcement
257 element with actual type in the alter_constraint_enforcement_list.
258 */
260};
261#endif // SQL_CONSTRAINT_INCLUDED
Class representing ALTER CHECK and ALTER CONSTRAINT clauses in ALTER TABLE statement.
Definition: sql_alter.h:184
Class representing DROP COLUMN, DROP KEY, DROP FOREIGN KEY, DROP CHECK CONSTRAINT and DROP CONSTRAINT...
Definition: sql_alter.h:65
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:205
Base class to resolve constraints type for the constraints specified in the ALTER TABLE ....
Definition: sql_constraint.h:50
Alter_info * m_alter_info
Alter_info instance describing table being altered.
Definition: sql_constraint.h:117
virtual bool is_type_resolution_needed() const =0
Method to check if constraint type resolution is needed.
KEY * is_primary_or_unique_constraint(const TABLE *src_table, const char *name)
Helper method to check if "name" type is PRIMARY or UNIQUE constraint.
Definition: sql_constraint.cc:36
bool is_referential_constraint(const dd::Table *dd_src_table, const char *name)
Helper method to check if "name" type is REFERENTIAL constraint.
Definition: sql_constraint.cc:50
Constraint_type_resolver(Alter_info *alter_info)
Definition: sql_constraint.h:52
virtual ~Constraint_type_resolver()=default
bool is_check_constraint(const TABLE *src_table, const char *name)
Helper method to check if "name" type is CHECK constraint.
Definition: sql_constraint.cc:62
virtual bool resolve_constraints_type(THD *thd, const TABLE *src_table, const dd::Table *dd_src_table)=0
Method to resolve constraints type.
Class to resolve constraints type for the constraints specified in the ALTER TABLE ....
Definition: sql_constraint.h:132
Drop_constraint_type_resolver(Alter_info *alter_info)
Definition: sql_constraint.h:134
bool is_type_resolution_needed() const override
Method to check if constraint type resolution is needed.
Definition: sql_constraint.cc:87
~Drop_constraint_type_resolver() override
Definition: sql_constraint.cc:75
bool resolve_constraint_type(THD *thd, const TABLE *src_table, const dd::Table *dd_src_table, const Alter_drop *drop)
Method to resolve constraint type.
Definition: sql_constraint.cc:91
uint m_first_fixed_alter_drop_pos
Definition: sql_constraint.h:187
bool resolve_constraints_type(THD *thd, const TABLE *src_table, const dd::Table *dd_src_table) override
Method to resolve constraints type.
Definition: sql_constraint.cc:158
ulonglong m_flags
Flags set in Alter_info::flags while fixing type for constraint.
Definition: sql_constraint.h:180
Class to resolve constraints type for the constraints specified in the ALTER TABLE ....
Definition: sql_constraint.h:202
~Enforce_constraint_type_resolver() override
Definition: sql_constraint.cc:174
Enforce_constraint_type_resolver(Alter_info *alter_info)
Definition: sql_constraint.h:204
bool resolve_constraint_type(THD *thd, const TABLE *src_table, const dd::Table *dd_src_table, const Alter_constraint_enforcement *alter_constraint)
Method to resolve constraint type.
Definition: sql_constraint.cc:195
bool is_type_resolution_needed() const override
Method to check if constraint type resolution is needed.
Definition: sql_constraint.cc:190
ulonglong m_flags
Flags set in Alter_info::flags while fixing type for check constraint.
Definition: sql_constraint.h:251
bool resolve_constraints_type(THD *thd, const TABLE *src_table, const dd::Table *dd_src_table) override
Method to resolve constraints type.
Definition: sql_constraint.cc:243
uint m_first_fixed_alter_constraint_pos
Definition: sql_constraint.h:259
Definition: key.h:113
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:47
bool drop(THD *thd, const Table *tp)
Remove SDI for a table.
Definition: sdi.cc:639
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
case opt name
Definition: sslopt-case.h:29
Definition: table.h:1406