MySQL  8.0.18
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 <map>
27 
28 #include "lex_string.h" // LEX_STRING
29 #include "mem_root_array.h" // Mem_root_array
30 #include "sql/dd/string_type.h" // dd::String_type
31 
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 #endif // SQL_CHECK_CONSTRAINT_INCLUDED
LEX_STRING column_name
Name of the column if check clause is defined at the column level.
Definition: sql_check_constraint.h:82
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
LEX_STRING alias_name
Alias name for check constraints.
Definition: sql_check_constraint.h:98
void print_expr(THD *thd, String &out)
Write check constraint expression into a String with proper syntax.
Definition: sql_check_constraint.cc:74
Definition: mysql_lex_string.h:34
LEX_CSTRING m_name
Check constraint name.
Definition: sql_check_constraint.h:131
bool m_is_enforced
Check constraint state.
Definition: sql_check_constraint.h:137
TABLE * table() const
Reference to owner table.
Definition: sql_check_constraint.h:176
Class to represent the check constraint specifications obtained from the SQL statement parse...
Definition: sql_check_constraint.h:42
Item * check_expr
Check constraint expression.
Definition: sql_check_constraint.h:79
LEX_CSTRING & name()
Constraint name.
Definition: sql_check_constraint.h:123
const char * str
Definition: mysql_lex_string.h:40
Sql_check_constraint_share & operator=(const Sql_check_constraint_share &)=delete
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:100
bool is_enforced
Check constraint state (enforced/not enforced)
Definition: sql_check_constraint.h:85
Definition: mysql_lex_string.h:39
LEX_CSTRING & expr_str()
Check expression in string form.
Definition: sql_check_constraint.h:125
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
void set_value_generator(Value_generator *val_gen)
Definition: sql_check_constraint.h:173
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:107
Definition: table.h:1301
Value_generator * value_generator()
Value generator.
Definition: sql_check_constraint.h:172
Definition: item.h:668
Sql_check_constraint_share(const LEX_CSTRING &name, const LEX_CSTRING &expr_str, bool is_enforced)
Definition: sql_check_constraint.h:113
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:398
Value_generator * m_val_gen
Value generator for the check constraint expression.
Definition: sql_check_constraint.h:180
TABLE * m_table
Parent table reference.
Definition: sql_check_constraint.h:183
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:81
Used for storing information associated with generated column, default values generated from expressi...
Definition: field.h:591
Class to represent check constraint in the TABLE instance.
Definition: sql_check_constraint.h:158
Sql_table_check_constraint & operator=(const Sql_table_check_constraint &)=delete
bool is_enforced()
Check constraint state (enforced / not enforced)
Definition: sql_check_constraint.h:127
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:33
~Sql_check_constraint_share()
Definition: sql_check_constraint.h:117
Sql_table_check_constraint()=default
LEX_STRING name
Name of the check constraint.
Definition: sql_check_constraint.h:76
LEX_CSTRING m_expr_str
Check constraint expression.
Definition: sql_check_constraint.h:134
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
Class to represent check constraint in the TABLE_SHARE.
Definition: sql_check_constraint.h:109
~Sql_table_check_constraint()
Definition: sql_check_constraint.cc:96
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
Sql_check_constraint_share()=default