Author: senga
Date: 2006-06-15 02:56:16 -0400 (Thu, 15 Jun 2006)
New Revision: 61744

Modified:
   trunk/mcs/class/System.Data/System.Data.Common/ChangeLog
   trunk/mcs/class/System.Data/System.Data.Common/Key.cs
   trunk/mcs/class/System.Data/System.Data/ChangeLog
   trunk/mcs/class/System.Data/System.Data/Constraint.cs
   trunk/mcs/class/System.Data/System.Data/DataRow.cs
   trunk/mcs/class/System.Data/System.Data/DataSet.cs
   trunk/mcs/class/System.Data/System.Data/DataTable.cs
   trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs
   trunk/mcs/class/System.Data/Test/System.Data/ChangeLog
   trunk/mcs/class/System.Data/Test/System.Data/DataRowTest2.cs
   trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs
   trunk/mcs/class/System.Data/Test/System.Data/DataViewTest_IBindingList.cs
Log:
2006-06-15  Senganal T <[EMAIL PROTECTED]>

        * Test/System.Data/DataSetTest2.cs : Test the behavior of 
EnforceConstraints 
        * Test/System.Data/DataViewTest_IBindingList.cs : Test to ensure the 
correct index is 
        used for comparision
        * Test/System.Data/DataRowTest2.cs : Test to ensure the RowErrors and 
ColumnErrors are
        filled correctly.

        * System.Data.Common/Key.cs : 
                -Equals : Check for Equality of Sort Order too.
                
        * System.Data/DataSet.cs :
                - InternalEnforceConstraints : Check if any null constraints 
                are violated. Throw ConstraintException only after all the 
errors
                are filled.
        * System.Data/DataTable.cs :
                - EnforceConstraints : Move away the logic to check for 
                NullConstraintViolation.
                - AssertNotNullConstraint : Checks for Null Constraint
                Violation.
                - EndLoadData : Do not throw any exceptions here. All the
                errors have to be found and RowErrors set before throwing the
                exception.
        * System.Data/DataRow.cs :
                - ImportRecrod : Check the values of the row for any null
                values.
                - SetColumnError : If the error for column is already set,
                do not add a new error but edit the existing error. 
                - CheckValue : Set the NullConstraintViolation flag if either
                table is being loaded or if EnforceConstraints is set to
                false.
        * System.Data/Constraint.cs : Do not throw exception when 
EnforceConstraints is
        set to false or table is being loaded.
        * System.Data/UniqueConstraint.cs : Correct the error message. Set 
ColumnErrors
        along with the RowError.



Modified: trunk/mcs/class/System.Data/System.Data/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/System.Data/ChangeLog   2006-06-15 05:16:38 UTC 
(rev 61743)
+++ trunk/mcs/class/System.Data/System.Data/ChangeLog   2006-06-15 06:56:16 UTC 
(rev 61744)
@@ -1,3 +1,30 @@
+2006-06-15  Senganal T  <[EMAIL PROTECTED]>
+
+       * DataSet.cs :
+       - InternalEnforceConstraints : Check if any null constraints 
+               are violated. Throw ConstraintException only after all the 
errors
+               are filled.
+       * DataTable.cs :
+               - EnforceConstraints : Move away the logic to check for 
+               NullConstraintViolation.
+               - AssertNotNullConstraint : Checks for Null Constraint
+               Violation.
+               - EndLoadData : Do not throw any exceptions here. All the
+               errors have to be found and RowErrors set before throwing the
+               exception.
+       * DataRow.cs :
+               - ImportRecrod : Check the values of the row for any null
+               values.
+               - SetColumnError : If the error for column is already set,
+               do not add a new error but edit the existing error. 
+               - CheckValue : Set the NullConstraintViolation flag if either
+               table is being loaded or if EnforceConstraints is set to
+               false.
+       * Constraint.cs : Do not throw exception when EnforceConstraints is
+       set to false or table is being loaded.
+       * UniqueConstraint.cs : Correct the error message. Set ColumnErrors
+       along with the RowError.
+
 2006-05-31  Gert Driesen  <[EMAIL PROTECTED]>
 
        * DataTable.cs: Renamed initInProgress to fInitInProgress. BeginInit

Modified: trunk/mcs/class/System.Data/System.Data/Constraint.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/Constraint.cs       2006-06-15 
05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/System.Data/Constraint.cs       2006-06-15 
06:56:16 UTC (rev 61744)
@@ -146,9 +146,15 @@
                {
                }
 
