示例#1
0
        public void TestFillEntityTables()
        {
            Worm_CodeGen_Core_OrmXmlParserAccessor parser;

            parser = null;
            using (XmlReader rdr = XmlReader.Create(GetSampleFileStream()))
            {
                object privateParser = Worm_CodeGen_Core_OrmXmlParserAccessor.CreatePrivate(rdr);
                parser = new Worm_CodeGen_Core_OrmXmlParserAccessor(privateParser);
                parser.Read();
            }

            parser.FillSourceFragments();
            parser.FindEntities();


            WXMLModel ormObjectDef = parser.Model;

            EntityDefinition entity = ormObjectDef.GetEntities()
                                      .Single(match => match.Identifier == "eArtist" && match.Name == "Artist");

            Assert.AreEqual <int>(2, entity.GetSourceFragments().Count());
            Assert.IsTrue(entity.GetSourceFragments().Any(match => match.Identifier.Equals("tblArtists") &&
                                                          match.Name.Equals("artists")));
            Assert.IsTrue(entity.GetSourceFragments().Any(match => match.Identifier.Equals("tblSiteAccess") &&
                                                          match.Name.Equals("sites_access")));
        }
        private static void CreateTablesLinkEnum(EntityDefinition entity, CodeTypeDeclaration entitySchemaDefClass)
        {
            if (!entity.InheritsBaseTables || entity.GetSourceFragments().Count() > 0)
            {
                var fullTables = entity.GetSourceFragments();

                CodeTypeDeclaration tablesEnum = new CodeTypeDeclaration("TablesLink")
                {
                    Attributes = MemberAttributes.Public,
                    IsClass    = false,
                    IsEnum     = true,
                    IsPartial  = false
                };

                if (entity.BaseEntity != null)
                {
                    tablesEnum.Attributes |= MemberAttributes.New;
                }

                int tableNum = 0;

                tablesEnum.Members.AddRange(fullTables.Select(tbl => new CodeMemberField
                {
                    InitExpression = new CodePrimitiveExpression(tableNum++),
                    Name           = WXMLCodeDomGeneratorNameHelper.GetSafeName(tbl.Identifier)
                }).ToArray());
                entitySchemaDefClass.Members.Add(tablesEnum);
            }
        }
示例#3
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);
            }
        }
示例#4
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());
            }
        }
        ///// <summary>
        ///// Gets class name of the entity using settings
        ///// </summary>
        ///// <param name="entity">The entity.</param>
        ///// <returns></returns>
        //public string GetEntityClassName(EntityDefinition entity)
        //{
        //    return GetEntityClassName(entity, false);
        //}

        /// <summary>
        /// Gets class name of the entity using settings
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="qualified">if set to <c>true</c> return qualified name.</param>
        /// <returns></returns>
        public string GetEntityClassName(EntityDefinition entity, bool qualified)
        {
            WXMLCodeDomGeneratorSettings settings = GetSettings();
            string en = entity.Name;

            if (entity.Model != null && entity.Model.OwnEntities.Any(e => e.Name == en && e.Identifier != entity.Identifier))
            {
                string sel = entity.GetSourceFragments().First().Selector;
                if (string.IsNullOrEmpty(sel))
                {
                    int idx = entity.Model.OwnEntities.Count(e => e.Name == en && e.Identifier != entity.Identifier);
                    en = en + idx;
                }
                else
                {
                    sel = sel.Trim('[', ']');
                    en  = sel + en;
                }
            }

            string className =
                // prefix from settings for class name
                settings.ClassNamePrefix +
                // entity's class name
                en +
                // suffix from settings for class name
                settings.ClassNameSuffix;

            string ns = string.Empty;

            if (qualified && !string.IsNullOrEmpty(entity.Namespace))
            {
                ns = entity.Namespace + ".";
            }

            return(ns + className);
        }
示例#6
0
        public void TestFillHierarchy()
        {
            MSSQLProvider p = new MSSQLProvider(GetTestDB(), null);

            SourceView sv = p.GetSourceView(null, "aspnet_Membership, aspnet_Users");

            WXMLModel model = new WXMLModel();

            SourceToModelConnector c = new SourceToModelConnector(sv, model);

            c.ApplySourceViewToModel();

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

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

            EntityDefinition membership = model.GetEntity("e_dbo_aspnet_Membership");

            Assert.IsNotNull(membership);

            EntityDefinition users = model.GetEntity("e_dbo_aspnet_Users");

            Assert.IsNotNull(users);

            Assert.AreEqual(membership.BaseEntity, users);

            Assert.AreEqual(2, membership.GetSourceFragments().Count());

            Assert.AreEqual(1, membership.OwnSourceFragments.Count());

            Assert.AreEqual(1, users.GetSourceFragments().Count());

            Assert.AreEqual(1, users.OwnSourceFragments.Count());

            Assert.IsNull(users.BaseEntity);
        }
示例#7
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);
        }