Ejemplo n.º 1
0
        /// <summary>
        ///     Makes a copy of this SelectExpression.
        /// </summary>
        /// <param name="alias"> The alias. </param>
        /// <returns>
        ///     A copy of this SelectExpression.
        /// </returns>
        public virtual SelectExpression Clone([CanBeNull] string alias = null)
        {
            var selectExpression
                = new SelectExpression(_querySqlGeneratorFactory, _queryCompilationContext)
                {
                _limit         = _limit,
                _offset        = _offset,
                _isDistinct    = _isDistinct,
                _subqueryDepth = _subqueryDepth,
                IsProjectStar  = IsProjectStar,
                Predicate      = Predicate
                };

            if (alias != null)
            {
                selectExpression.Alias = _queryCompilationContext.CreateUniqueTableAlias(alias);
            }

            selectExpression._projection.AddRange(_projection);

            selectExpression.AddTables(_tables);
            selectExpression.AddToOrderBy(_orderBy);

            return(selectExpression);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Creates a subquery based on this SelectExpression and makes that table the single entry in
        ///     <see cref="Tables" />. Clears all other top-level aspects of this SelectExpression.
        /// </summary>
        /// <returns>
        ///     A SelectExpression.
        /// </returns>
        public virtual SelectExpression PushDownSubquery()
        {
            _subqueryDepth++;

            var subquery = new SelectExpression(_querySqlGeneratorFactory, _queryCompilationContext, SystemAliasPrefix);

            var columnAliasCounter = 0;

            foreach (var expression in _projection)
            {
                var aliasExpression = expression as AliasExpression;

                if (aliasExpression != null)
                {
                    var columnExpression = aliasExpression.TryGetColumnExpression();

                    if (columnExpression != null &&
                        subquery._projection.OfType <AliasExpression>()
                        .Any(ae => (ae.Alias ?? ae.TryGetColumnExpression()?.Name) == (aliasExpression.Alias ?? columnExpression.Name)))
                    {
                        aliasExpression.Alias = "c" + columnAliasCounter++;
                    }
                }
                else
                {
                    aliasExpression = new AliasExpression("c" + columnAliasCounter++, expression);
                }

                subquery._projection.Add(aliasExpression);
            }

            subquery.AddTables(_tables);
            subquery.AddToOrderBy(_orderBy);

            subquery.Predicate = Predicate;

            subquery._limit           = _limit;
            subquery._offset          = _offset;
            subquery._isDistinct      = _isDistinct;
            subquery._subqueryDepth   = _subqueryDepth;
            subquery.ProjectStarAlias = ProjectStarAlias;
            subquery.IsProjectStar    = IsProjectStar || !subquery._projection.Any();

            _limit           = null;
            _offset          = null;
            _isDistinct      = false;
            ProjectStarAlias = null;

            Predicate = null;

            ClearTables();
            ClearProjection();
            ClearOrderBy();

            AddTable(subquery, createUniqueAlias: false);
            ProjectStarAlias = subquery.Alias;

            return(subquery);
        }
Ejemplo n.º 3
0
        public virtual SelectExpression Clone([NotNull] string alias)
        {
            Check.NotNull(alias, nameof(alias));

            var selectExpression
                = new SelectExpression(_querySqlGeneratorFactory, alias)
                {
                _limit         = _limit,
                _offset        = _offset,
                _isDistinct    = _isDistinct,
                _subqueryDepth = _subqueryDepth,
                IsProjectStar  = IsProjectStar,
                Predicate      = Predicate
                };

            selectExpression._projection.AddRange(_projection);

            selectExpression.AddTables(_tables);
            selectExpression.AddToOrderBy(_orderBy);

            return(selectExpression);
        }
Ejemplo n.º 4
0
        public virtual SelectExpression Clone([NotNull] string alias)
        {
            Check.NotNull(alias, nameof(alias));

            var selectExpression
                = new SelectExpression(_querySqlGeneratorFactory, _queryCompilationContext, alias)
                {
                    _limit = _limit,
                    _offset = _offset,
                    _isDistinct = _isDistinct,
                    _subqueryDepth = _subqueryDepth,
                    IsProjectStar = IsProjectStar,
                    Predicate = Predicate
                };

            selectExpression._projection.AddRange(_projection);

            selectExpression.AddTables(_tables);
            selectExpression.AddToOrderBy(_orderBy);

            return selectExpression;
        }
Ejemplo n.º 5
0
        public virtual SelectExpression PushDownSubquery()
        {
            _subqueryDepth++;

            var subquery = new SelectExpression(_querySqlGeneratorFactory, _queryCompilationContext, SystemAliasPrefix);

            var columnAliasCounter = 0;

            foreach (var expression in _projection)
            {
                var aliasExpression = expression as AliasExpression;

                if (aliasExpression != null)
                {
                    var columnExpression = aliasExpression.TryGetColumnExpression();

                    if (columnExpression != null
                        && subquery._projection.OfType<AliasExpression>()
                            .Any(ae => ae.TryGetColumnExpression()?.Name == columnExpression.Name))
                    {
                        aliasExpression.Alias = "c" + columnAliasCounter++;
                    }
                }
                else
                {
                    aliasExpression = new AliasExpression("c" + columnAliasCounter++, expression);
                }

                subquery._projection.Add(aliasExpression);
            }

            subquery.AddTables(_tables);
            subquery.AddToOrderBy(_orderBy);

            subquery.Predicate = Predicate;

            subquery._limit = _limit;
            subquery._offset = _offset;
            subquery._isDistinct = _isDistinct;
            subquery._subqueryDepth = _subqueryDepth;
            subquery.IsProjectStar = IsProjectStar || !subquery._projection.Any();

            _limit = null;
            _offset = null;
            _isDistinct = false;

            Predicate = null;

            ClearTables();
            ClearProjection();
            ClearOrderBy();

            AddTable(subquery, createUniqueAlias: false);

            return subquery;
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Makes a copy of this SelectExpression.
        /// </summary>
        /// <param name="alias"> The alias. </param>
        /// <returns>
        ///     A copy of this SelectExpression.
        /// </returns>
        public virtual SelectExpression Clone([CanBeNull] string alias = null)
        {
            var selectExpression
                = new SelectExpression(_querySqlGeneratorFactory, _queryCompilationContext)
                {
                    _limit = _limit,
                    _offset = _offset,
                    _isDistinct = _isDistinct,
                    _subqueryDepth = _subqueryDepth,
                    IsProjectStar = IsProjectStar,
                    Predicate = Predicate
                };

            if (alias != null)
            {
                selectExpression.Alias = _queryCompilationContext.CreateUniqueTableAlias(alias);
            }

            selectExpression._projection.AddRange(_projection);

            selectExpression.AddTables(_tables);
            selectExpression.AddToOrderBy(_orderBy);

            return selectExpression;
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Creates a subquery based on this SelectExpression and makes that table the single entry in
        ///     <see cref="Tables" />. Clears all other top-level aspects of this SelectExpression.
        /// </summary>
        /// <returns>
        ///     A SelectExpression.
        /// </returns>
        public virtual SelectExpression PushDownSubquery()
        {
            _subqueryDepth++;

            var subquery = new SelectExpression(Dependencies, _queryCompilationContext, SystemAliasPrefix);

            var columnAliasCounter = 0;

            foreach (var expression in _projection)
            {
                var aliasExpression = expression as AliasExpression;

                if (aliasExpression != null)
                {
                    var columnExpression = aliasExpression.TryGetColumnExpression();

                    if ((columnExpression != null &&
                         subquery._projection.OfType <AliasExpression>()
                         .Any(ae => (ae.Alias ?? ae.TryGetColumnExpression()?.Name) == (aliasExpression.Alias ?? columnExpression.Name))) ||
                        columnExpression == null)
                    {
                        aliasExpression.Alias = "c" + columnAliasCounter++;
                    }
                }
                else
                {
                    aliasExpression = new AliasExpression("c" + columnAliasCounter++, expression);
                }

                subquery._projection.Add(aliasExpression);
            }

            subquery.AddTables(_tables);
            subquery.AddToOrderBy(_orderBy);

            subquery.Predicate = Predicate;

            subquery._limit           = _limit;
            subquery._offset          = _offset;
            subquery._isDistinct      = _isDistinct;
            subquery._subqueryDepth   = _subqueryDepth;
            subquery.ProjectStarTable = ProjectStarTable;
            subquery.IsProjectStar    = IsProjectStar || !subquery._projection.Any();

            _limit           = null;
            _offset          = null;
            _isDistinct      = false;
            ProjectStarTable = null;

            Predicate = null;

            ClearTables();
            ClearProjection();
            ClearOrderBy();

            _tables.Add(subquery);
            ProjectStarTable = subquery;

            foreach (var ordering in subquery.OrderBy)
            {
                var expression = ordering.Expression;

                var aliasExpression = expression as AliasExpression;
                if (aliasExpression != null)
                {
                    if (aliasExpression.Alias != null)
                    {
                        _orderBy.Add(
                            new Ordering(
                                new ColumnExpression(aliasExpression.Alias, aliasExpression.Type, subquery),
                                ordering.OrderingDirection));
                    }
                    else
                    {
                        var newExpression = UpdateColumnExpression(aliasExpression.Expression, subquery);

                        _orderBy.Add(
                            new Ordering(
                                new AliasExpression(newExpression), ordering.OrderingDirection));
                    }
                }
                else
                {
                    if (!subquery.IsProjectStar)
                    {
                        subquery.AddToProjection(expression);
                    }

                    var newExpression = UpdateColumnExpression(expression, subquery);

                    _orderBy.Add(
                        new Ordering(
                            new AliasExpression(newExpression), ordering.OrderingDirection));
                }
            }

            if (subquery.Limit == null &&
                subquery.Offset == null)
            {
                subquery.ClearOrderBy();
            }

            return(subquery);
        }