Переглянути джерело

2006-07-11 Senganal T <[email protected]>

	* Test/System.Data/schemas/test017.xsd : test if Relations and columns are
	created correctly if schema is nested but relation is not.

	* Test/System.Data/DataSetTest2.cs : Added tests for DataSet.WriteXmlSchema and 
	ReadXmlSchema methods to test nested schemas. 
	Also tests for bug #78810.
	
	* System.Data/XmlSchemaWriter.cs :
		- WriteTableType : If Relation.Nested is true, then nest the 
		table schemas. Correct the xml to add the table element within
		the <Sequence> element.
		- WriteChildRelations : Correct the Xml element name and
		attributes. Close the attribute correctly.
	* System.Data/XmlSchemaDataImporter.cs :
		- GenerateRelationship : When creating the relations, if
		ParentColumn exists but Corr ChildCol is null, create a child
		Column with the same name and type as the parent column.
		- Renamed DataSetDefinesPrimaryKey to DataSetDefinesKey
		- DataSetDefinesKey : if either xsd:key or xsd:keyref is
		defined for the dataset, do not manually create the relations.
		fixes bug #78810
		- AddParentKeyColumn : if Columnname already exists, use
		colname_0 instead of throwing an exception.
		- FillDataColumnRepeatedSimpleElement : Do not set the Unique
		value as it creates a constraint by default. The unique value
		is automatically set when pk is set.
		- CreateChildColumns : Added. Creates a ChildColumn duplicating 
		ParentColumn properties.


svn path=/trunk/mcs/; revision=62443
Senganal T 19 роки тому
батько
коміт
a6010f5dc3

+ 24 - 0
mcs/class/System.Data/System.Data/ChangeLog

@@ -1,3 +1,27 @@
+2006-06-30  Senganal T  <[email protected]>
+	
+	* XmlSchemaWriter.cs :
+		- WriteTableType : If Relation.Nested is true, then nest the 
+		table schemas. Correct the xml to add the table element within
+		the <Sequence> element.
+		- WriteChildRelations : Correct the Xml element name and
+		attributes. Close the attribute correctly.
+
+	* XmlSchemaDataImporter.cs :
+		- GenerateRelationship : When creating the relations, if
+		ParentColumn exists but Corr ChildCol is null, create a child
+		Column with the same name and type as the parent column.
+		- Renamed DataSetDefinesPrimaryKey to DataSetDefinesKey
+		- DataSetDefinesKey : if either xsd:key or xsd:keyref is
+		defined for the dataset, do not manually create the relations.
+		- AddParentKeyColumn : if Columnname already exists, use
+		colname_0 instead of throwing an exception.
+		- FillDataColumnRepeatedSimpleElement : Do not set the Unique
+		value as it creates a constraint by default. The unique value
+		is automatically set when pk is set.
+		- CreateChildColumns : Added. Creates a ChildColumn duplicating 
+		ParentColumn properties.
+
 2006-06-30  Senganal T  <[email protected]>
 
 	* DataRow.cs : 

+ 24 - 20
mcs/class/System.Data/System.Data/XmlSchemaDataImporter.cs

@@ -598,21 +598,17 @@ el.ElementType != schemaAnyType)
 					pcol [i] = ptab.Columns [XmlHelper.Decode (pcolnames [i])];
 
 				DataColumn[] ccol = new DataColumn [ccolnames.Length];
-				for (int i=0; i < ccol.Length; ++i)
+				for (int i=0; i < ccol.Length; ++i) {
 					ccol [i] = ctab.Columns [XmlHelper.Decode (ccolnames [i])];
-
+					if (ccol [i] == null)
+						ccol [i] = CreateChildColumn (pcol [i], ctab);
+				}
 				rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
 			} else {
 				DataColumn pcol = ptab.Columns [XmlHelper.Decode (rs.ParentColumnName)];
 				DataColumn ccol = ctab.Columns [XmlHelper.Decode (rs.ChildColumnName)];
-				if (ccol == null) {
-					ccol = new DataColumn ();
-					ccol.ColumnName = pcol.ColumnName;
-					ccol.Namespace = String.Empty; // don't copy
-					ccol.ColumnMapping = MappingType.Hidden;
-					ccol.DataType = pcol.DataType;
-					ctab.Columns.Add (ccol);
-				}
+				if (ccol == null) 
+					ccol = CreateChildColumn (pcol, ctab);
 				rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
 			}
 			rel.Nested = rs.IsNested;
@@ -621,6 +617,15 @@ el.ElementType != schemaAnyType)
 			return rel;
 		}
 