-               internal void AssertConstraint() {
-                       if (IsConstraintViolated())
-                               ThrowConstraintException();
+               internal void AssertConstraint() 
+               {
+                       // The order is important.. IsConstraintViolated fills 
the RowErrors if it detects
+                       // a violation
+                       if (!IsConstraintViolated())
+                               return;
+                       if (Table._duringDataLoad || (Table.DataSet != null && 
!Table.DataSet.EnforceConstraints))
+                               return;
+                       ThrowConstraintException();
                }
 
                internal abstract void AssertConstraint(DataRow row);

Modified: trunk/mcs/class/System.Data/System.Data/DataRow.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/DataRow.cs  2006-06-15 05:16:38 UTC 
(rev 61743)
+++ trunk/mcs/class/System.Data/System.Data/DataRow.cs  2006-06-15 06:56:16 UTC 
(rev 61744)
@@ -471,6 +471,8 @@
                                
column.UpdateAutoIncrementValue(column.DataContainer.GetInt64(Proposed));
                        }
 
+                       foreach (DataColumn col in Table.Columns)
+                               CheckValue (this [col], col);
                }
 
                private void CheckValue (object v, DataColumn col) 
@@ -486,11 +488,9 @@
 
                                //Constraint violations during data load is 
raise in DataTable EndLoad
                                this._nullConstraintViolation = true;
-                               if (this.Table._duringDataLoad) {
+                               if (this.Table._duringDataLoad || 
(Table.DataSet != null && !Table.DataSet.EnforceConstraints))
                                        
this.Table._nullConstraintViolationDuringDataLoad = true;
-                               }
                                _nullConstraintMessage = "Column '" + 
col.ColumnName + "' does not allow nulls.";
-                       
                        }
                }
 
@@ -1365,10 +1365,10 @@
                        if (columnIndex < 0 || columnIndex >= 
Table.Columns.Count)
                                throw new IndexOutOfRangeException ();
 
-                       while(ColumnErrors.Count < columnIndex) {
-                               ColumnErrors.Add(null);
-                       }
-                       ColumnErrors.Add(error);
+                       while (columnIndex >= ColumnErrors.Count) 
+                               ColumnErrors.Add (null);
+
+                       ColumnErrors [columnIndex] = error;
                }
 
                /// <summary>

Modified: trunk/mcs/class/System.Data/System.Data/DataSet.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/DataSet.cs  2006-06-15 05:16:38 UTC 
(rev 61743)
+++ trunk/mcs/class/System.Data/System.Data/DataSet.cs  2006-06-15 06:56:16 UTC 
(rev 61744)
@@ -217,10 +217,18 @@
                                                table.ResetIndexes();
                                }
 
+                               // TODO : Need to take care of Error handling 
and settting of RowErrors 
+                               bool constraintViolated = false;
                                foreach (DataTable table in Tables) {
                                        foreach (Constraint constraint in 
table.Constraints)
                                                constraint.AssertConstraint();
+                                       table.AssertNotNullConstraints ();
+                                       if (!constraintViolated && 
table.HasErrors)
+                                               constraintViolated = true;
                                }
+
+                               if (constraintViolated)
+                                       Constraint.ThrowConstraintException ();
                        }
                        enforceConstraints = value;
                }
@@ -234,7 +242,7 @@
                {
                        Merge (dataSet, false, MissingSchemaAction.Add);
                }
