|
1 | | -using Community.OData.Linq.Builder.Validators; |
2 | | -using Community.OData.Linq.Common; |
3 | | -using Community.OData.Linq.Properties; |
4 | | - |
5 | | -namespace Community.OData.Linq |
| 1 | +namespace Community.OData.Linq |
6 | 2 | { |
7 | 3 | using System; |
8 | 4 | using System.Collections.Generic; |
9 | 5 | using System.ComponentModel.Design; |
10 | 6 | using System.Diagnostics.Contracts; |
11 | 7 | using System.Linq; |
12 | 8 | using System.Linq.Expressions; |
13 | | - |
| 9 | + |
14 | 10 | using Community.OData.Linq.Builder; |
| 11 | + using Community.OData.Linq.Builder.Validators; |
15 | 12 | using Community.OData.Linq.OData; |
16 | 13 | using Community.OData.Linq.OData.Query; |
17 | | - using Community.OData.Linq.OData.Query.Expressions; |
| 14 | + using Community.OData.Linq.OData.Query.Expressions; |
18 | 15 |
|
19 | 16 | using Microsoft.Extensions.DependencyInjection; |
20 | 17 | using Microsoft.OData; |
21 | 18 | using Microsoft.OData.Edm; |
22 | 19 | using Microsoft.OData.UriParser; |
23 | 20 |
|
24 | 21 | public static class ODataLinqExtensions |
25 | | - { |
26 | | - private static readonly FilterQueryValidator FilterValidator = |
27 | | - new FilterQueryValidator(new DefaultQuerySettings {EnableFilter = true}); |
28 | | - |
29 | | - private static readonly OrderByQueryValidator OrderValidator = |
30 | | - new OrderByQueryValidator(new DefaultQuerySettings { EnableOrderBy = true }); |
31 | | - |
| 22 | + { |
32 | 23 | /// <summary> |
33 | 24 | /// The simplified options. |
34 | 25 | /// </summary> |
@@ -112,7 +103,8 @@ public static ODataQuery<T> Filter<T>(this ODataQuery<T> query, string filterTex |
112 | 103 | filterClause = new FilterClause(filterExpression, filterClause.RangeVariable); |
113 | 104 | Contract.Assert(filterClause != null); |
114 | 105 |
|
115 | | - FilterValidator.Validate(filterClause, settings.ValidationSettings, edmModel); |
| 106 | + var validator = new FilterQueryValidator(new DefaultQuerySettings { EnableFilter = true }); |
| 107 | + validator.Validate(filterClause, settings.ValidationSettings, edmModel); |
116 | 108 |
|
117 | 109 | Expression filter = FilterBinder.Bind(query, filterClause, typeof(T), query.ServiceProvider); |
118 | 110 | var result = ExpressionHelpers.Where(query, filter, typeof(T)); |
@@ -141,93 +133,13 @@ public static IOrderedQueryable<T> OrderBy<T>(this ODataQuery<T> query, string o |
141 | 133 |
|
142 | 134 | ICollection<OrderByNode> nodes = OrderByNode.CreateCollection(orderByClause); |
143 | 135 |
|
144 | | - OrderValidator.Validate(nodes, settings.ValidationSettings, edmModel); |
| 136 | + var validator = new OrderByQueryValidator(new DefaultQuerySettings { EnableOrderBy = true }); |
| 137 | + validator.Validate(nodes, settings.ValidationSettings, edmModel); |
145 | 138 |
|
146 | | - IOrderedQueryable<T> result = (IOrderedQueryable<T>)OrderApplyToCore<T>(query, settings.QuerySettings, nodes, edmModel); |
| 139 | + IOrderedQueryable<T> result = (IOrderedQueryable<T>)OrderByBinder.OrderApplyToCore(query, settings.QuerySettings, nodes, edmModel); |
147 | 140 |
|
148 | 141 | return new ODataQueryOrdered<T>(result, query.ServiceProvider); |
149 | | - } |
150 | | - |
151 | | - private static IOrderedQueryable OrderApplyToCore<T>(ODataQuery<T> query, ODataQuerySettings querySettings, ICollection<OrderByNode> nodes, IEdmModel model) |
152 | | - { |
153 | | - bool alreadyOrdered = false; |
154 | | - IQueryable querySoFar = query; |
155 | | - |
156 | | - HashSet<object> propertiesSoFar = new HashSet<object>(); |
157 | | - HashSet<string> openPropertiesSoFar = new HashSet<string>(); |
158 | | - bool orderByItSeen = false; |
159 | | - |
160 | | - foreach (OrderByNode node in nodes) |
161 | | - { |
162 | | - OrderByPropertyNode propertyNode = node as OrderByPropertyNode; |
163 | | - OrderByOpenPropertyNode openPropertyNode = node as OrderByOpenPropertyNode; |
164 | | - |
165 | | - if (propertyNode != null) |
166 | | - { |
167 | | - // Use autonomy class to achieve value equality for HasSet. |
168 | | - var edmPropertyWithPath = new { propertyNode.Property, propertyNode.PropertyPath }; |
169 | | - OrderByDirection direction = propertyNode.Direction; |
170 | | - |
171 | | - // This check prevents queries with duplicate properties (e.g. $orderby=Id,Id,Id,Id...) from causing stack overflows |
172 | | - if (propertiesSoFar.Contains(edmPropertyWithPath)) |
173 | | - { |
174 | | - throw new ODataException(Error.Format(SRResources.OrderByDuplicateProperty, edmPropertyWithPath.PropertyPath)); |
175 | | - } |
176 | | - |
177 | | - propertiesSoFar.Add(edmPropertyWithPath); |
178 | | - |
179 | | - if (propertyNode.OrderByClause != null) |
180 | | - { |
181 | | - querySoFar = AddOrderByQueryForProperty(query, querySettings, propertyNode.OrderByClause, querySoFar, direction, alreadyOrdered); |
182 | | - } |
183 | | - else |
184 | | - { |
185 | | - querySoFar = ExpressionHelpers.OrderByProperty(querySoFar, model, edmPropertyWithPath.Property, direction, typeof(T), alreadyOrdered); |
186 | | - } |
187 | | - |
188 | | - alreadyOrdered = true; |
189 | | - } |
190 | | - else if (openPropertyNode != null) |
191 | | - { |
192 | | - // This check prevents queries with duplicate properties (e.g. $orderby=Id,Id,Id,Id...) from causing stack overflows |
193 | | - if (openPropertiesSoFar.Contains(openPropertyNode.PropertyName)) |
194 | | - { |
195 | | - throw new ODataException(Error.Format(SRResources.OrderByDuplicateProperty, openPropertyNode.PropertyPath)); |
196 | | - } |
197 | | - |
198 | | - openPropertiesSoFar.Add(openPropertyNode.PropertyName); |
199 | | - Contract.Assert(openPropertyNode.OrderByClause != null); |
200 | | - querySoFar = AddOrderByQueryForProperty(query, querySettings, openPropertyNode.OrderByClause, querySoFar, openPropertyNode.Direction, alreadyOrdered); |
201 | | - alreadyOrdered = true; |
202 | | - } |
203 | | - else |
204 | | - { |
205 | | - // This check prevents queries with duplicate nodes (e.g. $orderby=$it,$it,$it,$it...) from causing stack overflows |
206 | | - if (orderByItSeen) |
207 | | - { |
208 | | - throw new ODataException(Error.Format(SRResources.OrderByDuplicateIt)); |
209 | | - } |
210 | | - |
211 | | - querySoFar = ExpressionHelpers.OrderByIt(querySoFar, node.Direction, typeof(T), alreadyOrdered); |
212 | | - alreadyOrdered = true; |
213 | | - orderByItSeen = true; |
214 | | - } |
215 | | - } |
216 | | - |
217 | | - return querySoFar as IOrderedQueryable; |
218 | | - } |
219 | | - |
220 | | - private static IQueryable AddOrderByQueryForProperty<T>(ODataQuery<T> query, ODataQuerySettings querySettings, |
221 | | - OrderByClause orderbyClause, IQueryable querySoFar, OrderByDirection direction, bool alreadyOrdered) |
222 | | - { |
223 | | - //Context.UpdateQuerySettings(querySettings, query); |
224 | | - |
225 | | - LambdaExpression orderByExpression = |
226 | | - FilterBinder.Bind(query, orderbyClause, typeof(T), query.ServiceProvider); |
227 | | - querySoFar = ExpressionHelpers.OrderBy(querySoFar, orderByExpression, direction, typeof(T), |
228 | | - alreadyOrdered); |
229 | | - return querySoFar; |
230 | | - } |
| 142 | + } |
231 | 143 |
|
232 | 144 | private static OrderByClause TranslateParameterAlias(OrderByClause orderBy, ODataQueryOptionParser queryOptionParser) |
233 | 145 | { |
|
0 commit comments