protected override Expression VisitJoin(JoinExpression join)
 {
     join = (JoinExpression)base.VisitJoin(join);
     if ((join.Join == JoinType.CrossApply) || (join.Join == JoinType.OuterApply))
     {
         if (join.Right is TableExpression)
         {
             return(new JoinExpression(JoinType.CrossJoin, join.Left, join.Right, null));
         }
         SelectExpression right = join.Right as SelectExpression;
         if ((((right != null) && (right.Take == null)) && ((right.Skip == null) && !AggregateChecker.HasAggregates(right))) && ((right.GroupBy == null) || (right.GroupBy.Count == 0)))
         {
             SelectExpression     source = right.SetWhere(null);
             HashSet <TableAlias> set    = ReferencedAliasGatherer.Gather(source);
             HashSet <TableAlias> other  = DeclaredAliasGatherer.Gather(join.Left);
             set.IntersectWith(other);
             if (set.Count == 0)
             {
                 Expression where = right.Where;
                 right            = source;
                 ProjectedColumns columns = ColumnProjector.ProjectColumns(this.language, where, right.Columns, right.Alias, DeclaredAliasGatherer.Gather(right.From));
                 right = right.SetColumns(columns.Columns);
                 where = columns.Projector;
                 return(new JoinExpression((where == null) ? JoinType.CrossJoin : ((join.Join == JoinType.CrossApply) ? JoinType.InnerJoin : JoinType.LeftOuter), join.Left, right, where));
             }
         }
     }
     return(join);
 }
