diff options
Diffstat (limited to 'sql/sql_lex.cc')
-rw-r--r-- | sql/sql_lex.cc | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index a7e6ecd3387..7ff3e8f6a75 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -11168,3 +11168,71 @@ sp_condition_value *LEX::stmt_signal_value(const Lex_ident_sys_st &ident) } return cond; } + + +bool LEX::add_table_foreign_key(const LEX_CSTRING *name, + const LEX_CSTRING *constraint_name, + Table_ident *ref_table_name, + DDL_options ddl_options) +{ + Key *key= new (thd->mem_root) Foreign_key(name, + &last_key->columns, + constraint_name, + &ref_table_name->db, + &ref_table_name->table, + &ref_list, + fk_delete_opt, + fk_update_opt, + fk_match_option, + ddl_options); + if (unlikely(key == NULL)) + return true; + + /* + handle_if_exists_options() expects the two keys in this order: + the Foreign_key, followed by its auto-generated Key. + */ + alter_info.key_list.push_back(key, thd->mem_root); + alter_info.key_list.push_back(last_key, thd->mem_root); + + option_list= NULL; + + /* Only used for ALTER TABLE. Ignored otherwise. */ + alter_info.flags|= ALTER_ADD_FOREIGN_KEY; + + return false; +} + + +bool LEX::add_column_foreign_key(const LEX_CSTRING *name, + const LEX_CSTRING *constraint_name, + Table_ident *ref_table_name, + DDL_options ddl_options) +{ + if (last_field->vcol_info || last_field->vers_sys_field()) + { + thd->parse_error(); + return true; + } + if (unlikely(!(last_key= (new (thd->mem_root) + Key(Key::MULTIPLE, constraint_name, + HA_KEY_ALG_UNDEF, true, ddl_options))))) + return true; + Key_part_spec *key= new (thd->mem_root) Key_part_spec(name, 0); + if (unlikely(key == NULL)) + return true; + last_key->columns.push_back(key, thd->mem_root); + if (ref_list.is_empty()) + { + ref_list.push_back(key, thd->mem_root); + } + if (unlikely(add_table_foreign_key(constraint_name, constraint_name, + ref_table_name, ddl_options))) + return true; + option_list= NULL; + + /* Only used for ALTER TABLE. Ignored otherwise. */ + alter_info.flags|= ALTER_ADD_FOREIGN_KEY; + + return false; +} |