Constraint.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //------------------------------------------------------------------------------
  2. // <copyright file="Constraint.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.ComponentModel;
  12. using System.Globalization;
  13. /// <devdoc>
  14. /// <para>Represents a constraint that can be enforced on one or
  15. /// more <see cref='System.Data.DataColumn'/> objects.</para>
  16. /// </devdoc>
  17. [
  18. DefaultProperty("ConstraintName"),
  19. TypeConverter(typeof(ConstraintConverter))
  20. ]
  21. public abstract class Constraint {
  22. internal String name = "";
  23. private String _schemaName = "";
  24. private bool inCollection = false;
  25. private DataSet dataSet = null;
  26. internal PropertyCollection extendedProperties = null;
  27. /// <devdoc>
  28. /// <para>The name of this constraint within the <see cref='System.Data.ConstraintCollection'/>
  29. /// .</para>
  30. /// </devdoc>
  31. [
  32. DefaultValue(""),
  33. ResDescriptionAttribute(Res.ConstraintNameDescr),
  34. ResCategoryAttribute(Res.DataCategory_Data)
  35. ]
  36. public virtual string ConstraintName {
  37. get {
  38. return name;
  39. }
  40. set {
  41. if (value == null)
  42. value = "";
  43. if (Common.ADP.IsEmpty(value) && (Table != null) && InCollection)
  44. throw ExceptionBuilder.NoConstraintName();
  45. CultureInfo locale = (Table != null ? Table.Locale : CultureInfo.CurrentCulture);
  46. if (String.Compare(name, value, true, locale) != 0) {
  47. if ((Table != null) && InCollection) {
  48. Table.Constraints.RegisterName(value);
  49. if (name.Length != 0)
  50. Table.Constraints.UnregisterName(name);
  51. }
  52. name = value;
  53. }
  54. else if (String.Compare(name, value, false, locale) != 0) {
  55. name = value;
  56. }
  57. }
  58. }
  59. internal String SchemaName {
  60. get {
  61. if (Common.ADP.IsEmpty(_schemaName))
  62. return ConstraintName;
  63. else
  64. return _schemaName;
  65. }
  66. set {
  67. if (!Common.ADP.IsEmpty(value))
  68. _schemaName = value;
  69. }
  70. }
  71. internal virtual bool InCollection {
  72. get { // ACCESSOR: virtual was missing from this get
  73. return inCollection;
  74. }
  75. set {
  76. inCollection = value;
  77. if (value)
  78. dataSet = Table.DataSet;
  79. else
  80. dataSet = null;
  81. }
  82. }
  83. /// <devdoc>
  84. /// <para>Gets the <see cref='System.Data.DataTable'/> to which the constraint applies.</para>
  85. /// </devdoc>
  86. [ResDescriptionAttribute(Res.ConstraintTableDescr)]
  87. public abstract DataTable Table {
  88. get;
  89. }
  90. /// <devdoc>
  91. /// <para>Gets the collection of customized user information.</para>
  92. /// </devdoc>
  93. [
  94. ResCategoryAttribute(Res.DataCategory_Data),
  95. Browsable(false),
  96. ResDescriptionAttribute(Res.ExtendedPropertiesDescr)
  97. ]
  98. public PropertyCollection ExtendedProperties {
  99. get {
  100. if (extendedProperties == null) {
  101. extendedProperties = new PropertyCollection();
  102. }
  103. return extendedProperties;
  104. }
  105. }
  106. internal abstract bool ContainsColumn(DataColumn column);
  107. internal abstract bool CanEnableConstraint();
  108. internal abstract Constraint Clone(DataSet destination);
  109. internal abstract Constraint Clone(DataSet destination, bool ignoreNSforTableLookup);
  110. internal void CheckConstraint() {
  111. if (!CanEnableConstraint()) {
  112. throw ExceptionBuilder.ConstraintViolation(ConstraintName);
  113. }
  114. }
  115. internal abstract void CheckCanAddToCollection(ConstraintCollection constraint);
  116. internal abstract bool CanBeRemovedFromCollection(ConstraintCollection constraint, bool fThrowException);
  117. internal abstract void CheckConstraint(DataRow row, DataRowAction action);
  118. internal abstract void CheckState();
  119. protected void CheckStateForProperty() {
  120. try {
  121. CheckState();
  122. }
  123. catch (Exception e) {
  124. //
  125. if (!Common.ADP.IsCatchableExceptionType (e)) {
  126. throw;
  127. }
  128. throw ExceptionBuilder.BadObjectPropertyAccess(e.Message);
  129. }
  130. }
  131. /// <devdoc>
  132. /// <para>Gets the <see cref='System.Data.DataSet'/> to which this constraint belongs.</para>
  133. /// </devdoc>
  134. [CLSCompliant(false)]
  135. protected virtual DataSet _DataSet {
  136. get {
  137. return dataSet;
  138. }
  139. }
  140. /// <devdoc>
  141. /// <para>Sets the constraint's <see cref='System.Data.DataSet'/>.</para>
  142. /// </devdoc>
  143. protected internal void SetDataSet(DataSet dataSet) {
  144. this.dataSet = dataSet;
  145. }
  146. internal abstract bool IsConstraintViolated();
  147. public override string ToString() {
  148. return ConstraintName;
  149. }
  150. }
  151. }