Constraint.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // System.Data.Constraint.cs
  3. //
  4. // Author:
  5. // Franklin Wise <[email protected]>
  6. // Daniel Morgan
  7. // Tim Coleman ([email protected])
  8. //
  9. //
  10. // (C) Ximian, Inc. 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using System;
  36. using System.Collections;
  37. using System.ComponentModel;
  38. using System.Runtime.InteropServices;
  39. using System.Runtime.Serialization;
  40. using System.Data.Common;
  41. namespace System.Data {
  42. [Serializable]
  43. internal delegate void DelegateConstraintNameChange (object sender, string newName);
  44. [DefaultProperty ("ConstraintName")]
  45. [Serializable]
  46. [TypeConverterAttribute (typeof (ConstraintConverter))]
  47. public abstract class Constraint
  48. {
  49. internal event DelegateConstraintNameChange BeforeConstraintNameChange;
  50. //if constraintName is not set then a name is
  51. //created when it is added to
  52. //the ConstraintCollection
  53. //it can not be set to null, empty or duplicate
  54. //once it has been added to the collection
  55. private string _constraintName;
  56. private PropertyCollection _properties;
  57. private Index _index;
  58. //Used for membership checking
  59. private ConstraintCollection _constraintCollection;
  60. DataSet dataSet;
  61. protected Constraint ()
  62. {
  63. dataSet = null;
  64. _properties = new PropertyCollection();
  65. }
  66. [CLSCompliant (false)]
  67. protected internal virtual DataSet _DataSet {
  68. get { return dataSet; }
  69. }
  70. [DataCategory ("Data")]
  71. [DataSysDescription ("Indicates the name of this constraint.")]
  72. [DefaultValue ("")]
  73. public virtual string ConstraintName {
  74. get{ return _constraintName == null ? "" : _constraintName; }
  75. set{
  76. //This should only throw an exception when it
  77. //is a member of a ConstraintCollection which
  78. //means we should let the ConstraintCollection
  79. //handle exceptions when this value changes
  80. _onConstraintNameChange(value);
  81. _constraintName = value;
  82. }
  83. }
  84. [Browsable (false)]
  85. [DataCategory ("Data")]
  86. [DataSysDescription ("The collection that holds custom user information.")]
  87. public PropertyCollection ExtendedProperties {
  88. get { return _properties; }
  89. }
  90. [DataSysDescription ("Indicates the table of this constraint.")]
  91. public abstract DataTable Table {
  92. get;
  93. }
  94. internal ConstraintCollection ConstraintCollection {
  95. get{ return _constraintCollection; }
  96. set{ _constraintCollection = value; }
  97. }
  98. private void _onConstraintNameChange (string newName)
  99. {
  100. if (null != BeforeConstraintNameChange)
  101. {
  102. BeforeConstraintNameChange (this, newName);
  103. }
  104. }
  105. //call once before adding a constraint to a collection
  106. //will throw an exception to prevent the add if a rule is broken
  107. internal abstract void AddToConstraintCollectionSetup (ConstraintCollection collection);
  108. protected internal abstract bool IsConstraintViolated ();
  109. internal static void ThrowConstraintException(){
  110. throw new ConstraintException("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
  111. }
  112. internal void AssertConstraint() {
  113. if (IsConstraintViolated())
  114. ThrowConstraintException();
  115. }
  116. internal abstract void AssertConstraint(DataRow row);
  117. internal virtual void RollbackAssert (DataRow row)
  118. {
  119. }
  120. //call once before removing a constraint to a collection
  121. //can throw an exception to prevent the removal
  122. internal abstract void RemoveFromConstraintCollectionCleanup (ConstraintCollection collection);
  123. [MonoTODO]
  124. protected void CheckStateForProperty ()
  125. {
  126. throw new NotImplementedException ();
  127. }
  128. protected internal void SetDataSet (DataSet dataSet)
  129. {
  130. this.dataSet = dataSet;
  131. }
  132. internal Index Index
  133. {
  134. get {
  135. return _index;
  136. }
  137. set {
  138. if (_index != null) {
  139. _index.RemoveRef();
  140. Table.DropIndex(_index);
  141. }
  142. _index = value;
  143. if (_index != null) {
  144. _index.AddRef();
  145. }
  146. }
  147. }
  148. internal abstract bool IsColumnContained(DataColumn column);
  149. internal abstract bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow);
  150. /// <summary>
  151. /// Gets the ConstraintName, if there is one, as a string.
  152. /// </summary>
  153. public override string ToString ()
  154. {
  155. return _constraintName == null ? "" : _constraintName;
  156. }
  157. }
  158. }