1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

cleanup: on update default now

* remove one level of virtual functions
* remove redundant checks
* remove an if() as the value is always known at compilation time

don't pretend that "DEFAULT expr" and "ON UPDATE DEFAULT NOW"
are "basically the same thing"
This commit is contained in:
Sergei Golubchik
2019-09-02 10:53:46 +02:00
parent ef00ac4c86
commit 17ab02f4b0
7 changed files with 33 additions and 51 deletions

View File

@ -992,14 +992,6 @@ public:
} }
bool set_explicit_default(Item *value); bool set_explicit_default(Item *value);
/**
Evaluates the @c UPDATE default function, if one exists, and stores the
result in the record buffer. If no such function exists for the column,
or the function is not valid for the column's data type, invoking this
function has no effect.
*/
virtual int evaluate_update_default_function() { return 0; }
virtual bool binary() const { return 1; } virtual bool binary() const { return 1; }
virtual bool zero_pack() const { return 1; } virtual bool zero_pack() const { return 1; }
virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; } virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
@ -2519,13 +2511,6 @@ public:
void sql_type(String &str) const; void sql_type(String &str) const;
bool zero_pack() const { return 0; } bool zero_pack() const { return 0; }
int set_time(); int set_time();
int evaluate_update_default_function()
{
int res= 0;
if (has_update_default_function())
res= set_time();
return res;
}
/* Get TIMESTAMP field value as seconds since begging of Unix Epoch */ /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
virtual my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const; virtual my_time_t get_timestamp(const uchar *pos, ulong *sec_part) const;
my_time_t get_timestamp(ulong *sec_part) const my_time_t get_timestamp(ulong *sec_part) const
@ -2954,13 +2939,6 @@ public:
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate) bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
{ return Field_datetime::get_TIME(ltime, ptr, fuzzydate); } { return Field_datetime::get_TIME(ltime, ptr, fuzzydate); }
int set_time(); int set_time();
int evaluate_update_default_function()
{
int res= 0;
if (has_update_default_function())
res= set_time();
return res;
}
uchar *pack(uchar* to, const uchar *from, uchar *pack(uchar* to, const uchar *from,
uint max_length __attribute__((unused))) uint max_length __attribute__((unused)))
{ {

View File

@ -8072,7 +8072,7 @@ fill_record(THD *thd, TABLE *table_arg, List<Item> &fields, List<Item> &values,
} }
if (!update && table_arg->default_field && if (!update && table_arg->default_field &&
table_arg->update_default_fields(0, ignore_errors)) table_arg->update_default_fields(ignore_errors))
goto err; goto err;
/* Update virtual fields */ /* Update virtual fields */
if (table_arg->vfield && if (table_arg->vfield &&
@ -8317,7 +8317,7 @@ fill_record(THD *thd, TABLE *table, Field **ptr, List<Item> &values,
all_fields_have_values &= field->set_explicit_default(value); all_fields_have_values &= field->set_explicit_default(value);
} }
if (!all_fields_have_values && table->default_field && if (!all_fields_have_values && table->default_field &&
table->update_default_fields(0, ignore_errors)) table->update_default_fields(ignore_errors))
goto err; goto err;
/* Update virtual fields */ /* Update virtual fields */
thd->abort_on_warning= FALSE; thd->abort_on_warning= FALSE;

View File

@ -1796,10 +1796,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
be updated as if this is an UPDATE. be updated as if this is an UPDATE.
*/ */
if (different_records && table->default_field) if (different_records && table->default_field)
{ table->evaluate_update_default_function();
if (table->update_default_fields(1, info->ignore))
goto err;
}
/* CHECK OPTION for VIEW ... ON DUPLICATE KEY UPDATE ... */ /* CHECK OPTION for VIEW ... ON DUPLICATE KEY UPDATE ... */
res= info->table_list->view_check_option(table->in_use, info->ignore); res= info->table_list->view_check_option(table->in_use, info->ignore);
@ -3762,7 +3759,7 @@ int select_insert::send_data(List<Item> &values)
thd->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields thd->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
store_values(values); store_values(values);
if (table->default_field && table->update_default_fields(0, info.ignore)) if (table->default_field && table->update_default_fields(info.ignore))
DBUG_RETURN(1); DBUG_RETURN(1);
thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL; thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
if (thd->is_error()) if (thd->is_error())

View File

@ -9455,7 +9455,7 @@ do_continue:;
/* Check that we can call default functions with default field values */ /* Check that we can call default functions with default field values */
altered_table->reset_default_fields(); altered_table->reset_default_fields();
if (altered_table->default_field && if (altered_table->default_field &&
altered_table->update_default_fields(0, 1)) altered_table->update_default_fields(true))
goto err_new_table_cleanup; goto err_new_table_cleanup;
// Ask storage engine whether to use copy or in-place // Ask storage engine whether to use copy or in-place
@ -10138,7 +10138,7 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to,
} }
prev_insert_id= to->file->next_insert_id; prev_insert_id= to->file->next_insert_id;
if (to->default_field) if (to->default_field)
to->update_default_fields(0, ignore); to->update_default_fields(ignore);
if (to->vfield) if (to->vfield)
to->update_virtual_fields(to->file, VCOL_UPDATE_FOR_WRITE); to->update_virtual_fields(to->file, VCOL_UPDATE_FOR_WRITE);

