2009-10-29 Veerapuram Varadhan <vvaradhan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data / DataRow.cs
index b340c2895eb40db3c42ee889d1af9c9a5dea5648..941bc1f9097ef37a6888686e913d2dbdeb13f728 100644 (file)
@@ -8,6 +8,7 @@
 //   Ville Palo <vi64pa@koti.soon.fi>
 //   Alan Tam Siu Lung <Tam@SiuLung.com>
 //   Sureshkumar T <tsureshkumar@novell.com>
+//   Veerapuram Varadhan <vvaradhan@novell.com>
 //
 // (C) Ximian, Inc 2002
 // (C) Daniel Morgan 2002, 2003
@@ -15,7 +16,7 @@
 //
 
 //
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2009 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -67,7 +68,6 @@ namespace System.Data {
                internal int xmlRowID = 0;
                internal bool _nullConstraintViolation;
                private string _nullConstraintMessage;
-               private bool _hasParentCollection;
                private bool _inChangingEvent;
                private int _rowId;
                internal bool _rowChanged = false;
@@ -437,46 +437,40 @@ namespace System.Data {
                #endregion
 
                #region Methods
-
-               //FIXME?: Couldn't find a way to set the RowState when adding the DataRow
-               //to a Datatable so I added this method. Delete if there is a better way.
-               internal void AttachRow ()
+               // Called by DataRowCollection.Add/InsertAt
+               internal void AttachAt (int row_id, DataRowAction action)
                {
+                       _rowId = row_id;
                        if (Proposed != -1) {
                                if (Current >= 0)
                                        Table.RecordCache.DisposeRecord (Current);
                                Current = Proposed;
                                Proposed = -1;
                        }
+#if NET_2_0
+                       if ((action & (DataRowAction.ChangeCurrentAndOriginal | DataRowAction.ChangeOriginal)) != 0)
+                               Original = Current;
+#endif
                }
 
-               //FIXME?: Couldn't find a way to set the RowState when removing the DataRow
-               //from a Datatable so I added this method. Delete if there is a better way.
-               internal void DetachRow ()
+               void Detach ()
                {
-                       if (Proposed >= 0) {
+                       Table.DeleteRowFromIndexes (this);
+                       _table.Rows.RemoveInternal (this);
+
+                       if (Proposed >= 0 && Proposed != Current && Proposed != Original)
                                _table.RecordCache.DisposeRecord (Proposed);
-                               if (Proposed == Current)
-                                       Current = -1;
-                               if (Proposed == Original)
-                                       Original = -1;
-                               Proposed = -1;
-                       }
+                       Proposed = -1;
 
-                       if (Current >= 0) {
+                       if (Current >= 0 && Current != Original)
                                _table.RecordCache.DisposeRecord (Current);
-                               if (Current == Original)
-                                       Original = -1;
-                               Current = -1;
-                       }
+                       Current = -1;
 
-                       if (Original >= 0) {
+                       if (Original >= 0)
                                _table.RecordCache.DisposeRecord (Original);
-                               Original = -1;
-                       }
+                       Original = -1;
 
                        _rowId = -1;
-                       _hasParentCollection = false;
                }
 
                internal void ImportRecord (int record)
@@ -500,7 +494,7 @@ namespace System.Data {
 
                private void CheckValue (object v, DataColumn col, bool doROCheck)
                {
-                       if (doROCheck && _hasParentCollection && col.ReadOnly)
+                       if (doROCheck && _rowId != -1 && col.ReadOnly)
                                throw new ReadOnlyException ();
 
                        if (v == null || v == DBNull.Value) {
@@ -610,8 +604,6 @@ namespace System.Data {
                        _table.ChangingDataRow (this, DataRowAction.Commit);
                        CheckChildRows (DataRowAction.Commit);
                        switch (RowState) {
-                       case DataRowState.Unchanged:
-                               return;
                        case DataRowState.Added:
                        case DataRowState.Modified:
                                if (Original >= 0)
@@ -619,9 +611,7 @@ namespace System.Data {
                                Original = Current;
                                break;
                        case DataRowState.Deleted:
-                               Table.DeleteRowFromIndexes (this);
-                               _table.Rows.RemoveInternal (this);
-                               DetachRow ();
+                               Detach ();
                                break;
                        case DataRowState.Detached:
                                throw new RowNotInTableException("Cannot perform this operation on a row not in the table.");
@@ -693,13 +683,8 @@ namespace System.Data {
                        _table.DeletingDataRow (this, DataRowAction.Delete);
                        switch (RowState) {
                        case DataRowState.Added:
-                               // check what to do with child rows
                                CheckChildRows (DataRowAction.Delete);
-                               _table.DeleteRowFromIndexes (this);
-                               Table.Rows.RemoveInternal (this);
-
-                               // if row was in Added state we move it to Detached.
-                               DetachRow ();
+                               Detach ();
                                break;
                        case DataRowState.Deleted:
                        case DataRowState.Detached:
@@ -854,6 +839,7 @@ namespace System.Data {
                        Current = Proposed;
                        Proposed = -1;
 
+
                        //FIXME : ideally  indexes shouldnt be maintained during dataload.But this needs to
                        //be implemented at multiple places.For now, just maintain the index.
                        //if (!Table._duringDataLoad) {
@@ -863,13 +849,13 @@ namespace System.Data {
 
                        try {
                                AssertConstraints ();
-
-                               // restore previous state to let the cascade update to find the rows
+                               
+                               // restore previous state to let the cascade update to find the rows 
                                Proposed = Current;
-                               Current = oldRecord;
-
+                               Current = oldRecord; 
+                               
                                CheckChildRows (DataRowAction.Change);
-
+                               
                                // apply new state
                                Current = Proposed;
                                Proposed = -1;
@@ -1294,15 +1280,17 @@ namespace System.Data {
                        // If original is null, then nothing has happened since AcceptChanges
                        // was last called.  We have no "original" to go back to.
 
-                       _table.ChangedDataRow (this, DataRowAction.Rollback);
-                       CancelEdit ();
+                       // Varadhan: Following if may be un-necessary
+                       /*if (_inChangingEvent) {
+                               _table.ChangedDataRow (this, DataRowAction.Rollback);
+                               CancelEdit ();
+                       }*/
 
+                       Table.ChangingDataRow (this, DataRowAction.Rollback);
                        //TODO : Need to Verify the constraints..
                        switch (RowState) {
                        case DataRowState.Added:
-                               _table.DeleteRowFromIndexes (this);
-                               _table.Rows.RemoveInternal (this);
-                               DetachRow ();
+                               Detach ();
                                break;
                        case DataRowState.Modified:
                                int current = Current;
@@ -1319,6 +1307,7 @@ namespace System.Data {
                                this.Validate ();
                                break;
                        }
+                       Table.ChangedDataRow (this, DataRowAction.Rollback);
                }
 
                /// <summary>
@@ -1577,11 +1566,6 @@ namespace System.Data {
                        return false;
                }
 
-               internal bool HasParentCollection {
-                       get { return _hasParentCollection; }
-                       set { _hasParentCollection = value; }
-               }
-
                internal void Validate ()
                {
                        Table.AddRowToIndexes (this);