|
| 1 | +using JetBrains.Annotations; |
| 2 | +using Microsoft.EntityFrameworkCore.Diagnostics; |
| 3 | +using Microsoft.EntityFrameworkCore; |
| 4 | +using Microsoft.EntityFrameworkCore.Query; |
| 5 | +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| 6 | +using Microsoft.EntityFrameworkCore.Sqlite.Query.Internal; |
| 7 | +using System.Reflection; |
| 8 | +using Volo.Abp; |
| 9 | + |
| 10 | +namespace CmsKitDemo.Data |
| 11 | +{ |
| 12 | + public class CmsKitDemoSqliteMethodCallTranslatorProvider : SqliteMethodCallTranslatorProvider |
| 13 | + { |
| 14 | + public CmsKitDemoSqliteMethodCallTranslatorProvider([NotNull] RelationalMethodCallTranslatorProviderDependencies dependencies) |
| 15 | + : base(dependencies) |
| 16 | + { |
| 17 | + var sqlExpressionFactory = dependencies.SqlExpressionFactory; |
| 18 | + |
| 19 | + AddTranslators( |
| 20 | + new IMethodCallTranslator[] |
| 21 | + { |
| 22 | + new SqliteMathTranslator(sqlExpressionFactory), |
| 23 | + new SqliteDateTimeAddTranslator(sqlExpressionFactory.As<SqliteSqlExpressionFactory>()), |
| 24 | + new CmsKitDemoSqliteStringMethodTranslator(sqlExpressionFactory) |
| 25 | + }); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public class CmsKitDemoSqliteStringMethodTranslator : SqliteStringMethodTranslator |
| 30 | + { |
| 31 | + private static readonly MethodInfo _containsMethodInfo |
| 32 | + = typeof(string).GetRuntimeMethod(nameof(string.Contains), new[] { typeof(string) }); |
| 33 | + |
| 34 | + private readonly ISqlExpressionFactory _sqlExpressionFactory; |
| 35 | + |
| 36 | + public CmsKitDemoSqliteStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactory) : base(sqlExpressionFactory) |
| 37 | + { |
| 38 | + _sqlExpressionFactory = sqlExpressionFactory; |
| 39 | + } |
| 40 | + |
| 41 | + public override SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments, IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 42 | + { |
| 43 | + Check.NotNull(method, nameof(method)); |
| 44 | + Check.NotNull(arguments, nameof(arguments)); |
| 45 | + |
| 46 | + if (_containsMethodInfo.Equals(method)) |
| 47 | + { |
| 48 | + var pattern = arguments[0]; |
| 49 | + var stringTypeMapping = ExpressionExtensions.InferTypeMapping(instance, pattern); |
| 50 | + |
| 51 | + instance = _sqlExpressionFactory.ApplyTypeMapping(instance, stringTypeMapping); |
| 52 | + pattern = _sqlExpressionFactory.ApplyTypeMapping(pattern, stringTypeMapping); |
| 53 | + |
| 54 | + // this basically changes query to "instr(upper(left_expression), upper(right_expression))" |
| 55 | + return _sqlExpressionFactory.OrElse( |
| 56 | + _sqlExpressionFactory.Equal( |
| 57 | + pattern, |
| 58 | + _sqlExpressionFactory.Constant(string.Empty, stringTypeMapping)), |
| 59 | + _sqlExpressionFactory.GreaterThan( |
| 60 | + _sqlExpressionFactory.Function( |
| 61 | + "instr", |
| 62 | + new[] |
| 63 | + { |
| 64 | + _sqlExpressionFactory.Function("upper", new[]{instance}, false, new []{ false }, typeof(string)), |
| 65 | + _sqlExpressionFactory.Function("upper", new[]{pattern}, false, new []{ false }, typeof(string)) |
| 66 | + }, |
| 67 | + false, |
| 68 | + new[] { false, false }, |
| 69 | + typeof(int)), _sqlExpressionFactory.Constant(0))); |
| 70 | + } |
| 71 | + |
| 72 | + return base.Translate(instance, method, arguments, logger); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments