|
6 | 6 |
|
7 | 7 | namespace Gridify.Builder; |
8 | 8 |
|
| 9 | +internal static class BaseQueryBuilder |
| 10 | +{ |
| 11 | + internal static MemberNullPropogationVisitor MemberNullPropagatorVisitor { get; } = new(); |
| 12 | +} |
| 13 | + |
9 | 14 | public abstract class BaseQueryBuilder<TQuery, T>(IGridifyMapper<T> mapper) |
10 | 15 | where TQuery : class |
11 | 16 | { |
@@ -38,55 +43,59 @@ public TQuery Build(ExpressionSyntax expression) |
38 | 43 |
|
39 | 44 | protected abstract TQuery CombineWithOrOperator(TQuery left, TQuery right); |
40 | 45 |
|
41 | | - private (TQuery Query, bool IsNested) BuildQuery(ExpressionSyntax expression, bool isParenthesisOpen = false) |
| 46 | + private (TQuery Query, bool IsNested) BuildQueryFromBinaryExpression(BinaryExpressionSyntax? bExp, bool isParenthesisOpen = false) |
42 | 47 | { |
43 | | - while (true) |
44 | | - switch (expression.Kind) |
| 48 | + if (bExp!.Left is FieldExpressionSyntax && bExp.Right is ValueExpressionSyntax) |
| 49 | + try |
45 | 50 | { |
46 | | - case SyntaxKind.BinaryExpression: |
47 | | - { |
48 | | - var bExp = expression as BinaryExpressionSyntax; |
| 51 | + return ConvertBinaryExpressionSyntaxToQuery(bExp) |
| 52 | + ?? throw new GridifyFilteringException("Invalid expression"); |
| 53 | + } |
| 54 | + catch (GridifyMapperException) |
| 55 | + { |
| 56 | + if (mapper.Configuration.IgnoreNotMappedFields) |
| 57 | + return (BuildAlwaysTrueQuery(), false); |
49 | 58 |
|
50 | | - if (bExp!.Left is FieldExpressionSyntax && bExp.Right is ValueExpressionSyntax) |
51 | | - try |
52 | | - { |
53 | | - return ConvertBinaryExpressionSyntaxToQuery(bExp) |
54 | | - ?? throw new GridifyFilteringException("Invalid expression"); |
55 | | - } |
56 | | - catch (GridifyMapperException) |
57 | | - { |
58 | | - if (mapper.Configuration.IgnoreNotMappedFields) |
59 | | - return (BuildAlwaysTrueQuery(), false); |
| 59 | + throw; |
| 60 | + } |
60 | 61 |
|
61 | | - throw; |
62 | | - } |
| 62 | + (TQuery query, bool isNested) leftQuery; |
| 63 | + (TQuery query, bool isNested) rightQuery; |
63 | 64 |
|
64 | | - (TQuery query, bool isNested) leftQuery; |
65 | | - (TQuery query, bool isNested) rightQuery; |
| 65 | + if (bExp.Left is ParenthesizedExpressionSyntax lpExp) |
| 66 | + leftQuery = BuildQuery(lpExp.Expression, true); |
| 67 | + else |
| 68 | + leftQuery = BuildQuery(bExp.Left); |
66 | 69 |
|
67 | | - if (bExp.Left is ParenthesizedExpressionSyntax lpExp) |
68 | | - leftQuery = BuildQuery(lpExp.Expression, true); |
69 | | - else |
70 | | - leftQuery = BuildQuery(bExp.Left); |
71 | 70 |
|
| 71 | + if (bExp.Right is ParenthesizedExpressionSyntax rpExp) |
| 72 | + rightQuery = BuildQuery(rpExp.Expression, true); |
| 73 | + else |
| 74 | + rightQuery = BuildQuery(bExp.Right); |
72 | 75 |
|
73 | | - if (bExp.Right is ParenthesizedExpressionSyntax rpExp) |
74 | | - rightQuery = BuildQuery(rpExp.Expression, true); |
75 | | - else |
76 | | - rightQuery = BuildQuery(bExp.Right); |
| 76 | + // check for nested collections |
| 77 | + if (isParenthesisOpen && |
| 78 | + CheckIfCanMergeQueries(leftQuery, rightQuery, bExp.OperatorToken.Kind) is { } mergedResult) |
| 79 | + return (mergedResult, true); |
77 | 80 |
|
78 | | - // check for nested collections |
79 | | - if (isParenthesisOpen && |
80 | | - CheckIfCanMergeQueries(leftQuery, rightQuery, bExp.OperatorToken.Kind) is { } mergedResult) |
81 | | - return (mergedResult, true); |
| 81 | + var result = bExp.OperatorToken.Kind switch |
| 82 | + { |
| 83 | + SyntaxKind.And => CombineWithAndOperator(leftQuery.query, rightQuery.query), |
| 84 | + SyntaxKind.Or => CombineWithOrOperator(leftQuery.query, rightQuery.query), |
| 85 | + _ => throw new GridifyFilteringException($"Invalid expression Operator '{bExp.OperatorToken.Kind}'") |
| 86 | + }; |
| 87 | + return (result, false); |
| 88 | + } |
82 | 89 |
|
83 | | - var result = bExp.OperatorToken.Kind switch |
84 | | - { |
85 | | - SyntaxKind.And => CombineWithAndOperator(leftQuery.query, rightQuery.query), |
86 | | - SyntaxKind.Or => CombineWithOrOperator(leftQuery.query, rightQuery.query), |
87 | | - _ => throw new GridifyFilteringException($"Invalid expression Operator '{bExp.OperatorToken.Kind}'") |
88 | | - }; |
89 | | - return (result, false); |
| 90 | + private (TQuery Query, bool IsNested) BuildQuery(ExpressionSyntax expression, bool isParenthesisOpen = false) |
| 91 | + { |
| 92 | + while (true) |
| 93 | + switch (expression.Kind) |
| 94 | + { |
| 95 | + case SyntaxKind.BinaryExpression: |
| 96 | + { |
| 97 | + var bExp = expression as BinaryExpressionSyntax; |
| 98 | + return BuildQueryFromBinaryExpression(bExp, isParenthesisOpen); |
90 | 99 | } |
91 | 100 | case SyntaxKind.ParenthesizedExpression: // first entrypoint only |
92 | 101 | { |
@@ -129,10 +138,69 @@ public TQuery Build(ExpressionSyntax expression) |
129 | 138 |
|
130 | 139 | if (hasIndexer) |
131 | 140 | query = AddIndexerNullCheck(mapTarget, query); |
| 141 | + else |
| 142 | + query = AddNullPropagator(mapTarget, query); |
132 | 143 |
|
133 | 144 | return ((TQuery)query, false); |
134 | 145 | } |
135 | 146 |
|
| 147 | + |
| 148 | + private object AddNullPropagator(LambdaExpression mapTarget, object query) |
| 149 | + { |
| 150 | + if (!mapper.Configuration.AvoidNullReference) |
| 151 | + return query; |
| 152 | + |
| 153 | + var mainQuery = query as LambdaExpression; |
| 154 | + if (mainQuery == null) |
| 155 | + return query; |
| 156 | + |
| 157 | + var nullPropagatedExpression = BaseQueryBuilder.MemberNullPropagatorVisitor.Visit(mapTarget) as LambdaExpression; |
| 158 | + if (mainQuery.Body is MethodCallExpression methodExp |
| 159 | + && nullPropagatedExpression!.Body.Type != typeof(object) |
| 160 | + && methodExp.Arguments.Count == 1) |
| 161 | + { |
| 162 | + var call = Expression.Call(methodExp.Method!, nullPropagatedExpression!.Body); |
| 163 | + return Expression.Lambda(call, mainQuery.Parameters); |
| 164 | + } |
| 165 | + |
| 166 | + var body = mainQuery.Body; |
| 167 | + if (body is not BinaryExpression bExp) |
| 168 | + return query; |
| 169 | + |
| 170 | + if (bExp.Method == null |
| 171 | + || nullPropagatedExpression!.Body.Type == typeof(object) |
| 172 | + || nullPropagatedExpression.Body is MethodCallExpression) |
| 173 | + { |
| 174 | + return query; |
| 175 | + } |
| 176 | + var newExp = Expression.Call(bExp.Method!, nullPropagatedExpression.Body, bExp.Right); |
| 177 | + return Expression.Lambda(newExp, mainQuery.Parameters); |
| 178 | + } |
| 179 | + |
| 180 | + private object AddNullPropagator2(LambdaExpression mapTarget, object query) |
| 181 | + { |
| 182 | + if (!mapper.Configuration.AvoidNullReference) |
| 183 | + return query; |
| 184 | + |
| 185 | + var mainQuery = query as LambdaExpression; |
| 186 | + if (mainQuery == null) |
| 187 | + return query; |
| 188 | + |
| 189 | + var body = mainQuery.Body; |
| 190 | + if (body is not BinaryExpression bExp) |
| 191 | + return query; |
| 192 | + |
| 193 | + var nullPropagatedExpression = new MemberNullPropogationVisitor().Visit(mapTarget) as LambdaExpression; |
| 194 | + if (bExp.Method == null |
| 195 | + || nullPropagatedExpression.Body.Type == typeof(object) |
| 196 | + || nullPropagatedExpression.Body is MethodCallExpression) |
| 197 | + { |
| 198 | + return query; |
| 199 | + } |
| 200 | + var newExp = Expression.Call(bExp.Method!, nullPropagatedExpression.Body, bExp.Right); |
| 201 | + return Expression.Lambda(newExp, mainQuery.Parameters); |
| 202 | + } |
| 203 | + |
136 | 204 | private object AddIndexerNullCheck(LambdaExpression mapTarget, object query) |
137 | 205 | { |
138 | 206 | if (mapper.Configuration.DisableCollectionNullChecks || mapper.Configuration.EntityFrameworkCompatibilityLayer) |
|
0 commit comments