File: sql_check_constraint.h

package info (click to toggle)
mysql-8.0 8.0.45-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,048 kB
  • sloc: cpp: 4,685,434; ansic: 412,712; pascal: 108,396; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (274 lines) | stat: -rw-r--r-- 9,508 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Copyright (c) 2019, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef SQL_CHECK_CONSTRAINT_INCLUDED
#define SQL_CHECK_CONSTRAINT_INCLUDED

#include "lex_string.h"          // LEX_STRING
#include "sql/mem_root_array.h"  // Mem_root_array

class Alter_column;
class Alter_drop;
class Create_field;
class Item;
class String;
struct TABLE;
class THD;
class Value_generator;

/**
  Class to represent the check constraint specifications obtained from the SQL
  statement parse.
*/
class Sql_check_constraint_spec {
 public:
  /**
    Validate check constraint name, perform per item-type to check if the
    expression is allowed for the check constraint. Check expression is
    pre-validated at this stage. Validation of specific functions in expression
    is done later in the method open_table_from_share.

    @retval  false  Success.
    @retval  true   Failure.
  */
  bool pre_validate();

  /**
    Write check constraint expression into a String with proper syntax.

    @param[in]   thd   Thread handle.
    @param[out]  out   Check constraint expression.
  */
  void print_expr(THD *thd, String &out);

  /**
    Method to check if column "column_name" referred in the check constraint
    expression.

    @param[in]  column_name   Column name.

    @retval     true       If column name is referenced in the check expression.
    @retval     false      Otherwise.
  */
  bool expr_refers_column(const char *column_name);

 public:
  /// Name of the check constraint.
  LEX_STRING name{nullptr, 0};

  /// Check constraint expression.
  Item *check_expr{nullptr};

  /// Name of the column if check clause is defined at the column level.
  LEX_STRING column_name{nullptr, 0};

  /// Check constraint state (enforced/not enforced)
  bool is_enforced{true};

  /**
    During ALTER TABLE operation, the state of the Sql_check_constraint_spec
    instance(s) is set to alter mode in new table definition. In this
    mode, alias_name is stored to data-dictionary tables to avoid name
    conflicts. The name of the check constraint is updated to actual name after
    older table version is either dropped or when new version of table is
    renamed to actual table name.
  */
  bool is_alter_mode{false};

  /// Alias name for check constraints.
  LEX_STRING alias_name{nullptr, 0};
};

/**
  Class to represent check constraint in the TABLE_SHARE.

  The instance of Sql_check_constraint_share contains information as name,
  state and expression in string form. This information is obtained from
  the data-dictionary. The check expression is not in itemized (materialized)
  form here.
*/
class Sql_check_constraint_share {
 public:
  Sql_check_constraint_share() = default;

  Sql_check_constraint_share(const LEX_CSTRING &name,
                             const LEX_CSTRING &expr_str, bool is_enforced)
      : m_name(name), m_expr_str(expr_str), m_is_enforced(is_enforced) {}

  /// Constraint name.
  LEX_CSTRING &name() { return m_name; }
  /// Check expression in string form.
  LEX_CSTRING &expr_str() { return m_expr_str; }
  /// Check constraint state (enforced / not enforced)
  bool is_enforced() { return m_is_enforced; }

 private:
  /// Check constraint name.
  LEX_CSTRING m_name{nullptr, 0};

  /// Check constraint expression.
  LEX_CSTRING m_expr_str{nullptr, 0};

  /// Check constraint state.
  bool m_is_enforced{true};
};

/**
  Class to represent check constraint in the TABLE instance.

  The Sql_table_check_constraint is a Sql_check_constraint_share with reference
  to the parent TABLE instance and itemized (materialized) form of check
  constraint expression.
  Sql_table_check_constraint is prepared from the Sql_check_constraint_share of
  TABLE_SHARE instance.
*/
class Sql_table_check_constraint : public Sql_check_constraint_share {
 public:
  Sql_table_check_constraint() = default;

