2 // XmlSchemaDataImporter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
10 // ***** The design note became somewhat obsolete. Should be rewritten. *****
16 // This class is used to import an XML Schema into a DataSet schema.
18 // Only XmlReader is acceptable as the input to the class.
19 // This class is not expected to read XML Schema multi time.
21 // ** Targetable Schema Components
23 // Only global global elements that hold complex type are converted
26 // The components of the type of the element are subsequently converted
27 // into a table, BUT there is an exception. As for "DataSet elements",
28 // the type is just ignored (see "DataSet Element definition" below).
30 // The components of the type of the element are subsequently converted
31 // into a table. As for "DataSet elements", its complex type is also
35 // Unused complex types are never be converted.
37 // Global simple types and global attributes are never converted.
38 // They cannot be a table.
39 // Local complex types are also converted into a table.
41 // Local elements are converted into either a table or a column in
42 // the "context DataTable". Simple-typed element is not always converted
43 // into a DataColumn; if maxOccurs > 1, it will be converted as a table.
47 // Ignore this section. Microsoft.NET was buggy enough to confuse
48 // against these name conflicts.
50 // Since local complex types are anonymous, we have to name for each
51 // component. Thus, and since complex types and elements can have the
52 // same name each other, we have to manage a table for mappings from
53 // a name to a component. The names must be also used in DataRelation
54 // definitions correctly.
56 // ** DataSet element definition
58 // "DataSet element" is 1) such element that has an attribute
59 // msdata:IsDataSet (where prefix "msdata" is bound to
60 // urn:schemas-microsoft-com:xml-msdata), or 2) the only one
61 // element definition in the schema.
63 // There is another complicated rule. 1) If there is only one element EL
64 // in the schema, and 2) if the type of EL is complex named CT, and 3)
65 // the content of the CT is a group base, and 4) the group base contains
66 // an element EL2, and finally 5) if EL2 is complex, THEN the element is
67 // the DataSet element.
69 // Only the first global element that matches the condition above is
70 // regarded as DataSet element (by necessary design or just a bug?)
71 // instead of handling as an error.
73 // All global elements are considered as an alternative in the dataset
76 // For local elements, msdata:IsDataSet are just ignored.
78 // ** Importing Complex Types as Columns
80 // When an xs:element is going to be mapped, its complex type (remember
81 // that only complex-typed elements are targettable) are expanded to
84 // DataColumn has a property MappingType that shows whether this column
85 // came from attribute or element.
87 // [Question: How about MappingType.Simple? How is it used?]
89 // Additionally, for particle elements, it might also create another
90 // DataTable (but for the particle elements in context DataTable, it
91 // will create an index to the new table).
93 // For group base particles (XmlSchemaGroupBase; sequence, choice, all)
94 // each component in those groups are mapped to a column. Even if you
95 // import "choice" or "all" components, DataSet.WriteXmlSchema() will
96 // output them just as a "sequence".
98 // Columns cannot be added directly to current context DataTable; they
99 // need to be added after processing all the columns, because they may
100 // have msdata:Ordinal attribute that specifies the order of the columns
103 // "Nested elements" are not allowed. (Clarification required?)
105 // ** Identity Constraints and DataRelations
107 // *** DataRelations from element identity constraints
109 // Only constraints on "DataSet element" is considered. All other
110 // constraint definitions are ignored. Note that it is DataSet that has
111 // the property Relations (of type DataRelationCollection).
113 // xs:key and xs:unique are handled as the same (then both will be
114 // serialized as xs:unique).
116 // The XPath expressions in the constraints are strictly limited; they
117 // are expected to be expandable enough to be mappable for each
119 // * selector to "any_valid_XPath/is/OK/blah"
120 // where "blah" is one of the DataTable name. It looks that
121 // only the last QName section is significant and any heading
122 // XPath step is OK (even if the mapped node does not exist).
123 // * field to QName that is mapped to DataColumn in the DataTable
124 // (even ./QName is not allowed)
126 // *** DataRelations from annotations
128 // See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/_mapping_relationship_specified_for_nested_elements.asp and http://msdn.microsoft.com/library/en-us/cpguide/html/_specifying_relationship_between_elements_with_no_nesting.asp
130 // ** Informative References
132 // Generating DataSet Relational Structure from XML Schema (XSD)
133 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/_generating_dataset_relational_structure_from_xsd.asp
137 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
139 // Permission is hereby granted, free of charge, to any person obtaining
140 // a copy of this software and associated documentation files (the
141 // "Software"), to deal in the Software without restriction, including
142 // without limitation the rights to use, copy, modify, merge, publish,
143 // distribute, sublicense, and/or sell copies of the Software, and to
144 // permit persons to whom the Software is furnished to do so, subject to
145 // the following conditions:
147 // The above copyright notice and this permission notice shall be
148 // included in all copies or substantial portions of the Software.
150 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
151 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
152 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
153 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
154 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
155 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
156 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
160 using System.Collections;
162 using System.Globalization;
164 using System.Xml.Schema;
167 namespace System.Data
169 internal class TableStructureCollection : CollectionBase
171 public void Add (TableStructure table)
176 public TableStructure this [int i] {
177 get { return List [i] as TableStructure; }
180 public TableStructure this [string name] {
182 foreach (TableStructure ts in List)
183 if (ts.Table.TableName == name)
190 internal class RelationStructureCollection : CollectionBase
192 public void Add (RelationStructure rel)
197 public RelationStructure this [int i] {
198 get { return List [i] as RelationStructure; }
201 public RelationStructure this [string parent, string child] {
203 foreach (RelationStructure rel in List)
204 if (rel.ParentTableName == parent && rel.ChildTableName == child)
211 internal class TableStructure
213 public TableStructure (DataTable table)
218 // The columns and orders which will be added to the context
219 // table (See design notes; Because of the ordinal problem)
220 public DataTable Table;
221 public Hashtable OrdinalColumns = new Hashtable ();
222 public ArrayList NonOrdinalColumns = new ArrayList ();
223 public DataColumn PrimaryKey;
225 public bool ContainsColumn (string name)
227 foreach (DataColumn col in NonOrdinalColumns)
228 if (col.ColumnName == name)
230 foreach (DataColumn col in OrdinalColumns.Keys)
231 if (col.ColumnName == name)
237 internal class RelationStructure
239 public string ExplicitName;
240 public string ParentTableName;
241 public string ChildTableName;
242 public string ParentColumnName;
243 public string ChildColumnName;
244 public bool IsNested;
245 public bool CreateConstraint;
248 internal class ConstraintStructure
250 public readonly string TableName;
251 public readonly string [] Columns;
252 public readonly bool [] IsAttribute;
253 public readonly string ConstraintName;
254 public readonly bool IsPrimaryKey;
255 public readonly string ReferName;
256 public readonly bool IsNested;
257 public readonly bool IsConstraintOnly;
259 public ConstraintStructure (string tname, string [] cols, bool [] isAttr, string cname, bool isPK, string refName, bool isNested, bool isConstraintOnly)
263 IsAttribute = isAttr;
264 ConstraintName = XmlHelper.Decode (cname);
268 IsConstraintOnly = isConstraintOnly;
272 internal class XmlSchemaDataImporter
274 static readonly XmlSchemaDatatype schemaIntegerType;
275 static readonly XmlSchemaDatatype schemaDecimalType;
276 static readonly XmlSchemaComplexType schemaAnyType;
278 static XmlSchemaDataImporter ()
280 XmlSchema s = new XmlSchema ();
281 XmlSchemaAttribute a = new XmlSchemaAttribute ();
283 a.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
285 XmlSchemaAttribute b = new XmlSchemaAttribute ();
287 b.SchemaTypeName = new XmlQualifiedName ("decimal", XmlSchema.Namespace);
289 XmlSchemaElement e = new XmlSchemaElement ();
294 schemaIntegerType = ((XmlSchemaSimpleType) a.AttributeSchemaType).Datatype;
295 schemaDecimalType = ((XmlSchemaSimpleType) b.AttributeSchemaType).Datatype;
296 schemaAnyType = e.ElementSchemaType as XmlSchemaComplexType;
298 schemaIntegerType = a.AttributeType as XmlSchemaDatatype;
299 schemaDecimalType = b.AttributeType as XmlSchemaDatatype;
300 schemaAnyType = e.ElementType as XmlSchemaComplexType;
309 ArrayList relations = new ArrayList ();
310 Hashtable reservedConstraints = new Hashtable ();
312 // such element that has an attribute msdata:IsDataSet="true"
313 XmlSchemaElement datasetElement;
315 // choice alternatives in the "dataset element"
316 ArrayList topLevelElements = new ArrayList ();
318 // import target elements
319 ArrayList targetElements = new ArrayList ();
321 TableStructure currentTable;
327 public XmlSchemaDataImporter (DataSet dataset, XmlReader reader)
329 this.dataset = dataset;
330 dataset.DataSetName = "NewDataSet"; // Initialize always
331 schema = XmlSchema.Read (reader, null);
332 if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "schema" && reader.NamespaceURI == XmlSchema.Namespace)
333 reader.ReadEndElement ();
334 schema.Compile (null);
339 public void Process ()
341 if (schema.Id != null)
342 dataset.DataSetName = schema.Id; // default. Overridable by "DataSet element"
343 dataset.Namespace = schema.TargetNamespace;
345 // Find dataset element
346 foreach (XmlSchemaObject obj in schema.Items) {
347 XmlSchemaElement el = obj as XmlSchemaElement;
349 if (datasetElement == null &&
350 IsDataSetElement (el))
353 if (el.ElementSchemaType is XmlSchemaComplexType &&
354 el.ElementSchemaType != schemaAnyType)
356 if (el.ElementType is XmlSchemaComplexType &&
357 el.ElementType != schemaAnyType)
359 targetElements.Add (obj);
363 // make reservation of identity constraints
364 if (datasetElement != null) {
366 foreach (XmlSchemaObject obj in datasetElement.Constraints)
367 if (! (obj is XmlSchemaKeyref))
368 ReserveSelfIdentity ((XmlSchemaIdentityConstraint) obj);
370 foreach (XmlSchemaObject obj in datasetElement.Constraints)
371 if (obj is XmlSchemaKeyref)
372 ReserveRelationIdentity (datasetElement, (XmlSchemaKeyref) obj);
375 foreach (XmlSchemaObject obj in schema.Items) {
376 if (obj is XmlSchemaElement) {
377 XmlSchemaElement el = obj as XmlSchemaElement;
379 if (el.ElementSchemaType is XmlSchemaComplexType &&
380 el.ElementSchemaType != schemaAnyType)
382 if (el.ElementType is XmlSchemaComplexType &&
383 el.ElementType != schemaAnyType)
385 targetElements.Add (obj);
389 // This collection will grow up while processing elements.
390 int globalElementCount = targetElements.Count;
392 for (int i = 0; i < globalElementCount; i++)
393 ProcessGlobalElement ((XmlSchemaElement) targetElements [i]);
395 // Rest are local elements.
396 for (int i = globalElementCount; i < targetElements.Count; i++)
397 ProcessDataTableElement ((XmlSchemaElement) targetElements [i]);
399 // Handle relation definitions written as xs:annotation.
400 // See detail: http://msdn.microsoft.com/library/shared/happyUrl/fnf_msdn.asp?Redirect=%22http://msdn.microsoft.com/404/default.asp%22
401 foreach (XmlSchemaObject obj in schema.Items)
402 if (obj is XmlSchemaAnnotation)
403 HandleAnnotations ((XmlSchemaAnnotation) obj, false);
405 if (datasetElement != null) {
406 // Handle constraints in the DataSet element. First keys.
407 foreach (XmlSchemaObject obj in datasetElement.Constraints)
408 if (! (obj is XmlSchemaKeyref))
409 ProcessSelfIdentity (reservedConstraints [obj] as ConstraintStructure);
411 foreach (XmlSchemaObject obj in datasetElement.Constraints)
412 if (obj is XmlSchemaKeyref)
413 ProcessRelationIdentity (datasetElement, reservedConstraints [obj] as ConstraintStructure);
416 foreach (RelationStructure rs in this.relations)
417 dataset.Relations.Add (GenerateRelationship (rs));
420 private bool IsDataSetElement (XmlSchemaElement el)
422 if (el.UnhandledAttributes != null) {
423 foreach (XmlAttribute attr in el.UnhandledAttributes) {
424 if (attr.LocalName == "IsDataSet" &&
425 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
426 switch (attr.Value) {
427 case "true": // case sensitive
432 throw new DataException (String.Format ("Value {0} is invalid for attribute 'IsDataSet'.", attr.Value));
438 if (schema.Elements.Count != 1)
440 if (!(el.SchemaType is XmlSchemaComplexType))
442 XmlSchemaComplexType ct = (XmlSchemaComplexType) el.SchemaType;
443 if (ct.AttributeUses.Count > 0)
445 XmlSchemaGroupBase gb = ct.ContentTypeParticle as XmlSchemaGroupBase;
446 if (gb == null || gb.Items.Count == 0)
448 foreach (XmlSchemaParticle p in gb.Items) {
449 if (ContainsColumn (p))
455 private bool ContainsColumn (XmlSchemaParticle p)
457 XmlSchemaElement el = p as XmlSchemaElement;
459 XmlSchemaComplexType ct = null;
461 ct = el.ElementSchemaType as XmlSchemaComplexType;
463 ct = el.ElementType as XmlSchemaComplexType;
465 if (ct == null || ct == schemaAnyType)
466 return true; // column element
467 if (ct.AttributeUses.Count > 0)
468 return false; // table element
469 if (ct.ContentType == XmlSchemaContentType.TextOnly)
470 return true; // column element
472 return false; // table element
474 XmlSchemaGroupBase gb = p as XmlSchemaGroupBase;
475 for (int i = 0; i < gb.Items.Count; i++) {
476 if (ContainsColumn ((XmlSchemaParticle) gb.Items [i]))
482 private void ProcessGlobalElement (XmlSchemaElement el)
484 // If it is already registered (by resolving reference
485 // in previously-imported elements), just ignore.
486 if (dataset.Tables.Contains (el.QualifiedName.Name))
489 // If type is not complex, just skip this element
491 if (! (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType))
493 if (! (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType))
497 if (IsDataSetElement (el)) {
498 ProcessDataSetElement (el);
502 // Register as a top-level element
503 topLevelElements.Add (el);
504 // Create DataTable for this element
505 ProcessDataTableElement (el);
508 private void ProcessDataSetElement (XmlSchemaElement el)
510 dataset.DataSetName = el.Name;
511 this.datasetElement = el;
513 // Search for msdata:Locale attribute
514 if (el.UnhandledAttributes != null) {
515 foreach (XmlAttribute attr in el.UnhandledAttributes) {
516 if (attr.LocalName == "Locale" &&
517 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
518 CultureInfo ci = new CultureInfo (attr.Value);
524 // Process content type particle (and create DataTable)
525 XmlSchemaComplexType ct = null;
527 ct = el.ElementSchemaType as XmlSchemaComplexType;
529 ct = el.ElementType as XmlSchemaComplexType;
531 XmlSchemaParticle p = ct != null ? ct.ContentTypeParticle : null;
533 HandleDataSetContentTypeParticle (p);
536 private void HandleDataSetContentTypeParticle (XmlSchemaParticle p)
538 XmlSchemaElement el = p as XmlSchemaElement;
541 if (el.ElementSchemaType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
543 if (el.ElementType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
545 ProcessDataTableElement (el);
547 else if (p is XmlSchemaGroupBase) {
548 foreach (XmlSchemaParticle pc in ((XmlSchemaGroupBase) p).Items)
549 HandleDataSetContentTypeParticle (pc);
553 private void ProcessDataTableElement (XmlSchemaElement el)
555 string tableName = XmlHelper.Decode (el.QualifiedName.Name);
556 // If it is already registered, just ignore.
557 if (dataset.Tables.Contains (tableName))
560 DataTable table = new DataTable (tableName);
561 table.Namespace = el.QualifiedName.Namespace;
562 currentTable = new TableStructure (table);
564 dataset.Tables.Add (table);
567 if (el.UnhandledAttributes != null) {
568 foreach (XmlAttribute attr in el.UnhandledAttributes) {
569 if (attr.LocalName == "Locale" &&
570 attr.NamespaceURI == XmlConstants.MsdataNamespace)
571 table.Locale = new CultureInfo (attr.Value);
575 // Handle complex type (NOTE: It is (or should be)
576 // impossible the type is other than complex type).
577 XmlSchemaComplexType ct = null;
579 ct = (XmlSchemaComplexType) el.ElementSchemaType;
581 ct = (XmlSchemaComplexType) el.ElementType;
585 foreach (DictionaryEntry de in ct.AttributeUses)
586 ImportColumnAttribute ((XmlSchemaAttribute) de.Value);
588 // Handle content type particle
589 if (ct.ContentTypeParticle is XmlSchemaElement)
590 ImportColumnElement (el, (XmlSchemaElement) ct.ContentTypeParticle);
591 else if (ct.ContentTypeParticle is XmlSchemaGroupBase)
592 ImportColumnGroupBase (el, (XmlSchemaGroupBase) ct.ContentTypeParticle);
593 // else if null then do nothing.
595 // Handle simple content
596 switch (ct.ContentType) {
597 case XmlSchemaContentType.TextOnly:
598 // case XmlSchemaContentType.Mixed:
599 // LAMESPEC: When reading from XML Schema, it maps to "_text", while on the data inference, it is mapped to "_Text" (case ignorant).
600 string simpleName = el.QualifiedName.Name + "_text";
601 DataColumn simple = new DataColumn (simpleName);
602 simple.Namespace = el.QualifiedName.Namespace;
603 simple.AllowDBNull = (el.MinOccurs == 0);
604 simple.ColumnMapping = MappingType.SimpleContent;
605 simple.DataType = ConvertDatatype (ct.Datatype);
606 currentTable.NonOrdinalColumns.Add (simple);
610 // add columns to the table in specified order
611 // (by msdata:Ordinal attributes)
612 SortedList sd = new SortedList ();
613 foreach (DictionaryEntry de in currentTable.OrdinalColumns)
614 sd.Add (de.Value, de.Key);
615 foreach (DictionaryEntry de in sd)
616 table.Columns.Add ((DataColumn) de.Value);
617 foreach (DataColumn dc in currentTable.NonOrdinalColumns)
618 table.Columns.Add (dc);
621 private DataRelation GenerateRelationship (RelationStructure rs)
623 DataTable ptab = dataset.Tables [rs.ParentTableName];
624 DataTable ctab = dataset.Tables [rs.ChildTableName];
627 string name = rs.ExplicitName != null ? rs.ExplicitName : XmlHelper.Decode (ptab.TableName) + '_' + XmlHelper.Decode (ctab.TableName);
629 // Annotation Relations belonging to a DataSet can contain multiple colnames
630 // in parentkey and childkey.
631 if (datasetElement != null) {
632 String[] pcolnames = rs.ParentColumnName.Split (null);
633 String[] ccolnames = rs.ChildColumnName.Split (null);
635 DataColumn[] pcol = new DataColumn [pcolnames.Length];
636 for (int i=0; i<pcol.Length; ++i)
637 pcol [i] = ptab.Columns [XmlHelper.Decode (pcolnames [i])];
639 DataColumn[] ccol = new DataColumn [ccolnames.Length];
640 for (int i=0; i < ccol.Length; ++i) {
641 ccol [i] = ctab.Columns [XmlHelper.Decode (ccolnames [i])];
642 if (ccol [i] == null)
643 ccol [i] = CreateChildColumn (pcol [i], ctab);
645 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
647 DataColumn pcol = ptab.Columns [XmlHelper.Decode (rs.ParentColumnName)];
648 DataColumn ccol = ctab.Columns [XmlHelper.Decode (rs.ChildColumnName)];
650 ccol = CreateChildColumn (pcol, ctab);
651 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
653 rel.Nested = rs.IsNested;
654 if (rs.CreateConstraint)
655 rel.ParentTable.PrimaryKey = rel.ParentColumns;
659 private DataColumn CreateChildColumn (DataColumn parentColumn, DataTable childTable)
661 DataColumn col = childTable.Columns.Add (parentColumn.ColumnName,
662 parentColumn.DataType);
663 col.Namespace = String.Empty;
664 col.ColumnMapping = MappingType.Hidden;
668 private void ImportColumnGroupBase (XmlSchemaElement parent, XmlSchemaGroupBase gb)
670 foreach (XmlSchemaParticle p in gb.Items) {
671 XmlSchemaElement el = p as XmlSchemaElement;
673 ImportColumnElement (parent, el);
674 else if (p is XmlSchemaGroupBase)
675 ImportColumnGroupBase (parent, (XmlSchemaGroupBase) p);
676 // otherwise p is xs:any
680 private XmlSchemaDatatype GetSchemaPrimitiveType (object type)
682 if (type is XmlSchemaComplexType)
683 return null; // It came here, so that maybe it is xs:anyType
684 XmlSchemaDatatype dt = type as XmlSchemaDatatype;
685 if (dt == null && type != null)
686 dt = ((XmlSchemaSimpleType) type).Datatype;
690 // Note that this column might be Hidden
691 private void ImportColumnAttribute (XmlSchemaAttribute attr)
693 DataColumn col = new DataColumn ();
694 col.ColumnName = attr.QualifiedName.Name;
695 col.Namespace = attr.QualifiedName.Namespace;
696 XmlSchemaDatatype dt = null;
698 dt = GetSchemaPrimitiveType (((XmlSchemaSimpleType) attr.AttributeSchemaType).Datatype);
700 dt = GetSchemaPrimitiveType (attr.AttributeType);
702 // This complicated check comes from the fact that
703 // MS.NET fails to map System.Object to anyType (that
704 // will cause ReadTypedObject() fail on XmlValidatingReader).
705 // ONLY In DataSet context, we set System.String for
707 col.DataType = ConvertDatatype (dt);
708 if (col.DataType == typeof (object))
709 col.DataType = typeof (string);
710 // When attribute use="prohibited", then it is regarded as
712 if (attr.Use == XmlSchemaUse.Prohibited)
713 col.ColumnMapping = MappingType.Hidden;
715 col.ColumnMapping = MappingType.Attribute;
716 col.DefaultValue = GetAttributeDefaultValue (attr);
718 if (attr.Use == XmlSchemaUse.Required)
719 col.AllowDBNull = false;
722 FillFacet (col, attr.AttributeSchemaType as XmlSchemaSimpleType);
724 FillFacet (col, attr.AttributeType as XmlSchemaSimpleType);
727 // Call this method after filling the name
728 ImportColumnMetaInfo (attr, attr.QualifiedName, col);
732 private void ImportColumnElement (XmlSchemaElement parent, XmlSchemaElement el)
734 // FIXME: element nest check
736 DataColumn col = new DataColumn ();
737 col.DefaultValue = GetElementDefaultValue (el);
738 col.AllowDBNull = (el.MinOccurs == 0);
741 if (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType)
743 if (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType)
745 FillDataColumnComplexElement (parent, el, col);
746 else if (el.MaxOccurs != 1)
747 FillDataColumnRepeatedSimpleElement (parent, el, col);
749 FillDataColumnSimpleElement (el, col);
752 // common process for element and attribute
753 private void ImportColumnMetaInfo (XmlSchemaAnnotated obj, XmlQualifiedName name, DataColumn col)
756 if (obj.UnhandledAttributes != null) {
757 foreach (XmlAttribute attr in obj.UnhandledAttributes) {
758 if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
760 switch (attr.LocalName) {
761 case XmlConstants.Caption:
762 col.Caption = attr.Value;
764 case XmlConstants.DataType:
765 col.DataType = Type.GetType (attr.Value);
767 case XmlConstants.AutoIncrement:
768 col.AutoIncrement = bool.Parse (attr.Value);
770 case XmlConstants.AutoIncrementSeed:
771 col.AutoIncrementSeed = int.Parse (attr.Value);
773 case XmlConstants.AutoIncrementStep:
774 col.AutoIncrementStep = int.Parse (attr.Value);
776 case XmlConstants.ReadOnly:
777 col.ReadOnly = XmlConvert.ToBoolean (attr.Value);
779 case XmlConstants.Ordinal:
780 ordinal = int.Parse (attr.Value);
787 private void FillDataColumnComplexElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
789 if (targetElements.Contains (el))
790 return; // do nothing
792 string elName = XmlHelper.Decode (el.QualifiedName.Name);
793 if (elName == dataset.DataSetName)
794 // Well, why it is ArgumentException :-?
795 throw new ArgumentException ("Nested element must not have the same name as DataSet's name.");
797 if (el.Annotation != null)
798 HandleAnnotations (el.Annotation, true);
799 // If xsd:keyref xsd:key for this table exists, then don't add
800 // relation here manually.
801 else if (!DataSetDefinesKey (elName)) {
802 AddParentKeyColumn (parent, el, col);
804 RelationStructure rel = new RelationStructure ();
805 rel.ParentTableName = XmlHelper.Decode (parent.QualifiedName.Name);
806 rel.ChildTableName = elName;
807 rel.ParentColumnName = col.ColumnName;
808 rel.ChildColumnName = col.ColumnName;
809 rel.CreateConstraint = true;
814 // If the element is not referenced one, the element will be handled later.
815 if (el.RefName == XmlQualifiedName.Empty)
816 targetElements.Add (el);
820 private bool DataSetDefinesKey (string name)
822 foreach (ConstraintStructure c in reservedConstraints.Values)
823 if (c.TableName == name && (c.IsPrimaryKey || c.IsNested))
828 private void AddParentKeyColumn (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
830 if (currentTable.PrimaryKey != null)
833 // check name identity
834 string name = XmlHelper.Decode (parent.QualifiedName.Name) + "_Id";
836 while (currentTable.ContainsColumn (name))
837 name = String.Format ("{0}_{1}", name, count++);
838 // check existing primary key
839 if (currentTable.Table.PrimaryKey.Length > 0)
840 throw new DataException (String.Format ("There is already primary key columns in the table \"{0}\".", currentTable.Table.TableName));
842 col.ColumnName = name;
843 col.ColumnMapping = MappingType.Hidden;
844 col.Namespace = parent.QualifiedName.Namespace;
845 col.DataType = typeof (int);
846 col.AutoIncrement = true;
847 col.AllowDBNull = false;
849 ImportColumnMetaInfo (el, el.QualifiedName, col);
851 currentTable.PrimaryKey = col;
854 private void FillDataColumnRepeatedSimpleElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
856 if (targetElements.Contains (el))
857 return; // do nothing
859 AddParentKeyColumn (parent, el, col);
860 DataColumn pkey = currentTable.PrimaryKey;
862 string elName = XmlHelper.Decode (el.QualifiedName.Name);
863 string parentName = XmlHelper.Decode (parent.QualifiedName.Name);
865 DataTable dt = new DataTable ();
866 dt.TableName = elName;
867 dt.Namespace = el.QualifiedName.Namespace;
868 // reference key column to parent
869 DataColumn cc = new DataColumn ();
870 cc.ColumnName = parentName + "_Id";
871 cc.Namespace = parent.QualifiedName.Namespace;
872 cc.ColumnMapping = MappingType.Hidden;
873 cc.DataType = typeof (int);
875 // repeatable content simple element
876 DataColumn cc2 = new DataColumn ();
877 cc2.ColumnName = elName + "_Column";
878 cc2.Namespace = el.QualifiedName.Namespace;
879 cc2.ColumnMapping = MappingType.SimpleContent;
880 cc2.AllowDBNull = false;
882 cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
884 cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
887 dt.Columns.Add (cc2);
889 dataset.Tables.Add (dt);
891 RelationStructure rel = new RelationStructure ();
892 rel.ParentTableName = parentName;
893 rel.ChildTableName = dt.TableName;
894 rel.ParentColumnName = pkey.ColumnName;
895 rel.ChildColumnName = cc.ColumnName;
897 rel.CreateConstraint = true;
901 private void FillDataColumnSimpleElement (XmlSchemaElement el, DataColumn col)
903 col.ColumnName = XmlHelper.Decode (el.QualifiedName.Name);
904 col.Namespace = el.QualifiedName.Namespace;
905 col.ColumnMapping = MappingType.Element;
907 col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
908 FillFacet (col, el.ElementSchemaType as XmlSchemaSimpleType);
910 col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
911 FillFacet (col, el.ElementType as XmlSchemaSimpleType);
914 ImportColumnMetaInfo (el, el.QualifiedName, col);
919 private void AddColumn (DataColumn col)
922 currentTable.NonOrdinalColumns.Add (col);
924 currentTable.OrdinalColumns.Add (col, col.Ordinal);
927 private void FillFacet (DataColumn col, XmlSchemaSimpleType st)
929 if (st == null || st.Content == null)
932 // Handle restriction facets
934 XmlSchemaSimpleTypeRestriction restriction = st == null ? null : st.Content as XmlSchemaSimpleTypeRestriction;
935 if (restriction == null)
936 throw new DataException ("DataSet does not suport 'list' nor 'union' simple type.");
938 foreach (XmlSchemaFacet f in restriction.Facets) {
939 if (f is XmlSchemaMaxLengthFacet)
940 // There is no reason why MaxLength is limited to int, except for the fact that DataColumn.MaxLength property is int.
941 col.MaxLength = int.Parse (f.Value);
945 private Type ConvertDatatype (XmlSchemaDatatype dt)
948 return typeof (string);
949 else if (dt.ValueType == typeof (decimal)) {
950 // LAMESPEC: MSDN documentation says it is based
951 // on ValueType. However, in the System.Xml.Schema
952 // context, xs:integer is mapped to Decimal, while
953 // in DataSet context it is mapped to Int64.
954 if (dt == schemaDecimalType)
955 return typeof (decimal);
956 else if (dt == schemaIntegerType)
957 return typeof (long);
959 return typeof (ulong);
965 // This method cuts out the local name of the last step from XPath.
966 // It is nothing more than hack. However, MS looks to do similar.
967 private string GetSelectorTarget (string xpath)
969 string tableName = xpath;
970 int index = tableName.LastIndexOf ('/');
971 // '>' is enough. If XPath [0] = '/', it is invalid.
972 // Selector can specify only element axes.
974 tableName = tableName.Substring (index + 1);
976 // Round QName to NSName
977 index = tableName.LastIndexOf (':');
979 tableName = tableName.Substring (index + 1);
981 return XmlHelper.Decode (tableName);
984 private void ReserveSelfIdentity (XmlSchemaIdentityConstraint ic)
986 string tableName = GetSelectorTarget (ic.Selector.XPath);
988 string [] cols = new string [ic.Fields.Count];
989 bool [] isAttrSpec = new bool [cols.Length];
992 foreach (XmlSchemaXPath Field in ic.Fields) {
993 string colName = Field.XPath;
994 bool isAttr = colName.Length > 0 && colName [0] == '@';
995 int index = colName.LastIndexOf (':');
997 colName = colName.Substring (index + 1);
999 colName = colName.Substring (1);
1001 colName = XmlHelper.Decode (colName);
1003 isAttrSpec [i] = isAttr;
1008 // find if there is an attribute with the constraint name
1009 // if not use the XmlSchemaConstraint's name.
1010 string constraintName = ic.Name;
1011 if (ic.UnhandledAttributes != null) {
1012 foreach (XmlAttribute attr in ic.UnhandledAttributes) {
1013 if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1015 switch (attr.LocalName) {
1016 case XmlConstants.ConstraintName:
1017 constraintName = attr.Value;
1019 case XmlConstants.PrimaryKey:
1020 isPK = bool.Parse(attr.Value);
1025 reservedConstraints.Add (ic,
1026 new ConstraintStructure (tableName, cols,
1027 isAttrSpec, constraintName, isPK, null, false, false));
1030 private void ProcessSelfIdentity (ConstraintStructure c)
1032 // Basic concept came from XmlSchemaMapper.cs
1034 string tableName = c.TableName;
1036 DataTable dt = dataset.Tables [tableName];
1038 throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1040 DataColumn [] cols = new DataColumn [c.Columns.Length];
1041 for (int i = 0; i < cols.Length; i++) {
1042 string colName = c.Columns [i];
1043 bool isAttr = c.IsAttribute [i];
1044 DataColumn col = dt.Columns [colName];
1046 throw new DataException (String.Format ("Invalid XPath selection inside field. Cannot find: {0}", tableName));
1047 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1048 throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1049 if (!isAttr && col.ColumnMapping != MappingType.Element)
1050 throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1052 cols [i] = dt.Columns [colName];
1055 bool isPK = c.IsPrimaryKey;
1056 string constraintName = c.ConstraintName;
1057 dt.Constraints.Add (new UniqueConstraint (
1058 constraintName, cols, isPK));
1061 private void ReserveRelationIdentity (XmlSchemaElement element, XmlSchemaKeyref keyref)
1063 // Basic concept came from XmlSchemaMapper.cs
1065 string tableName = GetSelectorTarget (keyref.Selector.XPath);
1067 string [] cols = new string [keyref.Fields.Count];
1068 bool [] isAttrSpec = new bool [cols.Length];
1070 foreach (XmlSchemaXPath Field in keyref.Fields) {
1071 string colName = Field.XPath;
1072 bool isAttr = colName.Length > 0 && colName [0] == '@';
1073 int index = colName.LastIndexOf (':');
1075 colName = colName.Substring (index + 1);
1077 colName = colName.Substring (1);
1079 colName = XmlHelper.Decode (colName);
1081 isAttrSpec [i] = isAttr;
1084 string constraintName = keyref.Name;
1085 bool isNested = false;
1086 bool isConstraintOnly = false;
1087 if (keyref.UnhandledAttributes != null) {
1088 foreach (XmlAttribute attr in keyref.UnhandledAttributes) {
1089 if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1091 switch (attr.LocalName) {
1092 case XmlConstants.ConstraintName:
1093 constraintName = attr.Value;
1095 case XmlConstants.IsNested:
1096 if (attr.Value == "true")
1099 case XmlConstants.ConstraintOnly:
1100 if (attr.Value == "true")
1101 isConstraintOnly = true;
1107 reservedConstraints.Add (keyref, new ConstraintStructure (
1108 tableName, cols, isAttrSpec, constraintName,
1109 false, keyref.Refer.Name, isNested, isConstraintOnly));
1112 private void ProcessRelationIdentity (XmlSchemaElement element, ConstraintStructure c)
1114 // Basic concept came from XmlSchemaMapper.cs
1116 string tableName = c.TableName;
1119 DataTable dt = dataset.Tables [tableName];
1121 throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1123 cols = new DataColumn [c.Columns.Length];
1124 for (int i = 0; i < cols.Length; i++) {
1125 string colName = c.Columns [i];
1126 bool isAttr = c.IsAttribute [i];
1127 DataColumn col = dt.Columns [colName];
1128 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1129 throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1130 if (!isAttr && col.ColumnMapping != MappingType.Element)
1131 throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1134 string name = c.ReferName;
1135 // get the unique constraint for the releation
1136 UniqueConstraint uniq = FindConstraint (name, element);
1138 ForeignKeyConstraint fkc = new ForeignKeyConstraint(c.ConstraintName, uniq.Columns, cols);
1139 dt.Constraints.Add (fkc);
1141 if (!c.IsConstraintOnly) {
1142 // generate the relation.
1143 DataRelation rel = new DataRelation (c.ConstraintName, uniq.Columns, cols, false);
1144 rel.Nested = c.IsNested;
1145 rel.SetParentKeyConstraint (uniq);
1146 rel.SetChildKeyConstraint (fkc);
1148 dataset.Relations.Add (rel);
1152 // get the unique constraint for the relation.
1153 // name - the name of the XmlSchemaUnique element
1154 private UniqueConstraint FindConstraint (string name, XmlSchemaElement element)
1156 // Copied from XmlSchemaMapper.cs
1158 // find the element in the constraint collection.
1159 foreach (XmlSchemaIdentityConstraint c in element.Constraints) {
1160 if (c is XmlSchemaKeyref)
1163 if (c.Name == name) {
1164 string tableName = GetSelectorTarget (c.Selector.XPath);
1166 // find the table in the dataset.
1167 DataTable dt = dataset.Tables [tableName];
1169 string constraintName = c.Name;
1170 // find if there is an attribute with the constraint name
1171 // if not use the XmlSchemaUnique name.
1172 if (c.UnhandledAttributes != null)
1173 foreach (XmlAttribute attr in c.UnhandledAttributes)
1174 if (attr.LocalName == "ConstraintName" && attr.NamespaceURI == XmlConstants.MsdataNamespace)
1175 constraintName = attr.Value;
1176 return (UniqueConstraint) dt.Constraints [constraintName];
1179 throw new DataException ("Target identity constraint was not found: " + name);
1182 private void HandleAnnotations (XmlSchemaAnnotation an, bool nested)
1184 foreach (XmlSchemaObject content in an.Items) {
1185 XmlSchemaAppInfo ai = content as XmlSchemaAppInfo;
1187 foreach (XmlNode n in ai.Markup) {
1188 XmlElement el = n as XmlElement;
1189 if (el != null && el.LocalName == "Relationship" && el.NamespaceURI == XmlConstants.MsdataNamespace)
1190 HandleRelationshipAnnotation (el, nested);
1196 private void HandleRelationshipAnnotation (XmlElement el, bool nested)
1198 string name = el.GetAttribute ("name");
1199 string ptn = el.GetAttribute ("parent", XmlConstants.MsdataNamespace);
1200 string ctn = el.GetAttribute ("child", XmlConstants.MsdataNamespace);
1201 string pkn = el.GetAttribute ("parentkey", XmlConstants.MsdataNamespace);
1202 string fkn = el.GetAttribute ("childkey", XmlConstants.MsdataNamespace);
1204 RelationStructure rel = new RelationStructure ();
1205 rel.ExplicitName = XmlHelper.Decode (name);
1206 rel.ParentTableName = XmlHelper.Decode (ptn);
1207 rel.ChildTableName = XmlHelper.Decode (ctn);
1208 // ColumnNames will be decoded wherever they are used as they can
1209 // contain 'space' separated list of column-names.
1210 rel.ParentColumnName = pkn;
1211 rel.ChildColumnName = fkn;
1212 rel.IsNested = nested;
1213 rel.CreateConstraint = false; // by default?
1214 relations.Add (rel);
1217 private object GetElementDefaultValue (XmlSchemaElement elem)
1219 // Unlike attribute, element cannot have a default value.
1220 if (elem.RefName == XmlQualifiedName.Empty)
1221 return elem.DefaultValue;
1222 XmlSchemaElement referenced = schema.Elements [elem.RefName] as XmlSchemaElement;
1223 if (referenced == null) // considering missing sub components
1225 return referenced.DefaultValue;
1228 private object GetAttributeDefaultValue (XmlSchemaAttribute attr)
1230 #if BUGGY_MS_COMPATIBLE
1233 else if (attr.RefName != XmlQualifiedName.Empty) {
1234 XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1235 if (referenced != null)
1236 return referenced.DefaultValue;
1240 if (attr.DefaultValue != null)
1241 return attr.DefaultValue;
1242 return attr.FixedValue;
1244 if (attr.DefaultValue != null)
1245 return attr.DefaultValue;
1246 else if (attr.FixedValue != null)
1247 return attr.FixedValue;
1248 else if (attr.RefName == XmlQualifiedName.Empty)
1250 XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1251 if (referenced == null) // considering missing sub components
1253 if (referenced.DefaultValue != null)
1254 return referenced.DefaultValue;
1255 return referenced.FixedValue;