+		private DataColumn CreateChildColumn (DataColumn parentColumn, DataTable childTable)
+		{
+			DataColumn col = childTable.Columns.Add (parentColumn.ColumnName, 
+								parentColumn.DataType);
+			col.Namespace = String.Empty;
+			col.ColumnMapping = MappingType.Hidden;
+			return col;
+		}
+
 		private void ImportColumnGroupBase (XmlSchemaElement parent, XmlSchemaGroupBase gb)
 		{
 			foreach (XmlSchemaParticle p in gb.Items) {
@@ -739,17 +744,16 @@ el.ElementType != schemaAnyType)
 
 			if (el.Annotation != null)
 				HandleAnnotations (el.Annotation, true);
-			// If xsd:keyref for this table exists, then don't add
+			// If xsd:keyref xsd:key for this table exists, then don't add
 			// relation here manually.
-			else if (!DataSetDefinesPrimaryKey (elName)) {
+			else if (!DataSetDefinesKey (elName)) {
 				AddParentKeyColumn (parent, el, col);
-				DataColumn pkey = currentTable.PrimaryKey;
 
 				RelationStructure rel = new RelationStructure ();
 				rel.ParentTableName = XmlHelper.Decode (parent.QualifiedName.Name);
 				rel.ChildTableName = elName;
-				rel.ParentColumnName = pkey.ColumnName;
-				rel.ChildColumnName = pkey.ColumnName;
+				rel.ParentColumnName = col.ColumnName;
+				rel.ChildColumnName = col.ColumnName;
 				rel.CreateConstraint = true;
 				rel.IsNested = true;
 				relations.Add (rel);
@@ -761,10 +765,10 @@ el.ElementType != schemaAnyType)
 
 		}
 
-		private bool DataSetDefinesPrimaryKey (string name)
+		private bool DataSetDefinesKey (string name)
 		{
 			foreach (ConstraintStructure c in reservedConstraints.Values)
-				if (c.TableName == name && c.IsPrimaryKey)
+				if (c.TableName == name && (c.IsPrimaryKey || c.IsNested))
 					return true;
 			return false;
 		}
@@ -776,8 +780,9 @@ el.ElementType != schemaAnyType)
 
 			// check name identity
 			string name = XmlHelper.Decode (parent.QualifiedName.Name) + "_Id";
-			if (currentTable.ContainsColumn (name))
-				throw new DataException (String.Format ("There is already a column that has the same name: {0}", name));
+			int count = 0;
+			while (currentTable.ContainsColumn (name))
+				name = String.Format ("{0}_{1}", name, count++);
 			// check existing primary key
 			if (currentTable.Table.PrimaryKey.Length > 0)
 				throw new DataException (String.Format ("There is already primary key columns in the table \"{0}\".", currentTable.Table.TableName));
@@ -786,7 +791,6 @@ el.ElementType != schemaAnyType)
 			col.ColumnMapping = MappingType.Hidden;
 			col.Namespace = parent.QualifiedName.Namespace;
 			col.DataType = typeof (int);
-			col.Unique = true;
 			col.AutoIncrement = true;
 			col.AllowDBNull = false;
 

+ 11 - 10
mcs/class/System.Data/System.Data/XmlSchemaWriter.cs

@@ -539,15 +539,15 @@ namespace System.Data
 				if (elements.Count > 0) {
 					w.WriteStartElement ("xs", "sequence",
 						xmlnsxs);
+
 					foreach (DataColumn col in elements)
-						WriteTableTypeParticles (
-							col);
+						WriteTableTypeParticles (col);
+
+					foreach (DataRelation rel in table.ChildRelations)
+						if (rel.Nested)
+							WriteChildRelations (rel);
 					w.WriteEndElement ();
 				}
-
-				foreach (DataRelation rel in table.ChildRelations)
-					if (rel.Nested)
-						WriteChildRelations (rel);
 			}
 
 			w.WriteFullEndElement (); // complexType
@@ -649,17 +649,18 @@ namespace System.Data
 				w.WriteEndAttribute ();
 			} else {
 				w.WriteStartElement ("xs", "element", xmlnsxs);
-				w.WriteStartAttribute ("type", String.Empty);
+				w.WriteStartAttribute ("name", String.Empty);
 				w.WriteQualifiedName (
 					XmlHelper.Encode (rel.ChildTable.TableName),
 					rel.ChildTable.Namespace);
 				w.WriteEndAttribute ();
-				w.WriteEndElement ();
-
-				globalTypeTables.Add (rel.ChildTable);
 				w.WriteAttributeString ("minOccurs", "0");
 				w.WriteAttributeString ("maxOccurs", "unbounded");
+
+				globalTypeTables.Add (rel.ChildTable);
 			}
+			WriteTableType (rel.ChildTable);
+			w.WriteEndElement ();
 		}
 
 		private void WriteTableAttributes (ArrayList atts)

