2004-08-05 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Thu, 5 Aug 2004 11:18:12 +0000 (11:18 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Thu, 5 Aug 2004 11:18:12 +0000 (11:18 -0000)
* XmlConstants.cs : Added constants for "msprop" support.
* DataSet.cs : ExtendedProperties should be written in the schema.
  This fixes bug #61233.

svn path=/trunk/mcs/; revision=31919

mcs/class/System.Data/System.Data/ChangeLog
mcs/class/System.Data/System.Data/DataSet.cs
mcs/class/System.Data/System.Data/XmlConstants.cs

index 19a97068767a4d994285ec48dd77159d7f106d65..650f33d85c66eba3e5dc1772f4127aed520d8ba7 100644 (file)
@@ -1,3 +1,9 @@
+2004-08-05  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlConstants.cs : Added constants for "msprop" support.
+       * DataSet.cs : ExtendedProperties should be written in the schema.
+         This fixes bug #61233.
+
 2004-07-27  Atsushi Enomoto  <atsushi@ximian.com>
 
        * DataSet.cs : on serialization to XmlWriter, XmlConvert should be
index 49ae0f024f96056d5ef7b4fdcc4429ff015bba5e..24faf3d700ddd8a0c85b71354175c7f5a40cbcba 100644 (file)
@@ -1390,6 +1390,8 @@ namespace System.Data {
                                nsmgr.AddNamespace (XmlConstants.TnsPrefix, Namespace);
                                nsmgr.AddNamespace (String.Empty, Namespace);
                        }
+                       if (CheckExtendedPropertyExists ())
+                               nsmgr.AddNamespace (XmlConstants.MspropPrefix, XmlConstants.MspropNamespace);
 
                        if (atts.Count > 0)
                                schema.UnhandledAttributes = atts.ToArray (typeof (XmlAttribute)) as XmlAttribute [];
@@ -1414,6 +1416,13 @@ namespace System.Data {
                        attr.Value = locale.Name;
                        atts.Add (attr);
 
+                       // Extended properties
+                       foreach (DictionaryEntry de in ExtendedProperties) {
+                               attr = doc.CreateAttribute (XmlConstants.MspropPrefix, de.Key.ToString (), XmlConstants.MspropNamespace);
+                               attr.Value = de.Value != null ? de.Value.ToString () : String.Empty;
+                               atts.Add (attr);
+                       }
+
                        elem.UnhandledAttributes = atts.ToArray (typeof (XmlAttribute)) as XmlAttribute [];
 
                        XmlSchemaComplexType complex = new XmlSchemaComplexType ();
@@ -1446,7 +1455,7 @@ namespace System.Data {
 
                        schema.Items.Add (elem);
                        
-                       AddConstraintsToSchema (elem, constraintPrefix, tables, relations);
+                       AddConstraintsToSchema (elem, constraintPrefix, tables, relations, doc);
                        foreach (string prefix in nsmgr) {
                                string ns = nsmgr.LookupNamespace (nsmgr.NameTable.Get (prefix));
                                if (prefix != "xmlns" && prefix != "xml" && ns != null && ns != String.Empty)
@@ -1454,30 +1463,49 @@ namespace System.Data {
                        }
                        return schema;
                }
-               
+
+               private bool CheckExtendedPropertyExists ()
+               {
+                       if (ExtendedProperties.Count > 0)
+                               return true;
+                       foreach (DataTable dt in Tables) {
+                               if (dt.ExtendedProperties.Count > 0)
+                                       return true;
+                               foreach (DataColumn col in dt.Columns)
+                                       if (col.ExtendedProperties.Count > 0)
+                                               return true;
+                               foreach (Constraint c in dt.Constraints)
+                                       if (c.ExtendedProperties.Count > 0)
+                                               return true;
+                       }
+                       foreach (DataRelation rel in Relations)
+                               if (rel.ExtendedProperties.Count > 0)
+                                       return true;
+                       return false;
+               }
+
                // Add all constraints in all tables to the schema.
-               private void AddConstraintsToSchema (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables, DataRelationCollection relations) 
+               private void AddConstraintsToSchema (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables, DataRelationCollection relations, XmlDocument doc
                {
                        // first add all unique constraints.
-                       Hashtable uniqueNames = AddUniqueConstraints (elem, constraintPrefix, tables);
+                       Hashtable uniqueNames = AddUniqueConstraints (elem, constraintPrefix, tables, doc);
                        // Add all foriegn key constraints.
-                       AddForeignKeys (uniqueNames, elem, constraintPrefix, relations);
+                       AddForeignKeys (uniqueNames, elem, constraintPrefix, relations, doc);
                }
                
                // Add unique constaraints to the schema.
                // return hashtable with the names of all XmlSchemaUnique elements we created.
-               private Hashtable AddUniqueConstraints (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables)
+               private Hashtable AddUniqueConstraints (XmlSchemaElement elem, string constraintPrefix, DataTableCollection tables, XmlDocument doc)
                {
-                       XmlDocument doc = new XmlDocument();
                        Hashtable uniqueNames = new Hashtable();
                        foreach (DataTable table in tables) {
                                
-                               foreach (Constraint constaint in table.Constraints) {
+                               foreach (Constraint constraint in table.Constraints) {
                                        
-                                       if (constaint is UniqueConstraint) {
+                                       if (constraint is UniqueConstraint) {
                                                ArrayList attrs = new ArrayList ();
                                                XmlAttribute attrib;
-                                               UniqueConstraint uqConst = (UniqueConstraint)constaint;
+                                               UniqueConstraint uqConst = (UniqueConstraint) constraint;
                                                XmlSchemaUnique uniq = new XmlSchemaUnique ();
                                                
                                                // if column of the constraint is hidden do not write the constraint.
@@ -1520,6 +1548,8 @@ namespace System.Data {
                                                        uniq.Fields.Add(field);
                                                }
                                
+                                               AddExtendedPropertyAttributes (uniq, constraint.ExtendedProperties, doc);
+
                                                elem.Constraints.Add (uniq);
                                                uniqueNames.Add (uniq.Name, null);
                                        }
@@ -1529,11 +1559,10 @@ namespace System.Data {
                }
                
                // Add the foriegn keys to the schema.
-               private void AddForeignKeys (Hashtable uniqueNames, XmlSchemaElement elem, string constraintPrefix, DataRelationCollection relations)
+               private void AddForeignKeys (Hashtable uniqueNames, XmlSchemaElement elem, string constraintPrefix, DataRelationCollection relations, XmlDocument doc)
                {
                        if (relations == null) return;
                        
-                       XmlDocument doc = new XmlDocument();
                        foreach (DataRelation rel in relations) {
                                
                                if (rel.ParentKeyConstraint == null || rel.ChildKeyConstraint == null)
@@ -1585,7 +1614,10 @@ namespace System.Data {
                                        field.XPath = constraintPrefix+column.ColumnName;
                                        keyRef.Fields.Add(field);
                                }
+
                                keyRef.UnhandledAttributes = (XmlAttribute[])attrs.ToArray (typeof (XmlAttribute));
+                               AddExtendedPropertyAttributes (keyRef, rel.ExtendedProperties, doc);
+
                                elem.Constraints.Add (keyRef);
                        }
                }
@@ -1687,6 +1719,7 @@ namespace System.Data {
                                        }
                                        
                                        colElem.UnhandledAttributes = (XmlAttribute[])xattrs.ToArray(typeof (XmlAttribute));
+                                       AddExtendedPropertyAttributes (colElem, col.ExtendedProperties, doc);
                                        seq.Items.Add (colElem);
                                }
 
@@ -1726,12 +1759,33 @@ namespace System.Data {
                                        schemaToAdd.Namespaces.Add (prefix, col.Namespace);
                                }
                                att.SchemaTypeName = MapType (col.DataType);
+                               // FIXME: what happens if extended properties are set on attribute columns??
                                schemaAttributes.Add (att);
                        }
 
+                       AddExtendedPropertyAttributes (elem, table.ExtendedProperties, doc);
+
                        return elem;
                }
 
+               private void AddExtendedPropertyAttributes (XmlSchemaAnnotated xsobj, PropertyCollection props, XmlDocument doc)
+               {
+                       ArrayList attList = new ArrayList ();
+                       XmlAttribute xmlAttr;
+
+                       if (xsobj.UnhandledAttributes != null)
+                               attList.AddRange (xsobj.UnhandledAttributes);
+
+                       // add extended properties to xs:element
+                       foreach (DictionaryEntry de in props) {
+                               xmlAttr = doc.CreateAttribute (XmlConstants.MspropPrefix, XmlConvert.EncodeName (de.Key.ToString ()), XmlConstants.MspropNamespace);
+                               xmlAttr.Value = de.Value != null ? WriteObjectXml (de.Value) : String.Empty;
+                               attList.Add (xmlAttr);
+                       }
+                       if (attList.Count > 0)
+                               xsobj.UnhandledAttributes = attList.ToArray (typeof (XmlAttribute)) as XmlAttribute [];
+               }
+
                private string SafeNS (string ns)
                {
                        return ns != null ? ns : String.Empty;
index b50a80bc6d5702835e56a685c0f0dbe806c64c49..1d4591576069116041e4469b30d9297bf48950c5 100755 (executable)
@@ -83,6 +83,8 @@ internal class XmlConstants
        //ms schema objects     
        public const string MsdataPrefix = "msdata";    
        public const string MsdataNamespace = "urn:schemas-microsoft-com:xml-msdata";
+       public const string MspropPrefix = "msprop";    
+       public const string MspropNamespace = "urn:schemas-microsoft-com:xml-msprop";
        public const string DiffgrPrefix = "diffgr";
        public const string DiffgrNamespace = "urn:schemas-microsoft-com:xml-diffgram-v1";
        public const string TnsPrefix = "mstns";