Commit d923ebf9 authored by unknown's avatar unknown

In TRADITIONAL mode, don't allow a NOT NULL field with no default be set to

DEFAULT (with no argument) or to the field's type's default by not being
listed in the list of fields being inserted. (Bug #5986)                    


sql/item.cc:
  Add Item_default_value::save_in_field(), with check for setting fields
  with no default value set.
sql/item.h:
  Implementation of Item_default_value::save_in_field moved to item.cc
sql/sql_insert.cc:
  Call check_that_all_fields_are_given_values() if no values were
  given and we would be filling row with all default values
mysql-test/t/strict.test:
  Add tests for setting fields to DEFAULT in traditional and regular modes
mysql-test/r/strict.result:
  Add results
parent fb78b349
...@@ -921,3 +921,40 @@ col1 col2 ...@@ -921,3 +921,40 @@ col1 col2
3 3
99 99
DROP TABLE t1; DROP TABLE t1;
SET @@sql_mode = 'traditional';
CREATE TABLE t1 (i int not null);
INSERT INTO t1 VALUES ();
ERROR HY000: Field 'i' doesn't have a default value
INSERT INTO t1 VALUES (DEFAULT);
ERROR HY000: Field 'i' doesn't have a default value
INSERT INTO t1 VALUES (DEFAULT(i));
ERROR HY000: Field 'i' doesn't have a default value
ALTER TABLE t1 ADD j int;
INSERT INTO t1 SET j = 1;
ERROR HY000: Field 'i' doesn't have a default value
INSERT INTO t1 SET j = 1, i = DEFAULT;
ERROR HY000: Field 'i' doesn't have a default value
INSERT INTO t1 SET j = 1, i = DEFAULT(i);
ERROR HY000: Field 'i' doesn't have a default value
INSERT INTO t1 VALUES (DEFAULT,1);
ERROR HY000: Field 'i' doesn't have a default value
DROP TABLE t1;
SET @@sql_mode = '';
CREATE TABLE t1 (i int not null);
INSERT INTO t1 VALUES ();
INSERT INTO t1 VALUES (DEFAULT);
Warnings:
Warning 1364 Field 'i' doesn't have a default value
INSERT INTO t1 VALUES (DEFAULT(i));
ERROR HY000: Field 'i' doesn't have a default value
ALTER TABLE t1 ADD j int;
INSERT INTO t1 SET j = 1;
INSERT INTO t1 SET j = 1, i = DEFAULT;
Warnings:
Warning 1364 Field 'i' doesn't have a default value
INSERT INTO t1 SET j = 1, i = DEFAULT(i);
ERROR HY000: Field 'i' doesn't have a default value
INSERT INTO t1 VALUES (DEFAULT,1);
Warnings:
Warning 1364 Field 'i' doesn't have a default value
DROP TABLE t1;
...@@ -638,3 +638,37 @@ INSERT IGNORE INTO t1 (col1) values (3); ...@@ -638,3 +638,37 @@ INSERT IGNORE INTO t1 (col1) values (3);
INSERT IGNORE INTO t1 () values (); INSERT IGNORE INTO t1 () values ();
SELECT * FROM t1; SELECT * FROM t1;
DROP TABLE t1; DROP TABLE t1;
# Test fields with no default value that are NOT NULL (Bug #5986)
SET @@sql_mode = 'traditional';
CREATE TABLE t1 (i int not null);
--error 1364
INSERT INTO t1 VALUES ();
--error 1364
INSERT INTO t1 VALUES (DEFAULT);
--error 1364
INSERT INTO t1 VALUES (DEFAULT(i));
ALTER TABLE t1 ADD j int;
--error 1364
INSERT INTO t1 SET j = 1;
--error 1364
INSERT INTO t1 SET j = 1, i = DEFAULT;
--error 1364
INSERT INTO t1 SET j = 1, i = DEFAULT(i);
--error 1364
INSERT INTO t1 VALUES (DEFAULT,1);
DROP TABLE t1;
SET @@sql_mode = '';
CREATE TABLE t1 (i int not null);
INSERT INTO t1 VALUES ();
INSERT INTO t1 VALUES (DEFAULT);
# DEFAULT(i) is an error even with the default sql_mode
--error 1364
INSERT INTO t1 VALUES (DEFAULT(i));
ALTER TABLE t1 ADD j int;
INSERT INTO t1 SET j = 1;
INSERT INTO t1 SET j = 1, i = DEFAULT;
--error 1364
INSERT INTO t1 SET j = 1, i = DEFAULT(i);
INSERT INTO t1 VALUES (DEFAULT,1);
DROP TABLE t1;
...@@ -3204,6 +3204,7 @@ bool Item_default_value::fix_fields(THD *thd, ...@@ -3204,6 +3204,7 @@ bool Item_default_value::fix_fields(THD *thd,
return FALSE; return FALSE;
} }
void Item_default_value::print(String *str) void Item_default_value::print(String *str)
{ {
if (!arg) if (!arg)
...@@ -3216,6 +3217,27 @@ void Item_default_value::print(String *str) ...@@ -3216,6 +3217,27 @@ void Item_default_value::print(String *str)
str->append(')'); str->append(')');
} }
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
{
if (!arg)
{
if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
{
push_warning_printf(field_arg->table->in_use,
MYSQL_ERROR::WARN_LEVEL_WARN,
ER_NO_DEFAULT_FOR_FIELD,
ER(ER_NO_DEFAULT_FOR_FIELD),
field_arg->field_name);
return 1;
}
field_arg->set_default();
return 0;
}
return Item_field::save_in_field(field_arg, no_conversions);
}
bool Item_insert_value::eq(const Item *item, bool binary_cmp) const bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
{ {
return item->type() == INSERT_VALUE_ITEM && return item->type() == INSERT_VALUE_ITEM &&
......
...@@ -1316,15 +1316,7 @@ class Item_default_value : public Item_field ...@@ -1316,15 +1316,7 @@ class Item_default_value : public Item_field
bool eq(const Item *item, bool binary_cmp) const; bool eq(const Item *item, bool binary_cmp) const;
bool fix_fields(THD *, struct st_table_list *, Item **); bool fix_fields(THD *, struct st_table_list *, Item **);
void print(String *str); void print(String *str);
int save_in_field(Field *field_arg, bool no_conversions) int save_in_field(Field *field_arg, bool no_conversions);
{
if (!arg)
{
field_arg->set_default();
return 0;
}
return Item_field::save_in_field(field_arg, no_conversions);
}
table_map used_tables() const { return (table_map)0L; } table_map used_tables() const { return (table_map)0L; }
bool walk(Item_processor processor, byte *args) bool walk(Item_processor processor, byte *args)
......
...@@ -311,7 +311,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, ...@@ -311,7 +311,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
(MODE_STRICT_TRANS_TABLES | (MODE_STRICT_TRANS_TABLES |
MODE_STRICT_ALL_TABLES))); MODE_STRICT_ALL_TABLES)));
if (fields.elements && check_that_all_fields_are_given_values(thd, table)) if ((fields.elements || !value_count) &&
check_that_all_fields_are_given_values(thd, table))
{ {
/* thd->net.report_error is now set, which will abort the next loop */ /* thd->net.report_error is now set, which will abort the next loop */
error= 1; error= 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment