private CachedQuery buildCachedQuery <TDoc, TOut>(Type queryType, ICompiledQuery <TDoc, TOut> query)
        {
            Expression expression = query.QueryIs();
            var        invocation = Expression.Invoke(expression, Expression.Parameter(typeof(IMartenQueryable <TDoc>)));

            var queryableDocument = _store.Tenancy.Default.MappingFor(typeof(TDoc)).ToQueryableDocument();

            var setters = findSetters(queryableDocument, queryType, expression, _store.Serializer);

            var model = MartenQueryParser.TransformQueryFlyweight.GetParsedQuery(invocation);

            validateCompiledQuery(model);

            // TODO -- move this outside of this call
            _store.Tenancy.Default.EnsureStorageExists(typeof(TDoc));

            var includeJoins = new IIncludeJoin[0];

            if (model.HasOperator <IncludeResultOperator>())
            {
                var builder = new CompiledIncludeJoinBuilder <TDoc, TOut>(_store.Storage);
                includeJoins = builder.BuildIncludeJoins(model, query);
            }

            // Hokey. Need a non-null stats to trigger LinqQuery into "knowing" that it needs
            // to create a StatsSelector decorator
            var stats = model.HasOperator <StatsResultOperator>() ? new QueryStatistics() : null;

            var handler = _store.HandlerFactory.BuildHandler <TOut>(model, includeJoins, stats);

            var cmd = CommandBuilder.ToCommand(_store.Tenancy.Default, handler);

            for (int i = 0; i < setters.Count && i < cmd.Parameters.Count; i++)
            {
                setters[i].ReplaceValue(cmd.Parameters[i]);
            }

            var cachedQuery = new CachedQuery
            {
                Command          = cmd,
                ParameterSetters = setters,
                Handler          = handler,
            };

            if (model.HasOperator <StatsResultOperator>())
            {
                var prop = queryType.GetProperties().FirstOrDefault(x => x.PropertyType == typeof(QueryStatistics));
                if (prop != null)
                {
                    cachedQuery.StatisticsFinder =
                        typeof(QueryStatisticsFinder <>).CloseAndBuildAs <IQueryStatisticsFinder>(prop, queryType);
                }
            }

            return(cachedQuery);
        }
Exemple #2
0
        private CachedQuery buildCachedQuery <TDoc, TOut>(Type queryType, ICompiledQuery <TDoc, TOut> query)
        {
            Expression expression = query.QueryIs();
            var        invocation = Expression.Invoke(expression, Expression.Parameter(typeof(IMartenQueryable <TDoc>)));

            var setters = findSetters(_schema.MappingFor(typeof(TDoc)).ToQueryableDocument(), queryType, expression, _serializer.EnumStorage);

            var model = MartenQueryParser.TransformQueryFlyweight.GetParsedQuery(invocation);

            _schema.EnsureStorageExists(typeof(TDoc));

            var includeJoins = new IIncludeJoin[0];

            if (model.HasOperator <IncludeResultOperator>())
            {
                var builder = new CompiledIncludeJoinBuilder <TDoc, TOut>(_schema);
                includeJoins = builder.BuildIncludeJoins(model, query);
            }

            if (model.HasOperator <StatsResultOperator>())
            {
                SetStats(query, model);
            }

            var handler = _schema.HandlerFactory.BuildHandler <TOut>(model, includeJoins, Stats);
            var cmd     = new NpgsqlCommand();

            handler.ConfigureCommand(cmd);

            return(new CachedQuery
            {
                Command = cmd,
                ParameterSetters = setters,
                Handler = handler
            });
        }
Exemple #3
0
 public void AddInclude(IIncludeJoin include)
 {
     _includes.Add(include);
 }