File: sql_check_constraint.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (169 lines) | stat: -rw-r--r-- 6,745 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
/* 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 */

#include "sql/sql_check_constraint.h"

#include "libbinlogevents/include/binlog_event.h"  // UNDEFINED_SERVER_VERSION
#include "m_ctype.h"                               // CHARSET_INFO
#include "my_inttypes.h"                           // MYF, uchar
#include "my_sys.h"                                // my_error
#include "mysql/thread_type.h"                     // SYSTEM_THREAD_SLAVE_*
#include "mysql_com.h"                             // NAME_CHAR_LEN
#include "mysqld_error.h"                          // ER_*
#include "sql/create_field.h"                      // Create_field
#include "sql/enum_query_type.h"                   // QT_*
#include "sql/field.h"             // pre_validate_value_generator_expr
#include "sql/item.h"              // Item, Item_field
#include "sql/sql_class.h"         // THD
#include "sql/sql_const.h"         // enum_walk
#include "sql/sql_list.h"          // List
#include "sql/sql_parse.h"         // check_string_char_length
#include "sql/system_variables.h"  // System_variables
#include "sql/thd_raii.h"          // Sql_mode_parse_guard
#include "sql_string.h"            // String

bool Sql_check_constraint_spec::pre_validate() {
  /*
    Validate check constraint expression name. If name is not specified for the
    check constraint then name is generated before calling this method.
  */
  if (check_string_char_length(to_lex_cstring(name), "", NAME_CHAR_LEN,
                               system_charset_info, true)) {
    my_error(ER_TOO_LONG_IDENT, MYF(0), name.str);
    return true;
  }

  /*
    If this is a column check constraint then expression should refer only its
    column.
  */
  if (column_name.length != 0) {
    if (!check_constraint_expr_refers_to_only_column(check_expr,
                                                     column_name.str)) {
      my_error(ER_COLUMN_CHECK_CONSTRAINT_REFERENCES_OTHER_COLUMN, MYF(0),
               name.str);
      return true;
    }
  }

  // Check constraint expression must be a boolean expression.
  if (!check_expr->is_bool_func()) {
    my_error(ER_NON_BOOLEAN_EXPR_FOR_CHECK_CONSTRAINT, MYF(0), name.str);
    return true;
  }

  /*
    Pre-validate expression to determine if it is allowed for the check
    constraint.
  */
  if (pre_validate_value_generator_expr(check_expr, name.str,
                                        VGS_CHECK_CONSTRAINT))
    return true;

  return false;
}

void Sql_check_constraint_spec::print_expr(THD *thd, String &out) {
  out.length(0);
  Sql_mode_parse_guard parse_guard(thd);
  auto flags = enum_query_type(QT_NO_DB | QT_NO_TABLE | QT_FORCE_INTRODUCERS);
  check_expr->print(thd, &out, flags);
}

bool Sql_check_constraint_spec::expr_refers_column(const char *column_name) {
  mem_root_deque<Item_field *> fields(current_thd->mem_root);
  check_expr->walk(&Item::collect_item_field_processor, enum_walk::POSTFIX,
                   (uchar *)&fields);

  for (Item_field *cur_item : fields) {
    if (cur_item->type() == Item::FIELD_ITEM &&
        !my_strcasecmp(system_charset_info, cur_item->field_name, column_name))
      return true;
  }
  return false;
}

bool is_slave_with_master_without_check_constraints_support(THD *thd) {
  return ((thd->system_thread &
           (SYSTEM_THREAD_SLAVE_SQL | SYSTEM_THREAD_SLAVE_WORKER)) &&
          (thd->variables.original_server_version == UNDEFINED_SERVER_VERSION ||
           thd->variables.original_server_version < 80016));
}

bool check_constraint_expr_refers_to_only_column(Item *check_expr,
                                                 const char *column_name) {
  mem_root_deque<Item_field *> fields(current_thd->mem_root);
  check_expr->walk(&Item::collect_item_field_processor, enum_walk::POSTFIX,
                   (uchar *)&fields);

  // Expression does not refer to any columns.
  if (fields.empty()) return false;

  for (Item_field *cur_item : fields) {
    // Expression refers to some other column.
    if (cur_item->type() == Item::FIELD_ITEM &&
        my_strcasecmp(system_charset_info, cur_item->field_name, column_name))
      return false;
  }
  return true;
}

bool Check_constraint_column_dependency_checker::
    any_check_constraint_uses_column(const char *column_name) {
  auto column_used_by_constraint =
      [column_name](Sql_check_constraint_spec *cc_spec) {
        if (cc_spec->expr_refers_column(column_name)) {
          my_error(ER_DEPENDENT_BY_CHECK_CONSTRAINT, MYF(0), cc_spec->name.str,
                   column_name);
          return true;
        }
        return false;
      };

  return std::any_of(m_check_constraint_list.begin(),
                     m_check_constraint_list.end(), column_used_by_constraint);
}

bool Check_constraint_column_dependency_checker::operator()(
    const Alter_drop *drop) {
  if (drop->type != Alter_drop::COLUMN) return false;
  return any_check_constraint_uses_column(drop->name);
}

bool Check_constraint_column_dependency_checker::operator()(
    const Alter_column *alter_column) {
  if (alter_column->change_type() != Alter_column::Type::RENAME_COLUMN)
    return false;
  if (my_strcasecmp(system_charset_info, alter_column->name,
                    alter_column->m_new_name) == 0)
    return false;
  return any_check_constraint_uses_column(alter_column->name);
}

bool Check_constraint_column_dependency_checker::operator()(
    const Create_field &fld) {
  if (fld.change == nullptr) return false;
  if (my_strcasecmp(system_charset_info, fld.field_name, fld.change) == 0)
    return false;
  return any_check_constraint_uses_column(fld.change);
}