  Sql_table_check_constraint(const LEX_CSTRING &name,
                             const LEX_CSTRING &expr_str, bool is_enforced,
                             Value_generator *val_gen, TABLE *table)
      : Sql_check_constraint_share(name, expr_str, is_enforced),
        m_val_gen(val_gen),
        m_table(table) {}

  /// Value generator.
  Value_generator *value_generator() { return m_val_gen; }
  void set_value_generator(Value_generator *val_gen) { m_val_gen = val_gen; }

  /// Reference to owner table.
  TABLE *table() const { return m_table; }

 private:
  /// Value generator for the check constraint expression.
  Value_generator *m_val_gen{nullptr};

  /// Parent table reference.
  TABLE *m_table{nullptr};
};

/// Type for the list of Sql_check_constraint_spec elements.
using Sql_check_constraint_spec_list =
    Mem_root_array<Sql_check_constraint_spec *>;

/// Type for the list of Sql_check_constraint_share elements.
using Sql_check_constraint_share_list =
    Mem_root_array<Sql_check_constraint_share>;

/// Type for the list of Sql_table_check_constraint elements.
using Sql_table_check_constraint_list =
    Mem_root_array<Sql_table_check_constraint>;

/**
  Method to check if server is a slave server and master server is on a
  version not supporting check constraints feature. Check constraint support
  is introduced in server version 80016.

  Method is used by methods prepare_check_constraints_for_create() and
  prepare_check_constraints_for_alter(). Check constraints are not prepared
  (and specification list is cleared) when this method returns to true.
  In older versions, check constraint syntax was supported but check constraint
  feature was not supported. So if master is on older version and slave gets
  event with check constraint syntax then on slave supporting check constraint,
  query is parsed but during prepare time the specifications are ignored
  for the statement(event).

  @retval  true   if server is a slave server and master server is on a version
                  not supporting check constraints feature.
  @retval  false  Otherwise.
*/
bool is_slave_with_master_without_check_constraints_support(THD *thd);

/**
  Check if constraint expression refers to only "column_name" column of the
  table.

  @param[in]  check_expr    Check constraint expression.
  @param[in]  column_name   Column name.

  @retval     true       If expression refers to only "column_name".
  @retval     false      If expression refers to more than one column
                         or if expression does not refers to "column_name".
*/
bool check_constraint_expr_refers_to_only_column(Item *check_expr,
                                                 const char *column_name);

/**
  Helper class to check if column being dropped or removed in ALTER statement
  is in use by Check constraints.
*/
class Check_constraint_column_dependency_checker {
 public:
  explicit Check_constraint_column_dependency_checker(
      const Sql_check_constraint_spec_list &check_constraint_list)
      : m_check_constraint_list(check_constraint_list) {}

  /**
    Method to check if column being dropped is in use by check constraints.

    @param   drop    Instance of Alter_drop.

    @retval  true    If some check constraint uses the column being dropped.
    @retval  false   Otherwise.
  */
  bool operator()(const Alter_drop *drop);

  /**
    Method to check if column being renamed using RENAME COLUMN clause of the
    ALTER TABLE statement is in use by check constraints.

    @param   alter_column   Instance of Alter_column.

    @retval  true    If some check constraint uses the column being renamed.
    @retval  false   Otherwise.
  */
  bool operator()(const Alter_column *alter_column);

  /**
    Method to check if column being renamed using CHANGE [COLUMN] clause of the
    ALTER TABLE statement is in use by check constraints.

    @param   fld     Instance of Create_field.

    @retval  true    If some check constraint uses the column being renamed.
    @retval  false   Otherwise.
  */
  bool operator()(const Create_field &fld);

 private:
  /**
    Check if any check constraint uses "column_name".

    @param   column_name  Column name.

    @retval  true         If column is used by the check constraint.
    @retval  false        Otherwise.
  */
  bool any_check_constraint_uses_column(const char *column_name);

 private:
  /// Check constraint specification list.
  const Sql_check_constraint_spec_list &m_check_constraint_list;
};
#endif  // SQL_CHECK_CONSTRAINT_INCLUDED