Esempio n. 1
0
		private static Association CreateSSDLAssociation(IConstraint constraint)
		{
			string associationName = constraint.Name.Replace('.', '_');
			
			Association association = new Association()
			{
				Name = associationName,
				AssociationSetName = associationName
			};

            Role role1 = new Role()
            {
                Name = constraint.PKTableName,
                Cardinality = (Cardinality)constraint.PKCardinality
            };

            foreach (IColumn pkColumn in constraint.PKColumns)
            {
                Property role1Property = CreateSSDLProperty(pkColumn, CreateSSDLEntityType(constraint.PKTable));
                role1.Properties.Add(role1Property);
                role1.Type = role1Property.EntityType;
            }

            association.Role1 = role1;
            association.PrincipalRole = role1;

            Role role2 = new Role()
            {
                Name = constraint.FKTableName,
                Cardinality = (Cardinality)constraint.FKCardinality
            };

            foreach (IColumn fkColumn in constraint.FKColumns)
            {
                Property role2Property = CreateSSDLProperty(fkColumn, CreateSSDLEntityType(constraint.FKTable));
                role2.Properties.Add(role2Property);
                role2.Type = role2Property.EntityType;
            }

            association.Role2 = role2;
            association.DependantRole = role2;

            return association;
        }
Esempio n. 2
0
        public static SSDLContainer ReadXElement(XElement ssdlXElement)
        {
            XElement schemaElement = ssdlXElement.Element(XName.Get("StorageModels", edmxNamespace.NamespaceName)).Element(XName.Get("Schema", ssdlNamespace.NamespaceName));
            
            if (schemaElement == null || schemaElement.IsEmpty)
            	return null;
            
            XElement entityContainerElement = schemaElement.Element(XName.Get("EntityContainer", ssdlNamespace.NamespaceName));

            var value = new SSDLContainer { Namespace = schemaElement.Attribute("Namespace").Value, Name = entityContainerElement.Attribute("Name").Value };
            SetStringValueFromAttribute(schemaElement, "Provider", provider => value.Provider = provider);
            SetStringValueFromAttribute(schemaElement, "ProviderManifestToken", provider => value.ProviderManifestToken = provider);

            #region EntitySets

            foreach (var entitySetElement in entityContainerElement.Elements(XName.Get("EntitySet", ssdlNamespace.NamespaceName)))
            {
                var entityType = new EntityType { EntitySetName = entitySetElement.Attribute("Name").Value };
                var entityTypeName = GetName(entitySetElement.Attribute("EntityType").Value);
                entityType.Name = entityTypeName;
                SetEnumValueFromAttribute<StoreType>(entitySetElement, "Type", storeNamespace.NamespaceName, storeType => entityType.StoreType = storeType);
                SetStringValueFromAttribute(entitySetElement, "Schema", schema => entityType.Schema = schema);
                SetStringValueFromAttribute(entitySetElement, "Name", storeNamespace.NamespaceName, storeName => entityType.StoreName = storeName);
                SetStringValueFromAttribute(entitySetElement, "Schema", storeNamespace.NamespaceName, storeSchema => entityType.StoreSchema = storeSchema);
                SetStringValueFromAttribute(entitySetElement, "Table", table => entityType.Table = table);
                SetStringValueFromElement(entitySetElement, "DefiningQuery", ssdlNamespace.NamespaceName, query => entityType.DefiningQuery = query);

                #region Properties
                var entityTypeElement = schemaElement.Elements(XName.Get("EntityType", ssdlNamespace.NamespaceName)).First(etElement => etElement.Attribute("Name").Value == entityTypeName);
                foreach (var propertyElement in entityTypeElement.Elements(XName.Get("Property", ssdlNamespace.NamespaceName)))
                {
                    var name = propertyElement.Attribute("Name").Value;
                    var property = new Property(entityType) { Name = name, Type = propertyElement.Attribute("Type").Value, IsKey = entityTypeElement.Element(XName.Get("Key", ssdlNamespace.NamespaceName)).Elements(XName.Get("PropertyRef", ssdlNamespace.NamespaceName)).Any(pr => pr.Attribute("Name").Value == name) };
                    SetBoolValueFromAttribute(propertyElement, "Nullable", nullable => property.Nullable = nullable);
                    SetIntValueFromAttribute(propertyElement, "MaxLength", maxLength => property.MaxLength = maxLength);
                    SetBoolValueFromAttribute(propertyElement, "FixedLength", fixedLength => property.FixedLength = fixedLength);
                    SetIntValueFromAttribute(propertyElement, "Precision", precision => property.Precision = precision);
                    SetIntValueFromAttribute(propertyElement, "Scale", scale => property.Scale = scale);
                    SetStringValueFromAttribute(propertyElement, "Collation", collation => property.Collation = collation);
                    SetStringValueFromAttribute(propertyElement, "DefaultValue", defaultValue => property.DefaultValue = defaultValue);
                    SetBoolValueFromAttribute(propertyElement, "Unicode", unicode => property.Unicode = unicode);
                    SetEnumValueFromAttribute<StoreGeneratedPattern>(propertyElement, "StoreGeneratedPattern", storeGeneratedPattern => property.StoreGeneratedPattern = storeGeneratedPattern);
                    SetStringValueFromAttribute(propertyElement, "Name", storeNamespace.NamespaceName, storeName => property.StoreName = storeName);
                    SetStringValueFromAttribute(propertyElement, "Schema", storeNamespace.NamespaceName, storeSchema => property.StoreSchema = storeSchema);
                    SetStringValueFromAttribute(propertyElement, "Type", storeNamespace.NamespaceName, storeType => property.StoreType = storeType);
                    entityType.Properties.Add(property);
                }
                #endregion Properties

                value.EntityTypes.Add(entityType);
            }

            #endregion EntitySets

            #region AssociationSets

            foreach (var associationSetElement in entityContainerElement.Elements(XName.Get("AssociationSet", ssdlNamespace.NamespaceName)))
            {
                var association = new Association { AssociationSetName = associationSetElement.Attribute("Name").Value };
                var associationName = GetName(associationSetElement.Attribute("Association").Value);
                association.Name = associationName;

                #region Roles
                var associationElement = schemaElement.Elements(XName.Get("Association", ssdlNamespace.NamespaceName)).First(aElement => aElement.Attribute("Name").Value == associationName);
                bool isPrincipal = true;
                foreach (var roleElement in associationSetElement.Elements(XName.Get("End", ssdlNamespace.NamespaceName)))
                {
                    var roleName = roleElement.Attribute("Role").Value;
                    var role = new Role { Name = roleName, Type = value.EntityTypes.GetByName(roleElement.Attribute("EntitySet").Value) };
                    SetCardinalityValueFromAttribute(associationElement.Elements(XName.Get("End", ssdlNamespace.NamespaceName)).First(are => are.Attribute("Role").Value == roleName), cardinality => role.Cardinality = cardinality);
                    var referentialConstraintElement = associationElement.Element(XName.Get("ReferentialConstraint", ssdlNamespace.NamespaceName));
                    if (referentialConstraintElement != null)
                    {
                        var principalElement = referentialConstraintElement.Element(XName.Get("Principal", ssdlNamespace.NamespaceName));
                        if (principalElement.Attribute("Role").Value == role.Name)
                        {
                            isPrincipal = true;
                            association.PrincipalRole = role;

                            EventedObservableCollection<Property> properties = new EventedObservableCollection<Property>();

                            foreach (XElement element in principalElement.Elements(XName.Get("PropertyRef", ssdlNamespace.NamespaceName)))
                            {
                                foreach (Property property in role.Type.Properties)
                                {
                                    if (property.Name == element.Attribute("Name").Value)
                                        properties.Add(property);
                                }
                            }

                            role.Properties = properties;
                        }
                        else
                        {
                            isPrincipal = false;
                            association.DependantRole = role;

                            EventedObservableCollection<Property> properties = new EventedObservableCollection<Property>();
                            XElement dependentElement = referentialConstraintElement.Element(XName.Get("Dependent", ssdlNamespace.NamespaceName));

                            foreach (XElement element in dependentElement.Elements(XName.Get("PropertyRef", ssdlNamespace.NamespaceName)))
                            {
                                foreach (Property property in role.Type.Properties)
                                {
                                    if (property.Name == element.Attribute("Name").Value)
                                        properties.Add(property);
                                }
                            }

                            role.Properties = properties;
                        }
                    }
                    if (isPrincipal)
                    {
                        association.Role1 = role;
                        isPrincipal = false;
                    }
                    else
                        association.Role2 = role;
                }

                #endregion Roles

                value.AssociationSets.Add(association);
            }

            #endregion AssociationSets

            #region Functions

            foreach (var functionElement in schemaElement.Elements(XName.Get("Function", ssdlNamespace.NamespaceName)))
            {
                var function = new Function { Name = functionElement.Attribute("Name").Value };
                SetBoolValueFromAttribute(functionElement, "Aggregate", aggregate => function.Aggregate = aggregate);
                SetBoolValueFromAttribute(functionElement, "BuiltIn", builtIn => function.BuiltIn = builtIn);
                SetBoolValueFromAttribute(functionElement, "IsComposable", isComposable => function.IsComposable = isComposable);
                SetBoolValueFromAttribute(functionElement, "NiladicFunction", niladicFunction => function.NiladicFunction = niladicFunction);
                SetStringValueFromAttribute(functionElement, "Schema", schema => function.Schema = schema);
                SetStringValueFromAttribute(functionElement, "Name", storeNamespace.NamespaceName, name => function.StoreName = name);
                SetStringValueFromAttribute(functionElement, "Schema", storeNamespace.NamespaceName, schema => function.StoreSchema = schema);
                SetStringValueFromAttribute(functionElement, "Type", storeNamespace.NamespaceName, type => function.StoreType = type);
                SetStringValueFromAttribute(functionElement, "FunctionName", storeNamespace.NamespaceName, functionName => function.StoreFunctionName = functionName);
                SetEnumValueFromAttribute<ParameterTypeSemantics>(functionElement, "ParameterTypeSemantics", parameterTypeSemantics => function.ParameterTypeSemantics = parameterTypeSemantics);

                #region Parameters

                foreach (var parameterElement in functionElement.Elements(XName.Get("Parameter", ssdlNamespace.NamespaceName)))
                {
                    var parameter = new FunctionParameter { Name = parameterElement.Attribute("Name").Value, Type = parameterElement.Attribute("Type").Value };
                    SetEnumValueFromAttribute<ParameterMode>(parameterElement, "Mode", mode => parameter.Mode = mode);
                    SetIntValueFromAttribute(parameterElement, "MaxLength", maxLength => parameter.MaxLength = maxLength);
                    SetIntValueFromAttribute(parameterElement, "Precision", precision => parameter.Precision = precision);
                    SetIntValueFromAttribute(parameterElement, "Scale", scale => parameter.Scale = scale);
                    SetStringValueFromAttribute(parameterElement, "Name", storeNamespace.NamespaceName, name => parameter.StoreName = name);
                    SetStringValueFromAttribute(parameterElement, "Schema", storeNamespace.NamespaceName, schema => parameter.StoreSchema = schema);
                    SetStringValueFromAttribute(parameterElement, "Type", storeNamespace.NamespaceName, type => parameter.StoreType = type);

                    function.Parameters.Add(parameter);
                }

                #endregion Parameters

                value.Functions.Add(function);
            }

            #endregion Functions

            return value;
        }