| | 1 | | using Plainquire.Filter.Abstractions; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Diagnostics.CodeAnalysis; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Linq.Expressions; |
| | 7 | |
|
| | 8 | | namespace Plainquire.Filter.ValueFilterExpressions; |
| | 9 | |
|
| | 10 | | /// <inheritdoc cref="IStringFilterExpression"/> |
| | 11 | | [SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Provided as library, can be used from outsid |
| | 12 | | public class StringFilterExpression : DefaultFilterExpression, IStringFilterExpression |
| | 13 | | { |
| | 14 | | /// <inheritdoc /> |
| | 15 | | public override ICollection<FilterOperator> SupportedFilterOperators |
| | 16 | | => |
| 1064 | 17 | | [ |
| 1064 | 18 | | FilterOperator.Default, |
| 1064 | 19 | | FilterOperator.Contains, |
| 1064 | 20 | | FilterOperator.StartsWith, |
| 1064 | 21 | | FilterOperator.EndsWith, |
| 1064 | 22 | | FilterOperator.EqualCaseSensitive, |
| 1064 | 23 | | FilterOperator.EqualCaseInsensitive, |
| 1064 | 24 | | FilterOperator.NotEqual, |
| 1064 | 25 | | FilterOperator.IsNull, |
| 1064 | 26 | | FilterOperator.NotNull |
| 1064 | 27 | | ]; |
| | 28 | |
|
| | 29 | | /// <inheritdoc /> |
| | 30 | | public override bool CanCreateExpressionFor(Type type) |
| 35040 | 31 | | => type.GetUnderlyingType() == typeof(string); |
| | 32 | |
|
| | 33 | | /// <inheritdoc /> |
| | 34 | | protected internal override Expression CreateExpressionForValue<TEntity, TProperty>(Expression<Func<TEntity, TProper |
| | 35 | | { |
| 3368 | 36 | | var strFilter = value?.Trim(); |
| | 37 | | switch (filterOperator) |
| | 38 | | { |
| | 39 | | case FilterOperator.Default: |
| | 40 | | case FilterOperator.Contains: |
| 544 | 41 | | return CreateStringContainsExpression(propertySelector, strFilter); |
| | 42 | | case FilterOperator.StartsWith: |
| 462 | 43 | | return CreateStringStartsWithExpression(propertySelector, strFilter); |
| | 44 | | case FilterOperator.EndsWith: |
| 456 | 45 | | return CreateStringEndsWithExpression(propertySelector, strFilter); |
| | 46 | | case FilterOperator.EqualCaseSensitive: |
| 294 | 47 | | return CreateStringCaseSensitiveEqualExpression(propertySelector, strFilter); |
| | 48 | | case FilterOperator.EqualCaseInsensitive: |
| 1516 | 49 | | return CreateStringCaseInsensitiveEqualExpression(propertySelector, strFilter); |
| | 50 | | case FilterOperator.NotEqual: |
| 96 | 51 | | return CreateStringNotContainsExpression(propertySelector, strFilter); |
| | 52 | | default: |
| 0 | 53 | | throw CreateFilterExpressionCreationException($"Filter operator '{filterOperator}' not allowed for prope |
| | 54 | | } |
| | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Creates a string contains expression. |
| | 59 | | /// </summary> |
| | 60 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | 61 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 62 | | /// <param name="propertySelector">A property selector.</param> |
| | 63 | | /// <param name="value">The value.</param> |
| | 64 | | public static Expression CreateStringContainsExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro |
| | 65 | | { |
| 640 | 66 | | var valueToUpper = Expression.Constant(value?.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty)); |
| 640 | 67 | | var propertyToUpper = propertySelector.Body.StringToUpper(); |
| 640 | 68 | | var propertyContainsValue = propertyToUpper.StringContains(valueToUpper); |
| 640 | 69 | | var propertyIsNotNull = propertySelector.IsNotNull(); |
| 640 | 70 | | var propertyIsNotNullAndContainsValue = Expression.AndAlso(propertyIsNotNull, propertyContainsValue); |
| 640 | 71 | | return propertyIsNotNullAndContainsValue; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Creates a string case-sensitive equal expression. |
| | 76 | | /// </summary> |
| | 77 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | 78 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 79 | | /// <param name="propertySelector">The property selector.</param> |
| | 80 | | /// <param name="value">The value.</param> |
| | 81 | | public static Expression CreateStringCaseSensitiveEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TProp |
| | 82 | | { |
| 294 | 83 | | if (value == null) |
| 0 | 84 | | return propertySelector.IsNull(); |
| 294 | 85 | | if (value == string.Empty) |
| 54 | 86 | | return propertySelector.StringIsEmpty(); |
| | 87 | |
|
| 240 | 88 | | var valueExpression = Expression.Constant(value, typeof(TProperty)); |
| 240 | 89 | | var propertyEqualsValue = Expression.Equal(propertySelector.Body, valueExpression); |
| 240 | 90 | | return propertyEqualsValue; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Creates a string case-insensitive equal expression. |
| | 95 | | /// </summary> |
| | 96 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | 97 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 98 | | /// <param name="propertySelector">The property selector.</param> |
| | 99 | | /// <param name="value">The value.</param> |
| | 100 | | public static Expression CreateStringCaseInsensitiveEqualExpression<TEntity, TProperty>(Expression<Func<TEntity, TPr |
| | 101 | | { |
| 1522 | 102 | | if (value == null) |
| 0 | 103 | | return propertySelector.IsNull(); |
| 1522 | 104 | | if (value == string.Empty) |
| 192 | 105 | | return propertySelector.StringIsEmpty(); |
| | 106 | |
|
| 1330 | 107 | | var valueToUpper = Expression.Constant(value.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty)); |
| 1330 | 108 | | var propertyToUpper = propertySelector.Body.StringToUpper(); |
| 1330 | 109 | | var propertyEqualsValue = Expression.Equal(propertyToUpper, valueToUpper); |
| 1330 | 110 | | var propertyIsNotNull = propertySelector.IsNotNull(); |
| 1330 | 111 | | var propertyIsNotNullAndEqualsValue = Expression.AndAlso(propertyIsNotNull, propertyEqualsValue); |
| 1330 | 112 | | return propertyIsNotNullAndEqualsValue; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Creates a string starts with expression. |
| | 117 | | /// </summary> |
| | 118 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | 119 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 120 | | /// <param name="propertySelector">The property selector.</param> |
| | 121 | | /// <param name="value">The value.</param> |
| | 122 | | public static Expression CreateStringStartsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> p |
| | 123 | | { |
| 462 | 124 | | var valueToUpper = Expression.Constant(value?.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty)); |
| 462 | 125 | | var propertyToUpper = propertySelector.Body.StringToUpper(); |
| 462 | 126 | | var propertyStartsWithValue = propertyToUpper.StringStartsWith(valueToUpper); |
| 462 | 127 | | var propertyIsNotNull = propertySelector.IsNotNull(); |
| 462 | 128 | | var propertyIsNotNullAndStartsWithValue = Expression.AndAlso(propertyIsNotNull, propertyStartsWithValue); |
| 462 | 129 | | return propertyIsNotNullAndStartsWithValue; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <summary> |
| | 133 | | /// Creates a string ends with expression. |
| | 134 | | /// </summary> |
| | 135 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | 136 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 137 | | /// <param name="propertySelector">The property selector.</param> |
| | 138 | | /// <param name="value">The value.</param> |
| | 139 | | public static Expression CreateStringEndsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro |
| | 140 | | { |
| 456 | 141 | | var valueToUpper = Expression.Constant(value?.ToUpper(CultureInfo.InvariantCulture), typeof(TProperty)); |
| 456 | 142 | | var propertyToUpper = propertySelector.Body.StringToUpper(); |
| 456 | 143 | | var propertyEndsWithValue = propertyToUpper.StringEndsWith(valueToUpper); |
| 456 | 144 | | var propertyIsNotNull = propertySelector.IsNotNull(); |
| 456 | 145 | | var propertyIsNotNullAndEndsWithValue = Expression.AndAlso(propertyIsNotNull, propertyEndsWithValue); |
| 456 | 146 | | return propertyIsNotNullAndEndsWithValue; |
| | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Creates a negated string contains expression. |
| | 151 | | /// </summary> |
| | 152 | | /// <typeparam name="TEntity">The type of the t entity.</typeparam> |
| | 153 | | /// <typeparam name="TProperty">The type of the t property.</typeparam> |
| | 154 | | /// <param name="propertySelector">The property selector.</param> |
| | 155 | | /// <param name="value">The value.</param> |
| | 156 | | /// <autogeneratedoc /> |
| | 157 | | public static Expression CreateStringNotContainsExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> |
| 96 | 158 | | => Expression.Not(CreateStringContainsExpression(propertySelector, value)); |
| | 159 | | } |