Beispiel #1
0
        private IAsyncEnumerable <TResult> GetFromCache <TResult>(OeQueryContext queryContext, DbContext dbContext, out MethodCallExpression?countExpression)
        {
            Cache.OeCacheContext   cacheContext   = queryContext.CreateCacheContext();
            Cache.OeQueryCacheItem?queryCacheItem = base.QueryCache.GetQuery(cacheContext);

            Func <QueryContext, IAsyncEnumerable <TResult> >   queryExecutor;
            IReadOnlyList <Cache.OeQueryCacheDbParameterValue> parameterValues;

            if (queryCacheItem == null)
            {
                var        parameterVisitor = new OeConstantToParameterVisitor();
                Expression expression       = queryContext.CreateExpression(parameterVisitor);
                expression = TranslateExpression(queryContext.EdmModel, expression);
                expression = queryContext.TranslateSource(dbContext, expression);
                expression = OeEnumerableToQuerableVisitor.Translate(expression);

                queryExecutor = dbContext.CreateAsyncQueryExecutor <TResult>(expression);
                if (queryContext.EntryFactory == null)
                {
                    countExpression = null;
                }
                else
                {
                    countExpression = queryContext.CreateCountExpression(expression);
                    countExpression = (MethodCallExpression)OeEnumerableToQuerableVisitor.Translate(countExpression);
                    countExpression = (MethodCallExpression)TranslateExpression(queryContext.EdmModel, countExpression);
                }

                cacheContext = queryContext.CreateCacheContext(parameterVisitor.ConstantToParameterMapper);
                base.QueryCache.AddQuery(cacheContext, queryExecutor, countExpression, queryContext.EntryFactory);
                parameterValues = parameterVisitor.ParameterValues;
            }
            else
            {
                queryExecutor             = (Func <QueryContext, IAsyncEnumerable <TResult> >)queryCacheItem.Query;
                queryContext.EntryFactory = queryCacheItem.EntryFactory;
                countExpression           = queryCacheItem.CountExpression;
                parameterValues           = cacheContext.ParameterValues;
            }

            var          queryContextFactory = dbContext.GetService <IQueryContextFactory>();
            QueryContext efQueryContext      = queryContextFactory.Create();

            foreach (Cache.OeQueryCacheDbParameterValue parameterValue in parameterValues)
            {
                efQueryContext.AddParameter(parameterValue.ParameterName, parameterValue.ParameterValue);
            }

            if (queryContext.IsQueryCount() && countExpression != null)
            {
                countExpression = (MethodCallExpression)queryContext.TranslateSource(dbContext, countExpression);
                countExpression = (MethodCallExpression) new OeParameterToVariableVisitor().Translate(countExpression, parameterValues);
            }
            else
            {
                countExpression = null;
            }

            return(queryExecutor(efQueryContext));
        }
        private static IAsyncEnumerable <TResult> GetFromCache <TResult>(OeQueryContext queryContext, T dbContext, Cache.OeQueryCache queryCache,
                                                                         out MethodCallExpression countExpression)
        {
            countExpression = null;
            Cache.OeCacheContext   cacheContext   = queryContext.CreateCacheContext();
            Cache.OeQueryCacheItem queryCacheItem = queryCache.GetQuery(cacheContext);

            Func <QueryContext, IAsyncEnumerable <TResult> >   queryExecutor;
            IReadOnlyList <Cache.OeQueryCacheDbParameterValue> parameterValues;
            IQueryable query = queryContext.EntitySetAdapter.GetEntitySet(dbContext);

            if (queryCacheItem == null)
            {
                var        parameterVisitor = new OeConstantToParameterVisitor();
                Expression expression       = queryContext.CreateExpression(parameterVisitor);
                expression = queryContext.TranslateSource(dbContext, expression);

                queryExecutor   = dbContext.CreateAsyncQueryExecutor <TResult>(expression);
                countExpression = OeQueryContext.CreateCountExpression(expression);
                queryCache.AddQuery(queryContext.CreateCacheContext(parameterVisitor.ConstantToParameterMapper), queryExecutor, countExpression,
                                    queryContext.EntryFactory, queryContext.SkipTokenAccessors);
                parameterValues = parameterVisitor.ParameterValues;
            }
            else
            {
                queryExecutor                   = (Func <QueryContext, IAsyncEnumerable <TResult> >)queryCacheItem.Query;
                queryContext.EntryFactory       = queryCacheItem.EntryFactory;
                queryContext.SkipTokenAccessors = queryCacheItem.SkipTokenAccessors;
                countExpression                 = queryCacheItem.CountExpression;
                parameterValues                 = cacheContext.ParameterValues;
            }

            var          queryContextFactory = dbContext.GetService <IQueryContextFactory>();
            QueryContext efQueryContext      = queryContextFactory.Create();

            foreach (Cache.OeQueryCacheDbParameterValue parameterValue in parameterValues)
            {
                efQueryContext.AddParameter(parameterValue.ParameterName, parameterValue.ParameterValue);
            }

            if (queryContext.ODataUri.QueryCount.GetValueOrDefault())
            {
                countExpression = (MethodCallExpression)queryContext.TranslateSource(dbContext, countExpression);
                countExpression = (MethodCallExpression) new OeParameterToVariableVisitor().Translate(countExpression, parameterValues);
            }
            else
            {
                countExpression = null;
            }

            return(queryExecutor(efQueryContext));
        }
        private Expression TryExtractParameter(Expression expression)
        {
            try
            {
                string parameterName;

                var parameterValue = Evaluate(expression, out parameterName);

                var parameterExpression = parameterValue as Expression;

                if (parameterExpression != null)
                {
                    return(parameterExpression);
                }

                if (parameterName == null)
                {
                    parameterName = "p";
                }

                var compilerPrefixIndex
                    = parameterName.LastIndexOf(">", StringComparison.Ordinal);

                if (compilerPrefixIndex != -1)
                {
                    parameterName = parameterName.Substring(compilerPrefixIndex + 1);
                }

                parameterName
                    = CompiledQueryCache.CompiledQueryParameterPrefix
                      + parameterName
                      + "_"
                      + _queryContext.ParameterValues.Count;

                _queryContext.AddParameter(parameterName, parameterValue);

                return(Expression.Parameter(expression.Type, parameterName));
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException(
                          _logger.LogSensitiveData
                        ? CoreStrings.ExpressionParameterizationExceptionSensitive(expression)
                        : CoreStrings.ExpressionParameterizationException,
                          exception);
            }
        }
