Skip to content

Commit 9456648

Browse files
committed
Merge from mysql-5.6 to mysql-5.7
2 parents 820a35e + 094ce29 commit 9456648

File tree

2 files changed

+65
-57
lines changed

2 files changed

+65
-57
lines changed

storage/innobase/dict/dict0dict.cc

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,23 +3387,6 @@ dict_table_get_foreign_constraint(
33873387
return(NULL);
33883388
}
33893389

3390-
/*********************************************************************//**
3391-
Frees a foreign key struct. */
3392-
3393-
void
3394-
dict_foreign_free(
3395-
/*==============*/
3396-
dict_foreign_t* foreign) /*!< in, own: foreign key struct */
3397-
{
3398-
DBUG_ENTER("dict_foreign_free");
3399-
DBUG_PRINT("dict_foreign_free", ("id: '%s', heap: %p", foreign->id,
3400-
foreign->heap));
3401-
3402-
mem_heap_free(foreign->heap);
3403-
3404-
DBUG_VOID_RETURN;
3405-
}
3406-
34073390
/**********************************************************************//**
34083391
Removes a foreign constraint struct from the dictionary cache. */
34093392

@@ -4385,6 +4368,7 @@ dict_create_foreign_constraints_low(
43854368
const char* column_names[500];
43864369
const char* referenced_table_name;
43874370
dict_foreign_set local_fk_set;
4371+
dict_foreign_set_free local_fk_set_free(local_fk_set);
43884372

43894373
ut_ad(!srv_read_only_mode || handler);
43904374
ut_ad(mutex_own(&(dict_sys->mutex)) || handler);
@@ -4513,17 +4497,10 @@ dict_create_foreign_constraints_low(
45134497

45144498
table->foreign_set.insert(local_fk_set.begin(),
45154499
local_fk_set.end());
4516-
4517-
for (dict_foreign_set::iterator it
4518-
= local_fk_set.begin();
4519-
it != local_fk_set.end();
4520-
++it) {
4521-
4522-
dict_table_t* ref = (*it)->referenced_table;
4523-
if (ref != NULL) {
4524-
ref->referenced_set.insert(*it);
4525-
}
4526-
}
4500+
std::for_each(local_fk_set.begin(),
4501+
local_fk_set.end(),
4502+
dict_foreign_add_to_referenced_table());
4503+
local_fk_set.clear();
45274504
}
45284505
return(error);
45294506
}
@@ -4655,6 +4632,21 @@ dict_create_foreign_constraints_low(
46554632
strcpy(foreign->id + db_len + 1, constraint_name);
46564633
}
46574634

