/// <inheritdoc />
        public override Expression VisitAlias(AliasExpression aliasExpression)
        {
            var validatedAlias = SpannerQueryCompilationContext.GetValidSymbol(aliasExpression.Alias);

            return(base.VisitAlias(validatedAlias != aliasExpression.Alias
                ? new AliasExpression(validatedAlias, aliasExpression.Expression)
                : aliasExpression));
        }
        public void AliasExpressionTest()
        {
            AliasMapper     aliasMap   = new AliasMapper();
            AliasExpression expression = new AliasExpression(aliasMap);

            Assert.IsTrue(expression.Match("glob is C"));
            Assert.IsFalse(expression.Match("glob is N"));
            expression.Execute("glob is C");
            Assert.IsTrue(aliasMap.Exists("glob"));
            Assert.IsTrue(String.Equals(aliasMap.GetValueForAlias("glob"), "C"));
        }
Beispiel #3
0
        public void AliasExpressionTest()
        {
            AliasMapper     AliasMap   = new AliasMapper();
            AliasExpression expression = new AliasExpression(AliasMap);

            Assert.True(expression.Match("glob is I"));
            Assert.False(expression.Match("glob is N"));
            expression.Execute("glob is I");
            Assert.True(AliasMap.Exists("glob"));
            Assert.Equal("I", AliasMap.GetValueForAlias("glob"));
        }
Beispiel #4
0
        Expression VisitSelectExpression(SelectExpression selectExpression)
        {
            base.Visit(selectExpression);
            if (!RequiresRowNumberPaging(selectExpression))
            {
                return(selectExpression);
            }
            var subQuery = selectExpression.PushDownSubquery();

            foreach (var projection in subQuery.Projection)
            {
                selectExpression.AddToProjection(projection.LiftExpressionFromSubquery(subQuery));
            }
            if (subQuery.OrderBy.Count == 0)
            {
                subQuery.AddToOrderBy(
                    new Ordering(new SqlFunctionExpression("@@RowCount", typeof(int)), OrderingDirection.Asc));
            }
            var innerRowNumberExpression = new AliasExpression(
                RowNumberColumnName + (_counter != 0 ? $"{_counter}" : ""),
                new RowNumberExpression_SqlServer(subQuery.OrderBy));

            _counter++;
            subQuery.ClearOrderBy();
            subQuery.AddToProjection(innerRowNumberExpression, false);
            var rowNumberReferenceExpression = new ColumnReferenceExpression(innerRowNumberExpression, subQuery);
            var offset = subQuery.Offset ?? Expression.Constant(0);

            if (subQuery.Offset != null)
            {
                selectExpression.AddToPredicate
                    (Expression.GreaterThan(rowNumberReferenceExpression, offset));
                subQuery.Offset = null;
            }
            if (subQuery.Limit != null)
            {
                var constantValue = (subQuery.Limit as ConstantExpression)?.Value;
                var offsetValue   = (offset as ConstantExpression)?.Value;
                var limitExpression
                    = constantValue != null &&
                      offsetValue != null
                ? (Expression)Expression.Constant((int)offsetValue + (int)constantValue)
                : Expression.Add(offset, subQuery.Limit);
                selectExpression.AddToPredicate(
                    Expression.LessThanOrEqual(rowNumberReferenceExpression, limitExpression));
                subQuery.Limit = null;
            }
            return(selectExpression);
        }
		/// <summary>
		/// 重写生成投影,对表达式重新处理。
		/// </summary>
		/// <param name="projection">投影表达式</param>
		protected override void GenerateProjection(Expression projection)
		{
			try
			{
				if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
				{
					Trace<DbLoggerCategory.Query>.Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Entry, OracleTraceClassName.OracleQuerySqlGenerator, OracleTraceFuncName.GenerateProjection);
				}

				AliasExpression aliasExpression = projection as AliasExpression;
				Expression expressionProcess = aliasExpression?.Expression ?? projection;
				Expression updatedExperssion = ExplicitCastToBool(expressionProcess);

				expressionProcess = aliasExpression != null
					? new AliasExpression(aliasExpression.Alias, updatedExperssion)
					: updatedExperssion;

				// 此处会调用OracleSqlGenerationHelper中方法改写表达式,例如为表和字段加上引号等
				base.GenerateProjection(expressionProcess);

				if (is112SqlCompatibility && outerSelectRequired)
				{
					if (!(projection is AliasExpression))
					{
						Sql.Append(" K" + generateProjectionCallCountCounter);
					}
					generateProjectionCallCountCounter++;
				}
			}
			catch (Exception ex)
			{
				if (Check.IsErrorEnabled(m_oracleLogger?.Logger))
				{
					Trace<DbLoggerCategory.Query>.Write(m_oracleLogger, LogLevel.Error, OracleTraceTag.Error, OracleTraceClassName.OracleQuerySqlGenerator, OracleTraceFuncName.GenerateProjection, ex.ToString());
				}
				throw;
			}
			finally
			{
				if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
				{
					Trace<DbLoggerCategory.Query>.Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Exit, OracleTraceClassName.OracleQuerySqlGenerator, OracleTraceFuncName.GenerateProjection);
				}
			}
		}