Пример #2
0
        public override Expression GetMemberExpression(Expression root, MappingEntity entity, MemberInfo member)
        {
            if (this.mapping.IsAssociationRelationship(entity, member))
            {
                MappingEntity        relatedEntity   = this.mapping.GetRelatedEntity(entity, member);
                ProjectionExpression queryExpression = this.GetQueryExpression(relatedEntity);
                List <MemberInfo>    list            = this.mapping.GetAssociationKeyMembers(entity, member).ToList <MemberInfo>();
                List <MemberInfo>    list2           = this.mapping.GetAssociationRelatedKeyMembers(entity, member).ToList <MemberInfo>();
                Expression           expression2     = null;
                int num   = 0;
                int count = list2.Count;
                while (num < count)
                {
                    Expression expression3 = this.GetMemberExpression(queryExpression.Projector, relatedEntity, list2[num]).Equal(this.GetMemberExpression(root, entity, list[num]));
                    expression2 = (expression2 != null) ? expression2.And(expression3) : expression3;
                    num++;
                }
                TableAlias           newAlias   = new TableAlias();
                ProjectedColumns     columns    = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, queryExpression.Projector, null, newAlias, new TableAlias[] { queryExpression.Select.Alias });
                LambdaExpression     aggregator = Aggregator.GetAggregator(King.Framework.Linq.TypeHelper.GetMemberType(member), typeof(IEnumerable <>).MakeGenericType(new Type[] { columns.Projector.Type }));
                ProjectionExpression expression = new ProjectionExpression(new SelectExpression(newAlias, columns.Columns, queryExpression.Select, expression2), columns.Projector, aggregator);
                return(this.translator.Police.ApplyPolicy(expression, member));
            }
            AliasedExpression expression6 = root as AliasedExpression;

            if ((expression6 != null) && this.mapping.IsColumn(entity, member))
            {
                return(new ColumnExpression(King.Framework.Linq.TypeHelper.GetMemberType(member), this.GetColumnType(entity, member), expression6.Alias, this.mapping.GetColumnName(entity, member)));
            }
            return(QueryBinder.BindMember(root, member));
        }
        protected override Expression VisitProjection(ProjectionExpression proj)
        {
            Expression       expression7;
            SelectExpression save = this.currentSelect;

            this.currentSelect = proj.Select;
            try
            {
                if (!this.isTopLevel)
                {
                    if (!this.CanJoinOnClient(this.currentSelect))
                    {
                        bool canJoinOnClient = this.canJoinOnClient;
                        this.canJoinOnClient = false;
                        Expression expression6 = base.VisitProjection(proj);
                        this.canJoinOnClient = canJoinOnClient;
                        return(expression6);
                    }
                    Func <Expression, Expression> selector = null;
                    Func <Expression, Expression> func2    = null;
                    SelectExpression     newOuterSelect    = (SelectExpression)QueryDuplicator.Duplicate(save);
                    SelectExpression     source            = (SelectExpression)ColumnMapper.Map(proj.Select, newOuterSelect.Alias, new TableAlias[] { save.Alias });
                    ProjectionExpression expression2       = this.language.AddOuterJoinTest(new ProjectionExpression(source, proj.Projector));
                    source = expression2.Select;
                    Expression       projector    = expression2.Projector;
                    TableAlias       newAlias     = new TableAlias();
                    ProjectedColumns columns      = ColumnProjector.ProjectColumns(this.language, projector, null, newAlias, new TableAlias[] { newOuterSelect.Alias, source.Alias });
                    JoinExpression   from         = new JoinExpression(JoinType.OuterApply, newOuterSelect, source, null);
                    SelectExpression joinedSelect = new SelectExpression(newAlias, columns.Columns, from, null, null, null, proj.IsSingleton, null, null, false);
                    this.currentSelect = joinedSelect;
                    projector          = this.Visit(columns.Projector);
                    List <Expression> outerExpressions = new List <Expression>();
                    List <Expression> innerExpressions = new List <Expression>();
                    if (this.GetEquiJoinKeyExpressions(source.Where, newOuterSelect.Alias, outerExpressions, innerExpressions))
                    {
                        if (selector == null)
                        {
                            selector = k => ColumnMapper.Map(k, save.Alias, new TableAlias[] { newOuterSelect.Alias });
                        }
                        IEnumerable <Expression> outerKey = outerExpressions.Select <Expression, Expression>(selector);
                        if (func2 == null)
                        {
                            func2 = k => ColumnMapper.Map(k, joinedSelect.Alias, new TableAlias[] { ((ColumnExpression)k).Alias });
                        }
                        IEnumerable <Expression> innerKey = innerExpressions.Select <Expression, Expression>(func2);
                        return(new ClientJoinExpression(new ProjectionExpression(joinedSelect, projector, proj.Aggregator), outerKey, innerKey));
                    }
                }
                else
                {
                    this.isTopLevel = false;
                }
                expression7 = base.VisitProjection(proj);
            }
            finally
            {
                this.currentSelect = save;
            }
            return(expression7);
        }
        protected override Expression VisitProjection(ProjectionExpression proj)
        {
            if (this.isTopLevel)
            {
                this.isTopLevel    = false;
                this.currentSelect = proj.Select;
                Expression projector = this.Visit(proj.Projector);
                if ((projector != proj.Projector) || (this.currentSelect != proj.Select))
                {
                    return(new ProjectionExpression(this.currentSelect, projector, proj.Aggregator));
                }
                return(proj);
            }
            if (proj.IsSingleton && this.CanJoinOnServer(this.currentSelect))
            {
                TableAlias newAlias = new TableAlias();
                this.currentSelect = this.currentSelect.AddRedundantSelect(this.language, newAlias);
                SelectExpression     source      = (SelectExpression)ColumnMapper.Map(proj.Select, newAlias, new TableAlias[] { this.currentSelect.Alias });
                ProjectionExpression expression3 = this.language.AddOuterJoinTest(new ProjectionExpression(source, proj.Projector));
                ProjectedColumns     columns     = ColumnProjector.ProjectColumns(this.language, expression3.Projector, this.currentSelect.Columns, this.currentSelect.Alias, new TableAlias[] { newAlias, proj.Select.Alias });
                JoinExpression       from        = new JoinExpression(JoinType.OuterApply, this.currentSelect.From, expression3.Select, null);
                this.currentSelect = new SelectExpression(this.currentSelect.Alias, columns.Columns, from, null);
                return(this.Visit(columns.Projector));
            }
            bool             isTopLevel    = this.isTopLevel;
            SelectExpression currentSelect = this.currentSelect;

            this.isTopLevel    = true;
            this.currentSelect = null;
            Expression expression6 = base.VisitProjection(proj);

            this.isTopLevel    = isTopLevel;
            this.currentSelect = currentSelect;
            return(expression6);
        }
