@@ -19,8 +19,6 @@ public class CosmosRegexTranslator : IMethodCallTranslator
1919 private static readonly MethodInfo IsMatchWithRegexOptions =
2020 typeof ( Regex ) . GetRuntimeMethod ( nameof ( Regex . IsMatch ) , new [ ] { typeof ( string ) , typeof ( string ) , typeof ( RegexOptions ) } ) ! ;
2121
22- private const RegexOptions SupportedOptions = RegexOptions . Multiline | RegexOptions . Singleline | RegexOptions . IgnoreCase | RegexOptions . IgnorePatternWhitespace ;
23-
2422 private readonly ISqlExpressionFactory _sqlExpressionFactory ;
2523
2624 /// <summary>
@@ -53,48 +51,49 @@ public CosmosRegexTranslator(ISqlExpressionFactory sqlExpressionFactory)
5351
5452 var ( input , pattern ) = ( arguments [ 0 ] , arguments [ 1 ] ) ;
5553 var typeMapping = ExpressionExtensions . InferTypeMapping ( input , pattern ) ;
54+ ( input , pattern ) = (
55+ _sqlExpressionFactory . ApplyTypeMapping ( input , typeMapping ) ,
56+ _sqlExpressionFactory . ApplyTypeMapping ( pattern , typeMapping ) ) ;
5657
57- if ( method == IsMatch )
58+ if ( method == IsMatch || arguments [ 2 ] is SqlConstantExpression { Value : RegexOptions . None } )
5859 {
59- return _sqlExpressionFactory . Function (
60- "RegexMatch" ,
61- new [ ] {
62- _sqlExpressionFactory . ApplyTypeMapping ( input , typeMapping ) ,
63- _sqlExpressionFactory . ApplyTypeMapping ( pattern , typeMapping )
64- } ,
65- typeof ( bool ) ) ;
60+ return _sqlExpressionFactory . Function ( "RegexMatch" , new [ ] { input , pattern } , typeof ( bool ) ) ;
6661 }
67- else if ( arguments [ 2 ] is SqlConstantExpression { Value : RegexOptions regexOptions } )
62+
63+ if ( arguments [ 2 ] is SqlConstantExpression { Value : RegexOptions regexOptions } )
6864 {
69- string modifier = "" ;
65+ var modifier = "" ;
66+
7067 if ( regexOptions . HasFlag ( RegexOptions . Multiline ) )
7168 {
69+ regexOptions &= ~ RegexOptions . Multiline ;
7270 modifier += "m" ;
7371 }
72+
7473 if ( regexOptions . HasFlag ( RegexOptions . Singleline ) )
7574 {
75+ regexOptions &= ~ RegexOptions . Singleline ;
7676 modifier += "s" ;
7777 }
78+
7879 if ( regexOptions . HasFlag ( RegexOptions . IgnoreCase ) )
7980 {
81+ regexOptions &= ~ RegexOptions . IgnoreCase ;
8082 modifier += "i" ;
8183 }
84+
8285 if ( regexOptions . HasFlag ( RegexOptions . IgnorePatternWhitespace ) )
8386 {
87+ regexOptions &= ~ RegexOptions . IgnorePatternWhitespace ;
8488 modifier += "x" ;
8589 }
8690
87- return ( regexOptions & ~ SupportedOptions ) == 0
91+ return regexOptions == 0
8892 ? _sqlExpressionFactory . Function (
8993 "RegexMatch" ,
90- new [ ]
91- {
92- _sqlExpressionFactory . ApplyTypeMapping ( input , typeMapping ) ,
93- _sqlExpressionFactory . ApplyTypeMapping ( pattern , typeMapping ) ,
94- _sqlExpressionFactory . Constant ( modifier )
95- } ,
94+ new [ ] { input , pattern , _sqlExpressionFactory . Constant ( modifier ) } ,
9695 typeof ( bool ) )
97- : null ;
96+ : null ; // TODO: Report unsupported RegexOption, #26410
9897 }
9998
10099 return null ;
0 commit comments