예제 #1
0
 protected override Expression VisitJoin(JoinExpression join)
 {
     join = (JoinExpression)base.VisitJoin(join);
     if (join.Join == JoinType.CrossJoin && this.currentWhere != null)
     {
         // try to figure out which parts of the current where expression can be used for a join condition
         var declaredLeft  = DeclaredAliasGatherer.Gather(join.Left);
         var declaredRight = DeclaredAliasGatherer.Gather(join.Right);
         var declared      = new HashSet <TableAlias>(declaredLeft.Union(declaredRight));
         var exprs         = this.currentWhere.Split(ExpressionType.And, ExpressionType.AndAlso);
         var good          = exprs.Where(e => CanBeJoinCondition(e, declaredLeft, declaredRight, declared)).ToList();
         if (good.Count > 0)
         {
             var condition = good.Join(ExpressionType.And);
             join = this.UpdateJoin(join, JoinType.InnerJoin, join.Left, join.Right, condition);
             var newWhere = exprs.Where(e => !good.Contains(e)).Join(ExpressionType.And);
             this.currentWhere = newWhere;
         }
     }
     return(join);
 }
예제 #2
0
        protected override Expression VisitJoin(JoinExpression join)
        {
            this.VisitJoinLeft(join.Left);
            this.WriteLine(Indentation.Same);
            switch (join.Join)
            {
            case JoinType.CrossJoin:
                this.Write("CROSS JOIN ");
                break;

            case JoinType.InnerJoin:
                this.Write("INNER JOIN ");
                break;

            case JoinType.CrossApply:
                this.Write("CROSS APPLY ");
                break;

            case JoinType.OuterApply:
                this.Write("OUTER APPLY ");
                break;

            case JoinType.LeftOuter:
            case JoinType.SingletonLeftOuter:
                this.Write("LEFT OUTER JOIN ");
                break;
            }
            this.VisitJoinRight(join.Right);
            if (join.Condition != null)
            {
                this.WriteLine(Indentation.Inner);
                this.Write("ON ");
                this.VisitPredicate(join.Condition);
                this.Indent(Indentation.Outer);
            }
            return(join);
        }
