2 // System.Data.Constraint.cs
5 // Franklin Wise <gracenote@earthlink.net>
7 // Tim Coleman (tim@timcoleman.com)
10 // (C) Ximian, Inc. 2002
11 // Copyright (C) Tim Coleman, 2002
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Collections;
39 using System.ComponentModel;
40 using System.Runtime.InteropServices;
41 using System.Runtime.Serialization;
42 using System.Data.Common;
44 namespace System.Data {
46 internal delegate void DelegateConstraintNameChange (object sender, string newName);
48 [DefaultProperty ("ConstraintName")]
49 [TypeConverterAttribute (typeof (ConstraintConverter))]
50 public abstract class Constraint {
51 static readonly object beforeConstraintNameChange = new object ();
53 EventHandlerList events = new EventHandlerList ();
55 internal event DelegateConstraintNameChange BeforeConstraintNameChange {
56 add { events.AddHandler (beforeConstraintNameChange, value); }
57 remove { events.RemoveHandler (beforeConstraintNameChange, value); }
60 //if constraintName is not set then a name is
61 //created when it is added to
62 //the ConstraintCollection
63 //it can not be set to null, empty or duplicate
64 //once it has been added to the collection
65 private string _constraintName;
66 private PropertyCollection _properties;
70 //Used for membership checking
71 private ConstraintCollection _constraintCollection;
75 protected Constraint ()
78 _properties = new PropertyCollection ();
81 [CLSCompliant (false)]
82 protected internal virtual DataSet _DataSet {
83 get { return dataSet; }
86 [DataCategory ("Data")]
88 public virtual string ConstraintName {
89 get { return _constraintName == null ? "" : _constraintName; }
91 //This should only throw an exception when it
92 //is a member of a ConstraintCollection which
93 //means we should let the ConstraintCollection
94 //handle exceptions when this value changes
95 _onConstraintNameChange (value);
96 _constraintName = value;
101 [DataCategory ("Data")]
102 public PropertyCollection ExtendedProperties {
103 get { return _properties; }
106 public abstract DataTable Table {
110 internal ConstraintCollection ConstraintCollection {
111 get { return _constraintCollection; }
112 set { _constraintCollection = value; }
115 private void _onConstraintNameChange (string newName)
117 DelegateConstraintNameChange eh = events [beforeConstraintNameChange] as DelegateConstraintNameChange;
122 //call once before adding a constraint to a collection
123 //will throw an exception to prevent the add if a rule is broken
124 internal abstract void AddToConstraintCollectionSetup (ConstraintCollection collection);
126 internal abstract bool IsConstraintViolated ();
128 internal static void ThrowConstraintException ()
130 throw new ConstraintException("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
133 bool initInProgress = false;
134 internal virtual bool InitInProgress {
135 get { return initInProgress; }
136 set { initInProgress = value; }
139 internal virtual void FinishInit (DataTable table)
143 internal void AssertConstraint ()
145 // The order is important.. IsConstraintViolated fills the RowErrors if it detects
147 if (!IsConstraintViolated ())
149 if (Table._duringDataLoad || (Table.DataSet != null && !Table.DataSet.EnforceConstraints))
151 ThrowConstraintException ();
154 internal abstract void AssertConstraint (DataRow row);
156 internal virtual void RollbackAssert (DataRow row)
160 //call once before removing a constraint to a collection
161 //can throw an exception to prevent the removal
162 internal abstract void RemoveFromConstraintCollectionCleanup (ConstraintCollection collection);
165 protected void CheckStateForProperty ()
167 throw new NotImplementedException ();
170 protected internal void SetDataSet (DataSet dataSet)
172 this.dataSet = dataSet;
175 internal void SetExtendedProperties (PropertyCollection properties)
177 _properties = properties;
180 internal Index Index {
181 get { return _index; }
183 if (_index != null) {
185 Table.DropIndex(_index);
195 internal abstract bool IsColumnContained (DataColumn column);
196 internal abstract bool CanRemoveFromCollection (ConstraintCollection col, bool shouldThrow);
199 /// Gets the ConstraintName, if there is one, as a string.
201 public override string ToString ()
203 return _constraintName == null ? "" : _constraintName;