소스 검색

2006-03-05 Senganal T <[email protected]>

* Test/System.Data/DataSetTest2.cs
* Test/System.Data/ConstraintCollectionTest2.cs
* Test/System.Data/DataViewTest.cs
* Test/System.Data/DataRelationTest.cs
Added testcases for BeginInit and EndInit methods
* System.Data/DataSet.cs :
- BeginInit (), EndInit () : Implemented methods
- InitInProgress : Added.
* System.Data/DataTable.cs
- EndInit () : Move the adding of columns and constraints
to FinishInit.
- FinishInit () : Added. Adds the Constraints and Columns
to the collection.
- InitInProgress : Added.
* System.Data/Constraint.cs
- FinishInit () : Added. Virtual method
- InitInProgress : Added. Virtual property
* System.Data/UniqueConstraint.cs
* System.Data/ForeignKeyConstraint.cs
- FinishInit () : Added.
- InitInProgress : Added.
- DataColsNotValidated : Removed.
* System.Data/ConstraintCollection.cs
- PostAddRange : Removed event
- PostEndEdit () : Renamed to PostAddRange
- Add () : Simplified the testing if constraint is initialized
- AddRange () : Simplified initializing the constraints
* System.Data/DataTableCollection.cs
- PostAddRange () : Added. Adds the tables to the collection
* System.Data/DataColumnColletion.cs
- PostEndInit () : Renamed to PostAddRange.Also, add column to
the collection only if not null.

Added/Implemented methods for design time support. Renamed some methods for consistency
in naming across classes for methods serving the same purpose.


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

Senganal T 20 년 전
부모
커밋
2e0becc2fe

+ 33 - 0
mcs/class/System.Data/System.Data/ChangeLog

@@ -1,3 +1,36 @@
+2006-03-05  Senganal T  <[email protected]>
+
+ 	* DataSet.cs :
+ 		- BeginInit (), EndInit () : Implemented methods
+ 		- InitInProgress : Added.
+ 	* DataTable.cs
+ 		- EndInit () : Move the adding of columns and constraints
+ 		to FinishInit.
+ 		- FinishInit () : Added. Adds the Constraints and Columns
+ 		to the collection.
+ 		- InitInProgress : Added.
+ 	* Constraint.cs
+ 		- FinishInit () : Added. Virtual method
+ 		- InitInProgress : Added. Virtual property
+ 	* UniqueConstraint.cs
+ 	* ForeignKeyConstraint.cs
+ 		- FinishInit () : Added.
+ 		- InitInProgress : Added.
+ 		- DataColsNotValidated : Removed.
+ 	* ConstraintCollection.cs
+ 		- PostAddRange : Removed event
+ 		- PostEndEdit () : Renamed to PostAddRange
+ 		- Add () : Simplified the testing if constraint is initialized
+ 		- AddRange () : Simplified initializing the constraints
+ 	* DataTableCollection.cs
+ 		- PostAddRange () : Added. Adds the tables to the collection
+ 	* DataColumnColletion.cs
+ 		- PostEndInit () : Renamed to PostAddRange.Also, add column to
+ 		the collection only if not null.
+ 
+ 	Added/Implemented methods for design time support. Renamed some methods for consistency
+ 	in naming across classes for methods serving the same purpose. 
+
 2006-02-24  Senganal T  <[email protected]>
 
  	* UniqueConstraint.cs :

+ 10 - 0
mcs/class/System.Data/System.Data/Constraint.cs