예제 #3
0
        public override ProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            var tables = this.mapping.GetTables(entity);

            if (tables.Count <= 1)
            {
                return(base.GetQueryExpression(entity));
            }

            var          aliases   = new Dictionary <string, TableAlias>();
            MappingTable rootTable = tables.Single(ta => !this.mapping.IsExtensionTable(ta));
            var          tex       = new TableExpression(new TableAlias(), entity, this.mapping.GetTableName(rootTable));

            aliases.Add(this.mapping.GetTableId(rootTable), tex.Alias);
            Expression source = tex;

            foreach (MappingTable table in tables.Where(t => this.mapping.IsExtensionTable(t)))
            {
                TableAlias joinedTableAlias = new TableAlias();
                string     extensionAlias   = this.mapping.GetTableId(table);
                aliases.Add(extensionAlias, joinedTableAlias);

                List <string>     keyColumns     = this.mapping.GetExtensionKeyColumnNames(table).ToList();
                List <MemberInfo> relatedMembers = this.mapping.GetExtensionRelatedMembers(table).ToList();
                string            relatedAlias   = this.mapping.GetExtensionRelatedTableId(table);

                TableAlias relatedTableAlias;
                aliases.TryGetValue(relatedAlias, out relatedTableAlias);

                TableExpression joinedTex = new TableExpression(joinedTableAlias, entity, this.mapping.GetTableName(table));

                Expression cond = null;
                for (int i = 0, n = keyColumns.Count; i < n; i++)
                {
                    var memberType    = TypeHelper.GetMemberType(relatedMembers[i]);
                    var colType       = this.GetColumnType(entity, relatedMembers[i]);
                    var relatedColumn = new ColumnExpression(memberType, colType, relatedTableAlias, this.mapping.GetColumnName(entity, relatedMembers[i]));
                    var joinedColumn  = new ColumnExpression(memberType, colType, joinedTableAlias, keyColumns[i]);
                    var eq            = joinedColumn.Equal(relatedColumn);
                    cond = (cond != null) ? cond.And(eq) : eq;
                }

                source = new JoinExpression(JoinType.SingletonLeftOuter, source, joinedTex, cond);
            }

            var columns = new List <ColumnDeclaration>();

            this.GetColumns(entity, aliases, columns);
            SelectExpression root = new SelectExpression(new TableAlias(), columns, source, null);
            var existingAliases   = aliases.Values.ToArray();

            Expression projector   = this.GetEntityExpression(root, entity);
            var        selectAlias = new TableAlias();
            var        pc          = ColumnProjector.ProjectColumns(this.Translator.Linguist.Language, projector, null, selectAlias, root.Alias);
            var        proj        = new ProjectionExpression(
                new SelectExpression(selectAlias, pc.Columns, root, null),
                pc.Projector
                );

            return((ProjectionExpression)this.Translator.Police.ApplyPolicy(proj, entity.StaticType.GetTypeInfo()));
        }
        protected override Expression VisitProjection(ProjectionExpression proj)
        {
            SelectExpression save = this.currentSelect;

            this.currentSelect = proj.Select;
            try
            {
                if (!this.isTopLevel)
                {
                    if (this.CanJoinOnClient(this.currentSelect))
                    {
                        // make a query that combines all the constraints from the outer queries into a single select
                        SelectExpression newOuterSelect = (SelectExpression)QueryDuplicator.Duplicate(save);

                        // remap any references to the outer select to the new alias;
                        SelectExpression newInnerSelect = (SelectExpression)ColumnMapper.Map(proj.Select, newOuterSelect.Alias, save.Alias);
                        // add outer-join test
                        ProjectionExpression newInnerProjection = this.language.AddOuterJoinTest(new ProjectionExpression(newInnerSelect, proj.Projector));
                        newInnerSelect = newInnerProjection.Select;
                        Expression newProjector = newInnerProjection.Projector;

                        TableAlias newAlias = new TableAlias();
                        var        pc       = ColumnProjector.ProjectColumns(this.language, newProjector, null, newAlias, newOuterSelect.Alias, newInnerSelect.Alias);

                        JoinExpression   join         = new JoinExpression(JoinType.OuterApply, newOuterSelect, newInnerSelect, null);
                        SelectExpression joinedSelect = new SelectExpression(newAlias, pc.Columns, join, null, null, null, proj.IsSingleton, null, null, false);

                        // apply client-join treatment recursively
                        this.currentSelect = joinedSelect;
                        newProjector       = this.Visit(pc.Projector);

                        // compute keys (this only works if join condition was a single column comparison)
                        List <Expression> outerKeys = new List <Expression>();
                        List <Expression> innerKeys = new List <Expression>();
                        if (this.GetEquiJoinKeyExpressions(newInnerSelect.Where, newOuterSelect.Alias, outerKeys, innerKeys))
                        {
                            // outerKey needs to refer to the outer-scope's alias
                            var outerKey = outerKeys.Select(k => ColumnMapper.Map(k, save.Alias, newOuterSelect.Alias));
                            // innerKey needs to refer to the new alias for the select with the new join
                            var innerKey = innerKeys.Select(k => ColumnMapper.Map(k, joinedSelect.Alias, ((ColumnExpression)k).Alias));
                            ProjectionExpression newProjection = new ProjectionExpression(joinedSelect, newProjector, proj.Aggregator);
                            return(new ClientJoinExpression(newProjection, outerKey, innerKey));
                        }
                    }
                    else
                    {
                        bool saveJoin = this.canJoinOnClient;
                        this.canJoinOnClient = false;
                        var result = base.VisitProjection(proj);
                        this.canJoinOnClient = saveJoin;
                        return(result);
                    }
                }
                else
                {
                    this.isTopLevel = false;
                }
                return(base.VisitProjection(proj));
            }
            finally
            {
                this.currentSelect = save;
            }
        }