/// <summary>
        ///     Initializes a new instance of the <see cref="FilterableQueryBase" /> class.
        /// </summary>
        /// <param name="alias">
        ///     The alias.
        /// </param>
        /// <param name="query">
        ///     The query.
        /// </param>
        protected FilterableQueryBase
        (
            string alias           = null,
            IFilterableQuery query = null
        )
        {
            if (query != null)
            {
                Aliases    = query.Aliases.ToDictionary(x => x.Key, x => x.Value);
                Criterions = query.Criterions.ToList();
                Joins      = query.Joins.ToList();
            }
            else
            {
                Aliases    = new Dictionary <string, string>();
                Criterions = new List <ICriterion>();
                Joins      = new List <Join>();

                if (alias != null)
                {
                    Aliases.Add("entity.root.alias", alias);
                }
            }

            Alias = alias;

            Data = new QueryHelperData(Aliases, Joins);
        }
        /// <summary>
        ///     Adds the specified property to the projection list.
        /// </summary>
        /// <param name="list">
        ///     The projection list.
        /// </param>
        /// <param name="property">
        ///     The property.
        /// </param>
        /// <param name="data">
        ///     A <see cref="QueryHelperData" /> instance.
        /// </param>
        /// <typeparam name="TSource">
        ///     The <see cref="System.Type" /> of the source entity.
        /// </typeparam>
        /// <typeparam name="TProperty">
        ///     The <see cref="System.Type" /> of the property.
        /// </typeparam>
        /// <returns>
        ///     The <see cref="ProjectionList" /> instance.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="list" /> is null.
        /// </exception>
        public static ProjectionList AddProperty <TSource, TProperty>
        (
            this ProjectionList list,
            Expression <Func <TSource, TProperty> > property,
            QueryHelperData data
        )
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            IProjection projection = ProjectionHelper.GetProjection(property, data);

            string alias = null;

            var propertyProjection = projection as IPropertyProjection;

            if (propertyProjection != null)
            {
                alias = propertyProjection.PropertyName;
            }

            list.Add(projection, alias);

            return(list);
        }
        public void Given_NullAsProjection_When_AskedToTransformOrders_OrderHelperThrowsArgNullException()
        {
            var statements = new OrderByStatement[0];
            var data       = new QueryHelperData(new Dictionary <string, string>(), new List <Join>());

            Assert.That(() => OrderHelper.GetSourceBasedOrdersFrom(statements, null, data), Throws.InstanceOf <ArgumentNullException>());
        }
        public void Given_SubQueryWithModelBasedOrders_When_UsingUglyHack_Then_IsSortedByAppropriateSourceProjection()
        {
            IDetachedFlowQuery <UserEntity> subquery = Query <UserEntity>()
                                                       .OrderBy <UserDto>(x => x.SomeValue)
                                                       .Detached()
                                                       .Limit(2, 1)
                                                       .Select(x => x.Id);

            var subqueryAsFlowQuery = subquery as IFlowQuery;

            Assert.That(subqueryAsFlowQuery != null);

            OrderByStatement[] orders = subqueryAsFlowQuery.Orders.ToArray();
            QueryHelperData    data   = subqueryAsFlowQuery.Data;

            subquery.ClearOrders();

            IEnumerable <OrderByStatement> newOrders = OrderHelper.GetSourceBasedOrdersFrom(orders, Projection, data);

            subqueryAsFlowQuery.Orders.AddRange(newOrders);

            FlowQuerySelection <UserDto> users = Query <UserEntity>()
                                                 .Where(x => x.Id, NHibernate.FlowQuery.Is.In(subquery))
                                                 .Select(Projection);

            Assert.That(users.Count(), Is.EqualTo(2));
            Assert.That(users.ElementAt(0).SomeValue, Is.EqualTo("Lars Wilk"));
            Assert.That(users.ElementAt(1).SomeValue, Is.EqualTo("Lotta Brak"));
        }
