2008-06-13 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Fri, 13 Jun 2008 04:57:37 +0000 (04:57 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Fri, 13 Jun 2008 04:57:37 +0000 (04:57 -0000)
* DataColumn.cs
  DefaultValue can be non-DBNull. For SqlXxx type, it becomes
  SqlXxx.Null. Also, changing DataType may change DefaultValue
  to be consistent with the new type (say, having int DefaultValue
  for new string DataType is wrong).

* XmlSchemaWriter.cs
  This should be fixed to take non-DBNull default value into
  consideration.

* DataTableTest.cs, DataSetTest.cs
  We could use default value as is, not in string form. This makes
  comparison more strict.

* DataColumnTest.cs
  Added test for non-DBNull case and changing DataType case.

svn path=/trunk/mcs/; revision=105753

mcs/class/System.Data/System.Data/ChangeLog
mcs/class/System.Data/System.Data/DataColumn.cs
mcs/class/System.Data/System.Data/XmlSchemaWriter.cs
mcs/class/System.Data/Test/System.Data/ChangeLog
mcs/class/System.Data/Test/System.Data/DataColumnTest.cs
mcs/class/System.Data/Test/System.Data/DataSetTest.cs
mcs/class/System.Data/Test/System.Data/DataTableTest.cs

index 58f6a51d62dfa40cb69b36701f5da5432b00e42a..b7d9fca5741d912688ee6629b7a092694d1c76ea 100644 (file)
@@ -1,3 +1,15 @@
+2008-06-13  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * DataColumn.cs
+         DefaultValue can be non-DBNull. For SqlXxx type, it becomes
+         SqlXxx.Null. Also, changing DataType may change DefaultValue
+         to be consistent with the new type (say, having int DefaultValue
+         for new string DataType is wrong).
+
+       * XmlSchemaWriter.cs
+         This should be fixed to take non-DBNull default value into
+         consideration.
+
 2008-06-12  Atsushi Enomoto  <atsushi@ximian.com>
 
        * DataSet.cs, DataTable.cs, XmlSchemaDataImporter.cs : constraints
index aca1ef1ec3e451bb7fa740a02084536444ca1099..d931a819dd07b8f51de610d199d37bc330182d1e 100644 (file)
@@ -90,7 +90,7 @@ namespace System.Data {
                private string _caption;\r
                private MappingType _columnMapping;\r
                private string _columnName = String.Empty;\r
-               private object _defaultValue = DBNull.Value;\r
+               private object _defaultValue = GetDefaultValueForType (null);\r
                private string _expression;\r
                private IExpression _compiledExpression;\r
                private PropertyCollection _extendedProperties = new PropertyCollection ();\r
@@ -465,6 +465,8 @@ namespace System.Data {
                                         throw new InvalidConstraintException ("Cannot change datatype, " + \r
                                                                               "when column is part of a relation");\r
                                 \r
+                               Type prevType = _dataContainer != null ? _dataContainer.Type : null; // current\r
+\r
 #if NET_2_0\r
                                if (_dataContainer != null && _dataContainer.Type == typeof (DateTime))\r
                                        _datetimeMode = DataSetDateTime.UnspecifiedLocal;\r
@@ -482,6 +484,13 @@ namespace System.Data {
                                                AutoIncrement = false;\r
                                        }\r
                                }\r
+\r
+                               if (DefaultValue != GetDefaultValueForType (prevType))\r
+                                       SetDefaultValue (DefaultValue, true);\r
+#if NET_2_0\r
+                               else\r
+                                       _defaultValue = GetDefaultValueForType (DataType);\r
+#endif\r
                        }\r
                }\r
 \r
@@ -507,9 +516,15 @@ namespace System.Data {
                                        throw new ArgumentException("Can not set default value while" +\r
                                                " AutoIncrement is true on this column.");\r
                                }\r
+                               SetDefaultValue (value, false);\r
+                       }\r
+               }\r
 \r
