diff options
author | Sureshkumar T <suresh@mono-cvs.ximian.com> | 2005-02-02 17:22:36 +0300 |
---|---|---|
committer | Sureshkumar T <suresh@mono-cvs.ximian.com> | 2005-02-02 17:22:36 +0300 |
commit | 9dfaf97ca580ed02eef0c9fc046a8fb5fb4cf61f (patch) | |
tree | 142a41b3f53155534c5d0dc8e85e9665336a3c23 /mcs | |
parent | 09a5906206a1155822f2dbff4c9566ab3b57bc80 (diff) |
In Test/System.Data:
2005-02-02 Sureshkumar T <tsureshkumar@novell.com>
* DataTableTest.cs: ClearReset (): added valid error messages.
In System.Data:
* DataColumnCollection.cs: implemented todo item "check for
constraints" when removing columns from table.
* DataRowCollection.cs: Clear (): don't have to throw child key
constraint exception when the child table does not have any
rows. safe to remove parent rows.
* UniqueConstraint.cs: added method "Contains (DataColumn)" to
check whether a column is part of UniqueConstraint.
* ForeignKeyConstraint.cs: added method "Contains (DataColumn,
lookInParent)" to check whether a column is part of foreignkey.
fixes nunit failure: DataTableTest.ClearTest ()
2005-02-02 Sureshkumar T <tsureshkumar@novell.com>
svn path=/trunk/mcs/; revision=39986
Diffstat (limited to 'mcs')
7 files changed, 94 insertions, 24 deletions
diff --git a/mcs/class/System.Data/System.Data/ChangeLog b/mcs/class/System.Data/System.Data/ChangeLog index 1e8830daec6..1ddfb8cbc30 100644 --- a/mcs/class/System.Data/System.Data/ChangeLog +++ b/mcs/class/System.Data/System.Data/ChangeLog @@ -1,5 +1,19 @@ 2005-02-02 Sureshkumar T <tsureshkumar@novell.com> + * DataColumnCollection.cs: implemented todo item "check for + constraints" when removing columns from table. + * DataRowCollection.cs: Clear (): don't have to throw child key + constraint exception when the child table does not have any + rows. safe to remove parent rows. + * UniqueConstraint.cs: added method "Contains (DataColumn)" to + check whether a column is part of UniqueConstraint. + * ForeignKeyConstraint.cs: added method "Contains (DataColumn, + lookInParent)" to check whether a column is part of foreignkey. + + fixes nunit failure: DataTableTest.ClearTest () + +2005-02-02 Sureshkumar T <tsureshkumar@novell.com> + * ForeignKeyConstraint.cs: validating columns: move checking "tables are of same dataset" before "checking column types". diff --git a/mcs/class/System.Data/System.Data/DataColumnCollection.cs b/mcs/class/System.Data/System.Data/DataColumnCollection.cs index 7ed82a95f14..49539cda6fa 100644 --- a/mcs/class/System.Data/System.Data/DataColumnCollection.cs +++ b/mcs/class/System.Data/System.Data/DataColumnCollection.cs @@ -308,9 +308,7 @@ namespace System.Data { //Check that the column does not have a null reference. if (column == null) - { - return false; - } + return false; //Check that the column is part of this collection. @@ -372,8 +370,36 @@ namespace System.Data { } } - //TODO: check constraints - + // check for part of pk + UniqueConstraint uc = UniqueConstraint.GetPrimaryKeyConstraint (parentTable.Constraints); + if (uc != null && uc.Contains (column)) + throw new ArgumentException (String.Format ("Cannot remove column {0}, because" + + " it is part of primarykey", + column.ColumnName)); + // check for part of fk + DataSet ds = parentTable.DataSet; + + if (ds != null) { + foreach (DataTable t in ds.Tables) { + if (t == parentTable) + continue; + foreach (Constraint c in t.Constraints) { + if (! (c is ForeignKeyConstraint)) + continue; + ForeignKeyConstraint fk = (ForeignKeyConstraint) c; + if (fk.Contains (column, true) // look in parent + || fk.Contains (column, false)) // look in children + throw new ArgumentException (String.Format ("Cannot remove column {0}, because" + + " it is part of foreign key constraint", + column.ColumnName)); + + } + + } + + + } + return true; } @@ -424,7 +450,13 @@ namespace System.Data { } } - + + // whether all columns can be removed + foreach (DataColumn col in this) { + if (!CanRemove (col)) + throw new ArgumentException ("Cannot remove column {0}", col.ColumnName); + } + try { columnFromName.Clear(); autoIncrement.Clear(); diff --git a/mcs/class/System.Data/System.Data/DataRowCollection.cs b/mcs/class/System.Data/System.Data/DataRowCollection.cs index f702cf3739c..c8593545b30 100644 --- a/mcs/class/System.Data/System.Data/DataRowCollection.cs +++ b/mcs/class/System.Data/System.Data/DataRowCollection.cs @@ -124,19 +124,22 @@ namespace System.Data /// </summary> public void Clear () { - if (this.table.DataSet != null && this.table.DataSet.EnforceConstraints) - { - foreach (DataTable table in this.table.DataSet.Tables) - { - foreach (Constraint c in table.Constraints) - { - if (c is ForeignKeyConstraint) - { - if (((ForeignKeyConstraint) c).RelatedTable.Equals(this.table)) + if (this.table.DataSet != null && this.table.DataSet.EnforceConstraints) { + foreach (DataTable table in this.table.DataSet.Tables) { + foreach (Constraint c in table.Constraints) { + if (c is ForeignKeyConstraint) { + ForeignKeyConstraint fk = (ForeignKeyConstraint) c; + if (fk.RelatedTable.Equals(this.table) + && fk.Table.Rows.Count > 0) // check does not make sense if we don't have rows #if NET_1_1 - throw new InvalidConstraintException (String.Format ("Cannot clear table Parent because ForeignKeyConstraint {0} enforces Child.", c.ConstraintName)); + throw new InvalidConstraintException (String.Format ("Cannot clear table Parent" + + " because ForeignKeyConstraint "+ + "{0} enforces Child.", + c.ConstraintName)); #else - throw new ArgumentException (String.Format ("Cannot clear table Parent because ForeignKeyConstraint {0} enforces Child.", c.ConstraintName)); + throw new ArgumentException (String.Format ("Cannot clear table Parent because " + + "ForeignKeyConstraint {0} enforces Child.", + c.ConstraintName)); #endif } } diff --git a/mcs/class/System.Data/System.Data/ForeignKeyConstraint.cs b/mcs/class/System.Data/System.Data/ForeignKeyConstraint.cs index 2090b383374..62b480bff56 100644 --- a/mcs/class/System.Data/System.Data/ForeignKeyConstraint.cs +++ b/mcs/class/System.Data/System.Data/ForeignKeyConstraint.cs @@ -575,6 +575,15 @@ namespace System.Data { } } + internal bool Contains (DataColumn c, bool asParent) + { + DataColumn [] columns = asParent? RelatedColumns : Columns; + foreach (DataColumn col in columns) + if (col == c) + return true; + return false; + } + #endregion // Methods } diff --git a/mcs/class/System.Data/System.Data/UniqueConstraint.cs b/mcs/class/System.Data/System.Data/UniqueConstraint.cs index f50b704603d..6fc1efa3ae1 100644 --- a/mcs/class/System.Data/System.Data/UniqueConstraint.cs +++ b/mcs/class/System.Data/System.Data/UniqueConstraint.cs @@ -505,6 +505,14 @@ namespace System.Data { return "Column '" + colStr + "' is constrained to be unique. Value '" + valStr + "' is already present."; } + internal bool Contains (DataColumn c) + { + foreach (DataColumn col in Columns) + if (c == col) + return true; + return false; + } + #endregion // Methods diff --git a/mcs/class/System.Data/Test/System.Data/ChangeLog b/mcs/class/System.Data/Test/System.Data/ChangeLog index 321ec95bead..a9148a1a32a 100644 --- a/mcs/class/System.Data/Test/System.Data/ChangeLog +++ b/mcs/class/System.Data/Test/System.Data/ChangeLog @@ -1,3 +1,7 @@ +2005-02-02 Sureshkumar T <tsureshkumar@novell.com> + + * DataTableTest.cs: ClearReset (): added valid error messages. + 2005-02-02 Atsushi Enomoto <atsushi@ximian.com> * DataViewManagerTest.cs : new file. diff --git a/mcs/class/System.Data/Test/System.Data/DataTableTest.cs b/mcs/class/System.Data/Test/System.Data/DataTableTest.cs index 1510b56ac6f..befc668527b 100644 --- a/mcs/class/System.Data/Test/System.Data/DataTableTest.cs +++ b/mcs/class/System.Data/Test/System.Data/DataTableTest.cs @@ -1139,8 +1139,8 @@ namespace MonoTests.System.Data [Test] public void ClearReset () //To test Clear and Reset methods { - DataTable table = new DataTable (); - DataTable table1 = new DataTable (); + DataTable table = new DataTable ("table"); + DataTable table1 = new DataTable ("table1"); DataSet set = new DataSet (); set.Tables.Add (table); @@ -1186,14 +1186,14 @@ namespace MonoTests.System.Data AssertEquals (1, table.ChildRelations.Count); try { table.Reset (); - Fail ("#A01"); + Fail ("#A01, should have thrown ArgumentException"); } catch (ArgumentException) { } - AssertEquals (0, table.Rows.Count); - AssertEquals (0, table.ChildRelations.Count); - AssertEquals (0, table.ParentRelations.Count); - AssertEquals (0, table.Constraints.Count); + AssertEquals ("#CT01", 0, table.Rows.Count); + AssertEquals ("#CT02", 0, table.ChildRelations.Count); + AssertEquals ("#CT03", 0, table.ParentRelations.Count); + AssertEquals ("#CT04", 0, table.Constraints.Count); table.Clear (); table1.Reset (); |