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