-               
+
                public void Merge (DataTable table)
                {
                        Merge (table, false, MissingSchemaAction.Add);

Modified: trunk/mcs/class/System.Data/System.Data/DataTable.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/DataTable.cs        2006-06-15 
05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/System.Data/DataTable.cs        2006-06-15 
06:56:16 UTC (rev 61744)
@@ -617,39 +617,54 @@
                internal bool EnforceConstraints {
                        get { return enforceConstraints; }
                        set {
-                               if (value != enforceConstraints) {
-                                       if (value) {
-                                               // reset indexes since they may 
be outdated
-                                               ResetIndexes();
+                               if (value == enforceConstraints)
+                                       return;
 
-                                               bool violatesConstraints = 
false;
+                               if (value) {
+                                       // reset indexes since they may be 
outdated
+                                       ResetIndexes();
 
-                                               //FIXME: use index for 
AllowDBNull
-                                               for (int i = 0; i < 
Columns.Count; i++) {
-                                                       DataColumn column = 
Columns[i];
-                                                       if 
(!column.AllowDBNull) {
-                                                               for (int j = 0; 
j < Rows.Count; j++){
-                                                                       if 
(Rows[j].IsNull(column)) {
-                                                                               
violatesConstraints = true;
-                                                                               
Rows[j].RowError = String.Format("Column '{0}' does not allow DBNull.Value.", 
column.ColumnName);
-                                                                       }
-                                                               }
-                                                       }
-                                               }
+                                       // assert all constraints
+                                       foreach (Constraint constraint in 
Constraints)
+                                               constraint.AssertConstraint();
 
-                                               if (violatesConstraints)
-                                                       
Constraint.ThrowConstraintException();
+                                       AssertNotNullConstraints ();
 
-                                               // assert all constraints
-                                               foreach (Constraint constraint 
in Constraints) {
-                                                       
constraint.AssertConstraint();
-                                               }
-                                       }
-                                       enforceConstraints = value;
+                                       if (HasErrors)
+                                               
Constraint.ThrowConstraintException ();
                                }
+                               enforceConstraints = value;
                        }
                }
 
+               internal void AssertNotNullConstraints ()
+               {
+                       if (_duringDataLoad && 
!_nullConstraintViolationDuringDataLoad)
+                               return;
+
+                       bool result = false;
+                       String errMsg;
+                       for (int j = 0; j < Rows.Count; j++) {
+                               if (!Rows [j].HasVersion 
(DataRowVersion.Default))
+                                       continue;       
+                               errMsg = String.Empty;
+                               for (int i = 0; i < Columns.Count; i++) {
+                                       DataColumn column = Columns[i];
+                                       if (column.AllowDBNull || 
!Rows[j].IsNull (column))
+                                               continue;
+                                       result = true;
+                                       errMsg =  String.Format("Column '{0}' 
does not allow DBNull.Value.",
+                                                       column.ColumnName);
+                                       Rows [j].SetColumnError (i, errMsg);
+                               }
+                               // ms.net sets the last ColumnError as RowError
+                               if (errMsg != String.Empty)
+                                       Rows [j].RowError = errMsg;
+                       }
+                       if (!result)
+                               _nullConstraintViolationDuringDataLoad = false;
+               }
+
                internal bool RowsExist(DataColumn[] columns, DataColumn[] 
relatedColumns,DataRow row)
                {
                        int curIndex = 
row.IndexFromVersion(DataRowVersion.Default);
@@ -967,21 +982,12 @@
                public void EndLoadData() 
                {
                        if (this._duringDataLoad) {
-                               if(this._nullConstraintViolationDuringDataLoad) 
{
-                                       
this._nullConstraintViolationDuringDataLoad = false;
-                                       throw new ConstraintException ("Failed 
to enable constraints. One or more rows contain values violating non-null, 
unique, or foreign-key constraints.");
-                               }
-                               
-                               if (this.dataSet !=null) {
-                                       //Getting back to previous 
EnforceConstraint state
+                               //Getting back to previous EnforceConstraint 
state
+                               if (this.dataSet != null)
                                        
this.dataSet.InternalEnforceConstraints(this.dataSetPrevEnforceConstraints,true);
-                               }
-                               else {
-                                       //Getting back to the table's previous 
EnforceConstraint state
+                               else
                                        this.EnforceConstraints = true;
-                               }
 
-                               //Returning from loading mode, raising 
exceptions as usual
                                this._duringDataLoad = false;
                        }
                }

Modified: trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs 2006-06-15 
05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs 2006-06-15 
06:56:16 UTC (rev 61744)
@@ -431,17 +431,15 @@
                                                values.Add(row[col].ToString());
                                        }
 
-                                       string columnNames = String.Join(",", 
(string[])columns.ToArray(typeof(string)));
-                                       string columnValues = String.Join(",", 
(string[])values.ToArray(typeof(string)));
+                                       string columnNames = String.Join(", ", 
(string[])columns.ToArray(typeof(string)));
+                                       string columnValues = String.Join(", ", 
(string[])values.ToArray(typeof(string)));
 
-                                       row.RowError = String.Format("Column(s) 
'{0}' are constrained to be unique.  Value(s) '{1}' are already present", 
columnNames, columnValues);
+                                       row.RowError = String.Format("Column 
'{0}' is constrained to be unique.  Value '{1}' is already present.", 
columnNames, columnValues);
+                                       for (int j=0; j < Columns.Length; ++j)
+                                               row.SetColumnError (Columns 
[j], row.RowError);
                                }
-                               // FIXME : check the exception to be thrown here
-                               // throw new ConstraintException("These columns 
don't currently have unique values");
-                               //throw new ConstraintException ("Failed to 
enable constraints. One or more rows contain values violating non-null, unique, 
or foreign-key constraints.");
                                return true;
                        }
-
                        return false;
                }
 

