2 // System.Data.DataRelation.cs
5 // Daniel Morgan <danmorg@sc.rr.com>
6 // Alan Tam Siu Lung <Tam@SiuLung.com>
7 // Tim Coleman <tim@timcoleman.com>
9 // (C) 2002 Daniel Morgan
10 // (C) 2002 Ximian, Inc.
11 // Copyright (C) Tim Coleman, 2003
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
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.
38 using System
.ComponentModel
;
39 using System
.Runtime
.Serialization
;
44 /// DataRelation is used for a parent/child relationship
45 /// between two DataTable objects
48 [DefaultProperty ("RelationName")]
51 [TypeConverterAttribute (typeof (RelationshipConverter
))]
52 public class DataRelation
{
53 private DataSet dataSet
;
54 private string relationName
;
55 private UniqueConstraint parentKeyConstraint
;
56 private ForeignKeyConstraint childKeyConstraint
;
57 private DataColumn
[] parentColumns
;
58 private DataColumn
[] childColumns
;
60 internal bool createConstraints
;
61 private PropertyCollection extendedProperties
;
62 private PropertyChangedEventHandler onPropertyChangingDelegate
;
66 public DataRelation (string relationName
, DataColumn parentColumn
, DataColumn childColumn
)
67 : this(relationName
, parentColumn
, childColumn
, true)
71 public DataRelation (string relationName
, DataColumn
[] parentColumns
, DataColumn
[] childColumns
)
72 : this(relationName
, parentColumns
, childColumns
, true)
76 public DataRelation (string relationName
, DataColumn parentColumn
, DataColumn childColumn
, bool createConstraints
)
77 : this(relationName
, new DataColumn
[] { parentColumn }
, new DataColumn
[] { childColumn }
, createConstraints
)
81 public DataRelation (string relationName
, DataColumn
[] parentColumns
, DataColumn
[] childColumns
, bool createConstraints
)
83 this.extendedProperties
= new PropertyCollection();
84 if (relationName
== null) relationName
= string.Empty
;
85 this.relationName
= relationName
;
86 if (parentColumns
== null) throw new ArgumentNullException ();
87 this.parentColumns
= parentColumns
;
88 if (childColumns
== null) throw new ArgumentNullException ();
89 this.childColumns
= childColumns
;
90 this.createConstraints
= createConstraints
;
91 if (parentColumns
.Length
!= childColumns
.Length
)
92 throw new ArgumentException ("ParentColumns and ChildColumns should be the same length");
93 DataTable parentTable
= parentColumns
[0].Table
;
94 DataTable childTable
= childColumns
[0].Table
;
95 if (parentTable
.DataSet
!= childTable
.DataSet
)
96 throw new InvalidConstraintException ();
97 foreach (DataColumn column
in parentColumns
)
98 if (column
.Table
!= parentTable
)
99 throw new InvalidConstraintException ();
100 foreach (DataColumn column
in childColumns
)
101 if (column
.Table
!= childTable
)
102 throw new InvalidConstraintException ();
104 for (int i
=0; i
<ChildColumns
.Length
; i
++)
105 if (!( parentColumns
[i
].DataType
.Equals( childColumns
[i
].DataType
)))
106 throw new InvalidConstraintException();
111 public DataRelation (string relationName
, string parentTableName
, string childTableName
, string[] parentColumnNames
, string[] childColumnNames
, bool nested
)
113 throw new NotImplementedException ();
118 public DataRelation (string relationName
, string parentTableName
, string parentTableNamespace
, string childTableName
, string childTableNamespace
, string[] parentColumnNames
, string[] childColumnNames
, bool nested
)
120 throw new NotImplementedException ();
124 #endregion // Constructors
128 [DataCategory ("Data")]
129 [DataSysDescription ("Indicates the child columns of this relation.")]
130 public virtual DataColumn
[] ChildColumns
{
136 public virtual ForeignKeyConstraint ChildKeyConstraint
{
138 return childKeyConstraint
;
142 internal void SetChildKeyConstraint(ForeignKeyConstraint foreignKeyConstraint
) {
143 childKeyConstraint
= foreignKeyConstraint
;
146 public virtual DataTable ChildTable
{
148 return childColumns
[0].Table
;
153 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
154 public virtual DataSet DataSet
{
156 return childColumns
[0].Table
.DataSet
;
161 [DataCategory ("Data")]
162 [DataSysDescription ("The collection that holds custom user information.")]
163 public PropertyCollection ExtendedProperties
{
165 if (extendedProperties
== null)
166 extendedProperties
= new PropertyCollection();
167 return extendedProperties
;
171 [DataCategory ("Data")]
172 [DataSysDescription ("Indicates whether relations are nested.")]
173 [DefaultValue (false)]
174 public virtual bool Nested
{
184 [DataCategory ("Data")]
185 [DataSysDescription ("Indicates the parent columns of this relation.")]
186 public virtual DataColumn
[] ParentColumns
{
188 return parentColumns
;
192 public virtual UniqueConstraint ParentKeyConstraint
{
194 return parentKeyConstraint
;
198 internal void SetParentKeyConstraint(UniqueConstraint uniqueConstraint
) {
199 parentKeyConstraint
= uniqueConstraint
;
202 internal void SetDataSet(DataSet ds
) {
206 public virtual DataTable ParentTable
{
208 return parentColumns
[0].Table
;
212 [DataCategory ("Data")]
213 [DataSysDescription ("The name used to look up this relation in the Relations collection of a DataSet.")]
215 public virtual string RelationName
{
221 relationName
= value;
225 #endregion // Properties
229 protected void CheckStateForProperty ()
231 // TODO: check consistency of constraints
232 DataTable parentTable
= parentColumns
[0].Table
;
233 DataTable childTable
= parentColumns
[0].Table
;
234 if (parentTable
.DataSet
!= childTable
.DataSet
)
235 throw new DataException ();
236 bool allColumnsEqual
= false;
237 for (int colCnt
= 0; colCnt
< parentColumns
.Length
; ++colCnt
) {
238 if (!parentColumns
[colCnt
].DataType
.Equals (childColumns
[colCnt
].DataType
))
239 throw new DataException ();
240 if (parentColumns
[colCnt
] != childColumns
[colCnt
]) allColumnsEqual
= false;
242 if (allColumnsEqual
) throw new DataException ();
245 protected internal void OnPropertyChanging (PropertyChangedEventArgs pcevent
)
247 if (onPropertyChangingDelegate
!= null)
248 onPropertyChangingDelegate (this, pcevent
);
251 protected internal void RaisePropertyChanging (string name
)
253 OnPropertyChanging(new PropertyChangedEventArgs(name
));
256 public override string ToString ()
261 #endregion // Methods