@@ -14,20 +14,21 @@ string ISearchFormatter.Format(Expression<Func<AdvancedSearchFilter, bool>> sear
14
14
{
15
15
Expression expression = null ;
16
16
Stack < Expression > expressionStack = new Stack < Expression > ( ) ;
17
+ Stack < FormatInfo > formatInfoStack = new Stack < FormatInfo > ( ) ;
17
18
expressionStack . Push ( search . Body ) ;
18
19
Stack < string > searchStack = new Stack < string > ( ) ;
19
20
while ( expressionStack . Count > 0 ) {
20
21
expression = expressionStack . Pop ( ) ;
21
22
switch ( expression )
22
23
{
23
24
case MemberExpression memberExpression :
24
- searchStack . Push ( MemberExpressionHelper ( memberExpression ) ) ;
25
+ MemberExpressionHelper ( memberExpression , searchStack , formatInfoStack ) ;
25
26
break ;
26
27
case UnaryExpression unaryExpression :
27
- searchStack . Push ( UnaryExpressionHelper ( unaryExpression , expressionStack ) ) ;
28
+ UnaryExpressionHelper ( unaryExpression , expressionStack , formatInfoStack ) ;
28
29
break ;
29
30
case BinaryExpression binaryExpression :
30
- BinaryExpressionHelper ( binaryExpression , expressionStack , searchStack ) ;
31
+ BinaryExpressionHelper ( binaryExpression , expressionStack , formatInfoStack ) ;
31
32
break ;
32
33
case ConstantExpression constantExpresssion :
33
34
searchStack . Push ( ConstantExpressionHelper ( constantExpresssion ) ) ;
@@ -37,20 +38,32 @@ string ISearchFormatter.Format(Expression<Func<AdvancedSearchFilter, bool>> sear
37
38
}
38
39
}
39
40
40
- string searchQuery = searchStack . Pop ( ) ;
41
- while ( searchStack . Count > 0 )
41
+ string searchQuery = string . Empty ;
42
+ Stack < string > compoundSearchStack = new Stack < string > ( ) ;
43
+ while ( formatInfoStack . Count > 0 )
42
44
{
43
- searchQuery = string . Format ( searchStack . Pop ( ) , searchQuery ) ;
45
+ FormatInfo current = formatInfoStack . Pop ( ) ;
46
+ string [ ] formatParameters = new string [ current . ParameterCount ] ;
47
+ int currentCount = current . ParameterCount ;
48
+ while ( currentCount > 0 )
49
+ {
50
+ formatParameters [ formatParameters . Length - currentCount ] = current . IsCompound ? compoundSearchStack . Pop ( ) : searchStack . Pop ( ) ;
51
+ currentCount -- ;
52
+ }
53
+
54
+ compoundSearchStack . Push ( string . Format ( current . Pattern , formatParameters ) ) ;
55
+
44
56
}
45
- return searchQuery ;
57
+
58
+ return compoundSearchStack . Pop ( ) ;
46
59
}
47
60
48
61
private string ConstantExpressionHelper ( ConstantExpression constantExpresssion )
49
62
{
50
63
return constantExpresssion . ToString ( ) . Replace ( "\" " , "" ) ;
51
64
}
52
65
53
- private void BinaryExpressionHelper ( BinaryExpression expression , Stack < Expression > expressionStack , Stack < string > searchStack )
66
+ private void BinaryExpressionHelper ( BinaryExpression expression , Stack < Expression > expressionStack , Stack < FormatInfo > formatInfoStack )
54
67
{
55
68
if ( IsAdvancedSearchMemberExpression ( expression . Left ) && IsAdvancedSearchMemberExpression ( expression . Right ) )
56
69
{
@@ -70,40 +83,37 @@ private void BinaryExpressionHelper(BinaryExpression expression, Stack<Expressio
70
83
71
84
if ( expression . NodeType != ExpressionType . Equal )
72
85
{
73
- searchStack . Push ( expression . ToOperator ( ) ) ;
86
+ formatInfoStack . Push ( expression . ToFormatInfo ( ) ) ;
74
87
//searchStack.Push("NOT(+{0}+)");
75
88
}
76
89
77
90
78
91
}
79
92
80
93
81
- private string UnaryExpressionHelper ( UnaryExpression expression , Stack < Expression > expressionStack )
94
+ private void UnaryExpressionHelper ( UnaryExpression expression , Stack < Expression > expressionStack , Stack < FormatInfo > formatInfoStack )
82
95
{
83
- string expressionOperator = expression . ToOperator ( ) ;
96
+ formatInfoStack . Push ( expression . ToFormatInfo ( ) ) ;
84
97
expressionStack . Push ( expression . Operand ) ;
85
- return expressionOperator ;
98
+ // return expressionOperator;
86
99
}
87
100
88
- private string MemberExpressionHelper ( MemberExpression expression )
101
+ private void MemberExpressionHelper ( MemberExpression expression , Stack < string > searchStack , Stack < FormatInfo > formatInfoStack )
89
102
{
90
103
MemberInfo member = expression . Member ;
91
- string result = string . Empty ;
104
+
92
105
93
106
if ( member . DeclaringType == typeof ( AdvancedSearchFilter ) )
94
107
{
95
- result = member . Name . Replace ( BOOL_PROPERTY_PREFIX , string . Empty ) . ToLower ( ) ;
108
+ string result = member . Name . Replace ( BOOL_PROPERTY_PREFIX , string . Empty ) . ToLower ( ) ;
109
+ formatInfoStack . Push ( expression . ToFormatInfo ( ) ) ;
110
+ searchStack . Push ( result ) ;
96
111
if ( expression . Type == typeof ( bool ) )
97
112
{
98
- result = result + ":1" ;
113
+ searchStack . Push ( "1" ) ;
114
+
99
115
}
100
- else
101
- {
102
- result = result + ":{0}" ;
103
- }
104
-
105
116
}
106
- return result ;
107
117
}
108
118
109
119
@@ -137,28 +147,48 @@ private static bool IsAdvancedSearchMemberExpression(Expression expression)
137
147
MemberExpression memberExpression = expression as MemberExpression ;
138
148
return memberExpression ? . Member . DeclaringType == typeof ( AdvancedSearchFilter ) ;
139
149
}
150
+
151
+ internal class FormatInfo
152
+ {
153
+ public string Pattern { get ; private set ; }
154
+ public int ParameterCount { get ; private set ; }
155
+ public bool IsCompound { get ; private set ; }
156
+
157
+ public FormatInfo ( string pattern , int parameterCount = 0 , bool isCompound = false )
158
+ {
159
+ Pattern = pattern ;
160
+ ParameterCount = parameterCount ;
161
+ IsCompound = isCompound ;
162
+ }
163
+
164
+ internal static FormatInfo Not = new FormatInfo ( "NOT(+{0}+)" , 1 , true ) ;
165
+ internal static FormatInfo NotEqual = Not ;
166
+ internal static FormatInfo AndAlso = new FormatInfo ( "(+{0}+AND+{1}+)" , 2 , true ) ;
167
+ internal static FormatInfo OrElse = new FormatInfo ( "(+{0}+OR+{1}+)" , 2 , true ) ;
168
+ internal static FormatInfo MemberAccess = new FormatInfo ( "{1}:{0}" , 2 ) ;
169
+ }
140
170
}
141
171
142
172
public static class Extensions
143
173
{
144
- public static string ToOperator ( this Expression expression )
174
+ internal static DefaultSearchFormatter . FormatInfo ToFormatInfo ( this Expression expression )
145
175
{
146
176
ExpressionType ? type = expression ? . NodeType ;
147
- string result = string . Empty ;
148
177
switch ( type )
149
178
{
150
179
case ExpressionType . Not :
151
180
case ExpressionType . NotEqual :
152
- result = "NOT(+{0}+)" ;
153
- break ;
181
+ return DefaultSearchFormatter . FormatInfo . Not ;
154
182
case ExpressionType . Equal :
155
- result = ":" ;
156
- break ;
183
+ throw new NotImplementedException ( "Currently not supporting Equal expression." ) ;
157
184
case ExpressionType . AndAlso :
158
- result = "(+{0}+AND)" ;
159
- break ;
185
+ return DefaultSearchFormatter . FormatInfo . AndAlso ;
186
+ case ExpressionType . MemberAccess :
187
+ return DefaultSearchFormatter . FormatInfo . MemberAccess ;
188
+ case ExpressionType . OrElse :
189
+ return DefaultSearchFormatter . FormatInfo . OrElse ;
160
190
}
161
- return result ;
191
+ throw new NotImplementedException ( $ " { type . ToString ( ) } is not implemented." ) ;
162
192
}
163
193
164
194
}
0 commit comments