Skip to content

Commit cd51c7f

Browse files
committed
move away from TIMESTAMP_DNUN_FIELD/TIMESTAMP_DN_FIELD code
use the new approach with Field->default_value expressions. But keep the old TIMESTAMP_UN_FIELD for ON UPDATE NOW().
1 parent 12d2c4f commit cd51c7f

File tree

9 files changed

+85
-171
lines changed

9 files changed

+85
-171
lines changed

sql/field.cc

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4920,12 +4920,12 @@ void Field_double::sql_type(String &res) const
49204920
field has NOW() as default and is updated when row changes, else it is
49214921
field which has 0 as default value and is not automatically updated.
49224922
TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
4923-
automatically (TIMESTAMP DEFAULT NOW())
4923+
automatically (TIMESTAMP DEFAULT NOW()), not used in Field since 10.2.2
49244924
TIMESTAMP_UN_FIELD - field which is set on update automatically but has not
49254925
NOW() as default (but it may has 0 or some other const timestamp as
49264926
default) (TIMESTAMP ON UPDATE NOW()).
49274927
TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on
4928-
update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
4928+
update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW()), not used in Field since 10.2.2
49294929
NONE - field which is not auto-set on update with some other than NOW()
49304930
default value (TIMESTAMP DEFAULT 0).
49314931
@@ -4956,8 +4956,8 @@ Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
49564956
this field will be automaticly updated on insert.
49574957
*/
49584958
flags|= TIMESTAMP_FLAG;
4959-
if (unireg_check != TIMESTAMP_DN_FIELD)
4960-
flags|= ON_UPDATE_NOW_FLAG;
4959+
flags|= ON_UPDATE_NOW_FLAG;
4960+
DBUG_ASSERT(unireg_check == TIMESTAMP_UN_FIELD);
49614961
}
49624962
}
49634963

@@ -10561,40 +10561,24 @@ Column_definition::Column_definition(THD *thd, Field *old_field,
1056110561
- The column didn't have a default expression
1056210562
*/
1056310563
if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
10564-
old_field->ptr != NULL &&
10565-
orig_field != NULL &&
10566-
!default_value)
10564+
old_field->ptr != NULL && orig_field != NULL)
1056710565
{
10568-
bool default_now= false;
10569-
if (real_type_with_now_as_default(sql_type))
10570-
{
10571-
// The SQL type of the new field allows a function default:
10572-
default_now= orig_field->has_insert_default_function();
10573-
bool update_now= orig_field->has_update_default_function();
10574-
10575-
if (default_now && update_now)
10576-
unireg_check= Field::TIMESTAMP_DNUN_FIELD;
10577-
else if (default_now)
10578-
unireg_check= Field::TIMESTAMP_DN_FIELD;
10579-
else if (update_now)
10580-
unireg_check= Field::TIMESTAMP_UN_FIELD;
10581-
}
10582-
if (!default_now) // Give a constant default
10566+
if (orig_field->has_update_default_function())
10567+
unireg_check= Field::TIMESTAMP_UN_FIELD;
10568+
10569+
/* Get the value from default_values */
10570+
const uchar *dv= orig_field->table->s->default_values;
10571+
if (!default_value && !orig_field->is_null_in_record(dv))
1058310572
{
10584-
/* Get the value from default_values */
10585-
const uchar *dv= orig_field->table->s->default_values;
10586-
if (!orig_field->is_null_in_record(dv))
10587-
{
10588-
StringBuffer<MAX_FIELD_WIDTH> tmp(charset);
10589-
String *res= orig_field->val_str(&tmp, orig_field->ptr_in_record(dv));
10590-
char *pos= (char*) thd->strmake(res->ptr(), res->length());
10591-
default_value= new (thd->mem_root) Virtual_column_info();
10592-
default_value->expr_str.str= pos;
10593-
default_value->expr_str.length= res->length();
10594-
default_value->expr_item=
10595-
new (thd->mem_root) Item_string(thd, pos, res->length(), charset);
10596-
default_value->utf8= 0;
10597-
}
10573+
StringBuffer<MAX_FIELD_WIDTH> tmp(charset);
10574+
String *res= orig_field->val_str(&tmp, orig_field->ptr_in_record(dv));
10575+
char *pos= (char*) thd->strmake(res->ptr(), res->length());
10576+
default_value= new (thd->mem_root) Virtual_column_info();
10577+
default_value->expr_str.str= pos;
10578+
default_value->expr_str.length= res->length();
10579+
default_value->expr_item=
10580+
new (thd->mem_root) Item_string(thd, pos, res->length(), charset);
10581+
default_value->utf8= 0;
1059810582
}
1059910583
}
1060010584
}

