Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConstraintEnumerator.cs « Data « System « System.Data « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72c20a474333cb74994c681b85660f4f08319fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//------------------------------------------------------------------------------
// <copyright file="ConstraintEnumerator.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
// <owner current="true" primary="true">[....]</owner>
// <owner current="true" primary="false">[....]</owner>
// <owner current="false" primary="false">[....]</owner>
//------------------------------------------------------------------------------

namespace System.Data {
    using System;
    using System.Diagnostics;
    using System.Collections;
    using System.ComponentModel;

    /// <devdoc>
    /// ConstraintEnumerator is an object for enumerating all constraints in a DataSet
    /// </devdoc>
    internal class ConstraintEnumerator {

        System.Collections.IEnumerator tables;
        System.Collections.IEnumerator constraints;
        Constraint currentObject; 

        public ConstraintEnumerator(DataSet dataSet) {
            tables = (dataSet != null) ? dataSet.Tables.GetEnumerator() : null;
            currentObject = null;
        }

        public bool GetNext() {
            Constraint candidate;
            currentObject = null;
            while (tables != null) {
                if (constraints == null) {
                    if (!tables.MoveNext()) {
                        tables = null;
                        return false;
                    }
                    constraints = ((DataTable)tables.Current).Constraints.GetEnumerator();
                }

                if (!constraints.MoveNext()) {
                    constraints = null;
                    continue;
                }

                Debug.Assert(constraints.Current is Constraint, "ConstraintEnumerator, contains object which is not constraint");
                candidate = (Constraint)constraints.Current;
                if (IsValidCandidate(candidate)) {
                    currentObject = candidate;
                    return true;
                }

            }
            return false;
        }

        public Constraint GetConstraint() {
            // If currentObject is null we are before first GetNext or after last GetNext--consumer is bad
            Debug.Assert (currentObject != null, "GetObject should never be called w/ null currentObject.");
            return currentObject;   
        }

        protected virtual bool IsValidCandidate(Constraint constraint) {
            return true;
        }

        protected Constraint CurrentObject {
            get {
                return currentObject;
            }
        }

    }

    internal class ForeignKeyConstraintEnumerator : ConstraintEnumerator {

        public ForeignKeyConstraintEnumerator(DataSet dataSet) : base(dataSet) {

        }

        protected override bool IsValidCandidate(Constraint constraint) {
            return(constraint is ForeignKeyConstraint);
        }

        public ForeignKeyConstraint GetForeignKeyConstraint() {
            // If CurrentObject is null we are before first GetNext or after last GetNext--consumer is bad
            Debug.Assert (CurrentObject != null, "GetObject should never be called w/ null currentObject.");
            return(ForeignKeyConstraint)CurrentObject;   
        }
    }

    internal sealed class ChildForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {

        // this is the table to do comparisons against
        DataTable table;
        public ChildForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
            this.table = inTable;
        }

        protected override bool IsValidCandidate(Constraint constraint) {
            return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).Table == table));
        }
    }

    internal sealed class ParentForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {

        // this is the table to do comparisons against
        DataTable table;
        public ParentForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
            this.table = inTable;
        }

        protected override bool IsValidCandidate(Constraint constraint) {
            return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).RelatedTable == table));
        }
    }
}