+               void SetDefaultValue (object value, bool forcedTypeCheck)\r
+               {\r
+                       {\r
                                object tmpObj;\r
-                               if (!this._defaultValue.Equals(value)) {                \r
+                               if (forcedTypeCheck|| !this._defaultValue.Equals(value)) {\r
                                        if (value == null) {\r
                                                tmpObj = DBNull.Value;\r
                                        }\r
@@ -517,16 +532,24 @@ namespace System.Data {
                                                tmpObj = value;\r
                                        }\r
 \r
-                                       if ((this.DataType != typeof (object))&& (tmpObj != DBNull.Value)) {\r
+                                       if (!this.DataType.IsInstanceOfType (tmpObj) && tmpObj != DBNull.Value) {\r
                                                try {\r
                                                        //Casting to the new type\r
                                                        tmpObj= Convert.ChangeType(tmpObj,this.DataType);\r
                                                }\r
                                                catch (InvalidCastException) {\r
-                                                       throw new InvalidCastException("Default Value type is not compatible with" + \r
-                                                               " column type.");\r
+                                                       string msg = String.Format ("Default Value of type '{0}' is not compatible with column type '{1}'", tmpObj.GetType (), DataType);\r
+#if NET_2_0\r
+                                                       throw new DataException(msg);\r
+#else\r
+                                                       throw new ArgumentException(msg);\r
+#endif\r
                                                }\r
                                        }\r
+#if NET_2_0\r
+                                       if (tmpObj == DBNull.Value)\r
+                                               tmpObj = GetDefaultValueForType (DataType);\r
+#endif\r
                                        _defaultValue = tmpObj;\r
                                }\r
 \r
@@ -1004,6 +1027,19 @@ namespace System.Data {
 #endif\r
                        return true;\r
                }\r
+\r
+               internal static object GetDefaultValueForType (Type type)\r
+               {\r
+#if NET_2_0\r
+                       if (type == null)\r
+                               return DBNull.Value;\r
+                       if (type.Namespace == "System.Data.SqlTypes" && type.Assembly == typeof (DataColumn).Assembly)\r
+                               // For SqlXxx types, set SqlXxx.Null instead of DBNull.Value.\r
+                               return Activator.CreateInstance (type);\r
+#endif\r
+                       return DBNull.Value;\r
+               }\r
+\r
                #endregion // Methods\r
        }\r
 }\r
index 7a04810d3295700ac3564c594c863662e48bfc28..03094ec424552abb9e507065523bb029ebf8410e 100644 (file)
@@ -678,7 +678,7 @@ namespace System.Data
                                        XmlConvert.ToString (col.AutoIncrementStep));
                        }
 
-                       if (col.DefaultValue.ToString () != String.Empty)
+                       if (!DataColumn.GetDefaultValueForType (col.DataType).Equals (col.DefaultValue))
                                w.WriteAttributeString ("default",
                                        DataSet.WriteObjectXml (col.DefaultValue));
 
@@ -792,7 +792,7 @@ namespace System.Data
 
                                if (!col.AllowDBNull)
                                        w.WriteAttributeString ("use", "required");
-                               if (col.DefaultValue.ToString () != String.Empty)
+                               if (col.DefaultValue != DataColumn.GetDefaultValueForType (col.DataType))
                                        w.WriteAttributeString ("default",
                                                DataSet.WriteObjectXml (col.DefaultValue));
 
index 4f45cf408a0fcc2a761fdfdf54a6f7deda24416e..575e5c0e7e9dc4712b828d9e1098c938fdd1ace5 100644 (file)
@@ -1,3 +1,12 @@
+2008-06-13  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * DataTableTest.cs, DataSetTest.cs
+         We could use default value as is, not in string form. This makes
+         comparison more strict.
+
+       * DataColumnTest.cs
+         Added test for non-DBNull case and changing DataType case.
+
 2008-04-16  Veerapuram Varadhan <vvaradhan@novell.com>
 
        * DataSetReadXmlTest.cs: Added tests for same parent/child table
index 60310ff287eea643a7a802e3b6341f7d7b1e2fdf..1bafe09adddf9be0883749ba2746b732e4708d95 100644 (file)
@@ -38,6 +38,7 @@
 using System;
 using System.ComponentModel;
 using System.Data;
