Ejemplo n.º 1
0
        public static ProjectedColumns ProjectColumns(QueryLanguage language, Expression expression, IEnumerable <DbColumnDeclaration> existingColumns, TableAlias newAlias, IEnumerable <TableAlias> existingAliases)
        {
            var projector = new DbColumnProjector(language, expression, existingColumns, newAlias, existingAliases);
            var expr      = projector.Visit(expression);

            return(new ProjectedColumns(expr, projector.columns.AsReadOnly()));
        }
Ejemplo n.º 2
0
        public override Expression GetMemberExpression(Expression root, MappingEntity entity, MemberInfo member)
        {
            if (this.mapping.IsAssociationRelationship(entity, member))
            {
                var relatedEntity       = this.mapping.GetRelatedEntity(entity, member);
                var projection          = this.GetQueryExpression(relatedEntity);
                var declaredTypeMembers = this.mapping.GetAssociationKeyMembers(entity, member).ToList();
                var associatedMembers   = this.mapping.GetAssociationRelatedKeyMembers(entity, member).ToList();

                var where = null as Expression;

                for (int i = 0, n = associatedMembers.Count; i < n; i++)
                {
                    var equal = this.GetMemberExpression
                                (
                        projection.Projector,
                        relatedEntity,
                        associatedMembers[i]).Equal(this.GetMemberExpression(root, entity, declaredTypeMembers[i])
                                                    );

                    where = (where != null) ? where.And(equal) : equal;
                }

                var newAlias = new TableAlias();
                var pc       = DbColumnProjector.ProjectColumns
                               (
                    this.translator.Linguist.Language,
                    projection.Projector, null,
                    newAlias,
                    projection.Select.Alias
                               );

                var aggregator = DbAggregator.GetAggregator(TypeHelper.GetMemberType(member), typeof(IEnumerable <>).MakeGenericType(pc.Projector.Type));

                var result = new DbProjectionExpression
                             (
                    new DbSelectExpression(newAlias, pc.Columns, projection.Select, where),
                    pc.Projector, aggregator
                             );

                return(this.translator.Police.ApplyPolicy(result, member));
            }

            if (root is DbAliasedExpression aliasedRoot && this.mapping.IsColumn(entity, member))
            {
                return(new DbColumnExpression
                       (
                           TypeHelper.GetMemberType(member),
                           this.GetColumnType(entity, member),
                           aliasedRoot.Alias,
                           this.mapping.GetColumnName(entity, member)
                       ));
            }

            return(DbQueryBinder.BindMember(root, member));
        }
Ejemplo n.º 3
0
        internal override DbProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            var tableAlias  = new TableAlias();
            var selectAlias = new TableAlias();
            var table       = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(entity));

            var projector = this.GetEntityExpression(table, entity) as Expression;
            var pc        = DbColumnProjector.ProjectColumns(this.translator.Linguist.Language, projector, null, selectAlias, tableAlias);

            var proj = new DbProjectionExpression(new DbSelectExpression(selectAlias, pc.Columns, table, null), pc.Projector);

            return(this.Translator.Police.ApplyPolicy(proj, entity.ElementType.GetTypeInfo()) as DbProjectionExpression);
        }
Ejemplo n.º 4
0
        protected override Expression VisitProjection(DbProjectionExpression proj)
        {
            if (isTopLevel)
            {
                isTopLevel = false;

                this.currentSelect = proj.Select;

                var projector = this.Visit(proj.Projector);

                if (projector != proj.Projector || this.currentSelect != proj.Select)
                {
                    return(new DbProjectionExpression(this.currentSelect, projector, proj.Aggregator));
                }

                return(proj);
            }

            if (proj.IsSingleton && this.CanJoinOnServer(this.currentSelect))
            {
                var newAlias = new TableAlias();

                this.currentSelect = this.currentSelect.AddRedundantSelect(this.language, newAlias);

                var source = DbColumnMapper.Map(proj.Select, newAlias, this.currentSelect.Alias) as DbSelectExpression;

                var pex = this.language.AddOuterJoinTest(new DbProjectionExpression(source, proj.Projector));

                var pc = DbColumnProjector.ProjectColumns(this.language, pex.Projector, this.currentSelect.Columns, this.currentSelect.Alias, newAlias, proj.Select.Alias);

                var join = new DbJoinExpression(JoinType.OuterApply, this.currentSelect.From, pex.Select, null);

                this.currentSelect = new DbSelectExpression(this.currentSelect.Alias, pc.Columns, join, null);

                return(this.Visit(pc.Projector));
            }

            var saveTop    = this.isTopLevel;
            var saveSelect = this.currentSelect;

            this.isTopLevel    = true;
            this.currentSelect = null;

            var result = base.VisitProjection(proj);

            this.isTopLevel    = saveTop;
            this.currentSelect = saveSelect;

            return(result);
        }