Beispiel #6
0
        public override Expression VisitAlias(AliasExpression aliasExpression)
        {
            Check.NotNull(aliasExpression, nameof(aliasExpression));

            Visit(aliasExpression.Expression);

            if (aliasExpression.Alias != null)
            {
                Sql.Append(" ");
            }

            if (aliasExpression.Alias != null)
            {
                Sql.Append(SqlGenerator.DelimitIdentifier(aliasExpression.Alias));
            }

            return(aliasExpression);
        }
        public virtual Expression VisitAliasExpression(AliasExpression aliasExpression)
        {
            Check.NotNull(aliasExpression, nameof(aliasExpression));
            if (!aliasExpression.Projected)
            {
                VisitExpression(aliasExpression.Expression);
                if (aliasExpression.Alias != null)
                {
                    _sql.Append(" AS ");
                }
            }
            if (aliasExpression.Alias != null)
            {
                _sql.Append(DelimitIdentifier(aliasExpression.Alias));
            }

            return(aliasExpression);
        }
        public virtual Expression VisitAlias(AliasExpression aliasExpression)
        {
            Check.NotNull(aliasExpression, nameof(aliasExpression));

            if (!aliasExpression.Projected)
            {
                Visit(aliasExpression.Expression);

                if (aliasExpression.Alias != null)
                {
                    _relationalCommandBuilder.Append(" AS ");
                }
            }

            if (aliasExpression.Alias != null)
            {
                _relationalCommandBuilder.Append(_sqlGenerationHelper.DelimitIdentifier(aliasExpression.Alias));
            }

            return(aliasExpression);
        }
