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);
186 protected override void RemoveCore (DataRelation relation)
188 base.RemoveCore (relation);
191 protected override ArrayList List {
198 private int defaultNameIndex;
199 private bool inTransition;
202 /// Initializes a new instance of the DataRelationCollection class.
204 protected DataRelationCollection ()
207 defaultNameIndex = 1;
208 inTransition = false;
212 /// Gets the DataRelation object specified by name.
214 public abstract DataRelation this[string name]{get;}
217 /// Gets the DataRelation object at the specified index.
219 public abstract DataRelation this[int index]{get;}
223 private string GetNextDefaultRelationName ()
225 string defRelationName = "Relation";
226 for (int index = 1; Contains (defRelationName); ++index) {
227 defRelationName = "Relation" + index;
229 return defRelationName;
233 /// Adds a DataRelation to the DataRelationCollection.
235 /// <param name="relation">The DataRelation to add to the collection.</param>
237 public void Add(DataRelation relation)
239 this.AddCore (relation);
240 CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this);
242 OnCollectionChanged(e);
246 /// Creates a relation given the parameters and adds it to the collection. The name is defaulted.
247 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
248 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
249 /// The CollectionChanged event is fired if it succeeds.
251 /// <param name="parentColumn">parent column of relation.</param>
252 /// <param name="childColumn">child column of relation.</param>
253 /// <returns>The created DataRelation.</returns>
254 public virtual DataRelation Add(DataColumn parentColumn, DataColumn childColumn)
256 DataRelation dataRelation = new DataRelation(GetNextDefaultRelationName (), parentColumn, childColumn);
262 /// Creates a relation given the parameters and adds it to the collection. The name is defaulted.
263 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
264 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
265 /// The CollectionChanged event is raised if it succeeds.
267 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
268 /// <param name="childColumns">An array of child DataColumn objects.</param>
269 /// <returns>The created DataRelation.</returns>
270 public virtual DataRelation Add(DataColumn[] parentColumns, DataColumn[] childColumns)
272 DataRelation dataRelation = new DataRelation(GetNextDefaultRelationName (), parentColumns, childColumns);
278 /// Creates a relation given the parameters and adds it to the collection.
279 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
280 /// A DuplicateNameException is generated if this collection already has a relation with the same name (case insensitive).
281 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
282 /// The CollectionChanged event is raised if it succeeds.
284 /// <param name="name">The name of the relation.</param>
285 /// <param name="parentColumn">parent column of relation.</param>
286 /// <returns>The created DataRelation.</returns>
287 /// <returns></returns>
288 public virtual DataRelation Add(string name, DataColumn parentColumn, DataColumn childColumn)
290 //If no name was supplied, give it a default name.
291 if (name == null || name == "") name = GetNextDefaultRelationName ();
293 DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn);
299 /// Creates a DataRelation with the specified name, and arrays of parent and child columns, and adds it to the collection.
301 /// <param name="name">The name of the DataRelation to create.</param>
302 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
303 /// <param name="childColumns">An array of child DataColumn objects.</param>
304 /// <returns>The created DataRelation.</returns>
305 public virtual DataRelation Add(string name, DataColumn[] parentColumns, DataColumn[] childColumns)
307 //If no name was supplied, give it a default name.
308 if (name == null || name == "") name = GetNextDefaultRelationName ();
310 DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns);
316 /// Creates a relation given the parameters and adds it to the collection.
317 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
318 /// A DuplicateNameException is generated if this collection already has a relation with the same name (case insensitive).
319 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
320 /// The CollectionChanged event is raised if it succeeds.
322 /// <param name="name">The name of the relation.</param>
323 /// <param name="parentColumn">parent column of relation.</param>
324 /// <param name="childColumn">child column of relation.</param>
325 /// <param name="createConstraints">true to create constraints; otherwise false. (default is true)</param>
326 /// <returns>The created DataRelation.</returns>
327 public virtual DataRelation Add(string name, DataColumn parentColumn, DataColumn childColumn, bool createConstraints)
329 //If no name was supplied, give it a default name.
330 if (name == null || name == "") name = GetNextDefaultRelationName ();
332 DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn, createConstraints);
338 /// Creates a DataRelation with the specified name, arrays of parent and child columns,
339 /// and value specifying whether to create a constraint, and adds it to the collection.
341 /// <param name="name">The name of the DataRelation to create.</param>
342 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
343 /// <param name="childColumns">An array of child DataColumn objects.</param>
344 /// <param name="createConstraints">true to create a constraint; otherwise false.</param>
345 /// <returns>The created DataRelation.</returns>
346 public virtual DataRelation Add(string name, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
348 //If no name was supplied, give it a default name.
349 if (name == null || name == "") name = GetNextDefaultRelationName ();
351 DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns, createConstraints);
358 /// Performs verification on the table.
360 /// <param name="relation">The relation to check.</param>
362 protected virtual void AddCore(DataRelation relation)
364 if (relation == null)
366 //TODO: Issue a good exception message.
367 throw new ArgumentNullException();
369 else if(List.IndexOf(relation) != -1)
371 //TODO: Issue a good exception message.
372 throw new ArgumentException();
374 else if(List.Contains(relation.RelationName))
376 //TODO: Issue a good exception message.
377 throw new DuplicateNameException("A Relation named " + relation.RelationName + " already belongs to this DataSet.");
382 /// Copies the elements of the specified DataRelation array to the end of the collection.
384 /// <param name="relations">The array of DataRelation objects to add to the collection.</param>
385 public virtual void AddRange(DataRelation[] relations)
387 foreach (DataRelation relation in relations) Add(relation);
391 public virtual bool CanRemove(DataRelation relation)
397 public virtual void Clear()
402 public virtual bool Contains(string name)
404 return IndexOf(name) != -1;
407 private CollectionChangeEventArgs CreateCollectionChangeEvent (CollectionChangeAction action)
409 return new CollectionChangeEventArgs (action, this);
412 protected abstract DataSet GetDataSet();
414 public virtual int IndexOf(DataRelation relation)
416 return List.IndexOf(relation);
419 public virtual int IndexOf(string relationName)
421 return List.IndexOf(this[relationName]);
424 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
426 if (CollectionChanged != null)
427 CollectionChanged (this, ccevent);
431 protected internal virtual void OnCollectionChanging (CollectionChangeEventArgs ccevent)
433 throw new NotImplementedException ();
436 public void Remove (DataRelation relation)
438 RemoveCore (relation);
439 List.Remove (relation);
440 OnCollectionChanged (CreateCollectionChangeEvent (CollectionChangeAction.Remove));
443 public void Remove (string name)
445 Remove ((DataRelation) List[IndexOf (name)]);
448 public void RemoveAt (int index)
450 List.RemoveAt (index);
454 protected virtual void RemoveCore(DataRelation relation)
456 // TODO: What have to be done?
459 [ResDescriptionAttribute ("Occurs whenever this collection's membership changes.")]
460 public event CollectionChangeEventHandler CollectionChanged;