use consistent error messaging for IGNORE

1. the same message text for INSERT and INSERT IGNORE
2. no new warnings in UPDATE IGNORE yet (big change for 5.5)

and replace a commonly used expression with a
named constant
This commit is contained in:
Sergei Golubchik 2016-04-20 18:27:23 +02:00
parent 9e826bfa36
commit 24ac546d0f
5 changed files with 11 additions and 63 deletions

View File

@ -11,20 +11,11 @@ INSERT INTO t2 VALUES(0);
INSERT IGNORE INTO t2 VALUES(1);
Warnings:
Warning 1452 Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fld2`) REFERENCES `t1` (`fld1`))
Warning 1452 `test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fld2`) REFERENCES `t1` (`fld1`)
UPDATE IGNORE t2 SET fld2=20 WHERE fld2=0;
Warnings:
Warning 1452 `test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fld2`) REFERENCES `t1` (`fld1`)
UPDATE IGNORE t1 SET fld1=20 WHERE fld1=0;
Warnings:
Warning 1451 `test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fld2`) REFERENCES `t1` (`fld1`)
# Test for multi update.
UPDATE IGNORE t1, t2 SET t2.fld2= t2.fld2 + 3;
Warnings:
Warning 1452 `test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fld2`) REFERENCES `t1` (`fld1`)
UPDATE IGNORE t1, t2 SET t1.fld1= t1.fld1 + 3;
Warnings:
Warning 1451 `test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fld2`) REFERENCES `t1` (`fld1`)
# Reports an error since IGNORE is not used.
INSERT INTO t2 VALUES(1);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fld2`) REFERENCES `t1` (`fld1`))

View File

@ -5463,28 +5463,3 @@ fl_create_iterator(enum handler_iterator_type type,
}
}
#endif /*TRANS_LOG_MGM_EXAMPLE_CODE*/
/**
Report a warning for FK constraint violation.
@param thd Thread handle.
@param table table on which the operation is performed.
@param error handler error number.
*/
void warn_fk_constraint_violation(THD *thd,TABLE *table, int error)
{
String str;
switch(error) {
case HA_ERR_ROW_IS_REFERENCED:
table->file->get_error_message(error, &str);
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
ER_ROW_IS_REFERENCED_2, str.c_ptr_safe());
break;
case HA_ERR_NO_REFERENCED_ROW:
table->file->get_error_message(error, &str);
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
ER_NO_REFERENCED_ROW_2, str.c_ptr_safe());
break;
}
}

View File

@ -335,6 +335,7 @@
#define HA_CHECK_DUP_UNIQUE 2
#define HA_CHECK_FK_ERROR 4
#define HA_CHECK_DUP (HA_CHECK_DUP_KEY + HA_CHECK_DUP_UNIQUE)
#define HA_CHECK_ALL (~0U)
enum legacy_db_type
{
@ -3110,6 +3111,4 @@ inline const char *table_case_name(HA_CREATE_INFO *info, const char *name)
return ((lower_case_table_names == 2 && info->alias) ? info->alias : name);
}
void warn_fk_constraint_violation(THD *thd, TABLE *table, int error);
#endif /* HANDLER_INCLUDED */

View File

