whitespace cleanups
[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                 static readonly object beforeConstraintNameChange = new object ();
56
57                 EventHandlerList events = new EventHandlerList ();
58
59                 internal event DelegateConstraintNameChange BeforeConstraintNameChange {
60                         add { events.AddHandler (beforeConstraintNameChange, value); }
61                         remove { events.RemoveHandler (beforeConstraintNameChange, value); }
62                 }
63
64                 //if constraintName is not set then a name is
65                 //created when it is added to
66                 //the ConstraintCollection
67                 //it can not be set to null, empty or duplicate
68                 //once it has been added to the collection
69                 private string _constraintName;
70                 private PropertyCollection _properties;
71
72                 private Index _index;
73
74                 //Used for membership checking
75                 private ConstraintCollection _constraintCollection;
76
77                 DataSet dataSet;
78
79                 protected Constraint ()
80                 {
81                         dataSet = null;
82                         _properties = new PropertyCollection();
83                 }
84
85                 [CLSCompliant (false)]
86                 protected internal virtual DataSet _DataSet {
87                         get { return dataSet; }
88                 }
89
90                 [DataCategory ("Data")]
91 #if !NET_2_0
92                 [DataSysDescription ("Indicates the name of this constraint.")]
93 #endif
94                 [DefaultValue ("")]
95                 public virtual string ConstraintName {
96                         get{ return _constraintName == null ? "" : _constraintName; }
97                         set{
98                                 //This should only throw an exception when it
99                                 //is a member of a ConstraintCollection which
100                                 //means we should let the ConstraintCollection
101                                 //handle exceptions when this value changes
102                                 _onConstraintNameChange(value);
103                                 _constraintName = value;
104                         }
105                 }
106
107                 [Browsable (false)]
108                 [DataCategory ("Data")]
109 #if !NET_2_0
110                 [DataSysDescription ("The collection that holds custom user information.")]
111 #endif
112                 public PropertyCollection ExtendedProperties {
113                         get { return _properties; }
114 #if NET_2_0
115                         internal set { _properties = value; }
116 #endif
117                 }
118
119 #if !NET_2_0
120                 [DataSysDescription ("Indicates the table of this constraint.")]
121 #endif
122                 public abstract DataTable Table {
123                         get;
124                 }
125
126                 internal ConstraintCollection ConstraintCollection {
127                         get{ return _constraintCollection; }
128                         set{ _constraintCollection = value; }
129                 }
130
131                 private void _onConstraintNameChange (string newName)
132                 {
133                         DelegateConstraintNameChange eh = events [beforeConstraintNameChange] as DelegateConstraintNameChange;
134                         if (eh != null)
135                                 eh (this, newName);
136                 }
137
138                 //call once before adding a constraint to a collection
139                 //will throw an exception to prevent the add if a rule is broken
140                 internal abstract void AddToConstraintCollectionSetup (ConstraintCollection collection);
141
142                 internal abstract bool IsConstraintViolated ();
143
144                 internal static void ThrowConstraintException(){
145                         throw new ConstraintException("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
146                 }
147
148                 bool initInProgress = false;
149                 internal virtual bool InitInProgress {
150                         get { return initInProgress; }
151                         set { initInProgress = value; }
152                 }
153
154                 internal virtual void FinishInit (DataTable table)
155                 {
156                 }
157
158                 internal void AssertConstraint()
159                 {
160                         // The order is important.. IsConstraintViolated fills the RowErrors if it detects
161                         // a violation
162                         if (!IsConstraintViolated())
163                                 return;
164                         if (Table._duringDataLoad || (Table.DataSet != null && !Table.DataSet.EnforceConstraints))
165                                 return;
166                         ThrowConstraintException();
167                 }
168
169                 internal abstract void AssertConstraint(DataRow row);
170
171                 internal virtual void RollbackAssert (DataRow row)
172                 {
173                 }
174
175                 //call once before removing a constraint to a collection
176                 //can throw an exception to prevent the removal
177                 internal abstract void RemoveFromConstraintCollectionCleanup (ConstraintCollection collection);
178
179                 [MonoTODO]
180                 protected void CheckStateForProperty ()
181                 {
182                         throw new NotImplementedException ();
183                 }
184
185                 protected internal void SetDataSet (DataSet dataSet)
186                 {
187                         this.dataSet = dataSet;
188                 }
189
190                 internal Index Index
191                 {
192                         get {
193                                 return _index;
194                         }
195                         set {
196                                 if (_index != null) {
197                                         _index.RemoveRef();
198                                         Table.DropIndex(_index);
199                                 }
200
201                                 _index = value;
202
203                                 if (_index != null) {
204                                         _index.AddRef();
205                                 }
206                         }
207                 }
208
209                 internal abstract bool IsColumnContained(DataColumn column);
210                 internal abstract bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow);
211
212                 /// <summary>
213                 /// Gets the ConstraintName, if there is one, as a string.
214                 /// </summary>
215                 public override string ToString ()
216                 {
217                         return _constraintName == null ? "" : _constraintName;
218                 }
219
220         }
221 }