예제 #1
0
        public void TestAddEntity()
        {
            using (Stream stream = Resources.GetXmlDocumentStream("suppressed"))
            {
                Assert.IsNotNull(stream);

                WXMLModel model = WXMLModel.LoadFromXml(new XmlTextReader(stream));

                Assert.IsNotNull(model);

                Assert.AreEqual(2, model.GetActiveEntities().Count());

                WXMLModel newModel = new WXMLModel();

                EntityDefinition newEntity = new EntityDefinition("ee", "ee", string.Empty, string.Empty, newModel);

                SourceFragmentRefDefinition sf = new SourceFragmentRefDefinition(newModel.GetOrCreateSourceFragment("dbo", "ee"));

                newEntity.AddSourceFragment(sf);

                newEntity.AddProperty(new ScalarPropertyDefinition(newEntity, "ID", "ID", Field2DbRelations.None,
                                                                   string.Empty, newModel.GetOrCreateType(typeof(Int32)), new SourceFieldDefinition(sf, "id"), AccessLevel.Private,
                                                                   AccessLevel.Public));

                model.Merge(Normalize(newModel));

                Assert.AreEqual(3, model.GetActiveEntities().Count());

                Assert.AreEqual(1, model.GetActiveEntities().Single(item => item.Identifier == "ee").GetActiveProperties().Count());
            }
        }
예제 #2
0
        public void TestAlterEntity_ChangeTable()
        {
            using (Stream stream = Resources.GetXmlDocumentStream("suppressed"))
            {
                Assert.IsNotNull(stream);

                WXMLModel newModel = WXMLModel.LoadFromXml(new XmlTextReader(stream));

                Assert.IsNotNull(newModel);

                Assert.AreEqual(2, newModel.GetActiveEntities().Count());

                EntityDefinition e = newModel.GetActiveEntities().Single(item => item.Identifier == "e1");

                Assert.AreEqual(1, e.GetSourceFragments().Count());
                Assert.AreEqual(1, newModel.GetSourceFragments().Count());

                Assert.AreEqual("tbl1", e.GetSourceFragments().First().Name);
                Assert.IsTrue(string.IsNullOrEmpty(e.GetSourceFragments().First().Selector));

                SourceFragmentRefDefinition sf = new SourceFragmentRefDefinition(
                    newModel.GetOrCreateSourceFragment("dbo", "table"));

                //foreach (SourceFragmentRefDefinition rsf in e.GetSourceFragments())
                //{
                //    e.MarkAsDeleted(rsf);
                //}
                e.ClearSourceFragments();
                e.AddSourceFragment(sf);

                foreach (PropertyDefinition property in e.GetProperties())
                {
                    property.SourceFragment = sf;
                }

                WXMLModel model = GetModel("suppressed");

                model.Merge(Normalize(newModel));

                e = model.GetActiveEntities().Single(item => item.Identifier == "e1");

                Assert.AreEqual(1, e.GetSourceFragments().Count());
                Assert.AreEqual(2, model.GetSourceFragments().Count());

                Assert.AreEqual("table", e.GetSourceFragments().First().Name);
                Assert.AreEqual("dbo", e.GetSourceFragments().First().Selector);

                e = model.GetActiveEntities().Single(item => item.Identifier == "e11");

                Assert.AreEqual(1, e.GetSourceFragments().Count());
                Assert.AreEqual("table", e.GetSourceFragments().First().Name);
                Assert.AreEqual("dbo", e.GetSourceFragments().First().Selector);
            }
        }
예제 #3
0
        public void TestAddProperty()
        {
            using (Stream stream = Resources.GetXmlDocumentStream("suppressed"))
            {
                Assert.IsNotNull(stream);

                WXMLModel model = WXMLModel.LoadFromXml(new XmlTextReader(stream));

                Assert.IsNotNull(model);

                EntityDefinition entity = model.GetActiveEntities().Single(item => item.Identifier == "e1");

                Assert.IsNotNull(entity);

                Assert.AreEqual(2, model.GetActiveEntities().Count());

                Assert.AreEqual(2, entity.GetActiveProperties().Count());

                WXMLModel newModel = new WXMLModel();

                EntityDefinition newEntity = new EntityDefinition(entity.Identifier, entity.Name, entity.Namespace, entity.Description, newModel);

                //newModel.AddEntity(newEntity);

                TypeDefinition tString = model.GetTypes().Single(item => item.Identifier == "tString");

                newModel.AddType(tString);

                SourceFragmentRefDefinition newTable = entity.GetSourceFragments().First();

                newModel.AddSourceFragment(newTable);

                newEntity.AddSourceFragment(newTable);

                newEntity.AddProperty(new ScalarPropertyDefinition(newEntity, "Prop2", "Prop2", Field2DbRelations.None, null,
                                                                   tString, new SourceFieldDefinition(newTable, "prop2"), AccessLevel.Private, AccessLevel.Public));

                model.Merge(Normalize(newModel));

                Assert.AreEqual(2, model.GetActiveEntities().Count());

                Assert.AreEqual(3, entity.GetActiveProperties().Count());
            }
        }
