MySQL 9.1.0
Source Code Documentation
key_spec.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 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 KEY_SPEC_INCLUDED
25#define KEY_SPEC_INCLUDED
26
27#include <sys/types.h>
28
29#include "lex_string.h"
30#include "my_base.h"
31#include "nulls.h"
32#include "sql/mem_root_array.h"
33#include "sql/sql_list.h"
34
35class Create_field;
36class Item;
37class THD;
38struct MEM_ROOT;
39
40enum keytype {
47};
48
56};
57
63};
64
66
68 public:
70 /**
71 A flag which indicates that index algorithm was explicitly specified
72 by user.
73 */
75 ulong block_size = 0;
78 bool is_visible = true;
79
80 KEY_CREATE_INFO() = default;
81
82 explicit KEY_CREATE_INFO(bool is_visible_arg) : is_visible(is_visible_arg) {}
83
86};
87
89
91 public:
92 Key_part_spec(Item *expression, enum_order order)
93 : m_is_ascending((order == ORDER_DESC) ? false : true),
97 m_expression(expression),
98 m_has_expression(true) {}
99
100 Key_part_spec(const char *column_name, Item *expression, enum_order order)
101 : m_is_ascending((order == ORDER_DESC) ? false : true),
103 m_field_name(column_name),
105 m_expression(expression),
106 m_has_expression(true) {}
107
108 Key_part_spec(LEX_CSTRING column_name, uint prefix_length, enum_order order)
109 : m_is_ascending((order == ORDER_DESC) ? false : true),
111 m_field_name(column_name.str),
112 m_prefix_length(prefix_length),
114 m_has_expression(false) {}
115
116 bool operator==(const Key_part_spec &other) const;
117 /**
118 Construct a copy of this Key_part_spec. field_name is copied
119 by-pointer as it is known to never change. At the same time
120 'length' may be reset in mysql_prepare_create_table, and this
121 is why we supply it with a copy.
122
123 @return If out of memory, 0 is returned and an error is set in
124 THD.
125 */
127 return new (mem_root) Key_part_spec(*this);
128 }
129
130 const char *get_field_name() const { return m_field_name; }
131
132 uint get_prefix_length() const { return m_prefix_length; }
133
135 assert(has_expression());
136 return m_expression;
137 }
138
139 /**
140 @retval true if this is an ascending index.
141 @retval false if this is a descending index.
142 */
143 bool is_ascending() const { return m_is_ascending; }
144
145 /**
146 @retval true if the user explicitly specified the index direction when
147 creating the index.
148 @retval false if the user didn't specify the index direction.
149 */
150 bool is_explicit() const { return m_is_explicit; }
151
152 /**
153 Resolve the expression that this key part contains. Should only be called
154 if has_expression() returns true.
155
156 @param thd thread handler.
157
158 @retval true if an error occurred.
159 @retval false on success.
160 */
161 bool resolve_expression(THD *thd);
162
163 /**
164 Set the name and the prefix length of the column this key part references.
165 The supplied column name string should have a lifetime equal to or longer
166 than this Key_part_spec
167
168 @param name the new column that this key part points to.
169  @param prefix_length the prefix length of the index, or 0 if no length is
170 specified.
171 */
172 void set_name_and_prefix_length(const char *name, uint prefix_length);
173
174 /**
175 @retval true if this index has an expression. In that case, this a
176 functional key part.
177 @retval false if this index doesn't have an expression. In that case this
178 key part references a normal column.
179 */
180 bool has_expression() const { return m_has_expression; }
181
182 private:
183 /// true <=> ascending, false <=> descending.
184 const bool m_is_ascending;
185
186 /// true <=> ASC/DESC is explicitly specified, false <=> implicit ASC
187 const bool m_is_explicit;
188
189 /// The name of the column that this key part points to.
190 const char *m_field_name;
191
192 /// The prefix length of this index.
194
195 /**
196 The indexed expression if this is a functional key part. If this key part
197 points to a "normal" column, m_expression is nullptr.
198 */
200
201 /**
202 Whether this key part has an expression or not. If so, this is a functional
203 key part.
204 */
206};
207
208class Key_spec {
209 public:
214 const bool generated;
215 /**
216 A flag to determine if we will check for duplicate indexes.
217 This typically means that the key information was specified
218 directly by the user (set by the parser) or a column
219 associated with it was dropped.
220 */
222
223 Key_spec(MEM_ROOT *mem_root, keytype type_par, const LEX_CSTRING &name_arg,
224 const KEY_CREATE_INFO *key_info_arg, bool generated_arg,
225 bool check_for_duplicate_indexes_arg, List<Key_part_spec> &cols)
226 : type(type_par),
227 key_create_info(*key_info_arg),
229 name(name_arg),
230 generated(generated_arg),
231 check_for_duplicate_indexes(check_for_duplicate_indexes_arg) {
232 columns.reserve(cols.elements);
234 Key_part_spec *column;
235 while ((column = it++)) columns.push_back(column);
236 }
237
238 virtual ~Key_spec() = default;
239};
240
242 public:
251 /**
252 Indicates whether foreign key name was provided explicitly or
253 was generated automatically.
254
255 @todo Get rid of this flag once we implement a better way for
256 NDB SE to get generated foreign key name from SQL-layer.
257 @sa prepare_foreign_key().
258 */
260
262 List<Key_part_spec> cols, const LEX_CSTRING &ref_db_arg,
263 const LEX_CSTRING &orig_ref_db_arg,
264 const LEX_CSTRING &ref_table_arg,
265 const LEX_CSTRING &orig_ref_table_arg,
266 List<Key_part_spec> *ref_cols, fk_option delete_opt_arg,
267 fk_option update_opt_arg, fk_match_opt match_opt_arg)
269 false,
270 false, // We don't check for duplicate FKs.
271 cols),
272 ref_db(ref_db_arg),
273 orig_ref_db(orig_ref_db_arg),
274 ref_table(ref_table_arg),
275 orig_ref_table(orig_ref_table_arg),
277 delete_opt(delete_opt_arg),
278 update_opt(update_opt_arg),
279 match_opt(match_opt_arg),
280 has_explicit_name(name_arg.str != nullptr) {
281 if (ref_cols) {
282 ref_columns.reserve(ref_cols->elements);
283 List_iterator<Key_part_spec> it(*ref_cols);
284 Key_part_spec *ref_column;
285 while ((ref_column = it++)) ref_columns.push_back(ref_column);
286 }
287 }
288
289 /**
290 Check if the foreign key name has valid length and its options
291 are compatible with columns on which the FK is created.
292
293 @param thd Thread handle
294 @param table_name Table name (for error reporting)
295 @param table_fields List of columns
296
297 @retval false Key valid
298 @retval true Key invalid
299 */
300 bool validate(THD *thd, const char *table_name,
301 List<Create_field> &table_fields) const;
302
303 /**
304 Set referenced column list for a FK from the referenced table PK columns.
305
306 @param thd Thread handle.
307 @param is_self_referencing_fk Represents self referencing foreign key.
308 @param key_list key info list of parent/referenced table.
309
310 @retval false when ref_columns list is populated successfully or when
311 ref_columns list is empty since parent table or PK in
312 parent table is missing.
313 @retval true if any error
314 */
315 bool set_ref_columns_for_implicit_pk(THD *thd, bool is_self_referencing_fk,
317};
318
319/**
320 Test if a foreign key (= generated key) is a prefix of the given key
321 (ignoring key name, key type and order of columns)
322
323 @note This is only used to test if an index for a FOREIGN KEY exists.
324 We only compare field names.
325
326 @retval false Generated key is a prefix of other key
327 @retval true Not equal
328*/
329bool foreign_key_prefix(const Key_spec *a, const Key_spec *b);
330
331#endif // KEY_SPEC_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:51
Definition: key_spec.h:241
const LEX_CSTRING ref_db
Definition: key_spec.h:243
const bool has_explicit_name
Indicates whether foreign key name was provided explicitly or was generated automatically.
Definition: key_spec.h:259
const LEX_CSTRING orig_ref_db
Definition: key_spec.h:244
const fk_option update_opt
Definition: key_spec.h:249
bool validate(THD *thd, const char *table_name, List< Create_field > &table_fields) const
Check if the foreign key name has valid length and its options are compatible with columns on which t...
Definition: key_spec.cc:111
const LEX_CSTRING orig_ref_table
Definition: key_spec.h:246
Mem_root_array< Key_part_spec * > ref_columns
Definition: key_spec.h:247
const fk_option delete_opt
Definition: key_spec.h:248
const fk_match_opt match_opt
Definition: key_spec.h:250
bool set_ref_columns_for_implicit_pk(THD *thd, bool is_self_referencing_fk, Mem_root_array< Key_spec * > &key_list)
Set referenced column list for a FK from the referenced table PK columns.
Definition: key_spec.cc:180
const LEX_CSTRING ref_table
Definition: key_spec.h:245
Foreign_key_spec(MEM_ROOT *mem_root, const LEX_CSTRING &name_arg, List< Key_part_spec > cols, const LEX_CSTRING &ref_db_arg, const LEX_CSTRING &orig_ref_db_arg, const LEX_CSTRING &ref_table_arg, const LEX_CSTRING &orig_ref_table_arg, List< Key_part_spec > *ref_cols, fk_option delete_opt_arg, fk_option update_opt_arg, fk_match_opt match_opt_arg)
Definition: key_spec.h:261
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
Definition: key_spec.h:67
bool is_algorithm_explicit
A flag which indicates that index algorithm was explicitly specified by user.
Definition: key_spec.h:74
ulong block_size
Definition: key_spec.h:75
LEX_CSTRING m_secondary_engine_attribute
Definition: key_spec.h:85
LEX_CSTRING m_engine_attribute
Definition: key_spec.h:84
KEY_CREATE_INFO()=default
LEX_CSTRING parser_name
Definition: key_spec.h:76
KEY_CREATE_INFO(bool is_visible_arg)
Definition: key_spec.h:82
bool is_visible
Definition: key_spec.h:78
LEX_CSTRING comment
Definition: key_spec.h:77
enum ha_key_alg algorithm
Definition: key_spec.h:69
Definition: key_spec.h:90
const bool m_is_explicit
true <=> ASC/DESC is explicitly specified, false <=> implicit ASC
Definition: key_spec.h:187
Key_part_spec * clone(MEM_ROOT *mem_root) const
Construct a copy of this Key_part_spec.
Definition: key_spec.h:126
void set_name_and_prefix_length(const char *name, uint prefix_length)
Set the name and the prefix length of the column this key part references.
Definition: key_spec.cc:105
uint get_prefix_length() const
Definition: key_spec.h:132
bool has_expression() const
Definition: key_spec.h:180
const char * get_field_name() const
Definition: key_spec.h:130
Item * get_expression() const
Definition: key_spec.h:134
const char * m_field_name
The name of the column that this key part points to.
Definition: key_spec.h:190
Key_part_spec(Item *expression, enum_order order)
Definition: key_spec.h:92
uint m_prefix_length
The prefix length of this index.
Definition: key_spec.h:193
bool is_explicit() const
Definition: key_spec.h:150
bool operator==(const Key_part_spec &other) const
Definition: key_spec.cc:54
bool m_has_expression
Whether this key part has an expression or not.
Definition: key_spec.h:205
bool resolve_expression(THD *thd)
Resolve the expression that this key part contains.
Definition: key_spec.cc:95
Item * m_expression
The indexed expression if this is a functional key part.
Definition: key_spec.h:199
Key_part_spec(const char *column_name, Item *expression, enum_order order)
Definition: key_spec.h:100
Key_part_spec(LEX_CSTRING column_name, uint prefix_length, enum_order order)
Definition: key_spec.h:108
const bool m_is_ascending
true <=> ascending, false <=> descending.
Definition: key_spec.h:184
bool is_ascending() const
Definition: key_spec.h:143
Definition: key_spec.h:208
Key_spec(MEM_ROOT *mem_root, keytype type_par, const LEX_CSTRING &name_arg, const KEY_CREATE_INFO *key_info_arg, bool generated_arg, bool check_for_duplicate_indexes_arg, List< Key_part_spec > &cols)
Definition: key_spec.h:223
virtual ~Key_spec()=default
const KEY_CREATE_INFO key_create_info
Definition: key_spec.h:211
const keytype type
Definition: key_spec.h:210
Mem_root_array< Key_part_spec * > columns
Definition: key_spec.h:212
LEX_CSTRING name
Definition: key_spec.h:213
const bool generated
Definition: key_spec.h:214
const bool check_for_duplicate_indexes
A flag to determine if we will check for duplicate indexes.
Definition: key_spec.h:221
Definition: sql_list.h:633
Definition: sql_list.h:494
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
uint elements
Definition: sql_list.h:190
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
fk_match_opt
Definition: key_spec.h:58
@ FK_MATCH_UNDEF
Definition: key_spec.h:59
@ FK_MATCH_SIMPLE
Definition: key_spec.h:62
@ FK_MATCH_FULL
Definition: key_spec.h:60
@ FK_MATCH_PARTIAL
Definition: key_spec.h:61
KEY_CREATE_INFO default_key_create_info
Definition: key_spec.cc:52
enum_order
Definition: key_spec.h:65
@ ORDER_NOT_RELEVANT
Definition: key_spec.h:65
@ ORDER_ASC
Definition: key_spec.h:65
@ ORDER_DESC
Definition: key_spec.h:65
keytype
Definition: key_spec.h:40
@ KEYTYPE_FULLTEXT
Definition: key_spec.h:44
@ KEYTYPE_FOREIGN
Definition: key_spec.h:46
@ KEYTYPE_PRIMARY
Definition: key_spec.h:41
@ KEYTYPE_MULTIPLE
Definition: key_spec.h:43
@ KEYTYPE_SPATIAL
Definition: key_spec.h:45
@ KEYTYPE_UNIQUE
Definition: key_spec.h:42
bool foreign_key_prefix(const Key_spec *a, const Key_spec *b)
Test if a foreign key (= generated key) is a prefix of the given key (ignoring key name,...
Definition: key_spec.cc:61
fk_option
Definition: key_spec.h:49
@ FK_OPTION_DEFAULT
Definition: key_spec.h:55
@ FK_OPTION_SET_NULL
Definition: key_spec.h:53
@ FK_OPTION_UNDEF
Definition: key_spec.h:50
@ FK_OPTION_RESTRICT
Definition: key_spec.h:51
@ FK_OPTION_CASCADE
Definition: key_spec.h:52
@ FK_OPTION_NO_ACTION
Definition: key_spec.h:54
constexpr const LEX_CSTRING EMPTY_CSTR
Definition: lex_string.h:48
This file includes constants used by all storage engines.
ha_key_alg
Definition: my_base.h:98
@ HA_KEY_ALG_SE_SPECIFIC
Used for cases when key algorithm which is supported by SE can't be described by one of other classes...
Definition: my_base.h:107
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1105
const char * table_name
Definition: rules_table_service.cc:56
#define NullS
Definition of the null string (a null pointer of type char *), used in some of our string handling co...
Definition: nulls.h:33
case opt name
Definition: sslopt-case.h:29
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql_lex_string.h:40