Пример #1
0
        void Write51_XmlSchemaUnique(XmlSchemaUnique o)
        {
            if ((object)o == null)
            {
                return;
            }
            System.Type t = o.GetType();
            WriteStartElement("unique");

            WriteAttribute(@"id", @"", ((System.String)o.@Id));
            WriteAttribute(@"name", @"", ((System.String)o.@Name));
            WriteAttributes((XmlAttribute[])o.@UnhandledAttributes, o);
            Write5_XmlSchemaAnnotation((XmlSchemaAnnotation)o.@Annotation);
            Write49_XmlSchemaXPath("selector", "", (XmlSchemaXPath)o.@Selector);
            XmlSchemaObjectCollection a = (XmlSchemaObjectCollection)o.@Fields;

            if (a != null)
            {
                for (int ia = 0; ia < a.Count; ia++)
                {
                    Write49_XmlSchemaXPath("field", "", (XmlSchemaXPath)a[ia]);
                }
            }
            WriteEndElement();
        }
Пример #2
0
        private void WriteUniques(XmlSchema schema, XmlSchemaElement element, CremaDataTable dataTable)
        {
            foreach (var item in dataTable.Columns)
            {
                if (item.Unique == false)
                {
                    continue;
                }

                if (item.IsKey == true && dataTable.PrimaryKey.Length == 1)
                {
                    continue;
                }

                var unique = new XmlSchemaUnique()
                {
                    Name     = dataTable.Name + "." + item.ColumnName + "." + "Unique",
                    Selector = new XmlSchemaXPath()
                };
                unique.Selector.XPath = dataTable.GetSelectorXPath(CremaSchema.TableTypePrefix, schema.TargetNamespace);

                var field = new XmlSchemaXPath()
                {
                    XPath = CremaSchema.TableTypePrefix + ":" + item.ColumnName
                };
                unique.Fields.Add(field);
                element.Constraints.Add(unique);
            }
        }
Пример #3
0
        /// <summary>
        /// Creates a constraint that forces a set of columns to be unique.
        /// </summary>
        /// <param name="dataModelSchema">The parent data model schema.</param>
        /// <param name="xmlSchemaUnique">The XmlSchema description of the constraint.</param>
        public UniqueConstraintSchema(DataModelSchema dataModelSchema, XmlSchemaUnique xmlSchemaUnique)
            : base(dataModelSchema, xmlSchemaUnique)
        {
            // This determines whether the constraint should be used as the primary key for a table.
            XmlAttribute xmlAttribute = ObjectSchema.GetUnhandledAttribute(xmlSchemaUnique, QualifiedName.PrimaryKey);

            this.isPrimaryKey = xmlAttribute == null ? false : Convert.ToBoolean(xmlAttribute.Value);
        }