View File

@ -761,11 +761,8 @@ int mysql_update(THD *thd,
if (!can_compare_record || compare_record(table)) if (!can_compare_record || compare_record(table))
{ {
if (table->default_field && table->update_default_fields(1, ignore)) if (table->default_field)
{ table->evaluate_update_default_function();
error= 1;
break;
}
if ((res= table_list->view_check_option(thd, ignore)) != if ((res= table_list->view_check_option(thd, ignore)) !=
VIEW_CHECK_OK) VIEW_CHECK_OK)
{ {
@ -2156,8 +2153,8 @@ int multi_update::send_data(List<Item> &not_used_values)
{ {
int error; int error;
if (table->default_field && table->update_default_fields(1, ignore)) if (table->default_field)
DBUG_RETURN(1); table->evaluate_update_default_function();
if ((error= cur_table->view_check_option(thd, ignore)) != if ((error= cur_table->view_check_option(thd, ignore)) !=
VIEW_CHECK_OK) VIEW_CHECK_OK)
@ -2482,9 +2479,8 @@ int multi_update::do_updates()
if (!can_compare_record || compare_record(table)) if (!can_compare_record || compare_record(table))
{ {
int error; int error;
if (table->default_field && if (table->default_field)
(error= table->update_default_fields(1, ignore))) table->evaluate_update_default_function();
goto err2;
if (table->vfield && if (table->vfield &&
table->update_virtual_fields(table->file, VCOL_UPDATE_FOR_WRITE)) table->update_virtual_fields(table->file, VCOL_UPDATE_FOR_WRITE))
goto err2; goto err2;

View File

@ -7736,7 +7736,7 @@ int TABLE::update_virtual_field(Field *vf)
ignore_errors == 0. If set then an error was generated. ignore_errors == 0. If set then an error was generated.
*/ */
int TABLE::update_default_fields(bool update_command, bool ignore_errors) int TABLE::update_default_fields(bool ignore_errors)
{ {
Query_arena backup_arena; Query_arena backup_arena;
Field **field_ptr; Field **field_ptr;
@ -7756,14 +7756,9 @@ int TABLE::update_default_fields(bool update_command, bool ignore_errors)
*/ */
if (!field->has_explicit_value()) if (!field->has_explicit_value())
{ {
if (!update_command) if (field->default_value &&
{ (field->default_value->flags || field->flags & BLOB_FLAG))
if (field->default_value && res|= (field->default_value->expr->save_in_field(field, 0) < 0);
(field->default_value->flags || field->flags & BLOB_FLAG))
res|= (field->default_value->expr->save_in_field(field, 0) < 0);
}
else
res|= field->evaluate_update_default_function();
if (!ignore_errors && res) if (!ignore_errors && res)
{ {
my_error(ER_CALCULATING_DEFAULT_VALUE, MYF(0), field->field_name); my_error(ER_CALCULATING_DEFAULT_VALUE, MYF(0), field->field_name);
@ -7776,6 +7771,21 @@ int TABLE::update_default_fields(bool update_command, bool ignore_errors)
DBUG_RETURN(res); DBUG_RETURN(res);
} }
void TABLE::evaluate_update_default_function()
{
DBUG_ENTER("TABLE::evaluate_update_default_function");
if (s->has_update_default_function)
for (Field **field_ptr= default_field; *field_ptr ; field_ptr++)
{
Field *field= (*field_ptr);
if (!field->has_explicit_value() && field->has_update_default_function())
field->set_time();
}
DBUG_VOID_RETURN;
}
/** /**
Reset markers that fields are being updated Reset markers that fields are being updated
*/ */

View File

@ -1453,7 +1453,8 @@ public:
ulong actual_key_flags(KEY *keyinfo); ulong actual_key_flags(KEY *keyinfo);
int update_virtual_field(Field *vf); int update_virtual_field(Field *vf);
int update_virtual_fields(handler *h, enum_vcol_update_mode update_mode); int update_virtual_fields(handler *h, enum_vcol_update_mode update_mode);
int update_default_fields(bool update, bool ignore_errors); int update_default_fields(bool ignore_errors);
void evaluate_update_default_function();
void reset_default_fields(); void reset_default_fields();
inline ha_rows stat_records() { return used_stat_records; } inline ha_rows stat_records() { return used_stat_records; }