Пример #5
0
        protected virtual Expression GetInsertResult(MappingEntity entity, Expression instance, LambdaExpression selector, Dictionary <MemberInfo, Expression> map)
        {
            Expression expression2;
            Func <MemberInfo, bool>            func  = null;
            Func <MemberInfo, int, Expression> func2 = null;
            TableAlias         alias      = new TableAlias();
            TableExpression    tex        = new TableExpression(alias, entity, this.mapping.GetTableName(entity));
            LambdaExpression   aggregator = Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(new Type[] { selector.Body.Type }));
            DeclarationCommand command    = null;
            List <MemberInfo>  source     = (from m in this.mapping.GetMappedMembers(entity)
                                             where this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)
                                             select m).ToList <MemberInfo>();

            if (source.Count > 0)
            {
                if (map != null)
                {
                }
                if (!((func == null) && source.Any <MemberInfo>((func = m => map.ContainsKey(m)))))
                {
                    Dictionary <MemberInfo, Expression> dictionary = new Dictionary <MemberInfo, Expression>();
                    command = this.GetGeneratedIdCommand(entity, source.ToList <MemberInfo>(), dictionary);
                    map     = dictionary;
                }
                MemberExpression body = selector.Body as MemberExpression;
                if (((body != null) && this.mapping.IsPrimaryKey(entity, body.Member)) && this.mapping.IsGenerated(entity, body.Member))
                {
                    if (command != null)
                    {
                        return(new ProjectionExpression(command.Source, new ColumnExpression(body.Type, command.Variables[0].QueryType, command.Source.Alias, command.Source.Columns[0].Name), aggregator));
                    }
                    TableAlias alias2     = new TableAlias();
                    QueryType  columnType = this.GetColumnType(entity, body.Member);
                    return(new ProjectionExpression(new SelectExpression(alias2, new ColumnDeclaration[] { new ColumnDeclaration("", map[body.Member], columnType) }, null, null), new ColumnExpression(King.Framework.Linq.TypeHelper.GetMemberType(body.Member), columnType, alias2, ""), aggregator));
                }
                if (func2 == null)
                {
                    func2 = (m, i) => this.GetMemberExpression(tex, entity, m).Equal(map[m]);
                }
                expression2 = source.Select <MemberInfo, Expression>(func2).Aggregate <Expression>((x, y) => x.And(y));
            }
            else
            {
                expression2 = this.GetIdentityCheck(tex, entity, instance);
            }
            Expression           entityExpression = this.GetEntityExpression(tex, entity);
            Expression           expression       = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], entityExpression);
            TableAlias           newAlias         = new TableAlias();
            ProjectedColumns     columns          = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, expression, null, newAlias, new TableAlias[] { alias });
            ProjectionExpression expression6      = new ProjectionExpression(new SelectExpression(newAlias, columns.Columns, tex, expression2), columns.Projector, aggregator);

            if (command != null)
            {
                return(new BlockCommand(new Expression[] { command, expression6 }));
            }
            return(expression6);
        }
Пример #6
0
        protected virtual Expression GetUpdateResult(MappingEntity entity, Expression instance, LambdaExpression selector)
        {
            ProjectionExpression queryExpression = this.GetQueryExpression(entity);

            Expression where = this.GetIdentityCheck(queryExpression.Select, entity, instance);
            Expression       expression = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], queryExpression.Projector);
            TableAlias       newAlias   = new TableAlias();
            ProjectedColumns columns    = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, expression, null, newAlias, new TableAlias[] { queryExpression.Select.Alias });

            return(new ProjectionExpression(new SelectExpression(newAlias, columns.Columns, queryExpression.Select, where), columns.Projector, Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(new Type[] { selector.Body.Type }))));
        }
