Exemplo n.º 1
0
    public static List <AggregateExpression>?GetAggregates(ReadOnlyCollection <ColumnDeclaration> columns)
    {
        AggregateFinder ap = new AggregateFinder();

        Visit(columns, ap.VisitColumnDeclaration);
        return(ap.aggregates);
    }
Exemplo n.º 2
0
        public async Task find_when_stream_is_not_new_and_it_is_not_in_database_async()
        {
            var id = Guid.NewGuid();

            var finder = new AggregateFinder <QuestParty>();

            (await finder.FindAsync(new EventStream(id, false), theSession, new CancellationToken()))
            .ShouldNotBeNull();
        }
Exemplo n.º 3
0
        public async Task find_when_stream_is_new_async()
        {
            var finder = new AggregateFinder <QuestParty>();

            var id = Guid.NewGuid();

            (await finder.FindAsync(StreamAction.Start(id, new AEvent()), theSession, new CancellationToken()))
            .ShouldNotBeNull();
        }
Exemplo n.º 4
0
        public Aggregator <T> AggregateStreamsInlineWith <T>() where T : class, new()
        {
            var aggregator = AggregateFor <T>();
            var finder     = new AggregateFinder <T>();
            var projection = new AggregationProjection <T>(finder, aggregator);

            Inlines.Add(projection);

            return(aggregator);
        }
Exemplo n.º 5
0
        public void find_when_stream_is_not_new_and_it_is_not_in_database()
        {
            var session = Substitute.For <IDocumentSession>();
            var id      = Guid.NewGuid();

            var finder = new AggregateFinder <QuestParty>();

            finder.Find(new EventStream(id, false), session)
            .ShouldNotBeNull();
        }
Exemplo n.º 6
0
        public void find_when_stream_is_new()
        {
            var session = Substitute.For <IDocumentSession>();

            var finder = new AggregateFinder <QuestParty>();

            var id = Guid.NewGuid();

            finder.Find(new EventStream(id, true), session)
            .ShouldNotBeNull();

            session.DidNotReceive().Load <QuestParty>(id);
        }
Exemplo n.º 7
0
        public async Task find_when_stream_is_not_new_async()
        {
            var id = Guid.NewGuid();

            var persisted = new QuestParty {
                Id = id
            };

            theSession.Store(persisted);
            theSession.SaveChanges();

            var finder = new AggregateFinder <QuestParty>();

            (await finder.FindAsync(new EventStream(id, false), theSession, new CancellationToken()))
            .ShouldBeTheSameAs(persisted);
        }
Exemplo n.º 8
0
        public void find_when_stream_is_not_new()
        {
            var session = Substitute.For <IDocumentSession>();
            var id      = Guid.NewGuid();

            var persisted = new QuestParty {
                Id = id
            };

            var finder = new AggregateFinder <QuestParty>();

            session.Load <QuestParty>(id).Returns(persisted);

            finder.Find(new EventStream(id, false), session)
            .ShouldBeTheSameAs(persisted);
        }
Exemplo n.º 9
0
    protected internal override Expression VisitSelect(SelectExpression select)
    {
        bool isOuterMost = select == outerMostSelect;

        if (select.IsOrderAlsoByKeys || select.HasIndex || select.Top != null && hasProjectionInProjector)
        {
            if (gatheredKeys == null)
            {
                gatheredKeys = new List <ColumnExpression>();
            }
        }

        List <ColumnExpression>?savedKeys = null;

        if (gatheredKeys != null && (select.IsDistinct || select.GroupBy.HasItems() || select.IsAllAggregates))
        {
            savedKeys = gatheredKeys.ToList();
        }

        if ((AggregateFinder.GetAggregates(select.Columns)?.Any(a => a.AggregateFunction.OrderMatters()) ?? false) && select.From is SelectExpression from)
        {
            var oldOuterMostSelect = outerMostSelect;
            outerMostSelect = from;

            select = (SelectExpression)base.VisitSelect(select);

            outerMostSelect = oldOuterMostSelect;
        }
        else
        {
            select = (SelectExpression)base.VisitSelect(select);
        }


        if (savedKeys != null)
        {
            gatheredKeys = savedKeys;
        }

        List <ColumnDeclaration>?newColumns = null;

        if (select.GroupBy.HasItems())
        {
            gatheredOrderings = null;

            if (gatheredKeys != null)
            {
                ColumnGenerator cg = new ColumnGenerator(select.Columns);

                var newKeys = new List <ColumnDeclaration>();

                foreach (var ge in select.GroupBy)
                {
                    var cd = cg.Columns.NotNull().FirstOrDefault(a => DbExpressionComparer.AreEqual(a.Expression, ge));

                    if (cd != null)
                    {
                        newKeys.Add(cd);
                    }
                    else
                    {
                        newKeys.Add(cg.NewColumn(ge));
                    }
                }

                if (cg.Columns.Count() != select.Columns.Count)
                {
                    newColumns = cg.Columns.NotNull().ToList();
                }

                gatheredKeys.AddRange(newKeys.Select(cd => new ColumnExpression(cd.Expression.Type, select.Alias, cd.Name)));
            }
        }

        if (select.IsAllAggregates)
        {
            if (gatheredKeys != null)
            {
                gatheredKeys.AddRange(select.Columns.Select(cd => new ColumnExpression(cd.Expression.Type, select.Alias, cd.Name)));
            }
        }

        if (select.IsDistinct)
        {
            gatheredOrderings = null;

            if (gatheredKeys != null)
            {
                gatheredKeys.AddRange(select.Columns.Select(cd => cd.GetReference(select.Alias)));
            }
        }

        if (select.IsReverse && !gatheredOrderings.IsNullOrEmpty())
        {
            gatheredOrderings = gatheredOrderings.Select(o => new OrderExpression(
                                                             o.OrderType == OrderType.Ascending ? OrderType.Descending : OrderType.Ascending,
                                                             o.Expression)).ToReadOnly();
        }

        if (select.OrderBy.Count > 0)
        {
            this.PrependOrderings(select.OrderBy);
        }

        ReadOnlyCollection <OrderExpression>?orderings = null;

        if (isOuterMost && !IsCountSumOrAvg(select) || select.Top != null)
        {
            AppendKeys();

            orderings         = gatheredOrderings;
            gatheredOrderings = null;
        }

        if (AreEqual(select.OrderBy, orderings) && !select.IsReverse && newColumns == null)
        {
            return(select);
        }

        return(new SelectExpression(select.Alias, select.IsDistinct, select.Top, (IEnumerable <ColumnDeclaration>?)newColumns ?? select.Columns,
                                    select.From, select.Where, orderings, select.GroupBy, select.SelectOptions & ~SelectOptions.Reverse));
    }
