Exemplo n.º 1
0
        public void TestReturnedOperations()
        {
            ProjectionExitOperationFactory factory = ProjectionExitOperationFactory.GetFactory();
            var sessionFactory = Mock <ISessionFactoryImplementor>();

            using (Mocks.Record())
            {
            }
            using (Mocks.Playback())
            {
                Assert.IsInstanceOf(typeof(RowCountExitOperation),
                                    factory.GetProjectionExitOperation(Projections.RowCount(), sessionFactory));
                Assert.IsInstanceOf(typeof(AggregateExitOperation),
                                    factory.GetProjectionExitOperation(Projections.Max("foo"), sessionFactory));
                Assert.IsInstanceOf(typeof(AggregateExitOperation),
                                    factory.GetProjectionExitOperation(Projections.Min("foo"), sessionFactory));
                Assert.IsInstanceOf(typeof(AggregateExitOperation),
                                    factory.GetProjectionExitOperation(Projections.Sum("foo"), sessionFactory));
                try
                {
                    factory.GetProjectionExitOperation(Projections.Avg("foo"), sessionFactory);
                    Assert.Fail("example of one that we don't yet support");
                }
                catch (ArgumentException e)
                {
                    // good
                }
            }
        }
        public IList Apply(IList result)
        {
            /**
             * Herein lies the glory
             *
             * hibernate has done as much as it can, we're going to have to deal with
             * the rest in memory.
             *
             * The heirarchy of operations is this so far:
             * Distinct
             * Order
             * FirstResult
             * MaxResult
             * RowCount
             * Average
             * Min/Max/Sum
             */

            // ordering of the following operations *really* matters!
            if (distinct != null)
            {
                result = new DistinctExitOperation(distinct).Apply(result);
            }
            foreach (Order order in orders)
            {
                result = new OrderExitOperation(order).Apply(result);
            }
            if (firstResult != null)
            {
                result = new FirstResultExitOperation((int)firstResult).Apply(result);
            }
            if (maxResults != null)
            {
                result = new MaxResultsExitOperation((int)maxResults).Apply(result);
            }
            ProjectionExitOperationFactory factory = ProjectionExitOperationFactory.GetFactory();

            if (rowCountProjection != null)
            {
                result = factory.GetProjectionExitOperation(rowCountProjection, sessionFactoryImplementor).Apply(result);
            }
            if (avgProjection != null)
            {
                result = new AvgResultsExitOperation().Apply(result);
            }
            // min, max, sum
            if (aggregateProjection != null)
            {
                result = factory.GetProjectionExitOperation(aggregateProjection, sessionFactoryImplementor).Apply(result);
            }

            return(result);
        }