MySQL  8.0.17
Source Code Documentation
dd_table.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 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 
37 class Create_field;
38 class FOREIGN_KEY;
39 class KEY;
40 class THD;
41 namespace dd {
42 class Schema;
43 } // namespace dd
44 struct TABLE;
45 
49 
50 struct HA_CREATE_INFO;
51 template <class T>
52 class List;
53 
54 namespace dd {
55 class Abstract_table;
56 class Foreign_key;
57 class Table;
58 
59 namespace cache {
60 class Dictionary_client;
61 }
62 
63 static const char FIELD_NAME_SEPARATOR_CHAR = ';';
64 static const char FOREIGN_KEY_NAME_SUBSTR[] = "_ibfk_";
65 static 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 */
88 std::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 */
116 std::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 */
142 std::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 */
170 bool 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 */
186 bool 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 tha 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 */
201 bool 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 fk Foreign key to be checked.
211 
212  @note We assume that the name is generated if it starts with
213  *table_name*_ibfk_. i.e. follow InnoDB approach.
214 
215  @returns true if name is generated, false otherwise.
216 */
217 
219  size_t table_name_length,
220  const dd::Foreign_key &fk);
221 
222 /**
223  Rename foreign keys which have generated names to
224  match the new name of the table.
225 
226  @param thd Thread context.
227  @param old_db Table's database before rename.
228  @param old_table_name Table name before rename.
229  @param new_db Table's database after rename.
230  @param new_tab New version of the table with new name set.
231 
232  @todo Implement new naming scheme (or move responsibility of
233  naming to the SE layer).
234 
235  @returns true if error, false otherwise.
236 */
237 
238 bool rename_foreign_keys(THD *thd, const char *old_db,
239  const char *old_table_name, const char *new_db,
240  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 */
264 bool 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 */
280 template <typename T>
281 bool 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 */
298 bool 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 */
334 bool 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 
367 /**
368  Helper method to get numeric scale for types using Create_field type
369  object.
370 
371  @param[in] field Field object.
372  @param[out] scale numeric scale value for types.
373 
374  @retval false If numeric scale is calculated.
375  @retval true If numeric scale is not calculated;
376 */
377 
378 bool get_field_numeric_scale(const Create_field *field, uint *scale);
379 
380 /**
381  Helper method to get numeric precision for types using Create_field type
382  object.
383 
384  @param[in] field Field object.
385  @param[out] numeric_precision numeric precision value for types.
386 
387  @retval false If numeric precision is calculated.
388  @retval true If numeric precision is not calculated;
389 */
390 
391 bool get_field_numeric_precision(const Create_field *field,
392  uint *numeric_precision);
393 
394 /**
395  Helper method to get datetime precision for types using Create_field type
396  object.
397 
398  @param[in] field Field object.
399  @param[out] datetime_precision datetime precision value for types.
400 
401  @retval false If datetime precision is calculated.
402  @retval true If datetime precision is not calculated;
403 */
404 
406  uint *datetime_precision);
407 
408 /*
409  Does ENCRYPTION clause mean unencrypted or encrypted table ?
410 
411  @note SQL server expects value '', 'N' or 'n' to represent unecnryption.
412  We do not consider 'Y'/'y' as encryption, so that this allows storage
413  engines to accept any other string like 'aes' or other string to
414  represent encryption type.
415 
416  @param type - String given in ENCRYPTION clause.
417 
418  @return true if table would be encrypted, else false.
419 */
420 inline bool is_encrypted(const String_type &type) {
421  return (type.empty() == false && type != "" && type != "N" && type != "n");
422 }
423 
424 inline bool is_encrypted(const LEX_STRING &type) {
425  return is_encrypted(String_type(type.str, type.length));
426 }
427 
430  bool *found_tablespace);
431 
434  bool *found_tablespace);
435 
436 /**
437  Predicate which indicates if the table has real (non-hidden) primary key.
438 
439  @param t table to check
440  @return true if a non-hidden index has type dd::Index::IT_PRIMARY
441  */
442 bool has_primary_key(const Table &t);
443 
444 /**
445  Check if name of check constraint is generated one.
446 
447  @param table_name Table name.
448  @param table_name_length Table name length.
449  @param cc_name Check constraint name.
450  @param cc_name_length Check constraint name length.
451 
452  @retval true If check constraint name is generated one.
453  @retval false Otherwise.
454 */
456  size_t table_name_length,
457  const char *cc_name,
458  size_t cc_name_length);
459 
460 /**
461  Rename generated check constraint names to match the new name of the table.
462 
463  @param old_table_name Table name before rename.
464  @param new_tab New version of the table with new name set.
465 
466  @returns true if error, false otherwise.
467 */
468 bool rename_check_constraints(const char *old_table_name, dd::Table *new_tab);
469 
470 /**
471  Check if table uses general tablespace.
472 
473  @param t dd::Table instance.
474 
475  @returns true if table users general tablespace, false otherwise.
476 */
477 bool uses_general_tablespace(const Table &t);
478 
479 } // namespace dd
480 #endif // DD_TABLE_INCLUDED
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:395
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:2318
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:116
Definition: mysql_lex_string.h:34
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:446
enum_enable_or_disable
Definition: sql_alter.h:314
const string name("\ame\)
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:2636
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:2368
Some integer typedefs for easier portability.
Class to represent the check constraint specifications obtained from the SQL statement parse...
Definition: sql_check_constraint.h:42
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:2394
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:50
dd::String_type get_sql_type_by_create_field(TABLE *table, Create_field *field)
Function returns string representing column type by Create_field.
Definition: dd_table.cc:228
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:2724
bool is_generated_foreign_key_name(const char *table_name, size_t table_name_length, const dd::Foreign_key &fk)
Check if foreign key name is generated one.
Definition: dd_table.cc:2411
enum_column_types
Definition: column.h:52
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:3987
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:2548
Definition: table.h:1294
bool recreate_table(THD *thd, const char *schema_name, const char *table_name)
Regenerate a metadata locked table.
Definition: dd_table.cc:2573
bool is_encrypted(const String_type &type)
Definition: dd_table.h:420
Template for structs to hold results from functions.
Definition: result_type.h:32
Definition: aggregate_check.h:523
Definition: dictionary_client.h:145
static const char FIELD_NAME_SEPARATOR_CHAR
Definition: dd_table.h:63
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
static uint keys
Definition: hp_test2.cc:43
unsigned int uint
Definition: uca-dump.cc:29
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:2501
bool has_primary_key(const Table &t)
Predicate which indicates if the table has real (non-hidden) primary key.
Definition: dd_table.cc:2758
legacy_db_type
Definition: handler.h:635
t
Definition: dbug_analyze.cc:147
uint32_t uint32
Definition: my_inttypes.h:62
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2275
bool rename_foreign_keys(THD *thd, const char *old_db, const char *old_table_name, 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:2439
Definition: m_ctype.h:359
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:397
Abstract base class for tables and views.
Definition: abstract_table.h:68
static const char FOREIGN_KEY_NAME_SUBSTR[]
Definition: dd_table.h:64
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:1874
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:2615
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:2348
static MI_KEYDEF keyinfo[10]
Definition: mi_test1.cc:46
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:487
int type
Definition: http_common.h:411
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:111
Definition: key.h:41
Definition: handler.h:2607
Definition: schema.h:61
static const char CHECK_CONSTRAINT_NAME_SUBSTR[]
Definition: dd_table.h:65
Definition: test_sql_stmt.cc:150
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
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:2765
A table definition from the master.
Definition: rpl_utility.h:245
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:362
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:2777
bool drop_table(THD *thd, const char *schema_name, const char *name, const dd::Table &table_def)
Definition: dd_table.cc:2388
Definition: table.h:43
Definition: foreign_key.h:46
bool uses_general_tablespace(const Table &t)
Check if table uses general tablespace.
Definition: dd_table.cc:2900
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777
row_type
Definition: handler.h:671
const char * table_name
Definition: rules_table_service.cc:55