Esempio n. 1
0
        /// <summary>
        /// 构造最终执行的表达式。
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="translator">翻译器。</param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static Expression Build(TranslateContext transContext, Expression expression, BuildOptions options)
        {
            var builder = new ExecutionBuilder()
            {
                _transContext = transContext,
                _executor     = Expression.Parameter(typeof(IDatabase), "db"),
                _translator   = transContext.Translator
            };

            if (options.IsAsync != null)
            {
                builder._isAsync = (bool)options.IsAsync;
            }

            if (options.IsNoTracking == true)
            {
                builder._isNoTracking = true;
            }

            var newExpression = builder.Bind(expression);

            if (builder._isAsync)
            {
                var cancelToken = builder._cancelToken ?? Expression.Parameter(typeof(CancellationToken), "token");
                return(Expression.Lambda(newExpression, builder._executor, cancelToken));
            }

            return(Expression.Lambda(newExpression, builder._executor));
        }
Esempio n. 2
0
        private Expression BuildInner(Expression expression)
        {
            var eb = new ExecutionBuilder
            {
                _executor        = _executor,
                _scope           = _scope,
                _receivingMember = _receivingMember,
                _readers         = _readers,
                _lookup          = _lookup,
                _translator      = _translator,
                _variableMap     = _variableMap
            };

            return(eb.Visit(expression));
        }
        /// <summary>
        /// 获取表达式的执行计划。
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public Expression GetExecutionPlan(Expression expression, bool?isAsync = null)
        {
            try
            {
                if (expression is LambdaExpression lambda)
                {
                    expression = lambda.Body;
                }

                var options = GetTranslateOptions();

                var transContext    = new TranslateContext(ContextService, options);
                var transExpression = transContext.TranslateProvider.Translate(transContext, expression);

                var buildOptions = new ExecutionBuilder.BuildOptions {
                    IsAsync = isAsync, IsNoTracking = !options.TraceEntityState
                };
                return(ExecutionBuilder.Build(transContext, transExpression, buildOptions));
            }
            catch (Exception ex)
            {
                throw new TranslateException(expression, ex);
            }
        }