@@ -14,15 +14,24 @@ public class JetStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactor
1414 {
1515 private readonly JetSqlExpressionFactory _sqlExpressionFactory = ( JetSqlExpressionFactory ) sqlExpressionFactory ;
1616
17- private static readonly MethodInfo IndexOfMethodInfo
17+ private static readonly MethodInfo IndexOfMethodInfoString
1818 = typeof ( string ) . GetRuntimeMethod ( nameof ( string . IndexOf ) , [ typeof ( string ) ] ) ! ;
1919
20- private static readonly MethodInfo IndexOfMethodInfoWithStartingPosition
20+ private static readonly MethodInfo IndexOfMethodInfoChar
21+ = typeof ( string ) . GetRuntimeMethod ( nameof ( string . IndexOf ) , [ typeof ( char ) ] ) ! ;
22+
23+ private static readonly MethodInfo IndexOfMethodInfoWithStartingPositionString
2124 = typeof ( string ) . GetRuntimeMethod ( nameof ( string . IndexOf ) , [ typeof ( string ) , typeof ( int ) ] ) ! ;
2225
23- private static readonly MethodInfo _replaceMethodInfo
26+ private static readonly MethodInfo IndexOfMethodInfoWithStartingPositionChar
27+ = typeof ( string ) . GetRuntimeMethod ( nameof ( string . IndexOf ) , [ typeof ( char ) , typeof ( int ) ] ) ! ;
28+
29+ private static readonly MethodInfo ReplaceMethodInfoString
2430 = typeof ( string ) . GetRuntimeMethod ( nameof ( string . Replace ) , [ typeof ( string ) , typeof ( string ) ] ) ! ;
2531
32+ private static readonly MethodInfo ReplaceMethodInfoChar
33+ = typeof ( string ) . GetRuntimeMethod ( nameof ( string . Replace ) , [ typeof ( char ) , typeof ( char ) ] ) ! ;
34+
2635 private static readonly MethodInfo _toLowerMethodInfo
2736 = typeof ( string ) . GetRuntimeMethod ( nameof ( string . ToLower ) , Type . EmptyTypes ) ! ;
2837
@@ -76,16 +85,36 @@ private static readonly MethodInfo _lastOrDefaultMethodInfoWithoutArgs
7685 {
7786 if ( instance != null )
7887 {
79- if ( IndexOfMethodInfo . Equals ( method ) )
88+ if ( IndexOfMethodInfoString . Equals ( method ) || IndexOfMethodInfoChar . Equals ( method ) )
8089 {
8190 return TranslateIndexOf ( instance , method , arguments [ 0 ] , null ) ;
8291 }
8392
84- if ( IndexOfMethodInfoWithStartingPosition . Equals ( method ) )
93+ if ( IndexOfMethodInfoWithStartingPositionString . Equals ( method ) || IndexOfMethodInfoWithStartingPositionChar . Equals ( method ) )
8594 {
8695 return TranslateIndexOf ( instance , method , arguments [ 0 ] , arguments [ 1 ] ) ;
8796 }
8897
98+ if ( ReplaceMethodInfoString . Equals ( method ) || ReplaceMethodInfoChar . Equals ( method ) )
99+ {
100+ var firstArgument = arguments [ 0 ] ;
101+ var secondArgument = arguments [ 1 ] ;
102+ var stringTypeMapping =
103+ ExpressionExtensions . InferTypeMapping ( instance , firstArgument , secondArgument ) ;
104+
105+ instance = _sqlExpressionFactory . ApplyTypeMapping ( instance , stringTypeMapping ) ;
106+ firstArgument = _sqlExpressionFactory . ApplyTypeMapping ( firstArgument , firstArgument . Type == typeof ( char ) ? CharTypeMapping . Default : stringTypeMapping ) ;
107+ secondArgument = _sqlExpressionFactory . ApplyTypeMapping ( secondArgument , secondArgument . Type == typeof ( char ) ? CharTypeMapping . Default : stringTypeMapping ) ;
108+
109+ return _sqlExpressionFactory . Function (
110+ "REPLACE" ,
111+ [ instance , firstArgument , secondArgument ] ,
112+ nullable : true ,
113+ argumentsPropagateNullability : [ true , true , true ] ,
114+ method . ReturnType ,
115+ stringTypeMapping ) ;
116+ }
117+
89118 // Jet TRIM does not take arguments.
90119 // _trimWithNoParam is only available since .NET Core 2.0 (or .NET Standard 2.1).
91120 if ( Equals ( method , _trimMethodInfoWithoutArgs ) ||
@@ -169,26 +198,6 @@ private static readonly MethodInfo _lastOrDefaultMethodInfoWithoutArgs
169198 method . ReturnType ,
170199 instance . TypeMapping ) ;
171200 }
172-
173- if ( _replaceMethodInfo . Equals ( method ) )
174- {
175- var firstArgument = arguments [ 0 ] ;
176- var secondArgument = arguments [ 1 ] ;
177- var stringTypeMapping =
178- ExpressionExtensions . InferTypeMapping ( instance , firstArgument , secondArgument ) ;
179-
180- instance = _sqlExpressionFactory . ApplyTypeMapping ( instance , stringTypeMapping ) ;
181- firstArgument = _sqlExpressionFactory . ApplyTypeMapping ( firstArgument , stringTypeMapping ) ;
182- secondArgument = _sqlExpressionFactory . ApplyTypeMapping ( secondArgument , stringTypeMapping ) ;
183-
184- return _sqlExpressionFactory . Function (
185- "REPLACE" ,
186- [ instance , firstArgument , secondArgument ] ,
187- nullable : true ,
188- argumentsPropagateNullability : [ true , true , true ] ,
189- method . ReturnType ,
190- stringTypeMapping ) ;
191- }
192201 }
193202
194203 if ( _isNullOrEmptyMethodInfo . Equals ( method ) )
@@ -267,7 +276,7 @@ private SqlExpression TranslateIndexOf(
267276 SqlExpression ? startIndex )
268277 {
269278 var stringTypeMapping = ExpressionExtensions . InferTypeMapping ( instance , searchExpression ) ! ;
270- searchExpression = _sqlExpressionFactory . ApplyTypeMapping ( searchExpression , stringTypeMapping ) ;
279+ searchExpression = _sqlExpressionFactory . ApplyTypeMapping ( searchExpression , searchExpression . Type == typeof ( char ) ? CharTypeMapping . Default : stringTypeMapping ) ;
271280 instance = _sqlExpressionFactory . ApplyTypeMapping ( instance , stringTypeMapping ) ;
272281
273282 var charIndexArguments = new List < SqlExpression > { instance , searchExpression } ;
0 commit comments