Ejemplo n.º 1
0
        private static Func <QueryContext, TResult> CompileQueryCore <TResult>(
            Expression query,
            IModel model,
            IQueryModelGenerator queryModelGenerator,
            IDatabase database,
            IDiagnosticsLogger <DbLoggerCategory.Query> logger,
            Type contextType)
        {
            query = ExpandNavigations(query, model);

            var queryModel = queryModelGenerator.ParseQuery(query);

            // this is temporary, until relinq is removed
            var tirev = new TransparentIdentifierRemovingVisitor();

            queryModel.TransformExpressions(tirev.Visit);

            var atasev = new AnonymousObjectAccessSimplifyingVisitor();

            queryModel.TransformExpressions(atasev.Visit);

            var resultItemType
                = (queryModel.GetOutputDataInfo()
                   as StreamedSequenceInfo)?.ResultItemType
                  ?? typeof(TResult);

            if (resultItemType == typeof(TResult))
            {
                var compiledQuery = database.CompileQuery <TResult>(queryModel);

                return(qc =>
                {
                    try
                    {
                        return compiledQuery(qc).First();
                    }
                    catch (Exception exception)
                    {
                        logger.QueryIterationFailed(contextType, exception);

                        throw;
                    }
                });
            }

            try
            {
                return((Func <QueryContext, TResult>)CompileQueryMethod
                       .MakeGenericMethod(resultItemType)
                       .Invoke(database, new object[] { queryModel }));
            }
            catch (TargetInvocationException e)
            {
                ExceptionDispatchInfo.Capture(e.InnerException).Throw();

                throw;
            }
        }
Ejemplo n.º 2
0
        private static Func <QueryContext, TResult> CompileQueryCore <TResult>(
            Expression query,
            IQueryModelGenerator queryModelGenerator,
            IDatabase database,
            IDiagnosticsLogger <DbLoggerCategory.Query> logger,
            Type contextType)
        {
            var queryModel = queryModelGenerator.ParseQuery(query);

            var resultItemType
                = (queryModel.GetOutputDataInfo()
                   as StreamedSequenceInfo)?.ResultItemType
                  ?? typeof(TResult);

            if (resultItemType == typeof(TResult))
            {
                var compiledQuery = database.CompileQuery <TResult>(queryModel);

                return(qc =>
                {
                    try
                    {
                        return compiledQuery(qc).First();
                    }
                    catch (Exception exception)
                    {
                        logger.QueryIterationFailed(contextType, exception);

                        throw;
                    }
                });
            }

            try
            {
                return((Func <QueryContext, TResult>)CompileQueryMethod
                       .MakeGenericMethod(resultItemType)
                       .Invoke(database, new object[] { queryModel }));
            }
            catch (TargetInvocationException e)
            {
                ExceptionDispatchInfo.Capture(e.InnerException).Throw();

                throw;
            }
        }
        private static Func <QueryContext, TResult> CompileQueryCore <TResult>(
            Expression query,
            IQueryModelGenerator queryModelGenerator,
            IDatabase database,
            IDiagnosticsLogger <DbLoggerCategory.Query> logger,
            Type contextType,
            bool getRealResult = false)
        {
            var queryModel = queryModelGenerator.ParseQuery(query);

            var resultItemType
                = (queryModel.GetOutputDataInfo()
                   as StreamedSequenceInfo)?.ResultItemType
                  ?? typeof(TResult);

            if (resultItemType == typeof(TResult))
            {
                var compiledQuery = database.CompileQuery <TResult>(queryModel);

                return(qc =>
                {
                    try
                    {
                        return compiledQuery(qc).First();
                    }
                    catch (Exception exception)
                    {
                        logger.QueryIterationFailed(contextType, exception);

                        throw;
                    }
                });
            }

            try
            {
                // differs from base implmentation to return DB query result
                var compileFunction = (Func <QueryContext, TResult>)CompileQueryMethod
                                      .MakeGenericMethod(resultItemType)
                                      .Invoke(database, new object[] { queryModel });

                return(qc =>
                {
                    try
                    {
                        // calling ToList to materialize result
                        var genericToListMethod = ToListMethod.MakeGenericMethod(new Type[] { resultItemType });
                        var result = genericToListMethod.Invoke(compileFunction(qc), new object[] { compileFunction(qc) });

                        return (TResult)result;
                    }
                    catch (Exception exception)
                    {
                        logger.QueryIterationFailed(contextType, exception);

                        throw;
                    }
                });
            }
            catch (TargetInvocationException e)
            {
                ExceptionDispatchInfo.Capture(e.InnerException).Throw();

                throw;
            }
        }