ConstraintEnumerator.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConstraintEnumerator.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. // <owner current="false" primary="false">[....]</owner>
  8. //------------------------------------------------------------------------------
  9. namespace System.Data {
  10. using System;
  11. using System.Diagnostics;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. /// <devdoc>
  15. /// ConstraintEnumerator is an object for enumerating all constraints in a DataSet
  16. /// </devdoc>
  17. internal class ConstraintEnumerator {
  18. System.Collections.IEnumerator tables;
  19. System.Collections.IEnumerator constraints;
  20. Constraint currentObject;
  21. public ConstraintEnumerator(DataSet dataSet) {
  22. tables = (dataSet != null) ? dataSet.Tables.GetEnumerator() : null;
  23. currentObject = null;
  24. }
  25. public bool GetNext() {
  26. Constraint candidate;
  27. currentObject = null;
  28. while (tables != null) {
  29. if (constraints == null) {
  30. if (!tables.MoveNext()) {
  31. tables = null;
  32. return false;
  33. }
  34. constraints = ((DataTable)tables.Current).Constraints.GetEnumerator();
  35. }
  36. if (!constraints.MoveNext()) {
  37. constraints = null;
  38. continue;
  39. }
  40. Debug.Assert(constraints.Current is Constraint, "ConstraintEnumerator, contains object which is not constraint");
  41. candidate = (Constraint)constraints.Current;
  42. if (IsValidCandidate(candidate)) {
  43. currentObject = candidate;
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. public Constraint GetConstraint() {
  50. // If currentObject is null we are before first GetNext or after last GetNext--consumer is bad
  51. Debug.Assert (currentObject != null, "GetObject should never be called w/ null currentObject.");
  52. return currentObject;
  53. }
  54. protected virtual bool IsValidCandidate(Constraint constraint) {
  55. return true;
  56. }
  57. protected Constraint CurrentObject {
  58. get {
  59. return currentObject;
  60. }
  61. }
  62. }
  63. internal class ForeignKeyConstraintEnumerator : ConstraintEnumerator {
  64. public ForeignKeyConstraintEnumerator(DataSet dataSet) : base(dataSet) {
  65. }
  66. protected override bool IsValidCandidate(Constraint constraint) {
  67. return(constraint is ForeignKeyConstraint);
  68. }
  69. public ForeignKeyConstraint GetForeignKeyConstraint() {
  70. // If CurrentObject is null we are before first GetNext or after last GetNext--consumer is bad
  71. Debug.Assert (CurrentObject != null, "GetObject should never be called w/ null currentObject.");
  72. return(ForeignKeyConstraint)CurrentObject;
  73. }
  74. }
  75. internal sealed class ChildForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {
  76. // this is the table to do comparisons against
  77. DataTable table;
  78. public ChildForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
  79. this.table = inTable;
  80. }
  81. protected override bool IsValidCandidate(Constraint constraint) {
  82. return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).Table == table));
  83. }
  84. }
  85. internal sealed class ParentForeignKeyConstraintEnumerator : ForeignKeyConstraintEnumerator {
  86. // this is the table to do comparisons against
  87. DataTable table;
  88. public ParentForeignKeyConstraintEnumerator(DataSet dataSet, DataTable inTable) : base(dataSet) {
  89. this.table = inTable;
  90. }
  91. protected override bool IsValidCandidate(Constraint constraint) {
  92. return((constraint is ForeignKeyConstraint) && (((ForeignKeyConstraint)constraint).RelatedTable == table));
  93. }
  94. }
  95. }