-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Contains on byte arrays on SqlServer #19071
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
public class SqlServerByteArrayMethodTranslator : IMethodCallTranslator | ||
{ | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
public SqlServerByteArrayMethodTranslator(ISqlExpressionFactory sqlExpressionFactory) | ||
{ | ||
_sqlExpressionFactory = sqlExpressionFactory; | ||
} | ||
|
||
public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments) | ||
{ | ||
if (method.IsGenericMethod | ||
&& method.GetGenericMethodDefinition().Equals(EnumerableMethods.Contains) | ||
&& arguments[0].Type == typeof(byte[])) | ||
{ | ||
instance = arguments[0]; | ||
var typeMapping = instance.TypeMapping; | ||
|
||
var pattern = arguments[1] is SqlConstantExpression constantExpression | ||
? (SqlExpression)_sqlExpressionFactory.Constant(new[] { (byte)constantExpression.Value }, typeMapping) | ||
: _sqlExpressionFactory.Convert(arguments[1], typeof(byte[]), typeMapping); | ||
|
||
return _sqlExpressionFactory.GreaterThan( | ||
_sqlExpressionFactory.Function( | ||
"CHARINDEX", | ||
new[] { pattern, instance }, | ||
typeof(int)), | ||
_sqlExpressionFactory.Constant(0)); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,10 @@ public override async Task Select_datetimeoffset_comparison_in_projection(bool a | |
FROM ""Missions"" AS ""m"""); | ||
} | ||
|
||
public override Task Byte_array_contains_literal(bool async) => null; | ||
|
||
public override Task Byte_array_contains_parameter(bool async) => null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sqlite does have INSTR which works great for literal, but I couldn't find a way to cast/convert a non-literal byte into an Sqlite BLOB. Maybe @bricelam knows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SELECT *
FROM Squads
WHERE instr(Banner, char(@someByte)) > 0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, that converts Banner to TEXT, but the result is the same. Note, without char(), someByte is converted from integer to blob which results in an 8-byte blob. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do this. |
||
|
||
private void AssertSql(params string[] expected) | ||
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't assign to instance. Using parameters of a function to store local variable causes confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be even more confusing to have a two instance variables, one "bad" and one "good".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't call it instance. Just call it firstArgument.