Author: senga
Date: 2006-03-20 05:01:09 -0500 (Mon, 20 Mar 2006)
New Revision: 58177
Modified:
trunk/mcs/class/System.Data/System.Data/ChangeLog
trunk/mcs/class/System.Data/System.Data/ConstraintCollection.cs
trunk/mcs/class/System.Data/System.Data/DataSet.cs
trunk/mcs/class/System.Data/System.Data/MergeManager.cs
trunk/mcs/class/System.Data/Test/System.Data/ChangeLog
trunk/mcs/class/System.Data/Test/System.Data/ConstraintCollectionTest2.cs
trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs
Log:
2006-03-20 Senganal T <[EMAIL PROTECTED]>
* Test/System.Data/DataSetTest2.cs
- Added testcases for issues in Merge,Copy
* Test/System.Data/ConstraintCollectionTest2.cs
- Added testcases for issues in Add , IndexOf
* System.Data/DataSet.cs :
- CopyRelations () : When copying the relation, do not add
constraints
automatically for the relation.
- OnMergeFailed () : If merge fails and no handler is set for
the MergeFailedEvent,
then throw DataException
* System.Data/ConstraintCollection.cs :
- Add () : Check if a constraint already exists on the Columns
that are being
constrained.
- IndexOf () : Do not check for equivalent constraints, check
for the exact instance.
* System.Data/MergeManager.cs ():
- AdjustSchema () :
- Handle the MissingSchemaAction.Error case,
- Do not create constraints when mergin the DataRelation
- Follow the semantics for schema merge/validation
when constraints are already present
in the target DataSet.
- AdjustPrimaryKeys () :
- Code cleanup
- Verify Order of Columns in the PrimaryKey when
merging.
- ResolveColumns : Changed the arument to DataSet. Simplifies
code elsewhere.
- CompareColumnArrays, CompareColumnArrays : Helper funtions.
Modified: trunk/mcs/class/System.Data/System.Data/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/System.Data/ChangeLog 2006-03-20 09:30:47 UTC
(rev 58176)
+++ trunk/mcs/class/System.Data/System.Data/ChangeLog 2006-03-20 10:01:09 UTC
(rev 58177)
@@ -1,3 +1,26 @@
+2006-03-20 Senganal T <[EMAIL PROTECTED]>
+
+ * DataSet.cs :
+ - CopyRelations () : When copying the relation, do not add
constraints
+ automatically for the relation.
+ - OnMergeFailed () : If merge fails and no handler is set for
the MergeFailedEvent,
+ then throw DataException
+ * ConstraintCollection.cs :
+ - Add () : Check if a constraint already exists on the Columns
that are being
+ constrained.
+ - IndexOf () : Do not check for equivalent constraints, check
for the exact instance.
+ * MergeManager.cs ():
+ - AdjustSchema () :
+ - Handle the MissingSchemaAction.Error case,
+ - Do not create constraints when mergin the DataRelation
+ - Follow the semantics for schema merge/validation
when constraints are already present
+ in the target DataSet.
+ - AdjustPrimaryKeys () :
+ - Code cleanup
+ - Verify Order of Columns in the PrimaryKey when
merging.
+ - ResolveColumns : Changed the arument to DataSet. Simplifies
code elsewhere.
+ - CompareColumnArrays, CompareColumnArrays : Helper funtions.
+
2006-03-09 Senganal T <[EMAIL PROTECTED]>
* DataRow.cs :
Modified: trunk/mcs/class/System.Data/System.Data/ConstraintCollection.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/ConstraintCollection.cs
2006-03-20 09:30:47 UTC (rev 58176)
+++ trunk/mcs/class/System.Data/System.Data/ConstraintCollection.cs
2006-03-20 10:01:09 UTC (rev 58177)
@@ -172,6 +172,14 @@
if (null != constraint.ConstraintCollection)
throw new ArgumentException("Constraint already
belongs to another collection.");
+ //check if a constraint already exists for the
datacolums
+ foreach (Constraint c in this) {
+ if (!c.Equals (constraint))
+ continue;
+ throw new DataException ("Constraint matches
contraint named '" + c.ConstraintName
+ + "' already in
collection");
+ }
+
//check for duplicate name
if
(_isDuplicateConstraintName(constraint.ConstraintName,null) )
throw new DuplicateNameException("Constraint
name already exists.");
@@ -329,7 +337,13 @@
public int IndexOf(Constraint constraint)
{
- return List.IndexOf(constraint);
+ int index = 0;
+ foreach (Constraint c in this) {
+ if (c == constraint)
+ return index;
+ index++;
+ }
+ return -1;
}
public
Modified: trunk/mcs/class/System.Data/System.Data/DataSet.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/DataSet.cs 2006-03-20 09:30:47 UTC
(rev 58176)
+++ trunk/mcs/class/System.Data/System.Data/DataSet.cs 2006-03-20 10:01:09 UTC
(rev 58177)
@@ -506,7 +506,7 @@
i++;
}
- DataRelation cRel = new DataRelation
(MyRelation.RelationName, P_DC, C_DC);
+ DataRelation cRel = new DataRelation
(MyRelation.RelationName, P_DC, C_DC, false);
Copy.Relations.Add (cRel);
}
@@ -1266,6 +1266,8 @@
{
if (MergeFailed != null)
MergeFailed (this, e);
+ else
+ throw new DataException (e.Conflict);
}
[MonoTODO]
Modified: trunk/mcs/class/System.Data/System.Data/MergeManager.cs
===================================================================
--- trunk/mcs/class/System.Data/System.Data/MergeManager.cs 2006-03-20
09:30:47 UTC (rev 58176)
+++ trunk/mcs/class/System.Data/System.Data/MergeManager.cs 2006-03-20
10:01:09 UTC (rev 58177)
@@ -40,6 +40,7 @@
bool prevEC = targetSet.EnforceConstraints;
targetSet.EnforceConstraints = false;
+
foreach (DataTable t in sourceSet.Tables)
MergeManager.Merge(targetSet, t,
preserveChanges, missingSchemaAction);
@@ -154,78 +155,98 @@
// return false if adjusting fails.
private static bool AdjustSchema(DataSet targetSet, DataSet
sourceSet, MissingSchemaAction missingSchemaAction)
{
- if (missingSchemaAction == MissingSchemaAction.Add ||
missingSchemaAction == MissingSchemaAction.AddWithKey) {
- foreach (DataRelation relation in
sourceSet.Relations) {
- // TODO : add more precise condition
(columns)
- if
(!targetSet.Relations.Contains(relation.RelationName)) {
- DataTable targetTable =
targetSet.Tables[relation.ParentColumns[0].Table.TableName];
- DataColumn[] parentColumns =
ResolveColumns(sourceSet,targetTable,relation.ParentColumns);
- targetTable =
targetSet.Tables[relation.ChildColumns[0].Table.TableName];
- DataColumn[] childColumns =
ResolveColumns(sourceSet,targetTable,relation.ChildColumns);
- if (parentColumns != null &&
childColumns != null) {
- DataRelation
newRelation = new
DataRelation(relation.RelationName,parentColumns,childColumns);
- newRelation.Nested =
relation.Nested;
-
targetSet.Relations.Add(newRelation);
- }
+ if (missingSchemaAction == MissingSchemaAction.Ignore)
+ return true;
+
+ foreach(DataTable sourceTable in sourceSet.Tables) {
+
+ DataTable targetTable =
targetSet.Tables[sourceTable.TableName];
+ if (targetTable == null)
+ continue;
+
+ foreach (Constraint constraint in
sourceTable.Constraints) {
+
+ Constraint targetConstraint = null;
+
+ string constraintName =
constraint.ConstraintName;
+ if (targetTable.Constraints.Contains
(constraintName))
+ constraintName = "";
+
+ UniqueConstraint uc = constraint as
UniqueConstraint;
+ // PrimaryKey is already taken care of
while merging the table
+ // ForeignKey constraint takes care of
Parent Unique Constraints
+ if (uc != null) {
+ if (uc.IsPrimaryKey ||
uc.ChildConstraint != null)
+ continue;
+ DataColumn[] columns =
ResolveColumns (targetSet, uc.Columns);
+ targetConstraint = new
UniqueConstraint (constraintName, columns, false);
}
- else {
- // TODO : should we throw an
exeption ?
+
+ ForeignKeyConstraint fc = constraint as
ForeignKeyConstraint;
+ if (fc != null) {
+ DataColumn[] columns =
ResolveColumns (targetSet, fc.Columns);
+ DataColumn[] relatedColumns =
ResolveColumns (targetSet, fc.RelatedColumns);
+ targetConstraint = new
ForeignKeyConstraint (constraintName, relatedColumns, columns);
}
- }
- foreach(DataTable sourceTable in
sourceSet.Tables) {
- DataTable targetTable =
targetSet.Tables[sourceTable.TableName];
+ bool dupConstraintFound = false;
+ foreach (Constraint cons in
targetTable.Constraints) {
+ if (!targetConstraint.Equals
(cons))
+ continue;
+ dupConstraintFound = true;
+ break;
+ }
- if (targetTable != null) {
- foreach(Constraint constraint
in sourceTable.Constraints) {
+ // If equivalent-constraint already
exists, then just do nothing
+ if (dupConstraintFound)
+ continue;
- if (constraint is
UniqueConstraint) {
-
UniqueConstraint uc = (UniqueConstraint)constraint;
- // FIXME : add
more precise condition (columns)
- if (
!targetTable.Constraints.Contains(uc.ConstraintName) ) {
-
DataColumn[] columns = ResolveColumns(sourceSet,targetTable,uc.Columns);
- if
(columns != null) {
-
UniqueConstraint newConstraint = new
UniqueConstraint(uc.ConstraintName,columns,uc.IsPrimaryKey);
-
targetTable.Constraints.Add(newConstraint);
- }
- }
- else {
- //
FIXME : should we throw an exception ?
- }
- }
- else {
-
ForeignKeyConstraint fc = (ForeignKeyConstraint)constraint;
- // FIXME : add
more precise condition (columns)
- if
(!targetTable.Constraints.Contains(fc.ConstraintName)) {
-
DataColumn[] columns = ResolveColumns(sourceSet,targetTable,fc.Columns);
-
DataTable relatedTable = targetSet.Tables[fc.RelatedTable.TableName];
-
DataColumn[] relatedColumns =
ResolveColumns(sourceSet,relatedTable,fc.RelatedColumns);
- if
(columns != null && relatedColumns != null) {
-
ForeignKeyConstraint newConstraint = new
ForeignKeyConstraint(fc.ConstraintName,relatedColumns,columns);
-
targetTable.Constraints.Add(newConstraint);
- }
- }
- else {
- //
FIXME : should we throw an exception ?
- }
- }
- }
- }
+ if (missingSchemaAction ==
MissingSchemaAction.Error)
+ throw new DataException
("Target DataSet missing " + targetConstraint.GetType () +
+
targetConstraint.ConstraintName);
+ else
+ targetTable.Constraints.Add
(targetConstraint);
}
}
+ foreach (DataRelation relation in sourceSet.Relations) {
+ DataRelation targetRelation =
targetSet.Relations [relation.RelationName];
+ if (targetRelation == null) {
+ if (missingSchemaAction ==
MissingSchemaAction.Error)
+ throw new ArgumentException
("Target DataSet mising definition for " +
+
relation.RelationName);
+
+ DataColumn[] parentColumns =
ResolveColumns (targetSet, relation.ParentColumns);
+ DataColumn[] childColumns =
ResolveColumns (targetSet, relation.ChildColumns);
+ targetRelation =
targetSet.Relations.Add (relation.RelationName, parentColumns,
+
childColumns, false);
+ targetRelation.Nested = relation.Nested;
+ } else if (!CompareColumnArrays
(relation.ParentColumns, targetRelation.ParentColumns) ||
+ !CompareColumnArrays
(relation.ChildColumns, targetRelation.ChildColumns)) {
+ RaiseMergeFailedEvent (null, "Relation
" + relation.RelationName +
+ " cannot be merged, because
keys have mismatch columns.");
+ }
+ }
+
return true;
}
- private static DataColumn[] ResolveColumns(DataSet
targetSet,DataTable targetTable,DataColumn[] sourceColumns)
+ private static DataColumn[] ResolveColumns(DataSet targetSet,
DataColumn[] sourceColumns)
{
if (sourceColumns != null && sourceColumns.Length > 0) {
- // TODO : worth to check that all source colums
come from the same table
+ // lets just assume that all columns are from
the Same table
+ DataTable targetTable = targetSet.Tables
[sourceColumns[0].Table.TableName];
if (targetTable != null) {
int i=0;
DataColumn[] targetColumns = new
DataColumn[sourceColumns.Length];
+
+ DataColumn tmpCol ;
foreach(DataColumn sourceColumn in
sourceColumns) {
- targetColumns[i++] =
targetTable.Columns[sourceColumn.ColumnName];
+ tmpCol =
targetTable.Columns[sourceColumn.ColumnName];
+ if (tmpCol == null)
+ throw new DataException
("Column " + sourceColumn.ColumnName +
+ " does
not belong to table " + targetTable.TableName);
+ targetColumns [i++] = tmpCol;
}
return targetColumns;
}
@@ -233,64 +254,43 @@
return null;
}
-
+
// adjust the table schema according to the missingschemaaction
param.
// return false if adjusting fails.
private static bool AdjustSchema(DataSet targetSet, DataTable
sourceTable, MissingSchemaAction missingSchemaAction, ref DataTable newTable)
{
- string tableName = sourceTable.TableName;
-
// if the source table not exists in the target dataset
// we act according to the missingschemaaction param.
- int tmp = targetSet.Tables.IndexOf(tableName);
- // we need to check if it is equals names
- if (tmp != -1 &&
!targetSet.Tables[tmp].TableName.Equals(tableName))
- tmp = -1;
- if (tmp == -1) {
- if (missingSchemaAction ==
MissingSchemaAction.Ignore) {
+ DataTable targetTable = targetSet.Tables
[sourceTable.TableName];
+ if (targetTable == null) {
+ if (missingSchemaAction ==
MissingSchemaAction.Ignore)
return true;
- }
- if (missingSchemaAction ==
MissingSchemaAction.Error) {
- throw new ArgumentException("Target
DataSet missing definition for "+ tableName + ".");
- }
-
- DataTable cloneTable =
(DataTable)sourceTable.Clone();
- targetSet.Tables.Add(cloneTable);
- tableName = cloneTable.TableName;
- }
-
- DataTable table = targetSet.Tables[tableName];
-
+
+ if (missingSchemaAction ==
MissingSchemaAction.Error)
+ throw new ArgumentException ("Target
DataSet missing definition for " +
+ sourceTable.TableName +
".");
+
+ targetTable = (DataTable)sourceTable.Clone();
+ targetSet.Tables.Add(targetTable);
+ }
+
for (int i = 0; i < sourceTable.Columns.Count; i++) {
DataColumn sourceColumn =
sourceTable.Columns[i];
// if a column from the source table doesn't
exists in the target table
// we act according to the missingschemaaction
param.
- DataColumn targetColumn =
table.Columns[sourceColumn.ColumnName];
+ DataColumn targetColumn = targetTable.Columns
[sourceColumn.ColumnName];
if(targetColumn == null) {
- if (missingSchemaAction ==
MissingSchemaAction.Ignore) {
+ if (missingSchemaAction ==
MissingSchemaAction.Ignore)
continue;
- }
- if (missingSchemaAction ==
MissingSchemaAction.Error) {
- throw new
ArgumentException(("Column '" + sourceColumn.ColumnName + "' does not belong to
table Items."));
- }
+ if (missingSchemaAction ==
MissingSchemaAction.Error)
+ throw new DataException
("Target table " + targetTable.TableName +
+ " missing
definition for column " + sourceColumn.ColumnName);
- targetColumn = new
DataColumn(sourceColumn.ColumnName, sourceColumn.DataType,
sourceColumn.Expression, sourceColumn.ColumnMapping);
- table.Columns.Add(targetColumn);
+ targetColumn = new
DataColumn(sourceColumn.ColumnName, sourceColumn.DataType,
+
sourceColumn.Expression, sourceColumn.ColumnMapping);
+ targetTable.Columns.Add(targetColumn);
}
- if (sourceColumn.Unique) {
- try {
- targetColumn.Unique =
sourceColumn.Unique;
- }
- catch(Exception e){
-// Console.WriteLine("targetColumn
: {0} targetTable : {1} ",targetColumn.ColumnName,table.TableName);
- foreach(DataRow row in
table.Rows) {
-//
Console.WriteLine(row[targetColumn]);
- }
- throw e;
- }
- }
-
if(sourceColumn.AutoIncrement) {
targetColumn.AutoIncrement =
sourceColumn.AutoIncrement;
targetColumn.AutoIncrementSeed =
sourceColumn.AutoIncrementSeed;
@@ -298,88 +298,46 @@
}
}
- if (!AdjustPrimaryKeys(table, sourceTable)) {
+ if (!AdjustPrimaryKeys(targetTable, sourceTable))
return false;
- }
- newTable = table;
+ newTable = targetTable;
return true;
}
-
+
+
// find if there is a valid matching between the targetTable
PrimaryKey and the
// sourceTable primatyKey.
// return true if there is a match, else return false and raise
a MergeFailedEvent.
private static bool AdjustPrimaryKeys(DataTable targetTable,
DataTable sourceTable)
{
- // if the length of one of the tables primarykey if 0 -
there is nothing to check.
- if (sourceTable.PrimaryKey.Length != 0) {
- if (targetTable.PrimaryKey.Length == 0) {
- // if target table has no primary key
at all -
- // import primary key from source table
- DataColumn[] targetColumns = new
DataColumn[sourceTable.PrimaryKey.Length];
-
- for(int i=0; i <
sourceTable.PrimaryKey.Length; i++){
- DataColumn sourceColumn =
sourceTable.PrimaryKey[i];
- DataColumn targetColumn =
targetTable.Columns[sourceColumn.ColumnName];
+ if (sourceTable.PrimaryKey.Length == 0)
+ return true;
- if (targetColumn == null) {
- // is target table has
no column corresponding
- // to source table PK
column - merge fails
- string message =
"Column " + sourceColumn.ColumnName + " does not belongs to table " +
targetTable.TableName;
- MergeFailedEventArgs e
= new MergeFailedEventArgs(sourceTable, message);
-
targetTable.DataSet.OnMergeFailed(e);
- return false;
- }
- else {
- targetColumns[i] =
targetColumn;
- }
- }
- targetTable.PrimaryKey = targetColumns;
- }
- else {
- // if target table already has primary
key and
- // if the length of primarykey is not
equal - merge fails
- if (targetTable.PrimaryKey.Length !=
sourceTable.PrimaryKey.Length) {
- string message =
"<target>.PrimaryKey and <source>.PrimaryKey have different Length.";
- MergeFailedEventArgs e = new
MergeFailedEventArgs(sourceTable, message);
-
targetTable.DataSet.OnMergeFailed(e);
- return false;
- }
- else {
- // we have to see that each
primary column in the target table
- // has a column with the same
name in the sourcetable primarykey columns.
- bool foundMatch;
- DataColumn[] targetDataColumns
= targetTable.PrimaryKey;
- DataColumn[] srcDataColumns =
sourceTable.PrimaryKey;
-
- // loop on all primary key
columns in the targetTable.
- for (int i = 0; i <
targetDataColumns.Length; i++) {
- foundMatch = false;
- DataColumn col =
targetDataColumns[i];
-
- // find if there is a
column with the same name in the
- // sourceTable primary
key columns.
- for (int j = 0; j <
srcDataColumns.Length; j++) {
- if
(srcDataColumns[j].ColumnName == col.ColumnName) {
-
foundMatch = true;
- break;
- }
- }
- if (!foundMatch) {
- string message
= "Mismatch columns in the PrimaryKey : <target>." + col.ColumnName + " versus
<source>." + srcDataColumns[i].ColumnName + ".";
-
MergeFailedEventArgs e = new MergeFailedEventArgs(sourceTable, message);
-
targetTable.DataSet.OnMergeFailed(e);
- return false;
- }
-
- }
- }
- }
+ // If targetTable does not have a PrimaryKey, just
import the sourceTable PrimaryKey
+ if (targetTable.PrimaryKey.Length == 0) {
+ DataColumn[] targetColumns = ResolveColumns
(targetTable.DataSet, sourceTable.PrimaryKey);
+ targetTable.PrimaryKey = targetColumns;
+ return true;
}
+
+ // If both the tables have a primary key, verify that
they are equivalent.
+ // raise a MergeFailedEvent if the keys are not
equivalent
+ if (targetTable.PrimaryKey.Length !=
sourceTable.PrimaryKey.Length) {
+ RaiseMergeFailedEvent (targetTable,
"<target>.PrimaryKey and <source>.PrimaryKey have different Length.");
+ return false;
+ }
+ for (int i=0; i < targetTable.PrimaryKey.Length; ++i) {
+ if (targetTable.PrimaryKey
[i].ColumnName.Equals (sourceTable.PrimaryKey [i].ColumnName))
+ continue;
+ RaiseMergeFailedEvent (targetTable, "Mismatch
columns in the PrimaryKey : <target>." +
+ targetTable.PrimaryKey [i].ColumnName +
" versus <source>." + sourceTable.PrimaryKey [i].ColumnName);
+ return false;
+ }
return true;
}
-
+
// fill the data from the source table to the target table
private static void fillData(DataTable targetTable, DataTable
sourceTable, bool preserveChanges)
{
@@ -389,7 +347,7 @@
MergeRow(targetTable, row, preserveChanges);
}
}
-
+
// check tha column from 2 tables that has the same name also
has the same datatype.
private static void checkColumnTypes(DataTable targetTable,
DataTable sourceTable)
{
@@ -398,8 +356,27 @@
DataColumn fromCol = sourceTable.Columns[i];
DataColumn toCol =
targetTable.Columns[fromCol.ColumnName];
if((toCol != null) && (toCol.DataType !=
fromCol.DataType))
- throw new DataException("<target>." +
fromCol.ColumnName + " and <source>." + fromCol.ColumnName + " have conflicting
properties: DataType property mismatch.");
+ throw new DataException("<target>." +
fromCol.ColumnName + " and <source>." +
+ fromCol.ColumnName + "
have conflicting properties: DataType " +
+ " property mismatch.");
}
}
+
+ private static bool CompareColumnArrays (DataColumn[] arr1,
DataColumn[] arr2)
+ {
+ if (arr1.Length != arr2.Length)
+ return false;
+
+ for (int i=0; i < arr1.Length; ++i)
+ if (!arr1 [i].ColumnName.Equals (arr2
[i].ColumnName))
+ return false;
+ return true;
+ }
+
+ private static void RaiseMergeFailedEvent (DataTable
targetTable, string errMsg)
+ {
+ MergeFailedEventArgs args = new MergeFailedEventArgs
(targetTable, errMsg);
+ targetTable.DataSet.OnMergeFailed (args);
+ }
}
}
Modified: trunk/mcs/class/System.Data/Test/System.Data/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/ChangeLog 2006-03-20
09:30:47 UTC (rev 58176)
+++ trunk/mcs/class/System.Data/Test/System.Data/ChangeLog 2006-03-20
10:01:09 UTC (rev 58177)
@@ -1,3 +1,10 @@
+2006-03-20 Senganal T <[EMAIL PROTECTED]>
+
+ * DataSetTest2.cs
+ - Added testcases for issues in Merge,Copy
+ * ConstraintCollectionTest2.cs
+ - Added testcases for issues in Add , IndexOf
+
2006-03-19 Boris Kirzner <[EMAIL PROTECTED]>
* ConstraintCollectionTest2.cs, DataColumnCollectionTest.cs,
Modified:
trunk/mcs/class/System.Data/Test/System.Data/ConstraintCollectionTest2.cs
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/ConstraintCollectionTest2.cs
2006-03-20 09:30:47 UTC (rev 58176)
+++ trunk/mcs/class/System.Data/Test/System.Data/ConstraintCollectionTest2.cs
2006-03-20 10:01:09 UTC (rev 58177)
@@ -197,6 +197,64 @@
}
+ [Test] public void IndexOf_SameColumns ()
+ {
+ DataSet ds = new DataSet ();
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+ DataColumn pcol = table1.Columns.Add ("col1");
+ DataColumn ccol = table2.Columns.Add ("col1");
+
+ ds.Relations.Add ("fk_rel", pcol, ccol);
+
+ ForeignKeyConstraint fk = new ForeignKeyConstraint
("fk", pcol, ccol);
+ Assert.AreEqual (-1, ds.Tables [1].Constraints.IndexOf
(fk), "#1");
+ }
+
+ [Test]
+ public void Add_RelationFirst_ConstraintNext()
+ {
+ DataSet ds = new DataSet ();
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+ DataColumn pcol = table1.Columns.Add ("col1");
+ DataColumn ccol = table2.Columns.Add ("col1");
+
+ ds.Relations.Add ("fk_rel", pcol, ccol);
+
+ try {
+ table2.Constraints.Add ("fk_cons", pcol, ccol);
+ Assert.Fail ("#1 Cannot add duplicate fk
constraint");
+ }catch (DataException e) {
+ }
+
+ try {
+ table1.Constraints.Add ("pk_cons", pcol, false);
+ Assert.Fail ("#2 Cannot add duplicate unique
constraint");
+ }catch (DataException e) {
+ }
+ }
+
+ [Test]
+ public void Add_ConstraintFirst_RelationNext ()
+ {
+ DataSet ds = new DataSet ();
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+ DataColumn pcol = table1.Columns.Add ("col1");
+ DataColumn ccol = table2.Columns.Add ("col1");
+
+ table2.Constraints.Add ("fk_cons", pcol, ccol);
+
+ // Should not throw DataException
+ ds.Relations.Add ("fk_rel", pcol, ccol);
+
+ Assert.AreEqual (1, table2.Constraints.Count, "#1
duplicate constraint shudnt be added");
+ Assert.AreEqual (1, table1.Constraints.Count, "#2
duplicate constraint shudnt be added");
+ Assert.AreEqual ("fk_cons", table2.Constraints
[0].ConstraintName, "#3 shouldnt be overwritten");
+ Assert.AreEqual ("Constraint1", table1.Constraints
[0].ConstraintName, "#4 shouldnt be overwritten");
+ }
+
[Test] public void IsReadOnly()
{
DataTable dt = DataProvider.CreateUniqueConstraint();
Modified: trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs
===================================================================
--- trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs
2006-03-20 09:30:47 UTC (rev 58176)
+++ trunk/mcs/class/System.Data/Test/System.Data/DataSetTest2.cs
2006-03-20 10:01:09 UTC (rev 58177)
@@ -1418,6 +1418,193 @@
CompareResults_2("merge 3",ds,dsTarget1);
}
+ [Test]
+ public void Merge_RelationWithoutConstraints ()
+ {
+ DataSet ds = new DataSet ();
+
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+
+ DataColumn pcol = table1.Columns.Add ("col1", typeof
(int));
+ DataColumn ccol = table2.Columns.Add ("col1", typeof
(int));
+
+ DataSet ds1 = ds.Copy ();
+ DataRelation rel = ds1.Relations.Add ("rel1",
ds1.Tables[0].Columns[0],
+ ds1.Tables
[1].Columns [0], false);
+
+ ds.Merge (ds1);
+ Assert.AreEqual (1, ds.Relations.Count , "#1");
+ Assert.AreEqual (0, ds.Tables [0].Constraints.Count ,
"#2");
+ Assert.AreEqual (0, ds.Tables [1].Constraints.Count ,
"#2");
+ }
+
+ [Test]
+ public void Merge_DuplicateConstraints ()
+ {
+ DataSet ds = new DataSet ();
+
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+
+ DataColumn pcol = table1.Columns.Add ("col1", typeof
(int));
+ DataColumn ccol = table2.Columns.Add ("col1", typeof
(int));
+
+ DataSet ds1 = ds.Copy ();
+
+ DataRelation rel = ds.Relations.Add ("rel1", pcol,
ccol);
+
+ ds1.Tables [1].Constraints.Add ("fk", ds1.Tables
[0].Columns [0], ds1.Tables [1].Columns [0]);
+
+ // No Exceptions shud be thrown
+ ds.Merge (ds1);
+ Assert.AreEqual (1, table2.Constraints.Count, "#1
Constraints shudnt be duplicated");
+ }
+
+ [Test]
+ public void Merge_DuplicateConstraints_1 ()
+ {
+ DataSet ds = new DataSet ();
+
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+
+ DataColumn pcol = table1.Columns.Add ("col1", typeof
(int));
+ DataColumn ccol = table2.Columns.Add ("col1", typeof
(int));
+ DataColumn pcol1 = table1.Columns.Add ("col2", typeof
(int));
+ DataColumn ccol1 = table2.Columns.Add ("col2", typeof
(int));
+
+ DataSet ds1 = ds.Copy ();
+
+ table2.Constraints.Add ("fk", pcol, ccol);
+ ds1.Tables [1].Constraints.Add ("fk", ds1.Tables
[0].Columns ["col2"], ds1.Tables [1].Columns ["col2"]);
+
+ // No Exceptions shud be thrown
+ ds.Merge (ds1);
+ Assert.AreEqual (2, table2.Constraints.Count, "#1 fk
constraint shud be merged");
+ Assert.AreEqual ("Constraint1", table2.Constraints
[1].ConstraintName, "#2 constraint name shud be changed");
+ }
+
+ [Test]
+ public void CopyClone_RelationWithoutConstraints ()
+ {
+ DataSet ds = new DataSet ();
+
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+
+ DataColumn pcol = table1.Columns.Add ("col1", typeof
(int));
+ DataColumn ccol = table2.Columns.Add ("col1", typeof
(int));
+
+ DataRelation rel = ds.Relations.Add ("rel1", pcol,
ccol, false);
+
+ DataSet ds1 = ds.Copy ();
+ DataSet ds2 = ds.Clone ();
+
+ Assert.AreEqual (1, ds1.Relations.Count, "#1");
+ Assert.AreEqual (1, ds2.Relations.Count, "#2");
+
+ Assert.AreEqual (0, ds1.Tables [0].Constraints.Count,
"#3");
+ Assert.AreEqual (0, ds1.Tables [1].Constraints.Count,
"#4");
+
+ Assert.AreEqual (0, ds2.Tables [0].Constraints.Count,
"#5");
+ Assert.AreEqual (0, ds2.Tables [1].Constraints.Count,
"#6");
+ }
+
+ [Test]
+ [ExpectedException (typeof (DataException))]
+ public void Merge_MissingEventHandler ()
+ {
+ DataSet ds = new DataSet ();
+ DataTable table1 = ds.Tables.Add ("table1");
+
+ DataColumn pcol = table1.Columns.Add ("col1", typeof
(int));
+ DataColumn pcol1 = table1.Columns.Add ("col2", typeof
(int));
+
+ DataSet ds1 = ds.Copy ();
+ table1.PrimaryKey = new DataColumn[] {pcol};
+ ds1.Tables [0].PrimaryKey = new DataColumn[]
{ds1.Tables [0].Columns [1]};
+
+ // Exception shud be raised when handler is not set for
MergeFailed Event
+ ds1.Merge (ds);
+ }
+
+ [Test]
+ [ExpectedException (typeof(DataException))]
+ public void Merge_MissingColumn ()
+ {
+ DataSet ds = new DataSet ();
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+
+ table1.Columns.Add ("col1", typeof (int));
+ table2.Columns.Add ("col1", typeof (int));
+
+ DataSet ds1 = ds.Copy ();
+
+ ds1.Tables [0].Columns.Add ("col2");
+
+ ds.Merge (ds1, true, MissingSchemaAction.Error);
+ }
+
+ [Test]
+ public void Merge_MissingConstraint ()
+ {
+ DataSet ds = new DataSet ();
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+ table1.Columns.Add ("col1", typeof (int));
+ table2.Columns.Add ("col1", typeof (int));
+
+ try {
+ DataSet ds1 = ds.Copy ();
+ DataSet ds2 = ds.Copy ();
+ ds2.Tables [0].Constraints.Add ("uc",
ds2.Tables [0].Columns [0], false);
+ ds1.Merge (ds2, true,
MissingSchemaAction.Error);
+ Assert.Fail ("#1 If uniqueconstraint is
missing, exception shud be thrown");
+ }catch (DataException e) {
+ }
+
+ try {
+ DataSet ds1 = ds.Copy ();
+ DataSet ds2 = ds.Copy ();
+ ds2.Tables [0].Constraints.Add ("fk",
ds2.Tables [0].Columns [0], ds2.Tables[1].Columns [0]);
+ ds1.Tables [0].Constraints.Add ("uc",
ds1.Tables [0].Columns [0],false);
+ ds1.Merge (ds2, true,
MissingSchemaAction.Error);
+ Assert.Fail ("#2 If foreignkeyconstraint is
missing, exception shud be thrown");
+ }catch (DataException e) {
+ }
+
+ try {
+ DataSet ds1 = ds.Copy ();
+ DataSet ds2 = ds.Copy ();
+ ds2.Relations.Add ("rel", ds2.Tables
[0].Columns [0], ds2.Tables[1].Columns [0], false);
+ ds1.Merge (ds2, true,
MissingSchemaAction.Error);
+ Assert.Fail ("#2 If datarelation is missing,
exception shud be thrown");
+ }catch (ArgumentException e) {
+ }
+ }
+
+ [Test]
+ [ExpectedException (typeof (DataException))]
+ public void Merge_PrimaryKeys_IncorrectOrder ()
+ {
+ DataSet ds = new DataSet ();
+ DataTable table1 = ds.Tables.Add ("table1");
+ DataTable table2 = ds.Tables.Add ("table2");
+ DataColumn pcol = table1.Columns.Add ("col1", typeof
(int));
+ DataColumn pcol1 = table1.Columns.Add ("col2", typeof
(int));
+ DataColumn ccol = table2.Columns.Add ("col1", typeof
(int));
+
+ DataSet ds1 = ds.Copy ();
+ table1.PrimaryKey = new DataColumn[] {pcol,pcol1};
+ ds1.Tables [0].PrimaryKey = new DataColumn []
{ds1.Tables[0].Columns [1], ds1.Tables [0].Columns [0]};
+
+ // Though the key columns are the same, if the order is
incorrect
+ // Exception must be raised
+ ds1.Merge (ds);
+ }
+
void CompareResults_1(string Msg,DataSet ds, DataSet dsTarget)
{
// check Parent Primary key length
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches