Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        protected EntityPropertyDefinition AppendFK(EntityDefinition e, SourceFragmentDefinition sf,
                                                    SourceConstraint fk, relation1to1 rb, out bool created,
                                                    bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive)
        {
            created = false;
            var rels = SourceView.GetFKRelations(fk);
            SourceFragmentDefinition m  = rels.First().PKField.SourceFragment;
            EntityDefinition         re = e;

            if (sf != m)
            {
                re = GetEntity(m, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
            }
            string         rid = "t" + re.Name;
            TypeDefinition td  = Model.GetType(rid, false);

            if (td == null)
            {
                td = new TypeDefinition(rid, re);
                Model.AddType(td);
            }

            string propAlias = td.Entity.Name;

            if (rels.Count() == 1)
            {
                propAlias = Trim(GetName(rels.First().FKField.SourceFieldExpression), true);
            }

            if (capitalizeNames)
            {
                propAlias = Capitalize(propAlias);
            }

            string propName = propAlias;

            EntityPropertyDefinition ep = null;

            //try
            //{
            ep = (EntityPropertyDefinition)e.OwnProperties
                 .SingleOrDefault(item => item.Identifier == propAlias);

            if (ep == null)
            {
                ep = e.OwnProperties.OfType <EntityPropertyDefinition>().SingleOrDefault(item => fk.SourceFields.All(sf2 =>
                                                                                                                     item.SourceFields.Any(sff => sf2.SourceFieldExpression.Trim('[', ']') == sff.SourceFieldExpression.Trim('[', ']'))
                                                                                                                     ));
            }

            if (ep == null)
            {
                var one2one = fk.SourceFields.All(sss => sss.IsPK);

                if (one2one && e.BaseEntity == re)
                {
                    foreach (var sfd_ in fk.SourceFields)
                    {
                        bool x;
                        var  xx = AppendColumn(e, sfd_, out x, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
                        RaiseOnPropertyCreated(xx, x);
                    }
                    return(null);
                }

                int cnt = e.OwnProperties.Count(p => p.Name == propName);
                if (cnt > 0)
                {
                    propName  = propName + cnt;
                    propAlias = propAlias + cnt;
                }

                SourceFragmentDefinition sfd = GetSourceFragment(sf);

                ep = new EntityPropertyDefinition(propName, propAlias,
                                                  Field2DbRelations.None, "Auto generated from constraint " + fk.ConstraintName,
                                                  AccessLevel.Private, AccessLevel.Public, td, sfd, e);

                e.AddProperty(ep);
                created = true;

                foreach (SourceReferences rel in rels)
                {
                    SourceFieldDefinition fld = SourceView.GetSourceFields(sf).Single(item => item.SourceFieldExpression == rel.FKField.SourceFieldExpression);

                    ScalarPropertyDefinition pk = re.GetPkProperties().SingleOrDefault(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression);
                    if (pk == null)
                    {
                        if (rel.PKConstraint.ConstraintType != SourceConstraint.UniqueConstraintTypeName)
                        {
                            throw new WXMLException(string.Format("Cannot find pk for constraint {0}", rel.PKConstraint.ConstraintName));
                        }

                        pk = re.GetProperties().OfType <ScalarPropertyDefinition>().Single(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression);
                    }

                    ep.AddSourceField(pk.PropertyAlias,
                                      fld.SourceFieldExpression, null, fld.SourceType, fld.SourceTypeSize, fld.IsNullable, fld.DefaultValue
                                      );
                }
            }
            else
            {
                if (ep.Description == "Auto generated from constraint " + fk.ConstraintName)
                {
                    ep.PropertyType = td;
                }
            }

            foreach (SourceFieldDefinition pkField in SourceView.GetFKRelations(fk)
                     .Select(item => item.FKField)
                     .Where(item => item.IsPK))
            {
                string pkPropAlias = GetName(pkField.SourceFieldExpression);

                if (!SourceView.GetSourceFields(pkField.SourceFragment).Any(item => GetName(item.SourceFieldExpression).Equals(Trim(pkPropAlias, transforRawNamesToReadableForm), StringComparison.InvariantCultureIgnoreCase)))
                {
                    pkPropAlias = Trim(pkPropAlias, transforRawNamesToReadableForm);
                }

                //string pkPropAlias = Trim(GetName(pkField.SourceFieldExpression), transforRawNamesToReadableForm);
                if (capitalizeNames)
                {
                    pkPropAlias = Capitalize(pkPropAlias);
                }

                string             pkPropName = pkPropAlias;
                PropertyDefinition pe         = e.OwnProperties
                                                .SingleOrDefault(pd => pd.Identifier == pkPropAlias);
                Field2DbRelations attrs  = pkField.GetAttributes();
                TypeDefinition    pkType = GetClrType(pkField.SourceType, pkField.IsNullable, ref attrs);
                bool pkCreated           = pe == null;
                if (pkCreated)
                {
                    int cnt = e.OwnProperties.Count(p => p.Name == pkPropName);
                    if (cnt > 0)
                    {
                        pkPropName = pkPropName + cnt;
                        //pkPropAlias = pkPropAlias + cnt;
                    }

                    pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs,
                                                      "Auto generated from column " + pkField.SourceFieldExpression,
                                                      pkType, pkField, AccessLevel.Private, AccessLevel.Public);

                    e.AddProperty(pe);
                }
                else
                {
l1:
                    if (pe is ScalarPropertyDefinition)
                    {
                        pe.Attributes  |= attrs;
                        pe.PropertyType = pkType;
                        ((ScalarPropertyDefinition)pe).SourceField = pkField;
                    }
                    else
                    {
                        int cnt = e.OwnProperties.Count(p => p.Identifier == pkPropAlias);
                        if (cnt > 0)
                        {
                            if (e.OwnProperties.Any(item => item.Identifier == GetName(pkField.SourceFieldExpression)))
                            {
                                pkPropAlias = pkPropAlias + cnt;
                            }
                            else
                            {
                                pkPropAlias = GetName(pkField.SourceFieldExpression);
                            }
                        }
                        pkPropName = pkPropAlias;

                        pe = e.OwnProperties.SingleOrDefault(item => item.PropertyAlias == pkPropAlias);
                        if (pe != null)
                        {
                            if (pe is EntityPropertyDefinition)
                            {
                                throw new WXMLParserException(string.Format("Property {0} expected to be of type ScalarPropertyDefinition", pe.Identifier));
                            }
                            goto l1;
                        }

                        pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs,
                                                          "Auto generated from column " + pkField.SourceFieldExpression,
                                                          pkType, pkField, AccessLevel.Private, AccessLevel.Public);

                        e.AddProperty(pe);
                    }
                }
                RaiseOnPropertyCreated(pe, pkCreated);
            }
            //}
            //catch
            //{
            //    int i = 10;
            //}
            return(ep);
        }