Beispiel #4
0
        private Expression TryExtractParameter(Expression expression)
        {
            string parameterName;

            var parameterValue = Evaluate(expression, out parameterName);

            var parameterExpression = parameterValue as Expression;

            if (parameterExpression != null)
            {
                return(parameterExpression);
            }

            if (!_parameterize)
            {
                return(Expression.Constant(parameterValue));
            }

            if (parameterName == null)
            {
                parameterName = "p";
            }

            var compilerPrefixIndex
                = parameterName.LastIndexOf(">", StringComparison.Ordinal);

            if (compilerPrefixIndex != -1)
            {
                parameterName = parameterName.Substring(compilerPrefixIndex + 1);
            }

            parameterName
                = CompiledQueryCache.CompiledQueryParameterPrefix
                  + parameterName
                  + "_"
                  + _queryContext.ParameterValues.Count;

            _queryContext.AddParameter(parameterName, parameterValue);

            return(Expression.Parameter(expression.Type, parameterName));
        }
Beispiel #5
0
        private static IAsyncEnumerable <TResult> GetQueryExecutor <TResult>(OeQueryContext queryContext, T dbContext)
        {
            IQueryable query = queryContext.EntitySetAdapter.GetEntitySet(dbContext);

            var        parameterVisitor = new OeConstantToParameterVisitor(queryContext.SkipTokenParser != null);
            Expression expression       = queryContext.CreateExpression(query, parameterVisitor);
            Func <QueryContext, IAsyncEnumerable <TResult> > queryExecutor = dbContext.CreateAsyncQueryExecutor <TResult>(expression);

            var          queryContextFactory = dbContext.GetService <IQueryContextFactory>();
            QueryContext efQueryContext      = queryContextFactory.Create();

            foreach (Db.OeQueryCacheDbParameterValue parameterValue in parameterVisitor.ParameterValues)
            {
                efQueryContext.AddParameter(parameterValue.ParameterName, parameterValue.ParameterValue);
            }

            if (queryContext.ODataUri.QueryCount.GetValueOrDefault())
            {
                Expression countExpression = queryContext.CreateCountExpression(query, expression);
                queryContext.CountExpression = new OeParameterToVariableVisitor().Translate(countExpression, parameterVisitor.ParameterValues);
            }

            return(queryExecutor(efQueryContext));
        }