1 //------------------------------------------------------------------------------
2 // <copyright file="ConstraintEnumerator.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
10 namespace System.Data {
12 using System.Diagnostics;
13 using System.Collections;
14 using System.ComponentModel;
17 /// ConstraintEnumerator is an object for enumerating all constraints in a DataSet
19 internal class ConstraintEnumerator {
21 System.Collections.IEnumerator tables;
22 System.Collections.IEnumerator constraints;
23 Constraint currentObject;
25 public ConstraintEnumerator(DataSet dataSet) {
26 tables = (dataSet != null) ? dataSet.Tables.GetEnumerator() : null;
30 public bool GetNext() {
33 while (tables != null) {
34 if (constraints == null) {
35 if (!tables.MoveNext()) {
39 constraints = ((DataTable)tables.Current).Constraints.GetEnumerator();
42 if (!constraints.MoveNext()) {
47 Debug.Assert(constraints.Current is Constraint, "ConstraintEnumerator, contains object which is not constraint");
48 candidate = (Constraint)constraints.Current;
49 if (IsValidCandidate(candidate)) {
50 currentObject = candidate;
58 public Constraint GetConstraint() {
59 // If currentObject is null we are before first GetNext or after last GetNext--consumer is bad
60 Debug.Assert (currentObject != null, "GetObject should never be called w/ null currentObject.");
64 protected virtual bool IsValidCandidate(Constraint constraint) {
68 protected Constraint CurrentObject {
76 internal class ForeignKeyConstraintEnumerator : ConstraintEnumerator {
78 public ForeignKeyConstraintEnumerator(DataSet dataSet) : base(dataSet) {
82 protected override bool IsValidCandidate(Constraint constraint) {
83 return(constraint is ForeignKeyConstraint);
86 public ForeignKeyConstraint GetForeignKeyConstraint() {
87 // If CurrentObject is null we are before first GetNext or after last GetNext--consumer is bad
88 Debug.Assert (CurrentObject != null, "GetObject should never be called w/ null currentObject.");
89 return(ForeignKeyConstraint)CurrentObject;
93 internal sealed class ChildForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {
95 // this is the table to do comparisons against
97 public ChildForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
101 protected override bool IsValidCandidate(Constraint constraint) {
102 return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).Table == table));
106 internal sealed class ParentForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {
108 // this is the table to do comparisons against
110 public ParentForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
111 this.table = inTable;
114 protected override bool IsValidCandidate(Constraint constraint) {
115 return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).RelatedTable == table));