File: dd_table.h

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (527 lines) | stat: -rw-r--r-- 19,743 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* Copyright (c) 2015, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef DD_TABLE_INCLUDED
#define DD_TABLE_INCLUDED

#include <sys/types.h>
#include <memory>  // std:unique_ptr
#include <string>

#include "my_inttypes.h"
#include "sql/dd/result_type.h"  // dd::ResultType
#include "sql/dd/string_type.h"
#include "sql/dd/types/column.h"  // dd::enum_column_types
#include "sql/handler.h"          // legacy_db_type
#include "sql/sql_alter.h"        // Alter_info::enum_enable_or_disable

class Create_field;
class FOREIGN_KEY;
class KEY;
class THD;
namespace dd {
class Schema;
}  // namespace dd
struct TABLE;

class Sql_check_constraint_spec;
using Sql_check_constraint_spec_list =
    Mem_root_array<Sql_check_constraint_spec *>;

struct HA_CREATE_INFO;
template <class T>
class List;

namespace dd {
class Abstract_table;
class Foreign_key;
class Table;

namespace cache {
class Dictionary_client;
}

static const char FIELD_NAME_SEPARATOR_CHAR = ';';
static const char CHECK_CONSTRAINT_NAME_SUBSTR[] = "_chk_";

/**
  Prepares a dd::Table object from mysql_prepare_create_table() output
  and return it to the caller. This function creates a user table, as
  opposed to create_table() which can handle system tables as well.

  @param thd                Thread handle
  @param sch_obj            Schema.
  @param table_name         Table name.
  @param create_info        HA_CREATE_INFO describing the table to be created.
  @param create_fields      List of fields for the table.
  @param keyinfo            Array with descriptions of keys for the table.
  @param keys               Number of keys.
  @param keys_onoff         keys ON or OFF
  @param fk_keyinfo         Array with descriptions of foreign keys for the
                            table.
  @param fk_keys            Number of foreign keys.
  @param check_cons_spec    Specification of check constraints for the table.
  @param file               handler instance for the table.

  @returns Constructed dd::Table object, or nullptr in case of an error.
*/
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.

  @param thd                Thread handle
  @param sch_obj            Schema.
  @param table_name         Table name.
  @param create_info        HA_CREATE_INFO describing the table to be created.
  @param create_fields      List of fields for the table.
  @param keyinfo            Array with descriptions of keys for the table.
  @param keys               Number of keys.
  @param keys_onoff         Enable or disable keys.
  @param fk_keyinfo         Array with descriptions of foreign keys for the
  table.
  @param fk_keys            Number of foreign keys.
  @param check_cons_spec    Specification of check constraints for the table.
  @param file               handler instance for the table.

  @returns Constructed dd::Table object, or nullptr in case of an error.
*/
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 for a temporary table from
  mysql_prepare_create_table() output. Doesn't update DD tables,
  instead returns dd::Table object to caller.

  @param thd              Thread handle.
  @param sch_obj          Schema.
  @param table_name       Table name.
  @param create_info      HA_CREATE_INFO describing the table to be created.
  @param create_fields    List of fields for the table.
  @param keyinfo          Array with descriptions of keys for the table.
  @param keys             Number of keys.
  @param keys_onoff       Enable or disable keys.
  @param check_cons_spec  Specification of check constraints for the table.
  @param file             handler instance for the table.

  @returns Constructed dd::Table object, or nullptr in case of an error.
*/
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);

//////////////////////////////////////////////////////////////////////////
// Function common to 'table' and 'view' objects
//////////////////////////////////////////////////////////////////////////

/*
  Remove table metadata from the data dictionary.

  @param thd                Thread context.
  @param schema_name        Schema of the table to be removed.
  @param name               Name of the table to be removed.
  @param table_def          dd::Table object for the table to be removed.

  @note The caller must rollback both statement and transaction on failure,
        before any further accesses to DD. This is because such a failure
        might be caused by a deadlock, which requires rollback before any
        other operations on SE (including reads using attachable transactions)
        can be done.

  @retval false on success
  @retval true on failure
*/
bool drop_table(THD *thd, const char *schema_name, const char *name,
                const dd::Table &table_def);
/**
  Check if a table or view exists

  table_exists() sets exists=true if such a table or a view exists.

  @param       client       The dictionary client.
  @param       schema_name  The schema in which the object should be defined.
  @param       name         The object name to search for.
  @param [out] exists       A boolean which is set to true if the object
                            is found.

  @retval      true         Failure (error has been reported).
  @retval      false        Success.
*/
bool table_exists(dd::cache::Dictionary_client *client, const char *schema_name,
                  const char *name, bool *exists);

