Add translation for string methods with char arguments#3690
Add translation for string methods with char arguments#3690ChrisJollyAU wants to merge 1 commit intonpgsql:mainfrom
Conversation
Enable SQL translation for string.IndexOf, Replace, StartsWith, EndsWith, and Contains when called with char arguments. Update translators and type mapping to support char overloads, and implement corresponding tests to verify correct SQL generation.
There was a problem hiding this comment.
Pull request overview
This pull request enables SQL translation for string methods (IndexOf, Replace, StartsWith, EndsWith, and Contains) when called with char arguments, addressing issue #3547. The changes add support for char overloads in both the expression visitor and string method translator, implementing proper type mapping and LIKE pattern generation for char parameters.
Key Changes:
- Added char overload method references for
StartsWith,EndsWith, andContainsinNpgsqlSqlTranslatingExpressionVisitor - Implemented char argument handling in LIKE pattern generation with proper wildcard escaping
- Updated
NpgsqlStringMethodTranslatorto supportIndexOfandReplacewith char arguments - Converted 7 test methods from throwing exceptions to verifying correct SQL generation
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/EFCore.PG/Query/Internal/NpgsqlSqlTranslatingExpressionVisitor.cs |
Added char overload method references and implemented char handling in LIKE pattern generation for StartsWith/EndsWith/Contains with wildcard escaping support |
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs |
Added Replace_Char method reference and updated IndexOf/Replace translators to handle char arguments with type mapping |
test/EFCore.PG.FunctionalTests/Query/Translations/StringTranslationsNpgsqlTest.cs |
Updated 7 test methods (IndexOf_Char, Replace_Char, StartsWith_Literal_Char, StartsWith_Parameter_Char, EndsWith_Literal_Char, EndsWith_Parameter_Char, Contains_Literal_Char) from exception-throwing to proper SQL assertion verification |
Comments suppressed due to low confidence (2)
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs:216
- The char type mapping applied on line 210 is immediately overwritten on line 216 when the argument is reapplied with stringTypeMapping. This means char arguments will not have the correct type mapping. The assignment on line 210 should be removed, and the condition should be applied directly in the function call on line 216 instead.
argument = _sqlExpressionFactory.ApplyTypeMapping(argument, argument.Type == typeof(char) ? CharTypeMapping.Default : stringTypeMapping);
return _sqlExpressionFactory.Subtract(
_sqlExpressionFactory.Function(
"strpos",
[
_sqlExpressionFactory.ApplyTypeMapping(instance!, stringTypeMapping),
_sqlExpressionFactory.ApplyTypeMapping(argument, stringTypeMapping)
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs:238
- The char type mappings applied on lines 230-231 are immediately overwritten on lines 237-238 when the arguments are reapplied with stringTypeMapping. This means char arguments will not have the correct type mapping. The assignments on lines 230-231 should be removed, and the conditions should be applied directly in the function call on lines 237-238 instead.
oldValue = _sqlExpressionFactory.ApplyTypeMapping(oldValue, oldValue.Type == typeof(char) ? CharTypeMapping.Default : stringTypeMapping);
newValue = _sqlExpressionFactory.ApplyTypeMapping(newValue, newValue.Type == typeof(char) ? CharTypeMapping.Default : stringTypeMapping);
return _sqlExpressionFactory.Function(
"replace",
[
_sqlExpressionFactory.ApplyTypeMapping(instance!, stringTypeMapping),
_sqlExpressionFactory.ApplyTypeMapping(oldValue, stringTypeMapping),
_sqlExpressionFactory.ApplyTypeMapping(newValue, stringTypeMapping)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Enable SQL translation for string.IndexOf, Replace, StartsWith, EndsWith, and Contains when called with char arguments. Update translators and type mapping to support char overloads, and implement corresponding tests to verify correct SQL generation.
Fixes #3547