@ -1609,9 +1609,10 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
else
table->file->insert_id_for_cur_row= insert_id_for_cur_row;
bool is_duplicate_key_error;
if (table->file->is_fatal_error(error, HA_CHECK_DUP | HA_CHECK_FK_ERROR))
if (table->file->is_fatal_error(error, HA_CHECK_ALL))
goto err;
is_duplicate_key_error= table->file->is_fatal_error(error, 0);
is_duplicate_key_error=
table->file->is_fatal_error(error, HA_CHECK_ALL & ~HA_CHECK_DUP);
if (!is_duplicate_key_error)
{
/*
@ -1712,8 +1713,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
error != HA_ERR_RECORD_IS_THE_SAME)
{
if (info->ignore &&
!table->file->is_fatal_error(error, HA_CHECK_DUP_KEY |
HA_CHECK_FK_ERROR))
!table->file->is_fatal_error(error, HA_CHECK_ALL))
{
if (!(thd->variables.old_behavior &
OLD_MODE_NO_DUP_KEY_WARNINGS_WITH_IGNORE))
@ -1845,7 +1845,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
{
DEBUG_SYNC(thd, "write_row_noreplace");
if (!info->ignore ||
table->file->is_fatal_error(error, HA_CHECK_DUP | HA_CHECK_FK_ERROR))
table->file->is_fatal_error(error, HA_CHECK_ALL))
goto err;
if (!(thd->variables.old_behavior &
OLD_MODE_NO_DUP_KEY_WARNINGS_WITH_IGNORE))
@ -1866,9 +1866,6 @@ ok_or_after_trg_err:
my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
if (!table->file->has_transactions())
thd->transaction.stmt.modified_non_trans_table= TRUE;
if (info->ignore &&
!table->file->is_fatal_error(error, HA_CHECK_FK_ERROR))
warn_fk_constraint_violation(thd, table, error);
DBUG_RETURN(trg_error);
err:

View File

@ -774,8 +774,7 @@ int mysql_update(THD *thd,
error= 0;
}
else if (!ignore ||
table->file->is_fatal_error(error, HA_CHECK_DUP_KEY |
HA_CHECK_FK_ERROR))
table->file->is_fatal_error(error, HA_CHECK_ALL))
{
/*
If (ignore && error is ignorable) we don't have to
@ -783,8 +782,7 @@ int mysql_update(THD *thd,
*/
myf flags= 0;
if (table->file->is_fatal_error(error, HA_CHECK_DUP_KEY |
HA_CHECK_FK_ERROR))
if (table->file->is_fatal_error(error, HA_CHECK_ALL))
flags|= ME_FATALERROR; /* Other handler errors are fatal */
prepare_record_for_error_message(error, table);
@ -792,9 +790,6 @@ int mysql_update(THD *thd,
error= 1;
break;
}
else if (ignore && !table->file->is_fatal_error(error,
HA_CHECK_FK_ERROR))
warn_fk_constraint_violation(thd, table, error);
}
if (table->triggers &&
@ -1974,8 +1969,7 @@ int multi_update::send_data(List<Item> &not_used_values)
{
updated--;
if (!ignore ||
table->file->is_fatal_error(error, HA_CHECK_DUP_KEY |
HA_CHECK_FK_ERROR))
table->file->is_fatal_error(error, HA_CHECK_ALL))
{
/*
If (ignore && error == is ignorable) we don't have to
@ -1983,17 +1977,13 @@ int multi_update::send_data(List<Item> &not_used_values)
*/
myf flags= 0;
if (table->file->is_fatal_error(error, HA_CHECK_DUP_KEY |
HA_CHECK_FK_ERROR))
if (table->file->is_fatal_error(error, HA_CHECK_ALL))
flags|= ME_FATALERROR; /* Other handler errors are fatal */
prepare_record_for_error_message(error, table);
table->file->print_error(error,MYF(flags));
DBUG_RETURN(1);
}
else if (ignore && !table->file->is_fatal_error(error,
HA_CHECK_FK_ERROR))
warn_fk_constraint_violation(thd, table, error);
}
else
{
@ -2266,15 +2256,11 @@ int multi_update::do_updates()
local_error != HA_ERR_RECORD_IS_THE_SAME)
{
if (!ignore ||
table->file->is_fatal_error(local_error, HA_CHECK_DUP_KEY |
HA_CHECK_FK_ERROR))
table->file->is_fatal_error(local_error, HA_CHECK_ALL))
{
err_table= table;
goto err;
}
else if (ignore && !table->file->is_fatal_error(local_error,
HA_CHECK_FK_ERROR))
warn_fk_constraint_violation(thd, table, local_error);
}
if (local_error != HA_ERR_RECORD_IS_THE_SAME)
updated++;