2 // System.Data.DataRelationCollection.cs
5 // Christopher Podurgiel (cpodurgiel@msn.com)
6 // Daniel Morgan <danmorg@sc.rr.com>
7 // Tim Coleman (tim@timcoleman.com)
8 // Alan Tam Siu Lung <Tam@SiuLung.com>
10 // (C) Chris Podurgiel
11 // (C) 2002 Daniel Morgan
12 // Copyright (C) Tim Coleman, 2002
16 using System.Collections;
17 using System.ComponentModel;
19 namespace System.Data {
21 /// Represents the collection of DataRelation objects for this DataSet.
24 [DefaultEvent ("CollectionChanged")]
26 public abstract class DataRelationCollection : InternalDataCollectionBase
29 /// Summary description for DataTableRelationCollection.
31 internal class DataSetRelationCollection : DataRelationCollection
33 private DataSet dataSet;
36 /// Initializes a new instance of the DataSetRelationCollection class.
38 internal DataSetRelationCollection (DataSet dataSet)
40 this.dataSet = dataSet;
44 /// Gets the DataRelation object specified by name.
46 public override DataRelation this [string name]
49 foreach (DataRelation dataRelation in List)
50 if (dataRelation.RelationName == name) return dataRelation;
56 /// Gets the DataRelation object at the specified index.
58 public override DataRelation this [int index]
61 return List [index] as DataRelation;
65 protected override DataSet GetDataSet()
71 /// Performs verification on the table.
73 /// <param name="relation">The relation to check.</param>
74 protected override void AddCore (DataRelation relation)
76 base.AddCore (relation);
77 if (relation.ChildTable.DataSet != this.dataSet || relation.ParentTable.DataSet != this.dataSet)
78 throw new DataException ();
80 relation.SetDataSet (dataSet);
81 relation.ParentTable.ChildRelations.Add (relation);
82 relation.ChildTable.ParentRelations.Add (relation);
83 ForeignKeyConstraint foreignKeyConstraint = null;
84 if (relation.createConstraints) {
85 foreignKeyConstraint = new ForeignKeyConstraint (relation.ParentColumns, relation.ChildColumns);
86 relation.ChildTable.Constraints.Add (foreignKeyConstraint);
88 UniqueConstraint uniqueConstraint = null;
89 foreach (object o in List) {
90 if (o is UniqueConstraint) {
91 UniqueConstraint uc = (UniqueConstraint) o;
92 if (uc.Columns.Length == relation.ParentColumns.Length) {
93 bool allColumnsEqual = true;
94 for (int columnCnt = 0; columnCnt < uc.Columns.Length; ++columnCnt) {
95 if (uc.Columns[columnCnt] != relation.ParentColumns[columnCnt]) {
96 allColumnsEqual = false;
100 if (allColumnsEqual) {
101 uniqueConstraint = uc;
107 relation.SetParentKeyConstraint (uniqueConstraint);
108 relation.SetChildKeyConstraint (foreignKeyConstraint);
111 public override void AddRange (DataRelation[] relations)
113 base.AddRange (relations);
116 public override void Clear ()
121 protected override void RemoveCore (DataRelation relation)
123 base.RemoveCore (relation);
124 relation.SetDataSet (null);
125 relation.ParentTable.ChildRelations.Remove (relation);
126 relation.ChildTable.ParentRelations.Remove (relation);
127 ForeignKeyConstraint foreignKeyConstraint = null;
128 relation.SetParentKeyConstraint (null);
129 relation.SetChildKeyConstraint (null);
132 protected override ArrayList List {
140 /// Summary description for DataTableRelationCollection.
142 internal class DataTableRelationCollection : DataRelationCollection
144 private DataTable dataTable;
147 /// Initializes a new instance of the DataTableRelationCollection class.
149 internal DataTableRelationCollection (DataTable dataTable)
151 this.dataTable = dataTable;
155 /// Gets the DataRelation object specified by name.
157 public override DataRelation this [string name]
160 foreach (DataRelation dataRelation in List)
161 if (dataRelation.RelationName == name) return dataRelation;
167 /// Gets the DataRelation object at the specified index.
169 public override DataRelation this [int index]
172 return List [index] as DataRelation;
176 protected override DataSet GetDataSet()
178 return dataTable.DataSet;
181 protected override void AddCore (DataRelation relation)
183 base.AddCore (relation);
184 GetDataSet ().Relations.Add (relation);
187 protected override void RemoveCore (DataRelation relation)
189 base.RemoveCore (relation);
190 GetDataSet ().Relations.Remove (relation);
193 protected override ArrayList List {
200 private int defaultNameIndex;
201 private bool inTransition;
204 /// Initializes a new instance of the DataRelationCollection class.
206 protected DataRelationCollection ()
209 defaultNameIndex = 1;
210 inTransition = false;
214 /// Gets the DataRelation object specified by name.
216 public abstract DataRelation this[string name]{get;}
219 /// Gets the DataRelation object at the specified index.
221 public abstract DataRelation this[int index]{get;}
225 private string GetNextDefaultRelationName ()
227 string defRelationName = "Relation";
228 for (int index = 1; Contains (defRelationName); ++index) {
229 defRelationName = "Relation" + index;
231 return defRelationName;
235 /// Adds a DataRelation to the DataRelationCollection.
237 /// <param name="relation">The DataRelation to add to the collection.</param>
239 public void Add(DataRelation relation)
241 this.AddCore (relation);
242 CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this);
244 OnCollectionChanged(e);
248 /// Creates a relation given the parameters and adds it to the collection. The name is defaulted.
249 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
250 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
251 /// The CollectionChanged event is fired if it succeeds.
253 /// <param name="parentColumn">parent column of relation.</param>
254 /// <param name="childColumn">child column of relation.</param>
255 /// <returns>The created DataRelation.</returns>
256 public virtual DataRelation Add(DataColumn parentColumn, DataColumn childColumn)
258 DataRelation dataRelation = new DataRelation(GetNextDefaultRelationName (), parentColumn, childColumn);
264 /// Creates a relation given the parameters and adds it to the collection. The name is defaulted.
265 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
266 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
267 /// The CollectionChanged event is raised if it succeeds.
269 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
270 /// <param name="childColumns">An array of child DataColumn objects.</param>
271 /// <returns>The created DataRelation.</returns>
272 public virtual DataRelation Add(DataColumn[] parentColumns, DataColumn[] childColumns)
274 DataRelation dataRelation = new DataRelation(GetNextDefaultRelationName (), parentColumns, childColumns);
280 /// Creates a relation given the parameters and adds it to the collection.
281 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
282 /// A DuplicateNameException is generated if this collection already has a relation with the same name (case insensitive).
283 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
284 /// The CollectionChanged event is raised if it succeeds.
286 /// <param name="name">The name of the relation.</param>
287 /// <param name="parentColumn">parent column of relation.</param>
288 /// <returns>The created DataRelation.</returns>
289 /// <returns></returns>
290 public virtual DataRelation Add(string name, DataColumn parentColumn, DataColumn childColumn)
292 //If no name was supplied, give it a default name.
293 if (name == null || name == "") name = GetNextDefaultRelationName ();
295 DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn);
301 /// Creates a DataRelation with the specified name, and arrays of parent and child columns, and adds it to the collection.
303 /// <param name="name">The name of the DataRelation to create.</param>
304 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
305 /// <param name="childColumns">An array of child DataColumn objects.</param>
306 /// <returns>The created DataRelation.</returns>
307 public virtual DataRelation Add(string name, DataColumn[] parentColumns, DataColumn[] childColumns)
309 //If no name was supplied, give it a default name.
310 if (name == null || name == "") name = GetNextDefaultRelationName ();
312 DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns);
318 /// Creates a relation given the parameters and adds it to the collection.
319 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
320 /// A DuplicateNameException is generated if this collection already has a relation with the same name (case insensitive).
321 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
322 /// The CollectionChanged event is raised if it succeeds.
324 /// <param name="name">The name of the relation.</param>
325 /// <param name="parentColumn">parent column of relation.</param>
326 /// <param name="childColumn">child column of relation.</param>
327 /// <param name="createConstraints">true to create constraints; otherwise false. (default is true)</param>
328 /// <returns>The created DataRelation.</returns>
329 public virtual DataRelation Add(string name, DataColumn parentColumn, DataColumn childColumn, bool createConstraints)
331 //If no name was supplied, give it a default name.
332 if (name == null || name == "") name = GetNextDefaultRelationName ();
334 DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn, createConstraints);
340 /// Creates a DataRelation with the specified name, arrays of parent and child columns,
341 /// and value specifying whether to create a constraint, and adds it to the collection.
343 /// <param name="name">The name of the DataRelation to create.</param>
344 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
345 /// <param name="childColumns">An array of child DataColumn objects.</param>
346 /// <param name="createConstraints">true to create a constraint; otherwise false.</param>
347 /// <returns>The created DataRelation.</returns>
348 public virtual DataRelation Add(string name, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
350 //If no name was supplied, give it a default name.
351 if (name == null || name == "") name = GetNextDefaultRelationName ();
353 DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns, createConstraints);
360 /// Performs verification on the table.
362 /// <param name="relation">The relation to check.</param>
364 protected virtual void AddCore(DataRelation relation)
366 if (relation == null)
368 //TODO: Issue a good exception message.
369 throw new ArgumentNullException();
371 else if(List.IndexOf(relation) != -1)
373 //TODO: Issue a good exception message.
374 throw new ArgumentException();
376 else if(List.Contains(relation.RelationName))
378 //TODO: Issue a good exception message.
379 throw new DuplicateNameException("A Relation named " + relation.RelationName + " already belongs to this DataSet.");
384 /// Copies the elements of the specified DataRelation array to the end of the collection.
386 /// <param name="relations">The array of DataRelation objects to add to the collection.</param>
387 public virtual void AddRange(DataRelation[] relations)
389 foreach (DataRelation relation in relations) Add(relation);
393 public virtual bool CanRemove(DataRelation relation)
399 public virtual void Clear()
404 public virtual bool Contains(string name)
406 return IndexOf(name) != -1;
409 private CollectionChangeEventArgs CreateCollectionChangeEvent (CollectionChangeAction action)
411 return new CollectionChangeEventArgs (action, this);
414 protected abstract DataSet GetDataSet();
416 public virtual int IndexOf(DataRelation relation)
418 return List.IndexOf(relation);
421 public virtual int IndexOf(string relationName)
423 return List.IndexOf(this[relationName]);
426 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
428 if (CollectionChanged != null)
429 CollectionChanged (this, ccevent);
433 protected internal virtual void OnCollectionChanging (CollectionChangeEventArgs ccevent)
435 throw new NotImplementedException ();
438 public void Remove (DataRelation relation)
440 RemoveCore (relation);
441 List.Remove (relation);
442 OnCollectionChanged (CreateCollectionChangeEvent (CollectionChangeAction.Remove));
445 public void Remove (string name)
447 Remove ((DataRelation) List[IndexOf (name)]);
450 public void RemoveAt (int index)
452 List.RemoveAt (index);
456 protected virtual void RemoveCore(DataRelation relation)
458 // TODO: What have to be done?
461 [ResDescriptionAttribute ("Occurs whenever this collection's membership changes.")]
462 public event CollectionChangeEventHandler CollectionChanged;