BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Data / System.Data / ForeignKeyConstraint.cs
index e8963146bf62be81d0269037f5e7e7fac56deed6..965810599ccbff1dc9aad2ad54ea69780fa51574 100644 (file)
@@ -56,6 +56,9 @@ namespace System.Data {
                private Rule _updateRule = Rule.Cascade;
                private AcceptRejectRule _acceptRejectRule = AcceptRejectRule.None;
                private string _parentTableName;
+#if NET_2_0
+               private string _parentTableNamespace;
+#endif
                private string _childTableName;
 
                //FIXME: remove those; and use only DataColumns[]
@@ -99,6 +102,7 @@ namespace System.Data {
                [Browsable (false)]
                public ForeignKeyConstraint(string constraintName, string parentTableName, string[] parentColumnNames, string[] childColumnNames, AcceptRejectRule acceptRejectRule, Rule deleteRule, Rule updateRule) 
                {
+                       int i;
                        InitInProgress = true;
                        base.ConstraintName = constraintName;
 
@@ -109,11 +113,16 @@ namespace System.Data {
                        // Keep reference to parentTableName to resolve later
                        _parentTableName = parentTableName;
 
-                       // Keep reference to parentColumnNames to resolve later
-                       _parentColumnNames = parentColumnNames;
-
-                       // Keep reference to childColumnNames to resolve later
-                       _childColumnNames = childColumnNames;
+                       // Keep a copy of parentColumnNames to resolve later
+                       _parentColumnNames = new string [parentColumnNames.Length];
+                       for (i = 0; i < parentColumnNames.Length; i++)
+                              _parentColumnNames[i] = parentColumnNames[i];
+                       
+                       // Keep a copy of childColumnNames to resolve later
+                       _childColumnNames = new string [childColumnNames.Length];
+                       for (i = 0; i < childColumnNames.Length; i++)
+                              _childColumnNames[i] = childColumnNames[i];
+                       
 
                        _acceptRejectRule = acceptRejectRule;
                        _deleteRule = deleteRule;
@@ -157,30 +166,57 @@ namespace System.Data {
                        _validateColumns (parentColumns, childColumns);
                        _parentColumns = parentColumns;
                        _childColumns = childColumns;
+#if NET_2_0
+                       parentTable.Namespace = _parentTableNamespace;
+#endif
                        InitInProgress = false;
                }
 
 #if NET_2_0
-               [MonoTODO]
+               [Browsable (false)]
                public ForeignKeyConstraint (string constraintName, string parentTableName, string parentTableNamespace, string[] parentColumnNames, string[] childColumnNames, AcceptRejectRule acceptRejectRule, Rule deleteRule, Rule updateRule)
                {
-                       throw new NotImplementedException ();
+                       InitInProgress = true;
+                       base.ConstraintName = constraintName;
+
+                       // "parentTableName" is searched in the "DataSet" to which the "DataTable"
+                       // from which AddRange() is called
+                       // childTable is the "DataTable" which calls AddRange()
+
+                       // Keep reference to parentTableName to resolve later
+                       _parentTableName = parentTableName;
+                       _parentTableNamespace = parentTableNamespace;
+
+                       // Keep reference to parentColumnNames to resolve later
+                       _parentColumnNames = parentColumnNames;
+
+                       // Keep reference to childColumnNames to resolve later
+                       _childColumnNames = childColumnNames;
+
+                       _acceptRejectRule = acceptRejectRule;
+                       _deleteRule = deleteRule;
+                       _updateRule = updateRule;
                }
 #endif
 
                private void _foreignKeyConstraint(string constraintName, DataColumn[] parentColumns,
                                DataColumn[] childColumns)
                {
-
+                       int i;
                        //Validate 
                        _validateColumns(parentColumns, childColumns);
 
                        //Set Constraint Name
                        base.ConstraintName = constraintName;   
 
-                       //Keep reference to columns
-                       _parentColumns = parentColumns;
-                       _childColumns = childColumns;
+                       //copy the columns - Do not keep reference #672113
+                       _parentColumns = new DataColumn [parentColumns.Length];
+                       for (i = 0; i < parentColumns.Length; i++)
+                              _parentColumns[i] = parentColumns[i];
+                       
+                       _childColumns = new DataColumn [childColumns.Length];                   
+                       for (i = 0; i < childColumns.Length; i++)
+                              _childColumns[i] = childColumns[i];
                }
 
 #endregion // Constructors
@@ -247,24 +283,26 @@ namespace System.Data {
                        }       
 
 
+                       int identicalCols = 0;
                        for (int i = 0; i < parentColumns.Length; i++)
                        {
                                DataColumn pc = parentColumns[i];
                                DataColumn cc = childColumns[i];
                                
-                               //Can't be the same column
-                               if (pc == cc)
-                                       throw new InvalidOperationException("Parent and child columns can't be the same column.");
+                               if (pc == cc) {
+                                       identicalCols++;
+                                       continue;
+                               }
 
-                               if (! pc.DataType.Equals(cc.DataType))
-                               {
+                               if (!pc.DataTypeMatches (cc)) {
                                        //LAMESPEC: spec says throw InvalidConstraintException
                                        //              implementation throws InvalidOperationException
-                                       throw new InvalidConstraintException("Parent column is not type compatible with it's child"
+                                       throw new InvalidOperationException ("Parent column is not type compatible with it's child"
                                                + " column.");
                                }
-                                       
                        }
+                       if (identicalCols == parentColumns.Length)
+                               throw new InvalidOperationException ("Property not accessible because 'ParentKey and ChildKey are identical.'.");
                        
                }