コード例 #1
0
        private SourceFragmentDefinition GetMasterTable(SourceFragmentDefinition sf,
                                                        out List <SourceFragmentRefDefinition.Condition> conditions)
        {
            SourceConstraint fk = SourceView.GetSourceFields(sf)
                                  .Where(item => item.IsPK)
                                  .SelectMany(item => item.Constraints)
                                  .Single(item => item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName);

            SourceFragmentDefinition m = null;

            conditions = new List <SourceFragmentRefDefinition.Condition>();
            foreach (SourceReferences rel in SourceView.GetFKRelations(fk))
            {
                if (m == null)
                {
                    m = rel.PKField.SourceFragment;
                }

                conditions.Add(new SourceFragmentRefDefinition.Condition(
                                   rel.PKField.SourceFieldExpression, rel.FKField.SourceFieldExpression
                                   ));
            }

            return(m);
        }
コード例 #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);
        }
コード例 #3
0
        protected void ProcessMany2Many()
        {
            foreach (SourceFragmentDefinition sf in SourceView.GetSourceFragments()
                     .Where(item => SourceView.GetSourceFields(item).All(clm => clm.IsFK) &&
                            item.Constraints.Count(citem => citem.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName) == 2))
            {
                List <LinkTarget> targets = new List <LinkTarget>();
                foreach (SourceConstraint fk in sf.Constraints.Where(item =>
                                                                     item.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName))
                {
                    var rels = SourceView.GetFKRelations(fk);
                    SourceFragmentDefinition m = rels.First().PKField.SourceFragment;

                    EntityDefinition e  = Model.GetEntity(GetEntityIdentifier(m.Selector, m.Name));
                    LinkTarget       lt = new LinkTarget(
                        e,
                        rels.Select(item => item.FKField.SourceFieldExpression).ToArray(),
                        rels.Select(item => e.GetPkProperties().Single(p => p.SourceFieldExpression == item.PKField.SourceFieldExpression).PropertyAlias).ToArray(),
                        rels.First().DeleteAction == SourceConstraint.CascadeAction
                        );
                    targets.Add(lt);
                }

                if (targets.Count != 2)
                {
                    continue;
                }

                if (targets[0].Entity.Name == targets[1].Entity.Name)
                {
                    LinkTarget             t      = targets[0];
                    SelfRelationDefinition newRel = new SelfRelationDefinition(
                        t.Entity, t.EntityProperties, targets[0], targets[1],
                        GetSourceFragment(sf), null);

                    if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName))
                    {
                        newRel.Constraint = RelationConstraint.PrimaryKey;
                    }
                    else if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName))
                    {
                        newRel.Constraint = RelationConstraint.Unique;
                    }

                    if (Model.GetSimilarRelation(newRel) == null)
                    {
                        string postFix = string.Empty;
                        if (string.IsNullOrEmpty(newRel.Left.AccessorName))
                        {
                            if (newRel.Left.FieldName.Length == 1)
                            {
                                if (newRel.Left.FieldName[0].EndsWith("_id", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    newRel.Left.AccessorName = newRel.Left.FieldName[0]
                                                               .Substring(0, newRel.Left.FieldName[0].Length - 3);
                                }
                                else if (newRel.Left.FieldName[0].EndsWith("id", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    newRel.Left.AccessorName = newRel.Left.FieldName[0]
                                                               .Substring(0, newRel.Left.FieldName[0].Length - 2);
                                }
                            }

                            if (string.IsNullOrEmpty(newRel.Left.AccessorName))
                            {
                                newRel.Left.AccessorName = newRel.Entity.Name;
                                postFix = "1";
                            }
                        }

                        if (string.IsNullOrEmpty(newRel.Right.AccessorName))
                        {
                            if (newRel.Left.FieldName.Length == 1)
                            {
                                if (newRel.Right.FieldName[0].EndsWith("_id", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    newRel.Right.AccessorName = newRel.Right.FieldName[0]
                                                                .Substring(0, newRel.Right.FieldName[0].Length - 3);
                                }
                                else if (newRel.Right.FieldName[0].EndsWith("id", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    newRel.Right.AccessorName = newRel.Right.FieldName[0]
                                                                .Substring(0, newRel.Right.FieldName[0].Length - 2);
                                }
                            }

                            if (string.IsNullOrEmpty(newRel.Right.AccessorName))
                            {
                                newRel.Right.AccessorName = newRel.Entity.Name + postFix;
                            }
                        }
                        Model.AddRelation(newRel);
                    }
                }
                else
                {
                    RelationDefinition newRel = new RelationDefinition(
                        targets[0], targets[1], GetSourceFragment(sf), null);

                    if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName))
                    {
                        newRel.Constraint = RelationConstraint.PrimaryKey;
                    }
                    else if (sf.Constraints.Any(item => item.ConstraintType == SourceConstraint.UniqueConstraintTypeName))
                    {
                        newRel.Constraint = RelationConstraint.Unique;
                    }

                    if (!Model.GetRelations().OfType <RelationDefinition>().Any(m => m.Equals(newRel)))
                    {
                        if (Model.HasSimilarRelationM2M(newRel))
                        {
                            if (string.IsNullOrEmpty(newRel.Left.AccessorName) ||
                                string.IsNullOrEmpty(newRel.Right.AccessorName))
                            {
                                var lst = from r in Model.GetRelations().OfType <RelationDefinition>()
                                          where
                                          !ReferenceEquals(r.Left, newRel.Left) &&
                                          !ReferenceEquals(r.Right, newRel.Right) &&
                                          (
                                    ((r.Left.Entity == newRel.Left.Entity &&
                                      string.IsNullOrEmpty(r.Right.AccessorName))
                                     &&
                                     (r.Right.Entity == newRel.Right.Entity &&
                                      string.IsNullOrEmpty(r.Left.AccessorName))) ||
                                    ((r.Left.Entity == newRel.Right.Entity &&
                                      string.IsNullOrEmpty(r.Right.AccessorName))
                                     &&
                                     (r.Right.Entity == newRel.Left.Entity &&
                                      string.IsNullOrEmpty(r.Left.AccessorName)))
                                          )
                                          select r;

                                if (lst.Count() > 0)
                                {
                                    foreach (RelationDefinition r in lst)
                                    {
                                        if (string.IsNullOrEmpty(r.Left.AccessorName))
                                        {
                                            r.Left.AccessorName = r.SourceFragment.Name.TrimEnd(']').TrimStart('[') +
                                                                  r.Right.Entity.Name;
                                        }
                                        if (string.IsNullOrEmpty(r.Right.AccessorName))
                                        {
                                            r.Right.AccessorName = r.SourceFragment.Name.TrimEnd(']').TrimStart('[') +
                                                                   r.Left.Entity.Name;
                                        }
                                    }

                                    if (string.IsNullOrEmpty(newRel.Left.AccessorName))
                                    {
                                        newRel.Left.AccessorName =
                                            newRel.SourceFragment.Name.TrimEnd(']').TrimStart('[') +
                                            newRel.Right.Entity.Name;
                                    }
                                    if (string.IsNullOrEmpty(newRel.Right.AccessorName))
                                    {
                                        newRel.Right.AccessorName =
                                            newRel.SourceFragment.Name.TrimEnd(']').TrimStart('[') +
                                            newRel.Left.Entity.Name;
                                    }
                                }
                            }
                        }
                        Model.AddRelation(newRel);
                    }
                }
            }

            foreach (SelfRelationDefinition rdb in Model.GetActiveRelations().OfType <SelfRelationDefinition>())
            {
                NormalizeRelationAccessors(rdb, rdb.Right.AccessorName, rdb.Entity);
                NormalizeRelationAccessors(rdb, rdb.Left.AccessorName, rdb.Entity);
            }

            foreach (RelationDefinition rdb in Model.GetActiveRelations().OfType <RelationDefinition>())
            {
                NormalizeRelationAccessors(rdb, rdb.Right.AccessorName, rdb.Right.Entity);
                NormalizeRelationAccessors(rdb, rdb.Left.AccessorName, rdb.Left.Entity);
            }
        }