Exemplo n.º 3
0
        public void ApplySourceViewToModel(bool dropProperties, relation1to1 rb,
                                           bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive)
        {
            _tables2skip   = new List <SourceFragmentDefinition>();
            _entities2skip = new Dictionary <string, EntityDefinition>();

            foreach (SourceFragmentDefinition sf in SourceView.GetSourceFragments())
            {
                if (_tables2skip.Contains(sf))
                {
                    continue;
                }

                if (sf.Constraints.Count(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName) == 2 &&
                    SourceView.GetSourceFields(sf).All(clm => clm.IsFK))
                {
                    continue;
                }

                GetEntity(sf, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive);
            }

            //Dictionary<string, EntityDefinition> dic = Process1to1Relations(columns, defferedCols, odef, escape, notFound, rb);

            ProcessMany2Many();

            if (dropProperties)
            {
                foreach (EntityDefinition ed in Model.GetActiveEntities())
                {
                    foreach (PropertyDefinition pd in ed.GetActiveProperties()
                             .Where(item =>
                                    !string.IsNullOrEmpty(item.Description) &&
                                    item.Description.StartsWith("Auto generated from ", StringComparison.Ordinal)
                                    ).ToArray()
                             )
                    {
                        var sourceFields = SourceView.GetSourceFields(pd.SourceFragment);

                        if (pd is ScalarPropertyDefinition)
                        {
                            ScalarPropertyDefinition p = pd as ScalarPropertyDefinition;
                            if (!sourceFields
                                .Any(item => (
                                         caseSensitive?Trim(
                                             (capitalizeNames ? Capitalize(item.SourceFieldExpression) : item.SourceFieldExpression),
                                             transforRawNamesToReadableForm
                                             ):item.SourceFieldExpression.ToLower()
                                         ) == (
                                         caseSensitive?p.SourceFieldExpression:p.SourceFieldExpression.ToLower()
                                         )))
                            {
                                ed.RemoveProperty(pd);
                                RaiseOnPropertyRemoved(pd);
                            }
                        }
                        else if (pd is EntityPropertyDefinition)
                        {
                            EntityPropertyDefinition p = pd as EntityPropertyDefinition;
                            foreach (EntityPropertyDefinition.SourceField field in p.SourceFields.ToArray())
                            {
                                if (!sourceFields.Any(item =>
                                                      (caseSensitive?Trim(capitalizeNames ? Capitalize(item.SourceFieldExpression)
                                        : item.SourceFieldExpression, transforRawNamesToReadableForm):item.SourceFieldExpression.ToLower()) == (caseSensitive?field.SourceFieldExpression:field.SourceFieldExpression.ToLower())))
                                {
                                    p.RemoveSourceField(field);
                                }
                            }
                            if (p.SourceFields.Count() == 0)
                            {
                                ed.RemoveProperty(pd);
                                RaiseOnPropertyRemoved(pd);
                            }
                        }
                        else
                        {
                            throw new NotSupportedException(pd.GetType().ToString());
                        }
                    }
                }
            }

            ProcessOne2Many();
        }