107
103
list_copy_and_replace_each_value(columns, mem_root);
111
Construct an (almost) deep copy of this foreign key. Only those
112
elements that are known to never change are not copied.
113
If out of memory, a partial copy is returned and an error is set
117
Foreign_key::Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root)
119
ref_table(rhs.ref_table),
120
ref_columns(rhs.ref_columns),
121
delete_opt(rhs.delete_opt),
122
update_opt(rhs.update_opt),
123
match_opt(rhs.match_opt)
125
list_copy_and_replace_each_value(ref_columns, mem_root);
129
Test if a foreign key (= generated key) is a prefix of the given key
130
(ignoring key name, key type and order of columns)
133
This is only used to test if an index for a FOREIGN KEY exists
136
We only compare field names
139
0 Generated key is a prefix of other key
143
bool foreign_key_prefix(Key *a, Key *b)
145
/* Ensure that 'a' is the generated key */
148
if (b->generated && a->columns.elements > b->columns.elements)
149
std::swap(a, b); // Put shorter key in 'a'
154
return true; // No foreign key
155
std::swap(a, b); // Put generated key in 'a'
158
/* Test if 'a' is a prefix of 'b' */
159
if (a->columns.elements > b->columns.elements)
160
return true; // Can't be prefix
162
List_iterator<Key_part_spec> col_it1(a->columns);
163
List_iterator<Key_part_spec> col_it2(b->columns);
164
const Key_part_spec *col1, *col2;
166
#ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
167
while ((col1= col_it1++))
171
while ((col2= col_it2++))
180
return true; // Error
182
return false; // Is prefix
184
while ((col1= col_it1++))
187
if (!(*col1 == *col2))
190
return false; // Is prefix
196
Check if the foreign key options are compatible with columns
197
on which the FK is created.
203
bool Foreign_key::validate(List<Create_field> &table_fields)
205
Create_field *sql_field;
206
Key_part_spec *column;
207
List_iterator<Key_part_spec> cols(columns);
208
List_iterator<Create_field> it(table_fields);
209
while ((column= cols++))
212
while ((sql_field= it++) &&
213
my_strcasecmp(system_charset_info,
214
column->field_name.str,
215
sql_field->field_name)) {}
218
my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str);
221
if (type == Key::FOREIGN_KEY && sql_field->vcol_info)
223
if (delete_opt == FK_OPTION_SET_NULL)
225
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
226
"ON DELETE SET NULL");
229
if (update_opt == FK_OPTION_SET_NULL)
231
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
232
"ON UPDATE SET NULL");
235
if (update_opt == FK_OPTION_CASCADE)
237
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
238
"ON UPDATE CASCADE");
247
106
/****************************************************************************
248
107
** Thread specific functions
249
108
****************************************************************************/