cleanup: RAII helper for changing thd->count_cuted_rows

This commit is contained in:
Sergei Golubchik 2020-11-23 19:40:47 +01:00
parent 08b0b70daa
commit 00f54b56b1
12 changed files with 42 additions and 64 deletions

View file

@ -1867,21 +1867,15 @@ bool Field::compatible_field_size(uint field_metadata,
int Field::store(const char *to, size_t length, CHARSET_INFO *cs, int Field::store(const char *to, size_t length, CHARSET_INFO *cs,
enum_check_fields check_level) enum_check_fields check_level)
{ {
int res; Check_level_instant_set check_level_save(get_thd(), check_level);
THD *thd= get_thd(); return store(to, length, cs);
enum_check_fields old_check_level= thd->count_cuted_fields;
thd->count_cuted_fields= check_level;
res= store(to, length, cs);
thd->count_cuted_fields= old_check_level;
return res;
} }
int Field::store_timestamp(my_time_t ts, ulong sec_part) int Field::store_timestamp(my_time_t ts, ulong sec_part)
{ {
MYSQL_TIME ltime; MYSQL_TIME ltime;
THD *thd= get_thd(); get_thd()->timestamp_to_TIME(&ltime, ts, sec_part, 0);
thd->timestamp_to_TIME(&ltime, ts, sec_part, 0);
return store_time_dec(&ltime, decimals()); return store_time_dec(&ltime, decimals());
} }

View file

@ -3292,7 +3292,6 @@ int handler::update_auto_increment()
THD *thd= table->in_use; THD *thd= table->in_use;
struct system_variables *variables= &thd->variables; struct system_variables *variables= &thd->variables;
int result=0, tmp; int result=0, tmp;
enum enum_check_fields save_count_cuted_fields;
DBUG_ENTER("handler::update_auto_increment"); DBUG_ENTER("handler::update_auto_increment");
/* /*
@ -3434,10 +3433,10 @@ int handler::update_auto_increment()
nr, append ? nb_reserved_values : 0)); nr, append ? nb_reserved_values : 0));
/* Store field without warning (Warning will be printed by insert) */ /* Store field without warning (Warning will be printed by insert) */
save_count_cuted_fields= thd->count_cuted_fields; {
thd->count_cuted_fields= CHECK_FIELD_IGNORE; Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
tmp= table->next_number_field->store((longlong)nr, TRUE); tmp= table->next_number_field->store((longlong)nr, TRUE);
thd->count_cuted_fields= save_count_cuted_fields; }
if (unlikely(tmp)) // Out of range value in store if (unlikely(tmp)) // Out of range value in store
{ {

View file

@ -1620,18 +1620,13 @@ int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
int res; int res;
TABLE *table= field->table; TABLE *table= field->table;
THD *thd= table->in_use; THD *thd= table->in_use;
enum_check_fields tmp= thd->count_cuted_fields; Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); Sql_mode_save sql_mode(thd);
sql_mode_t sql_mode= thd->variables.sql_mode;
thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE); thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
thd->variables.sql_mode|= MODE_INVALID_DATES; thd->variables.sql_mode|= MODE_INVALID_DATES;
thd->count_cuted_fields= CHECK_FIELD_IGNORE; my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
res= save_in_field(field, no_conversions); res= save_in_field(field, no_conversions);
thd->count_cuted_fields= tmp;
dbug_tmp_restore_column_map(table->write_set, old_map); dbug_tmp_restore_column_map(table->write_set, old_map);
thd->variables.sql_mode= sql_mode;
return res; return res;
} }

View file

