1 //------------------------------------------------------------------------------
2 // <copyright file="Constraint.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">[....]</owner>
6 // <owner current="true" primary="false">[....]</owner>
7 // <owner current="false" primary="false">[....]</owner>
8 //------------------------------------------------------------------------------
10 namespace System.Data {
12 using System.ComponentModel;
13 using System.Globalization;
16 /// <para>Represents a constraint that can be enforced on one or
17 /// more <see cref='System.Data.DataColumn'/> objects.</para>
20 DefaultProperty("ConstraintName"),
21 TypeConverter(typeof(ConstraintConverter))
23 public abstract class Constraint {
24 internal String name = "";
25 private String _schemaName = "";
26 private bool inCollection = false;
27 private DataSet dataSet = null;
29 internal PropertyCollection extendedProperties = null;
32 /// <para>The name of this constraint within the <see cref='System.Data.ConstraintCollection'/>
37 ResDescriptionAttribute(Res.ConstraintNameDescr),
38 ResCategoryAttribute(Res.DataCategory_Data)
40 public virtual string ConstraintName {
48 if (Common.ADP.IsEmpty(value) && (Table != null) && InCollection)
49 throw ExceptionBuilder.NoConstraintName();
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);
56 Table.Constraints.UnregisterName(name);
60 else if (String.Compare(name, value, false, locale) != 0) {
66 internal String SchemaName {
68 if (Common.ADP.IsEmpty(_schemaName))
69 return ConstraintName;
75 if (!Common.ADP.IsEmpty(value))
81 internal virtual bool InCollection {
82 get { // ACCESSOR: virtual was missing from this get
88 dataSet = Table.DataSet;
95 /// <para>Gets the <see cref='System.Data.DataTable'/> to which the constraint applies.</para>
97 [ResDescriptionAttribute(Res.ConstraintTableDescr)]
98 public abstract DataTable Table {
103 /// <para>Gets the collection of customized user information.</para>
106 ResCategoryAttribute(Res.DataCategory_Data),
108 ResDescriptionAttribute(Res.ExtendedPropertiesDescr)
110 public PropertyCollection ExtendedProperties {
112 if (extendedProperties == null) {
113 extendedProperties = new PropertyCollection();
115 return extendedProperties;
119 internal abstract bool ContainsColumn(DataColumn column);
121 internal abstract bool CanEnableConstraint();
123 internal abstract Constraint Clone(DataSet destination);
124 internal abstract Constraint Clone(DataSet destination, bool ignoreNSforTableLookup);
126 internal void CheckConstraint() {
127 if (!CanEnableConstraint()) {
128 throw ExceptionBuilder.ConstraintViolation(ConstraintName);
132 internal abstract void CheckCanAddToCollection(ConstraintCollection constraint);
133 internal abstract bool CanBeRemovedFromCollection(ConstraintCollection constraint, bool fThrowException);
135 internal abstract void CheckConstraint(DataRow row, DataRowAction action);
136 internal abstract void CheckState();
138 protected void CheckStateForProperty() {
142 catch (Exception e) {
144 if (!Common.ADP.IsCatchableExceptionType (e)) {
147 throw ExceptionBuilder.BadObjectPropertyAccess(e.Message);
152 /// <para>Gets the <see cref='System.Data.DataSet'/> to which this constraint belongs.</para>
154 [CLSCompliant(false)]
155 protected virtual DataSet _DataSet {
162 /// <para>Sets the constraint's <see cref='System.Data.DataSet'/>.</para>
164 protected internal void SetDataSet(DataSet dataSet) {
165 this.dataSet = dataSet;
167 internal abstract bool IsConstraintViolated();
169 public override string ToString() {
170 return ConstraintName;