Author: suresh
Date: 2005-05-11 01:41:50 -0400 (Wed, 11 May 2005)
New Revision: 44375
Modified:
trunk/mcs/class/System.Data/System.Data/ChangeLog
trunk/mcs/class/System.Data/System.Data/DataRow.cs
trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs
trunk/mcs/class/System.Data/Test/System.Data/ChangeLog
trunk/mcs/class/System.Data/Test/System.Data/DataTableTest.cs
Log:
fix for "datatable import row does not import deleted records"
* Test/System.Data/DataTableTest.cs: revamped tests for ImportRow
method.
* System.Data/UniqueConstraint.cs: AssertConstraint: check & assert for
deleted records also.
* System.Data/DataRow.cs: Added restriction-less methods GetValue to
fetch a value even if it is in deleted state.
- If a row is detached, don't try to delete it from indexes.
- sanity check for record_index in CopyValuesToRow method
Modified: trunk/mcs/class/System.Data/System.Data/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/System.Data/ChangeLog 2005-05-11 04:44:52 UTC
(rev 44374)
+++ trunk/mcs/class/System.Data/System.Data/ChangeLog 2005-05-11 05:41:50 UTC
(rev 44375)
@@ -1,3 +1,17 @@
+2005-05-11 Sureshkumar T <[EMAIL PROTECTED]>
+
+ fix for "import row does not import deleted records"
+
+ * UniqueConstraint.cs: Make a way to access value even from a
+ deleted record. deleted records are added to the collection by
+ ImportRow method.
+
+ * DataRow.cs:
+ - Added restriction-less methods GetValue to fetch a
+ value even if it is in deleted state.
+ - If a row is detached, don't try to delete it from indexes.
+ - sanity check for record_index in CopyValuesToRow method
+
2005-05-05 Sureshkumar T <[EMAIL PROTECTED]>
* DataTableReader.cs: Implemented Delete handler and move the
Modified: trunk/mcs/class/System.Data/System.Data/DataRow.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/DataRow.cs 2005-05-11 04:44:52 UTC
(rev 44374)
+++ trunk/mcs/class/System.Data/System.Data/DataRow.cs 2005-05-11 05:41:50 UTC
(rev 44375)
@@ -676,7 +676,8 @@
DetachRow();
break;
case DataRowState.Deleted:
- break;
+ case DataRowState.Detached:
+ break;
default:
// check what to do with child rows
CheckChildRows(DataRowAction.Delete);
@@ -1450,9 +1451,13 @@
targetColumn[row._proposed] =
val;
}
- //Saving the current value as the
column value
- object defaultVal = column
[IndexFromVersion (DataRowVersion.Default)];
- row [index] = defaultVal;
+ int srcIndex = IndexFromVersion
(DataRowVersion.Default);
+ if (srcIndex >= 0) {
+ //Saving the current value as
the column value
+ // FIXME: no idea why is this
required
+ object defaultVal = column
[srcIndex];
+ row [index] = defaultVal;
+ }
}
}
CopyState(row);
@@ -1526,6 +1531,37 @@
}
}
}
+
+ /// <summary>
+ /// Internal method to get a value of the record. This is
a utility
+ /// method to fetch a value without any restriction. The
only exception is
+ /// when the given index or record index is out of range.
That has to be
+ /// handled by the caller.
+ /// </summary>
+ internal object GetValue (int index, int record)
+ {
+ return Table.Columns [index] [record];
+ }
+
+ /// <summary>
+ /// Internal method to get a value of the record for a
given version.
+ /// This is a utility to fetch a value without any
restriction.
+ /// </summary>
+ /// <exception type="VersionNotFoundException">
+ /// if the row does not have version will throw exception
+ /// </exception>
+ internal object GetValue (int index, DataRowVersion version)
+ {
+ int record = IndexFromVersion (version); // returns -1
if version not found
+ if (record < 0)
+ throw new VersionNotFoundException
(String.Format ("This row does not have" +
+
" version {0}.",
+
version)
+ );
+ return GetValue (index, record);
+ }
+
+
#endregion // Methods
Modified: trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs 2005-05-11
04:44:52 UTC (rev 44374)
+++ trunk/mcs/class/System.Data/System.Data/UniqueConstraint.cs 2005-05-11
05:41:50 UTC (rev 44375)
@@ -476,7 +476,10 @@
if (IsPrimaryKey) {
object val;
for (int i = 0; i < _dataColumns.Length; i++) {
- val = row[_dataColumns[i]];
+ if (row.RowState ==
DataRowState.Deleted)
+ val = row.GetValue (i,
DataRowVersion.Original);
+ else
+ val = row.GetValue (i,
DataRowVersion.Default);
if (val == null || val == DBNull.Value)
throw new
NoNullAllowedException("Column '" + _dataColumns[i].ColumnName + "' does not
allow nulls.");
}
Modified: trunk/mcs/class/System.Data/Test/System.Data/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/ChangeLog 2005-05-11
04:44:52 UTC (rev 44374)
+++ trunk/mcs/class/System.Data/Test/System.Data/ChangeLog 2005-05-11
05:41:50 UTC (rev 44375)
@@ -1,3 +1,8 @@
+2005-05-11 Sureshkumar T <[EMAIL PROTECTED]>
+
+ * DataTableTest.cs: revamped tests for ImportRow method. Checks
+ for all rowstates and pk violation of importing a deleted record.
+
2005-05-05 Sureshkumar T <[EMAIL PROTECTED]>
* DataTableReaderTest.cs: Added a test to check when deleting the
Modified: trunk/mcs/class/System.Data/Test/System.Data/DataTableTest.cs
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/DataTableTest.cs
2005-05-11 04:44:52 UTC (rev 44374)
+++ trunk/mcs/class/System.Data/Test/System.Data/DataTableTest.cs
2005-05-11 05:41:50 UTC (rev 44375)
@@ -1076,42 +1076,73 @@
AssertEquals ("#A03", "Abc" , (table.Rows [0])
["Name"]);
AssertEquals ("#A04", 2, table.Rows.Count);
}
- [Test]
- public void ImportRow ()
- {
- DataTable table = new DataTable ();
- DataColumn col = new DataColumn ();
- col.ColumnName = "Id";
- col.DataType = Type.GetType ("System.Int32");
- table.Columns.Add (col);
+
+ [Test]
+ public void ImportRowTest ()
+ {
+ // build source table
+ DataTable src = new DataTable ();
+ src.Columns.Add ("id", typeof (int));
+ src.Columns.Add ("name", typeof (string));
- col = new DataColumn ();
- col.ColumnName = "Name";
- col.DataType = Type.GetType ("System.String");
- table.Columns.Add (col);
-
- DataRow row = table.NewRow ();
- row ["Id"] = 147;
- row ["name"] = "Abc";
- table.Rows.Add (row);
- table.AcceptChanges ();
-
- row = table.NewRow ();
- row ["Id"] = 47;
- row ["name"] = "Efg";
- table.Rows.Add (row);
+ src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
- (table.Rows [0]) ["Name"] = "AaBbCc";
-
- table.ImportRow (table.Rows [0]);
- table.ImportRow (table.Rows [1]);
+ src.Rows.Add (new object [] { 1, "mono 1" });
+ src.Rows.Add (new object [] { 2, "mono 2" });
+ src.Rows.Add (new object [] { 3, "mono 3" });
+ src.AcceptChanges ();
- AssertEquals ("#A01", 147, table.Rows [2]["Id"]);
- AssertEquals ("#A02", 47, table.Rows [3]["Id"]);
- AssertEquals ("#A03", DataRowState.Modified, table.Rows
[2].RowState);
- AssertEquals ("#A04", DataRowState.Added, table.Rows
[3].RowState);
- }
+ src.Rows [0] [1] = "mono changed 1"; // modify 1st row
+ src.Rows [1].Delete (); // delete 2nd row
+ // 3rd row is unchanged
+ src.Rows.Add (new object [] { 4, "mono 4" }); // add
4th row
+ // build target table
+ DataTable target = new DataTable ();
+ target.Columns.Add ("id", typeof (int));
+ target.Columns.Add ("name", typeof (string));
+
+ target.PrimaryKey = new DataColumn [] {target.Columns
[0]} ;
+
+ // import all rows
+ target.ImportRow (src.Rows [0]); // import 1st row
+ target.ImportRow (src.Rows [1]); // import 2nd row
+ target.ImportRow (src.Rows [2]); // import 3rd row
+ target.ImportRow (src.Rows [3]); // import 4th row
+
+ try {
+ target.ImportRow (src.Rows [2]); // import 3rd
row again
+ Fail ("#AA1 Should have thrown exception
violativ PK");
+ } catch (ConstraintException e) {}
+
+ // check row states
+ AssertEquals ("#A1", src.Rows [0].RowState,
target.Rows [0].RowState);
+ AssertEquals ("#A2", src.Rows [1].RowState,
target.Rows [1].RowState);
+ AssertEquals ("#A3", src.Rows [2].RowState,
target.Rows [2].RowState);
+ AssertEquals ("#A4", src.Rows [3].RowState,
target.Rows [3].RowState);
+
+ // check for modified row (1st row)
+ AssertEquals ("#B1", (string) src.Rows [0] [1],
(string) target.Rows [0] [1]);
+ AssertEquals ("#B2", (string) src.Rows [0] [1,
DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
+ AssertEquals ("#B3", (string) src.Rows [0] [1,
DataRowVersion.Original], (string) target.Rows [0] [1,
DataRowVersion.Original]);
+ AssertEquals ("#B4", (string) src.Rows [0] [1,
DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
+ AssertEquals ("#B5", false, target.Rows
[0].HasVersion(DataRowVersion.Proposed));
+
+ // check for deleted row (2nd row)
+ AssertEquals ("#C1", (string) src.Rows [1] [1,
DataRowVersion.Original], (string) target.Rows [1] [1,
DataRowVersion.Original]);
+
+ // check for unchanged row (3rd row)
+ AssertEquals ("#D1", (string) src.Rows [2] [1],
(string) target.Rows [2] [1]);
+ AssertEquals ("#D2", (string) src.Rows [2] [1,
DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
+ AssertEquals ("#D3", (string) src.Rows [2] [1,
DataRowVersion.Original], (string) target.Rows [2] [1,
DataRowVersion.Original]);
+ AssertEquals ("#D4", (string) src.Rows [2] [1,
DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
+
+ // check for newly added row (4th row)
+ AssertEquals ("#E1", (string) src.Rows [3] [1],
(string) target.Rows [3] [1]);
+ AssertEquals ("#E2", (string) src.Rows [3] [1,
DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
+ AssertEquals ("#E3", (string) src.Rows [3] [1,
DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
+ }
+
[Test]
public void ImportRowDetachedTest ()
{
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches