2004-03-10 Umadevi S (sumadevi@novell.com)
[mono.git] / mcs / class / System.Data / System.Data / DataRowCollection.cs
index f4f809f3710b2e3f1952d9504c2c73d95f5cb06f..1debc20ca4260867b88ff1a61fa4c01d64b1c999 100644 (file)
@@ -237,11 +237,15 @@ namespace System.Data
                {
                        if (pos < 0)
                                throw new IndexOutOfRangeException ("The row insert position " + pos + " is invalid.");
-                               
+                       
+                       if (row == null)
+                               throw new ArgumentNullException("row", "'row' argument cannot be null.");
+       
                        if (row.Table != this.table)
                                throw new ArgumentException ("This row already belongs to another table.");
 
-                       if (list.IndexOf(row) != -1)
+                       // If row id is not -1, we know that it is in the collection.
+                       if (row.RowID != -1)
                                throw new ArgumentException ("This row already belongs to this table.");
                        
                        if ((table.DataSet == null || table.DataSet.EnforceConstraints) && !table._duringDataLoad)
@@ -310,17 +314,46 @@ namespace System.Data
                {
                        //first check for null violations.
                        row.CheckNullConstraints();
-                       //FIXME: this validates constraints in the order they appear
-                       //in the collection. Most probably we need to do it in a 
-                       //specific order like unique/primary keys first, then Foreignkeys, etc
-                       foreach(Constraint constraint in table.Constraints)
-                       {
-                               constraint.AssertConstraint(row);
+                       // This validates constraints in the specific order : 
+                       // first unique/primary keys first, then Foreignkeys, etc
+                       ArrayList uniqueConstraintsDone = new ArrayList();
+                       ArrayList foreignKeyConstraintsDone = new ArrayList();
+                       try {
+                               foreach(Constraint constraint in table.Constraints.UniqueConstraints) {
+                                       constraint.AssertConstraint(row);
+                                       uniqueConstraintsDone.Add(constraint);
+                               }
+                       
+                               foreach(Constraint constraint in table.Constraints.ForeignKeyConstraints) {
+                                       constraint.AssertConstraint(row);
+                                       foreignKeyConstraintsDone.Add(constraint);
+                               }
+                       }
+                       // if one of the AssertConstraint failed - we need to "rollback" all the changes
+                       // caused by AssertCoinstraint calls already succeeded
+                       catch(ConstraintException e) {
+                               RollbackAsserts(row,foreignKeyConstraintsDone,uniqueConstraintsDone);
+                               throw e;
+                       }
+                       catch(InvalidConstraintException e) {   
+                               RollbackAsserts(row,foreignKeyConstraintsDone,uniqueConstraintsDone);
+                               throw e;
                        }
-
                }
-               
-       }
 
+               private void RollbackAsserts(DataRow row,ICollection foreignKeyConstraintsDone,
+                       ICollection uniqueConstraintsDone)
+               {
+                       // if any of constraints assert failed - 
+                       // we have to rollback all the asserts scceeded
+                       // on order reverse to thier original execution
+                       foreach(Constraint constraint in foreignKeyConstraintsDone) {
+                               constraint.RollbackAssert(row);
+                       }
 
+                       foreach(Constraint constraint in uniqueConstraintsDone) {
+                               constraint.RollbackAssert(row);
+                       }
+               }
+       }
 }