Beispiel #9
0
 protected override void GenerateProjection(Expression projection)
 {
     try
     {
         if (m_oracleLogger != null && m_oracleLogger.Logger != null && m_oracleLogger.Logger.IsEnabled(LogLevel.Trace))
         {
             Trace <DbLoggerCategory.Query> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Entry, OracleTraceClassName.OracleQuerySqlGenerator, OracleTraceFuncName.GenerateProjection);
         }
         AliasExpression aliasExpression = projection as AliasExpression;
         Expression      expression      = aliasExpression?.Expression ?? projection;
         Expression      expression2     = ExplicitCastToBool(expression);
         expression = ((aliasExpression != null) ? new AliasExpression(aliasExpression.Alias, expression2) : expression2);
         base.GenerateProjection(expression);
         if (is112SqlCompatibility && outerSelectRequired)
         {
             if (!(projection is AliasExpression))
             {
                 Sql.Append(" K" + generateProjectionCallCountCounter);
             }
             generateProjectionCallCountCounter++;
         }
     }
     catch (Exception ex)
     {
         if (m_oracleLogger != null && m_oracleLogger.Logger != null && m_oracleLogger.Logger.IsEnabled(LogLevel.Error))
         {
             Trace <DbLoggerCategory.Query> .Write(m_oracleLogger, LogLevel.Error, OracleTraceTag.Error, OracleTraceClassName.OracleQuerySqlGenerator, OracleTraceFuncName.GenerateProjection, ex.ToString());
         }
         throw;
     }
     finally
     {
         if (m_oracleLogger != null && m_oracleLogger.Logger != null && m_oracleLogger.Logger.IsEnabled(LogLevel.Trace))
         {
             Trace <DbLoggerCategory.Query> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Exit, OracleTraceClassName.OracleQuerySqlGenerator, OracleTraceFuncName.GenerateProjection);
         }
     }
 }
        Expression VisitSelectExpression(SelectExpression selectExpression)
        {
            base.Visit(selectExpression);
            if (!RequiresRowNumberPaging(selectExpression))
            {
                return(selectExpression);
            }
            SelectExpression selectExpression2 = selectExpression.PushDownSubquery();

            foreach (Expression item in selectExpression2.Projection)
            {
                selectExpression.AddToProjection(item.LiftExpressionFromSubquery(selectExpression2), true);
            }
            AliasExpression aliasExpression = new AliasExpression("row_number" + ((_counter != 0) ? $"{_counter}" : ""), new RowNumberExpression_DB2(selectExpression2.OrderBy));

            _counter++;
            selectExpression2.ClearOrderBy();
            selectExpression2.AddToProjection(aliasExpression, false);
            ColumnReferenceExpression left = new ColumnReferenceExpression(aliasExpression, selectExpression2);
            Expression expression          = selectExpression2.Offset ?? Expression.Constant(0);

            if (selectExpression2.Offset != null)
            {
                selectExpression.AddToPredicate(Expression.GreaterThan(left, expression));
                selectExpression2.Offset = null;
            }
            if (selectExpression2.Limit != null)
            {
                object     obj   = (selectExpression2.Limit as ConstantExpression)?.Value;
                object     obj2  = (expression as ConstantExpression)?.Value;
                Expression right = (obj != null && obj2 != null) ? ((Expression)Expression.Constant((int)obj2 + (int)obj)) : ((Expression)Expression.Add(expression, selectExpression2.Limit));
                selectExpression.AddToPredicate(Expression.LessThanOrEqual(left, right));
                selectExpression2.Limit = null;
            }
            return(selectExpression);
        }
Beispiel #11
0
 public static ColumnExpression TryGetColumnExpression([NotNull] this AliasExpression aliasExpression)
 => aliasExpression.Expression as ColumnExpression;