/**
  Checking if the table is being created in a restricted
  tablespace.

  @param thd            Thread context.
  @param schema_name    Name of the schema where the table is located.
  @param table_name     Name of the table.
  @param create_info    Table options, e.g. the tablespace name.

  @retval  true         Invalid usage (error has been reported).
  @retval  false        Success, no invalid usage.
*/
bool invalid_tablespace_usage(THD *thd, const dd::String_type &schema_name,
                              const dd::String_type &table_name,
                              const HA_CREATE_INFO *create_info);

/**
  Check if foreign key name is generated one.

  @param table_name         Table name.
  @param table_name_length  Table name length.
  @param hton               Table storage engine.
  @param fk                 Foreign key to be checked.

  @note We assume that the name is generated if it starts with
        (table name)(SE-specific or default foreign key name suffix)
        (e.g. "_ibfk_" for InnoDB or "_fk_" for NDB).

  @returns true if name is generated, false otherwise.
*/

bool is_generated_foreign_key_name(const char *table_name,
                                   size_t table_name_length, handlerton *hton,
                                   const dd::Foreign_key &fk);

/**
  Rename foreign keys which have generated names to
  match the new name of the table.

  @param thd             Thread context.
  @param old_db          Table's database before rename.
  @param old_table_name  Table name before rename.
  @param hton            Table's storage engine.
  @param new_db          Table's database after rename.
  @param new_tab         New version of the table with new name set.

  @returns true if error, false otherwise.
*/

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);

//////////////////////////////////////////////////////////////////////////
// Functions for retrieving, inspecting and manipulating instances of
// dd::Abstract_table, dd::Table and dd::View
//////////////////////////////////////////////////////////////////////////

/**
  Get the legacy db type from the options of the given table.

  This function does not set error codes beyond what is set by the
  functions it calls.

  @param[in]  thd             Thread context
  @param[in]  schema_name     Name of the schema
  @param[in]  table_name      Name of the table
  @param[out] db_type         Value of the legacy db type option

  @retval     true            Error, e.g. name is not a table, or no
                              legacy_db_type in the table's options.
                              In this case, the value of db_type is
                              undefined.
  @retval     false           Success
*/
bool table_legacy_db_type(THD *thd, const char *schema_name,
                          const char *table_name, enum legacy_db_type *db_type);

/**
  Get the storage engine handlerton for the given table or tablespace.

  This function sets explicit error codes if:
  - The SE is invalid:      ER_UNKNOWN_STORAGE_ENGINE

  @param[in]  thd             Thread context
  @param[in]  obj             dd::Table or a dd::Tablespace object.
  @param[out] hton            Handlerton for the table's storage engine

  @retval     true            Error
  @retval     false           Success
*/
template <typename T>
bool table_storage_engine(THD *thd, const T *obj, handlerton **hton);

/**
  Regenerate a metadata locked table.

  This function does not set error codes beyond what is set by the
  functions it calls.

  @pre There must be an exclusive MDL lock on the table.

  @param[in]    thd                 Thread context
  @param[in]    schema_name         Name of the schema
  @param[in]    table_name          Name of the table

  @retval       false       Success
  @retval       true        Error
*/
bool recreate_table(THD *thd, const char *schema_name, const char *table_name);

/**
  Function prepares string representing columns data type.
  This is required for IS implementation which uses views on DD tables
*/
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);

/**
  Convert field type from MySQL server type to new enum types in DD.
  We have plans to retain both old and new enum values in DD tables so as
  to handle client compatibility and information schema requirements.

  @param[in]    type   MySQL server field type.

  @retval  field type used by DD framework.
*/

enum_column_types get_new_field_type(enum_field_types type);

/**
  Update row format for the table with the value
  value supplied by caller function.

  @pre There must be an exclusive MDL lock on the table.

  @param[in]    thd              Thread context.
  @param[in]    table            Table object for the table.
  @param[in]    correct_row_type row_type to be set.

  @retval       false       Success
  @retval       true        Error
*/
bool fix_row_type(THD *thd, dd::Table *table, row_type correct_row_type);

/**
  Add column objects to dd::Abstract_table objects according to the
  list of Create_field objects.

  @param   thd              Thread handle.
  @param   tab_obj          dd::Table or dd::View's instance.
  @param   create_fields    List of Create_field objects to fill
                            dd::Column object(s).
  @param   file             handler instance for the table.

  @retval  false            On Success
  @retval  true             On error.
*/

bool fill_dd_columns_from_create_fields(THD *thd, Abstract_table *tab_obj,
                                        const List<Create_field> &create_fields,
                                        handler *file);

/**
  @brief Function returns string representing column type by Create_field.
         This is required for the IS implementation which uses views on DD
         tables

  @param[in]   table           TABLE object.
  @param[in]   field           Column information.

  @return dd::String_type representing column type.
*/