@ -343,19 +343,18 @@ static bool convert_const_to_int(THD *thd, Item_field *field_item,
if ((*item)->const_item() && !(*item)->is_expensive()) if ((*item)->const_item() && !(*item)->is_expensive())
{ {
TABLE *table= field->table; TABLE *table= field->table;
sql_mode_t orig_sql_mode= thd->variables.sql_mode; Sql_mode_save sql_mode(thd);
enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields; Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
my_bitmap_map *old_maps[2] = { NULL, NULL }; my_bitmap_map *old_maps[2] = { NULL, NULL };
ulonglong UNINIT_VAR(orig_field_val); /* original field value if valid */ ulonglong UNINIT_VAR(orig_field_val); /* original field value if valid */
/* table->read_set may not be set if we come here from a CREATE TABLE */ /* table->read_set may not be set if we come here from a CREATE TABLE */
if (table && table->read_set) if (table && table->read_set)
dbug_tmp_use_all_columns(table, old_maps, dbug_tmp_use_all_columns(table, old_maps,
table->read_set, table->write_set); table->read_set, table->write_set);
/* For comparison purposes allow invalid dates like 2000-01-32 */ /* For comparison purposes allow invalid dates like 2000-01-32 */
thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) | thd->variables.sql_mode= (thd->variables.sql_mode & ~MODE_NO_ZERO_DATE) |
MODE_INVALID_DATES; MODE_INVALID_DATES;
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
/* /*
Store the value of the field/constant because the call to save_in_field Store the value of the field/constant because the call to save_in_field
@ -392,8 +391,6 @@ static bool convert_const_to_int(THD *thd, Item_field *field_item,
/* orig_field_val must be a valid value that can be restored back. */ /* orig_field_val must be a valid value that can be restored back. */
DBUG_ASSERT(!result); DBUG_ASSERT(!result);
} }
thd->variables.sql_mode= orig_sql_mode;
thd->count_cuted_fields= orig_count_cuted_fields;
if (table && table->read_set) if (table && table->read_set)
dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_maps); dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_maps);
} }

View file

