MySQL 8.4.2
Source Code Documentation
dd_table.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 DD_TABLE_INCLUDED
25#define DD_TABLE_INCLUDED
26
27#include <sys/types.h>
28#include <memory> // std:unique_ptr
29#include <string>
30
31#include "my_inttypes.h"
32#include "sql/dd/result_type.h" // dd::ResultType
33#include "sql/dd/string_type.h"
34#include "sql/dd/types/column.h" // dd::enum_column_types
35#include "sql/handler.h" // legacy_db_type
36#include "sql/sql_alter.h" // Alter_info::enum_enable_or_disable
37
38class Create_field;
39class FOREIGN_KEY;
40class KEY;
41class THD;
42namespace dd {
43class Schema;
44} // namespace dd
45struct CHARSET_INFO;
46struct TABLE;
47
51
52struct HA_CREATE_INFO;
53template <class T>
54class List;
55
56namespace dd {
57class Abstract_table;
58class Foreign_key;
59class Table;
60
61namespace cache {
62class Dictionary_client;
63}
64
65static const char FIELD_NAME_SEPARATOR_CHAR = ';';
66static const char CHECK_CONSTRAINT_NAME_SUBSTR[] = "_chk_";
67
68/**
69 Prepares a dd::Table object from mysql_prepare_create_table() output
70 and return it to the caller. This function creates a user table, as
71 opposed to create_table() which can handle system tables as well.
72
73 @param thd Thread handle
74 @param sch_obj Schema.
75 @param table_name Table name.
76 @param create_info HA_CREATE_INFO describing the table to be created.
77 @param create_fields List of fields for the table.
78 @param keyinfo Array with descriptions of keys for the table.
79 @param keys Number of keys.
80 @param keys_onoff keys ON or OFF
81 @param fk_keyinfo Array with descriptions of foreign keys for the
82 table.
83 @param fk_keys Number of foreign keys.
84 @param check_cons_spec Specification of check constraints for the table.
85 @param file handler instance for the table.
86
87 @returns Constructed dd::Table object, or nullptr in case of an error.
88*/
89std::unique_ptr<dd::Table> create_dd_user_table(
90 THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name,
91 HA_CREATE_INFO *create_info, const List<Create_field> &create_fields,
92 const KEY *keyinfo, uint keys,
94 const FOREIGN_KEY *fk_keyinfo, uint fk_keys,
95 const Sql_check_constraint_spec_list *check_cons_spec, handler *file);
96
97/**
98 Prepares a dd::Table object from mysql_prepare_create_table() output
99 and return it to the caller.
100
101 @param thd Thread handle
102 @param sch_obj Schema.
103 @param table_name Table name.
104 @param create_info HA_CREATE_INFO describing the table to be created.
105 @param create_fields List of fields for the table.
106 @param keyinfo Array with descriptions of keys for the table.
107 @param keys Number of keys.
108 @param keys_onoff Enable or disable keys.
109 @param fk_keyinfo Array with descriptions of foreign keys for the
110 table.
111 @param fk_keys Number of foreign keys.
112 @param check_cons_spec Specification of check constraints for the table.
113 @param file handler instance for the table.
114
115 @returns Constructed dd::Table object, or nullptr in case of an error.
116*/
117std::unique_ptr<dd::Table> create_table(
118 THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name,
119 HA_CREATE_INFO *create_info, const List<Create_field> &create_fields,
120 const KEY *keyinfo, uint keys,
122 const FOREIGN_KEY *fk_keyinfo, uint fk_keys,
123 const Sql_check_constraint_spec_list *check_cons_spec, handler *file);
124
125/**
126 Prepares a dd::Table object for a temporary table from
127 mysql_prepare_create_table() output. Doesn't update DD tables,
128 instead returns dd::Table object to caller.
129
130 @param thd Thread handle.
131 @param sch_obj Schema.
132 @param table_name Table name.
133 @param create_info HA_CREATE_INFO describing the table to be created.
134 @param create_fields List of fields for the table.
135 @param keyinfo Array with descriptions of keys for the table.
136 @param keys Number of keys.
137 @param keys_onoff Enable or disable keys.
138 @param check_cons_spec Specification of check constraints for the table.
139 @param file handler instance for the table.
140
141 @returns Constructed dd::Table object, or nullptr in case of an error.
142*/
143std::unique_ptr<dd::Table> create_tmp_table(
144 THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name,
145 HA_CREATE_INFO *create_info, const List<Create_field> &create_fields,
146 const KEY *keyinfo, uint keys,
148 const Sql_check_constraint_spec_list *check_cons_spec, handler *file);
149
150//////////////////////////////////////////////////////////////////////////
151// Function common to 'table' and 'view' objects
152//////////////////////////////////////////////////////////////////////////
153
154/*
155 Remove table metadata from the data dictionary.
156
157 @param thd Thread context.
158 @param schema_name Schema of the table to be removed.
159 @param name Name of the table to be removed.
160 @param table_def dd::Table object for the table to be removed.
161
162 @note The caller must rollback both statement and transaction on failure,
163 before any further accesses to DD. This is because such a failure
164 might be caused by a deadlock, which requires rollback before any
165 other operations on SE (including reads using attachable transactions)
166 can be done.
167
168 @retval false on success
169 @retval true on failure
170*/
171bool drop_table(THD *thd, const char *schema_name, const char *name,
172 const dd::Table &table_def);
173/**
174 Check if a table or view exists
175
176 table_exists() sets exists=true if such a table or a view exists.
177
178 @param client The dictionary client.
179 @param schema_name The schema in which the object should be defined.
180 @param name The object name to search for.
181 @param [out] exists A boolean which is set to true if the object
182 is found.
183
184 @retval true Failure (error has been reported).
185 @retval false Success.
186*/
187bool table_exists(dd::cache::Dictionary_client *client, const char *schema_name,
188 const char *name, bool *exists);
189
190/**
191 Checking if the table is being created in a restricted
192 tablespace.
193
194 @param thd Thread context.
195 @param schema_name Name of the schema where the table is located.
196 @param table_name Name of the table.
197 @param create_info Table options, e.g. the tablespace name.
198
199 @retval true Invalid usage (error has been reported).
200 @retval false Success, no invalid usage.
201*/
202bool invalid_tablespace_usage(THD *thd, const dd::String_type &schema_name,
204 const HA_CREATE_INFO *create_info);
205
206/**
207 Check if foreign key name is generated one.
208
209 @param table_name Table name.
210 @param table_name_length Table name length.
211 @param hton Table storage engine.
212 @param fk Foreign key to be checked.
213
214 @note We assume that the name is generated if it starts with
215 (table name)(SE-specific or default foreign key name suffix)
216 (e.g. "_ibfk_" for InnoDB or "_fk_" for NDB).
217
218 @returns true if name is generated, false otherwise.
219*/
220
222 size_t table_name_length, handlerton *hton,
223 const dd::Foreign_key &fk);
224
225/**
226 Rename foreign keys which have generated names to
227 match the new name of the table.
228
229 @param thd Thread context.
230 @param old_db Table's database before rename.
231 @param old_table_name Table name before rename.
232 @param hton Table's storage engine.
233 @param new_db Table's database after rename.
234 @param new_tab New version of the table with new name set.
235
236 @returns true if error, false otherwise.
237*/
238
239bool rename_foreign_keys(THD *thd, const char *old_db,
240 const char *old_table_name, handlerton *hton,
241 const char *new_db, dd::Table *new_tab);
242
243//////////////////////////////////////////////////////////////////////////
244// Functions for retrieving, inspecting and manipulating instances of
245// dd::Abstract_table, dd::Table and dd::View
246//////////////////////////////////////////////////////////////////////////
247
248/**
249 Get the legacy db type from the options of the given table.
250
251 This function does not set error codes beyond what is set by the
252 functions it calls.
253
254 @param[in] thd Thread context
255 @param[in] schema_name Name of the schema
256 @param[in] table_name Name of the table
257 @param[out] db_type Value of the legacy db type option
258
259 @retval true Error, e.g. name is not a table, or no
260 legacy_db_type in the table's options.
261 In this case, the value of db_type is
262 undefined.
263 @retval false Success
264*/
265bool table_legacy_db_type(THD *thd, const char *schema_name,
266 const char *table_name, enum legacy_db_type *db_type);
267
268/**
269 Get the storage engine handlerton for the given table or tablespace.
270
271 This function sets explicit error codes if:
272 - The SE is invalid: ER_UNKNOWN_STORAGE_ENGINE
273
274 @param[in] thd Thread context
275 @param[in] obj dd::Table or a dd::Tablespace object.
276 @param[out] hton Handlerton for the table's storage engine
277
278 @retval true Error
279 @retval false Success
280*/
281template <typename T>
282bool table_storage_engine(THD *thd, const T *obj, handlerton **hton);
283
284/**
285 Regenerate a metadata locked table.
286
287 This function does not set error codes beyond what is set by the
288 functions it calls.
289
290 @pre There must be an exclusive MDL lock on the table.
291
292 @param[in] thd Thread context
293 @param[in] schema_name Name of the schema
294 @param[in] table_name Name of the table
295
296 @retval false Success
297 @retval true Error
298*/
299bool recreate_table(THD *thd, const char *schema_name, const char *table_name);
300
301/**
302 Function prepares string representing columns data type.
303 This is required for IS implementation which uses views on DD tables
304*/
306 uint32 field_length, uint32 decimals,
307 bool maybe_null, bool is_unsigned,
308 const CHARSET_INFO *field_charset);
309
310/**
311 Convert field type from MySQL server type to new enum types in DD.
312 We have plans to retain both old and new enum values in DD tables so as
313 to handle client compatibility and information schema requirements.
314
315 @param[in] type MySQL server field type.
316
317 @retval field type used by DD framework.
318*/
319
321
322/**
323 Update row format for the table with the value
324 value supplied by caller function.
325
326 @pre There must be an exclusive MDL lock on the table.
327
328 @param[in] thd Thread context.
329 @param[in] table Table object for the table.
330 @param[in] correct_row_type row_type to be set.
331
332 @retval false Success
333 @retval true Error
334*/
335bool fix_row_type(THD *thd, dd::Table *table, row_type correct_row_type);
336
337/**
338 Add column objects to dd::Abstract_table objects according to the
339 list of Create_field objects.
340
341 @param thd Thread handle.
342 @param tab_obj dd::Table or dd::View's instance.
343 @param create_fields List of Create_field objects to fill
344 dd::Column object(s).
345 @param file handler instance for the table.
346
347 @retval false On Success
348 @retval true On error.
349*/
350
352 const List<Create_field> &create_fields,
353 handler *file);
354
355/**
356 @brief Function returns string representing column type by Create_field.
357 This is required for the IS implementation which uses views on DD
358 tables
359
360 @param[in] table TABLE object.
361 @param[in] field Column information.
362
363 @return dd::String_type representing column type.
364*/
365
367 const Create_field &field);
368
369/**
370 Helper method to get numeric scale for types using Create_field type
371 object.
372
373 @param[in] field Field object.
374 @param[out] scale numeric scale value for types.
375
376 @retval false If numeric scale is calculated.
377 @retval true If numeric scale is not calculated;
378*/
379
380bool get_field_numeric_scale(const Create_field *field, uint *scale);
381
382/**
383 Helper method to get numeric precision for types using Create_field type
384 object.
385
386 @param[in] field Field object.
387 @param[out] numeric_precision numeric precision value for types.
388
389 @retval false If numeric precision is calculated.
390 @retval true If numeric precision is not calculated;
391*/
392
394 uint *numeric_precision);
395
396/**
397 Helper method to get datetime precision for types using Create_field type
398 object.
399
400 @param[in] field Field object.
401 @param[out] datetime_precision datetime precision value for types.
402
403 @retval false If datetime precision is calculated.
404 @retval true If datetime precision is not calculated;
405*/
406
408 uint *datetime_precision);
409
410/*
411 Does ENCRYPTION clause mean unencrypted or encrypted table ?
412
413 @note SQL server expects value '', 'N' or 'n' to represent unecnryption.
414 We do not consider 'Y'/'y' as encryption, so that this allows storage
415 engines to accept any other string like 'aes' or other string to
416 represent encryption type.
417
418 @param type - String given in ENCRYPTION clause.
419
420 @return true if table would be encrypted, else false.
421*/
422inline bool is_encrypted(const String_type &type) {
423 return (type.empty() == false && type != "" && type != "N" && type != "n");
424}
425
426inline bool is_encrypted(const LEX_STRING &type) {
427 return is_encrypted(String_type(type.str, type.length));
428}
429
432 bool *found_tablespace);
433
436 bool *found_tablespace);
437
438/**
439 Predicate which indicates if the table has real (non-hidden) primary key.
440
441 @param t table to check
442 @return true if a non-hidden index has type dd::Index::IT_PRIMARY
443 */
444bool has_primary_key(const Table &t);
445
446/**
447 Check if name of check constraint is generated one.
448
449 @param table_name Table name.
450 @param table_name_length Table name length.
451 @param cc_name Check constraint name.
452 @param cc_name_length Check constraint name length.
453
454 @retval true If check constraint name is generated one.
455 @retval false Otherwise.
456*/
458 size_t table_name_length,
459 const char *cc_name,
460 size_t cc_name_length);
461
462/**
463 Rename generated check constraint names to match the new name of the table.
464
465 @param old_table_name Table name before rename.
466 @param new_tab New version of the table with new name set.
467
468 @returns true if error, false otherwise.
469*/
470bool rename_check_constraints(const char *old_table_name, dd::Table *new_tab);
471
472/**
473 Check if table uses general tablespace.
474
475 @param t dd::Table instance.
476
477 @returns true if table users general tablespace, false otherwise.
478*/
479bool uses_general_tablespace(const Table &t);
480
481/**
482 Check and give error if table uses prefix keys in the partitioning function.
483
484 @param schema_name Schema name
485 @param orig_table_name Original table name (required in case of ALTER TABLE,
486 since temporary table name is created)
487 @param table dd::Table instance
488 @param is_upgrade True if this is called during upgrade. Error will be
489 sent to error log instead of the client.
490
491 @return true - On failure
492 @return false - On success
493
494*/
495bool prefix_key_partition_exists(const char *schema_name,
496 const char *orig_table_name,
497 const Table *table, const bool is_upgrade);
498
499/**
500 Get the autoextend_size option value for implicit tablespaces
501 @param thd Thread handler
502 @param table dd::Table instance
503 @param autoextend_size Value of autoextend_size attribute
504
505 @return true - On failure
506 @return false - On success
507*/
509 ulonglong *autoextend_size);
510
511/**
512 Validate if table uses foreign keys referring to proper index.
513 FK cannot refer to non unique index and partial elements in index.
514
515 @param thd Thread handle.
516 @param table dd::Table instance of referenced table
517*/
519} // namespace dd
520#endif // DD_TABLE_INCLUDED
enum_enable_or_disable
Definition: sql_alter.h:349
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:51
Definition: key.h:43
Definition: key.h:113
Definition: sql_list.h:467
Class to represent the check constraint specifications obtained from the SQL statement parse.
Definition: sql_check_constraint.h:43
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Abstract base class for tables and views.
Definition: abstract_table.h:69
Definition: foreign_key.h:47
Definition: schema.h:63
Definition: table.h:47
Definition: dictionary_client.h:149
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4573
A table definition from the master.
Definition: rpl_utility.h:248
enum_field_types
Column types for MySQL Note: Keep include/mysql/components/services/bits/stored_program_bits....
Definition: field_types.h:55
static uint keys
Definition: hp_test2.cc:49
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
uint32_t uint32
Definition: my_inttypes.h:67
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
borrowable::session_track::Schema< true > Schema
Definition: classic_protocol_session_track.h:288
constexpr value_type is_unsigned
Definition: classic_protocol_constants.h:273
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
dd::String_type get_sql_type_by_create_field(TABLE *table, const Create_field &field)
Function returns string representing column type by Create_field.
Definition: dd_table.cc:238
bool fill_dd_columns_from_create_fields(THD *thd, dd::Abstract_table *tab_obj, const List< Create_field > &create_fields, handler *file)
Add column objects to dd::Abstract_table according to list of Create_field objects.
Definition: dd_table.cc:499
bool table_legacy_db_type(THD *thd, const char *schema_name, const char *table_name, enum legacy_db_type *db_type)
Get the legacy db type from the options of the given table.
Definition: dd_table.cc:2568
bool table_storage_engine(THD *thd, const T *obj, handlerton **hton)
Get the storage engine handlerton for the given table or tablespace.
Definition: dd_table.cc:2615
bool uses_general_tablespace(const Table &t)
Check if table uses general tablespace.
Definition: dd_table.cc:2967
bool fix_row_type(THD *thd, dd::Table *table_def, row_type correct_row_type)
Update row format for the table with the value value supplied by caller function.
Definition: dd_table.cc:2703
std::unique_ptr< dd::Table > create_dd_user_table(THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name, HA_CREATE_INFO *create_info, const List< Create_field > &create_fields, const KEY *keyinfo, uint keys, Alter_info::enum_enable_or_disable keys_onoff, const FOREIGN_KEY *fk_keyinfo, uint fk_keys, const Sql_check_constraint_spec_list *check_cons_spec, handler *file)
Prepares a dd::Table object from mysql_prepare_create_table() output and return it to the caller.
Definition: dd_table.cc:2383
static const char CHECK_CONSTRAINT_NAME_SUBSTR[]
Definition: dd_table.h:66
bool table_exists(dd::cache::Dictionary_client *client, const char *schema_name, const char *name, bool *exists)
Check if a table or view exists.
Definition: dd_table.cc:2458
enum_column_types
Definition: column.h:53
bool rename_check_constraints(const char *old_table_name, dd::Table *new_tab)
Rename generated check constraint names to match the new name of the table.
Definition: dd_table.cc:2844
bool get_field_numeric_precision(const Create_field *field, uint *numeric_precision)
Helper method to get numeric precision for types using Create_field type object.
Definition: dd_table.cc:407
bool is_generated_check_constraint_name(const char *table_name, size_t table_name_length, const char *cc_name, size_t cc_name_length)
Check if name of check constraint is generated one.
Definition: dd_table.cc:2832
static const char FIELD_NAME_SEPARATOR_CHAR
Definition: dd_table.h:65
dd::String_type get_sql_type_by_field_info(THD *thd, enum_field_types field_type, uint32 field_length, uint32 decimals, bool maybe_null, bool is_unsigned, const CHARSET_INFO *field_charset)
Function returns string representing column type by ST_FIELD_INFO.
Definition: dd_table.cc:2682
bool recreate_table(THD *thd, const char *schema_name, const char *table_name)
Regenerate a metadata locked table.
Definition: dd_table.cc:2640
bool invalid_tablespace_usage(THD *thd, const dd::String_type &schema_name, const dd::String_type &table_name, const HA_CREATE_INFO *create_info)
Checking if the table is being created in a restricted tablespace.
Definition: dd_table.cc:1907
bool prefix_key_partition_exists(const char *schema_name, const char *orig_table_name, const Table *table, const bool is_upgrade)
Check and give error if table uses prefix keys in the partitioning function.
Definition: dd_table.cc:2981
bool get_field_numeric_scale(const Create_field *field, uint *scale)
Helper method to get numeric scale for types using Create_field type object.
Definition: dd_table.cc:374
bool is_encrypted(const String_type &type)
Definition: dd_table.h:422
bool get_field_datetime_precision(const Create_field *field, uint *datetime_precision)
Helper method to get datetime precision for types using Create_field type object.
Definition: dd_table.cc:458
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
dd::enum_column_types get_new_field_type(enum_field_types type)
Convert to and from new enum types in DD framework to current MySQL server enum types.
Definition: dd_table.cc:124
bool check_non_standard_key_exists_in_fk(THD *thd, const Table *table)
Validate if table uses foreign keys referring to proper index.
Definition: dd_table.cc:3161
Encrypt_result is_tablespace_encrypted(THD *thd, const Table &t, bool *is_general_tablespace)
Predicate to determine if a table resides in an encrypted tablespace.
Definition: dd_table.cc:2791
bool drop_table(THD *thd, const char *schema_name, const char *name, const dd::Table &table_def)
Definition: dd_table.cc:2452
std::unique_ptr< dd::Table > create_table(THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name, HA_CREATE_INFO *create_info, const List< Create_field > &create_fields, const KEY *keyinfo, uint keys, Alter_info::enum_enable_or_disable keys_onoff, const FOREIGN_KEY *fk_keyinfo, uint fk_keys, const Sql_check_constraint_spec_list *check_cons_spec, handler *file)
Prepares a dd::Table object from mysql_prepare_create_table() output and return it to the caller.
Definition: dd_table.cc:2412
bool is_generated_foreign_key_name(const char *table_name, size_t table_name_length, handlerton *hton, const dd::Foreign_key &fk)
Check if foreign key name is generated one.
Definition: dd_table.cc:2475
bool get_implicit_tablespace_options(THD *thd, const Table *table, ulonglong *autoextend_size)
Get the autoextend_size option value for implicit tablespaces.
Definition: dd_table.cc:3082
bool rename_foreign_keys(THD *thd, const char *old_db, const char *old_table_name, handlerton *hton, const char *new_db, dd::Table *new_tab)
Rename foreign keys which have generated names to match the new name of the table.
Definition: dd_table.cc:2506
bool has_primary_key(const Table &t)
Predicate which indicates if the table has real (non-hidden) primary key.
Definition: dd_table.cc:2825
std::unique_ptr< dd::Table > create_tmp_table(THD *thd, const dd::Schema &sch_obj, const dd::String_type &table_name, HA_CREATE_INFO *create_info, const List< Create_field > &create_fields, const KEY *keyinfo, uint keys, Alter_info::enum_enable_or_disable keys_onoff, const Sql_check_constraint_spec_list *check_cons_spec, handler *file)
Prepares a dd::Table object for a temporary table from mysql_prepare_create_table() output.
Definition: dd_table.cc:2432
Definition: os0file.h:89
const char * table_name
Definition: rules_table_service.cc:56
static int exists(node_address *name, node_list const *nodes, u_int with_uid)
Definition: node_list.cc:106
required string type
Definition: replication_group_member_actions.proto:34
row_type
Definition: handler.h:684
legacy_db_type
Definition: handler.h:648
case opt name
Definition: sslopt-case.h:29
Definition: m_ctype.h:423
Struct to hold information about the table that should be created.
Definition: handler.h:3202
Definition: mysql_lex_string.h:35
Definition: table.h:1406
Template for structs to hold results from functions.
Definition: result_type.h:33
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2734