Example #1
0
        /// <summary>
        ///     Populates the given <see cref="IResultStream{TDestination}" /> with the results, from the execution of
        ///     the given <see cref="IQueryableFlowQuery" />, in a streamed fashion.
        /// </summary>
        /// <typeparam name="TSource">
        ///     The <see cref="System.Type" /> of the source entity.
        /// </typeparam>
        /// <typeparam name="TDestination">
        ///     The <see cref="System.Type" /> of the result.
        /// </typeparam>
        /// <param name="query">
        ///     The query.
        /// </param>
        /// <param name="resultStream">
        ///     The <see cref="IResultStream{TDestination}" /> to stream the results into.
        /// </param>
        public static void SelectStream <TSource, TDestination>
        (
            IQueryableFlowQuery query,
            IResultStream <TDestination> resultStream
        )
        {
            Func <object, TDestination> converter = null;

            if (query.Constructor != null)
            {
                converter = ConstructionHelper.GetObjectByExpressionConverter <TDestination>(query.Constructor);
            }

            if (converter == null)
            {
                converter = x => (TDestination)x;
            }

            var streamer = new ResultStreamer <TDestination>(resultStream, converter);

            ICriteriaBuilder criteriaBuilder = GetCriteriaBuilder(query);

            ICriteria criteria = criteriaBuilder.Build <TSource, TDestination>(query);

            criteria.List(streamer);

            resultStream.EndOfStream();
        }
Example #2
0
        /// <summary>
        ///     Resolves an appropriate <see cref="ICriteriaBuilder" /> for the given
        ///     <see cref="IQueryableFlowQuery" />.
        /// </summary>
        /// <param name="query">
        ///     The <see cref="IQueryableFlowQuery" />.
        /// </param>
        /// <returns>
        ///     The <see cref="ICriteriaBuilder" />.
        /// </returns>
        private static ICriteriaBuilder GetCriteriaBuilder(IQueryableFlowQuery query)
        {
            if (query != null && query.Options != null && query.Options.CriteriaBuilder != null)
            {
                return(query.Options.CriteriaBuilder);
            }

            return(FlowQueryOptions.GlobalCriteriaBuilder);
        }
Example #3
0
        /// <summary>
        ///     Creates a value query selection.
        /// </summary>
        /// <param name="query">
        ///     The query.
        /// </param>
        /// <typeparam name="TSource">
        ///     The <see cref="System.Type" /> of the source entity.
        /// </typeparam>
        /// <typeparam name="TDestination">
        ///     The <see cref="System.Type" /> of the result.
        /// </typeparam>
        /// <returns>
        ///     An action to retrieve the selected value.
        /// </returns>
        public static Func <TDestination> SelectValue <TSource, TDestination>(IQueryableFlowQuery query)
            where TSource : class
        {
            ICriteriaBuilder criteriaBuilder = GetCriteriaBuilder(query);

            ICriteria criteria = criteriaBuilder.Build <TSource, TDestination>(query);

            if (query.IsDelayed)
            {
                IFutureValue <TDestination> value = criteria.FutureValue <TDestination>();

                return(() => value.Value);
            }

            return(criteria.UniqueResult <TDestination>);
        }
Example #4
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="QuerySelection" /> class.
        /// </summary>
        /// <param name="query">
        ///     The query.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="query" /> is null.
        /// </exception>
        protected QuerySelection(IQueryableFlowQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            Alias           = query.Alias;
            Aliases         = query.Aliases.ToDictionary(x => x.Key, x => x.Value);
            CacheMode       = query.CacheMode;
            CacheRegion     = query.CacheRegion;
            CommentValue    = query.CommentValue;
            Constructor     = query.Constructor;
            CriteriaFactory = query.CriteriaFactory;
            Criterions      = query.Criterions.ToList();
            Data            = query.Data;
            Fetches         = query.Fetches.ToList();
            FetchSizeValue  = query.FetchSizeValue;
            GroupBys        = query.GroupBys.ToList();
            IsCacheable     = query.IsCacheable;
            IsDelayed       = query.IsDelayed;
            IsDistinct      = query.IsDistinct;
            IsReadOnly      = query.IsReadOnly;
            Joins           = query.Joins.ToList();
            Locks           = query.Locks.ToList();
            Mappings        = query.Mappings == null
                ? null
                : query.Mappings.ToDictionary(x => x.Key, x => x.Value);
            Options           = query.Options;
            Orders            = query.Orders.ToList();
            Projection        = query.Projection;
            ResultsToSkip     = query.ResultsToSkip;
            ResultsToTake     = query.ResultsToTake;
            ResultTransformer = query.ResultTransformer;
            TimeoutValue      = query.TimeoutValue;
        }
Example #5
0
 /// <summary>
 ///     Creates an instance of this class.
 /// </summary>
 /// <param name="query">
 ///     The query to create an instance from.
 /// </param>
 /// <returns>
 ///     The created <see cref="QuerySelection" /> instance.
 /// </returns>
 public static QuerySelection Create(IQueryableFlowQuery query)
 {
     return(new QuerySelection(query));
 }
Example #6
0
 public override ICriteria Build <TSource, TDestination>(IQueryableFlowQuery query)
 {
     throw new NotImplementedException();
 }
