summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunknown <jimw@mysql.com>2005-01-13 18:23:34 -0800
committerunknown <jimw@mysql.com>2005-01-13 18:23:34 -0800
commitd923ebf9fff81840b52e715aadc14d0d377e935d (patch)
treee4c435a8dda482a7c10a95252cb154e42eaa4a06
parentfb78b3495dc8e0867a431562cae9ea8d1530ef61 (diff)
downloadmariadb-git-d923ebf9fff81840b52e715aadc14d0d377e935d.tar.gz
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
-rw-r--r--mysql-test/r/strict.result37
-rw-r--r--mysql-test/t/strict.test34
-rw-r--r--sql/item.cc22
-rw-r--r--sql/item.h10
-rw-r--r--sql/sql_insert.cc3
5 files changed, 96 insertions, 10 deletions
diff --git a/mysql-test/r/strict.result b/mysql-test/r/strict.result
index fb228d37da3..45f7caa0b11 100644
--- a/mysql-test/r/strict.result
+++ b/mysql-test/r/strict.result
@@ -921,3 +921,40 @@ col1 col2
3
99
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;
diff --git a/mysql-test/t/strict.test b/mysql-test/t/strict.test
index a0cfc0c60f4..ba8fafe3fdd 100644
--- a/mysql-test/t/strict.test
+++ b/mysql-test/t/strict.test
@@ -638,3 +638,37 @@ INSERT IGNORE INTO t1 (col1) values (3);
INSERT IGNORE INTO t1 () values ();
SELECT * FROM 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;
diff --git a/sql/item.cc b/sql/item.cc
index c84496f8eb7..a82037c3762 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -3204,6 +3204,7 @@ bool Item_default_value::fix_fields(THD *thd,
return FALSE;
}
+
void Item_default_value::print(String *str)
{
if (!arg)
@@ -3216,6 +3217,27 @@ void Item_default_value::print(String *str)
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
{
return item->type() == INSERT_VALUE_ITEM &&
diff --git a/sql/item.h b/sql/item.h
index 2503f137355..b4386edeabc 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -1316,15 +1316,7 @@ public:
bool eq(const Item *item, bool binary_cmp) const;
bool fix_fields(THD *, struct st_table_list *, Item **);
void print(String *str);
- 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);
- }
+ int save_in_field(Field *field_arg, bool no_conversions);
table_map used_tables() const { return (table_map)0L; }
bool walk(Item_processor processor, byte *args)
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index ceb31f76953..d93010d9915 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -311,7 +311,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
(MODE_STRICT_TRANS_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 */
error= 1;