Ejemplo n.º 5
0
        protected virtual Expression GetUpdateResult(MappingEntity entity, Expression instance, LambdaExpression selector)
        {
            var tq = this.GetQueryExpression(entity);

            var where = this.GetIdentityCheck(tq.Select, entity, instance);
            var selection = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], tq.Projector);
            var newAlias  = new TableAlias();
            var pc        = DbColumnProjector.ProjectColumns(this.translator.Linguist.Language, selection, null, newAlias, tq.Select.Alias);

            return(new DbProjectionExpression
                   (
                       new DbSelectExpression(newAlias, pc.Columns, tq.Select, where),
                       pc.Projector,
                       DbAggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(selector.Body.Type))
                   ));
        }
Ejemplo n.º 6
0
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            join = base.VisitJoin(join) as DbJoinExpression;

            if (join.JoinType == JoinType.CrossApply || join.JoinType == JoinType.OuterApply)
            {
                if (join.Right is DbTableExpression)
                {
                    return(new DbJoinExpression(JoinType.CrossJoin, join.Left, join.Right, null));
                }

                if (join.Right is DbSelectExpression select && select.Take == null && select.Skip == null && !DbAggregateChecker.HasAggregates(select) && (select.GroupBy == null || select.GroupBy.Count == 0))
                {
                    var selectWithoutWhere = select.SetWhere(null);
                    var referencedAliases  = DbReferencedAliasGatherer.Gather(selectWithoutWhere);
                    var declaredAliases    = DbDeclaredAliasGatherer.Gather(join.Left);

                    if (referencedAliases != null)
                    {
                        referencedAliases.IntersectWith(declaredAliases);
                    }

                    if (referencedAliases.Count == 0)
                    {
                        var where = select.Where;

                        select = selectWithoutWhere;

                        var pc = DbColumnProjector.ProjectColumns(this.language, where, select.Columns, select.Alias, DbDeclaredAliasGatherer.Gather(select.From));

                        select = select.SetColumns(pc.Columns);

                        where = pc.Projector;

                        var jt = (where == null) ? JoinType.CrossJoin : (join.JoinType == JoinType.CrossApply ? JoinType.InnerJoin : JoinType.LeftOuter);

                        return(new DbJoinExpression(jt, join.Left, select, where));
                    }
                }
            }

            return(join);
        }
