Commit e279c007 authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-17187: Code cleanup

fkerr_t: Errors for the foreign key checks. Replaces ulint,
which used #define that looked like dberr_t literals.

wsrep_dict_foreign_find_index(): Remove. Use
dict_foreign_find_index() instead, with default parameters.

dict_foreign_push_index_error(): Do not add redundant quotes
around quoted table names.
parent ddaebdd2
...@@ -25,7 +25,7 @@ create table t2(a int, constraint a foreign key a (a) references t1(a)) engine=i ...@@ -25,7 +25,7 @@ create table t2(a int, constraint a foreign key a (a) references t1(a)) engine=i
ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
show warnings; show warnings;
Level Code Message Level Code Message
Warning 150 Create table '`test`.`t2`' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near ' foreign key a (a) references t1(a)) engine=innodb'. Warning 150 Create table `test`.`t2` with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near ' foreign key a (a) references t1(a)) engine=innodb'.
Error 1005 Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") Error 1005 Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
Warning 1215 Cannot add foreign key constraint Warning 1215 Cannot add foreign key constraint
drop table t1; drop table t1;
...@@ -42,7 +42,7 @@ alter table t2 add constraint b foreign key (b) references t2(b); ...@@ -42,7 +42,7 @@ alter table t2 add constraint b foreign key (b) references t2(b);
ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
show warnings; show warnings;
Level Code Message Level Code Message
Warning 150 Alter table '`test`.`t2`' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near ' foreign key (b) references t2(b)'. Warning 150 Alter table `test`.`t2` with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near ' foreign key (b) references t2(b)'.
Error 1005 Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed") Error 1005 Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
Warning 1215 Cannot add foreign key constraint Warning 1215 Cannot add foreign key constraint
drop table t2, t1; drop table t2, t1;
......
...@@ -1524,7 +1524,7 @@ ALTER TABLE child ADD FOREIGN KEY(p) REFERENCES parent(p); ...@@ -1524,7 +1524,7 @@ ALTER TABLE child ADD FOREIGN KEY(p) REFERENCES parent(p);
ERROR HY000: Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed") ERROR HY000: Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed")
show warnings; show warnings;
Level Code Message Level Code Message
Warning 150 Alter table '`test`.`child`' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY(p) REFERENCES parent(p)'. Warning 150 Alter table `test`.`child` with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY(p) REFERENCES parent(p)'.
Error 1005 Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed") Error 1005 Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed")
Warning 1215 Cannot add foreign key constraint Warning 1215 Cannot add foreign key constraint
ALTER TABLE parent DROP INDEX idx1; ALTER TABLE parent DROP INDEX idx1;
...@@ -1532,7 +1532,7 @@ ALTER TABLE child ADD FOREIGN KEY(p) REFERENCES parent(p); ...@@ -1532,7 +1532,7 @@ ALTER TABLE child ADD FOREIGN KEY(p) REFERENCES parent(p);
Got one of the listed errors Got one of the listed errors
show warnings; show warnings;
Level Code Message Level Code Message
Warning 150 Alter table '`test`.`child`' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY(p) REFERENCES parent(p)'. Warning 150 Alter table `test`.`child` with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY(p) REFERENCES parent(p)'.
Error 1005 Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed") Error 1005 Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed")
Warning 1215 Cannot add foreign key constraint Warning 1215 Cannot add foreign key constraint
ALTER TABLE child DROP INDEX idx2; ALTER TABLE child DROP INDEX idx2;
...@@ -1540,7 +1540,7 @@ ALTER TABLE child ADD FOREIGN KEY(p) REFERENCES parent(p); ...@@ -1540,7 +1540,7 @@ ALTER TABLE child ADD FOREIGN KEY(p) REFERENCES parent(p);
Got one of the listed errors Got one of the listed errors
show warnings; show warnings;
Level Code Message Level Code Message
Warning 150 Alter table '`test`.`child`' with foreign key constraint failed. There is only prefix index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY(p) REFERENCES parent(p)'. Warning 150 Alter table `test`.`child` with foreign key constraint failed. There is only prefix index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY(p) REFERENCES parent(p)'.
Error 1005 Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed") Error 1005 Can't create table `test`.`child` (errno: 150 "Foreign key constraint is incorrectly formed")
Warning 1215 Cannot add foreign key constraint Warning 1215 Cannot add foreign key constraint
DROP TABLE child, parent; DROP TABLE child, parent;
......
...@@ -3211,11 +3211,6 @@ dict_index_build_internal_fts( ...@@ -3211,11 +3211,6 @@ dict_index_build_internal_fts(
} }
/*====================== FOREIGN KEY PROCESSING ========================*/ /*====================== FOREIGN KEY PROCESSING ========================*/
#define DB_FOREIGN_KEY_IS_PREFIX_INDEX 200
#define DB_FOREIGN_KEY_COL_NOT_NULL 201
#define DB_FOREIGN_KEY_COLS_NOT_EQUAL 202
#define DB_FOREIGN_KEY_INDEX_NOT_FOUND 203
/** Check whether the dict_table_t is a partition. /** Check whether the dict_table_t is a partition.
A partitioned table on the SQL level is composed of InnoDB tables, A partitioned table on the SQL level is composed of InnoDB tables,
where each InnoDB table is a [sub]partition including its secondary indexes where each InnoDB table is a [sub]partition including its secondary indexes
...@@ -3322,7 +3317,7 @@ dict_foreign_find_index( ...@@ -3322,7 +3317,7 @@ dict_foreign_find_index(
/*!< in: nonzero if none of /*!< in: nonzero if none of
the columns must be declared the columns must be declared
NOT NULL */ NOT NULL */
ulint* error, /*!< out: error code */ fkerr_t* error, /*!< out: error code */
ulint* err_col_no, ulint* err_col_no,
/*!< out: column number where /*!< out: column number where
error happened */ error happened */
...@@ -3330,17 +3325,15 @@ dict_foreign_find_index( ...@@ -3330,17 +3325,15 @@ dict_foreign_find_index(
/*!< out: index where error /*!< out: index where error
happened */ happened */
{ {
dict_index_t* index;
ut_ad(mutex_own(&dict_sys->mutex)); ut_ad(mutex_own(&dict_sys->mutex));
if (error) { if (error) {
*error = DB_FOREIGN_KEY_INDEX_NOT_FOUND; *error = FK_INDEX_NOT_FOUND;
} }
index = dict_table_get_first_index(table); for (dict_index_t* index = dict_table_get_first_index(table);
index;
while (index != NULL) { index = dict_table_get_next_index(index)) {
if (types_idx != index if (types_idx != index
&& !index->to_be_dropped && !index->to_be_dropped
&& !dict_index_is_online_ddl(index) && !dict_index_is_online_ddl(index)
...@@ -3348,42 +3341,17 @@ dict_foreign_find_index( ...@@ -3348,42 +3341,17 @@ dict_foreign_find_index(
table, col_names, columns, n_cols, table, col_names, columns, n_cols,
index, types_idx, index, types_idx,
check_charsets, check_null, check_charsets, check_null,
error, err_col_no,err_index)) { error, err_col_no, err_index)) {
if (error) { if (error) {
*error = DB_SUCCESS; *error = FK_SUCCESS;
} }
return(index); return(index);
} }
index = dict_table_get_next_index(index);
} }
return(NULL); return(NULL);
} }
#ifdef WITH_WSREP
dict_index_t*
wsrep_dict_foreign_find_index(
/*====================*/
dict_table_t* table, /*!< in: table */
const char** col_names, /*!< in: column names, or NULL
to use table->col_names */
const char** columns,/*!< in: array of column names */
ulint n_cols, /*!< in: number of columns */
dict_index_t* types_idx, /*!< in: NULL or an index to whose types the
column types must match */
ibool check_charsets,
/*!< in: whether to check charsets.
only has an effect if types_idx != NULL */
ulint check_null)
/*!< in: nonzero if none of the columns must
be declared NOT NULL */
{
return dict_foreign_find_index(
table, col_names, columns, n_cols, types_idx, check_charsets,
check_null, NULL, NULL, NULL);
}
#endif /* WITH_WSREP */
/**********************************************************************//** /**********************************************************************//**
Report an error in a foreign key definition. */ Report an error in a foreign key definition. */
static static
...@@ -3480,15 +3448,11 @@ dict_foreign_add_to_cache( ...@@ -3480,15 +3448,11 @@ dict_foreign_add_to_cache(
} }
if (ref_table && !for_in_cache->referenced_table) { if (ref_table && !for_in_cache->referenced_table) {
ulint index_error;
ulint err_col;
dict_index_t *err_index=NULL;
index = dict_foreign_find_index( index = dict_foreign_find_index(
ref_table, NULL, ref_table, NULL,
for_in_cache->referenced_col_names, for_in_cache->referenced_col_names,
for_in_cache->n_fields, for_in_cache->foreign_index, for_in_cache->n_fields, for_in_cache->foreign_index,
check_charsets, false, &index_error, &err_col, &err_index); check_charsets, false);
if (index == NULL if (index == NULL
&& !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) { && !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) {
...@@ -3520,10 +3484,6 @@ dict_foreign_add_to_cache( ...@@ -3520,10 +3484,6 @@ dict_foreign_add_to_cache(
} }
if (for_table && !for_in_cache->foreign_table) { if (for_table && !for_in_cache->foreign_table) {
ulint index_error;
ulint err_col;
dict_index_t *err_index=NULL;
index = dict_foreign_find_index( index = dict_foreign_find_index(
for_table, col_names, for_table, col_names,
for_in_cache->foreign_col_names, for_in_cache->foreign_col_names,
...@@ -3531,8 +3491,7 @@ dict_foreign_add_to_cache( ...@@ -3531,8 +3491,7 @@ dict_foreign_add_to_cache(
for_in_cache->referenced_index, check_charsets, for_in_cache->referenced_index, check_charsets,
for_in_cache->type for_in_cache->type
& (DICT_FOREIGN_ON_DELETE_SET_NULL & (DICT_FOREIGN_ON_DELETE_SET_NULL
| DICT_FOREIGN_ON_UPDATE_SET_NULL), | DICT_FOREIGN_ON_UPDATE_SET_NULL));
&index_error, &err_col, &err_index);
if (index == NULL if (index == NULL
&& !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) { && !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) {
...@@ -4244,7 +4203,7 @@ dict_foreign_push_index_error( ...@@ -4244,7 +4203,7 @@ dict_foreign_push_index_error(
const char* latest_foreign, /*!< in: start of latest foreign key const char* latest_foreign, /*!< in: start of latest foreign key
constraint name */ constraint name */
const char** columns, /*!< in: foreign key columns */ const char** columns, /*!< in: foreign key columns */
ulint index_error, /*!< in: error code */ fkerr_t index_error, /*!< in: error code */
ulint err_col, /*!< in: column where error happened ulint err_col, /*!< in: column where error happened
*/ */
dict_index_t* err_index, /*!< in: index where error happened dict_index_t* err_index, /*!< in: index where error happened
...@@ -4253,37 +4212,37 @@ dict_foreign_push_index_error( ...@@ -4253,37 +4212,37 @@ dict_foreign_push_index_error(
FILE* ef) /*!< in: output stream */ FILE* ef) /*!< in: output stream */
{ {
switch (index_error) { switch (index_error) {
case DB_FOREIGN_KEY_INDEX_NOT_FOUND: { case FK_SUCCESS:
break;
case FK_INDEX_NOT_FOUND:
fprintf(ef, fprintf(ef,
"%s table '%s' with foreign key constraint" "%s table %s with foreign key constraint"
" failed. There is no index in the referenced" " failed. There is no index in the referenced"
" table where the referenced columns appear" " table where the referenced columns appear"
" as the first columns near '%s'.\n", " as the first columns near '%s'.\n",
operation, create_name, latest_foreign); operation, create_name, latest_foreign);
ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT, ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
"%s table '%s' with foreign key constraint" "%s table %s with foreign key constraint"
" failed. There is no index in the referenced" " failed. There is no index in the referenced"
" table where the referenced columns appear" " table where the referenced columns appear"
" as the first columns near '%s'.", " as the first columns near '%s'.",
operation, create_name, latest_foreign); operation, create_name, latest_foreign);
break; return;
} case FK_IS_PREFIX_INDEX:
case DB_FOREIGN_KEY_IS_PREFIX_INDEX: {
fprintf(ef, fprintf(ef,
"%s table '%s' with foreign key constraint" "%s table %s with foreign key constraint"
" failed. There is only prefix index in the referenced" " failed. There is only prefix index in the referenced"
" table where the referenced columns appear" " table where the referenced columns appear"
" as the first columns near '%s'.\n", " as the first columns near '%s'.\n",
operation, create_name, latest_foreign); operation, create_name, latest_foreign);
ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT, ib_push_warning(trx, DB_CANNOT_ADD_CONSTRAINT,
"%s table '%s' with foreign key constraint" "%s table %s with foreign key constraint"
" failed. There is only prefix index in the referenced" " failed. There is only prefix index in the referenced"
" table where the referenced columns appear" " table where the referenced columns appear"
" as the first columns near '%s'.", " as the first columns near '%s'.",
operation, create_name, latest_foreign); operation, create_name, latest_foreign);
break; return;
} case FK_COL_NOT_NULL:
case DB_FOREIGN_KEY_COL_NOT_NULL: {
fprintf(ef, fprintf(ef,
"%s table %s with foreign key constraint" "%s table %s with foreign key constraint"
" failed. You have defined a SET NULL condition but " " failed. You have defined a SET NULL condition but "
...@@ -4294,9 +4253,8 @@ dict_foreign_push_index_error( ...@@ -4294,9 +4253,8 @@ dict_foreign_push_index_error(
" failed. You have defined a SET NULL condition but " " failed. You have defined a SET NULL condition but "
"column '%s' on index is defined as NOT NULL near '%s'.", "column '%s' on index is defined as NOT NULL near '%s'.",
operation, create_name, columns[err_col], latest_foreign); operation, create_name, columns[err_col], latest_foreign);
break; return;
} case FK_COLS_NOT_EQUAL:
case DB_FOREIGN_KEY_COLS_NOT_EQUAL: {
dict_field_t* field; dict_field_t* field;
const char* col_name; const char* col_name;
field = dict_index_get_nth_field(err_index, err_col); field = dict_index_get_nth_field(err_index, err_col);
...@@ -4315,11 +4273,9 @@ dict_foreign_push_index_error( ...@@ -4315,11 +4273,9 @@ dict_foreign_push_index_error(
" failed. Field type or character set for column '%s' " " failed. Field type or character set for column '%s' "
"does not mach referenced column '%s' near '%s'.", "does not mach referenced column '%s' near '%s'.",
operation, create_name, columns[err_col], col_name, latest_foreign); operation, create_name, columns[err_col], col_name, latest_foreign);
break; return;
}
default:
ut_error;
} }
DBUG_ASSERT(!"unknown error");
} }
/*********************************************************************//** /*********************************************************************//**
...@@ -4351,7 +4307,7 @@ dict_create_foreign_constraints_low( ...@@ -4351,7 +4307,7 @@ dict_create_foreign_constraints_low(
const char* start_of_latest_foreign = sql_string; const char* start_of_latest_foreign = sql_string;
const char* start_of_latest_set = NULL; const char* start_of_latest_set = NULL;
FILE* ef = dict_foreign_err_file; FILE* ef = dict_foreign_err_file;
ulint index_error = DB_SUCCESS; fkerr_t index_error = FK_SUCCESS;
dict_index_t* err_index = NULL; dict_index_t* err_index = NULL;
ulint err_col; ulint err_col;
const char* constraint_name; const char* constraint_name;
...@@ -6832,7 +6788,7 @@ dict_foreign_qualify_index( ...@@ -6832,7 +6788,7 @@ dict_foreign_qualify_index(
/*!< in: nonzero if none of /*!< in: nonzero if none of
the columns must be declared the columns must be declared
NOT NULL */ NOT NULL */
ulint* error, /*!< out: error code */ fkerr_t* error, /*!< out: error code */
ulint* err_col_no, ulint* err_col_no,
/*!< out: column number where /*!< out: column number where
error happened */ error happened */
...@@ -6860,7 +6816,7 @@ dict_foreign_qualify_index( ...@@ -6860,7 +6816,7 @@ dict_foreign_qualify_index(
/* We do not accept column prefix /* We do not accept column prefix
indexes here */ indexes here */
if (error && err_col_no && err_index) { if (error && err_col_no && err_index) {
*error = DB_FOREIGN_KEY_IS_PREFIX_INDEX; *error = FK_IS_PREFIX_INDEX;
*err_col_no = i; *err_col_no = i;
*err_index = (dict_index_t*)index; *err_index = (dict_index_t*)index;
} }
...@@ -6870,7 +6826,7 @@ dict_foreign_qualify_index( ...@@ -6870,7 +6826,7 @@ dict_foreign_qualify_index(
if (check_null if (check_null
&& (field->col->prtype & DATA_NOT_NULL)) { && (field->col->prtype & DATA_NOT_NULL)) {
if (error && err_col_no && err_index) { if (error && err_col_no && err_index) {
*error = DB_FOREIGN_KEY_COL_NOT_NULL; *error = FK_COL_NOT_NULL;
*err_col_no = i; *err_col_no = i;
*err_index = (dict_index_t*)index; *err_index = (dict_index_t*)index;
} }
...@@ -6900,7 +6856,7 @@ dict_foreign_qualify_index( ...@@ -6900,7 +6856,7 @@ dict_foreign_qualify_index(
dict_index_get_nth_col(types_idx, i), dict_index_get_nth_col(types_idx, i),
check_charsets)) { check_charsets)) {
if (error && err_col_no && err_index) { if (error && err_col_no && err_index) {
*error = DB_FOREIGN_KEY_COLS_NOT_EQUAL; *error = FK_COLS_NOT_EQUAL;
*err_col_no = i; *err_col_no = i;
*err_index = (dict_index_t*)index; *err_index = (dict_index_t*)index;
} }
......
...@@ -10331,17 +10331,6 @@ ha_innobase::ft_read( ...@@ -10331,17 +10331,6 @@ ha_innobase::ft_read(
} }
#ifdef WITH_WSREP #ifdef WITH_WSREP
extern dict_index_t*
wsrep_dict_foreign_find_index(
/*==========================*/
dict_table_t* table,
const char** col_names,
const char** columns,
ulint n_cols,
dict_index_t* types_idx,
ibool check_charsets,
ulint check_null);
inline inline
const char* const char*
wsrep_key_type_to_str(wsrep_key_type type) wsrep_key_type_to_str(wsrep_key_type type)
...@@ -10404,7 +10393,7 @@ wsrep_append_foreign_key( ...@@ -10404,7 +10393,7 @@ wsrep_append_foreign_key(
foreign->referenced_table_name_lookup); foreign->referenced_table_name_lookup);
if (foreign->referenced_table) { if (foreign->referenced_table) {
foreign->referenced_index = foreign->referenced_index =
wsrep_dict_foreign_find_index( dict_foreign_find_index(
foreign->referenced_table, NULL, foreign->referenced_table, NULL,
foreign->referenced_col_names, foreign->referenced_col_names,
foreign->n_fields, foreign->n_fields,
...@@ -10418,7 +10407,7 @@ wsrep_append_foreign_key( ...@@ -10418,7 +10407,7 @@ wsrep_append_foreign_key(
if (foreign->foreign_table) { if (foreign->foreign_table) {
foreign->foreign_index = foreign->foreign_index =
wsrep_dict_foreign_find_index( dict_foreign_find_index(
foreign->foreign_table, NULL, foreign->foreign_table, NULL,
foreign->foreign_col_names, foreign->foreign_col_names,
foreign->n_fields, foreign->n_fields,
......
...@@ -527,6 +527,21 @@ dict_table_open_on_name( ...@@ -527,6 +527,21 @@ dict_table_open_on_name(
dict_err_ignore_t ignore_err) dict_err_ignore_t ignore_err)
MY_ATTRIBUTE((warn_unused_result)); MY_ATTRIBUTE((warn_unused_result));
/** Outcome of dict_foreign_find_index() or dict_foreign_qualify_index() */
enum fkerr_t
{
/** A backing index was found for a FOREIGN KEY constraint */
FK_SUCCESS = 0,
/** There is no index that covers the columns in the constraint. */
FK_INDEX_NOT_FOUND,
/** The index is for a prefix index, not a full column. */
FK_IS_PREFIX_INDEX,
/** A condition of SET NULL conflicts with a NOT NULL column. */
FK_COL_NOT_NULL,
/** The column types do not match */
FK_COLS_NOT_EQUAL
};
/*********************************************************************//** /*********************************************************************//**
Tries to find an index whose first fields are the columns in the array, Tries to find an index whose first fields are the columns in the array,
in the same order and is not marked for deletion and is not the same in the same order and is not marked for deletion and is not the same
...@@ -553,11 +568,11 @@ dict_foreign_find_index( ...@@ -553,11 +568,11 @@ dict_foreign_find_index(
/*!< in: nonzero if none of /*!< in: nonzero if none of
the columns must be declared the columns must be declared
NOT NULL */ NOT NULL */
ulint* error, /*!< out: error code */ fkerr_t* error = NULL, /*!< out: error code */
ulint* err_col_no, ulint* err_col_no = NULL,
/*!< out: column number where /*!< out: column number where
error happened */ error happened */
dict_index_t** err_index) dict_index_t** err_index = NULL)
/*!< out: index where error /*!< out: index where error
happened */ happened */
...@@ -643,7 +658,7 @@ dict_foreign_qualify_index( ...@@ -643,7 +658,7 @@ dict_foreign_qualify_index(
/*!< in: nonzero if none of /*!< in: nonzero if none of
the columns must be declared the columns must be declared
NOT NULL */ NOT NULL */
ulint* error, /*!< out: error code */ fkerr_t* error, /*!< out: error code */
ulint* err_col_no, ulint* err_col_no,
/*!< out: column number where /*!< out: column number where
error happened */ error happened */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment