-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathHQLQueryPlan.cs
171 lines (156 loc) · 6.47 KB
/
HQLQueryPlan.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Event;
using NHibernate.Hql;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Engine.Query
{
using System.Threading.Tasks;
using System.Threading;
public partial interface IQueryPlan
{
Task PerformListAsync(QueryParameters queryParameters, ISessionImplementor statelessSessionImpl, IList results, CancellationToken cancellationToken);
Task<int> PerformExecuteUpdateAsync(QueryParameters queryParameters, ISessionImplementor statelessSessionImpl, CancellationToken cancellationToken);
Task<IEnumerable<T>> PerformIterateAsync<T>(QueryParameters queryParameters, IEventSource session, CancellationToken cancellationToken);
Task<IEnumerable> PerformIterateAsync(QueryParameters queryParameters, IEventSource session, CancellationToken cancellationToken);
}
public partial class HQLQueryPlan : IQueryPlan
{
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)
{
if (Translators.Any(t => t.ContainsOrderByClause))
// in memory evaluation is only problematic if items are skipped or if there is an order by clause thus correctness is not ensured
Log.Warn("firstResult/maxResults specified on polymorphic query with order by; applying in memory!");
else if (queryParameters.RowSelection.FirstRow > 0)
// in memory evaluation is only problematic if items are skipped or if there is an order by clause thus correctness is not ensured
Log.Warn("firstResult specified on polymorphic query; applying in memory!");
RowSelection selection = new RowSelection();
UpdateRowSelection(selection, alreadyIncluded: 0);
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 = 0;
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 = queryParametersToUse.RowSelection.MaxRows;
int size = tmp.Count;
for (int x = 0; x < size; x++)
{
object result = tmp[x];
if (!distinction.Add(result))
{
continue;
}
if (includedCount++ < first)
{
continue;
}
combinedResults.Add(result);
if (max != RowSelection.NoValue && includedCount >= max)
{
// break the outer loop !!!
return;
}
}
UpdateRowSelection(queryParametersToUse.RowSelection, includedCount);
}
else
ArrayHelper.AddAll(combinedResults, tmp);
}
void UpdateRowSelection(RowSelection selection, int alreadyIncluded)
{
if (queryParameters.RowSelection.MaxRows != RowSelection.NoValue)
{
if (queryParameters.RowSelection.FirstRow > 0)
selection.MaxRows = Math.Max(0, queryParameters.RowSelection.FirstRow + queryParameters.RowSelection.MaxRows - alreadyIncluded);
else
selection.MaxRows = Math.Max(0, queryParameters.RowSelection.MaxRows - alreadyIncluded);
}
}
}
public async Task<IEnumerable> PerformIterateAsync(QueryParameters queryParameters, IEventSource session, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (Log.IsDebugEnabled())
{
Log.Debug("enumerable: {0}", _sourceQuery);
queryParameters.LogParameters(session.Factory);
}
if (Translators.Length == 0)
{
return CollectionHelper.EmptyEnumerable;
}
if (Translators.Length == 1)
{
return await (Translators[0].GetEnumerableAsync(queryParameters, session, cancellationToken)).ConfigureAwait(false);
}
var results = new IEnumerable[Translators.Length];
for (int i = 0; i < Translators.Length; i++)
{
var result = await (Translators[i].GetEnumerableAsync(queryParameters, session, cancellationToken)).ConfigureAwait(false);
results[i] = result;
}
return Enumerate(results);
}
public async Task<IEnumerable<T>> PerformIterateAsync<T>(QueryParameters queryParameters, IEventSource session, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return (await (PerformIterateAsync(queryParameters, session, cancellationToken)).ConfigureAwait(false)).CastOrDefault<T>();
}
public async Task<int> PerformExecuteUpdateAsync(QueryParameters queryParameters, ISessionImplementor session, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (Log.IsDebugEnabled())
{
Log.Debug("executeUpdate: {0}", _sourceQuery);
queryParameters.LogParameters(session.Factory);
}
if (Translators.Length != 1)
{
Log.Warn("manipulation query [{0}] resulted in [{1}] split queries", _sourceQuery, Translators.Length);
}
int result = 0;
for (int i = 0; i < Translators.Length; i++)
{
result += await (Translators[i].ExecuteUpdateAsync(queryParameters, session, cancellationToken)).ConfigureAwait(false);
}
return result;
}
}
}