Ejemplo n.º 7
0
        internal override DbProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            var tables = this.mapping.GetTables(entity);

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

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

            aliases.Add(this.mapping.GetAlias(rootTable), tex.Alias);

            var source = tex as Expression;

            foreach (var table in tables.Where(t => this.mapping.IsExtensionTable(t)))
            {
                var joinedTableAlias = new TableAlias();
                var extensionAlias   = this.mapping.GetAlias(table);

                aliases.Add(extensionAlias, joinedTableAlias);

                var keyColumns     = this.mapping.GetExtensionKeyColumnNames(table).ToList();
                var relatedMembers = this.mapping.GetExtensionRelatedMembers(table).ToList();
                var relatedAlias   = this.mapping.GetExtensionRelatedAlias(table);

                aliases.TryGetValue(relatedAlias, out TableAlias relatedTableAlias);

                var joinedTex = new DbTableExpression(joinedTableAlias, entity, this.mapping.GetTableName(table));
                var cond      = null as Expression;

                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 DbColumnExpression(memberType, colType, relatedTableAlias, this.mapping.GetColumnName(entity, relatedMembers[i]));
                    var joinedColumn  = new DbColumnExpression(memberType, colType, joinedTableAlias, keyColumns[i]);
                    var eq            = joinedColumn.Equal(relatedColumn);

                    cond = (cond != null) ? cond.And(eq) : eq;
                }

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

            var columns = new List <DbColumnDeclaration>();

            this.GetColumns(entity, aliases, columns);

            var root            = new DbSelectExpression(new TableAlias(), columns, source, null);
            var existingAliases = aliases.Values.ToArray();

            var projector   = this.GetEntityExpression(root, entity) as Expression;
            var selectAlias = new TableAlias();
            var pc          = DbColumnProjector.ProjectColumns(this.Translator.Linguist.Language, projector, null, selectAlias, root.Alias);
            var proj        = new DbProjectionExpression
                              (
                new DbSelectExpression(selectAlias, pc.Columns, root, null),
                pc.Projector
                              );

            return(this.Translator.Police.ApplyPolicy(proj, entity.ElementType.GetTypeInfo()) as DbProjectionExpression);
        }
        protected override Expression VisitProjection(DbProjectionExpression proj)
        {
            var save = this.currentSelect;

            this.currentSelect = proj.Select;

            try
            {
                if (this.isTopLevel == false)
                {
                    if (this.CanJoinOnClient(this.currentSelect))
                    {
                        var newOuterSelect = DbQueryDuplicator.Duplicate(save) as DbSelectExpression;

                        var newInnerSelect     = DbColumnMapper.Map(proj.Select, newOuterSelect.Alias, save.Alias) as DbSelectExpression;
                        var newInnerProjection = this.language.AddOuterJoinTest(new DbProjectionExpression(newInnerSelect, proj.Projector));

                        if (newInnerProjection != null)
                        {
                            newInnerSelect = newInnerProjection.Select;
                        }

                        var newProjector = newInnerProjection.Projector;

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

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

                        this.currentSelect = joinedSelect;

                        if (pc != null)
                        {
                            newProjector = this.Visit(pc.Projector);
                        }

                        var outerKeys = new List <Expression>();
                        var innerKeys = new List <Expression>();

                        if (this.GetEquiJoinKeyExpressions(newInnerSelect.Where, newOuterSelect.Alias, outerKeys, innerKeys))
                        {
                            var outerKey      = outerKeys.Select(k => DbColumnMapper.Map(k, save.Alias, newOuterSelect.Alias));
                            var innerKey      = innerKeys.Select(k => DbColumnMapper.Map(k, joinedSelect.Alias, ((DbColumnExpression)k).Alias));
                            var newProjection = new DbProjectionExpression(joinedSelect, newProjector, proj.Aggregator);

                            return(new DbClientJoinExpression(newProjection, outerKey, innerKey));
                        }
                    }
                    else
                    {
                        var 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;
            }
        }
Ejemplo n.º 9
0
        protected virtual Expression GetInsertResult(MappingEntity entity, Expression instance, LambdaExpression selector, Dictionary <MemberInfo, Expression> map)
        {
            var tableAlias = new TableAlias();
            var tex        = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(entity));
            var aggregator = DbAggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(selector.Body.Type));

            var where = null as Expression;
            var genIdCommand = null as DbDeclarationCommand;
            var generatedIds = this.mapping.GetMappedMembers(entity).Where(m => this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)).ToList();

            if (generatedIds.Count > 0)
            {
                if (map == null || !generatedIds.Any(m => map.ContainsKey(m)))
                {
                    var localMap = new Dictionary <MemberInfo, Expression>();
                    genIdCommand = this.GetGeneratedIdCommand(entity, generatedIds.ToList(), localMap);
                    map          = localMap;
                }

                if (selector.Body is MemberExpression mex && this.mapping.IsPrimaryKey(entity, mex.Member) && this.mapping.IsGenerated(entity, mex.Member))
                {
                    if (genIdCommand != null)
                    {
                        return(new DbProjectionExpression
                               (
                                   genIdCommand.Source,
                                   new DbColumnExpression(mex.Type, genIdCommand.Variables[0].QueryType, genIdCommand.Source.Alias, genIdCommand.Source.Columns[0].Name),
                                   aggregator
                               ));
                    }
                    else
                    {
                        var alias   = new TableAlias();
                        var colType = this.GetColumnType(entity, mex.Member);

                        return(new DbProjectionExpression
                               (
                                   new DbSelectExpression(alias, new[] { new DbColumnDeclaration(string.Empty, map[mex.Member], colType) }, null, null),
                                   new DbColumnExpression(TypeHelper.GetMemberType(mex.Member), colType, alias, string.Empty),
                                   aggregator
                               ));
                    }
                }

                where = generatedIds.Select((m, i) => this.GetMemberExpression(tex, entity, m).Equal(map[m])).Aggregate((x, y) => x.And(y));
            }
            else
            {
                where = this.GetIdentityCheck(tex, entity, instance);
            }

            var typeProjector = this.GetEntityExpression(tex, entity);
            var selection     = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], typeProjector);
            var newAlias      = new TableAlias();
            var pc            = DbColumnProjector.ProjectColumns(this.translator.Linguist.Language, selection, null, newAlias, tableAlias);
            var pe            = new DbProjectionExpression
                                (
                new DbSelectExpression(newAlias, pc.Columns, tex, where),
                pc.Projector,
                aggregator
                                );

            if (genIdCommand != null)
            {
                return(new DbBlockCommand(genIdCommand, pe));
            }

            return(pe);
        }