@@ -136,6 +136,16 @@ namespace System.Data {
 			throw new ConstraintException("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
 		}
 
+		bool initInProgress = false;
+		internal virtual bool InitInProgress {
+			get { return initInProgress; }
+			set { initInProgress = value; }
+		}
+
+		internal virtual void FinishInit (DataTable table)
+		{
+		}
+
 		internal void AssertConstraint() {
 			if (IsConstraintViolated())
 				ThrowConstraintException();

+ 45 - 80
mcs/class/System.Data/System.Data/ConstraintCollection.cs

@@ -61,11 +61,6 @@ namespace System.Data {
 		public event CollectionChangeEventHandler CollectionChanged;
 		private DataTable table;
 		
-		// Call this to set the "table" property of the UniqueConstraint class
-                // intialized with UniqueConstraint( string, string[], bool );
-                // And also validate that the named columns exist in the "table"
-                private delegate void PostAddRange( DataTable table );
-		
 		// Keep reference to most recent constraints passed to AddRange()
                 // so that they can be added when EndInit() is called.
                 private Constraint [] _mostRecentConstraints;
@@ -166,64 +161,40 @@ namespace System.Data {
 		{		
 			//not null
 			if (null == constraint) throw new ArgumentNullException("Can not add null.");
-			
+
+			if (constraint.InitInProgress)
+				throw new ArgumentException ("Hmm .. Failed to Add to collection");
+
 			//check constraint membership 
 			//can't already exist in this collection or any other
 			if (this == constraint.ConstraintCollection) 
 				throw new ArgumentException("Constraint already belongs to this collection.");
 			if (null != constraint.ConstraintCollection) 
 				throw new ArgumentException("Constraint already belongs to another collection.");
-			
+
 			//check for duplicate name
 			if (_isDuplicateConstraintName(constraint.ConstraintName,null)  )
 				throw new DuplicateNameException("Constraint name already exists.");
-	
-			// Check whether Constraint is UniqueConstraint and initailized with the special
-            // constructor - UniqueConstraint( string, string[], bool );
-            // If yes, It must be added via AddRange() only
-            // Environment.StackTrace can help us 
-			// FIXME: Is a different mechanism to do this?
-            if (constraint is UniqueConstraint){
-                if ((constraint as UniqueConstraint).DataColsNotValidated == true){
-                    if ( Environment.StackTrace.IndexOf( "AddRange" ) == -1 ){
-                        throw new ArgumentException(" Some DataColumns are invalid - They may not belong to the table associated with this Constraint Collection" );
-                    }
-                }
-            }
-
-			if (constraint is ForeignKeyConstraint){
-                if ((constraint as ForeignKeyConstraint).DataColsNotValidated == true){
-                    if ( Environment.StackTrace.IndexOf( "AddRange" ) == -1 ){
-                        throw new ArgumentException(" Some DataColumns are invalid - They may not belong to the table associated with this Constraint Collection" );
-                    }
-                }
-            }
 
 			//Allow constraint to run validation rules and setup 
-			constraint.AddToConstraintCollectionSetup(this); //may throw if it can't setup			
-
-			//Run Constraint to check existing data in table
-			// this is redundant, since AddToConstraintCollectionSetup 
-			// calls AssertConstraint right before this call
-			//constraint.AssertConstraint();
+			constraint.AddToConstraintCollectionSetup(this); //may throw if it can't setup
 
 			//if name is null or empty give it a name
 			if (constraint.ConstraintName == null || 
-				constraint.ConstraintName == "" ) 
+					constraint.ConstraintName == "" ) 
 			{ 
 				constraint.ConstraintName = _createNewConstraintName();
 			}
 
 			//Add event handler for ConstraintName change
 			constraint.BeforeConstraintNameChange += new DelegateConstraintNameChange(
-				_handleBeforeConstraintNameChange);
-			
+					_handleBeforeConstraintNameChange);
+
 			constraint.ConstraintCollection = this;
 			List.Add(constraint);
 
-			if (constraint is UniqueConstraint && ((UniqueConstraint)constraint).IsPrimaryKey) { 
+			if (constraint is UniqueConstraint && ((UniqueConstraint)constraint).IsPrimaryKey)
 				table.PrimaryKey = ((UniqueConstraint)constraint).Columns;
-			}
 
 			OnCollectionChanged( new CollectionChangeEventArgs( CollectionChangeAction.Add, this) );
 		}
@@ -237,7 +208,7 @@ namespace System.Data {
 
 			UniqueConstraint uc = new UniqueConstraint(name, column, primaryKey);
 			Add(uc);
-			 
+
 			return uc;
 		}
 
@@ -246,7 +217,7 @@ namespace System.Data {
 		virtual
 #endif
 		Constraint Add(string name, DataColumn primaryKeyColumn,
-			DataColumn foreignKeyColumn) 
+				DataColumn foreignKeyColumn) 
 		{
 			ForeignKeyConstraint fc = new ForeignKeyConstraint(name, primaryKeyColumn, 
 					foreignKeyColumn);
@@ -281,52 +252,46 @@ namespace System.Data {
 			return fc;
 		}
 
-		public void AddRange(Constraint[] constraints) {
-
+		public void AddRange(Constraint[] constraints) 
+		{
 			//When AddRange() occurs after BeginInit,
-            //it does not add any elements to the collection until EndInit is called.
-			if (this.table.fInitInProgress) {
+			//it does not add any elements to the collection until EndInit is called.
+			if (Table.InitInProgress) {
 				// Keep reference so that they can be added when EndInit() is called.
-                    _mostRecentConstraints = constraints;
-                    return;
-            }
-
-			if ( (constraints == null) || (constraints.Length == 0))
-					return;
-
-            // Check whether the constraint is UniqueConstraint
-            // And whether it was initialized with the special ctor
-            // i.e UniqueConstraint( string, string[], bool );
-            for (int i = 0; i < constraints.Length; i++){
-                if (constraints[i] is UniqueConstraint){
-                    if (( constraints[i] as UniqueConstraint).DataColsNotValidated == true){
-                            PostAddRange _postAddRange= new PostAddRange ((constraints[i] as UniqueConstraint).PostAddRange);
-                            // UniqueConstraint.PostAddRange() validates whether all named
-                            // columns exist in the table associated with this instance of
-                            // ConstraintCollection.
-                            _postAddRange (this.table);                                                                                    
-                    }
-                }
-				else if (constraints [i] is ForeignKeyConstraint){
-                        if (( constraints [i] as ForeignKeyConstraint).DataColsNotValidated == true){
-                            (constraints [i] as ForeignKeyConstraint).postAddRange (this.table);
-                        }
-					}
-                }
-                        
-                foreach (Constraint constraint in constraints)
-                        Add (constraint);
+				_mostRecentConstraints = constraints;
+				return;
+			}
+			
+			if (constraints == null)
+				return;
 
+			for (int i=0; i < constraints.Length; ++i) {
+				if (constraints [i] == null)
+					continue;
+				Add (constraints [i]);
+			}
 		}
 
 		// Helper AddRange() - Call this function when EndInit is called
-        internal void PostEndInit()
-        {
-			Constraint[] constraints = _mostRecentConstraints;
-			_mostRecentConstraints = null;
-			AddRange (constraints);
-        }
+		// keeps track of the Constraints most recently added and adds them
+		// to the collection
+		internal void PostAddRange ()
+		{
+			if (_mostRecentConstraints == null)
+				return;
 
+			// Check whether the constraint is Initialized
+			// If not, initialize before adding to collection
+			for (int i = 0; i < _mostRecentConstraints.Length; i++) {
+				Constraint c = _mostRecentConstraints [i];
+				if (c == null) 
+					continue;
+				if (c.InitInProgress)
+					c.FinishInit (Table);
+				Add (c);
+			}
+			_mostRecentConstraints = null;
+		}
 
 		public bool CanRemove(Constraint constraint) 
 		{

+ 16 - 6
mcs/class/System.Data/System.Data/DataColumnCollection.cs

@@ -336,7 +336,7 @@ namespace System.Data {
 		/// <param name="columns">The array of DataColumn objects to add to the collection.</param>
 		public void AddRange(DataColumn[] columns)
 		{
-			if (parentTable.fInitInProgress){
+			if (parentTable.InitInProgress){
 				_mostRecentColumns = columns;
 				return;
 			}
@@ -344,8 +344,9 @@ namespace System.Data {
 			if (columns == null)
 				return;
 
-			foreach (DataColumn column in columns)
-			{
+			foreach (DataColumn column in columns){
+				if (column == null)
+					continue;
 				Add(column);
 			}
 		}
@@ -365,6 +366,7 @@ namespace System.Data {
 					return String.Format (" constraint {0} on the table {1}.", 
 							c.ConstraintName, parentTable);
 			
+			
 			// check if the foreign-key constraint on any table in the dataset refers to this column.
 			// though a forignkeyconstraint automatically creates a uniquecontrainton the parent 
 			// table and would fail above, but we still need to check, as it is legal to manually remove
@@ -375,6 +377,7 @@ namespace System.Data {
 						if (c is ForeignKeyConstraint && c.IsColumnContained(column))
 							return String.Format (" constraint {0} on the table {1}.", 
 									c.ConstraintName, table.TableName);
+			
 			foreach (DataColumn col in this) 
 				if (col.CompiledExpression != null && col.CompiledExpression.DependsOn (column))
 					return  col.Expression;
@@ -581,10 +584,17 @@ namespace System.Data {
 		}
 
 		// Helper AddRange() - Call this function when EndInit is called
-		internal void PostEndInit() {
-			DataColumn[] cols = _mostRecentColumns;
+		internal void PostAddRange ()
+		{
+			if (_mostRecentColumns == null)
+				return;
+
+			foreach (DataColumn column in _mostRecentColumns){
+				if (column == null)
+					continue;
+				Add (column);
+			}
 			_mostRecentColumns = null;
-			AddRange (cols);
 		}
 
 

+ 50 - 1
mcs/class/System.Data/System.Data/DataRelation.cs

@@ -64,6 +64,14 @@ namespace System.Data
 		private PropertyCollection extendedProperties;
 		private PropertyChangedEventHandler onPropertyChangingDelegate;
 
+		string _relationName;
+		string _parentTableName;
+		string _childTableName;
+		string[] _parentColumnNames;
+		string[] _childColumnNames;
+		bool _nested;
+		bool initInProgress = false;
+	
 		#region Constructors
 
 		public DataRelation (string relationName, DataColumn parentColumn, DataColumn childColumn) 
@@ -113,9 +121,50 @@ namespace System.Data
 		[Browsable (false)]
 		public DataRelation (string relationName, string parentTableName, string childTableName, string[] parentColumnNames, string[] childColumnNames, bool nested) 
 		{
-			throw new NotImplementedException ();
+			_relationName = relationName;
+			_parentTableName = parentTableName;
+			_childTableName = childTableName;
+			_parentColumnNames = parentColumnNames;
+			_childColumnNames = childColumnNames;
+			_nested = nested;
+			InitInProgress = true;
+		}
+	
+		internal bool InitInProgress {
+			get { return initInProgress; }
+			set { initInProgress = value; }
 		}
 
+		internal void FinishInit (DataSet ds)
+		{
+			if (!ds.Tables.Contains (_parentTableName) ||
+				!ds.Tables.Contains (_childTableName))
+				throw new InvalidOperationException ();
+
+			if (_parentColumnNames.Length != _childColumnNames.Length)
+				throw new InvalidOperationException ();
+
+			DataTable parent = ds.Tables [_parentTableName];
+			DataTable child = ds.Tables [_childTableName];
+
+			parentColumns = new DataColumn [_parentColumnNames.Length];
+			childColumns = new DataColumn [_childColumnNames.Length];
+
+			for (int i=0; i < _parentColumnNames.Length; ++i) {
+				if (!parent.Columns.Contains (_parentColumnNames [i]))
+					throw new InvalidOperationException ();
+				parentColumns [i] = parent.Columns [_parentColumnNames [i]];
+				if (!child.Columns.Contains (_childColumnNames [i]))
+					throw new InvalidOperationException ();
+				childColumns [i] = child.Columns [_childColumnNames [i]];
+			}
+
+			this.RelationName = _relationName;
+			this.Nested = _nested;
+			this.createConstraints = false;
+			this.extendedProperties = new PropertyCollection ();
+			InitInProgress = false;
+		}
 #if NET_2_0
 		[MonoTODO]
 		public DataRelation (string relationName, string parentTableName, string parentTableNamespace, string childTableName, string childTableNamespace, string[] parentColumnNames, string[] childColumnNames, bool nested)

+ 40 - 6
mcs/class/System.Data/System.Data/DataRelationCollection.cs

@@ -58,6 +58,7 @@ namespace System.Data {
 		internal class DataSetRelationCollection : DataRelationCollection
 		{
 			private DataSet dataSet;
+			DataRelation[] mostRecentRelations;
 			
 			/// <summary>
 			/// Initializes a new instance of the DataSetRelationCollection class.
@@ -87,11 +88,6 @@ namespace System.Data {
 				relation.ChildTable.ParentRelations.Add (relation);                
 				relation.SetDataSet (dataSet);
 				relation.UpdateConstraints ();
-            }
-
-			public override void AddRange (DataRelation[] relations)
-			{
-				base.AddRange (relations);
 			}
 
 			protected override void RemoveCore (DataRelation relation)
@@ -104,6 +100,38 @@ namespace System.Data {
 				relation.SetChildKeyConstraint (null);
 			}
 
+			public override void AddRange (DataRelation[] relations)
+			{
+				if (relations == null)
+					return;
+
+				if (dataSet != null && dataSet.InitInProgress){
+					mostRecentRelations = relations;
+					return; 
+				}
+
+				foreach (DataRelation rel in relations){
+					if (rel == null)
+						continue;
+					Add (rel);
+				}
+			}
+
+			internal override void PostAddRange ()
+			{
+				if (mostRecentRelations == null)
+					return;
+
+				foreach (DataRelation rel in mostRecentRelations){
+					if (rel == null)
+						continue;
+					if (rel.InitInProgress)
+						rel.FinishInit (dataSet);
+					Add (rel);
+				}
+				mostRecentRelations = null;
+			}
+
 			protected override ArrayList List {
 				get {
 					return base.List;
@@ -442,7 +470,13 @@ namespace System.Data {
 		{
 			if (relations == null)
 				return;
-			foreach (DataRelation relation in relations) Add(relation);
+
+			foreach (DataRelation relation in relations)
+				Add (relation);
+		}
+
+		internal virtual void PostAddRange ()
+		{
 		}
 
 		public virtual bool CanRemove(DataRelation relation)

+ 19 - 0
mcs/class/System.Data/System.Data/DataSet.cs

@@ -75,6 +75,7 @@ namespace System.Data {
 		private CultureInfo locale = System.Threading.Thread.CurrentThread.CurrentCulture;
 		internal XmlDataDocument _xmlDataDocument = null;
 		
+		bool initInProgress = false;
 		#region Constructors
 
 		public DataSet () : this ("NewDataSet") 
@@ -1145,12 +1146,30 @@ namespace System.Data {
 		#endregion IListSource methods
 		
 		#region ISupportInitialize methods
+
+		internal bool InitInProgress {
+			get { return initInProgress; }
+			set { initInProgress = value; }
+		}
+
 		public void BeginInit ()
 		{
+			InitInProgress = true;
 		}
 		
 		public void EndInit ()
 		{
+			// Finsh the init'ing the tables only after adding all the
+			// tables to the collection.
+			Tables.PostAddRange ();
+			for (int i=0; i < Tables.Count; ++i) {
+				if (!Tables [i].InitInProgress)
+					continue;
+				Tables [i].FinishInit ();
+			}
+
+			Relations.PostAddRange ();
+			InitInProgress = false;
 		}
 		#endregion
 

+ 17 - 7
mcs/class/System.Data/System.Data/DataTable.cs

@@ -96,7 +96,7 @@ namespace System.Data {
 		private ArrayList _indexes;
 		private RecordCache _recordCache;
 		private int _defaultValuesRowIndex = -1;
-		protected internal bool fInitInProgress;
+		protected internal bool initInProgress;
 
 		// If CaseSensitive property is changed once it does not anymore follow owner DataSet's 
 		// CaseSensitive property. So when you lost you virginity it's gone for ever
@@ -499,7 +499,7 @@ namespace System.Data {
 					return;
 				}
 				
-				if (fInitInProgress) {
+				if (InitInProgress) {
 					_latestPrimaryKeyCols = value;
 					return;
 				}
@@ -728,7 +728,7 @@ namespace System.Data {
 		/// </summary>
 		public virtual void BeginInit () 
 		{
-			fInitInProgress = true;
+			InitInProgress = true;
 		}
 
 		/// <summary>
@@ -925,16 +925,26 @@ namespace System.Data {
 		[MonoTODO]
 		public virtual void EndInit () 
 		{
-			fInitInProgress = false;
+			InitInProgress = false;
+			FinishInit ();
+		}
+
+		internal bool InitInProgress {
+			get { return initInProgress; }
+			set { initInProgress = value; }
+		}
+
+		internal void FinishInit ()
+		{
 			UniqueConstraint oldPK = _primaryKeyConstraint;
 			
 			// Columns shud be added 'before' the constraints
-			Columns.PostEndInit();
+			Columns.PostAddRange ();
 
 			// Add the constraints
-			_constraintCollection.PostEndInit();
+			_constraintCollection.PostAddRange ();
 			
-			// ms.net behavior : If a PrimaryKey is added thru AddRange,
+			// ms.net behavior : If a PrimaryKey (UniqueConstraint) is added thru AddRange,
 			// then it takes precedence over an direct assignment of PrimaryKey
 			if (_primaryKeyConstraint == oldPK)
 				PrimaryKey = _latestPrimaryKeyCols;

+ 25 - 6
mcs/class/System.Data/System.Data/DataTableCollection.cs

@@ -55,7 +55,7 @@ namespace System.Data {
 	class DataTableCollection : InternalDataCollectionBase
 	{
 		DataSet dataSet;
-		
+		DataTable[] mostRecentTables;
 		#region Constructors 
 
 		internal DataTableCollection (DataSet dataSet)
@@ -169,15 +169,34 @@ namespace System.Data {
 		}
 #endif
 
-		public void AddRange (DataTable[] tables) {
+		public void AddRange (DataTable[] tables)
+		{
+			if (dataSet != null && dataSet.InitInProgress) {
+				mostRecentTables = tables;
+				return;
+			}
+
 			if (tables == null)
 				return;
 
-			for (int i = 0; i < tables.Length; i++){
-				DataTable table = tables[i];
-				if (table != null)
-					this.Add (table);
+			foreach (DataTable table in tables) {
+				if (table == null)
+					continue;
+				Add (table);
+			}
+		}
+
+		internal void PostAddRange ()
+		{
+			if (mostRecentTables == null)
+				return;
+
+			foreach (DataTable table in mostRecentTables){
+				if (table == null)
+					continue;
+				Add (table);
 			}
+			mostRecentTables = null;
 		}
 
 		public bool CanRemove (DataTable table) 

+ 66 - 79
mcs/class/System.Data/System.Data/ForeignKeyConstraint.cs

@@ -55,12 +55,12 @@ namespace System.Data {
 		private Rule _deleteRule = Rule.Cascade;
 		private Rule _updateRule = Rule.Cascade;
 		private AcceptRejectRule _acceptRejectRule = AcceptRejectRule.None;
-	    private string _parentTableName;
-        private string _childTableName;
+		private string _parentTableName;
+		private string _childTableName;
+
 		//FIXME: remove those; and use only DataColumns[]
-        private string [] _parentColumnNames;
-        private string [] _childColumnNames;
-        private bool _dataColsNotValidated = false;
+		private string [] _parentColumnNames;
+		private string [] _childColumnNames;
 			
 		#region Constructors
 
@@ -94,76 +94,72 @@ namespace System.Data {
 		{
 			_foreignKeyConstraint(constraintName, parentColumns, childColumns);
 		}
-		
+
 		//special case
 		[Browsable (false)]
 		public ForeignKeyConstraint(string constraintName, string parentTableName, string[] parentColumnNames, string[] childColumnNames, AcceptRejectRule acceptRejectRule, Rule deleteRule, Rule updateRule) 
 		{
-			_dataColsNotValidated = true;
-            base.ConstraintName = constraintName;
-                                                                                        
-            // "parentTableName" is searched in the "DataSet" to which the "DataTable"
-            // from which AddRange() is called
-            // childTable is the "DataTable" which calls AddRange()
-                                                                                        
-            // Keep reference to parentTableName to resolve later
-            _parentTableName = parentTableName;
-                                                                                        
-            // Keep reference to parentColumnNames to resolve later
-            _parentColumnNames = parentColumnNames;
-                                                                                        
-            // Keep reference to childColumnNames to resolve later
-            _childColumnNames = childColumnNames;
-                                                                                        
-            _acceptRejectRule = acceptRejectRule;
-            _deleteRule = deleteRule;
-            _updateRule = updateRule;
+			InitInProgress = true;
+			base.ConstraintName = constraintName;
+
+			// "parentTableName" is searched in the "DataSet" to which the "DataTable"
+			// from which AddRange() is called
+			// childTable is the "DataTable" which calls AddRange()
+
+			// Keep reference to parentTableName to resolve later
+			_parentTableName = parentTableName;
+
+			// Keep reference to parentColumnNames to resolve later
+			_parentColumnNames = parentColumnNames;
+
+			// Keep reference to childColumnNames to resolve later
+			_childColumnNames = childColumnNames;
 
+			_acceptRejectRule = acceptRejectRule;
+			_deleteRule = deleteRule;
+			_updateRule = updateRule;
 		}
 
-		internal void postAddRange (DataTable childTable)
-        {
-            // LAMESPEC - Does not say that this is mandatory
-            // Check whether childTable belongs to a DataSet
-            if (childTable.DataSet == null)
-                    throw new InvalidConstraintException ("ChildTable : " + childTable.TableName + " does not belong to any DataSet");
-            DataSet dataSet = childTable.DataSet;
-            _childTableName = childTable.TableName;
-            // Search for the parentTable in the childTable's DataSet
-            if (!dataSet.Tables.Contains (_parentTableName))
-                    throw new InvalidConstraintException ("Table : " + _parentTableName + "does not exist in DataSet : " + dataSet);
-                                                                                        
-            // Keep reference to parentTable
-            DataTable parentTable = dataSet.Tables [_parentTableName];
-                                                                                        
-            int i = 0, j = 0;
-                                                                                        
-            // LAMESPEC - Does not say which Exception is thrown
-            if (_parentColumnNames.Length < 0 || _childColumnNames.Length < 0)
-                    throw new InvalidConstraintException ("Neither parent nor child columns can be zero length");
-            // LAMESPEC - Does not say which Exception is thrown
-            if (_parentColumnNames.Length != _childColumnNames.Length)
-			        throw new InvalidConstraintException ("Both parent and child columns must be of same length");                                                                                                    
-            DataColumn []parentColumns = new DataColumn [_parentColumnNames.Length];
-            DataColumn []childColumns = new DataColumn [_childColumnNames.Length];
-                                                                                        
-            // Search for the parentColumns in parentTable
-            foreach (string parentCol in _parentColumnNames){
-                    if (!parentTable.Columns.Contains (parentCol))
-                            throw new InvalidConstraintException ("Table : " + _parentTableName + "does not contain the column :" + parentCol);
-                    parentColumns [i++] = parentTable. Columns [parentCol];
-            }
-            // Search for the childColumns in childTable
-            foreach (string childCol in _childColumnNames){
-                    if (!childTable.Columns.Contains (childCol))
-                            throw new InvalidConstraintException ("Table : " + _childTableName + "does not contain the column : " + childCol);
-                    childColumns [j++] = childTable.Columns [childCol];
-            }
-            _validateColumns (parentColumns, childColumns);
-            _parentColumns = parentColumns;
-            _childColumns = childColumns;
+		internal override void FinishInit (DataTable childTable)
+		{
+			if (childTable.DataSet == null)
+				throw new InvalidConstraintException ("ChildTable : " + childTable.TableName + " does not belong to any DataSet");
+
+			DataSet dataSet = childTable.DataSet;
+			_childTableName = childTable.TableName;
+
+			if (!dataSet.Tables.Contains (_parentTableName))
+				throw new InvalidConstraintException ("Table : " + _parentTableName + "does not exist in DataSet : " + dataSet);
+
+			DataTable parentTable = dataSet.Tables [_parentTableName];
+
+			int i = 0, j = 0;
+
+			if (_parentColumnNames.Length < 0 || _childColumnNames.Length < 0)
+				throw new InvalidConstraintException ("Neither parent nor child columns can be zero length");
+
+			if (_parentColumnNames.Length != _childColumnNames.Length)
+				throw new InvalidConstraintException ("Both parent and child columns must be of same length");                                                                                                    
+			DataColumn[] parentColumns = new DataColumn [_parentColumnNames.Length];
+			DataColumn[] childColumns = new DataColumn [_childColumnNames.Length];
+
+			foreach (string parentCol in _parentColumnNames){
+				if (!parentTable.Columns.Contains (parentCol))
+					throw new InvalidConstraintException ("Table : " + _parentTableName + "does not contain the column :" + parentCol);
+				parentColumns [i++] = parentTable. Columns [parentCol];
+			}
+
+			foreach (string childCol in _childColumnNames){
+				if (!childTable.Columns.Contains (childCol))
+					throw new InvalidConstraintException ("Table : " + _childTableName + "does not contain the column : " + childCol);
+				childColumns [j++] = childTable.Columns [childCol];
+			}
+			_validateColumns (parentColumns, childColumns);
+			_parentColumns = parentColumns;
+			_childColumns = childColumns;
+			InitInProgress = false;
 		}
-			
+
 #if NET_2_0
 		[MonoTODO]
 		public ForeignKeyConstraint (string constraintName, string parentTableName, string parentTableNamespace, string[] parentColumnNames, string[] childColumnNames, AcceptRejectRule acceptRejectRule, Rule deleteRule, Rule updateRule)
@@ -187,16 +183,16 @@ namespace System.Data {
 			_childColumns = childColumns;
 		}
 
-		#endregion // Constructors
+#endregion // Constructors
 
-		#region Helpers
+#region Helpers
 
 		private void _validateColumns(DataColumn[] parentColumns, DataColumn[] childColumns)
 		{
 			//not null
 			if (null == parentColumns || null == childColumns) 
 				throw new ArgumentNullException();
-			
+
 			//at least one element in each array
 			if (parentColumns.Length < 1 || childColumns.Length < 1)
 				throw new ArgumentException("Neither ParentColumns or ChildColumns can't be" +
@@ -382,13 +378,6 @@ namespace System.Data {
 			}
 		}
 
-		internal bool DataColsNotValidated
-		{
-            get { 
-				return (_dataColsNotValidated); 
-			}
-        }
-
 		internal UniqueConstraint ParentConstraint {
 			get { return _parentUniqueConstraint; }
 		}
@@ -458,8 +447,6 @@ namespace System.Data {
 			//we must have a unique constraint on the parent
 			_ensureUniqueConstraintExists(collection, _parentColumns);
 			
-			//Make sure we can create this thing
-			//AssertConstraint(); 
 			if ( (Table.DataSet != null && Table.DataSet.EnforceConstraints)
 			     || (Table.DataSet == null && Table.EnforceConstraints)) {
 				if (IsConstraintViolated())

+ 8 - 25
mcs/class/System.Data/System.Data/UniqueConstraint.cs

@@ -57,7 +57,6 @@ namespace System.Data {
 
 		//TODO:provide helpers for this case
 		private string [] _dataColumnNames; //unique case
-		private bool _dataColsNotValidated;
 		private ForeignKeyConstraint _childConstraint = null;
 
 		#region Constructors
@@ -106,21 +105,17 @@ namespace System.Data {
 		[Browsable (false)]
 		public UniqueConstraint (string name, string[] columnNames, bool isPrimaryKey) 
 		{
-			 _dataColsNotValidated = true;
-                                                                                                    
-            //keep list of names to resolve later
-            _dataColumnNames = columnNames;
-                                                                                        
-            base.ConstraintName = name;
-                                                                                        
-            _isPrimaryKey = isPrimaryKey;
+			InitInProgress = true;
 
+			//keep list of names to resolve later
+			_dataColumnNames = columnNames;
+			base.ConstraintName = name;
+			_isPrimaryKey = isPrimaryKey;
 		}
 
 		//helper ctor
 		private void _uniqueConstraint(string name, DataColumn column, bool isPrimaryKey) 
 		{
-			_dataColsNotValidated = false;
 			//validate
 			_validateColumn (column);
 
@@ -139,8 +134,6 @@ namespace System.Data {
 		//helpter ctor	
 		private void _uniqueConstraint(string name, DataColumn[] columns, bool isPrimaryKey) 
 		{
-			_dataColsNotValidated = false;
-			
 			//validate
 			_validateColumns (columns, out _dataTable);
 
@@ -265,14 +258,6 @@ namespace System.Data {
 			return null;
 		}
 
-		internal bool DataColsNotValidated 
-		{               
-			get { 
-				return (_dataColsNotValidated); 
-			}
-		 }
-
-
 		internal ForeignKeyConstraint ChildConstraint {
 			get { return _childConstraint; }
 			set { _childConstraint = value; }
@@ -282,7 +267,7 @@ namespace System.Data {
 		// Set the _dataTable property to the table to which this instance is bound when AddRange()
 		// is called with the special constructor.
 		// Validate whether the named columns exist in the _dataTable
-		internal void PostAddRange(DataTable _setTable) 
+		internal override void FinishInit (DataTable _setTable) 
 		{                
 			_dataTable = _setTable;
 			if (_isPrimaryKey == true && _setTable.PrimaryKey.Length != 0)
@@ -301,6 +286,8 @@ namespace System.Data {
 				throw(new InvalidConstraintException ("The named columns must exist in the table"));
 			}
 			_dataColumns = cols;
+			_validateColumns (cols);
+			InitInProgress = false;
 		}
 
 
@@ -404,10 +391,6 @@ namespace System.Data {
 				_dataColumns[0].SetUnique();
 			}
 					
-			//FIXME: ConstraintCollection calls AssertContraint() again rigth after calling
-			//this method, so that it is executed twice. Need to investigate which
-			// call to remove as that migth affect other parts of the classes.
-			//AssertConstraint();
 			if (IsConstraintViolated())
 				throw new ArgumentException("These columns don't currently have unique values.");
 

+ 8 - 0
mcs/class/System.Data/Test/System.Data/ChangeLog

@@ -1,3 +1,11 @@
+2006-03-05  Senganal T  <[email protected]>
+ 
+	* DataSetTest2.cs
+	* ConstraintCollectionTest2.cs
+	* DataViewTest.cs
+	* DataRelationTest.cs
+ 		Added testcases for BeginInit and EndInit methods
+
 2006-02-24  Senganal T  <[email protected]>
  
  	* ConstraintCollectionTest2.cs : added testcase for #77630

+ 1 - 1
mcs/class/System.Data/Test/System.Data/ConstraintCollectionTest2.cs

@@ -347,7 +347,7 @@ namespace MonoTests.System.Data
 			TestException( new testExceptionMethodCallback(DataProvider.TryToBreakForigenConstraint),arr);			
 		}
 
-		private void TestException(testExceptionMethodCallback dlg,IList exceptionList)
+		void TestException(testExceptionMethodCallback dlg,IList exceptionList)
 		{				
 			try {
 				dlg();

+ 2 - 3
mcs/class/System.Data/Test/System.Data/DataRelationTest.cs

@@ -340,7 +340,6 @@ namespace MonoTests.System.Data
 		}
 
 		[Test]
-		[Ignore ("Set.Relations.AddRange() fails, but why?")]
 		public void Creation4 ()
 		{
 			
@@ -362,9 +361,9 @@ namespace MonoTests.System.Data
 				AssertEquals ("test#04", typeof (NullReferenceException), e.GetType ());
 			}
 			
-			//Set.BeginInit ();
+			Set.BeginInit ();
 			Set.Relations.AddRange (new DataRelation [] {Relation});
-			//Set.EndInit ();
+			Set.EndInit ();
 			
 			DataRelation Test = null;
 			AssertEquals ("test#01", 1, Mom.ChildRelations.Count);

+ 55 - 0
mcs/class/System.Data/Test/System.Data/DataSetTest2.cs

@@ -338,6 +338,61 @@ namespace MonoTests_System.Data
 			Assert.AreEqual(arrUnchanged, ds.GetChanges(DataRowState.Unchanged).Tables[0].Rows[0].ItemArray , "DS34");
 		}
 
+		[Test] public void BeginInitTest ()
+		{
+			DataSet ds = new DataSet ();
+
+			DataTable table1 = new DataTable ("table1");
+			DataTable table2 = new DataTable ("table2");
+
+			DataColumn col1 = new DataColumn ("col1", typeof (int));
+			DataColumn col2 = new DataColumn ("col2", typeof (int));
+			table1.Columns.Add (col1);
+			table2.Columns.Add (col2);
+			
+			UniqueConstraint pkey = new UniqueConstraint ("pk", new string[] {"col1"}, true);
+			ForeignKeyConstraint fkey = new ForeignKeyConstraint ("fk", "table1", new String[] {"col1"}, 
+								new String[] {"col2"}, AcceptRejectRule.Cascade,
+								Rule.Cascade, Rule.Cascade);
+			DataRelation relation = new DataRelation ("rel", "table1", "table2", new String[] {"col1"},
+								 new String[] {"col2"}, false);
+			ds.BeginInit ();
+			table1.BeginInit ();
+			table2.BeginInit ();
+
+			ds.Tables.AddRange (new DataTable[] {table1, table2});
+			ds.Relations.AddRange (new DataRelation[] {relation});
+			
+			table1.Constraints.AddRange (new Constraint[] {pkey});
+			table2.Constraints.AddRange (new Constraint[] {fkey});
+
+			// The tables/relations shud not get added to the DataSet yet
+			Assert.AreEqual (0, ds.Tables.Count, "#1");
+			Assert.AreEqual (0, ds.Relations.Count, "#2");
+			Assert.AreEqual (0, table1.Constraints.Count, "#3");
+			Assert.AreEqual (0, table2.Constraints.Count, "#4");
+			ds.EndInit ();
+
+			Assert.AreEqual (2, ds.Tables.Count, "#5");
+			Assert.AreEqual (1, ds.Relations.Count, "#6");
+			Assert.AreEqual (1, ds.Tables [0].Constraints.Count, "#7");
+			Assert.AreEqual (1, ds.Tables [1].Constraints.Count, "#8");
+
+			// Table shud still be in BeginInit .. 
+			DataColumn col3 = new DataColumn ("col2");
+			UniqueConstraint uc = new UniqueConstraint ("uc", new string[] {"col2"}, false);
+
+			table1.Columns.AddRange (new DataColumn[] {col3});
+			table1.Constraints.AddRange (new Constraint[] {uc});
+
+			Assert.AreEqual (1, table1.Columns.Count, "#9");
+			Assert.AreEqual (1, table1.Constraints.Count, "#10");
+
+			table1.EndInit ();
+			Assert.AreEqual (2, table1.Columns.Count, "#11");
+			Assert.AreEqual (2, table1.Columns.Count, "#12");
+		}
+
 		[Test] public void GetXml()
 		{
 			DataSet ds = new DataSet();

+ 18 - 2
mcs/class/System.Data/Test/System.Data/DataViewTest.cs

@@ -356,10 +356,26 @@ namespace MonoTests.System.Data
 		}
 
 		[Test]
-		[Ignore("Test code not implemented")]
 		public void BeginInit ()
 		{
-			//TODO
+			DataTable table = new DataTable ("table");
+			DataView dv = new DataView ();
+			DataColumn col1 = new DataColumn ("col1");
+			DataColumn col2 = new DataColumn ("col2");
+			
+			dv.BeginInit ();
+			table.BeginInit ();
+			table.Columns.AddRange (new DataColumn[] {col1,col2});
+
+			dv.Table = table;
+			AssertNull ("#1", dv.Table);
+			dv.EndInit ();
+
+			AssertEquals ("#2", table, dv.Table);
+			AssertEquals ("#3", 0, table.Columns.Count);
+
+			table.EndInit ();
+			AssertEquals ("#4", 2, table.Columns.Count);
 		}
 
 		[Test]