| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/dict0crea.ic |
| 29 |
Database object creation |
| 30 |
|
| 31 |
Created 1/8/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "ha_prototypes.h" |
| 35 |
|
| 36 |
#include "mem0mem.h" |
| 37 |
|
| 38 |
/** Checks if a table name contains the string "/#sql" which denotes temporary |
| 39 |
tables in MySQL. |
| 40 |
@return true if temporary table */ |
| 41 |
[[nodiscard]] bool row_is_mysql_tmp_table_name(const char *name); |
| 42 |
/*!< in: table name in the form |
| 43 |
'database/tablename' */ |
| 44 |
|
| 45 |
/** Generate a foreign key constraint name when it was not named by the user. |
| 46 |
A generated constraint has a name of the format dbname/tablename_ibfk_NUMBER, |
| 47 |
where the numbers start from 1, and are given locally for this table, that is, |
| 48 |
the number is not global, as it used to be before MySQL 4.0.18. */ |
| 49 |
static inline dberr_t dict_create_add_foreign_id( |
| 50 |
ulint *id_nr, /*!< in/out: number to use in id generation; |
| 51 |
incremented if used */ |
| 52 |
const char *name, /*!< in: table name */ |
| 53 |
dict_foreign_t *foreign) /*!< in/out: foreign key */ |
| 54 |
{ |
| 55 |
DBUG_TRACE; |
| 56 |
|
| 57 |
if (foreign->id == nullptr) { |
| 58 |
/* Generate a new constraint id */ |
| 59 |
ulint namelen = strlen(name); |
| 60 |
char *id = static_cast<char *>(mem_heap_alloc(foreign->heap, namelen + 20)); |
| 61 |
|
| 62 |
if (row_is_mysql_tmp_table_name(name)) { |
| 63 |
/* no overflow if number < 1e13 */ |
| 64 |
sprintf(id, "%s_ibfk_%lu", name, (ulong)(*id_nr)++); |
| 65 |
} else { |
| 66 |
char table_name[MAX_TABLE_NAME_LEN + 20 + 1] = ""; |
| 67 |
uint errors = 0; |
| 68 |
|
| 69 |
strncpy(table_name, name, MAX_TABLE_NAME_LEN + 20); |
| 70 |
|
| 71 |
innobase_convert_to_system_charset(strchr(table_name, '/') + 1, |
| 72 |
strchr(name, '/') + 1, |
| 73 |
MAX_TABLE_NAME_LEN, &errors); |
| 74 |
|
| 75 |
if (errors) { |
| 76 |
strncpy(table_name, name, MAX_TABLE_NAME_LEN + 20); |
| 77 |
} |
| 78 |
|
| 79 |
/* no overflow if number < 1e13 */ |
| 80 |
sprintf(id, "%s_ibfk_%lu", table_name, (ulong)(*id_nr)++); |
| 81 |
|
| 82 |
if (innobase_check_identifier_length(strchr(id, '/') + 1)) { |
| 83 |
/* Server has already done this check, |
| 84 |
So assert here */ |
| 85 |
ut_d(ut_error); |
| 86 |
ut_o(return DB_IDENTIFIER_TOO_LONG); |
| 87 |
} |
| 88 |
} |
| 89 |
foreign->id = id; |
| 90 |
|
| 91 |
DBUG_PRINT("dict_create_add_foreign_id", ("generated foreign id: %s", id)); |
| 92 |
} |
| 93 |
|
| 94 |
return DB_SUCCESS; |
| 95 |
} |
| 96 |
|
| 97 |
/** Compose a column number for a virtual column, stored in the "POS" field |
| 98 |
of Sys_columns. The column number includes both its virtual column sequence |
| 99 |
(the "nth" virtual column) and its actual column position in original table |
| 100 |
@param[in] v_pos virtual column sequence |
| 101 |
@param[in] col_pos column position in original table definition |
| 102 |
@return composed column position number */ |
| 103 |
static inline ulint dict_create_v_col_pos(ulint v_pos, ulint col_pos) { |
| 104 |
ut_ad(v_pos <= REC_MAX_N_FIELDS); |
| 105 |
ut_ad(col_pos <= REC_MAX_N_FIELDS); |
| 106 |
|
| 107 |
return (((v_pos + 1) << 16) + col_pos); |
| 108 |
} |
| 109 |
|
| 110 |
/** Get the column number for a virtual column (the column position in |
| 111 |
original table), stored in the "POS" field of Sys_columns |
| 112 |
@param[in] pos virtual column position |
| 113 |
@return column position in original table */ |
| 114 |
static inline ulint dict_get_v_col_mysql_pos(ulint pos) { |
| 115 |
return (pos & 0xFFFF); |
| 116 |
} |
| 117 |
|
| 118 |
/** Get a virtual column sequence (the "nth" virtual column) for a |
| 119 |
virtual column, stord in the "POS" field of Sys_columns |
| 120 |
@param[in] pos virtual column position |
| 121 |
@return virtual column sequence */ |
| 122 |
static inline ulint dict_get_v_col_pos(ulint pos) { return ((pos >> 16) - 1); } |