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.Data.Common;
163 using System.Globalization;
165 using System.Xml.Schema;
168 namespace System.Data
170 internal class TableStructureCollection : CollectionBase
172 public void Add (TableStructure table)
177 public TableStructure this [int i] {
178 get { return List [i] as TableStructure; }
181 public TableStructure this [string name] {
183 foreach (TableStructure ts in List)
184 if (ts.Table.TableName == name)
191 internal class RelationStructureCollection : CollectionBase
193 public void Add (RelationStructure rel)
198 public RelationStructure this [int i] {
199 get { return List [i] as RelationStructure; }
202 public RelationStructure this [string parent, string child] {
204 foreach (RelationStructure rel in List)
205 if (rel.ParentTableName == parent && rel.ChildTableName == child)
212 internal class TableStructure
214 public TableStructure (DataTable table)
219 // The columns and orders which will be added to the context
220 // table (See design notes; Because of the ordinal problem)
221 public DataTable Table;
222 public Hashtable OrdinalColumns = new Hashtable ();
223 public ArrayList NonOrdinalColumns = new ArrayList ();
224 public DataColumn PrimaryKey;
226 public bool ContainsColumn (string name)
228 foreach (DataColumn col in NonOrdinalColumns)
229 if (col.ColumnName == name)
231 foreach (DataColumn col in OrdinalColumns.Keys)
232 if (col.ColumnName == name)
238 internal class RelationStructure
240 public string ExplicitName;
241 public string ParentTableName;
242 public string ChildTableName;
243 public string ParentColumnName;
244 public string ChildColumnName;
245 public bool IsNested;
246 public bool CreateConstraint;
249 internal class ConstraintStructure
251 public readonly string TableName;
252 public readonly string [] Columns;
253 public readonly bool [] IsAttribute;
254 public readonly string ConstraintName;
255 public readonly bool IsPrimaryKey;
256 public readonly string ReferName;
257 public readonly bool IsNested;
258 public readonly bool IsConstraintOnly;
260 public ConstraintStructure (string tname, string [] cols, bool [] isAttr, string cname, bool isPK, string refName, bool isNested, bool isConstraintOnly)
264 IsAttribute = isAttr;
265 ConstraintName = XmlHelper.Decode (cname);
269 IsConstraintOnly = isConstraintOnly;
273 internal class XmlSchemaDataImporter
275 static readonly XmlSchemaDatatype schemaIntegerType;
276 static readonly XmlSchemaDatatype schemaDecimalType;
277 static readonly XmlSchemaComplexType schemaAnyType;
279 static XmlSchemaDataImporter ()
281 XmlSchema s = new XmlSchema ();
282 XmlSchemaAttribute a = new XmlSchemaAttribute ();
284 a.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
286 XmlSchemaAttribute b = new XmlSchemaAttribute ();
288 b.SchemaTypeName = new XmlQualifiedName ("decimal", XmlSchema.Namespace);
290 XmlSchemaElement e = new XmlSchemaElement ();
295 schemaIntegerType = ((XmlSchemaSimpleType) a.AttributeSchemaType).Datatype;
296 schemaDecimalType = ((XmlSchemaSimpleType) b.AttributeSchemaType).Datatype;
297 schemaAnyType = e.ElementSchemaType as XmlSchemaComplexType;
299 schemaIntegerType = a.AttributeType as XmlSchemaDatatype;
300 schemaDecimalType = b.AttributeType as XmlSchemaDatatype;
301 schemaAnyType = e.ElementType as XmlSchemaComplexType;
311 ArrayList relations = new ArrayList ();
312 Hashtable reservedConstraints = new Hashtable ();
314 // such element that has an attribute msdata:IsDataSet="true"
315 XmlSchemaElement datasetElement;
317 // choice alternatives in the "dataset element"
318 ArrayList topLevelElements = new ArrayList ();
320 // import target elements
321 ArrayList targetElements = new ArrayList ();
323 TableStructure currentTable;
326 // TODO: Do we need a collection here?
327 TableAdapterSchemaInfo currentAdapter;
333 public XmlSchemaDataImporter (DataSet dataset, XmlReader reader, bool forDataSet)
335 this.dataset = dataset;
336 this.forDataSet = forDataSet;
337 dataset.DataSetName = "NewDataSet"; // Initialize always
338 schema = XmlSchema.Read (reader, null);
339 if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "schema" && reader.NamespaceURI == XmlSchema.Namespace)
340 reader.ReadEndElement ();
341 schema.Compile (null);
346 internal TableAdapterSchemaInfo CurrentAdapter {
347 get { return currentAdapter; }
353 public void Process ()
355 if (schema.Id != null)
356 dataset.DataSetName = schema.Id; // default. Overridable by "DataSet element"
357 dataset.Namespace = schema.TargetNamespace;
359 // Find dataset element
360 foreach (XmlSchemaObject obj in schema.Items) {
361 XmlSchemaElement el = obj as XmlSchemaElement;
363 if (datasetElement == null &&
364 IsDataSetElement (el))
367 if (el.ElementSchemaType is XmlSchemaComplexType &&
368 el.ElementSchemaType != schemaAnyType)
370 if (el.ElementType is XmlSchemaComplexType &&
371 el.ElementType != schemaAnyType)
373 targetElements.Add (obj);
377 // make reservation of identity constraints
378 if (datasetElement != null) {
380 foreach (XmlSchemaObject obj in datasetElement.Constraints)
381 if (! (obj is XmlSchemaKeyref))
382 ReserveSelfIdentity ((XmlSchemaIdentityConstraint) obj);
384 foreach (XmlSchemaObject obj in datasetElement.Constraints)
385 if (obj is XmlSchemaKeyref)
386 ReserveRelationIdentity (datasetElement, (XmlSchemaKeyref) obj);
389 foreach (XmlSchemaObject obj in schema.Items) {
390 if (obj is XmlSchemaElement) {
391 XmlSchemaElement el = obj as XmlSchemaElement;
393 if (el.ElementSchemaType is XmlSchemaComplexType &&
394 el.ElementSchemaType != schemaAnyType)
396 if (el.ElementType is XmlSchemaComplexType &&
397 el.ElementType != schemaAnyType)
399 targetElements.Add (obj);
403 // This collection will grow up while processing elements.
404 int globalElementCount = targetElements.Count;
406 for (int i = 0; i < globalElementCount; i++)
407 ProcessGlobalElement ((XmlSchemaElement) targetElements [i]);
409 // Rest are local elements.
410 for (int i = globalElementCount; i < targetElements.Count; i++)
411 ProcessDataTableElement ((XmlSchemaElement) targetElements [i]);
413 // Handle relation definitions written as xs:annotation.
414 // See detail: http://msdn.microsoft.com/library/shared/happyUrl/fnf_msdn.asp?Redirect=%22http://msdn.microsoft.com/404/default.asp%22
415 foreach (XmlSchemaObject obj in schema.Items)
416 if (obj is XmlSchemaAnnotation)
417 HandleAnnotations ((XmlSchemaAnnotation) obj, false);
419 if (datasetElement != null) {
420 // Handle constraints in the DataSet element. First keys.
421 foreach (XmlSchemaObject obj in datasetElement.Constraints)
422 if (! (obj is XmlSchemaKeyref))
423 ProcessSelfIdentity (reservedConstraints [obj] as ConstraintStructure);
425 foreach (XmlSchemaObject obj in datasetElement.Constraints)
426 if (obj is XmlSchemaKeyref)
427 ProcessRelationIdentity (datasetElement, reservedConstraints [obj] as ConstraintStructure);
430 foreach (RelationStructure rs in this.relations)
431 dataset.Relations.Add (GenerateRelationship (rs));
434 private bool IsDataSetElement (XmlSchemaElement el)
436 if (el.UnhandledAttributes != null) {
437 foreach (XmlAttribute attr in el.UnhandledAttributes) {
438 if (attr.LocalName == "IsDataSet" &&
439 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
440 switch (attr.Value) {
441 case "true": // case sensitive
446 throw new DataException (String.Format ("Value {0} is invalid for attribute 'IsDataSet'.", attr.Value));
452 if (schema.Elements.Count != 1)
454 if (!(el.SchemaType is XmlSchemaComplexType))
456 XmlSchemaComplexType ct = (XmlSchemaComplexType) el.SchemaType;
457 if (ct.AttributeUses.Count > 0)
459 XmlSchemaGroupBase gb = ct.ContentTypeParticle as XmlSchemaGroupBase;
460 if (gb == null || gb.Items.Count == 0)
462 foreach (XmlSchemaParticle p in gb.Items) {
463 if (ContainsColumn (p))
469 private bool ContainsColumn (XmlSchemaParticle p)
471 XmlSchemaElement el = p as XmlSchemaElement;
473 XmlSchemaComplexType ct = null;
475 ct = el.ElementSchemaType as XmlSchemaComplexType;
477 ct = el.ElementType as XmlSchemaComplexType;
479 if (ct == null || ct == schemaAnyType)
480 return true; // column element
481 if (ct.AttributeUses.Count > 0)
482 return false; // table element
483 if (ct.ContentType == XmlSchemaContentType.TextOnly)
484 return true; // column element
486 return false; // table element
488 XmlSchemaGroupBase gb = p as XmlSchemaGroupBase;
489 for (int i = 0; i < gb.Items.Count; i++) {
490 if (ContainsColumn ((XmlSchemaParticle) gb.Items [i]))
496 private void ProcessGlobalElement (XmlSchemaElement el)
498 // If it is already registered (by resolving reference
499 // in previously-imported elements), just ignore.
500 if (dataset.Tables.Contains (el.QualifiedName.Name))
503 // If type is not complex, just skip this element
505 if (! (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType))
507 if (! (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType))
511 if (IsDataSetElement (el)) {
512 ProcessDataSetElement (el);
516 dataset.Locale = CultureInfo.CurrentCulture;
518 // Register as a top-level element
519 topLevelElements.Add (el);
520 // Create DataTable for this element
521 ProcessDataTableElement (el);
524 private void ProcessDataSetElement (XmlSchemaElement el)
526 dataset.DataSetName = el.Name;
527 this.datasetElement = el;
529 // Search for locale attributes
530 bool useCurrent = false;
531 if (el.UnhandledAttributes != null) {
532 foreach (XmlAttribute attr in el.UnhandledAttributes) {
534 if (attr.LocalName == "UseCurrentLocale" &&
535 attr.NamespaceURI == XmlConstants.MsdataNamespace)
538 if (attr.LocalName == "Locale" &&
539 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
540 CultureInfo ci = new CultureInfo (attr.Value);
546 if (!useCurrent && !dataset.LocaleSpecified) // then set current culture instance _explicitly_
547 dataset.Locale = CultureInfo.CurrentCulture;
550 // Process content type particle (and create DataTable)
551 XmlSchemaComplexType ct = null;
553 ct = el.ElementSchemaType as XmlSchemaComplexType;
555 ct = el.ElementType as XmlSchemaComplexType;
557 XmlSchemaParticle p = ct != null ? ct.ContentTypeParticle : null;
559 HandleDataSetContentTypeParticle (p);
562 private void HandleDataSetContentTypeParticle (XmlSchemaParticle p)
564 XmlSchemaElement el = p as XmlSchemaElement;
567 if (el.ElementSchemaType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
569 if (el.ElementType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
571 ProcessDataTableElement (el);
573 else if (p is XmlSchemaGroupBase) {
574 foreach (XmlSchemaParticle pc in ((XmlSchemaGroupBase) p).Items)
575 HandleDataSetContentTypeParticle (pc);
579 private void ProcessDataTableElement (XmlSchemaElement el)
581 string tableName = XmlHelper.Decode (el.QualifiedName.Name);
582 // If it is already registered, just ignore.
583 if (dataset.Tables.Contains (tableName))
586 DataTable table = new DataTable (tableName);
587 table.Namespace = el.QualifiedName.Namespace;
588 TableStructure oldTable = currentTable;
589 currentTable = new TableStructure (table);
591 dataset.Tables.Add (table);
594 if (el.UnhandledAttributes != null) {
595 foreach (XmlAttribute attr in el.UnhandledAttributes) {
596 if (attr.LocalName == "Locale" &&
597 attr.NamespaceURI == XmlConstants.MsdataNamespace)
598 table.Locale = new CultureInfo (attr.Value);
602 // Handle complex type (NOTE: It is (or should be)
603 // impossible the type is other than complex type).
604 XmlSchemaComplexType ct = null;
606 ct = (XmlSchemaComplexType) el.ElementSchemaType;
608 ct = (XmlSchemaComplexType) el.ElementType;
612 foreach (DictionaryEntry de in ct.AttributeUses)
613 ImportColumnAttribute ((XmlSchemaAttribute) de.Value);
615 // Handle content type particle
616 if (ct.ContentTypeParticle is XmlSchemaElement)
617 ImportColumnElement (el, (XmlSchemaElement) ct.ContentTypeParticle);
618 else if (ct.ContentTypeParticle is XmlSchemaGroupBase)
619 ImportColumnGroupBase (el, (XmlSchemaGroupBase) ct.ContentTypeParticle);
620 // else if null then do nothing.
622 // Handle simple content
623 switch (ct.ContentType) {
624 case XmlSchemaContentType.TextOnly:
625 // case XmlSchemaContentType.Mixed:
626 // LAMESPEC: When reading from XML Schema, it maps to "_text", while on the data inference, it is mapped to "_Text" (case ignorant).
627 string simpleName = el.QualifiedName.Name + "_text";
628 DataColumn simple = new DataColumn (simpleName);
629 simple.Namespace = el.QualifiedName.Namespace;
630 simple.AllowDBNull = (el.MinOccurs == 0);
631 simple.ColumnMapping = MappingType.SimpleContent;
632 simple.DataType = ConvertDatatype (ct.Datatype);
633 currentTable.NonOrdinalColumns.Add (simple);
637 // add columns to the table in specified order
638 // (by msdata:Ordinal attributes)
639 SortedList sd = new SortedList ();
640 foreach (DictionaryEntry de in currentTable.OrdinalColumns)
641 sd.Add (de.Value, de.Key);
642 foreach (DictionaryEntry de in sd)
643 table.Columns.Add ((DataColumn) de.Value);
644 foreach (DataColumn dc in currentTable.NonOrdinalColumns)
645 table.Columns.Add (dc);
647 currentTable = oldTable;
650 private DataRelation GenerateRelationship (RelationStructure rs)
652 DataTable ptab = dataset.Tables [rs.ParentTableName];
653 DataTable ctab = dataset.Tables [rs.ChildTableName];
656 string name = rs.ExplicitName != null ? rs.ExplicitName : XmlHelper.Decode (ptab.TableName) + '_' + XmlHelper.Decode (ctab.TableName);
658 // Annotation Relations belonging to a DataSet can contain multiple colnames
659 // in parentkey and childkey.
660 if (datasetElement != null) {
661 String[] pcolnames = rs.ParentColumnName.Split (null);
662 String[] ccolnames = rs.ChildColumnName.Split (null);
664 DataColumn[] pcol = new DataColumn [pcolnames.Length];
665 for (int i=0; i<pcol.Length; ++i)
666 pcol [i] = ptab.Columns [XmlHelper.Decode (pcolnames [i])];
668 DataColumn[] ccol = new DataColumn [ccolnames.Length];
669 for (int i=0; i < ccol.Length; ++i) {
670 ccol [i] = ctab.Columns [XmlHelper.Decode (ccolnames [i])];
671 if (ccol [i] == null)
672 ccol [i] = CreateChildColumn (pcol [i], ctab);
674 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
676 DataColumn pcol = ptab.Columns [XmlHelper.Decode (rs.ParentColumnName)];
677 DataColumn ccol = ctab.Columns [XmlHelper.Decode (rs.ChildColumnName)];
679 ccol = CreateChildColumn (pcol, ctab);
680 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
682 rel.Nested = rs.IsNested;
683 if (rs.CreateConstraint)
684 rel.ParentTable.PrimaryKey = rel.ParentColumns;
688 private DataColumn CreateChildColumn (DataColumn parentColumn, DataTable childTable)
690 DataColumn col = childTable.Columns.Add (parentColumn.ColumnName,
691 parentColumn.DataType);
692 col.Namespace = String.Empty;
693 col.ColumnMapping = MappingType.Hidden;
697 private void ImportColumnGroupBase (XmlSchemaElement parent, XmlSchemaGroupBase gb)
699 foreach (XmlSchemaParticle p in gb.Items) {
700 XmlSchemaElement el = p as XmlSchemaElement;
702 ImportColumnElement (parent, el);
703 else if (p is XmlSchemaGroupBase)
704 ImportColumnGroupBase (parent, (XmlSchemaGroupBase) p);
705 // otherwise p is xs:any
709 private XmlSchemaDatatype GetSchemaPrimitiveType (object type)
711 if (type is XmlSchemaComplexType)
712 return null; // It came here, so that maybe it is xs:anyType
713 XmlSchemaDatatype dt = type as XmlSchemaDatatype;
714 if (dt == null && type != null)
715 dt = ((XmlSchemaSimpleType) type).Datatype;
719 // Note that this column might be Hidden
720 private void ImportColumnAttribute (XmlSchemaAttribute attr)
722 DataColumn col = new DataColumn ();
723 col.ColumnName = attr.QualifiedName.Name;
724 col.Namespace = attr.QualifiedName.Namespace;
725 XmlSchemaDatatype dt = null;
727 dt = GetSchemaPrimitiveType (((XmlSchemaSimpleType) attr.AttributeSchemaType).Datatype);
729 dt = GetSchemaPrimitiveType (attr.AttributeType);
731 // This complicated check comes from the fact that
732 // MS.NET fails to map System.Object to anyType (that
733 // will cause ReadTypedObject() fail on XmlValidatingReader).
734 // ONLY In DataSet context, we set System.String for
736 col.DataType = ConvertDatatype (dt);
737 if (col.DataType == typeof (object))
738 col.DataType = typeof (string);
739 // When attribute use="prohibited", then it is regarded as
741 if (attr.Use == XmlSchemaUse.Prohibited)
742 col.ColumnMapping = MappingType.Hidden;
744 col.ColumnMapping = MappingType.Attribute;
745 col.DefaultValue = GetAttributeDefaultValue (attr);
747 if (attr.Use == XmlSchemaUse.Required)
748 col.AllowDBNull = false;
751 FillFacet (col, attr.AttributeSchemaType as XmlSchemaSimpleType);
753 FillFacet (col, attr.AttributeType as XmlSchemaSimpleType);
756 // Call this method after filling the name
757 ImportColumnMetaInfo (attr, attr.QualifiedName, col);
761 private void ImportColumnElement (XmlSchemaElement parent, XmlSchemaElement el)
763 // FIXME: element nest check
765 DataColumn col = new DataColumn ();
766 col.DefaultValue = GetElementDefaultValue (el);
767 col.AllowDBNull = (el.MinOccurs == 0);
770 if (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType)
772 if (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType)
774 FillDataColumnComplexElement (parent, el, col);
775 else if (el.MaxOccurs != 1)
776 FillDataColumnRepeatedSimpleElement (parent, el, col);
778 FillDataColumnSimpleElement (el, col);
781 // common process for element and attribute
782 private void ImportColumnMetaInfo (XmlSchemaAnnotated obj, XmlQualifiedName name, DataColumn col)
784 if (obj.UnhandledAttributes != null) {
785 foreach (XmlAttribute attr in obj.UnhandledAttributes) {
786 if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
788 switch (attr.LocalName) {
789 case XmlConstants.Caption:
790 col.Caption = attr.Value;
792 case XmlConstants.DataType:
793 col.DataType = Type.GetType (attr.Value);
795 case XmlConstants.AutoIncrement:
796 col.AutoIncrement = bool.Parse (attr.Value);
798 case XmlConstants.AutoIncrementSeed:
799 col.AutoIncrementSeed = int.Parse (attr.Value);
801 case XmlConstants.AutoIncrementStep:
802 col.AutoIncrementStep = int.Parse (attr.Value);
804 case XmlConstants.ReadOnly:
805 col.ReadOnly = XmlConvert.ToBoolean (attr.Value);
807 case XmlConstants.Ordinal:
808 int ordinal = int.Parse (attr.Value);
815 private void FillDataColumnComplexElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
817 if (targetElements.Contains (el))
818 return; // do nothing
820 string elName = XmlHelper.Decode (el.QualifiedName.Name);
821 if (elName == dataset.DataSetName)
822 // Well, why it is ArgumentException :-?
823 throw new ArgumentException ("Nested element must not have the same name as DataSet's name.");
825 if (el.Annotation != null)
826 HandleAnnotations (el.Annotation, true);
827 // If xsd:keyref xsd:key for this table exists, then don't add
828 // relation here manually.
829 else if (!DataSetDefinesKey (elName)) {
830 AddParentKeyColumn (parent, el, col);
832 RelationStructure rel = new RelationStructure ();
833 rel.ParentTableName = XmlHelper.Decode (parent.QualifiedName.Name);
834 rel.ChildTableName = elName;
835 rel.ParentColumnName = col.ColumnName;
836 rel.ChildColumnName = col.ColumnName;
837 rel.CreateConstraint = true;
842 // If the element is not referenced one, the element will be handled later.
843 if (el.RefName == XmlQualifiedName.Empty)
844 ProcessDataTableElement (el);
848 private bool DataSetDefinesKey (string name)
850 foreach (ConstraintStructure c in reservedConstraints.Values)
851 if (c.TableName == name && (c.IsPrimaryKey || c.IsNested))
856 private void AddParentKeyColumn (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
858 if (currentTable.PrimaryKey != null)
861 // check name identity
862 string name = XmlHelper.Decode (parent.QualifiedName.Name) + "_Id";
864 while (currentTable.ContainsColumn (name))
865 name = String.Format ("{0}_{1}", name, count++);
866 // check existing primary key
867 if (currentTable.Table.PrimaryKey.Length > 0)
868 throw new DataException (String.Format ("There is already primary key columns in the table \"{0}\".", currentTable.Table.TableName));
870 col.ColumnName = name;
871 col.ColumnMapping = MappingType.Hidden;
872 col.Namespace = parent.QualifiedName.Namespace;
873 col.DataType = typeof (int);
874 col.AutoIncrement = true;
875 col.AllowDBNull = false;
877 ImportColumnMetaInfo (el, el.QualifiedName, col);
879 currentTable.PrimaryKey = col;
882 private void FillDataColumnRepeatedSimpleElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
884 if (targetElements.Contains (el))
885 return; // do nothing
887 AddParentKeyColumn (parent, el, col);
888 DataColumn pkey = currentTable.PrimaryKey;
890 string elName = XmlHelper.Decode (el.QualifiedName.Name);
891 string parentName = XmlHelper.Decode (parent.QualifiedName.Name);
893 DataTable dt = new DataTable ();
894 dt.TableName = elName;
895 dt.Namespace = el.QualifiedName.Namespace;
896 // reference key column to parent
897 DataColumn cc = new DataColumn ();
898 cc.ColumnName = parentName + "_Id";
899 cc.Namespace = parent.QualifiedName.Namespace;
900 cc.ColumnMapping = MappingType.Hidden;
901 cc.DataType = typeof (int);
903 // repeatable content simple element
904 DataColumn cc2 = new DataColumn ();
905 cc2.ColumnName = elName + "_Column";
906 cc2.Namespace = el.QualifiedName.Namespace;
907 cc2.ColumnMapping = MappingType.SimpleContent;
908 cc2.AllowDBNull = false;
910 cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
912 cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
915 dt.Columns.Add (cc2);
917 dataset.Tables.Add (dt);
919 RelationStructure rel = new RelationStructure ();
920 rel.ParentTableName = parentName;
921 rel.ChildTableName = dt.TableName;
922 rel.ParentColumnName = pkey.ColumnName;
923 rel.ChildColumnName = cc.ColumnName;
925 rel.CreateConstraint = true;
929 private void FillDataColumnSimpleElement (XmlSchemaElement el, DataColumn col)
931 col.ColumnName = XmlHelper.Decode (el.QualifiedName.Name);
932 col.Namespace = el.QualifiedName.Namespace;
933 col.ColumnMapping = MappingType.Element;
935 col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
936 FillFacet (col, el.ElementSchemaType as XmlSchemaSimpleType);
938 col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
939 FillFacet (col, el.ElementType as XmlSchemaSimpleType);
942 ImportColumnMetaInfo (el, el.QualifiedName, col);
947 private void AddColumn (DataColumn col)
950 currentTable.NonOrdinalColumns.Add (col);
952 currentTable.OrdinalColumns.Add (col, col.Ordinal);
955 private void FillFacet (DataColumn col, XmlSchemaSimpleType st)
957 if (st == null || st.Content == null)
960 // Handle restriction facets
962 XmlSchemaSimpleTypeRestriction restriction = st == null ? null : st.Content as XmlSchemaSimpleTypeRestriction;
963 if (restriction == null)
964 throw new DataException ("DataSet does not suport 'list' nor 'union' simple type.");
966 foreach (XmlSchemaFacet f in restriction.Facets) {
967 if (f is XmlSchemaMaxLengthFacet)
968 // There is no reason why MaxLength is limited to int, except for the fact that DataColumn.MaxLength property is int.
969 col.MaxLength = int.Parse (f.Value);
973 private Type ConvertDatatype (XmlSchemaDatatype dt)
976 return typeof (string);
977 else if (dt.ValueType == typeof (decimal)) {
978 // LAMESPEC: MSDN documentation says it is based
979 // on ValueType. However, in the System.Xml.Schema
980 // context, xs:integer is mapped to Decimal, while
981 // in DataSet context it is mapped to Int64.
982 if (dt == schemaDecimalType)
983 return typeof (decimal);
984 else if (dt == schemaIntegerType)
985 return typeof (long);
987 return typeof (ulong);
993 // This method cuts out the local name of the last step from XPath.
994 // It is nothing more than hack. However, MS looks to do similar.
995 private string GetSelectorTarget (string xpath)
997 string tableName = xpath;
998 int index = tableName.LastIndexOf ('/');
999 // '>' is enough. If XPath [0] = '/', it is invalid.
1000 // Selector can specify only element axes.
1002 tableName = tableName.Substring (index + 1);
1004 // Round QName to NSName
1005 index = tableName.LastIndexOf (':');
1007 tableName = tableName.Substring (index + 1);
1009 return XmlHelper.Decode (tableName);
1012 private void ReserveSelfIdentity (XmlSchemaIdentityConstraint ic)
1014 string tableName = GetSelectorTarget (ic.Selector.XPath);
1016 string [] cols = new string [ic.Fields.Count];
1017 bool [] isAttrSpec = new bool [cols.Length];
1020 foreach (XmlSchemaXPath Field in ic.Fields) {
1021 string colName = Field.XPath;
1022 bool isAttr = colName.Length > 0 && colName [0] == '@';
1023 int index = colName.LastIndexOf (':');
1025 colName = colName.Substring (index + 1);
1027 colName = colName.Substring (1);
1029 colName = XmlHelper.Decode (colName);
1031 isAttrSpec [i] = isAttr;
1036 // find if there is an attribute with the constraint name
1037 // if not use the XmlSchemaConstraint's name.
1038 string constraintName = ic.Name;
1039 if (ic.UnhandledAttributes != null) {
1040 foreach (XmlAttribute attr in ic.UnhandledAttributes) {
1041 if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1043 switch (attr.LocalName) {
1044 case XmlConstants.ConstraintName:
1045 constraintName = attr.Value;
1047 case XmlConstants.PrimaryKey:
1048 isPK = bool.Parse(attr.Value);
1053 reservedConstraints.Add (ic,
1054 new ConstraintStructure (tableName, cols,
1055 isAttrSpec, constraintName, isPK, null, false, false));
1058 private void ProcessSelfIdentity (ConstraintStructure c)
1060 // Basic concept came from XmlSchemaMapper.cs
1062 string tableName = c.TableName;
1064 DataTable dt = dataset.Tables [tableName];
1067 throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1069 // nonexistent table name. .NET ignores it for DataTable.ReadXmlSchema().
1073 DataColumn [] cols = new DataColumn [c.Columns.Length];
1074 for (int i = 0; i < cols.Length; i++) {
1075 string colName = c.Columns [i];
1076 bool isAttr = c.IsAttribute [i];
1077 DataColumn col = dt.Columns [colName];
1079 throw new DataException (String.Format ("Invalid XPath selection inside field. Cannot find: {0}", tableName));
1080 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1081 throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1082 if (!isAttr && col.ColumnMapping != MappingType.Element)
1083 throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1085 cols [i] = dt.Columns [colName];
1088 bool isPK = c.IsPrimaryKey;
1089 string constraintName = c.ConstraintName;
1090 dt.Constraints.Add (new UniqueConstraint (
1091 constraintName, cols, isPK));
1094 private void ReserveRelationIdentity (XmlSchemaElement element, XmlSchemaKeyref keyref)
1096 // Basic concept came from XmlSchemaMapper.cs
1098 string tableName = GetSelectorTarget (keyref.Selector.XPath);
1100 string [] cols = new string [keyref.Fields.Count];
1101 bool [] isAttrSpec = new bool [cols.Length];
1103 foreach (XmlSchemaXPath Field in keyref.Fields) {
1104 string colName = Field.XPath;
1105 bool isAttr = colName.Length > 0 && colName [0] == '@';
1106 int index = colName.LastIndexOf (':');
1108 colName = colName.Substring (index + 1);
1110 colName = colName.Substring (1);
1112 colName = XmlHelper.Decode (colName);
1114 isAttrSpec [i] = isAttr;
1117 string constraintName = keyref.Name;
1118 bool isNested = false;
1119 bool isConstraintOnly = false;
1120 if (keyref.UnhandledAttributes != null) {
1121 foreach (XmlAttribute attr in keyref.UnhandledAttributes) {
1122 if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1124 switch (attr.LocalName) {
1125 case XmlConstants.ConstraintName:
1126 constraintName = attr.Value;
1128 case XmlConstants.IsNested:
1129 if (attr.Value == "true")
1132 case XmlConstants.ConstraintOnly:
1133 if (attr.Value == "true")
1134 isConstraintOnly = true;
1140 reservedConstraints.Add (keyref, new ConstraintStructure (
1141 tableName, cols, isAttrSpec, constraintName,
1142 false, keyref.Refer.Name, isNested, isConstraintOnly));
1145 private void ProcessRelationIdentity (XmlSchemaElement element, ConstraintStructure c)
1147 // Basic concept came from XmlSchemaMapper.cs
1149 string tableName = c.TableName;
1152 DataTable dt = dataset.Tables [tableName];
1154 throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1156 cols = new DataColumn [c.Columns.Length];
1157 for (int i = 0; i < cols.Length; i++) {
1158 string colName = c.Columns [i];
1159 bool isAttr = c.IsAttribute [i];
1160 DataColumn col = dt.Columns [colName];
1161 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1162 throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1163 if (!isAttr && col.ColumnMapping != MappingType.Element)
1164 throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1167 string name = c.ReferName;
1168 // get the unique constraint for the releation
1169 UniqueConstraint uniq = FindConstraint (name, element);
1171 ForeignKeyConstraint fkc = new ForeignKeyConstraint(c.ConstraintName, uniq.Columns, cols);
1172 dt.Constraints.Add (fkc);
1174 if (!c.IsConstraintOnly) {
1175 // generate the relation.
1176 DataRelation rel = new DataRelation (c.ConstraintName, uniq.Columns, cols, true);
1177 rel.Nested = c.IsNested;
1178 rel.SetParentKeyConstraint (uniq);
1179 rel.SetChildKeyConstraint (fkc);
1181 dataset.Relations.Add (rel);
1185 // get the unique constraint for the relation.
1186 // name - the name of the XmlSchemaUnique element
1187 private UniqueConstraint FindConstraint (string name, XmlSchemaElement element)
1189 // Copied from XmlSchemaMapper.cs
1191 // find the element in the constraint collection.
1192 foreach (XmlSchemaIdentityConstraint c in element.Constraints) {
1193 if (c is XmlSchemaKeyref)
1196 if (c.Name == name) {
1197 string tableName = GetSelectorTarget (c.Selector.XPath);
1199 // find the table in the dataset.
1200 DataTable dt = dataset.Tables [tableName];
1202 string constraintName = c.Name;
1203 // find if there is an attribute with the constraint name
1204 // if not use the XmlSchemaUnique name.
1205 if (c.UnhandledAttributes != null)
1206 foreach (XmlAttribute attr in c.UnhandledAttributes)
1207 if (attr.LocalName == "ConstraintName" && attr.NamespaceURI == XmlConstants.MsdataNamespace)
1208 constraintName = attr.Value;
1209 return (UniqueConstraint) dt.Constraints [constraintName];
1212 throw new DataException ("Target identity constraint was not found: " + name);
1215 private void HandleAnnotations (XmlSchemaAnnotation an, bool nested)
1217 foreach (XmlSchemaObject content in an.Items) {
1218 XmlSchemaAppInfo ai = content as XmlSchemaAppInfo;
1220 foreach (XmlNode n in ai.Markup) {
1221 XmlElement el = n as XmlElement;
1223 // #325464 debugging
1224 //Console.WriteLine ("Name: " + el.LocalName + " NS: " + el.NamespaceURI + " Const: " + XmlConstants.MsdataNamespace);
1225 if (el != null && el.LocalName == "Relationship" && el.NamespaceURI == XmlConstants.MsdataNamespace)
1226 HandleRelationshipAnnotation (el, nested);
1228 if (el != null && el.LocalName == "DataSource" && el.NamespaceURI == XmlConstants.MsdatasourceNamespace)
1229 HandleDataSourceAnnotation (el, nested);
1237 private void HandleDataSourceAnnotation (XmlElement el, bool nested)
1239 // Handle: Connections and Tables
1240 // For Tables: extract the provider information from connection and use
1241 // the corresponding providerfactory to create the adapter and et al objects
1242 // and populate them
1244 // #325464 debugging
1245 //Console.WriteLine ("In HandleDataSourceAnnotation... ");
1246 string providerName = null;
1247 string connString = null;
1248 DbProviderFactory provider = null;
1249 XmlElement e, tablesElement = null;
1251 foreach (XmlNode n in el.ChildNodes) {
1252 e = n as XmlElement;
1258 if (e.LocalName == "Connections") {
1259 providerName = ((XmlElement)e.FirstChild).GetAttribute ("Provider");
1260 connString = ((XmlElement)e.FirstChild).GetAttribute ("AppSettingsPropertyName");
1261 provider = DbProviderFactories.GetFactory (providerName);
1264 #endif // !MONOTOUCH
1265 // #325464 debugging
1266 //Console.WriteLine ("ProviderName: " + providerName + "Connstr: " + connString);
1268 if (e.LocalName == "Tables")
1272 if (tablesElement != null && provider != null) {
1273 foreach (XmlNode node in tablesElement.ChildNodes) {
1274 ProcessTableAdapter (node as XmlElement, provider, connString);
1279 private void ProcessTableAdapter (XmlElement el, DbProviderFactory provider, string connStr)
1282 string datasetTableName = null;
1287 // #325464 debugging
1288 //Console.WriteLine ("in ProcessTableAdapters...");
1289 currentAdapter = new TableAdapterSchemaInfo (provider);
1290 currentAdapter.ConnectionString = connStr;
1292 //Console.WriteLine ("Provider: {0}, connection: {1}, adapter: {2}",
1293 // provider, currentAdapter.Connection, currentAdapter.Adapter);
1294 currentAdapter.BaseClass = el.GetAttribute ("BaseClass");
1295 datasetTableName = el.GetAttribute ("Name");
1296 currentAdapter.Name = el.GetAttribute ("GeneratorDataComponentClassName");
1298 if (String.IsNullOrEmpty (currentAdapter.Name))
1299 currentAdapter.Name = el.GetAttribute ("DataAccessorName");
1301 //Console.WriteLine ("Name: "+currentAdapter.Name);
1302 foreach (XmlNode n in el.ChildNodes) {
1303 e = n as XmlElement;
1305 //Console.WriteLine ("Children of Tables: "+e.LocalName);
1309 switch (e.LocalName) {
1312 foreach (XmlNode msn in e.ChildNodes)
1313 ProcessDbSource (msn as XmlElement);
1317 DataTableMapping tableMapping = new DataTableMapping ();
1318 tableMapping.SourceTable = "Table";
1319 tableMapping.DataSetTable = datasetTableName;
1321 foreach (XmlNode mps in e.ChildNodes)
1322 ProcessColumnMapping (mps as XmlElement, tableMapping);
1324 currentAdapter.Adapter.TableMappings.Add (tableMapping);
1330 private void ProcessDbSource (XmlElement el)
1340 //Console.WriteLine ("ProcessDbSources: "+el.LocalName);
1342 tmp = el.GetAttribute ("GenerateShortCommands");
1343 //Console.WriteLine ("GenerateShortCommands: {0}", tmp);
1344 if (!String.IsNullOrEmpty (tmp))
1345 currentAdapter.ShortCommands = Convert.ToBoolean (tmp);
1347 DbCommandInfo cmdInfo = new DbCommandInfo ();
1348 tmp = el.GetAttribute ("GenerateMethods");
1349 if (!String.IsNullOrEmpty (tmp)) {
1350 DbSourceMethodInfo mthdInfo = null;
1352 switch ((GenerateMethodsType) Enum.Parse (typeof (GenerateMethodsType), tmp)) {
1353 case GenerateMethodsType.Get:
1354 mthdInfo = new DbSourceMethodInfo ();
1355 mthdInfo.Name = el.GetAttribute ("GetMethodName");
1356 mthdInfo.Modifier = el.GetAttribute ("GetMethodModifier");
1357 if (String.IsNullOrEmpty (mthdInfo.Modifier))
1358 mthdInfo.Modifier = "Public";
1359 mthdInfo.ScalarCallRetval = el.GetAttribute ("ScalarCallRetval");
1360 mthdInfo.QueryType = el.GetAttribute ("QueryType");
1361 mthdInfo.MethodType = GenerateMethodsType.Get;
1362 cmdInfo.Methods = new DbSourceMethodInfo [1];
1363 cmdInfo.Methods[0] = mthdInfo;
1366 case GenerateMethodsType.Fill:
1367 mthdInfo = new DbSourceMethodInfo ();
1368 mthdInfo.Name = el.GetAttribute ("FillMethodName");
1369 mthdInfo.Modifier = el.GetAttribute ("FillMethodModifier");
1370 if (String.IsNullOrEmpty (mthdInfo.Modifier))
1371 mthdInfo.Modifier = "Public";
1372 mthdInfo.ScalarCallRetval = null;
1373 mthdInfo.QueryType = null;
1374 mthdInfo.MethodType = GenerateMethodsType.Fill;
1375 cmdInfo.Methods = new DbSourceMethodInfo [1];
1376 cmdInfo.Methods[0] = mthdInfo;
1379 case GenerateMethodsType.Both:
1380 mthdInfo = new DbSourceMethodInfo ();
1382 mthdInfo.Name = el.GetAttribute ("GetMethodName");
1383 mthdInfo.Modifier = el.GetAttribute ("GetMethodModifier");
1384 if (String.IsNullOrEmpty (mthdInfo.Modifier))
1385 mthdInfo.Modifier = "Public";
1386 mthdInfo.ScalarCallRetval = el.GetAttribute ("ScalarCallRetval");
1387 mthdInfo.QueryType = el.GetAttribute ("QueryType");
1388 mthdInfo.MethodType = GenerateMethodsType.Get;
1389 cmdInfo.Methods = new DbSourceMethodInfo [2];
1390 cmdInfo.Methods[0] = mthdInfo;
1393 mthdInfo = new DbSourceMethodInfo ();
1394 mthdInfo.Name = el.GetAttribute ("FillMethodName");
1395 mthdInfo.Modifier = el.GetAttribute ("FillMethodModifier");
1396 if (String.IsNullOrEmpty (mthdInfo.Modifier))
1397 mthdInfo.Modifier = "Public";
1398 mthdInfo.ScalarCallRetval = null;
1399 mthdInfo.QueryType = null;
1400 mthdInfo.MethodType = GenerateMethodsType.Fill;
1401 cmdInfo.Methods[1] = mthdInfo;
1405 // no Get or Fill methods - non <MainSource> sources
1406 DbSourceMethodInfo mthdInfo = new DbSourceMethodInfo ();
1407 mthdInfo.Name = el.GetAttribute ("Name");
1408 mthdInfo.Modifier = el.GetAttribute ("Modifier");
1409 if (String.IsNullOrEmpty (mthdInfo.Modifier))
1410 mthdInfo.Modifier = "Public";
1411 mthdInfo.ScalarCallRetval = el.GetAttribute ("ScalarCallRetval");
1412 mthdInfo.QueryType = el.GetAttribute ("QueryType");
1413 mthdInfo.MethodType = GenerateMethodsType.None;
1414 // Add MethodInfo to DbCommandInfo
1415 cmdInfo.Methods = new DbSourceMethodInfo [1];
1416 cmdInfo.Methods[0] = mthdInfo;
1419 foreach (XmlNode n in el.ChildNodes) {
1420 e = n as XmlElement;
1425 switch (e.LocalName) {
1426 case "SelectCommand":
1427 cmdInfo.Command = ProcessDbCommand (e.FirstChild as XmlElement);
1428 currentAdapter.Commands.Add (cmdInfo);
1430 case "InsertCommand":
1431 currentAdapter.Adapter.InsertCommand = ProcessDbCommand (e.FirstChild as XmlElement);
1433 case "UpdateCommand":
1434 currentAdapter.Adapter.UpdateCommand = ProcessDbCommand (e.FirstChild as XmlElement);
1436 case "DeleteCommand":
1437 currentAdapter.Adapter.DeleteCommand = ProcessDbCommand (e.FirstChild as XmlElement);
1443 private DbCommand ProcessDbCommand (XmlElement el)
1446 //Console.WriteLine (el.LocalName);
1447 string cmdText = null;
1448 string cmdType = null;
1449 ArrayList parameters = null;
1454 cmdType = el.GetAttribute ("CommandType");
1455 foreach (XmlNode n in el.ChildNodes) {
1456 e = n as XmlElement;
1457 if (e != null && e.LocalName == "CommandText")
1458 cmdText = e.InnerText;
1459 else if (e != null && e.LocalName == "Parameters" && !e.IsEmpty)
1460 parameters = ProcessDbParameters (e);
1463 DbCommand cmd = currentAdapter.Provider.CreateCommand ();
1464 cmd.CommandText = cmdText;
1465 if (cmdType == "StoredProcedure")
1466 cmd.CommandType = CommandType.StoredProcedure;
1468 cmd.CommandType = CommandType.Text;
1470 if (parameters != null)
1471 cmd.Parameters.AddRange (parameters.ToArray ());
1473 //Console.WriteLine ("Parameters count: {0}", cmd.Parameters.Count);
1477 private ArrayList ProcessDbParameters (XmlElement el)
1479 //Console.WriteLine ("ProcessDbParameters: "+el.LocalName);
1482 DbParameter param = null;
1483 ArrayList parameters = new ArrayList ();
1488 foreach (XmlNode n in el.ChildNodes) {
1489 e = n as XmlElement;
1493 param = currentAdapter.Provider.CreateParameter ();
1495 tmp = e.GetAttribute ("AllowDbNull");
1496 if (!String.IsNullOrEmpty (tmp))
1497 param.IsNullable = Convert.ToBoolean (tmp);
1499 param.ParameterName = e.GetAttribute ("ParameterName");
1500 tmp = e.GetAttribute ("ProviderType");
1501 if (!String.IsNullOrEmpty (tmp))
1502 tmp = e.GetAttribute ("DbType");
1503 param.FrameworkDbType = tmp;
1505 tmp = e.GetAttribute ("Direction");
1506 param.Direction = (ParameterDirection) Enum.Parse (typeof (ParameterDirection), tmp);
1508 ((IDbDataParameter)param).Precision = Convert.ToByte (e.GetAttribute ("Precision"));
1509 ((IDbDataParameter)param).Scale = Convert.ToByte (e.GetAttribute ("Scale"));
1510 param.Size = Convert.ToInt32 (e.GetAttribute ("Size"));
1511 param.SourceColumn = e.GetAttribute ("SourceColumn");
1513 tmp = e.GetAttribute ("SourceColumnNullMapping");
1514 if (!String.IsNullOrEmpty (tmp))
1515 param.SourceColumnNullMapping = Convert.ToBoolean (tmp);
1517 tmp = e.GetAttribute ("SourceVersion");
1518 param.SourceVersion = (DataRowVersion) Enum.Parse (typeof (DataRowVersion), tmp);
1519 parameters.Add (param);
1525 private void ProcessColumnMapping (XmlElement el, DataTableMapping tableMapping)
1530 tableMapping.ColumnMappings.Add (el.GetAttribute ("SourceColumn"),
1531 el.GetAttribute ("DataSetColumn"));
1536 private void HandleRelationshipAnnotation (XmlElement el, bool nested)
1538 string name = el.GetAttribute ("name");
1539 string ptn = el.GetAttribute ("parent", XmlConstants.MsdataNamespace);
1540 string ctn = el.GetAttribute ("child", XmlConstants.MsdataNamespace);
1541 string pkn = el.GetAttribute ("parentkey", XmlConstants.MsdataNamespace);
1542 string fkn = el.GetAttribute ("childkey", XmlConstants.MsdataNamespace);
1544 RelationStructure rel = new RelationStructure ();
1545 rel.ExplicitName = XmlHelper.Decode (name);
1546 rel.ParentTableName = XmlHelper.Decode (ptn);
1547 rel.ChildTableName = XmlHelper.Decode (ctn);
1548 // ColumnNames will be decoded wherever they are used as they can
1549 // contain 'space' separated list of column-names.
1550 rel.ParentColumnName = pkn;
1551 rel.ChildColumnName = fkn;
1552 rel.IsNested = nested;
1553 rel.CreateConstraint = false; // by default?
1554 relations.Add (rel);
1557 private object GetElementDefaultValue (XmlSchemaElement elem)
1559 // Unlike attribute, element cannot have a default value.
1560 if (elem.RefName == XmlQualifiedName.Empty)
1561 return elem.DefaultValue;
1562 XmlSchemaElement referenced = schema.Elements [elem.RefName] as XmlSchemaElement;
1563 if (referenced == null) // considering missing sub components
1565 return referenced.DefaultValue;
1568 private object GetAttributeDefaultValue (XmlSchemaAttribute attr)
1570 #if BUGGY_MS_COMPATIBLE
1573 else if (attr.RefName != XmlQualifiedName.Empty) {
1574 XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1575 if (referenced != null)
1576 return referenced.DefaultValue;
1580 if (attr.DefaultValue != null)
1581 return attr.DefaultValue;
1582 return attr.FixedValue;
1584 if (attr.DefaultValue != null)
1585 return attr.DefaultValue;
1586 else if (attr.FixedValue != null)
1587 return attr.FixedValue;
1588 else if (attr.RefName == XmlQualifiedName.Empty)
1590 XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1591 if (referenced == null) // considering missing sub components
1593 if (referenced.DefaultValue != null)
1594 return referenced.DefaultValue;
1595 return referenced.FixedValue;