1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
//
// System.Data.DataRelation.cs
//
// Author:
// Daniel Morgan <danmorg@sc.rr.com>
// Alan Tam Siu Lung <Tam@SiuLung.com>
// Tim Coleman <tim@timcoleman.com>
//
// (C) 2002 Daniel Morgan
// (C) 2002 Ximian, Inc.
// Copyright (C) Tim Coleman, 2003
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace System.Data
{
/// <summary>
/// DataRelation is used for a parent/child relationship
/// between two DataTable objects
/// </summary>
[Editor ("Microsoft.VSDesigner.Data.Design.DataRelationEditor, " + Consts.AssemblyMicrosoft_VSDesigner,
"System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
[DefaultProperty ("RelationName")]
[Serializable]
[MonoTODO]
[TypeConverterAttribute (typeof (RelationshipConverter))]
public class DataRelation {
private DataSet dataSet;
private string relationName;
private UniqueConstraint parentKeyConstraint;
private ForeignKeyConstraint childKeyConstraint;
private DataColumn[] parentColumns;
private DataColumn[] childColumns;
private bool nested;
internal bool createConstraints;
private PropertyCollection extendedProperties;
private PropertyChangedEventHandler onPropertyChangingDelegate;
#region Constructors
public DataRelation (string relationName, DataColumn parentColumn, DataColumn childColumn)
: this(relationName, parentColumn, childColumn, true)
{
}
public DataRelation (string relationName, DataColumn[] parentColumns, DataColumn[] childColumns)
: this(relationName, parentColumns, childColumns, true)
{
}
public DataRelation (string relationName, DataColumn parentColumn, DataColumn childColumn, bool createConstraints)
: this(relationName, new DataColumn[] { parentColumn }, new DataColumn[] { childColumn }, createConstraints)
{
}
public DataRelation (string relationName, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
{
this.extendedProperties = new PropertyCollection();
if (relationName == null) relationName = string.Empty;
this.relationName = relationName;
if (parentColumns == null) throw new ArgumentNullException ();
this.parentColumns = parentColumns;
if (childColumns == null) throw new ArgumentNullException ();
this.childColumns = childColumns;
this.createConstraints = createConstraints;
if (parentColumns.Length != childColumns.Length)
throw new ArgumentException ("ParentColumns and ChildColumns should be the same length");
DataTable parentTable = parentColumns[0].Table;
DataTable childTable = childColumns[0].Table;
if (parentTable.DataSet != childTable.DataSet)
throw new InvalidConstraintException ();
foreach (DataColumn column in parentColumns)
if (column.Table != parentTable)
throw new InvalidConstraintException ();
foreach (DataColumn column in childColumns)
if (column.Table != childTable)
throw new InvalidConstraintException ();
for (int i=0; i<ChildColumns.Length; i++)
if (!( parentColumns[i].DataType.Equals( childColumns[i].DataType)))
throw new InvalidConstraintException();
}
[MonoTODO]
[Browsable (false)]
public DataRelation (string relationName, string parentTableName, string childTableName, string[] parentColumnNames, string[] childColumnNames, bool nested)
{
throw new NotImplementedException ();
}
#if NET_2_0
[MonoTODO]
public DataRelation (string relationName, string parentTableName, string parentTableNamespace, string childTableName, string childTableNamespace, string[] parentColumnNames, string[] childColumnNames, bool nested)
{
throw new NotImplementedException ();
}
#endif
#endregion // Constructors
#region Properties
[DataCategory ("Data")]
[DataSysDescription ("Indicates the child columns of this relation.")]
public virtual DataColumn[] ChildColumns {
get {
return childColumns;
}
}
public virtual ForeignKeyConstraint ChildKeyConstraint {
get {
return childKeyConstraint;
}
}
internal void SetChildKeyConstraint(ForeignKeyConstraint foreignKeyConstraint) {
childKeyConstraint = foreignKeyConstraint;
}
public virtual DataTable ChildTable {
get {
return childColumns[0].Table;
}
}
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public virtual DataSet DataSet {
get {
return childColumns[0].Table.DataSet;
}
}
[Browsable (false)]
[DataCategory ("Data")]
[DataSysDescription ("The collection that holds custom user information.")]
public PropertyCollection ExtendedProperties {
get {
if (extendedProperties == null)
extendedProperties = new PropertyCollection();
return extendedProperties;
}
}
[DataCategory ("Data")]
[DataSysDescription ("Indicates whether relations are nested.")]
[DefaultValue (false)]
public virtual bool Nested {
get {
return nested;
}
set {
nested = value;
}
}
[DataCategory ("Data")]
[DataSysDescription ("Indicates the parent columns of this relation.")]
public virtual DataColumn[] ParentColumns {
get {
return parentColumns;
}
}
public virtual UniqueConstraint ParentKeyConstraint {
get {
return parentKeyConstraint;
}
}
internal void SetParentKeyConstraint(UniqueConstraint uniqueConstraint) {
parentKeyConstraint = uniqueConstraint;
}
internal void SetDataSet(DataSet ds) {
dataSet = ds;
}
public virtual DataTable ParentTable {
get {
return parentColumns[0].Table;
}
}
[DataCategory ("Data")]
[DataSysDescription ("The name used to look up this relation in the Relations collection of a DataSet.")]
[DefaultValue ("")]
public virtual string RelationName {
get {
return relationName;
}
set {
relationName = value;
}
}
#endregion // Properties
#region Methods
protected void CheckStateForProperty ()
{
// TODO: check consistency of constraints
DataTable parentTable = parentColumns[0].Table;
DataTable childTable = parentColumns[0].Table;
if (parentTable.DataSet != childTable.DataSet)
throw new DataException ();
bool allColumnsEqual = false;
for (int colCnt = 0; colCnt < parentColumns.Length; ++colCnt) {
if (!parentColumns [colCnt].DataType.Equals (childColumns [colCnt].DataType))
throw new DataException ();
if (parentColumns [colCnt] != childColumns [colCnt]) allColumnsEqual = false;
}
if (allColumnsEqual) throw new DataException ();
}
protected internal void OnPropertyChanging (PropertyChangedEventArgs pcevent)
{
if (onPropertyChangingDelegate != null)
onPropertyChangingDelegate (this, pcevent);
}
protected internal void RaisePropertyChanging (string name)
{
OnPropertyChanging(new PropertyChangedEventArgs(name));
}
public override string ToString ()
{
return relationName;
}
internal void UpdateConstraints ()
{
if ( ! createConstraints)
return;
ForeignKeyConstraint foreignKeyConstraint = null;
UniqueConstraint uniqueConstraint = null;
foreignKeyConstraint = FindForeignKey (ChildTable.Constraints);
uniqueConstraint = FindUniqueConstraint (ParentTable.Constraints);
// if we did not find the unique constraint in the parent table.
// we generate new uniqueConstraint and add it to the parent table.
if (uniqueConstraint == null) {
uniqueConstraint = new UniqueConstraint (ParentColumns, false);
ParentTable.Constraints.Add (uniqueConstraint);
}
// if we did not find the foreign key constraint in the parent table.
// we generate new foreignKeyConstraint and add it to the parent table.
if (foreignKeyConstraint == null) {
foreignKeyConstraint = new ForeignKeyConstraint (RelationName,
ParentColumns,
ChildColumns);
ChildTable.Constraints.Add (foreignKeyConstraint);
}
SetParentKeyConstraint (uniqueConstraint);
SetChildKeyConstraint (foreignKeyConstraint);
}
private static bool CompareDataColumns (DataColumn [] dc1, DataColumn [] dc2)
{
if (dc1.Length != dc2.Length)
return false;
for (int columnCnt = 0; columnCnt < dc1.Length; ++columnCnt){
if (dc1 [columnCnt] != dc2 [columnCnt])
return false;
}
return true;
}
private ForeignKeyConstraint FindForeignKey (ConstraintCollection cl)
{
ForeignKeyConstraint fkc = null;
foreach (Constraint o in cl) {
if (! (o is ForeignKeyConstraint))
continue;
fkc = (ForeignKeyConstraint) o;
/* Check ChildColumns & ParentColumns */
if (CompareDataColumns (ChildColumns, fkc.Columns) &&
CompareDataColumns (ParentColumns, fkc.RelatedColumns))
return fkc;
}
return null;
}
private UniqueConstraint FindUniqueConstraint (ConstraintCollection cl)
{
UniqueConstraint uc = null;
// find if the unique constraint already exists in the parent table.
foreach (Constraint o in cl){
if (! (o is UniqueConstraint))
continue;
uc = (UniqueConstraint) o;
//Check in ParentColumns
if (CompareDataColumns (ParentColumns, uc.Columns))
return uc;
}
return null;
}
/// <summary>
/// Check whether the given column is part of this relation.
/// <summary>
/// <returns>
/// true if the column is part of this relation, otherwise false.
/// </returns>
internal bool Contains (DataColumn column)
{
foreach (DataColumn col in ParentColumns)
if (col == column)
return true;
foreach (DataColumn col in ChildColumns)
if (col == column)
return true;
return false;
}
#endregion // Methods
}
}
|