@ -1180,8 +1180,6 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
CHARSET_INFO *db_cs= get_default_db_collation(thd, sp->m_db.str); CHARSET_INFO *db_cs= get_default_db_collation(thd, sp->m_db.str);
enum_check_fields saved_count_cuted_fields;
bool store_failed= FALSE; bool store_failed= FALSE;
DBUG_ENTER("sp_create_routine"); DBUG_ENTER("sp_create_routine");
DBUG_PRINT("enter", ("type: %s name: %.*s", DBUG_PRINT("enter", ("type: %s name: %.*s",
@ -1215,8 +1213,7 @@ Sp_handler::sp_create_routine(THD *thd, const sp_head *sp) const
/* Reset sql_mode during data dictionary operations. */ /* Reset sql_mode during data dictionary operations. */
thd->variables.sql_mode= 0; thd->variables.sql_mode= 0;
saved_count_cuted_fields= thd->count_cuted_fields; Check_level_instant_set check_level_save(thd, CHECK_FIELD_WARN);
thd->count_cuted_fields= CHECK_FIELD_WARN;
if (!(table= open_proc_table_for_update(thd))) if (!(table= open_proc_table_for_update(thd)))
{ {
@ -1476,7 +1473,6 @@ log:
ret= FALSE; ret= FALSE;
done: done:
thd->count_cuted_fields= saved_count_cuted_fields;
thd->variables.sql_mode= saved_mode; thd->variables.sql_mode= saved_mode;
DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row()); DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
DBUG_RETURN(ret); DBUG_RETURN(ret);

View file

@ -6581,6 +6581,23 @@ class Switch_to_definer_security_ctx
}; };
class Check_level_instant_set
{
THD *m_thd;
enum_check_fields m_check_level;
public:
Check_level_instant_set(THD *thd, enum_check_fields temporary_value)
:m_thd(thd), m_check_level(thd->count_cuted_fields)
{
thd->count_cuted_fields= temporary_value;
}
~Check_level_instant_set()
{
m_thd->count_cuted_fields= m_check_level;
}
};
/** /**
This class resembles the SQL Standard schema qualified object name: This class resembles the SQL Standard schema qualified object name:
<schema qualified name> ::= [ <schema name> <period> ] <qualified identifier> <schema qualified name> ::= [ <schema name> <period> ] <qualified identifier>

View file

@ -23313,8 +23313,7 @@ cmp_buffer_with_ref(THD *thd, TABLE *table, TABLE_REF *tab_ref)
bool bool
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref) cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
{ {
enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields; Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
bool result= 0; bool result= 0;
@ -23326,7 +23325,6 @@ cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
break; break;
} }
} }
thd->count_cuted_fields= save_count_cuted_fields;
dbug_tmp_restore_column_map(table->write_set, old_map); dbug_tmp_restore_column_map(table->write_set, old_map);
return result; return result;
} }

View file

@ -1872,18 +1872,11 @@ public:
{ {
enum store_key_result result; enum store_key_result result;
THD *thd= to_field->table->in_use; THD *thd= to_field->table->in_use;
enum_check_fields saved_count_cuted_fields= thd->count_cuted_fields; Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
sql_mode_t orig_sql_mode= thd->variables.sql_mode; Sql_mode_save sql_mode(thd);
thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE); thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
thd->variables.sql_mode|= MODE_INVALID_DATES; thd->variables.sql_mode|= MODE_INVALID_DATES;
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
result= copy_inner(); result= copy_inner();
thd->count_cuted_fields= saved_count_cuted_fields;
thd->variables.sql_mode= orig_sql_mode;
return result; return result;
} }

View file

@ -3695,7 +3695,7 @@ static bool show_status_array(THD *thd, const char *wild,
char name_buffer[NAME_CHAR_LEN]; char name_buffer[NAME_CHAR_LEN];
int len; int len;
SHOW_VAR tmp, *var; SHOW_VAR tmp, *var;
enum_check_fields save_count_cuted_fields= thd->count_cuted_fields; Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
bool res= FALSE; bool res= FALSE;
CHARSET_INFO *charset= system_charset_info; CHARSET_INFO *charset= system_charset_info;
DBUG_ENTER("show_status_array"); DBUG_ENTER("show_status_array");
@ -3818,7 +3818,6 @@ static bool show_status_array(THD *thd, const char *wild,
} }
} }
end: end:
thd->count_cuted_fields= save_count_cuted_fields;
DBUG_RETURN(res); DBUG_RETURN(res);
} }
@ -4543,8 +4542,7 @@ fill_schema_table_by_open(THD *thd, MEM_ROOT *mem_root,
Open_tables_backup *open_tables_state_backup, Open_tables_backup *open_tables_state_backup,
bool can_deadlock) bool can_deadlock)
{ {
Query_arena i_s_arena(mem_root, Query_arena i_s_arena(mem_root, Query_arena::STMT_CONVENTIONAL_EXECUTION),
Query_arena::STMT_CONVENTIONAL_EXECUTION),
backup_arena, *old_arena; backup_arena, *old_arena;
LEX *old_lex= thd->lex, temp_lex, *lex; LEX *old_lex= thd->lex, temp_lex, *lex;
LEX_CSTRING db_name, table_name; LEX_CSTRING db_name, table_name;
@ -5058,12 +5056,9 @@ end:
class Warnings_only_error_handler : public Internal_error_handler class Warnings_only_error_handler : public Internal_error_handler
{ {
public: public:
bool handle_condition(THD *thd, bool handle_condition(THD *thd, uint sql_errno, const char* sqlstate,
uint sql_errno,
const char* sqlstate,
Sql_condition::enum_warning_level *level, Sql_condition::enum_warning_level *level,
const char* msg, const char* msg, Sql_condition ** cond_hdl)
Sql_condition ** cond_hdl)
{ {
if (sql_errno == ER_TRG_NO_DEFINER || sql_errno == ER_TRG_NO_CREATION_CTX) if (sql_errno == ER_TRG_NO_DEFINER || sql_errno == ER_TRG_NO_CREATION_CTX)
return true; return true;

View file

@ -2831,7 +2831,6 @@ int read_statistics_for_table(THD *thd, TABLE *table, TABLE_LIST *stat_tables)
Field **field_ptr; Field **field_ptr;
KEY *key_info, *key_info_end; KEY *key_info, *key_info_end;
TABLE_SHARE *table_share= table->s; TABLE_SHARE *table_share= table->s;
enum_check_fields old_check_level= thd->count_cuted_fields;
DBUG_ENTER("read_statistics_for_table"); DBUG_ENTER("read_statistics_for_table");
DEBUG_SYNC(thd, "statistics_mem_alloc_start1"); DEBUG_SYNC(thd, "statistics_mem_alloc_start1");
@ -2847,7 +2846,7 @@ int read_statistics_for_table(THD *thd, TABLE *table, TABLE_LIST *stat_tables)
} }
/* Don't write warnings for internal field conversions */ /* Don't write warnings for internal field conversions */
thd->count_cuted_fields= CHECK_FIELD_IGNORE; Check_level_instant_set check_level_save(thd, CHECK_FIELD_IGNORE);
/* Read statistics from the statistical table table_stats */ /* Read statistics from the statistical table table_stats */
Table_statistics *read_stats= table_share->stats_cb.table_stats; Table_statistics *read_stats= table_share->stats_cb.table_stats;
@ -2929,7 +2928,6 @@ int read_statistics_for_table(THD *thd, TABLE *table, TABLE_LIST *stat_tables)
} }
} }
thd->count_cuted_fields= old_check_level;
table_share->stats_cb.end_stats_load(); table_share->stats_cb.end_stats_load();
DBUG_RETURN(0); DBUG_RETURN(0);
} }