+using System.Data.SqlTypes;
 
 using NUnit.Framework;
 
@@ -325,6 +326,43 @@ namespace MonoTests.System.Data
                        Assert.AreEqual (DBNull.Value, v, "#C2");
                }
 
+               [Test]
+               public void Defaults3 ()
+               {
+                       DataColumn col = new DataColumn ("foo", typeof (SqlBoolean));
+#if NET_2_0
+                       Assert.AreEqual (SqlBoolean.Null, col.DefaultValue, "#1");
+                       col.DefaultValue = SqlBoolean.True;
+                       // FIXME: not working yet
+                       //col.DefaultValue = true;
+                       //Assert.AreEqual (SqlBoolean.True, col.DefaultValue, "#2"); // not bool but SqlBoolean
+                       col.DefaultValue = DBNull.Value;
+                       Assert.AreEqual (SqlBoolean.Null, col.DefaultValue, "#3"); // not DBNull
+#else
+                       Assert.AreEqual (DBNull.Value, col.DefaultValue, "#1");
+                       col.DefaultValue = true;
+                       Assert.AreEqual (true, col.DefaultValue, "#2");
+                       try {
+                               col.DefaultValue = DBNull.Value; // throws. DBNull is not allowed!
+                               Assert.Fail ("Should raise ArgumentException");
+                       } catch (ArgumentException) {
+                       }
+#endif
+               }
+
+               [Test]
+#if NET_2_0
+               [ExpectedException (typeof (DataException))]
+#else
+               [ExpectedException (typeof (ArgumentException))]
+#endif
+               public void ChangeTypeAfterSettingDefaultValue ()
+               {
+                       DataColumn col = new DataColumn ("foo", typeof (SqlBoolean));
+                       col.DefaultValue = true;
+                       col.DataType = typeof (int);
+               }
+
                [Test]
                public void ExpressionSubstringlimits () {
                        DataTable t = new DataTable ();
index 195468500971a2e4ba9ece9a0b9baff4040c7403..a3c6e6b6020a253a8bf74d4b4a9fe6d3155a9f17 100644 (file)
@@ -133,7 +133,11 @@ namespace MonoTests.System.Data
                        Assert.AreEqual ("Element", column2.ColumnMapping.ToString (), "test#33");
                        Assert.AreEqual ("second", column2.ColumnName, "test#34");
                        Assert.AreEqual ("System.Data.SqlTypes.SqlGuid", column2.DataType.ToString (), "test#35");
-                       Assert.AreEqual ("", column2.DefaultValue.ToString (), "test#36");
+#if NET_2_0
+                       Assert.AreEqual (SqlGuid.Null, column2.DefaultValue, "test#36");
+#else
+                       Assert.AreEqual (DBNull.Value, column2.DefaultValue, "test#36");
+#endif
                        Assert.IsFalse (column2.DesignMode, "test#37");
                        Assert.AreEqual ("", column2.Expression, "test#38");
                        Assert.AreEqual (-1, column2.MaxLength, "test#39");
index bd2285d7886713f9a487a0a1069effc323430ded..e8cac86d42efb786cf7620596d6716d5030247f4 100644 (file)
@@ -2793,7 +2793,8 @@ namespace MonoTests.System.Data
                        Assert.AreEqual ("Element", column2.ColumnMapping.ToString (), "test#33");
                        Assert.AreEqual ("second", column2.ColumnName, "test#34");
                        Assert.AreEqual ("System.Data.SqlTypes.SqlGuid", column2.DataType.ToString (), "test#35");
-                       Assert.AreEqual ("Null", column2.DefaultValue.ToString (), "test#36");
+                       Assert.AreEqual (SqlGuid.Null, column2.DefaultValue, "test#36");
+                       Assert.AreEqual (typeof (SqlGuid), column2.DefaultValue.GetType (), "test#36-2");
                        Assert.IsFalse (column2.DesignMode, "test#37");
                        Assert.AreEqual ("", column2.Expression, "test#38");
                        Assert.AreEqual (-1, column2.MaxLength, "test#39");