dd::String_type get_sql_type_by_create_field(TABLE *table,
                                             const Create_field &field);

/**
  Helper method to get numeric scale for types using Create_field type
  object.

  @param[in]  field      Field object.
  @param[out] scale      numeric scale value for types.

  @retval false  If numeric scale is calculated.
  @retval true   If numeric scale is not calculated;
*/

bool get_field_numeric_scale(const Create_field *field, uint *scale);

/**
  Helper method to get numeric precision for types using Create_field type
  object.

  @param[in]  field             Field object.
  @param[out] numeric_precision numeric precision value for types.

  @retval false  If numeric precision is calculated.
  @retval true   If numeric precision is not calculated;
*/

bool get_field_numeric_precision(const Create_field *field,
                                 uint *numeric_precision);

/**
  Helper method to get datetime precision for types using Create_field type
  object.

  @param[in]  field              Field object.
  @param[out] datetime_precision datetime precision value for types.

  @retval false  If datetime precision is calculated.
  @retval true   If datetime precision is not calculated;
*/

bool get_field_datetime_precision(const Create_field *field,
                                  uint *datetime_precision);

/*
  Does ENCRYPTION clause mean unencrypted or encrypted table ?

  @note SQL server expects value '', 'N' or 'n' to represent unecnryption.
  We do not consider 'Y'/'y' as encryption, so that this allows storage
  engines to accept any other string like 'aes' or other string to
  represent encryption type.

  @param type - String given in ENCRYPTION clause.

  @return true if table would be encrypted, else false.
*/
inline bool is_encrypted(const String_type &type) {
  return (type.empty() == false && type != "" && type != "N" && type != "n");
}

inline bool is_encrypted(const LEX_STRING &type) {
  return is_encrypted(String_type(type.str, type.length));
}

using Encrypt_result = ResultType<bool>;
Encrypt_result is_tablespace_encrypted(THD *thd, const dd::Table &t,
                                       bool *found_tablespace);

using Encrypt_result = ResultType<bool>;
Encrypt_result is_tablespace_encrypted(THD *thd, const HA_CREATE_INFO *ci,
                                       bool *found_tablespace);

/**
  Predicate which indicates if the table has real (non-hidden) primary key.

  @param t table to check
  @return true if a non-hidden index has type dd::Index::IT_PRIMARY
 */
bool has_primary_key(const Table &t);

/**
  Check if name of check constraint is generated one.

  @param    table_name         Table name.
  @param    table_name_length  Table name length.
  @param    cc_name            Check constraint name.
  @param    cc_name_length     Check constraint name length.

  @retval   true               If check constraint name is generated one.
  @retval   false              Otherwise.
*/
bool is_generated_check_constraint_name(const char *table_name,
                                        size_t table_name_length,
                                        const char *cc_name,
                                        size_t cc_name_length);

/**
  Rename generated check constraint names to match the new name of the table.

  @param old_table_name  Table name before rename.
  @param new_tab         New version of the table with new name set.

  @returns true if error, false otherwise.
*/
bool rename_check_constraints(const char *old_table_name, dd::Table *new_tab);

/**
  Check if table uses general tablespace.

  @param   t    dd::Table instance.

  @returns true if table users general tablespace, false otherwise.
*/
bool uses_general_tablespace(const Table &t);

/**
  Throw deprecation warnings if table uses prefix keys in the partitioning
  function.

  @param  thd             Thread handler
  @param  schema_name     Schema name
  @param  orig_table_name Original table name (required in case of ALTER TABLE,
  since temporary table name is created)
  @param  table           dd::Table instance
  @param  is_upgrade      True if this is called during upgrade. Warning will be
  sent to error log instead of the client.

*/
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);

/**
  Get the autoextend_size option value for implicit tablespaces
  @param  thd             Thread handler
  @param  table           dd::Table instance
  @param  autoextend_size Value of autoextend_size attribute

  @return true  - On failure
  @return false - On success
*/
bool get_implicit_tablespace_options(THD *thd, const Table *table,
                                     ulonglong *autoextend_size);

/**
  Check whether a table has constraints, virtual columns,
  default, or partitions that may use functions. If so, we may
  wish to try to open the table during upgrade to see whether
  we can (or whether any of the functions have changed in such
  a way that opening the table is no longer possible).

  @param  table_def    The table to examine.
  @param  schema_name  The name of the schema, or nullptr.
  @param  debug        A String_type to return debug_info in, or nullptr.
  @return true         The table definition potentially uses functions.
  @return false        The table is basic.
*/
bool uses_functions(const Table *table_def, const char *schema_name = nullptr,
                    String_type *debug = nullptr);

}  // namespace dd
#endif  // DD_TABLE_INCLUDED