sql/field.h

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -473,20 +473,6 @@ inline bool is_temporal_type_with_date(enum_field_types type)
473473
}
474474

475475

476-
/**
477-
Tests if a field real type can have "DEFAULT CURRENT_TIMESTAMP"
478-
479-
@param type Field type, as returned by field->real_type().
480-
@retval true If field real type can have "DEFAULT CURRENT_TIMESTAMP".
481-
@retval false If field real type can not have "DEFAULT CURRENT_TIMESTAMP".
482-
*/
483-
inline bool real_type_with_now_as_default(enum_field_types type)
484-
{
485-
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
486-
type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
487-
}
488-
489-
490476
/**
491477
Recognizer for concrete data type (called real_type for some reason),
492478
returning true if it is one of the TIMESTAMP types.
@@ -928,16 +914,9 @@ class Field: public Value_source
928914
}
929915
virtual void set_default();
930916

931-
bool has_insert_default_function() const
932-
{
933-
return (unireg_check == TIMESTAMP_DN_FIELD ||
934-
unireg_check == TIMESTAMP_DNUN_FIELD);
935-
}
936-
937917
bool has_update_default_function() const
938918
{
939-
return (unireg_check == TIMESTAMP_UN_FIELD ||
940-
unireg_check == TIMESTAMP_DNUN_FIELD);
919+
return unireg_check == TIMESTAMP_UN_FIELD;
941920
}
942921

943922
/*
@@ -2377,21 +2356,7 @@ class Field_timestamp :public Field_temporal {
23772356
void sql_type(String &str) const;
23782357
bool zero_pack() const { return 0; }
23792358
virtual int set_time();
2380-
virtual void set_default()
2381-
{
2382-
if (has_insert_default_function())
2383-
set_time();
2384-
else
2385-
Field::set_default();
2386-
}
23872359
virtual void set_explicit_default(Item *value);
2388-
virtual int evaluate_insert_default_function()
2389-
{
2390-
int res= 0;
2391-
if (has_insert_default_function())
2392-
res= set_time();
2393-
return res;
2394-
}
23952360
virtual int evaluate_update_default_function()
23962361
{
23972362
int res= 0;
@@ -2821,20 +2786,6 @@ class Field_datetime :public Field_temporal_with_date {
28212786
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
28222787
{ return Field_datetime::get_TIME(ltime, ptr, fuzzydate); }
28232788
virtual int set_time();
2824-
virtual void set_default()
2825-
{
2826-
if (has_insert_default_function())
2827-
set_time();
2828-
else
2829-
Field::set_default();
2830-
}
2831-
virtual int evaluate_insert_default_function()
2832-
{
2833-
int res= 0;
2834-
if (has_insert_default_function())
2835-
res= set_time();
2836-
return res;
2837-
}
28382789
virtual int evaluate_update_default_function()
28392790
{
28402791
int res= 0;
@@ -3813,9 +3764,7 @@ class Column_definition: public Sql_alloc
38133764

38143765
bool has_default_function() const
38153766
{
3816-
return (unireg_check == Field::TIMESTAMP_DN_FIELD ||
3817-
unireg_check == Field::TIMESTAMP_DNUN_FIELD ||
3818-
unireg_check == Field::TIMESTAMP_UN_FIELD ||
3767+
return (unireg_check == Field::TIMESTAMP_UN_FIELD ||
38193768
unireg_check == Field::NEXT_NUMBER);
38203769
}
38213770

sql/item.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,7 @@ bool Item_field::check_field_expression_processor(void *arg)
956956
{
957957
if (field->flags & NO_DEFAULT_VALUE_FLAG)
958958
return 0;
959-
if ((field->default_value && field->default_value->flags)
960-
|| field->has_insert_default_function() || field->vcol_info)
959+
if ((field->default_value && field->default_value->flags) || field->vcol_info)
961960
{
962961
Field *org_field= (Field*) arg;
963962
if (field == org_field ||
@@ -8258,7 +8257,7 @@ void Item_default_value::print(String *str, enum_query_type query_type)
82588257

82598258
void Item_default_value::calculate()
82608259
{
8261-
if (field->default_value || field->has_insert_default_function())
8260+
if (field->default_value)
82628261
field->set_default();
82638262
}
82648263

sql/sql_insert.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,8 +2489,8 @@ TABLE *Delayed_insert::get_local_table(THD* client_thd)
24892489
(*field)->default_value= vcol;
24902490
*dfield_ptr++= *field;
24912491
}
2492-
if ((*field)->has_insert_default_function() ||
2493-
(*field)->has_update_default_function())
2492+
else
2493+
if ((*field)->has_update_default_function())
24942494
*dfield_ptr++= *field;
24952495
}
24962496
if (vfield)

sql/sql_show.cc

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,20 +1634,11 @@ static bool get_field_default_value(THD *thd, Field *field, String *def_value,
16341634
bool quoted)
16351635
{
16361636
bool has_default;
1637-
bool has_now_default;
16381637
enum enum_field_types field_type= field->type();
16391638

1640-
/*
1641-
We are using CURRENT_TIMESTAMP instead of NOW because it is
1642-
more standard
1643-
*/
1644-
has_now_default= field->has_insert_default_function();
1645-
16461639
has_default= (field->default_value ||
16471640
(!(field->flags & NO_DEFAULT_VALUE_FLAG) &&
1648-
field->unireg_check != Field::NEXT_NUMBER &&
1649-
!((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
1650-
&& has_now_default)));
1641+
field->unireg_check != Field::NEXT_NUMBER));
16511642