Пример #7
0
        public override ProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            TableAlias           alias            = new TableAlias();
            TableAlias           newAlias         = new TableAlias();
            TableExpression      root             = new TableExpression(alias, entity, this.mapping.GetTableName(entity));
            Expression           entityExpression = this.GetEntityExpression(root, entity);
            ProjectedColumns     columns          = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, entityExpression, null, newAlias, new TableAlias[] { alias });
            ProjectionExpression expression       = new ProjectionExpression(new SelectExpression(newAlias, columns.Columns, root, null), columns.Projector);

            return((ProjectionExpression)this.Translator.Police.ApplyPolicy(expression, entity.ElementType));
        }
Пример #8
0
        public override ProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            IList <MappingTable> tables = this.mapping.GetTables(entity);

            if (tables.Count <= 1)
            {
                return(base.GetQueryExpression(entity));
            }
            Dictionary <string, TableAlias> aliases = new Dictionary <string, TableAlias>();
            MappingTable    table      = tables.Single <MappingTable>(ta => !this.mapping.IsExtensionTable(ta));
            TableExpression expression = new TableExpression(new TableAlias(), entity, this.mapping.GetTableName(table));

            aliases.Add(this.mapping.GetAlias(table), expression.Alias);
            Expression left = expression;

            foreach (MappingTable table2 in from t in tables
                     where this.mapping.IsExtensionTable(t)
                     select t)
            {
                TableAlias alias2;
                TableAlias alias = new TableAlias();
                string     key   = this.mapping.GetAlias(table2);
                aliases.Add(key, alias);
                List <string>     list2 = this.mapping.GetExtensionKeyColumnNames(table2).ToList <string>();
                List <MemberInfo> list3 = this.mapping.GetExtensionRelatedMembers(table2).ToList <MemberInfo>();
                string            extensionRelatedAlias = this.mapping.GetExtensionRelatedAlias(table2);
                aliases.TryGetValue(extensionRelatedAlias, out alias2);
                TableExpression right       = new TableExpression(alias, entity, this.mapping.GetTableName(table2));
                Expression      expression4 = null;
                int             num         = 0;
                int             count       = list2.Count;
                while (num < count)
                {
                    Type             memberType  = King.Framework.Linq.TypeHelper.GetMemberType(list3[num]);
                    QueryType        columnType  = this.GetColumnType(entity, list3[num]);
                    ColumnExpression expression5 = new ColumnExpression(memberType, columnType, alias2, this.mapping.GetColumnName(entity, list3[num]));
                    Expression       expression7 = new ColumnExpression(memberType, columnType, alias, list2[num]).Equal(expression5);
                    expression4 = (expression4 != null) ? expression4.And(expression7) : expression7;
                    num++;
                }
                left = new JoinExpression(JoinType.SingletonLeftOuter, left, right, expression4);
            }
            List <ColumnDeclaration> list4 = new List <ColumnDeclaration>();

            this.GetColumns(entity, aliases, list4);
            SelectExpression     root             = new SelectExpression(new TableAlias(), list4, left, null);
            Expression           entityExpression = this.GetEntityExpression(root, entity);
            TableAlias           newAlias         = new TableAlias();
            ProjectedColumns     columns          = ColumnProjector.ProjectColumns(this.Translator.Linguist.Language, entityExpression, null, newAlias, new TableAlias[] { root.Alias });
            ProjectionExpression expression10     = new ProjectionExpression(new SelectExpression(newAlias, columns.Columns, root, null), columns.Projector);

            return((ProjectionExpression)this.Translator.Police.ApplyPolicy(expression10, entity.ElementType));
        }