2002-10-06 Luis Fernandez <luifer@onetel.net.uk>
[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 //   
8 //
9 // (C) Ximian, Inc. 2002
10 //
11
12 using System;
13 using System.Collections;
14 using System.ComponentModel;
15 using System.Runtime.InteropServices;
16 using System.Runtime.Serialization;
17
18 namespace System.Data
19 {
20         [Serializable]
21         internal delegate void DelegateConstraintNameChange(object sender,
22                         string newName);
23         
24         [Serializable]
25         public abstract class Constraint 
26         {
27                 internal event DelegateConstraintNameChange 
28                                         BeforeConstraintNameChange;
29
30                 //if constraintName is not set then a name is 
31                 //created when it is added to
32                 //the ConstraintCollection
33                 //it can not be set to null, empty or duplicate
34                 //once it has been added to the collection
35                 private string _constraintName = null;
36                 private PropertyCollection _properties = null;
37
38                 //Used for membership checking
39                 private ConstraintCollection _constraintCollection;
40
41                 protected Constraint() 
42                 {
43                         _properties = new PropertyCollection();
44                 }
45
46                 public virtual string ConstraintName {
47                         get{
48                                 return "" + _constraintName;
49                         } 
50
51                         set{
52                                 //This should only throw an exception when it
53                                 //is a member of a ConstraintCollection which
54                                 //means we should let the ConstraintCollection
55                                 //handle exceptions when this value changes
56                                 _onConstraintNameChange(value);
57                                 _constraintName = value;
58                         }
59                 }
60
61                 public PropertyCollection ExtendedProperties {
62                         get {
63                                 return _properties;
64                         }
65                 }
66
67                 public abstract DataTable Table {
68                         get;
69                 }
70
71                 /// <summary>
72                 /// Gets the ConstraintName, if there is one, as a string. 
73                 /// </summary>
74                 public override string ToString() 
75                 {
76                         return "" + _constraintName;
77                 }
78
79                 internal ConstraintCollection ConstraintCollection {
80                         get{
81                                 return _constraintCollection;
82                         }
83                         set{
84                                 _constraintCollection = value;
85                         }
86                 }
87                 
88
89                 private void _onConstraintNameChange(string newName)
90                 {
91                         if (null != BeforeConstraintNameChange)
92                         {
93                                 BeforeConstraintNameChange(this, newName);
94                         }
95                 }
96
97                 //call once before adding a constraint to a collection
98                 //will throw an exception to prevent the add if a rule is broken
99                 internal virtual void AddToConstraintCollectionSetup(
100                         ConstraintCollection collection){}
101                                         
102                 //call once before removing a constraint to a collection
103                 //can throw an exception to prevent the removal
104                 internal virtual void RemoveFromConstraintCollectionCleanup( 
105                         ConstraintCollection collection){}
106
107                 internal virtual void AssertConstraint(){}
108                 
109                 internal virtual void AssertConstraint(DataRow row){}
110         }
111 }