| | 1 | | using Plainquire.Filter.Abstractions; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Diagnostics.CodeAnalysis; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Linq.Expressions; |
| | 7 | |
|
| | 8 | | namespace Plainquire.Filter.ValueFilterExpressions; |
| | 9 | |
|
| | 10 | | /// <inheritdoc /> |
| | 11 | | [SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Provided as library, can be used from outsid |
| | 12 | | [SuppressMessage("ReSharper", "MemberCanBeProtected.Global", Justification = "Provided as library, can be used from outs |
| | 13 | | public class DefaultFilterExpression : IValueFilterExpression |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Gets the supported filter operators. |
| | 17 | | /// </summary> |
| | 18 | | public virtual ICollection<FilterOperator> SupportedFilterOperators |
| | 19 | | => |
| 0 | 20 | | [ |
| 0 | 21 | | FilterOperator.Default, |
| 0 | 22 | | FilterOperator.EqualCaseSensitive, |
| 0 | 23 | | FilterOperator.EqualCaseInsensitive, |
| 0 | 24 | | FilterOperator.NotEqual, |
| 0 | 25 | | FilterOperator.LessThanOrEqual, |
| 0 | 26 | | FilterOperator.LessThan, |
| 0 | 27 | | FilterOperator.GreaterThanOrEqual, |
| 0 | 28 | | FilterOperator.GreaterThan, |
| 0 | 29 | | FilterOperator.IsNull, |
| 0 | 30 | | FilterOperator.NotNull |
| 0 | 31 | | ]; |
| | 32 | |
|
| | 33 | | /// <inheritdoc /> |
| | 34 | | public bool CanCreateExpressionFor<TType>() |
| 72492 | 35 | | => CanCreateExpressionFor(typeof(TType)); |
| | 36 | |
|
| | 37 | | /// <inheritdoc /> |
| | 38 | | public virtual bool CanCreateExpressionFor(Type type) |
| 0 | 39 | | => true; |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| | 42 | | public Expression? CreateExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> propertySelector, IEnum |
| | 43 | | { |
| 18194 | 44 | | var filterCollection = filters?.ToList(); |
| 18194 | 45 | | if (filterCollection == null || filterCollection.Count == 0) |
| 10 | 46 | | return null; |
| | 47 | |
|
| 18184 | 48 | | var propertyType = typeof(TProperty); |
| 18184 | 49 | | var propertyCanBeNull = !propertyType.IsValueType || Nullable.GetUnderlyingType(propertyType) != null; |
| | 50 | |
|
| 18184 | 51 | | var filterExpressions = filterCollection |
| 18184 | 52 | | .Select(filter => |
| 18184 | 53 | | { |
| 18184 | 54 | | if (!SupportedFilterOperators.Contains(filter.Operator)) |
| 18184 | 55 | | throw CreateFilterExpressionCreationException($"Filter operator '{filter.Operator}' not allowed for |
| 18184 | 56 | |
|
| 18184 | 57 | | if (filter.IsEmpty) |
| 18184 | 58 | | return null; |
| 18184 | 59 | |
|
| 18184 | 60 | | switch (filter.Operator) |
| 18184 | 61 | | { |
| 18184 | 62 | | case FilterOperator.IsNull: |
| 18184 | 63 | | if (!propertyCanBeNull) |
| 18184 | 64 | | throw CreateFilterExpressionCreationException($"Filter operator '{filter.Operator}' not allo |
| 18184 | 65 | | return Expression.Equal(propertySelector.Body, Expression.Constant(null)); |
| 18184 | 66 | | case FilterOperator.NotNull: |
| 18184 | 67 | | if (!propertyCanBeNull) |
| 18184 | 68 | | throw CreateFilterExpressionCreationException($"Filter operator '{filter.Operator}' not allo |
| 18184 | 69 | | return Expression.NotEqual(propertySelector.Body, Expression.Constant(null)); |
| 18184 | 70 | | default: |
| 18184 | 71 | | return CreateExpressionForValue(propertySelector, filter.Operator, filter.Value, configuration, |
| 18184 | 72 | | } |
| 18184 | 73 | | }) |
| 18184 | 74 | | .WhereNotNull() |
| 18184 | 75 | | .ToList(); |
| | 76 | |
|
| 16304 | 77 | | if (filterExpressions.Count == 0) |
| 195 | 78 | | return null; |
| | 79 | |
|
| 16109 | 80 | | var filterExpression = filterExpressions.Aggregate(Expression.OrElse); |
| | 81 | |
|
| 16109 | 82 | | return filterExpression; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Creates the body of a filter expression. |
| | 87 | | /// </summary> |
| | 88 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 89 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 90 | | /// <param name="propertySelector">The property to create the expression for.</param> |
| | 91 | | /// <param name="filterOperator">The filter operator to use.</param> |
| | 92 | | /// <param name="value">The value to create the expression for.</param> |
| | 93 | | /// <param name="configuration">The filter configuration to use.</param> |
| | 94 | | /// <param name="interceptor">An interceptor to manipulate the generated filters.</param> |
| | 95 | | protected internal virtual Expression? CreateExpressionForValue<TEntity, TProperty>(Expression<Func<TEntity, TProper |
| | 96 | | { |
| | 97 | | switch (filterOperator) |
| | 98 | | { |
| | 99 | | case FilterOperator.NotEqual: |
| 0 | 100 | | return CreateNotEqualExpression(propertySelector, value); |
| | 101 | | case FilterOperator.GreaterThan: |
| 0 | 102 | | return CreateGreaterThanExpression(propertySelector, value); |
| | 103 | | case FilterOperator.GreaterThanOrEqual: |
| 0 | 104 | | return CreateGreaterThanOrEqualExpression(propertySelector, value); |
| | 105 | | case FilterOperator.LessThan: |
| 0 | 106 | | return CreateLessThanExpression(propertySelector, value); |
| | 107 | | case FilterOperator.LessThanOrEqual: |
| 0 | 108 | | return CreateLessThanOrEqualExpression(propertySelector, value); |
| | 109 | | case FilterOperator.Default: |
| | 110 | | case FilterOperator.EqualCaseInsensitive: |
| | 111 | | case FilterOperator.EqualCaseSensitive: |
| 0 | 112 | | return CreateEqualExpression(propertySelector, value); |
| | 113 | | default: |
| 0 | 114 | | throw CreateFilterExpressionCreationException($"Filter operator '{filterOperator}' not allowed for prope |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Creates a binary 'equals' ('==') expression for the given property/value pair. |
| | 120 | | /// </summary> |
| | 121 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 122 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 123 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 124 | | /// <param name="propertySelector">The property to use.</param> |
| | 125 | | /// <param name="value">The value to use.</param> |
| | 126 | | public static Expression CreateEqualExpression<TEntity, TProperty, TValue>(Expression<Func<TEntity, TProperty>> prop |
| 5276 | 127 | | => CreateEqualExpression(propertySelector, typeof(TValue), value); |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Creates a binary 'equals' ('==') expression for the given property/value pair. |
| | 131 | | /// </summary> |
| | 132 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 133 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 134 | | /// <param name="propertySelector">The property to use.</param> |
| | 135 | | /// <param name="valueType">The type of the value.</param> |
| | 136 | | /// <param name="value">The value to use.</param> |
| | 137 | | protected static Expression CreateEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> propertyS |
| | 138 | | { |
| 5276 | 139 | | var (propertyExpression, valueExpression) = CreateAndCastExpressionParts(propertySelector, valueType, value); |
| 5276 | 140 | | return Expression.Equal(propertyExpression, valueExpression); |
| | 141 | | } |
| | 142 | |
|
| | 143 | | /// <summary> |
| | 144 | | /// Creates a binary 'not equals' ('!=') expression for the given property/value pair. |
| | 145 | | /// </summary> |
| | 146 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 147 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 148 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 149 | | /// <param name="propertySelector">The property to use.</param> |
| | 150 | | /// <param name="value">The value to use.</param> |
| | 151 | | public static Expression CreateNotEqualExpression<TEntity, TProperty, TValue>(Expression<Func<TEntity, TProperty>> p |
| 1344 | 152 | | => CreateNotEqualExpression(propertySelector, typeof(TValue), value); |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Creates a binary 'not equals' ('!=') expression for the given property/value pair. |
| | 156 | | /// </summary> |
| | 157 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 158 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 159 | | /// <param name="propertySelector">The property to use.</param> |
| | 160 | | /// <param name="valueType">The type of the value.</param> |
| | 161 | | /// <param name="value">The value to use.</param> |
| | 162 | | protected static Expression CreateNotEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> proper |
| | 163 | | { |
| 1344 | 164 | | var (propertyExpression, valueExpression) = CreateAndCastExpressionParts(propertySelector, valueType, value); |
| 1344 | 165 | | return Expression.NotEqual(propertyExpression, valueExpression); |
| | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Creates a binary 'less than' ('<') expression for the given property/value pair. |
| | 170 | | /// </summary> |
| | 171 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 172 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 173 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 174 | | /// <param name="propertySelector">The property to use.</param> |
| | 175 | | /// <param name="value">The value to use.</param> |
| | 176 | | public static Expression CreateLessThanExpression<TEntity, TProperty, TValue>(Expression<Func<TEntity, TProperty>> p |
| 678 | 177 | | => CreateLessThanExpression(propertySelector, typeof(TValue), value); |
| | 178 | |
|
| | 179 | | /// <summary> |
| | 180 | | /// Creates a binary 'less than' ('<') expression for the given property/value pair. |
| | 181 | | /// </summary> |
| | 182 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 183 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 184 | | /// <param name="propertySelector">The property to use.</param> |
| | 185 | | /// <param name="valueType">The type of the value.</param> |
| | 186 | | /// <param name="value">The value to use.</param> |
| | 187 | | protected static Expression CreateLessThanExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> proper |
| | 188 | | { |
| 942 | 189 | | var (propertyExpression, valueExpression) = CreateAndCastExpressionParts(propertySelector, valueType, value); |
| 942 | 190 | | return Expression.LessThan(propertyExpression, valueExpression); |
| | 191 | | } |
| | 192 | |
|
| | 193 | | /// <summary> |
| | 194 | | /// Creates a binary 'less than or equal' ('<=') expression for the given property/value pair. |
| | 195 | | /// </summary> |
| | 196 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 197 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 198 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 199 | | /// <param name="propertySelector">The property to use.</param> |
| | 200 | | /// <param name="value">The value to use.</param> |
| | 201 | | public static Expression CreateLessThanOrEqualExpression<TEntity, TProperty, TValue>(Expression<Func<TEntity, TPrope |
| 672 | 202 | | => CreateLessThanOrEqualExpression(propertySelector, typeof(TValue), value); |
| | 203 | |
|
| | 204 | | /// <summary> |
| | 205 | | /// Creates a binary 'less than or equal' ('<=') expression for the given property/value pair. |
| | 206 | | /// </summary> |
| | 207 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 208 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 209 | | /// <param name="propertySelector">The property to use.</param> |
| | 210 | | /// <param name="valueType">The type of the value.</param> |
| | 211 | | /// <param name="value">The value to use.</param> |
| | 212 | | protected static Expression CreateLessThanOrEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> |
| | 213 | | { |
| 936 | 214 | | var (propertyExpression, valueExpression) = CreateAndCastExpressionParts(propertySelector, valueType, value); |
| 936 | 215 | | return Expression.LessThanOrEqual(propertyExpression, valueExpression); |
| | 216 | | } |
| | 217 | |
|
| | 218 | | /// <summary> |
| | 219 | | /// Creates a binary 'greater than' ('>') expression for the given property/value pair. |
| | 220 | | /// </summary> |
| | 221 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 222 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 223 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 224 | | /// <param name="propertySelector">The property to use.</param> |
| | 225 | | /// <param name="value">The value to use.</param> |
| | 226 | | public static Expression CreateGreaterThanExpression<TEntity, TProperty, TValue>(Expression<Func<TEntity, TProperty> |
| 672 | 227 | | => CreateGreaterThanExpression(propertySelector, typeof(TValue), value); |
| | 228 | |
|
| | 229 | | /// <summary> |
| | 230 | | /// Creates a binary 'greater than' ('>') expression for the given property/value pair. |
| | 231 | | /// </summary> |
| | 232 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 233 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 234 | | /// <param name="propertySelector">The property to use.</param> |
| | 235 | | /// <param name="valueType">The type of the value.</param> |
| | 236 | | /// <param name="value">The value to use.</param> |
| | 237 | | protected static Expression CreateGreaterThanExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro |
| | 238 | | { |
| 936 | 239 | | var (propertyExpression, valueExpression) = CreateAndCastExpressionParts(propertySelector, valueType, value); |
| 936 | 240 | | return Expression.GreaterThan(propertyExpression, valueExpression); |
| | 241 | | } |
| | 242 | |
|
| | 243 | | /// <summary> |
| | 244 | | /// Creates a binary 'greater than or equal' ('>=') expression for the given property/value pair. |
| | 245 | | /// </summary> |
| | 246 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 247 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 248 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 249 | | /// <param name="propertySelector">The property to use.</param> |
| | 250 | | /// <param name="value">The value to use.</param> |
| | 251 | | public static Expression CreateGreaterThanOrEqualExpression<TEntity, TProperty, TValue>(Expression<Func<TEntity, TPr |
| 678 | 252 | | => CreateGreaterThanOrEqualExpression(propertySelector, typeof(TValue), value); |
| | 253 | |
|
| | 254 | | /// <summary> |
| | 255 | | /// Creates a binary 'greater than or equal' ('>=') expression for the given property/value pair. |
| | 256 | | /// </summary> |
| | 257 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 258 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 259 | | /// <param name="propertySelector">The property to use.</param> |
| | 260 | | /// <param name="valueType">The type of the value.</param> |
| | 261 | | /// <param name="value">The value to use.</param> |
| | 262 | | protected static Expression CreateGreaterThanOrEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TPropert |
| | 263 | | { |
| 942 | 264 | | var (propertyExpression, valueExpression) = CreateAndCastExpressionParts(propertySelector, valueType, value); |
| 942 | 265 | | return Expression.GreaterThanOrEqual(propertyExpression, valueExpression); |
| | 266 | | } |
| | 267 | |
|
| | 268 | | private static (Expression propertyExpression, Expression valueExpression) CreateAndCastExpressionParts<TEntity, TPr |
| | 269 | | { |
| 10376 | 270 | | var underlingValueType = valueType.GetUnderlyingType(); |
| 10376 | 271 | | var partsHaveSameUnderlyingTypes = typeof(TProperty).GetUnderlyingType() == underlingValueType; |
| 10376 | 272 | | var propertyIsNullable = Nullable.GetUnderlyingType(typeof(TProperty)) != null; |
| | 273 | |
|
| 10376 | 274 | | var destinationType = propertyIsNullable && underlingValueType.IsValueType |
| 10376 | 275 | | ? typeof(Nullable<>).MakeGenericType(underlingValueType) |
| 10376 | 276 | | : valueType; |
| | 277 | |
|
| 10376 | 278 | | var valueExpression = partsHaveSameUnderlyingTypes |
| 10376 | 279 | | ? Expression.Constant(value, typeof(TProperty)) |
| 10376 | 280 | | : Expression.Constant(value, destinationType); |
| | 281 | |
|
| 10376 | 282 | | var propertyExpression = partsHaveSameUnderlyingTypes |
| 10376 | 283 | | ? propertySelector.Body |
| 10376 | 284 | | : propertySelector.Body.Cast(typeof(TProperty), destinationType); |
| | 285 | |
|
| 10376 | 286 | | return (propertyExpression, valueExpression); |
| | 287 | | } |
| | 288 | |
|
| | 289 | | /// <summary> |
| | 290 | | /// Creates a filter expression creation exception. |
| | 291 | | /// </summary> |
| | 292 | | /// <typeparam name="TEntity">The type of the class that declares <typeparamref name="TProperty"/>.</typeparam> |
| | 293 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 294 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 295 | | /// <param name="message">The exception message.</param> |
| | 296 | | /// <param name="property">The property the filter is created for.</param> |
| | 297 | | /// <param name="filterOperator">The filter operator.</param> |
| | 298 | | /// <param name="value">The value to filter.</param> |
| | 299 | | protected FilterExpressionException CreateFilterExpressionCreationException<TEntity, TProperty, TValue>(string messa |
| | 300 | | { |
| 1880 | 301 | | return new FilterExpressionException(message) |
| 1880 | 302 | | { |
| 1880 | 303 | | FilteredEntity = typeof(TEntity), |
| 1880 | 304 | | FilteredProperty = ((MemberExpression)property.Body).Member.Name, |
| 1880 | 305 | | FilteredPropertyType = typeof(TProperty), |
| 1880 | 306 | | FilterOperator = filterOperator, |
| 1880 | 307 | | Value = value, |
| 1880 | 308 | | ValueType = typeof(TValue), |
| 1880 | 309 | | SupportedFilterOperators = SupportedFilterOperators |
| 1880 | 310 | | }; |
| | 311 | | } |
| | 312 | | } |