Modified: trunk/mcs/class/System.Data/System.Data.Common/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/System.Data.Common/ChangeLog    2006-06-15 
05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/System.Data.Common/ChangeLog    2006-06-15 
06:56:16 UTC (rev 61744)
@@ -1,3 +1,8 @@
+2006-06-15  Senganal T <[EMAIL PROTECTED]>
+
+       * Key.cs : 
+               -Equals : Check for Equality of Sort Order too.
+               
 2006-04-07  Senganal T <[EMAIL PROTECTED]>
 
        * DbDataAdapter.cs :

Modified: trunk/mcs/class/System.Data/System.Data.Common/Key.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data.Common/Key.cs       2006-06-15 
05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/System.Data.Common/Key.cs       2006-06-15 
06:56:16 UTC (rev 61744)
@@ -255,7 +255,7 @@
                        }
                        else {
                                for(int i=0; i < columns.Length; i++) {
-                                       if (Columns[i] != columns[i]) {
+                                       if (Sort [i] != 
ListSortDirection.Ascending || Columns[i] != columns[i]) {
                                                return false;
                                        }
                                }

Modified: trunk/mcs/class/System.Data/Test/System.Data/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/ChangeLog      2006-06-15 
05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/Test/System.Data/ChangeLog      2006-06-15 
06:56:16 UTC (rev 61744)
@@ -1,3 +1,11 @@
+2006-06-15  Senganal T <[EMAIL PROTECTED]>
+
+       * DataSetTest2.cs : Test the behavior of EnforceConstraints 
+       * DataViewTest_IBindingList.cs : Test to ensure the correct index is 
+       used for comparision
+       * DataRowTest2.cs : Test to ensure the RowErrors and ColumnErrors are
+       filled correctly.
+
 2006-05-31  Senganal T <[EMAIL PROTECTED]>
 
        * DataSetTest2.cs : Fixed InferXmlSchema_inferingTables5.

Modified: trunk/mcs/class/System.Data/Test/System.Data/DataRowTest2.cs
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/DataRowTest2.cs        
2006-06-15 05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/Test/System.Data/DataRowTest2.cs        
2006-06-15 06:56:16 UTC (rev 61744)
@@ -1130,6 +1130,8 @@
 
                        // error string
                        Assert.AreEqual(sColErr  ,  dr.GetColumnError(0) , 
"DRW117");
+                       dr.SetColumnError (0, "");
+                       Assert.AreEqual("",  dr.GetColumnError (0) , "DRW118");
                }
 
                [Test] public void SetColumnError_ByColumnNameError()
@@ -1319,7 +1321,42 @@
                        ds.Tables[0].EndLoadData(); //Foreign constraint 
violation
                }
 
+
                [Test]
