/// <summary> /// Create a table schema from the XML Schema specification. /// </summary> /// <param name="dataModelSchema">The data model to which this table belongs.</param> /// <param name="xmlSchemaElement">The root of the XmlSchema element that describes the table.</param> public TableSchema(DataModelSchema dataModelSchema, XmlSchemaElement xmlSchemaElement) : base(dataModelSchema, xmlSchemaElement) { // Initialize the object. this.dataModelSchema = dataModelSchema; this.isPersistent = GetPersistentIndicator(xmlSchemaElement); this.name = xmlSchemaElement.Name; this.columnList = new SortedList <string, ColumnSchema>(); this.constraintList = new SortedList <string, ConstraintSchema>(); this.childRelationList = new SortedList <string, RelationSchema>(); this.parentRelationList = new SortedList <string, RelationSchema>(); // Every table has a row version column which tracks the history of changes to the row. ColumnSchema rowVersionSchema = new ColumnSchema(this, "RowVersion", typeof(Int64), false, true, DBNull.Value, Int32.MaxValue); this.columnList.Add(rowVersionSchema.Name, rowVersionSchema); // Initialize the columns of the table. Initialize(xmlSchemaElement); }
/// <summary> /// Creates the element that describes a column in a table. /// </summary> /// <param name="columnSchema">A description of a column.</param> /// <returns>An element that can be used in an XML Schema document to describe a column.</returns> private static XElement CreateColumn(ColumnSchema columnSchema) { string dataType = String.Empty; string xmlType = String.Empty; object defaultValue = null; switch (columnSchema.DataType.ToString()) { case "System.Object": xmlType = "xs:anyType"; break; case "System.Int32": defaultValue = 0; xmlType = "xs:int"; break; case "System.Int64": defaultValue = 0L; xmlType = "xs:long"; break; case "System.Decimal": defaultValue = 0.0M; xmlType = "xs:decimal"; break; case "System.Boolean": defaultValue = false; xmlType = "xs:boolean"; break; case "System.String": defaultValue = String.Empty; xmlType = "xs:string"; break; case "System.DateTime": xmlType = "xs:dateTime"; break; case "System.Byte[]": xmlType = "xs:base64Binary"; break; default: dataType = columnSchema.DataType.AssemblyQualifiedName; xmlType = "xs:anyType"; break; } // <xs:element name="UserId" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" // msprop:Generator_UserColumnName="UserId" msprop:Generator_ColumnVarNameInTable="columnUserId" msprop:Generator_ColumnPropNameInRow="UserId" // msprop:Generator_ColumnPropNameInTable="UserIdColumn" type="xs:string" minOccurs="0" /> XElement columnElement = new XElement( SchemaScrubber.xs + "element", new XAttribute("name", columnSchema.Name)); // Microsoft uses a custom decoration to describe data types that are not part of the cannon XML Schema datatypes. if (dataType != string.Empty) { columnElement.Add(new XAttribute(SchemaScrubber.msdata + "DataType", dataType)); } // These are Microsoft added decorations for the schema that describe how a generated DataSet should name the internal and external values. columnElement.Add( new XAttribute(SchemaScrubber.msprop + "Generator_UserColumnName", columnSchema.Name), new XAttribute(SchemaScrubber.msprop + "Generator_ColumnPropNameInRow", columnSchema.Name), new XAttribute(SchemaScrubber.msprop + "Generator_ColumnVarNameInTable", String.Format("column{0}", columnSchema.Name)), new XAttribute(SchemaScrubber.msprop + "Generator_ColumnPropNameInTable", String.Format("{0}Column", columnSchema.Name))); if (columnSchema.MaximumLength == int.MaxValue) { columnElement.Add(new XAttribute("type", xmlType)); } else { // <xs:simpleType> // <xs:restriction base="xs:string"> // <xs:maxLength value="128" /> // </xs:restriction> // </xs:simpleType> columnElement.Add( new XElement( SchemaScrubber.xs + "simpleType", new XElement( SchemaScrubber.xs + "restriction", new XAttribute("base", xmlType), new XElement( SchemaScrubber.xs + "maxLength", new XAttribute("value", columnSchema.MaximumLength))))); } // An optional column is identified with a 'minOccurs=0' attribute. if (columnSchema.IsNullable) { columnElement.Add(new XAttribute("minOccurs", 0)); } // Provide an explicit default value for all column elements. if (!columnSchema.IsNullable && defaultValue != null) { columnElement.Add(new XAttribute("default", defaultValue)); } // This describes the column of a table. return(columnElement); }