Example #7
0
        /// <summary>
        ///     Creates a regular <see cref="FlowQuerySelection{TDestination}" /> instance.
        /// </summary>
        /// <param name="query">
        ///     The query.
        /// </param>
        /// <typeparam name="TSource">
        ///     The <see cref="System.Type" /> of the source entity.
        /// </typeparam>
        /// <typeparam name="TDestination">
        ///     The <see cref="System.Type" /> of the result.
        /// </typeparam>
        /// <returns>
        ///     The created <see cref="FlowQuerySelection{TDestination}" /> instance.
        /// </returns>
        public static FlowQuerySelection <TDestination> SelectList <TSource, TDestination>(IQueryableFlowQuery query)
            where TSource : class
        {
            ICriteriaBuilder criteriaBuilder = GetCriteriaBuilder(query);

            ICriteria criteria = criteriaBuilder.Build <TSource, TDestination>(query);

            if (query.Constructor != null)
            {
                IEnumerable enumerable = query.IsDelayed
                    ? criteria.Future <object>()
                    : (IEnumerable)criteria.List();

                return(new FlowQuerySelection <TDestination>
                       (
                           () => ConstructionHelper.GetListByExpression <TDestination>(query.Constructor, enumerable)
                       ));
            }

            IEnumerable <TDestination> selection = query.IsDelayed
                ? criteria.Future <TDestination>()
                : criteria.List <TDestination>();

            // ReSharper disable once PossibleMultipleEnumeration
            return(new FlowQuerySelection <TDestination>(() => selection));
        }
        /// <summary>
        ///     Builds a <see cref="ICriteria" /> from the given <see cref="IQueryableFlowQuery" /> query.
        /// </summary>
        /// <param name="query">
        ///     The query from which to build a <see cref="ICriteria" />.
        /// </param>
        /// <typeparam name="TSource">
        ///     The <see cref="System.Type" /> of the underlying entity for the given query.
        /// </typeparam>
        /// <typeparam name="TDestination">
        ///     The <see cref="System.Type" /> of the result.
        /// </typeparam>
        /// <returns>
        ///     The built <see cref="ICriteria" />.
        /// </returns>
        public virtual ICriteria Build <TSource, TDestination>(IQueryableFlowQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            ICriteria criteria = query.CriteriaFactory(typeof(TSource), query.Alias);

            if (query.Options != null)
            {
                query.Options.Use(criteria);
            }

            // remove fetch duplicates created by aliasing
            var fetches = query.Fetches
                          .Select(x => new
            {
                x.Path,
                x.FetchMode
            })
                          .Distinct();

            foreach (var fetch in fetches)
            {
                criteria.SetFetchMode(fetch.Path, fetch.FetchMode);
            }

            foreach (Join join in query.Joins)
            {
                criteria.CreateAlias(join.Property, join.Alias, join.JoinType, join.WithClause);
            }

            foreach (ICriterion criterion in query.Criterions)
            {
                criteria.Add(criterion);
            }

            foreach (Lock queryLock in query.Locks)
            {
                if (queryLock.Alias == null)
                {
                    criteria.SetLockMode(queryLock.LockMode);
                }
                else
                {
                    criteria.SetLockMode(queryLock.Alias, queryLock.LockMode);
                }
            }

            if (query.ResultsToSkip.HasValue)
            {
                criteria.SetFirstResult(query.ResultsToSkip.Value);
            }

            if (query.ResultsToTake.HasValue)
            {
                criteria.SetMaxResults(query.ResultsToTake.Value);
            }

            if (query.IsCacheable)
            {
                criteria.SetCacheable(true);

                if (query.CacheMode.HasValue)
                {
                    criteria.SetCacheMode(query.CacheMode.Value);
                }

                if (!string.IsNullOrEmpty(query.CacheRegion))
                {
                    criteria.SetCacheRegion(query.CacheRegion);
                }
            }

            if (query.IsReadOnly.HasValue)
            {
                criteria.SetReadOnly(query.IsReadOnly.Value);
            }

            if (query.CommentValue != null)
            {
                criteria.SetComment(query.CommentValue);
            }

            if (query.FetchSizeValue > 0)
            {
                criteria.SetFetchSize(query.FetchSizeValue);
            }

            criteria
            .SetProjection
            (
                GetProjection(query)
            );

            if (query.TimeoutValue.HasValue)
            {
                criteria.SetTimeout(query.TimeoutValue.Value);
            }

            if (query.ResultTransformer != null)
            {
                criteria.SetResultTransformer(query.ResultTransformer);
            }

            if (query.Orders.Count > 0)
            {
                bool suppressErrors = FlowQueryOptions.GloballySuppressOrderByProjectionErrors ||
                                      (query.Options != null && query.Options.ShouldSuppressOrderByProjectionErrors);

                foreach (OrderByStatement statement in query.Orders)
                {
                    if (statement.IsBasedOnSource)
                    {
                        criteria.AddOrder(statement.Order);
                    }
                    else if (query.Mappings != null)
                    {
                        if (statement.ProjectionSourceType == typeof(TDestination))
                        {
                            string mappingKey = query.Mappings.Keys
                                                .SingleOrDefault
                                                (
                                x => string
                                .Equals(x, statement.Property, StringComparison.InvariantCultureIgnoreCase)
                                                );

                            if (mappingKey != null)
                            {
                                criteria.AddOrder(new Order(mappingKey, statement.OrderAscending));
                            }
                            else if (!suppressErrors)
                            {
                                throw new InvalidOperationException
                                      (
                                          "unable to order by a projection property that is not projected into by the query"
                                      );
                            }
                        }
                        else if (!suppressErrors)
                        {
                            throw new InvalidOperationException
                                  (
                                      "unable to order by a projection property on a projection of a type other than the " +
                                      "returned one."
                                  );
                        }
                    }
                }
            }

            return(criteria);
        }