Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / ConstraintEnumerator.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="ConstraintEnumerator.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
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 //------------------------------------------------------------------------------
9
10 namespace System.Data {
11     using System;
12     using System.Diagnostics;
13     using System.Collections;
14     using System.ComponentModel;
15
16     /// <devdoc>
17     /// ConstraintEnumerator is an object for enumerating all constraints in a DataSet
18     /// </devdoc>
19     internal class ConstraintEnumerator {
20
21         System.Collections.IEnumerator tables;
22         System.Collections.IEnumerator constraints;
23         Constraint currentObject; 
24
25         public ConstraintEnumerator(DataSet dataSet) {
26             tables = (dataSet != null) ? dataSet.Tables.GetEnumerator() : null;
27             currentObject = null;
28         }
29
30         public bool GetNext() {
31             Constraint candidate;
32             currentObject = null;
33             while (tables != null) {
34                 if (constraints == null) {
35                     if (!tables.MoveNext()) {
36                         tables = null;
37                         return false;
38                     }
39                     constraints = ((DataTable)tables.Current).Constraints.GetEnumerator();
40                 }
41
42                 if (!constraints.MoveNext()) {
43                     constraints = null;
44                     continue;
45                 }
46
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;
51                     return true;
52                 }
53
54             }
55             return false;
56         }
57
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.");
61             return currentObject;   
62         }
63
64         protected virtual bool IsValidCandidate(Constraint constraint) {
65             return true;
66         }
67
68         protected Constraint CurrentObject {
69             get {
70                 return currentObject;
71             }
72         }
73
74     }
75
76     internal class ForeignKeyConstraintEnumerator : ConstraintEnumerator {
77
78         public ForeignKeyConstraintEnumerator(DataSet dataSet) : base(dataSet) {
79
80         }
81
82         protected override bool IsValidCandidate(Constraint constraint) {
83             return(constraint is ForeignKeyConstraint);
84         }
85
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;   
90         }
91     }
92
93     internal sealed class ChildForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {
94
95         // this is the table to do comparisons against
96         DataTable table;
97         public ChildForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
98             this.table = inTable;
99         }
100
101         protected override bool IsValidCandidate(Constraint constraint) {
102             return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).Table == table));
103         }
104     }
105
106     internal sealed class ParentForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {
107
108         // this is the table to do comparisons against
109         DataTable table;
110         public ParentForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
111             this.table = inTable;
112         }
113
114         protected override bool IsValidCandidate(Constraint constraint) {
115             return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).RelatedTable == table));
116         }
117     }
118 }