Example #5
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="SelectSetup{TSource,TDestination}" /> class.
        /// </summary>
        /// <param name="selectionBuilder">
        ///     The selection builder.
        /// </param>
        /// <param name="data">
        ///     The <see cref="QueryHelperData" /> instance.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="selectionBuilder" /> is null.
        /// </exception>
        public SelectSetup(SelectionBuilder <TSource, TDestination> selectionBuilder, QueryHelperData data)
        {
            if (selectionBuilder == null)
            {
                throw new ArgumentNullException("selectionBuilder");
            }

            Data = data;

            Mappings = new Dictionary <string, IProjection>();

            ProjectionList = Projections.ProjectionList();

            SelectionBuilder = selectionBuilder;
        }
        public void Given_AllSourceBasedListOfOrderByStatements_When_AskedToTransformOrders_OrderHelperReturnsGivenReference()
        {
            var data = new QueryHelperData(new Dictionary <string, string>(), new List <Join>());

            var orders = new[]
            {
                new OrderByStatement
                {
                    IsBasedOnSource = true
                }
            };

            var newOrders = OrderHelper.GetSourceBasedOrdersFrom(orders, Projection, data);

            Assert.That(newOrders, Is.SameAs(orders));
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="SelectSetupPart{TSource,TDestination}" /> class.
        /// </summary>
        /// <param name="forProperty">
        ///     The property name.
        /// </param>
        /// <param name="setup">
        ///     The <see cref="ISelectSetup{TSource, TDestination}" /> instance.
        /// </param>
        /// <param name="data">
        ///     The <see cref="QueryHelperData" /> instance.
        /// </param>
        /// <exception cref="ArgumentException">
        ///     <paramref name="forProperty" /> is null or <see cref="string.Empty" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="setup" /> is null.
        /// </exception>
        public SelectSetupPart(string forProperty, ISelectSetup <TSource, TDestination> setup, QueryHelperData data)
        {
            if (string.IsNullOrEmpty(forProperty))
            {
                throw new ArgumentException("forProperty");
            }

            if (setup == null)
            {
                throw new ArgumentNullException("setup");
            }

            Data = data;

            ForProperty = forProperty;

            Setup = setup;
        }
        /// <summary>
        ///     Adds the specified properties to the projection list.
        /// </summary>
        /// <param name="list">
        ///     The projection list.
        /// </param>
        /// <param name="data">
        ///     A <see cref="QueryHelperData" /> instance.
        /// </param>
        /// <param name="properties">
        ///     The property expressions.
        /// </param>
        /// <typeparam name="TSource">
        ///     The <see cref="System.Type" /> of the source entity.
        /// </typeparam>
        /// <returns>
        ///     The <see cref="ProjectionList" /> instance.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="list" /> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="properties" /> contain null-entries.
        /// </exception>
        public static ProjectionList AddProperties <TSource>
        (
            this ProjectionList list,
            QueryHelperData data,
            params Expression <Func <TSource, object> >[] properties
        )
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            foreach (Expression <Func <TSource, object> > property in properties)
            {
                if (property == null)
                {
                    throw new ArgumentException("Properties contains null value", "properties");
                }

                list.AddProperty(property, data);
            }

            return(list);
        }
        public void Given_MixedListOfOrderByStatements_When_AskedToTransformOrders_OrderHelperReturnsAlreadySourceBasedOrdersAndInCorrectOrder()
        {
            var data = new QueryHelperData(new Dictionary <string, string>(), new List <Join>());

            var orders = new[]
            {
                new OrderByStatement
                {
                    IsBasedOnSource = true
                },

                new OrderByStatement
                {
                    IsBasedOnSource = false,
                    Property        = "SomeValue"
                }
            };

            var newOrders = OrderHelper.GetSourceBasedOrdersFrom(orders, Projection, data);

            Assert.That(newOrders.Count(), Is.EqualTo(2));
            Assert.That(newOrders.ElementAt(0).Order, Is.Null);
            Assert.That(newOrders.ElementAt(1).Order, Is.Not.Null);
        }
        public void GetProjectionValueCriterionThrowsIfNodeTypeIsInvalid()
        {
            var data = new QueryHelperData(new Dictionary <string, string>(), new List <Join>());

            var context = new HelperContext(data, (string)null, HelperType.Filter);

            Assert
            .That
            (
                () =>
            {
                RestrictionHelper
                .GetProjectionValueCriterion
                (
                    Expression.Equal(Expression.Constant(true), Expression.Constant(true)),
                    true,
                    ExpressionType.ListInit,
                    context,
                    false
                );
            },
                Throws.InstanceOf <NotSupportedException>()
            );
        }