+ 6 - 0
mcs/class/System.Data/Test/System.Data/ChangeLog

@@ -1,3 +1,9 @@
+2006-06-30  Senganal T <[email protected]>
+	
+	* DataSetTest2.cs : Added tests for DataSet.WriteXmlSchema and 
+	ReadXmlSchema methods to test nested schemas. 
+	Also tests for bug #78810.
+
 2006-06-30  Senganal T <[email protected]>
 
 	* DataRowTest2.cs : Added tests for new 2.0 methods in DataRow

+ 38 - 0
mcs/class/System.Data/Test/System.Data/DataSetTest2.cs

@@ -3309,5 +3309,43 @@ namespace MonoTests_System.Data
 			//should not throw an exception
 			ds.WriteXmlSchema (sw);
 		}
+
+		[Test]
+		public void ReadWriteXmlSchema_Nested ()
+		{
+			DataSet ds = new DataSet ("dataset");
+			ds.Tables.Add ("table1");
+			ds.Tables.Add ("table2");
+			ds.Tables[0].Columns.Add ("col");
+			ds.Tables[1].Columns.Add ("col");
+			ds.Relations.Add ("rel", ds.Tables [0].Columns [0],ds.Tables [1].Columns [0], true);
+			ds.Relations [0].Nested = true;
+
+			MemoryStream ms = new MemoryStream ();
+			ds.WriteXmlSchema (ms);
+
+			DataSet ds1 = new DataSet ();
+			ds1.ReadXmlSchema (new MemoryStream (ms.GetBuffer ()));
+
+			// no new relation, and <table>_Id columns, should get created when 
+			// Relation.Nested = true
+			Assert.AreEqual (1, ds1.Relations.Count, "#1");
+			Assert.AreEqual (1, ds1.Tables [0].Columns.Count, "#2");
+			Assert.AreEqual (1, ds1.Tables [1].Columns.Count, "#3");
+		}
+
+		[Test]
+		public void ReadXmlSchema_Nested ()
+		{
+			//when Relation.Nested = false, and the schema is nested, create new relations on <table>_Id
+			//columns.
+			DataSet ds = new DataSet ();
+			ds.ReadXmlSchema ("Test/System.Data/schemas/test017.xsd");
+			Assert.AreEqual (2, ds.Relations.Count, "#1");
+			Assert.AreEqual (3, ds.Tables [0].Columns.Count, "#2");
+			Assert.AreEqual (3, ds.Tables [1].Columns.Count, "#3");
+			Assert.AreEqual ("table1_Id_0", ds.Tables [0].Columns [2].ColumnName, "#4");
+			Assert.AreEqual ("table1_Id_0", ds.Tables [0].PrimaryKey [0].ColumnName, "#5");
+		}
 	}
 }

+ 5 - 0
mcs/class/System.Data/Test/System.Data/schemas/ChangeLog

@@ -1,3 +1,8 @@
+2006-07-11  Senganal T <[email protected]>
+
+	* test017.xsd : test if Relations and columns are
+	created correctly if schema is nested but relation is not.
+
 2006-01-16  Senganal T <[email protected]>
 
 	* test015.xsd, test016.xsd : added 

+ 33 - 0
mcs/class/System.Data/Test/System.Data/schemas/test017.xsd

@@ -0,0 +1,33 @@
+<?xml version="1.0" standalone="yes"?>
+<xs:schema id="dataset" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+  <xs:element name="dataset" msdata:IsDataSet="true" msdata:Locale="en-US">
+    <xs:complexType>
+      <xs:choice maxOccurs="unbounded">
+        <xs:element name="table1">
+          <xs:complexType>
+            <xs:sequence>
+              <xs:element name="col" type="xs:string" minOccurs="0" />
+              <xs:element name="table1_Id" type="xs:string" minOccurs="0" />
+              <xs:element name="table2" minOccurs="0" maxOccurs="unbounded">
+                <xs:complexType>
+                  <xs:sequence>
+                    <xs:element name="col" type="xs:string" minOccurs="0" />
+		    <xs:element name="table1_Id" type="xs:string" minOccurs="0" />
+                  </xs:sequence>
+                </xs:complexType>
+              </xs:element>
+            </xs:sequence>
+          </xs:complexType>
+        </xs:element>
+      </xs:choice>
+    </xs:complexType>
+    <xs:unique name="Constraint1">
+      <xs:selector xpath=".//table1" />
+      <xs:field xpath="col" />
+    </xs:unique>
+    <xs:keyref name="rel" refer="Constraint1" msdata:IsNested="false">
+      <xs:selector xpath=".//table2" />
+      <xs:field xpath="col" />
+    </xs:keyref>
+  </xs:element>
+</xs:schema>