Skip to content

Commit

Permalink
Fixed errors when using LIKE queries on non-string fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Dec 22, 2021
1 parent deb85d2 commit b0bcff2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/BaseDataNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ private bool TranslateFetchXMLCriteriaWithVirtualAttributes(EntityMetadata meta,
}
}

// Can't fold LIKE queries for non-string fields - the server will try to convert the value to the type of
// the attribute (e.g. integer) and throw an exception.
if (attribute != null && attributeSuffix == null && (op == @operator.like || op == @operator.notlike) && !(attribute.AttributeType == AttributeTypeCode.String || attribute.AttributeType == AttributeTypeCode.Memo))
return false;

var value = literals == null ? null : literals.Length == 1 ? literals[0] is Literal l ? l.Value : literals[0] is VariableReference v ? v.Name : null : null;
var values = literals == null ? null : literals.Select(lit => new conditionValue { Value = lit is Literal lit1 ? lit1.Value : lit is VariableReference var1 ? var1.Name : null }).ToArray();
var entityAliases = new[] { entityAlias };
Expand All @@ -635,7 +640,7 @@ private bool TranslateFetchXMLCriteriaWithVirtualAttributes(EntityMetadata meta,
var usesItems = values != null && values.Length > 1 || op == @operator.@in || op == @operator.notin || op == @operator.containvalues || op == @operator.notcontainvalues;

if (attribute is DateTimeAttributeMetadata && literals != null &&
(op == @operator.eq || op == @operator.ne || op == @operator.neq || op == @operator.gt || op == @operator.ge || op == @operator.lt || op == @operator.le))
(op == @operator.eq || op == @operator.ne || op == @operator.neq || op == @operator.gt || op == @operator.ge || op == @operator.lt || op == @operator.le || op == @operator.@in || op == @operator.notin))
{
for (var i = 0; i < literals.Length; i++)
{
Expand Down
1 change: 1 addition & 0 deletions MarkMpn.Sql4Cds.Engine/MarkMpn.Sql4Cds.Engine.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Reduced attributes retrieved to process COUNT(*) queries
Fixed GROUP BY and aggregates on virtual attributes
Fixed retrieving file attributes
Fixed GROUP BY and ORDER BY on multi-select picklist fields
Fixed errors when using LIKE queries on non-string fields
</releaseNotes>
<copyright>Copyright © 2020 Mark Carrington</copyright>
<language>en-GB</language>
Expand Down
1 change: 1 addition & 0 deletions MarkMpn.Sql4Cds/MarkMpn.SQL4CDS.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Fixed GROUP BY and aggregates on virtual attributes
Fixed retrieving file attributes
Fixed using quoted identifiers
Fixed GROUP BY and ORDER BY on multi-select picklist fields
Fixed errors when using LIKE queries on non-string fields
</releaseNotes>
<copyright>Copyright © 2019 Mark Carrington</copyright>
<language>en-GB</language>
Expand Down

0 comments on commit b0bcff2

Please sign in to comment.