Inheritance: SqlBaseExpression
コード例 #1
0
        protected override Expression VisitOver(SqlOverExpression expression)
        {
            SqlOverExpression current;

            if (!this.TryGetCurrent(expression, out current))
            {
                return(expression);
            }

            foreach (var value in expression.OrderBy)
            {
                this.Visit(value);

                if (!result)
                {
                    return(expression);
                }
            }

            if (!result)
            {
                return(expression);
            }

            this.Visit(expression.Source);

            if (!result)
            {
                return(expression);
            }

            return(expression);
        }
コード例 #2
0
        protected override Expression VisitSelect(SqlSelectExpression selectExpression)
        {
            if (selectExpression.Skip != null)
            {
                var rowNumber = new SqlFunctionCallExpression(typeof(int), "ROW_NUMBER");
                var over = new SqlOverExpression(rowNumber, selectExpression.OrderBy ?? new ReadOnlyList<Expression>(new [] { new SqlOrderByExpression(OrderType.Ascending, selectExpression.Columns[0].Expression) }));
                var additionalColumn = new SqlColumnDeclaration(RowColumnName, over);

                var newAlias = selectExpression.Alias + "INNER";
                var innerColumns = new ReadOnlyList<SqlColumnDeclaration>(selectExpression.Columns.Select(c => c).Concat(new[] { additionalColumn }));

                var innerSelect = new SqlSelectExpression(selectExpression.Type, newAlias, innerColumns, selectExpression.From, selectExpression.Where, null, selectExpression.GroupBy, selectExpression.Distinct, null, null, selectExpression.ForUpdate);

                var outerColumns = selectExpression.Columns.Select(c => new SqlColumnDeclaration(c.Name, new SqlColumnExpression(c.Expression.Type, newAlias, c.Name)));

                Expression rowPredicate = Expression.GreaterThan(new SqlColumnExpression(typeof(int), newAlias, additionalColumn.Name), selectExpression.Skip);

                if (selectExpression.Take != null && !(selectExpression.Take is SqlTakeAllValueExpression))
                {
                    rowPredicate = Expression.And
                    (
                        rowPredicate,
                        Expression.LessThanOrEqual(new SqlColumnExpression(typeof(int), newAlias, additionalColumn.Name), Expression.Add(selectExpression.Skip, selectExpression.Take))
                    );
                }

                return new SqlSelectExpression(selectExpression.Type, selectExpression.Alias, outerColumns, innerSelect, rowPredicate, null, selectExpression.ForUpdate);
            }

            return base.VisitSelect(selectExpression);
        }
コード例 #3
0
        protected override Expression VisitOver(SqlOverExpression expression)
        {
            SqlOverExpression current;
            if (!TryGetCurrent(expression, out current))
            {
                return expression;
            }

            if (!(this.result &= current.NodeType == expression.NodeType))
            {
                return expression;
            }

            if (!(this.result &= current.Type == expression.Type))
            {
                return expression;
            }

            this.currentObject = current.Source;
            this.Visit(expression.Source);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current.OrderBy;
            this.VisitExpressionList(expression.OrderBy);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current;
            return expression;
        }
コード例 #4
0
        protected virtual Expression VisitOver(SqlOverExpression expression)
        {
            var source  = this.Visit(expression.Source);
            var orderBy = this.VisitExpressionList(expression.OrderBy);

            if (source != expression.Source || orderBy != expression.OrderBy)
            {
                return(new SqlOverExpression(source, orderBy));
            }

            return(expression);
        }
コード例 #5
0
		protected override Expression VisitSelect(SqlSelectExpression selectExpression)
		{
			if (selectExpression.Skip != null)
			{
				var rowNumber = new SqlFunctionCallExpression(typeof(int), "ROW_NUMBER");

				var oldAliases = (selectExpression.From as ISqlExposesAliases)?.Aliases;
				var innerSelectWithRowAlias = selectExpression.Alias + "_ROW";

				var cols = selectExpression.Columns.Select(c => new SqlColumnDeclaration(c.Name, new SqlColumnExpression(c.Expression.Type, selectExpression.Alias, c.Name))).ToList();
				var over = new SqlOverExpression(rowNumber, selectExpression.OrderBy?.ToReadOnlyCollection() ?? cols.Select(c => new SqlOrderByExpression(OrderType.Ascending, c.Expression)).ToReadOnlyCollection());

				if (oldAliases != null)
				{
					over = (SqlOverExpression)AliasReferenceReplacer.Replace(over, oldAliases.Contains, selectExpression.Alias);
				}

				var rowColumn = new SqlColumnDeclaration(RowColumnName, over);

				cols.Add(rowColumn);

				var innerSelectWithRowColumns = cols.ToReadOnlyCollection();
				var innerSelectWithRow = new SqlSelectExpression(selectExpression.Type, innerSelectWithRowAlias, innerSelectWithRowColumns, this.Visit(selectExpression.ChangeOrderBy(null).ChangeSkipTake(null, null)), null, null, null, false, null, null, false);
				var outerColumns = selectExpression.Columns.Select(c => new SqlColumnDeclaration(c.Name, new SqlColumnExpression(c.Expression.Type, innerSelectWithRowAlias, c.Name)));

				Expression rowPredicate = Expression.GreaterThan(new SqlColumnExpression(typeof(int), innerSelectWithRowAlias, rowColumn.Name), selectExpression.Skip);

				if (selectExpression.Take != null && !(selectExpression.Take is SqlTakeAllValueExpression))
				{
					rowPredicate = Expression.And
					(
						rowPredicate,
						Expression.LessThanOrEqual(new SqlColumnExpression(typeof(int), innerSelectWithRowAlias, rowColumn.Name), Expression.Add(selectExpression.Skip, selectExpression.Take))
					);
				}

				var retval = new SqlSelectExpression(selectExpression.Type, selectExpression.Alias, outerColumns.ToReadOnlyCollection(), innerSelectWithRow, rowPredicate, null, null, selectExpression.Distinct, null, null, selectExpression.ForUpdate, selectExpression.Reverse, selectExpression.Into);

				return retval;
			}

			return base.VisitSelect(selectExpression);
		}
コード例 #6
0
        protected override Expression VisitOver(SqlOverExpression selectExpression)
        {
            this.Visit(selectExpression.Source);

            this.Write(" OVER (ORDER BY ");

            this.WriteDeliminatedListOfItems<Expression>(selectExpression.OrderBy, c =>
            {
                this.Visit(c);

                if (((SqlOrderByExpression)c).OrderType == OrderType.Descending)
                {
                    this.Write(" DESC");
                }
            });

            this.Write(")");

            return selectExpression;
        }
コード例 #7
0
 protected override Expression VisitOver(SqlOverExpression expression)
 {
     // TODO
     return(base.VisitOver(expression));
 }
コード例 #8
0
		protected virtual Expression VisitOver(SqlOverExpression expression)
		{
			var source = this.Visit(expression.Source);
			var orderBy = this.VisitExpressionList(expression.OrderBy);

			if (source != expression.Source || orderBy != expression.OrderBy)
			{
				return new SqlOverExpression(source, orderBy);
			}

			return expression;
		}
コード例 #9
0
		protected override Expression VisitOver(SqlOverExpression selectExpression)
		{
			this.Visit(selectExpression.Source);

			this.Write(" OVER (ORDER BY ");

			this.WriteDeliminatedListOfItems<Expression>(selectExpression.OrderBy, c =>
			{
				this.Visit(c);
			});

			this.Write(")");

			return selectExpression;
		}