View file

@ -9939,16 +9939,14 @@ do_continue:;
if (use_inplace) if (use_inplace)
{ {
table->s->frm_image= &frm; table->s->frm_image= &frm;
enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
/* /*
Set the truncated column values of thd as warning Set the truncated column values of thd as warning
for alter table. for alter table.
*/ */
thd->count_cuted_fields = CHECK_FIELD_WARN; Check_level_instant_set check_level_save(thd, CHECK_FIELD_WARN);
int res= mysql_inplace_alter_table(thd, table_list, table, altered_table, int res= mysql_inplace_alter_table(thd, table_list, table, altered_table,
&ha_alter_info, inplace_supported, &ha_alter_info, inplace_supported,
&target_mdl_request, &alter_ctx); &target_mdl_request, &alter_ctx);
thd->count_cuted_fields= save_count_cuted_fields;
my_free(const_cast<uchar*>(frm.str)); my_free(const_cast<uchar*>(frm.str));
if (res) if (res)

View file

@ -1032,7 +1032,6 @@ static bool make_empty_rec(THD *thd, uchar *buff, uint table_options,
TABLE table; TABLE table;
TABLE_SHARE share; TABLE_SHARE share;
Create_field *field; Create_field *field;
enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
DBUG_ENTER("make_empty_rec"); DBUG_ENTER("make_empty_rec");
/* We need a table to generate columns for default values */ /* We need a table to generate columns for default values */
@ -1051,7 +1050,7 @@ static bool make_empty_rec(THD *thd, uchar *buff, uint table_options,
null_pos= buff; null_pos= buff;
List_iterator<Create_field> it(create_fields); List_iterator<Create_field> it(create_fields);
thd->count_cuted_fields= CHECK_FIELD_WARN; // To find wrong default values Check_level_instant_set check_level_save(thd, CHECK_FIELD_WARN);
while ((field=it++)) while ((field=it++))
{ {
/* regfield don't have to be deleted as it's allocated on THD::mem_root */ /* regfield don't have to be deleted as it's allocated on THD::mem_root */
@ -1131,6 +1130,5 @@ static bool make_empty_rec(THD *thd, uchar *buff, uint table_options,
*(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1); *(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
err: err:
thd->count_cuted_fields= old_count_cuted_fields;
DBUG_RETURN(error); DBUG_RETURN(error);
} /* make_empty_rec */ } /* make_empty_rec */