16521643
def_value->length(0);
16531644
if (has_default)
@@ -1662,17 +1653,14 @@ static bool get_field_default_value(THD *thd, Field *field, String *def_value,
16621653
field->default_value->expr_str.length);
16631654
def_value->append(')');
16641655
}
1656+
else if (field->unireg_check)
1657+
def_value->append(field->default_value->expr_str.str,
1658+
field->default_value->expr_str.length);
16651659
else
16661660
def_value->set(field->default_value->expr_str.str,
16671661
field->default_value->expr_str.length,
16681662
&my_charset_utf8mb4_general_ci);
16691663
}
1670-
else if (has_now_default)
1671-
{
1672-
def_value->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
1673-
if (field->decimals() > 0)
1674-
def_value->append_parenthesized(field->decimals());
1675-
}
16761664
else if (!field->is_null())
16771665
{ // Not null by default
16781666
char tmp[MAX_FIELD_WIDTH];
@@ -1704,13 +1692,13 @@ static bool get_field_default_value(THD *thd, Field *field, String *def_value,
17041692
if (quoted)
17051693
append_unescaped(def_value, def_val.ptr(), def_val.length());
17061694
else
1707-
def_value->append(def_val.ptr(), def_val.length());
1695+
def_value->move(def_val);
17081696
}
17091697
else if (quoted)
1710-
def_value->append(STRING_WITH_LEN("''"));
1698+
def_value->set(STRING_WITH_LEN("''"), system_charset_info);
17111699
}
17121700
else if (field->maybe_null() && quoted)
1713-
def_value->append(STRING_WITH_LEN("NULL")); // Null as default
1701+
def_value->set(STRING_WITH_LEN("NULL"), system_charset_info); // Null as default
17141702
else
17151703
return 0;
17161704

@@ -1797,8 +1785,8 @@ int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
17971785
List<Item> field_list;
17981786
char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], def_value_buf[MAX_FIELD_WIDTH];
17991787
const char *alias;
1800-
String type(tmp, sizeof(tmp), system_charset_info);
1801-
String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
1788+
String type;
1789+
String def_value;
18021790
Field **ptr,*field;
18031791
uint primary_key;
18041792
KEY *key_info;
@@ -1891,12 +1879,8 @@ int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
18911879
packet->append(STRING_WITH_LEN(" "));
18921880
append_identifier(thd,packet,field->field_name, strlen(field->field_name));
18931881
packet->append(' ');
1894-
// check for surprises from the previous call to Field::sql_type()
1895-
if (type.ptr() != tmp)
1896-
type.set(tmp, sizeof(tmp), system_charset_info);
1897-
else
1898-
type.set_charset(system_charset_info);
18991882

1883+
type.set(tmp, sizeof(tmp), system_charset_info);
19001884
field->sql_type(type);
19011885
packet->append(type.ptr(), type.length(), system_charset_info);
19021886

@@ -1943,6 +1927,7 @@ int show_create_table(THD *thd, TABLE_LIST *table_list, String *packet,
19431927
packet->append(STRING_WITH_LEN(" NULL"));
19441928
}
19451929

1930+
def_value.set(def_value_buf, sizeof(def_value_buf), system_charset_info);
19461931
if (get_field_default_value(thd, field, &def_value, 1))
19471932
{
19481933
packet->append(STRING_WITH_LEN(" DEFAULT "));

sql/sql_table.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9670,8 +9670,7 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to,
96709670
Old fields keep their current values, and therefore should not be
96719671
present in the set of autoupdate fields.
96729672
*/
9673-
if ((*ptr)->default_value ||
9674-
((*ptr)->has_insert_default_function()))
9673+
if ((*ptr)->default_value)
96759674
{
96769675
*(dfield_ptr++)= *ptr;
96779676
++to->s->default_fields;

0 commit comments

Comments
 (0)