|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Text; |
| 9 | +using Microsoft.EntityFrameworkCore.Query; |
| 10 | +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| 11 | +using Microsoft.EntityFrameworkCore.Storage; |
| 12 | + |
| 13 | +namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal |
| 14 | +{ |
| 15 | + public class SqlServerByteArrayMethodTranslator : IMethodCallTranslator |
| 16 | + { |
| 17 | + private readonly ISqlExpressionFactory _sqlExpressionFactory; |
| 18 | + |
| 19 | + public SqlServerByteArrayMethodTranslator(ISqlExpressionFactory sqlExpressionFactory) |
| 20 | + { |
| 21 | + _sqlExpressionFactory = sqlExpressionFactory; |
| 22 | + } |
| 23 | + |
| 24 | + public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments) |
| 25 | + { |
| 26 | + if (method.IsGenericMethod |
| 27 | + && method.GetGenericMethodDefinition().Equals(EnumerableMethods.Contains) |
| 28 | + && arguments[0].Type == typeof(byte[])) |
| 29 | + { |
| 30 | + instance = arguments[0]; |
| 31 | + var typeMapping = instance.TypeMapping; |
| 32 | + |
| 33 | + var pattern = arguments[1] is SqlConstantExpression constantExpression |
| 34 | + ? (SqlExpression)_sqlExpressionFactory.Constant(new[] { (byte)constantExpression.Value }, typeMapping) |
| 35 | + : _sqlExpressionFactory.Convert(arguments[1], typeof(byte[]), typeMapping); |
| 36 | + |
| 37 | + return _sqlExpressionFactory.GreaterThan( |
| 38 | + _sqlExpressionFactory.Function( |
| 39 | + "CHARINDEX", |
| 40 | + new[] { pattern, instance }, |
| 41 | + typeof(int)), |
| 42 | + _sqlExpressionFactory.Constant(0)); |
| 43 | + } |
| 44 | + |
| 45 | + return null; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments