Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Constraint.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="Constraint.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">[....]</owner>
6 // <owner current="true" primary="false">[....]</owner>
7 // <owner current="false" primary="false">[....]</owner>
8 //------------------------------------------------------------------------------
9
10 namespace System.Data {
11     using System;
12     using System.ComponentModel;
13     using System.Globalization;
14
15     /// <devdoc>
16     ///    <para>Represents a constraint that can be enforced on one or
17     ///       more <see cref='System.Data.DataColumn'/> objects.</para>
18     /// </devdoc>
19     [
20     DefaultProperty("ConstraintName"),
21     TypeConverter(typeof(ConstraintConverter))
22     ]
23     public abstract class Constraint {
24         internal String name = "";
25         private String _schemaName = "";
26         private bool inCollection = false;
27         private DataSet dataSet = null;
28
29         internal PropertyCollection extendedProperties = null;
30
31         /// <devdoc>
32         /// <para>The name of this constraint within the <see cref='System.Data.ConstraintCollection'/> 
33         /// .</para>
34         /// </devdoc>
35         [
36         DefaultValue(""), 
37         ResDescriptionAttribute(Res.ConstraintNameDescr),
38         ResCategoryAttribute(Res.DataCategory_Data)
39         ]
40         public virtual string ConstraintName {
41             get {
42                 return name;
43             }
44             set {
45                 if (value == null)
46                     value = "";
47
48                 if (Common.ADP.IsEmpty(value) && (Table != null) && InCollection)
49                     throw ExceptionBuilder.NoConstraintName();
50
51                 CultureInfo locale = (Table != null ? Table.Locale : CultureInfo.CurrentCulture);
52                 if (String.Compare(name, value, true, locale) != 0) {
53                     if ((Table != null) && InCollection) {
54                         Table.Constraints.RegisterName(value);
55                         if (name.Length != 0)
56                             Table.Constraints.UnregisterName(name);
57                     }
58                     name = value;
59                 }
60                 else if (String.Compare(name, value, false, locale) != 0) {
61                     name = value;
62                 }
63             }
64         }
65
66         internal String SchemaName {
67             get {
68                 if (Common.ADP.IsEmpty(_schemaName))
69                     return ConstraintName;
70                 else
71                     return _schemaName;
72             }
73
74             set {
75                 if (!Common.ADP.IsEmpty(value))
76                     _schemaName = value;
77             }
78         }
79
80
81         internal virtual bool InCollection {
82             get {               // ACCESSOR: virtual was missing from this get
83                 return inCollection;
84             }
85             set {
86                 inCollection = value;
87                 if (value)
88                     dataSet = Table.DataSet;
89                 else
90                     dataSet = null;
91             }
92         }
93
94         /// <devdoc>
95         /// <para>Gets the <see cref='System.Data.DataTable'/> to which the constraint applies.</para>
96         /// </devdoc>
97         [ResDescriptionAttribute(Res.ConstraintTableDescr)]
98         public abstract DataTable Table {
99             get;
100         }
101
102         /// <devdoc>
103         ///    <para>Gets the collection of customized user information.</para>
104         /// </devdoc>
105         [
106         ResCategoryAttribute(Res.DataCategory_Data), 
107         Browsable(false),
108         ResDescriptionAttribute(Res.ExtendedPropertiesDescr)
109         ]
110         public PropertyCollection ExtendedProperties {
111             get {
112                 if (extendedProperties == null) {
113                     extendedProperties = new PropertyCollection();
114                 }
115                 return extendedProperties;
116             }
117         }
118
119         internal abstract bool ContainsColumn(DataColumn column);
120
121         internal abstract bool CanEnableConstraint();
122
123         internal abstract Constraint Clone(DataSet destination);
124         internal abstract Constraint Clone(DataSet destination, bool ignoreNSforTableLookup);
125
126         internal void CheckConstraint() {
127             if (!CanEnableConstraint()) {
128                 throw ExceptionBuilder.ConstraintViolation(ConstraintName);
129             }
130         }
131
132         internal abstract void CheckCanAddToCollection(ConstraintCollection constraint);
133         internal abstract bool CanBeRemovedFromCollection(ConstraintCollection constraint, bool fThrowException);
134
135         internal abstract void CheckConstraint(DataRow row, DataRowAction action);
136         internal abstract void CheckState();
137
138          protected void CheckStateForProperty() {
139             try {
140                 CheckState();
141             }
142             catch (Exception e) {
143                 // 
144                 if (!Common.ADP.IsCatchableExceptionType (e)) {
145                    throw;
146                 }
147                 throw ExceptionBuilder.BadObjectPropertyAccess(e.Message);            
148             }
149         }
150
151         /// <devdoc>
152         /// <para>Gets the <see cref='System.Data.DataSet'/> to which this constraint belongs.</para>
153         /// </devdoc>
154         [CLSCompliant(false)]
155         protected virtual DataSet _DataSet {
156             get {
157                 return dataSet;
158             }
159         }
160         
161         /// <devdoc>
162         /// <para>Sets the constraint's <see cref='System.Data.DataSet'/>.</para>
163         /// </devdoc>
164         protected internal void SetDataSet(DataSet dataSet) {
165             this.dataSet = dataSet;
166         }
167         internal abstract bool IsConstraintViolated();
168
169          public override string ToString() {
170             return ConstraintName;
171         }
172     }
173 }