Beispiel #12
0
 public static bool HasColumnExpression([CanBeNull] this AliasExpression aliasExpression)
 => aliasExpression?.Expression is ColumnExpression;
            private Expression VisitSelectExpression(SelectExpression selectExpression)
            {
                base.Visit(selectExpression);

                if (!RequiresRowNumberPaging(selectExpression))
                {
                    return(selectExpression);
                }

                var subQuery = selectExpression.PushDownSubquery();

                foreach (var projection in subQuery.Projection)
                {
                    var alias  = projection as AliasExpression;
                    var column = projection as ColumnExpression;

                    if (column != null)
                    {
                        column = new ColumnExpression(column.Name, column.Property, subQuery);
                        selectExpression.AddToProjection(column);
                        continue;
                    }

                    column = alias?.TryGetColumnExpression();

                    if (column != null)
                    {
                        column = new ColumnExpression(column.Name, column.Property, subQuery);
                        alias  = new AliasExpression(alias.Alias, column);
                        selectExpression.AddToProjection(alias);
                    }
                    else
                    {
                        column = new ColumnExpression(alias.Alias, alias.Expression.Type, subQuery);
                        selectExpression.AddToProjection(column);
                    }
                }

                if (subQuery.OrderBy.Count == 0)
                {
                    subQuery.AddToOrderBy(
                        new Ordering(new SqlFunctionExpression("@@RowCount", typeof(int)), OrderingDirection.Asc));
                }

                var columnExpression = new ColumnExpression(RowNumberColumnName, typeof(int), subQuery);
                var rowNumber        = new RowNumberExpression(columnExpression, subQuery.OrderBy);

                subQuery.ClearOrderBy();
                subQuery.AddToProjection(rowNumber, false);

                Expression predicate = null;

                var offset = subQuery.Offset ?? Expression.Constant(0);

                if (subQuery.Offset != null)
                {
                    predicate = Expression.GreaterThan(columnExpression, offset);
                }

                if (subQuery.Limit != null)
                {
                    var constantValue = (subQuery.Limit as ConstantExpression)?.Value;
                    var offsetValue   = (offset as ConstantExpression)?.Value;

                    var limitExpression
                        = constantValue != null &&
                          offsetValue != null
                            ? (Expression)Expression.Constant((int)offsetValue + (int)constantValue)
                            : Expression.Add(offset, subQuery.Limit);

                    var expression = Expression.LessThanOrEqual(columnExpression, limitExpression);

                    if (predicate != null)
                    {
                        expression = Expression.AndAlso(predicate, expression);
                    }

                    predicate = expression;
                }

                selectExpression.Predicate = predicate;

                return(selectExpression);
            }
Beispiel #14
0
            private Expression VisitSelectExpression(SelectExpression selectExpression)
            {
                base.Visit(selectExpression);

                if (!RequiresRowNumberPaging(selectExpression))
                {
                    return(selectExpression);
                }

                var projections = new List <Expression>();

                projections.AddRange(selectExpression.Projection);

                var subQuery = selectExpression.PushDownSubquery();

                foreach (var projection in projections)
                {
                    var alias  = projection as AliasExpression;
                    var column = projection as ColumnExpression;

                    if (column != null)
                    {
                        column = new ColumnExpression(column.Name, column.Property, subQuery);
                        selectExpression.AddToProjection(column);
                        continue;
                    }

                    column = alias?.TryGetColumnExpression();

                    if (column != null)
                    {
                        column = new ColumnExpression(column.Name, column.Property, subQuery);
                        alias  = new AliasExpression(alias.Alias, column);
                        selectExpression.AddToProjection(alias);
                    }
                    else
                    {
                        selectExpression.AddToProjection(projection);
                    }
                }

                if (subQuery.OrderBy.Count == 0)
                {
                    subQuery.AddToOrderBy(
                        new Ordering(new SqlFunctionExpression("@@RowCount", typeof(int)), OrderingDirection.Asc));
                }

                var columnExpression = new ColumnExpression(RowNumberColumnName, typeof(int), subQuery);
                var rowNumber        = new RowNumberExpression(columnExpression, subQuery.OrderBy);

                subQuery.ClearOrderBy();
                subQuery.ClearProjection();
                subQuery.AddToProjection(rowNumber);
                subQuery.IsProjectStar = true;

                Expression predicate = null;

                var offset = subQuery.Offset ?? 0;

                if (subQuery.Offset.HasValue)
                {
                    predicate = Expression.GreaterThan(columnExpression, Expression.Constant(offset));
                }

                if (subQuery.Limit.HasValue)
                {
                    var exp = Expression.LessThanOrEqual(columnExpression, Expression.Constant(offset + subQuery.Limit.Value));
                    if (predicate != null)
                    {
                        exp = Expression.AndAlso(predicate, exp);
                    }
                    predicate = exp;
                }

                selectExpression.Predicate = predicate;

                return(selectExpression);
            }
 protected virtual Expression VisitAlias(AliasExpression node)
 {
     Visit(node.From);
     Visit(node.To);
     return(node);
 }