summaryrefslogtreecommitdiff
path: root/sql/field.cc
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.org>2015-05-05 13:22:09 +0400
committerAlexander Barkov <bar@mariadb.org>2015-05-05 13:22:09 +0400
commit73c2356e7bc7162211622d7ea64c01c2c2bb5342 (patch)
tree41544ac0d905e86b19e11036b9f24d561b3f2757 /sql/field.cc
parentae18a28500974351cf42fa3cac67c83e0647d510 (diff)
downloadmariadb-git-73c2356e7bc7162211622d7ea64c01c2c2bb5342.tar.gz
MDEV-7778 impossible create copy of table, if table contain default value for timestamp field
MDEV-8082 ON UPDATE is not preserved by CREATE TABLE .. SELECT
Diffstat (limited to 'sql/field.cc')
-rw-r--r--sql/field.cc69
1 files changed, 43 insertions, 26 deletions
diff --git a/sql/field.cc b/sql/field.cc
index e7e046a8458..31d8b46e587 100644
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -9923,35 +9923,52 @@ Create_field::Create_field(Field *old_field,Field *orig_field)
char_length= length;
/*
- Copy the default value from the column object orig_field, if:
- 1) The column has a constant default value.
- 2) The column type is not a BLOB type.
- 3) The original column (old_field) was properly initialized with a record
- buffer pointer.
- 4) The original column doesn't have a default function to auto-initialize
- the column on INSERT
+ Copy the default (constant/function) from the column object orig_field, if
+ supplied. We do this if all these conditions are met:
+
+ - The column allows a default.
+
+ - The column type is not a BLOB type.
+
+ - The original column (old_field) was properly initialized with a record
+ buffer pointer.
*/
- if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) && // 1) 2)
- old_field->ptr && orig_field && // 3)
- !old_field->has_insert_default_function()) // 4)
- {
- char buff[MAX_FIELD_WIDTH];
- String tmp(buff,sizeof(buff), charset);
- my_ptrdiff_t diff;
-
- /* Get the value from default_values */
- diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
- orig_field->table->record[0]);
- orig_field->move_field_offset(diff); // Points now at default_values
- if (!orig_field->is_real_null())
+ if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
+ old_field->ptr != NULL &&
+ orig_field != NULL)
+ {
+ bool default_now= false;
+ if (real_type_with_now_as_default(sql_type))
{
- char buff[MAX_FIELD_WIDTH], *pos;
- String tmp(buff, sizeof(buff), charset), *res;
- res= orig_field->val_str(&tmp);
- pos= (char*) sql_strmake(res->ptr(), res->length());
- def= new Item_string(pos, res->length(), charset);
+ // The SQL type of the new field allows a function default:
+ default_now= orig_field->has_insert_default_function();
+ bool update_now= orig_field->has_update_default_function();
+
+ if (default_now && update_now)
+ unireg_check= Field::TIMESTAMP_DNUN_FIELD;
+ else if (default_now)
+ unireg_check= Field::TIMESTAMP_DN_FIELD;
+ else if (update_now)
+ unireg_check= Field::TIMESTAMP_UN_FIELD;
+ }
+ if (!default_now) // Give a constant default
+ {
+ char buff[MAX_FIELD_WIDTH];
+ String tmp(buff,sizeof(buff), charset);
+
+ /* Get the value from default_values */
+ my_ptrdiff_t diff= orig_field->table->default_values_offset();
+ orig_field->move_field_offset(diff); // Points now at default_values
+ if (!orig_field->is_real_null())
+ {
+ char buff[MAX_FIELD_WIDTH], *pos;
+ String tmp(buff, sizeof(buff), charset), *res;
+ res= orig_field->val_str(&tmp);
+ pos= (char*) sql_strmake(res->ptr(), res->length());
+ def= new Item_string(pos, res->length(), charset);
+ }
+ orig_field->move_field_offset(-diff); // Back to record[0]
}
- orig_field->move_field_offset(-diff); // Back to record[0]
}
}