1/***************************************************************************** 2 3Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved. 4 5This program is free software; you can redistribute it and/or modify 6it under the terms of the GNU General Public License, version 2.0, 7as published by the Free Software Foundation. 8 9This program is also distributed with certain software (including 10but not limited to OpenSSL) that is licensed under separate terms, 11as designated in a particular file or component or in included license 12documentation. The authors of MySQL hereby grant you an additional 13permission to link the program and your derivative works with the 14separately licensed software that they have included with MySQL. 15 16This program is distributed in the hope that it will be useful, 17but WITHOUT ANY WARRANTY; without even the implied warranty of 18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19GNU General Public License, version 2.0, for more details. 20 21You should have received a copy of the GNU General Public License along with 22this program; if not, write to the Free Software Foundation, Inc., 2351 Franklin Street, Suite 500, Boston, MA 02110-1335 USA 24 25*****************************************************************************/ 26 27/**************************************************//** 28@file include/dict0crea.ic 29Database object creation 30 31Created 1/8/1996 Heikki Tuuri 32*******************************************************/ 33 34#include "mem0mem.h" 35 36/*********************************************************************//** 37Checks if a table name contains the string "/#sql" which denotes temporary 38tables in MySQL. 39@return true if temporary table */ 40UNIV_INTERN 41bool 42row_is_mysql_tmp_table_name( 43/*========================*/ 44 const char* name) MY_ATTRIBUTE((warn_unused_result)); 45 /*!< in: table name in the form 46 'database/tablename' */ 47 48 49/********************************************************************//** 50Generate a foreign key constraint name when it was not named by the user. 51A generated constraint has a name of the format dbname/tablename_ibfk_NUMBER, 52where the numbers start from 1, and are given locally for this table, that is, 53the number is not global, as it used to be before MySQL 4.0.18. */ 54UNIV_INLINE 55dberr_t 56dict_create_add_foreign_id( 57/*=======================*/ 58 ulint* id_nr, /*!< in/out: number to use in id generation; 59 incremented if used */ 60 const char* name, /*!< in: table name */ 61 dict_foreign_t* foreign)/*!< in/out: foreign key */ 62{ 63 if (foreign->id == NULL) { 64 /* Generate a new constraint id */ 65 ulint namelen = strlen(name); 66 char* id = static_cast<char*>( 67 mem_heap_alloc(foreign->heap, 68 namelen + 20)); 69 70 if (row_is_mysql_tmp_table_name(name)) { 71 72 /* no overflow if number < 1e13 */ 73 sprintf(id, "%s_ibfk_%lu", name, 74 (ulong) (*id_nr)++); 75 } else { 76 char table_name[MAX_TABLE_NAME_LEN + 20] = ""; 77 uint errors = 0; 78 79 strncpy(table_name, name, 80 MAX_TABLE_NAME_LEN + 20); 81 82 innobase_convert_to_system_charset( 83 strchr(table_name, '/') + 1, 84 strchr(name, '/') + 1, 85 MAX_TABLE_NAME_LEN, &errors); 86 87 if (errors) { 88 strncpy(table_name, name, 89 MAX_TABLE_NAME_LEN + 20); 90 } 91 92 /* no overflow if number < 1e13 */ 93 sprintf(id, "%s_ibfk_%lu", table_name, 94 (ulong) (*id_nr)++); 95 96 if (innobase_check_identifier_length( 97 strchr(id,'/') + 1)) { 98 return(DB_IDENTIFIER_TOO_LONG); 99 } 100 } 101 foreign->id = id; 102 } 103 104 return(DB_SUCCESS); 105} 106 107