Example #1
0
        /// <summary>
        /// Optimizes the query without executing it.
        /// Used for unit tests.
        /// </summary>
        public static Expression OptimizeQuery(Expression inputExpression)
        {
            Expression partiallyEvaluatedExpression = PartialEvaluator.Eval(inputExpression, CanBeEvaluatedStatically);
            Expression expression = new ConvertToQueryAstVisitor(new QueryExecutionOptions()).Visit(partiallyEvaluatedExpression);

            return(new OptimizeQueryExpressionVisitor().Visit(expression));
        }
Example #2
0
        public override object Execute(Expression inputExpression)
        {
            Stopwatch  watch = Stopwatch.StartNew();
            Expression partiallyEvaluatedExpression = PartialEvaluator.Eval(inputExpression, CanBeEvaluatedStatically);

            QueryExecutionOptions options    = new QueryExecutionOptions();
            Expression            expression = new ConvertToQueryAstVisitor(options).Visit(partiallyEvaluatedExpression);

            // options have been initialized by ConvertToQueryAstVisitor, start logging:
            if (options.HasLoggers)
            {
                options.WriteLogLine("Input expression: " + inputExpression);
                options.WriteLogLine("Partially evaluated expression: " + partiallyEvaluatedExpression);
                options.WriteLogLine("Converted to Query AST: " + expression);
            }

            expression = new OptimizeQueryExpressionVisitor().Visit(expression);
            if (options.HasLoggers)
            {
                options.WriteLogLine("Optimized Query AST: " + expression);
                options.WriteLogLine("Query preparation time: " + watch.Elapsed);
            }

            object result;
            // If the whole query was converted, execute it:
            QueryNode query = expression as QueryNode;

            if (query != null)
            {
                result = query.Execute(this, options);
            }
            else
            {
                // Query not converted completely: we have to use a LINQ-To-Objects / LINQ-To-Profiler mix
                expression = new ExecuteAllQueriesVisitor(this, options).Visit(expression);
                if (expression.Type.IsValueType)
                {
                    expression = Expression.Convert(expression, typeof(object));
                }
                var lambdaExpression = Expression.Lambda <Func <object> >(expression);
                result = lambdaExpression.Compile()();
            }
            watch.Stop();
            options.WriteLogLine("Total query execution time: " + watch.Elapsed);
            return(result);
        }
		public override object Execute(Expression inputExpression)
		{
			Stopwatch watch = Stopwatch.StartNew();
			Expression partiallyEvaluatedExpression = PartialEvaluator.Eval(inputExpression, CanBeEvaluatedStatically);
			
			QueryExecutionOptions options = new QueryExecutionOptions();
			Expression expression = new ConvertToQueryAstVisitor(options).Visit(partiallyEvaluatedExpression);
			// options have been initialized by ConvertToQueryAstVisitor, start logging:
			if (options.HasLoggers) {
				options.WriteLogLine("Input expression: " + inputExpression);
				options.WriteLogLine("Partially evaluated expression: " + partiallyEvaluatedExpression);
				options.WriteLogLine("Converted to Query AST: " + expression);
			}
			
			expression = new OptimizeQueryExpressionVisitor().Visit(expression);
			if (options.HasLoggers) {
				options.WriteLogLine("Optimized Query AST: " + expression);
				options.WriteLogLine("Query preparation time: " + watch.Elapsed);
			}
			
			object result;
			// If the whole query was converted, execute it:
			QueryNode query = expression as QueryNode;
			if (query != null) {
				result = query.Execute(this, options);
			} else {
				// Query not converted completely: we have to use a LINQ-To-Objects / LINQ-To-Profiler mix
				expression = new ExecuteAllQueriesVisitor(this, options).Visit(expression);
				if (expression.Type.IsValueType) {
					expression = Expression.Convert(expression, typeof(object));
				}
				var lambdaExpression = Expression.Lambda<Func<object>>(expression);
				result = lambdaExpression.Compile()();
			}
			watch.Stop();
			options.WriteLogLine("Total query execution time: " + watch.Elapsed);
			return result;
		}
		/// <summary>
		/// Optimizes the query without executing it.
		/// Used for unit tests.
		/// </summary>
		public static Expression OptimizeQuery(Expression inputExpression)
		{
			Expression partiallyEvaluatedExpression = PartialEvaluator.Eval(inputExpression, CanBeEvaluatedStatically);
			Expression expression = new ConvertToQueryAstVisitor(new QueryExecutionOptions()).Visit(partiallyEvaluatedExpression);
			return new OptimizeQueryExpressionVisitor().Visit(expression);
		}