1 | /***************************************************************************** |
2 | |
3 | Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved. |
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 as published by the Free Software |
7 | Foundation; version 2 of the License. |
8 | |
9 | This program is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
11 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License along with |
14 | this program; if not, write to the Free Software Foundation, Inc., |
15 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
16 | |
17 | *****************************************************************************/ |
18 | |
19 | /**************************************************//** |
20 | @file include/dict0crea.ic |
21 | Database object creation |
22 | |
23 | Created 1/8/1996 Heikki Tuuri |
24 | *******************************************************/ |
25 | |
26 | #include "ha_prototypes.h" |
27 | |
28 | #include "mem0mem.h" |
29 | |
30 | /*********************************************************************//** |
31 | Checks if a table name contains the string "/#sql" which denotes temporary |
32 | tables in MySQL. |
33 | @return true if temporary table */ |
34 | bool |
35 | row_is_mysql_tmp_table_name( |
36 | /*========================*/ |
37 | const char* name) MY_ATTRIBUTE((warn_unused_result)); |
38 | /*!< in: table name in the form |
39 | 'database/tablename' */ |
40 | |
41 | |
42 | /********************************************************************//** |
43 | Generate a foreign key constraint name when it was not named by the user. |
44 | A generated constraint has a name of the format dbname/tablename_ibfk_NUMBER, |
45 | where the numbers start from 1, and are given locally for this table, that is, |
46 | the number is not global, as it used to be before MySQL 4.0.18. */ |
47 | UNIV_INLINE |
48 | dberr_t |
49 | dict_create_add_foreign_id( |
50 | /*=======================*/ |
51 | ulint* id_nr, /*!< in/out: number to use in id generation; |
52 | incremented if used */ |
53 | const char* name, /*!< in: table name */ |
54 | dict_foreign_t* foreign)/*!< in/out: foreign key */ |
55 | { |
56 | DBUG_ENTER("dict_create_add_foreign_id" ); |
57 | |
58 | if (foreign->id == NULL) { |
59 | /* Generate a new constraint id */ |
60 | ulint namelen = strlen(name); |
61 | char* id = static_cast<char*>( |
62 | mem_heap_alloc(foreign->heap, |
63 | namelen + 20)); |
64 | |
65 | if (row_is_mysql_tmp_table_name(name)) { |
66 | |
67 | /* no overflow if number < 1e13 */ |
68 | sprintf(id, "%s_ibfk_%lu" , name, |
69 | (ulong) (*id_nr)++); |
70 | } else { |
71 | char table_name[MAX_TABLE_NAME_LEN + 20] = "" ; |
72 | uint errors = 0; |
73 | |
74 | strncpy(table_name, name, |
75 | MAX_TABLE_NAME_LEN + 20); |
76 | |
77 | innobase_convert_to_system_charset( |
78 | strchr(table_name, '/') + 1, |
79 | strchr(name, '/') + 1, |
80 | MAX_TABLE_NAME_LEN, &errors); |
81 | |
82 | if (errors) { |
83 | strncpy(table_name, name, |
84 | MAX_TABLE_NAME_LEN + 20); |
85 | } |
86 | |
87 | /* no overflow if number < 1e13 */ |
88 | sprintf(id, "%s_ibfk_%lu" , table_name, |
89 | (ulong) (*id_nr)++); |
90 | |
91 | if (innobase_check_identifier_length( |
92 | strchr(id,'/') + 1)) { |
93 | DBUG_RETURN(DB_IDENTIFIER_TOO_LONG); |
94 | } |
95 | } |
96 | foreign->id = id; |
97 | |
98 | DBUG_PRINT("dict_create_add_foreign_id" , |
99 | ("generated foreign id: %s" , id)); |
100 | } |
101 | |
102 | |
103 | DBUG_RETURN(DB_SUCCESS); |
104 | } |
105 | |
106 | /** Compose a column number for a virtual column, stored in the "POS" field |
107 | of Sys_columns. The column number includes both its virtual column sequence |
108 | (the "nth" virtual column) and its actual column position in original table |
109 | @param[in] v_pos virtual column sequence |
110 | @param[in] col_pos column position in original table definition |
111 | @return composed column position number */ |
112 | UNIV_INLINE |
113 | ulint |
114 | dict_create_v_col_pos( |
115 | ulint v_pos, |
116 | ulint col_pos) |
117 | { |
118 | ut_ad(v_pos <= REC_MAX_N_FIELDS); |
119 | ut_ad(col_pos <= REC_MAX_N_FIELDS); |
120 | |
121 | return(((v_pos + 1) << 16) + col_pos); |
122 | } |
123 | |
124 | /** Get the column number for a virtual column (the column position in |
125 | original table), stored in the "POS" field of Sys_columns |
126 | @param[in] pos virtual column position |
127 | @return column position in original table */ |
128 | UNIV_INLINE |
129 | ulint |
130 | dict_get_v_col_mysql_pos( |
131 | ulint pos) |
132 | { |
133 | return(pos & 0xFFFF); |
134 | } |
135 | |
136 | /** Get a virtual column sequence (the "nth" virtual column) for a |
137 | virtual column, stord in the "POS" field of Sys_columns |
138 | @param[in] pos virtual column position |
139 | @return virtual column sequence */ |
140 | UNIV_INLINE |
141 | ulint |
142 | dict_get_v_col_pos( |
143 | ulint pos) |
144 | { |
145 | return((pos >> 16) - 1); |
146 | } |
147 | |