예제 #4
0
        private EntityDefinition GetEntity(SourceFragmentDefinition sf, out bool created, bool capitalizeNames)
        {
            created = false;
            string           entityId = GetEntityIdentifier(sf.Selector, sf.Name);
            EntityDefinition e        = Model.GetEntity(entityId);

            if (e == null)
            {
                string ename = GetName(sf.Name);
                if (capitalizeNames)
                {
                    ename = Capitalize(ename);
                }

                e = new EntityDefinition(entityId, ename, string.Empty,
                                         string.Format("Auto generated from {0}", string.IsNullOrEmpty(sf.Selector) ? sf.Name : $"{sf.Selector}.{sf.Name}"),
                                         Model);
                var t = new SourceFragmentRefDefinition(GetSourceFragment(sf));
                e.AddSourceFragment(t);
                //odef.AddEntity(e);
                created = true;
            }
            return(e);
        }
예제 #5
0
        private EntityDefinition GetEntity(SourceFragmentDefinition sf, relation1to1 rb,
                                           bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive)
        {
            var entityIdentifier = GetEntityIdentifier(sf.Selector, sf.Name);
            EntityDefinition e;

            if (!_entities2skip.TryGetValue(entityIdentifier, out e))
            {
                EntityDefinition         masterEntity = null;
                SourceFragmentDefinition masterTable  = null;
                List <SourceFragmentRefDefinition.Condition> conds = null;
                if (rb != relation1to1.Default && SourceView.GetSourceFields(sf)
                    .Where(item => item.IsPK)
                    .SelectMany(item => item.Constraints)
                    .Count(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName) == 1 &&
                    SourceView.GetSourceFields(sf)
                    .Where(item => item.IsPK)
                    .All(item => item.IsFK)
                    )
                {
                    switch (rb)
                    {
                    case relation1to1.Unify:
                    case relation1to1.Hierarchy:
                        masterTable  = GetMasterTable(sf, out conds);
                        masterEntity = GetEntity(masterTable, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
                        break;

                    default:
                        throw new NotSupportedException(rb.ToString());
                    }
                }

                e = Model.GetEntity(entityIdentifier);
                if (e == null)
                {
                    bool entCreated;
                    e = GetEntity(sf, out entCreated, capitalizeNames);
                    if (entCreated)
                    {
                        RaiseOnEntityCreated(e);
                    }
                }
                _tables2skip.Add(sf);
                _entities2skip.Add(entityIdentifier, e);

                foreach (SourceFieldDefinition field in SourceView.GetSourceFields(sf)
                         .Where(item => !item.IsFK))
                {
                    bool propCreated;
                    PropertyDefinition prop = AppendColumn(e, field, out propCreated, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
                    RaiseOnPropertyCreated(prop, propCreated);
                }

                foreach (SourceConstraint fk in sf.Constraints.Where(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName))
                {
                    bool propCreated;
                    PropertyDefinition prop = AppendFK(e, sf, fk, rb, out propCreated, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
                    if (prop != null)
                    {
                        RaiseOnPropertyCreated(prop, propCreated);
                    }
                }

                if (masterEntity != null)
                {
                    SourceFragmentRefDefinition sfr;
                    switch (rb)
                    {
                    case relation1to1.Unify:
                        sfr = masterEntity.GetSourceFragments()
                              .Single(item => item.Identifier == masterTable.Identifier);
                        sfr.AnchorTable = sf;
                        sfr.JoinType    = SourceFragmentRefDefinition.JoinTypeEnum.outer;
                        sfr.Conditions.AddRange(conds);
                        masterEntity.AddSourceFragment(new SourceFragmentRefDefinition(sf));

                        foreach (PropertyDefinition property in e.GetProperties()
                                 .Where(item => !item.HasAttribute(Field2DbRelations.PK) /* &&
                                                                                          * item.SourceFragment != masterTable*/))
                        {
                            if (masterEntity.GetProperties().Any(item => item.PropertyAlias == property.PropertyAlias))
                            {
                                property.PropertyAlias = e.Name + "_" + property.PropertyAlias;
                                property.Name          = e.Name + "_" + property.Name;
                            }

                            if (!masterEntity.GetProperties().Any(item => item.PropertyAlias == property.PropertyAlias))
                            {
                                masterEntity.AddProperty(property);
                            }
                        }

                        Model.RemoveEntity(e);

                        break;

                    case relation1to1.Hierarchy:
                        sfr             = e.GetSourceFragments().Single();
                        sfr.AnchorTable = masterTable;
                        sfr.JoinType    = SourceFragmentRefDefinition.JoinTypeEnum.inner;
                        foreach (SourceFragmentRefDefinition.Condition cond in conds)
                        {
                            sfr.Conditions.Add(new SourceFragmentRefDefinition.Condition(cond.RightColumn, cond.LeftColumn));
                        }

                        e.BaseEntity         = masterEntity;
                        e.InheritsBaseTables = true;

                        break;
                    }
                }
            }

            return(e);
        }
예제 #6
0
        private void MergeEntities(WXMLModel mergeWith)
        {
            foreach (EntityDefinition newEntity in mergeWith.GetEntities())
            {
                string newEntityIdentifier = newEntity.Identifier;

                EntityDefinition entity = GetEntities().SingleOrDefault(item => item.Identifier == newEntityIdentifier);
                if (entity != null)
                {
                    if (!string.IsNullOrEmpty(newEntity.Name))
                    {
                        entity.Name = newEntity.Name;
                    }

                    entity.Namespace          = newEntity.Namespace;
                    entity.BaseEntity         = newEntity.BaseEntity;
                    entity.Behaviour          = newEntity.Behaviour;
                    entity.CacheCheckRequired = newEntity.CacheCheckRequired;
                    entity.Description        = newEntity.Description;
                    entity.Disabled           = newEntity.Disabled;
                    entity.InheritsBaseTables = newEntity.InheritsBaseTables;
                    entity.AutoInterface      = newEntity.AutoInterface;
                    entity.UseGenerics        = newEntity.UseGenerics;
                    entity.FamilyName         = newEntity.FamilyName;
                    foreach (var @interface in newEntity.Interfaces)
                    {
                        if (!entity.Interfaces.ContainsKey(@interface.Key))
                        {
                            entity.Interfaces.Add(@interface.Key, @interface.Value);
                        }
                    }

                    entity.ClearSourceFragments();
                    foreach (SourceFragmentRefDefinition newsf in newEntity.OwnSourceFragments)
                    {
                        //string newsfId = newsf.Identifier;
                        //SourceFragmentRefDefinition sf =
                        //    entity.GetSourceFragments().SingleOrDefault(item => item.Identifier == newsfId);

                        //foreach (PropertyDefinition rp in entity.GetProperties()
                        //    .Where(item=>item.SourceFragment == sf))
                        //{
                        //    if (rp is ScalarPropertyDefinition)
                        //    {
                        //        ScalarPropertyDefinition property = rp as ScalarPropertyDefinition;
                        //        property.SourceField.SourceFragment = newsf;
                        //    }
                        //    else if (rp is EntityPropertyDefinition)
                        //    {
                        //        EntityPropertyDefinition property = rp as EntityPropertyDefinition;
                        //        property.SourceFragment = newsf;
                        //    }
                        //}

                        //entity.RemoveSourceFragment(sf);
                        entity.AddSourceFragment(newsf);
                    }
                    //List<SourceFragmentRefDefinition> torem = new List<SourceFragmentRefDefinition>();
                    //foreach (SourceFragmentRefDefinition newsf in newEntity.GetSourceFragments())
                    //{
                    //    string newsfId = newsf.Identifier;
                    //    SourceFragmentRefDefinition sf =
                    //        entity.GetSourceFragments().SingleOrDefault(item => item.Identifier == newsfId);

                    //    if (sf != null)
                    //    {
                    //        if (newsf.Action == MergeAction.Delete)
                    //        {
                    //            entity.RemoveSourceFragment(sf);
                    //            torem.Add(newsf);
                    //        }
                    //        else if (newsf.AnchorTable != null)
                    //        {
                    //            sf.AnchorTable = newsf.AnchorTable;
                    //            sf.JoinType = newsf.JoinType;
                    //            if (newsf.Conditions.Count > 0)
                    //            {
                    //                foreach (SourceFragmentRefDefinition.Condition c in newsf.Conditions)
                    //                {
                    //                    SourceFragmentRefDefinition.Condition ec =
                    //                        sf.Conditions.SingleOrDefault(item =>
                    //                            item.LeftColumn == c.LeftColumn &&
                    //                            item.RightColumn == c.RightColumn
                    //                        );
                    //                    if (ec != null)
                    //                    {
                    //                        if (c.Action == MergeAction.Delete)
                    //                            sf.Conditions.Remove(ec);
                    //                    }
                    //                    else
                    //                        sf.Conditions.Add(c);
                    //                }
                    //            }
                    //        }
                    //    }
                    //    else
                    //        entity.AddSourceFragment(newsf);
                    //}

                    //foreach (SourceFragmentRefDefinition newsf in torem)
                    //{
                    //    newEntity.RemoveSourceFragment(newsf);
                    //}

                    foreach (PropertyDefinition newProperty in newEntity.GetProperties())
                    {
                        string newPropertyName = newProperty.PropertyAlias;

                        PropertyDefinition rp =
                            entity.GetProperties().SingleOrDefault(item => item.PropertyAlias == newPropertyName);

                        if (rp != null)
                        {
                            if (newProperty.Action == MergeAction.Delete)
                            {
                                entity.RemoveProperty(rp);
                            }
                            else
                            {
                                rp.Name = MergeString(rp, newProperty, item => item.Name);
                                rp.ObsoleteDescripton = MergeString(rp, newProperty, item => item.ObsoleteDescripton);
                                rp.DefferedLoadGroup  = MergeString(rp, newProperty, item => item.DefferedLoadGroup);
                                rp.GenerateAttribute  = newProperty.GenerateAttribute;
                                rp.AvailableFrom      = MergeString(rp, newProperty, item => item.AvailableFrom);
                                rp.AvailableTo        = MergeString(rp, newProperty, item => item.AvailableTo);
                                rp.Description        = MergeString(rp, newProperty, item => item.Description);
                                if (newProperty.Attributes != Field2DbRelations.None)
                                {
                                    rp.Attributes = newProperty.Attributes;
                                }
                                rp.Feature               = MergeString(rp, newProperty, item => item.Feature);
                                rp.Group                 = newProperty.Group ?? rp.Group;
                                rp.PropertyType          = newProperty.PropertyType ?? rp.PropertyType;
                                rp.Disabled              = newProperty.Disabled;
                                rp.EnablePropertyChanged = newProperty.EnablePropertyChanged;
                                rp.SourceFragment        = newProperty.SourceFragment ?? rp.SourceFragment;

                                if (newProperty.FieldAccessLevel != default(AccessLevel))
                                {
                                    rp.FieldAccessLevel = newProperty.FieldAccessLevel;
                                }

                                if (newProperty.PropertyAccessLevel != default(AccessLevel))
                                {
                                    rp.PropertyAccessLevel = newProperty.PropertyAccessLevel;
                                }

                                if (newProperty.Obsolete != default(ObsoleteType))
                                {
                                    rp.Obsolete = newProperty.Obsolete;
                                }

                                if (rp.GetType() != newProperty.GetType())
                                {
                                    PropertyDefinition newProp = null;
                                    if (rp is EntityPropertyDefinition && newProperty is ScalarPropertyDefinition)
                                    {
                                        newProp = new ScalarPropertyDefinition(entity, newPropertyName);
                                        rp.CopyTo(newProp);
                                    }
                                    else if (rp is ScalarPropertyDefinition && newProperty is EntityPropertyDefinition)
                                    {
                                        newProp = new EntityPropertyDefinition(rp as ScalarPropertyDefinition);
                                    }
                                    entity.RemoveProperty(rp);
                                    entity.AddProperty(newProp);
                                    rp = newProp;
                                }

                                if (rp is ScalarPropertyDefinition)
                                {
                                    ScalarPropertyDefinition property = rp as ScalarPropertyDefinition;
                                    property.SourceField.SourceType            = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceType);
                                    property.SourceFieldAlias                  = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceFieldAlias);
                                    property.SourceField.SourceFieldExpression = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceFieldExpression);
                                    property.SourceField.IsNullable            = ((ScalarPropertyDefinition)newProperty).IsNullable;
                                    property.SourceField.SourceTypeSize        = ((ScalarPropertyDefinition)newProperty).SourceTypeSize ?? property.SourceTypeSize;
                                }
                            }
                        }
                        else
                        {
                            entity.AddProperty(newProperty);
                        }
                    }

                    MergeExtensions(entity.Extensions, newEntity.Extensions);
                }
                else
                {
                    AddEntity(newEntity);
                }
            }
        }