+               public void TestRowErrors ()
+               {
+                       DataTable table = new DataTable ();
+                       DataColumn col1 = table.Columns.Add ("col1", typeof 
(int));
+                       DataColumn col2 = table.Columns.Add ("col2", typeof 
(int));
+                       DataColumn col3 = table.Columns.Add ("col3", typeof 
(int));
+
+                       col1.AllowDBNull = false;
+                       table.Constraints.Add ("uc", new DataColumn[] 
{col2,col3}, false);
+                       table.BeginLoadData ();
+                       table.Rows.Add (new object[] {null,1,1});
+                       table.Rows.Add (new object[] {1,1,1});
+                       try {
+                               table.EndLoadData ();
+                               Assert.Fail ("#0");
+                       } catch (ConstraintException) {}
+                       Assert.IsTrue (table.HasErrors, "#1");
+                       DataRow[] rows = table.GetErrors ();
+
+                       Assert.AreEqual (2, rows.Length, "#2");
+                       Assert.AreEqual ("Column 'col1' does not allow 
DBNull.Value.", table.Rows [0].RowError, "#3");
+                       Assert.AreEqual ("Column 'col2, col3' is constrained to 
be unique.  Value '1, 1' is already present."
+                                       , table.Rows [1].RowError, "#4");
+
+                       Assert.AreEqual (table.Rows [0].RowError, table.Rows 
[0].GetColumnError (0), "#5");
+                       Assert.AreEqual (table.Rows [1].RowError, table.Rows 
[0].GetColumnError (1), "#6");
+                       Assert.AreEqual (table.Rows [1].RowError, table.Rows 
[0].GetColumnError (2), "#7");
+
+                       Assert.AreEqual ("", table.Rows [1].GetColumnError (0), 
"#8");
+                       Assert.AreEqual (table.Rows [1].RowError, table.Rows 
[1].GetColumnError (1), "#9");
+                       Assert.AreEqual (table.Rows [1].RowError, table.Rows 
[1].GetColumnError (2), "#10");
+               }
+
+               [Test]
                public void BeginEdit()
                {
                        DataTable myTable = new DataTable("myTable"); 

Modified: trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs        
2006-06-15 05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs        
2006-06-15 06:56:16 UTC (rev 61744)
@@ -262,6 +262,43 @@
                        Assert.AreEqual(false, ds.EnforceConstraints  , "DS25");
                }
 
+               [Test]
+               public void EnforceConstraints_CheckPrimaryConstraint ()
+               {
+                       DataSet ds = new DataSet();
+                       ds.Tables.Add ("table");
+                       ds.Tables [0].Columns.Add ("col");
+                       ds.Tables [0].PrimaryKey = new DataColumn[] {ds.Tables 
[0].Columns [0]};
+                       ds.EnforceConstraints = false;
+                       ds.Tables [0].Rows.Add (new object[] {null});
+                       try {
+                               ds.EnforceConstraints = true;
+                               Assert.Fail ("#1");
+                       } catch (ConstraintException e) {
+                               Assert.AreEqual ("Failed to enable constraints. 
One or more rows contain values " + 
+                                               "violating non-null, unique, or 
foreign-key constraints.", e.Message, "#2");
+                       }
+               }
+
+               [Test]
+               public void EnforceConstraints_NonNullCols ()
+               {
+                       DataSet ds = new DataSet();
+                       ds.Tables.Add ("table");
+                       ds.Tables [0].Columns.Add ("col");
+                       ds.Tables [0].Columns [0].AllowDBNull = false;
+
+                       ds.EnforceConstraints = false;
+                       ds.Tables [0].Rows.Add (new object[] {null});
+                       try {
+                               ds.EnforceConstraints = true;
+                               Assert.Fail ("#1");
+                       } catch (ConstraintException e) {
+                               Assert.AreEqual ("Failed to enable constraints. 
One or more rows contain values " + 
+                                               "violating non-null, unique, or 
foreign-key constraints.", e.Message, "#2");
+                       }
+               }
+
                [Test] public void GetChanges()
                {
                        DataSet ds = new DataSet();
@@ -2568,7 +2605,6 @@
                        System.IO.StringReader sr = new 
System.IO.StringReader(a_xmlData) ;
                        System.Xml.XmlTextReader xReader = new 
System.Xml.XmlTextReader(sr) ;
                        ds.ReadXml (xReader);
-                       ds.WriteXml(a_name + System.DateTime.Now.Ticks + 
".xml");
                        Assert.AreEqual(a_expected, 
this.dataSetDescription(ds), "DS337");
                }
 

Modified: 
trunk/mcs/class/System.Data/Test/System.Data/DataViewTest_IBindingList.cs
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/DataViewTest_IBindingList.cs   
2006-06-15 05:16:38 UTC (rev 61743)
+++ trunk/mcs/class/System.Data/Test/System.Data/DataViewTest_IBindingList.cs   
2006-06-15 06:56:16 UTC (rev 61744)
@@ -175,7 +175,7 @@
                        ib.ListChanged += new ListChangedEventHandler 
(OnListChanged);
                        try {
                                args = null;
-
+                               dv.Sort = "id DESC";
                                PropertyDescriptorCollection pds = ( 
(ITypedList) dv).GetItemProperties (null);
                                PropertyDescriptor pd = pds.Find ("id", false);
                                int index = ib.Find (pd, 15);
@@ -188,5 +188,30 @@
                                ib.ListChanged -= new ListChangedEventHandler 
(OnListChanged);
                        }
                }
+
+               [Test]
+               public void TestIfCorrectIndexIsUsed ()
+               {
+                       DataTable table= new DataTable ();
+                       table.Columns.Add ("id", typeof (int));
+
+                       table.Rows.Add (new object[] {1});
+                       table.Rows.Add (new object[] {2});
+                       table.Rows.Add (new object[] {3});
+                       table.Rows.Add (new object[] {4});
+
+                       DataView dv = new DataView(table);
+
+                       dv.Sort = "[id] DESC";
+
+                       // for the new view, the index thats chosen, shud be 
different from the the one
+                       // created for the older view.
+                       dv = new DataView (table);
+                       IBindingList ib = (IBindingList) dv;
+                       PropertyDescriptorCollection pds = 
((ITypedList)dv).GetItemProperties (null);
+                       PropertyDescriptor pd = pds.Find ("id", false);
+                       int index = ib.Find (pd, 4);
+                       Assert.AreEqual (3, index, "#1");
+               }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to