Example #1
0
        public void PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug("find: " + _sourceQuery);
                queryParameters.LogParameters(session.Factory);
            }

            bool            hasLimit   = queryParameters.RowSelection != null && queryParameters.RowSelection.DefinesLimits;
            bool            needsLimit = hasLimit && Translators.Length > 1;
            QueryParameters queryParametersToUse;

            if (needsLimit)
            {
                Log.Warn("firstResult/maxResults specified on polymorphic query; applying in memory!");
                RowSelection selection = new RowSelection();
                selection.FetchSize  = queryParameters.RowSelection.FetchSize;
                selection.Timeout    = queryParameters.RowSelection.Timeout;
                queryParametersToUse = queryParameters.CreateCopyUsing(selection);
            }
            else
            {
                queryParametersToUse = queryParameters;
            }

            IList       combinedResults = results ?? new List <object>();
            IdentitySet distinction     = new IdentitySet();
            int         includedCount   = -1;

            for (int i = 0; i < Translators.Length; i++)
            {
                IList tmp = Translators[i].List(session, queryParametersToUse);
                if (needsLimit)
                {
                    // NOTE : firstRow is zero-based
                    int first = queryParameters.RowSelection.FirstRow == RowSelection.NoValue
                                                                                                ? 0
                                                                                                : queryParameters.RowSelection.FirstRow;

                    int max = queryParameters.RowSelection.MaxRows == RowSelection.NoValue
                                                                                        ? RowSelection.NoValue
                                                                                        : queryParameters.RowSelection.MaxRows;

                    int size = tmp.Count;
                    for (int x = 0; x < size; x++)
                    {
                        object result = tmp[x];
                        if (distinction.Add(result))
                        {
                            continue;
                        }
                        includedCount++;
                        if (includedCount < first)
                        {
                            continue;
                        }
                        combinedResults.Add(result);
                        if (max >= 0 && includedCount > max)
                        {
                            // break the outer loop !!!
                            return;
                        }
                    }
                }
                else
                {
                    ArrayHelper.AddAll(combinedResults, tmp);
                }
            }
        }
        public async Task <IList> ListAsync(ISessionImplementor session, QueryParameters queryParameters, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            // Delegate to the QueryLoader...
            ErrorIfDML();
            var  query            = ( QueryNode )_sqlAst;
            bool hasLimit         = queryParameters.RowSelection != null && queryParameters.RowSelection.DefinesLimits;
            bool needsDistincting = (query.GetSelectClause().IsDistinct || hasLimit) && ContainsCollectionFetches;

            QueryParameters queryParametersToUse;

            if (hasLimit && ContainsCollectionFetches)
            {
                log.Warn("firstResult/maxResults specified with collection fetch; applying in memory!");
                var selection = new RowSelection
                {
                    FetchSize = queryParameters.RowSelection.FetchSize,
                    Timeout   = queryParameters.RowSelection.Timeout
                };
                queryParametersToUse = queryParameters.CreateCopyUsing(selection);
            }
            else
            {
                queryParametersToUse = queryParameters;
            }

            IList results = await(_queryLoader.ListAsync(session, queryParametersToUse, cancellationToken)).ConfigureAwait(false);

            if (needsDistincting)
            {
                int includedCount = -1;
                // NOTE : firstRow is zero-based
                int first = !hasLimit || queryParameters.RowSelection.FirstRow == RowSelection.NoValue
                                                        ? 0
                                                        : queryParameters.RowSelection.FirstRow;
                int max = !hasLimit || queryParameters.RowSelection.MaxRows == RowSelection.NoValue
                                                        ? -1
                                                        : queryParameters.RowSelection.MaxRows;

                int size        = results.Count;
                var tmp         = new List <object>();
                var distinction = new HashSet <object>(ReferenceComparer <object> .Instance);

                for (int i = 0; i < size; i++)
                {
                    object result = results[i];
                    if (!distinction.Add(result))
                    {
                        continue;
                    }
                    includedCount++;
                    if (includedCount < first)
                    {
                        continue;
                    }
                    tmp.Add(result);
                    // NOTE : ( max - 1 ) because first is zero-based while max is not...
                    if (max >= 0 && (includedCount - first) >= (max - 1))
                    {
                        break;
                    }
                }

                results = tmp;
            }

            return(results);
        }
Example #3
0
        public async Task PerformListAsync(QueryParameters queryParameters, ISessionImplementor session, IList results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (Log.IsDebugEnabled())
            {
                Log.Debug("find: {0}", _sourceQuery);
                queryParameters.LogParameters(session.Factory);
            }

            bool            hasLimit   = queryParameters.RowSelection != null && queryParameters.RowSelection.DefinesLimits;
            bool            needsLimit = hasLimit && Translators.Length > 1;
            QueryParameters queryParametersToUse;

            if (needsLimit)
            {
                Log.Warn("firstResult/maxResults specified on polymorphic query; applying in memory!");
                RowSelection selection = new RowSelection();
                selection.FetchSize  = queryParameters.RowSelection.FetchSize;
                selection.Timeout    = queryParameters.RowSelection.Timeout;
                queryParametersToUse = queryParameters.CreateCopyUsing(selection);
            }
            else
            {
                queryParametersToUse = queryParameters;
            }

            IList combinedResults = results ?? new List <object>();
            var   distinction     = new HashSet <object>(ReferenceComparer <object> .Instance);
            int   includedCount   = -1;

            for (int i = 0; i < Translators.Length; i++)
            {
                IList tmp = await(Translators[i].ListAsync(session, queryParametersToUse, cancellationToken)).ConfigureAwait(false);
                if (needsLimit)
                {
                    // NOTE : firstRow is zero-based
                    int first = queryParameters.RowSelection.FirstRow == RowSelection.NoValue
                                                                                                ? 0
                                                                                                : queryParameters.RowSelection.FirstRow;

                    int max = queryParameters.RowSelection.MaxRows == RowSelection.NoValue
                                                                                        ? RowSelection.NoValue
                                                                                        : queryParameters.RowSelection.MaxRows;

                    int size = tmp.Count;
                    for (int x = 0; x < size; x++)
                    {
                        object result = tmp[x];
                        if (distinction.Add(result))
                        {
                            continue;
                        }
                        includedCount++;
                        if (includedCount < first)
                        {
                            continue;
                        }
                        combinedResults.Add(result);
                        if (max >= 0 && includedCount > max)
                        {
                            // break the outer loop !!!
                            return;
                        }
                    }
                }
                else
                {
                    ArrayHelper.AddAll(combinedResults, tmp);
                }
            }
        }