Пример #4
0
        public static XmlSchemaIdentityConstraint FindKey(XmlSchemaKeyref xmlSchemaKeyref)
        {
            foreach (XmlSchemaObject xmlSchemaObject in GetXmlSchema(xmlSchemaKeyref).Items)
            {
                if (xmlSchemaObject is XmlSchemaElement)
                {
                    XmlSchemaElement xmlSchemaElement = xmlSchemaObject as XmlSchemaElement;

                    if (IsDataSetElement(xmlSchemaElement))
                    {
                        XmlSchemaElement dataSetElement = xmlSchemaObject as XmlSchemaElement;
                        foreach (XmlSchemaIdentityConstraint xmlSchemaIdentityConstraint in dataSetElement.Constraints)
                        {
                            if (xmlSchemaIdentityConstraint is XmlSchemaUnique)
                            {
                                XmlSchemaUnique xmlSchemaUnique = xmlSchemaIdentityConstraint as XmlSchemaUnique;
                                if (xmlSchemaUnique.QualifiedName.Namespace == xmlSchemaKeyref.Refer.Namespace &&
                                    xmlSchemaUnique.QualifiedName.Name == xmlSchemaKeyref.Refer.Name)
                                {
                                    return(xmlSchemaUnique);
                                }
                            }
                            if (xmlSchemaIdentityConstraint is XmlSchemaKey)
                            {
                                XmlSchemaKey xmlSchemaKey = xmlSchemaIdentityConstraint as XmlSchemaKey;
                                if (xmlSchemaKey.QualifiedName.Namespace == xmlSchemaKeyref.Refer.Namespace &&
                                    xmlSchemaKey.QualifiedName.Name == xmlSchemaKeyref.Refer.Name)
                                {
                                    return(xmlSchemaKey);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (XmlSchemaIdentityConstraint xmlSchemaIdentityConstraint in xmlSchemaElement.Constraints)
                        {
                            if (xmlSchemaIdentityConstraint is XmlSchemaKey)
                            {
                                XmlSchemaKey xmlSchemaKey = xmlSchemaIdentityConstraint as XmlSchemaKey;
                                if (xmlSchemaKey.QualifiedName.Namespace == xmlSchemaKeyref.Refer.Namespace &&
                                    xmlSchemaKey.QualifiedName.Name == xmlSchemaKeyref.Refer.Name)
                                {
                                    return(xmlSchemaKey);
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Пример #5
0
        private void WriteUniques(XmlSchema schema, XmlSchemaElement element, CremaDataTable dataTable)
        {
            //var query = from item in dataTable.Columns
            //            where item.Unique == true
            //            select item;

            //if (query.Any() == false)
            //    return;

            //var unique = new XmlSchemaUnique()
            //{
            //    Name = dataTable.UniqueTypeName,
            //    Selector = new XmlSchemaXPath()
            //};
            //unique.Selector.XPath = dataTable.GetSelectorXPath(CremaSchema.TableTypePrefix, schema.TargetNamespace);

            //foreach (var item in query)
            //{
            //    var field = new XmlSchemaXPath()
            //    {
            //        XPath = CremaSchema.TableTypePrefix + ":" + item.ColumnName
            //    };
            //    unique.Fields.Add(field);
            //}
            //element.Constraints.Add(unique);

            foreach (var item in dataTable.Columns)
            {
                if (item.Unique == false)
                {
                    continue;
                }

                if (item.IsKey == true && dataTable.PrimaryKey.Length == 1)
                {
                    continue;
                }

                var unique = new XmlSchemaUnique()
                {
                    Name     = dataTable.Name + "." + item.ColumnName + "." + "Unique",
                    Selector = new XmlSchemaXPath()
                };
                unique.Selector.XPath = dataTable.GetSelectorXPath(CremaSchema.TableTypePrefix, schema.TargetNamespace);

                var field = new XmlSchemaXPath()
                {
                    XPath = CremaSchema.TableTypePrefix + ":" + item.ColumnName
                };
                unique.Fields.Add(field);
                element.Constraints.Add(unique);
            }
        }
Пример #6
0
        /// <summary>
        /// Creates a collection of tables found in the schema.
        /// </summary>
        /// <param name="xmlSchema">The schema that describes the data model.</param>
        /// <returns>A list of TableSchema objects that describe the tables found in the data model schema.</returns>
        private void FirstPass(XmlSchemaSet xmlSchemaSet)
        {
            // Scan through the schema set looking for table elements.  These can either be defined at the root element for a standard
            // schema or they can be found as choices of a special element describing a 'DataSet' for the Microsoft version of a
            // schema.
            foreach (XmlSchemaElement xmlSchemaElement in xmlSchemaSet.GlobalElements.Values)
            {
                // If the element read from the schema is the Microsoft DataSet element, then the tables are described as choices
                // associated with an implicit complex type on that element.
                if (ObjectSchema.IsDataSetElement(xmlSchemaElement))
                {
                    // The tables are described as an choices of an implicit (nested) complex type.
                    if (xmlSchemaElement.SchemaType is XmlSchemaComplexType)
                    {
                        // The complex type describes the table.
                        XmlSchemaComplexType xmlSchemaComplexType = xmlSchemaElement.SchemaType as XmlSchemaComplexType;

                        // The data model is described as a set of one or more choices of tables.
                        if (xmlSchemaComplexType.Particle is XmlSchemaChoice)
                        {
                            // The data model is described as a set of choices.  Each choice represents a table.
                            XmlSchemaChoice xmlSchemaChoice = xmlSchemaComplexType.Particle as XmlSchemaChoice;

                            // Create a table for each of the choices described in the complex type.
                            foreach (XmlSchemaObject choiceObject in xmlSchemaChoice.Items)
                            {
                                if (choiceObject is XmlSchemaElement)
                                {
                                    XmlSchemaElement tableElement = choiceObject as XmlSchemaElement;
                                    this.tableList.Add(tableElement.Name, new TableSchema(this, tableElement));
                                }
                            }
                        }
                    }

                    // The constraints describe the columns that are unique for a table.
                    foreach (XmlSchemaIdentityConstraint xmlSchemaIdentityConstraint in xmlSchemaElement.Constraints)
                    {
                        // This describes the columns that must be unique within a table.
                        if (xmlSchemaIdentityConstraint is XmlSchemaUnique)
                        {
                            XmlSchemaUnique        xmlSchemaUnique        = xmlSchemaIdentityConstraint as XmlSchemaUnique;
                            UniqueConstraintSchema uniqueConstraintSchema = new UniqueConstraintSchema(this, xmlSchemaUnique);
                            uniqueConstraintSchema.Table.Add(uniqueConstraintSchema);
                        }
                    }
                }
            }
        }
Пример #7
0
        private void ReadUnique(XmlSchemaUnique unique)
        {
            var dataTable = this.GetTable(unique, CremaSchema.UniqueTypeNameExtension);

            lock (CremaSchema.lockobj)
            {
                foreach (var item in unique.GetFields())
                {
                    var columnName = item.XPath.Replace(CremaSchema.TableTypePrefix + ":", string.Empty);
                    var dataColumn = dataTable.Columns[columnName];

                    dataColumn.InternalUnique = true;
                }
            }
        }
Пример #8
0
 private void Write51_XmlSchemaUnique(XmlSchemaUnique o)
 {
     if (o != null)
     {
         o.GetType();
         this.WriteStartElement("unique");
         this.WriteAttribute("id", "", o.Id);
         this.WriteAttribute("name", "", o.Name);
         this.WriteAttributes(o.UnhandledAttributes, o);
         this.Write5_XmlSchemaAnnotation(o.Annotation);
         this.Write49_XmlSchemaXPath("selector", "", o.Selector);
         XmlSchemaObjectCollection fields = o.Fields;
         if (fields != null)
         {
             for (int i = 0; i < fields.Count; i++)
             {
                 this.Write49_XmlSchemaXPath("field", "", (XmlSchemaXPath)fields[i]);
             }
         }
         this.WriteEndElement();
     }
 }
Пример #9
0
        public bool GetPrimaryKeyStatus(XmlSchemaIdentityConstraint xmlSchemaIdentityConstraint)
        {
            if (xmlSchemaIdentityConstraint is XmlSchemaUnique)
            {
                XmlSchemaUnique xmlSchemaUnique = xmlSchemaIdentityConstraint as XmlSchemaUnique;
                if (xmlSchemaUnique.UnhandledAttributes != null)
                {
                    foreach (XmlAttribute xmlAttribute in xmlSchemaUnique.UnhandledAttributes)
                    {
                        if (xmlAttribute.NamespaceURI == KeySchema.primaryKeyAttributeName.Namespace &&
                            xmlAttribute.LocalName == KeySchema.primaryKeyAttributeName.Name)
                        {
                            return(Convert.ToBoolean(xmlAttribute.Value));
                        }
                    }
                }
            }

            if (xmlSchemaIdentityConstraint is XmlSchemaKey)
            {
                XmlSchemaKey xmlSchemaKey = xmlSchemaIdentityConstraint as XmlSchemaKey;
                if (xmlSchemaKey.UnhandledAttributes != null)
                {
                    foreach (XmlAttribute xmlAttribute in xmlSchemaKey.UnhandledAttributes)
                    {
                        if (xmlAttribute.NamespaceURI == KeySchema.primaryKeyAttributeName.Namespace &&
                            xmlAttribute.LocalName == KeySchema.primaryKeyAttributeName.Name)
                        {
                            return(Convert.ToBoolean(xmlAttribute.Value));
                        }
                    }
                }
            }

            return(false);
        }
Пример #10
0
    public static void Main()
    {
        XmlSchema schema = new XmlSchema();

        // <xs:complexType name="customerOrderType">
        XmlSchemaComplexType customerOrderType = new XmlSchemaComplexType();

        customerOrderType.Name = "customerOrderType";

        // <xs:sequence>
        XmlSchemaSequence sequence1 = new XmlSchemaSequence();

        // <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
        XmlSchemaElement item = new XmlSchemaElement();

        item.MinOccurs       = 0;
        item.MaxOccursString = "unbounded";
        item.Name            = "item";

        // <xs:complexType>
        XmlSchemaComplexType ct1 = new XmlSchemaComplexType();

        // <xs:attribute name="itemID" type="xs:string"/>
        XmlSchemaAttribute itemID = new XmlSchemaAttribute();

        itemID.Name           = "itemID";
        itemID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        // </xs:complexType>
        ct1.Attributes.Add(itemID);

        // </xs:element>
        item.SchemaType = ct1;

        // </xs:sequence>
        sequence1.Items.Add(item);
        customerOrderType.Particle = sequence1;

        // <xs:attribute name="CustomerID" type="xs:string"/>
        XmlSchemaAttribute CustomerID = new XmlSchemaAttribute();

        CustomerID.Name           = "CustomerID";
        CustomerID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

        customerOrderType.Attributes.Add(CustomerID);

        // </xs:complexType>
        schema.Items.Add(customerOrderType);

        // <xs:element name="ordersByCustomer">
        XmlSchemaElement ordersByCustomer = new XmlSchemaElement();

        ordersByCustomer.Name = "ordersByCustomer";

        // <xs:complexType>
        XmlSchemaComplexType ct2 = new XmlSchemaComplexType();

        // <xs:sequence>
        XmlSchemaSequence sequence2 = new XmlSchemaSequence();

        // <xs:element name="customerOrders" type="customerOrderType" minOccurs="0" maxOccurs="unbounded" />
        XmlSchemaElement customerOrders = new XmlSchemaElement();

        customerOrders.MinOccurs       = 0;
        customerOrders.MaxOccursString = "unbounded";
        customerOrders.Name            = "customerOrders";
        customerOrders.SchemaTypeName  = new XmlQualifiedName("customerOrderType", "");

        // </xs:sequence>
        sequence2.Items.Add(customerOrders);

        // </xs:complexType>
        ct2.Particle = sequence2;
        ordersByCustomer.SchemaType = ct2;

        // <xs:unique name="oneCustomerOrdersforEachCustomerID">
        XmlSchemaUnique element_unique = new XmlSchemaUnique();

        element_unique.Name = "oneCustomerOrdersforEachCustomerID";

        // <xs:selector xpath="customerOrders"/>
        element_unique.Selector       = new XmlSchemaXPath();
        element_unique.Selector.XPath = "customerOrders";

        // <xs:field xpath="@customerID"/>
        XmlSchemaXPath field = new XmlSchemaXPath();

        field.XPath = "@customerID";

        // </xs:unique>
        element_unique.Fields.Add(field);
        ordersByCustomer.Constraints.Add(element_unique);

        // </xs:element>
        schema.Items.Add(ordersByCustomer);

        XmlSchemaSet schemaSet = new XmlSchemaSet();

        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
        schemaSet.Add(schema);
        schemaSet.Compile();

        XmlSchema compiledSchema = null;

        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema.Write(Console.Out, nsmgr);
    }
Пример #11
0
 private static void Equal(XmlSchemaUnique expected, XmlSchemaUnique actual)
 {
     throw new NotImplementedException();
 }
Пример #12
0
 public UniqueSchema(DataModelSchema schema, XmlSchemaUnique xmlSchemaUnique)
     : base(schema, xmlSchemaUnique)
 {
     // Initialize the object
     this.xmlSchemaUnique = xmlSchemaUnique;
 }
Пример #13
0
 protected override void Visit(XmlSchemaUnique unique)
 {
     _writer.WriteConstraintRow(_context, ArtItem.Unique, "Unique", unique);
 }
Пример #14
0
 protected virtual void Visit(XmlSchemaUnique unique)
 {
     Traverse(unique.Selector);
     Traverse(unique.Fields);
 }
Пример #15
0
        private void handleObject(XmlSchemaObject o)
        {
            string str = "unknown";
            XmlSchemaObjectCollection children = new XmlSchemaObjectCollection();

            if (o is XmlSchema)
            {
                str      = "root";
                children = ((XmlSchema)o).Items;
            }
            else if (o is XmlSchemaComplexType)
            {
                XmlSchemaComplexType type = (XmlSchemaComplexType)o;
                startNewDbFile(type);
                str      = type.Name;
                children = type.Attributes;
                infos    = new List <TypeInfo> ();
            }
            else if (o is XmlSchemaAttribute)
            {
                XmlSchemaAttribute attribute = (XmlSchemaAttribute)o;
                addDbAttribute(attribute);
                str = string.Format("{0} ({1})", attribute.Name, attribute.AttributeSchemaType.TypeCode);
            }
            else if (o is XmlSchemaElement)
            {
                // children = ((XmlSchemaElement)o).
                XmlSchemaElement element = (XmlSchemaElement)o;
                foreach (XmlSchemaObject constraint in element.Constraints)
                {
                    if (constraint is XmlSchemaUnique)
                    {
                        XmlSchemaUnique unique = (XmlSchemaUnique)constraint;
                        List <string>   fields = new List <string> (unique.Fields.Count);
                        foreach (XmlSchemaObject field in unique.Fields)
                        {
                            if (field is XmlSchemaXPath)
                            {
                                fields.Add(((XmlSchemaXPath)field).XPath);
                            }
                        }
                        constraints.Add(new TableConstraint
                        {
                            Name   = unique.Name,
                            Table  = unique.Selector.XPath,
                            Fields = fields
                        });
                    }
                    else if (constraint is XmlSchemaKeyref)
                    {
                        XmlSchemaKeyref reference = (XmlSchemaKeyref)constraint;
                        string          fromTable = reference.Selector.XPath.Substring(3).Replace("_tables", "");
                        string          fromRow   = ((XmlSchemaXPath)reference.Fields [0]).XPath.Substring(1);
                        try {
                            TableReferenceEntry tableRef = resolveReference(reference.Name, fromTable, fromRow, reference.Refer.Name);
                            if (tableRef != null)
                            {
                                Console.WriteLine("{0}#{1} - {2}#{3}", tableRef.fromTable, tableRef.fromTableIndex, tableRef.toTable, tableRef.toTableIndex);
                            }
                            else
                            {
                                Console.WriteLine("could not resolve reference");
                            }
                        } catch (Exception x) {
                            Console.WriteLine(x);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("unknown type: {0}", o);
            }

            if (o is XmlSchemaAnnotated && ((XmlSchemaAnnotated)o).UnhandledAttributes != null)
            {
                string attlist = "";
                new List <XmlAttribute> (((XmlSchemaAnnotated)o).UnhandledAttributes).ForEach(uh => attlist += " " + uh);
                str = string.Format("{0} (unhandled: {1})", str, attlist);
            }

            foreach (XmlSchemaObject child in children)
            {
                handleObject(child);
            }
        }
Пример #16
0
        private CremaDataTable GetTable(XmlSchemaUnique constraint, string extension)
        {
            if (this.version.Major == CremaSchema.MajorVersion)
            {
                var keyName = constraint.Name.Substring(0, constraint.Name.Length - extension.Length);
                if (this.version.Minor >= 5) // 3.5
                {
                    var items = StringUtility.Split(keyName, '.');
                    keyName = string.Join(".", items.Take(items.Length - 1));
                }

                if (this.itemName == null)
                {
                    return(this.tables[keyName]);
                }
                else
                {
                    var tableName = CremaDataSet.GetTableName(this.dataSet, constraint.QualifiedName.Namespace);

                    if (keyName == tableName)
                    {
                        return(this.tables[this.itemName.Name]);
                    }
                    else
                    {
                        tableName = Regex.Replace(keyName, string.Format("(^{0})([.].*)", tableName), this.itemName.Name + "$2");
                        return(this.tables[tableName]);
                    }
                }
            }
            else
            {
                var keyName = GetTableNameObsolete(constraint);

                if (this.itemName == null)
                {
                    var tableName    = CremaDataSet.GetTableName(CremaSchemaObsolete.TableNamespaceObsolete, constraint.QualifiedName.Namespace);
                    var categoryPath = CremaDataSet.GetTableCategoryPath(CremaSchemaObsolete.TableNamespaceObsolete, constraint.QualifiedName.Namespace);
                    if (keyName == tableName)
                    {
                        return(this.dataSet.Tables[tableName, categoryPath]);
                    }
                    else
                    {
                        return(this.dataSet.Tables[tableName + "." + keyName, categoryPath]);
                    }
                }
                else
                {
                    var tableName    = CremaDataSet.GetTableName(CremaSchemaObsolete.TableNamespaceObsolete, constraint.QualifiedName.Namespace);
                    var categoryPath = CremaDataSet.GetTableCategoryPath(CremaSchemaObsolete.TableNamespaceObsolete, constraint.QualifiedName.Namespace);
                    if (keyName == tableName)
                    {
                        return(this.dataSet.Tables[itemName.Name, itemName.CategoryPath]);
                    }
                    else
                    {
                        return(this.dataSet.Tables[itemName.Name + "." + keyName, itemName.CategoryPath]);
                    }
                }
            }
            throw new CremaDataException();
        }