Repository navigation
Fix {Starts,Ends}With and support citext properly - #1147
Conversation
e0dd19e to
02318f6
Compare
| typeof(int)), | ||
| _sqlExpressionFactory.Constant(0)); | ||
|
|
||
| if (pattern is SqlConstantExpression constantPattern) |
There was a problem hiding this comment.
Check for a constant pattern earlier and do not allocate an STRPOS expression.
There was a problem hiding this comment.
Unless I'm missing something that would require checking for constant twice, or duplicating the code for constructing the STRPOS expression; and the only advantage would be to save a single expression construction during compilation when the pattern is an empty string (an edge case if there ever was one...). So I think it's better to keep the code simple as it is...
| : strposCheck; | ||
| } | ||
|
|
||
| return _sqlExpressionFactory.OrElse( |
There was a problem hiding this comment.
And check the pattern for null here:
var pattern = arguments[0];
var constantPattern = pattern as SqlConstantExpression;
if (constantPattern != null && (string)constantPattern.Value == string.Empty)
return _sqlExpressionFactory.Constant(true);
// ...
return constantPattern is null
? _sqlExpressionFactory.OrElse(
_sqlExpressionFactory.Equal(
pattern,
_sqlExpressionFactory.Constant(string.Empty, stringTypeMapping)),
strposCheck)
: strposCheck;There was a problem hiding this comment.
Right, this checks constantPattern for null twice... I think it's cleaner/more understandable as-is, where you have one block handling the constant case and another the non-constance.
There was a problem hiding this comment.
Without double check this introduces a few more useless allocations, while a null check is very cheap action and it won't hurt performance. If you think that constant patterns isn't a common case, feel free to leave it as is.
| : _sqlExpressionFactory.Convert( | ||
| pattern, | ||
| typeof(string), | ||
| pattern.TypeMapping)); |
There was a problem hiding this comment.
These two parameters could be generated via two nested methods, and you won't need to duplicate the code.
There was a problem hiding this comment.
Is it really worth it? I like nested methods for more complicated cases, but here it seems more readable to just keep the code inline, so that everything is clear when reading and you don't have to jump to another function...
There was a problem hiding this comment.
It's worth at least for conversion to citext. It minimizes a chance to make a bug or a fix in the code and not to affect another place.
There was a problem hiding this comment.
This isn't just about conversion to citext, it could be another text type as well (e.g. json). Can you post a sample of exactly what you mean?
IMHO we shouldn't always, everywhere extract duplicated code into methods - methods make it more difficult to follow what's happening (you have to jump around). It's always a case-by-case decision based on how much is being duplicated, etc. etc.
There was a problem hiding this comment.
// Could be used for RIGHT and LEFT
return _sqlExpressionFactory.Equal(
GetInstanceMapping(rightPart),
GetPatternMapping());
SqlExpression GetInstanceMapping(SqlExpression value)
=> instance.TypeMapping == _textTypeMapping
? value
: _sqlExpressionFactory.Convert(value, typeof(string), _textTypeMapping);
// The same method for pattern or make a common method until the bug isn't fixed.There was a problem hiding this comment.
OK, I ended up refactoring in a slightly different way but I think you'll like it - can you take another look?
There was a problem hiding this comment.
Now it looks much better. Let's merge (:
|
@roji Thanks, looks good, tested StringStuff branch, StartsWith and EndsWith both now work and also work using the reverse matching. |
Fixes #1146
Fixes #388
Fixes #996