4635+
if (foreign->id == NULL) {
4636+
error = dict_create_add_foreign_id(&number,
4637+
table->name, foreign);
4638+
if (error != DB_SUCCESS) {
4639+
dict_foreign_free(foreign);
4640+
return(error);
4641+
}
4642+
}
4643+
4644+
std::pair<dict_foreign_set::iterator, bool> ret
4645+
= local_fk_set.insert(foreign);
4646+
4647+
ut_a(ret.second); /* second is true if the insertion
4648+
took place */
4649+
46584650
foreign->foreign_table = table;
46594651
foreign->foreign_table_name = mem_heap_strdup(
46604652
foreign->heap, table->name);
@@ -4680,8 +4672,6 @@ dict_create_foreign_constraints_low(
46804672
checking of foreign key constraints! */
46814673

46824674
if (!success || (!referenced_table && trx->check_foreigns)) {
4683-
dict_foreign_free(foreign);
4684-
46854675
mutex_enter(&dict_foreign_err_mutex);
46864676
dict_foreign_error_report_low(ef, name);
46874677
fprintf(ef, "%s:\nCannot resolve table name close to:\n"
@@ -4695,7 +4685,6 @@ dict_create_foreign_constraints_low(
46954685
ptr = dict_accept(cs, ptr, "(", &success);
46964686

46974687
if (!success) {
4698-
dict_foreign_free(foreign);
46994688
dict_foreign_report_syntax_err(name, start_of_latest_foreign,
47004689
ptr);
47014690
return(DB_CANNOT_ADD_CONSTRAINT);
@@ -4710,7 +4699,6 @@ dict_create_foreign_constraints_low(
47104699
i++;
47114700

47124701
if (!success) {
4713-
dict_foreign_free(foreign);
47144702

47154703
mutex_enter(&dict_foreign_err_mutex);
47164704
dict_foreign_error_report_low(ef, name);
@@ -4731,7 +4719,6 @@ dict_create_foreign_constraints_low(
47314719
ptr = dict_accept(cs, ptr, ")", &success);
47324720

47334721
if (!success || foreign->n_fields != i) {
4734-
dict_foreign_free(foreign);
47354722

47364723
dict_foreign_report_syntax_err(name, start_of_latest_foreign,
47374724
ptr);
@@ -4757,7 +4744,6 @@ dict_create_foreign_constraints_low(
47574744
ptr = dict_accept(cs, ptr, "UPDATE", &success);
47584745

47594746
if (!success) {
4760-
dict_foreign_free(foreign);
47614747

47624748
dict_foreign_report_syntax_err(
47634749
name, start_of_latest_foreign, ptr);
@@ -4795,7 +4781,6 @@ dict_create_foreign_constraints_low(
47954781
ptr = dict_accept(cs, ptr, "ACTION", &success);
47964782

47974783
if (!success) {
4798-
dict_foreign_free(foreign);
47994784
dict_foreign_report_syntax_err(
48004785
name, start_of_latest_foreign, ptr);
48014786

@@ -4814,7 +4799,6 @@ dict_create_foreign_constraints_low(
48144799
ptr = dict_accept(cs, ptr, "SET", &success);
48154800

48164801
if (!success) {
4817-
dict_foreign_free(foreign);
48184802
dict_foreign_report_syntax_err(name, start_of_latest_foreign,
48194803
ptr);
48204804
return(DB_CANNOT_ADD_CONSTRAINT);
@@ -4823,7 +4807,6 @@ dict_create_foreign_constraints_low(
48234807
ptr = dict_accept(cs, ptr, "NULL", &success);
48244808

48254809
if (!success) {
4826-
dict_foreign_free(foreign);
48274810
dict_foreign_report_syntax_err(name, start_of_latest_foreign,
48284811
ptr);
48294812
return(DB_CANNOT_ADD_CONSTRAINT);
@@ -4836,8 +4819,6 @@ dict_create_foreign_constraints_low(
48364819
/* It is not sensible to define SET NULL
48374820
if the column is not allowed to be NULL! */
48384821

4839-
dict_foreign_free(foreign);
4840-
48414822
mutex_enter(&dict_foreign_err_mutex);
48424823
dict_foreign_error_report_low(ef, name);
48434824
fprintf(ef, "%s:\n"
@@ -4863,8 +4844,6 @@ dict_create_foreign_constraints_low(
48634844
if (n_on_deletes > 1 || n_on_updates > 1) {
48644845
/* It is an error to define more than 1 action */
48654846

4866-
dict_foreign_free(foreign);
4867-
48684847
mutex_enter(&dict_foreign_err_mutex);
48694848
dict_foreign_error_report_low(ef, name);
48704849
fprintf(ef, "%s:\n"
@@ -4886,7 +4865,6 @@ dict_create_foreign_constraints_low(
48864865
foreign->foreign_index,
48874866
TRUE, FALSE);
48884867
if (!index) {
4889-
dict_foreign_free(foreign);
48904868
mutex_enter(&dict_foreign_err_mutex);
48914869
dict_foreign_error_report_low(ef, name);
48924870
fprintf(ef, "%s:\n"
@@ -4928,21 +4906,6 @@ dict_create_foreign_constraints_low(
49284906
= mem_heap_strdup(foreign->heap, column_names[i]);
49294907
}
49304908

4931-
/* We found an ok constraint definition: add to the set */
4932-
4933-
if (foreign->id == NULL) {
4934-
error = dict_create_add_foreign_id(&number,
4935-
table->name, foreign);
4936-
if (error != DB_SUCCESS) {
4937-
return(error);
4938-
}
4939-
}
4940-
4941-
std::pair<dict_foreign_set::iterator, bool> ret
4942-
= local_fk_set.insert(foreign);
4943-
4944-
ut_a(ret.second); /* second is true if the insertion
4945-
took place */
49464909
goto loop;
49474910
}
49484911
/**************************************************************************

storage/innobase/include/dict0mem.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Created 1/8/1996 Heikki Tuuri
4949
#include "gis0type.h"
5050
#include "os0once.h"
5151
#include <set>
52+
#include <algorithm>
5253

5354
/* Forward declaration. */
5455
struct ib_rbt_t;
@@ -924,6 +925,37 @@ struct dict_foreign_matches_id {
924925

925926
typedef std::set<dict_foreign_t*, dict_foreign_compare> dict_foreign_set;
926927

928+
/*********************************************************************//**
929+
Frees a foreign key struct. */
930+
inline
931+
void
932+
dict_foreign_free(
933+
/*==============*/
934+
dict_foreign_t* foreign) /*!< in, own: foreign key struct */
935+
{
936+
mem_heap_free(foreign->heap);
937+
}
938+
939+
/** The destructor will free all the foreign key constraints in the set
940+
by calling dict_foreign_free() on each of the foreign key constraints.
941+
This is used to free the allocated memory when a local set goes out
942+
of scope. */
943+
struct dict_foreign_set_free {
944+
945+
dict_foreign_set_free(const dict_foreign_set& foreign_set)
946+
: m_foreign_set(foreign_set)
947+
{}
948+
949+
~dict_foreign_set_free()
950+
{
951+
std::for_each(m_foreign_set.begin(),
952+
m_foreign_set.end(),
953+
dict_foreign_free);
954+
}
955+
956+
const dict_foreign_set& m_foreign_set;
957+
};
958+
927959
/** The flags for ON_UPDATE and ON_DELETE can be ORed; the default is that
928960
a foreign key constraint is enforced, therefore RESTRICT just means no flag */
929961
/* @{ */
@@ -1303,6 +1335,19 @@ lock_table_lock_list_init(
13031335
/*======================*/
13041336
table_lock_list_t* locks); /*!< List to initialise */
13051337

1338+
/** A function object to add the foreign key constraint to the referenced set
1339+
of the referenced table, if it exists in the dictionary cache. */
1340+
struct dict_foreign_add_to_referenced_table {
1341+
void operator()(dict_foreign_t* foreign) const
1342+
{
1343+
if (dict_table_t* table = foreign->referenced_table) {
1344+
std::pair<dict_foreign_set::iterator, bool> ret
1345+
= table->referenced_set.insert(foreign);
1346+
ut_a(ret.second);
1347+
}
1348+
}
1349+
};
1350+
13061351
#ifndef UNIV_NONINL
13071352
#include "dict0mem.ic"
13081353
#endif

0 commit comments

Comments
 (0)