2 // System.Data.ForeignKeyConstraint.cs
5 // Franklin Wise <gracenote@earthlink.net>
6 // Daniel Morgan <danmorg@sc.rr.com>
8 // (C) 2002 Franklin Wise
9 // (C) 2002 Daniel Morgan
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.ComponentModel;
38 using System.Runtime.InteropServices;
39 using System.Data.Common;
41 namespace System.Data {
42 [Editor ("Microsoft.VSDesigner.Data.Design.ForeignKeyConstraintEditor, " + Consts.AssemblyMicrosoft_VSDesigner,
43 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
44 [DefaultProperty ("ConstraintName")]
48 public class ForeignKeyConstraint : Constraint
50 private UniqueConstraint _parentUniqueConstraint;
51 //FIXME: create a class which will wrap this collection
52 private DataColumn [] _parentColumns;
53 //FIXME: create a class which will wrap this collection
54 private DataColumn [] _childColumns;
55 private Rule _deleteRule = Rule.Cascade;
56 private Rule _updateRule = Rule.Cascade;
57 private AcceptRejectRule _acceptRejectRule = AcceptRejectRule.None;
58 private string _parentTableName;
59 private string _childTableName;
60 //FIXME: remove those; and use only DataColumns[]
61 private string [] _parentColumnNames;
62 private string [] _childColumnNames;
63 private bool _dataColsNotValidated = false;
67 public ForeignKeyConstraint(DataColumn parentColumn, DataColumn childColumn)
69 if (null == parentColumn || null == childColumn) {
70 throw new NullReferenceException("Neither parentColumn or" +
71 " childColumn can be null.");
73 _foreignKeyConstraint(null, new DataColumn[] {parentColumn},
74 new DataColumn[] {childColumn});
77 public ForeignKeyConstraint(DataColumn[] parentColumns, DataColumn[] childColumns)
79 _foreignKeyConstraint(null, parentColumns, childColumns);
82 public ForeignKeyConstraint(string constraintName, DataColumn parentColumn, DataColumn childColumn)
84 if (null == parentColumn || null == childColumn) {
85 throw new NullReferenceException("Neither parentColumn or" +
86 " childColumn can be null.");
89 _foreignKeyConstraint(constraintName, new DataColumn[] {parentColumn},
90 new DataColumn[] {childColumn});
93 public ForeignKeyConstraint(string constraintName, DataColumn[] parentColumns, DataColumn[] childColumns)
95 _foreignKeyConstraint(constraintName, parentColumns, childColumns);
100 public ForeignKeyConstraint(string constraintName, string parentTableName, string[] parentColumnNames, string[] childColumnNames, AcceptRejectRule acceptRejectRule, Rule deleteRule, Rule updateRule)
102 _dataColsNotValidated = true;
103 base.ConstraintName = constraintName;
105 // "parentTableName" is searched in the "DataSet" to which the "DataTable"
106 // from which AddRange() is called
107 // childTable is the "DataTable" which calls AddRange()
109 // Keep reference to parentTableName to resolve later
110 _parentTableName = parentTableName;
112 // Keep reference to parentColumnNames to resolve later
113 _parentColumnNames = parentColumnNames;
115 // Keep reference to childColumnNames to resolve later
116 _childColumnNames = childColumnNames;
118 _acceptRejectRule = acceptRejectRule;
119 _deleteRule = deleteRule;
120 _updateRule = updateRule;
124 internal void postAddRange (DataTable childTable)
126 // LAMESPEC - Does not say that this is mandatory
127 // Check whether childTable belongs to a DataSet
128 if (childTable.DataSet == null)
129 throw new InvalidConstraintException ("ChildTable : " + childTable.TableName + " does not belong to any DataSet");
130 DataSet dataSet = childTable.DataSet;
131 _childTableName = childTable.TableName;
132 // Search for the parentTable in the childTable's DataSet
133 if (!dataSet.Tables.Contains (_parentTableName))
134 throw new InvalidConstraintException ("Table : " + _parentTableName + "does not exist in DataSet : " + dataSet);
136 // Keep reference to parentTable
137 DataTable parentTable = dataSet.Tables [_parentTableName];
141 // LAMESPEC - Does not say which Exception is thrown
142 if (_parentColumnNames.Length < 0 || _childColumnNames.Length < 0)
143 throw new InvalidConstraintException ("Neither parent nor child columns can be zero length");
144 // LAMESPEC - Does not say which Exception is thrown
145 if (_parentColumnNames.Length != _childColumnNames.Length)
146 throw new InvalidConstraintException ("Both parent and child columns must be of same length");
147 DataColumn []parentColumns = new DataColumn [_parentColumnNames.Length];
148 DataColumn []childColumns = new DataColumn [_childColumnNames.Length];
150 // Search for the parentColumns in parentTable
151 foreach (string parentCol in _parentColumnNames){
152 if (!parentTable.Columns.Contains (parentCol))
153 throw new InvalidConstraintException ("Table : " + _parentTableName + "does not contain the column :" + parentCol);
154 parentColumns [i++] = parentTable. Columns [parentCol];
156 // Search for the childColumns in childTable
157 foreach (string childCol in _childColumnNames){
158 if (!childTable.Columns.Contains (childCol))
159 throw new InvalidConstraintException ("Table : " + _childTableName + "does not contain the column : " + childCol);
160 childColumns [j++] = childTable.Columns [childCol];
162 _validateColumns (parentColumns, childColumns);
163 _parentColumns = parentColumns;
164 _childColumns = childColumns;
169 public ForeignKeyConstraint (string constraintName, string parentTableName, string parentTableNamespace, string[] parentColumnNames, string[] childColumnNames, AcceptRejectRule acceptRejectRule, Rule deleteRule, Rule updateRule)
171 throw new NotImplementedException ();
175 private void _foreignKeyConstraint(string constraintName, DataColumn[] parentColumns,
176 DataColumn[] childColumns)
180 _validateColumns(parentColumns, childColumns);
182 //Set Constraint Name
183 base.ConstraintName = constraintName;
185 //Keep reference to columns
186 _parentColumns = parentColumns;
187 _childColumns = childColumns;
190 #endregion // Constructors
194 private void _validateColumns(DataColumn[] parentColumns, DataColumn[] childColumns)
197 if (null == parentColumns || null == childColumns)
198 throw new ArgumentNullException();
200 //at least one element in each array
201 if (parentColumns.Length < 1 || childColumns.Length < 1)
202 throw new ArgumentException("Neither ParentColumns or ChildColumns can't be" +
206 if (parentColumns.Length != childColumns.Length)
207 throw new ArgumentException("Parent columns and child columns must be the same length.");
210 DataTable ptable = parentColumns[0].Table;
211 DataTable ctable = childColumns[0].Table;
213 for (int i = 0; i < parentColumns.Length; i++) {
214 DataColumn pc = parentColumns[i];
215 DataColumn cc = childColumns[i];
218 if (null == pc.Table)
219 throw new ArgumentException("All columns must belong to a table." +
220 " ColumnName: " + pc.ColumnName + " does not belong to a table.");
222 //All columns must belong to the same table
223 if (ptable != pc.Table)
224 throw new InvalidConstraintException("Parent columns must all belong to the same table.");
227 if (null == cc.Table)
228 throw new ArgumentException("All columns must belong to a table." +
229 " ColumnName: " + pc.ColumnName + " does not belong to a table.");
231 //All columns must belong to the same table.
232 if (ctable != cc.Table)
233 throw new InvalidConstraintException("Child columns must all belong to the same table.");
235 if (pc.CompiledExpression != null)
236 throw new ArgumentException(String.Format("Cannot create a constraint based on Expression column {0}.", pc.ColumnName));
238 if (cc.CompiledExpression != null)
239 throw new ArgumentException(String.Format("Cannot create a constraint based on Expression column {0}.", cc.ColumnName));
243 //Same dataset. If both are null it's ok
244 if (ptable.DataSet != ctable.DataSet)
246 //LAMESPEC: spec says InvalidConstraintExceptoin
247 // impl does InvalidOperationException
248 throw new InvalidOperationException("Parent column and child column must belong to" +
249 " tables that belong to the same DataSet.");
254 for (int i = 0; i < parentColumns.Length; i++)
256 DataColumn pc = parentColumns[i];
257 DataColumn cc = childColumns[i];
259 //Can't be the same column
261 throw new InvalidOperationException("Parent and child columns can't be the same column.");
263 if (! pc.DataType.Equals(cc.DataType))
265 //LAMESPEC: spec says throw InvalidConstraintException
266 // implementation throws InvalidOperationException
267 throw new InvalidConstraintException("Parent column is not type compatible with it's child"
277 private void _validateRemoveParentConstraint(ConstraintCollection sender,
278 Constraint constraint, ref bool cancel, ref string failReason)
281 //if we hold a reference to the parent then cancel it
282 if (constraint == _parentUniqueConstraint)
285 failReason = "Cannot remove UniqueConstraint because the"
286 + " ForeignKeyConstraint " + this.ConstraintName + " exists.";
291 //Checks to see if a related unique constraint exists
292 //if it doesn't then a unique constraint is created.
293 //if a unique constraint can't be created an exception will be thrown
294 private void _ensureUniqueConstraintExists(ConstraintCollection collection,
295 DataColumn [] parentColumns)
298 if (null == parentColumns) throw new ArgumentNullException(
299 "ParentColumns can't be null");
301 UniqueConstraint uc = null;
303 //see if unique constraint already exists
304 //if not create unique constraint
305 if(parentColumns[0] != null)
306 uc = UniqueConstraint.GetUniqueConstraintForColumnSet(parentColumns[0].Table.Constraints, parentColumns);
309 uc = new UniqueConstraint(parentColumns, false); //could throw
310 parentColumns [0].Table.Constraints.Add (uc);
314 _parentUniqueConstraint = uc;
315 //parentColumns [0].Table.Constraints.Add (uc);
316 //if this unique constraint is attempted to be removed before us
317 //we can fail the validation
318 //collection.ValidateRemoveConstraint += new DelegateValidateRemoveConstraint(
319 // _validateRemoveParentConstraint);
327 [DataCategory ("Data")]
329 [DataSysDescription ("For accept and reject changes, indicates what kind of cascading should take place across this relation.")]
331 [DefaultValue (AcceptRejectRule.None)]
332 public virtual AcceptRejectRule AcceptRejectRule {
333 get { return _acceptRejectRule; }
334 set { _acceptRejectRule = value; }
337 [DataCategory ("Data")]
339 [DataSysDescription ("Indicates the child columns of this constraint.")]
342 public virtual DataColumn[] Columns {
343 get { return _childColumns; }
346 [DataCategory ("Data")]
348 [DataSysDescription ("For deletions, indicates what kind of cascading should take place across this relation.")]
350 [DefaultValue (Rule.Cascade)]
351 public virtual Rule DeleteRule {
352 get { return _deleteRule; }
353 set { _deleteRule = value; }
356 [DataCategory ("Data")]
358 [DataSysDescription ("For updates, indicates what kind of cascading should take place across this relation.")]
360 [DefaultValue (Rule.Cascade)]
361 public virtual Rule UpdateRule {
362 get { return _updateRule; }
363 set { _updateRule = value; }
366 [DataCategory ("Data")]
368 [DataSysDescription ("Indicates the parent columns of this constraint.")]
371 public virtual DataColumn[] RelatedColumns {
372 get { return _parentColumns; }
375 [DataCategory ("Data")]
377 [DataSysDescription ("Indicates the child table of this constraint.")]
380 public virtual DataTable RelatedTable {
382 if (_parentColumns != null)
383 if (_parentColumns.Length > 0)
384 return _parentColumns[0].Table;
386 throw new InvalidOperationException ("Property not accessible because 'Object reference not set to an instance of an object'");
390 [DataCategory ("Data")]
392 [DataSysDescription ("Indicates the table of this constraint.")]
395 public override DataTable Table {
397 if (_childColumns != null)
398 if (_childColumns.Length > 0)
399 return _childColumns[0].Table;
401 throw new InvalidOperationException ("Property not accessible because 'Object reference not set to an instance of an object'");
405 internal bool DataColsNotValidated
408 return (_dataColsNotValidated);
413 #endregion // Properties
417 public override bool Equals(object key)
419 ForeignKeyConstraint fkc = key as ForeignKeyConstraint;
420 if (null == fkc) return false;
422 //if the fk constrains the same columns then they are equal
423 if (! DataColumn.AreColumnSetsTheSame( this.RelatedColumns, fkc.RelatedColumns))
425 if (! DataColumn.AreColumnSetsTheSame( this.Columns, fkc.Columns) )
431 public override int GetHashCode()
433 //initialize hash1 and hash2 with default hashes
434 //any two DIFFERENT numbers will do here
435 int hash1 = 32, hash2 = 88;
438 //derive the hash code from the columns that way
439 //Equals and GetHashCode return Equal objects to be the
442 //Get the first parent column hash
443 if (this.Columns.Length > 0)
444 hash1 ^= this.Columns[0].GetHashCode();
446 //get the rest of the parent column hashes if there any
447 for (i = 1; i < this.Columns.Length; i++)
449 hash1 ^= this.Columns[1].GetHashCode();
453 //Get the child column hash
454 if (this.RelatedColumns.Length > 0)
455 hash2 ^= this.Columns[0].GetHashCode();
457 for (i = 1; i < this.RelatedColumns.Length; i++)
459 hash2 ^= this.RelatedColumns[1].GetHashCode();
462 //combine the two hashes
463 return hash1 ^ hash2;
466 internal override void AddToConstraintCollectionSetup(
467 ConstraintCollection collection)
469 if (collection.Table != Table)
470 throw new InvalidConstraintException("This constraint cannot be added since ForeignKey doesn't belong to table " + RelatedTable.TableName + ".");
472 //run Ctor rules again
473 _validateColumns(_parentColumns, _childColumns);
475 //we must have a unique constraint on the parent
476 _ensureUniqueConstraintExists(collection, _parentColumns);
478 //Make sure we can create this thing
479 //AssertConstraint();
480 if ( (Table.DataSet != null && Table.DataSet.EnforceConstraints)
481 || (Table.DataSet == null && Table.EnforceConstraints)) {
482 if (IsConstraintViolated())
483 throw new ArgumentException("This constraint cannot be enabled as not all values have corresponding parent values.");
485 //FIXME : if this fails and we created a unique constraint
486 //we should probably roll it back
487 // and remove index form Table
490 internal override void RemoveFromConstraintCollectionCleanup(
491 ConstraintCollection collection)
496 internal override bool IsConstraintViolated()
498 if (Table.DataSet == null || RelatedTable.DataSet == null)
501 bool hasErrors = false;
502 foreach (DataRow row in Table.Rows) {
503 // first we check if all values in _childColumns place are nulls.
505 if (row.IsNullColumns(_childColumns))
508 // check whenever there is (at least one) parent row in RelatedTable
509 if(!RelatedTable.RowsExist(_parentColumns,_childColumns,row)) {
510 // if no parent row exists - constraint is violated
512 string[] values = new string[_childColumns.Length];
513 for (int i = 0; i < _childColumns.Length; i++){
514 DataColumn col = _childColumns[i];
515 values[i] = row[col].ToString();
518 row.RowError = String.Format("ForeignKeyConstraint {0} requires the child key values ({1}) to exist in the parent table.",
519 ConstraintName, String.Join(",", values));
524 //throw new ConstraintException("Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.");
530 internal override void AssertConstraint(DataRow row)
532 // first we check if all values in _childColumns place are nulls.
534 if (row.IsNullColumns(_childColumns))
537 // check whenever there is (at least one) parent row in RelatedTable
538 if(!RelatedTable.RowsExist(_parentColumns,_childColumns,row)) {
539 // if no parent row exists - constraint is violated
540 throw new InvalidConstraintException(GetErrorMessage(row));
544 internal override bool IsColumnContained(DataColumn column)
546 for (int i = 0; i < _parentColumns.Length; i++)
547 if (column == _parentColumns[i])
550 for (int i = 0; i < _childColumns.Length; i++)
551 if (column == _childColumns[i])
557 internal override bool CanRemoveFromCollection(ConstraintCollection col, bool shouldThrow){
561 private string GetErrorMessage(DataRow row)
563 System.Text.StringBuilder sb = new System.Text.StringBuilder();
564 for (int i = 0; i < _childColumns.Length; i++) {
565 sb.Append(row[_childColumns[0]].ToString());
566 if (i != _childColumns.Length - 1) {
570 string valStr = sb.ToString();
571 return "ForeignKeyConstraint " + ConstraintName + " requires the child key values (" + valStr + ") to exist in the parent table.";
574 #endregion // Methods