MySQL  8.0.17
Source Code Documentation
key_spec.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 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 KEY_SPEC_INCLUDED
24 #define KEY_SPEC_INCLUDED
25 
26 #include <sys/types.h>
27 
28 #include "lex_string.h"
29 #include "m_string.h"
30 #include "my_base.h"
31 #include "sql/mem_root_array.h"
32 #include "sql/sql_list.h"
33 
34 class Create_field;
35 class Item;
36 class THD;
37 struct MEM_ROOT;
38 
39 enum keytype {
46 };
47 
48 enum fk_option {
55 };
56 
62 };
63 
65 
67  public:
69  /**
70  A flag which indicates that index algorithm was explicitly specified
71  by user.
72  */
73  bool is_algorithm_explicit = false;
77  bool is_visible = true;
78 
79  KEY_CREATE_INFO() = default;
80 
81  explicit KEY_CREATE_INFO(bool is_visible_arg) : is_visible(is_visible_arg) {}
82 };
83 
85 
87  public:
88  Key_part_spec(Item *expression, enum_order order)
89  : m_is_ascending((order == ORDER_DESC) ? false : true),
91  m_field_name(nullptr),
92  m_prefix_length(0),
93  m_expression(expression),
95 
96  Key_part_spec(const char *column_name, Item *expression, enum_order order)
97  : m_is_ascending((order == ORDER_DESC) ? false : true),
99  m_field_name(column_name),
100  m_prefix_length(0),
101  m_expression(expression),
103 
104  Key_part_spec(LEX_CSTRING column_name, uint prefix_length, enum_order order)
105  : m_is_ascending((order == ORDER_DESC) ? false : true),
107  m_field_name(column_name.str),
108  m_prefix_length(prefix_length),
109  m_expression(nullptr),
111 
112  bool operator==(const Key_part_spec &other) const;
113  /**
114  Construct a copy of this Key_part_spec. field_name is copied
115  by-pointer as it is known to never change. At the same time
116  'length' may be reset in mysql_prepare_create_table, and this
117  is why we supply it with a copy.
118 
119  @return If out of memory, 0 is returned and an error is set in
120  THD.
121  */
123  return new (mem_root) Key_part_spec(*this);
124  }
125 
126  const char *get_field_name() const { return m_field_name; }
127 
129 
130  Item *get_expression() const {
132  return m_expression;
133  }
134 
135  /**
136  @retval true if this is an ascending index.
137  @retval false if this is a descending index.
138  */
139  bool is_ascending() const { return m_is_ascending; }
140 
141  /**
142  @retval true if the user explicitly specified the index direction when
143  creating the index.
144  @retval false if the user didn't specify the index direction.
145  */
146  bool is_explicit() const { return m_is_explicit; }
147 
148  /**
149  Resolve the expression that this key part contains. Should only be called
150  if has_expression() returns true.
151 
152  @param thd thread handler.
153 
154  @retval true if an error occurred.
155  @retval false on success.
156  */
157  bool resolve_expression(THD *thd);
158 
159  /**
160  Set the name and the prefix length of the column this key part references.
161  The supplied column name string should have a lifetime equal to or longer
162  than this Key_part_spec
163 
164  @param name the new column that this key part points to.
165   @param prefix_length the prefix length of the index, or 0 if no length is
166  specified.
167  */
168  void set_name_and_prefix_length(const char *name, uint prefix_length);
169 
170  /**
171  @retval true if this index has an expression. In that case, this a
172  functional key part.
173  @retval false if this index doesn't have an expression. In that case this
174  key part references a normal column.
175  */
176  bool has_expression() const { return m_has_expression; }
177 
178  private:
179  /// true <=> ascending, false <=> descending.
180  const bool m_is_ascending;
181 
182  /// true <=> ASC/DESC is explicitly specified, false <=> implicit ASC
183  const bool m_is_explicit;
184 
185  /// The name of the column that this key part points to.
186  const char *m_field_name;
187 
188  /// The prefix length of this index.
190 
191  /**
192  The indexed expression if this is a functional key part. If this key part
193  points to a "normal" column, m_expression is nullptr.
194  */
196 
197  /**
198  Whether this key part has an expression or not. If so, this is a functional
199  key part.
200  */
202 };
203 
204 class Key_spec {
205  public:
206  const keytype type;
210  const bool generated;
211  /**
212  A flag to determine if we will check for duplicate indexes.
213  This typically means that the key information was specified
214  directly by the user (set by the parser) or a column
215  associated with it was dropped.
216  */
218 
219  Key_spec(MEM_ROOT *mem_root, keytype type_par, const LEX_CSTRING &name_arg,
220  const KEY_CREATE_INFO *key_info_arg, bool generated_arg,
221  bool check_for_duplicate_indexes_arg, List<Key_part_spec> &cols)
222  : type(type_par),
223  key_create_info(*key_info_arg),
224  columns(mem_root),
225  name(name_arg),
226  generated(generated_arg),
227  check_for_duplicate_indexes(check_for_duplicate_indexes_arg) {
228  columns.reserve(cols.elements);
230  Key_part_spec *column;
231  while ((column = it++)) columns.push_back(column);
232  }
233 
234  virtual ~Key_spec() {}
235 };
236 
237 class Foreign_key_spec : public Key_spec {
238  public:
247 
249  List<Key_part_spec> cols, const LEX_CSTRING &ref_db_arg,
250  const LEX_CSTRING &orig_ref_db_arg,
251  const LEX_CSTRING &ref_table_arg,
252  const LEX_CSTRING &orig_ref_table_arg,
253  List<Key_part_spec> *ref_cols, fk_option delete_opt_arg,
254  fk_option update_opt_arg, fk_match_opt match_opt_arg)
256  false,
257  false, // We don't check for duplicate FKs.
258  cols),
259  ref_db(ref_db_arg),
260  orig_ref_db(orig_ref_db_arg),
261  ref_table(ref_table_arg),
262  orig_ref_table(orig_ref_table_arg),
264  delete_opt(delete_opt_arg),
265  update_opt(update_opt_arg),
266  match_opt(match_opt_arg) {
267  if (ref_cols) {
268  ref_columns.reserve(ref_cols->elements);
269  List_iterator<Key_part_spec> it(*ref_cols);
270  Key_part_spec *ref_column;
271  while ((ref_column = it++)) ref_columns.push_back(ref_column);
272  }
273  }
274 
275  /**
276  Check if the foreign key name has valid length and its options
277  are compatible with columns on which the FK is created.
278 
279  @param thd Thread handle
280  @param table_name Table name (for error reporting)
281  @param table_fields List of columns
282 
283  @retval false Key valid
284  @retval true Key invalid
285  */
286  bool validate(THD *thd, const char *table_name,
287  List<Create_field> &table_fields) const;
288 };
289 
290 /**
291  Test if a foreign key (= generated key) is a prefix of the given key
292  (ignoring key name, key type and order of columns)
293 
294  @note This is only used to test if an index for a FOREIGN KEY exists.
295  We only compare field names.
296 
297  @retval false Generated key is a prefix of other key
298  @retval true Not equal
299 */
300 bool foreign_key_prefix(const Key_spec *a, const Key_spec *b);
301 
302 #endif // KEY_SPEC_INCLUDED
enum ha_key_alg algorithm
Definition: key_spec.h:68
Used for cases when key algorithm which is supported by SE can&#39;t be described by one of other classes...
Definition: my_base.h:105
Key_part_spec(LEX_CSTRING column_name, uint prefix_length, enum_order order)
Definition: key_spec.h:104
Definition: key_spec.h:51
Key_part_spec(const char *column_name, Item *expression, enum_order order)
Definition: key_spec.h:96
Definition: key_spec.h:60
enum_order
Definition: key_spec.h:64
const LEX_CSTRING ref_db
Definition: key_spec.h:239
const string name("\ame\)
Definition: key_spec.h:54
uint m_prefix_length
The prefix length of this index.
Definition: key_spec.h:189
const bool check_for_duplicate_indexes
A flag to determine if we will check for duplicate indexes.
Definition: key_spec.h:217
virtual ~Key_spec()
Definition: key_spec.h:234
Definition: key_spec.h:237
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:248
Mem_root_array< Key_part_spec * > ref_columns
Definition: key_spec.h:243
Definition: key_spec.h:50
Definition: mysql_lex_string.h:39
const char * get_field_name() const
Definition: key_spec.h:126
#define NullS
Definition of the null string (a null pointer of type char *), used in some of our string handling co...
Definition: m_string.h:49
Definition: key_spec.h:64
bool is_algorithm_explicit
A flag which indicates that index algorithm was explicitly specified by user.
Definition: key_spec.h:73
Item * m_expression
The indexed expression if this is a functional key part.
Definition: key_spec.h:195
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:219
const KEY_CREATE_INFO key_create_info
Definition: key_spec.h:207
Key_part_spec * clone(MEM_ROOT *mem_root) const
Construct a copy of this Key_part_spec.
Definition: key_spec.h:122
const bool m_is_explicit
true <=> ASC/DESC is explicitly specified, false <=> implicit ASC
Definition: key_spec.h:183
const LEX_CSTRING ref_table
Definition: key_spec.h:241
Definition: key_spec.h:59
Definition: key_spec.h:66
This file includes constants used by all storage engines.
#define DBUG_ASSERT(A)
Definition: my_dbug.h:183
Item * get_expression() const
Definition: key_spec.h:130
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:56
bool m_has_expression
Whether this key part has an expression or not.
Definition: key_spec.h:201
Definition: key_spec.h:64
bool resolve_expression(THD *thd)
Resolve the expression that this key part contains.
Definition: key_spec.cc:90
bool has_expression() const
Definition: key_spec.h:176
ulong block_size
Definition: key_spec.h:74
const LEX_CSTRING orig_ref_db
Definition: key_spec.h:240
Definition: key_spec.h:49
bool reserve(size_t n)
Reserves space for array elements.
Definition: mem_root_array.h:157
Definition: key_spec.h:44
keytype
Definition: key_spec.h:39
KEY_CREATE_INFO()=default
Definition: item.h:666
unsigned int uint
Definition: uca-dump.cc:29
#define true
Definition: config_static.h:44
Definition: key_spec.h:58
const fk_option update_opt
Definition: key_spec.h:245
uint get_prefix_length() const
Definition: key_spec.h:128
bool is_ascending() const
Definition: key_spec.h:139
const bool m_is_ascending
true <=> ascending, false <=> descending.
Definition: key_spec.h:180
Mem_root_array< Key_part_spec * > columns
Definition: key_spec.h:208
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:397
fk_option
Definition: key_spec.h:48
static MEM_ROOT mem_root
Definition: client_plugin.cc:107
const keytype type
Definition: key_spec.h:206
const fk_option delete_opt
Definition: key_spec.h:244
Definition: key_spec.h:204
KEY_CREATE_INFO(bool is_visible_arg)
Definition: key_spec.h:81
Definition: key_spec.h:43
Definition: key_spec.h:42
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:50
bool is_explicit() const
Definition: key_spec.h:146
Key_part_spec(Item *expression, enum_order order)
Definition: key_spec.h:88
Definition: key_spec.h:45
uint elements
Definition: sql_list.h:135
bool operator==(const Key_part_spec &other) const
Definition: key_spec.cc:49
bool push_back(const Element_type &element)
Adds a new element at the end of the array, after its current last element.
Definition: mem_root_array.h:188
ha_key_alg
Definition: my_base.h:96
LEX_CSTRING parser_name
Definition: key_spec.h:75
const fk_match_opt match_opt
Definition: key_spec.h:246
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:106
KEY_CREATE_INFO default_key_create_info
Definition: key_spec.cc:47
Definition: key_spec.h:53
LEX_CSTRING comment
Definition: key_spec.h:76
Definition: key_spec.h:41
Definition: key_spec.h:86
Definition: key_spec.h:40
fk_match_opt
Definition: key_spec.h:57
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
Definition: item.h:78
const bool generated
Definition: key_spec.h:210
const LEX_CSTRING orig_ref_table
Definition: key_spec.h:242
Definition: key_spec.h:64
const char * m_field_name
The name of the column that this key part points to.
Definition: key_spec.h:186
LEX_CSTRING name
Definition: key_spec.h:209
Definition: key_spec.h:52
unsigned long ulong
Definition: my_inttypes.h:48
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:100
#define false
Definition: config_static.h:43
bool is_visible
Definition: key_spec.h:77
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777
const char * table_name
Definition: rules_table_service.cc:55
Definition: key_spec.h:61