New test.
[mono.git] / mcs / class / System.Data / System.Data / Constraint.cs
1 //
2 // System.Data.Constraint.cs
3 //
4 // Author:
5 //      Franklin Wise <gracenote@earthlink.net>
6 //      Daniel Morgan
7 //      Tim Coleman (tim@timcoleman.com)
8 //   
9 //
10 // (C) Ximian, Inc. 2002
11 // Copyright (C) Tim Coleman, 2002
12 //
13
14 //
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 //
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:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
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.
35 //
36
37 using System;
38 using System.Collections;
39 using System.ComponentModel;
40 using System.Runtime.InteropServices;
41 using System.Runtime.Serialization;
42 using System.Data.Common;
43
44 namespace System.Data {
45         [Serializable]
46         internal delegate void DelegateConstraintNameChange (object sender, string newName);
47
48         [DefaultProperty ("ConstraintName")]    
49 #if !NET_2_0
50         [Serializable]
51 #endif
52         [TypeConverterAttribute (typeof (ConstraintConverter))]
53         public abstract class Constraint 
54         {
55                 internal event DelegateConstraintNameChange BeforeConstraintNameChange;
56
57                 //if constraintName is not set then a name is 
58                 //created when it is added to
59                 //the ConstraintCollection
60                 //it can not be set to null, empty or duplicate
61                 //once it has been added to the collection
62                 private string _constraintName;
63                 private PropertyCollection _properties;
64
65                 private Index _index;
66
67                 //Used for membership checking
68                 private ConstraintCollection _constraintCollection;
69
70                 DataSet dataSet;
71
72                 protected Constraint () 
73                 {
74                         dataSet = null;
75                         _properties = new PropertyCollection();
76                 }
77
78                 [CLSCompliant (false)]
79                 protected internal virtual DataSet _DataSet {
80                         get { return dataSet; }
81                 }
82
83                 [DataCategory ("Data")]
84 #if !NET_2_0
85                 [DataSysDescription ("Indicates the name of this constraint.")]
86 #endif
87                 [DefaultValue ("")]
88                 public virtual string ConstraintName {
89                         get{ return _constraintName == null ? "" : _constraintName; } 
90                         set{
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;
97                         }
98                 }
99
100                 [Browsable (false)]
101                 [DataCategory ("Data")]
102 #if !NET_2_0
103                 [DataSysDescription ("The collection that holds custom user information.")]
104 #endif
105                 public PropertyCollection ExtendedProperties {
106                         get { return _properties; }
107 #if NET_2_0
108                         internal set { _properties = value; }
109 #endif
110                 }
111
112 #if !NET_2_0
113                 [DataSysDescription ("Indicates the table of this constraint.")]
114 #endif
115                 public abstract DataTable Table {
116                         get;
117                 }
118
119                 internal ConstraintCollection ConstraintCollection {
120                         get{ return _constraintCollection; }
121                         set{ _constraintCollection = value; }
122                 }
123                 
124                 private void _onConstraintNameChange (string newName)
125                 {
126                         if (null != BeforeConstraintNameChange)
127                         {
128                                 BeforeConstraintNameChange (this, newName);
129                         }
130                 }
131
132                 //call once before adding a constraint to a collection
133                 //will throw an exception to prevent the add if a rule is broken
134                 internal abstract void AddToConstraintCollectionSetup (ConstraintCollection collection);
135                                         
136                 internal abstract bool IsConstraintViolated ();
137                 
138                 internal static void ThrowConstraintException(){
139                         throw new ConstraintException("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
140                 }
141
142                 bool initInProgress = false;
143                 internal virtual bool InitInProgress {
144                         get { return initInProgress; }
145                         set { initInProgress = value; }
146                 }
147
148                 internal virtual void FinishInit (DataTable table)
149                 {
150                 }
151
152                 internal void AssertConstraint() 
153                 {
154                         // The order is important.. IsConstraintViolated fills the RowErrors if it detects
155                         // a violation
156                         if (!IsConstraintViolated())
157                                 return;
158                         if (Table._duringDataLoad || (Table.DataSet != null && !Table.DataSet.EnforceConstraints))
159                                 return;
160                         ThrowConstraintException();
161                 }
162
163                 internal abstract void AssertConstraint(DataRow row);
164
165                 internal virtual void RollbackAssert (DataRow row)
166                 {
167                 }
168
169                 //call once before removing a constraint to a collection
170                 //can throw an exception to prevent the removal
171                 internal abstract void RemoveFromConstraintCollectionCleanup (ConstraintCollection collection);
172
173                 [MonoTODO]
174                 protected void CheckStateForProperty ()
175                 {
176                         throw new NotImplementedException ();
177                 }
178
179                 protected internal void SetDataSet (DataSet dataSet)
180                 {
181                         this.dataSet = dataSet;
182                 }
183
184                 internal Index Index
185                 {
186                         get {
187                                 return _index;
188                         }
189                         set {
190                                 if (_index != null) {
191                                         _index.RemoveRef();
192                                         Table.DropIndex(_index);
193                                 }
194
195                                 _index = value;
196
197                                 if (_index != null) {
198                                         _index.AddRef();
199                                 }
200                         }
201                 }
202
203                 internal abstract bool IsColumnContained(DataColumn column);
204                 internal abstract bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow);
205
206                 /// <summary>
207                 /// Gets the ConstraintName, if there is one, as a string. 
208                 /// </summary>
209                 public override string ToString () 
210                 {
211                         return _constraintName == null ? "" : _constraintName;
212                 }
213
214         }
215 }