Exemplo n.º 10
0
        private PrivateExecuteResult <T> PrivateExecute <T>(Expression expression)
        {
            var projectionExpression = expression as SqlProjectionExpression;

            if (projectionExpression == null)
            {
                expression = Evaluator.PartialEval(expression);

                if (this.RelatedDataAccessObjectContext == null)
                {
                    expression = QueryBinder.Bind(this.DataAccessModel, expression, null, null);
                }
                else
                {
                    expression = QueryBinder.Bind(this.DataAccessModel, expression, this.RelatedDataAccessObjectContext.ElementType, this.RelatedDataAccessObjectContext.ExtraCondition);
                }

                projectionExpression = (SqlProjectionExpression)Optimize(expression, this.SqlDatabaseContext.SqlDataTypeProvider.GetTypeForEnums(), true);
            }

            ProjectorCacheInfo cacheInfo;

            var columns = projectionExpression.Select.Columns.Select(c => c.Name);

            var formatResult = this.SqlDatabaseContext.SqlQueryFormatterManager.Format(projectionExpression, SqlQueryFormatterOptions.Default);

            var placeholderValues = PlaceholderValuesCollector.CollectValues(expression);

            var key = new ProjectorCacheKey(projectionExpression, this.SqlDatabaseContext);

            var projectorCache = this.SqlDatabaseContext.projectorCache;

            if (!projectorCache.TryGetValue(key, out cacheInfo))
            {
                var projectionLambda = ProjectionBuilder.Build(this.DataAccessModel, this.SqlDatabaseContext, projectionExpression.Projector, columns);

                cacheInfo.elementType = projectionLambda.Body.Type;
                cacheInfo.projector   = projectionLambda.Compile();

                var aggregates = AggregateFinder.Find(projectionExpression);

                if (aggregates.Count == 1)
                {
                    cacheInfo.sqlAggregateType = aggregates.First().AggregateType;
                }

                var newCache = new Dictionary <ProjectorCacheKey, ProjectorCacheInfo>(projectorCache, ProjectorCacheEqualityComparer.Default);

                if (!projectorCache.ContainsKey(key))
                {
                    newCache[key] = cacheInfo;
                }

                this.SqlDatabaseContext.projectorCache = newCache;
            }

            var elementType         = TypeHelper.GetElementType(cacheInfo.elementType);
            var concreteElementType = elementType;

            if (elementType.IsDataAccessObjectType())
            {
                Type type;
                TypeHelper.GetElementType(cacheInfo.elementType);
                elementType         = this.DataAccessModel.GetDefinitionTypeFromConcreteType(elementType);
                concreteElementType = this.DataAccessModel.GetConcreteTypeFromDefinitionType(elementType);

                if (this.RelatedDataAccessObjectContext == null)
                {
                    type = typeof(DataAccessObjectProjector <,>);
                }
                else
                {
                    type = typeof(RelatedDataAccessObjectProjector <,>);
                }

                return(new PrivateExecuteResult <T>
                       (
                           (IEnumerable <T>)Activator.CreateInstance
                           (
                               type.MakeGenericType(elementType, concreteElementType),
                               this,
                               this.DataAccessModel,
                               formatResult,
                               this.SqlDatabaseContext,
                               cacheInfo.projector,
                               this.RelatedDataAccessObjectContext,
                               projectionExpression.SelectFirstType,
                               cacheInfo.sqlAggregateType,
                               projectionExpression.IsDefaultIfEmpty,
                               placeholderValues
                           ),
                           projectionExpression.SelectFirstType,
                           cacheInfo.sqlAggregateType,
                           projectionExpression.IsDefaultIfEmpty,
                           projectionExpression.DefaultValueExpression
                       ));
            }
            else
            {
                return(new PrivateExecuteResult <T>
                       (
                           (IEnumerable <T>)Activator.CreateInstance
                           (
                               typeof(ObjectProjector <,>).MakeGenericType(elementType, concreteElementType),
                               this,
                               this.DataAccessModel,
                               formatResult,
                               this.SqlDatabaseContext,
                               cacheInfo.projector,
                               this.RelatedDataAccessObjectContext,
                               projectionExpression.SelectFirstType,
                               cacheInfo.sqlAggregateType,
                               projectionExpression.IsDefaultIfEmpty,
                               placeholderValues
                           ),
                           projectionExpression.SelectFirstType,
                           cacheInfo.sqlAggregateType,
                           projectionExpression.IsDefaultIfEmpty,
                           projectionExpression.DefaultValueExpression
                       ));
            }
        }