|
| 1 | +using System.Collections.Immutable; |
| 2 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions; |
| 3 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; |
| 4 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping; |
| 5 | +using static Npgsql.EntityFrameworkCore.PostgreSQL.Utilities.Statics; |
| 6 | + |
| 7 | +namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 11 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 12 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 13 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 14 | +/// </summary> |
| 15 | +public class NpgsqlHstoreTranslator : IMethodCallTranslator, IMemberTranslator |
| 16 | +{ |
| 17 | + private static readonly Type DictionaryType = typeof(Dictionary<string, string>); |
| 18 | + private static readonly Type ImmutableDictionaryType = typeof(ImmutableDictionary<string, string>); |
| 19 | + |
| 20 | + private static readonly MethodInfo Dictionary_ContainsKey = |
| 21 | + DictionaryType.GetMethod(nameof(Dictionary<string, string>.ContainsKey))!; |
| 22 | + |
| 23 | + private static readonly MethodInfo ImmutableDictionary_ContainsKey = |
| 24 | + ImmutableDictionaryType.GetMethod(nameof(ImmutableDictionary<string, string>.ContainsKey))!; |
| 25 | + |
| 26 | + private static readonly MethodInfo Dictionary_ContainsValue = |
| 27 | + DictionaryType.GetMethod(nameof(Dictionary<string, string>.ContainsValue))!; |
| 28 | + |
| 29 | + private static readonly MethodInfo ImmutableDictionary_ContainsValue = |
| 30 | + ImmutableDictionaryType.GetMethod(nameof(ImmutableDictionary<string, string>.ContainsValue))!; |
| 31 | + |
| 32 | + private static readonly MethodInfo Dictionary_Item_Getter = |
| 33 | + DictionaryType.GetProperty("Item")!.GetMethod!; |
| 34 | + |
| 35 | + private static readonly MethodInfo ImmutableDictionary_Item_Getter = |
| 36 | + ImmutableDictionaryType.GetProperty("Item")!.GetMethod!; |
| 37 | + |
| 38 | + private static readonly MethodInfo Enumerable_Any = |
| 39 | + typeof(Enumerable).GetMethod(nameof(Enumerable.Any), |
| 40 | + BindingFlags.Public | BindingFlags.Static, new[] { typeof(IEnumerable<>).MakeGenericType(Type.MakeGenericMethodParameter(0)) })! |
| 41 | + .MakeGenericMethod(typeof(KeyValuePair<string, string>)); |
| 42 | + |
| 43 | + private static readonly PropertyInfo Dictionary_Count = DictionaryType.GetProperty(nameof(Dictionary<string, string>.Count))!; |
| 44 | + |
| 45 | + private static readonly PropertyInfo ImmutableDictionary_Count = |
| 46 | + ImmutableDictionaryType.GetProperty(nameof(ImmutableDictionary<string, string>.Count))!; |
| 47 | + |
| 48 | + private static readonly PropertyInfo ImmutableDictionary_IsEmpty = |
| 49 | + ImmutableDictionaryType.GetProperty(nameof(ImmutableDictionary<string, string>.IsEmpty))!; |
| 50 | + |
| 51 | + private readonly RelationalTypeMapping _stringListTypeMapping; |
| 52 | + private readonly NpgsqlSqlExpressionFactory _sqlExpressionFactory; |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 56 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 57 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 58 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 59 | + /// </summary> |
| 60 | + public NpgsqlHstoreTranslator(IRelationalTypeMappingSource typeMappingSource, NpgsqlSqlExpressionFactory sqlExpressionFactory) |
| 61 | + { |
| 62 | + _sqlExpressionFactory = sqlExpressionFactory; |
| 63 | + _stringListTypeMapping = typeMappingSource.FindMapping(typeof(List<string>))!; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 68 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 69 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 70 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 71 | + /// </summary> |
| 72 | + public SqlExpression? Translate( |
| 73 | + SqlExpression? instance, |
| 74 | + MethodInfo method, |
| 75 | + IReadOnlyList<SqlExpression> arguments, |
| 76 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 77 | + { |
| 78 | + if (method == Enumerable_Any) |
| 79 | + { |
| 80 | + var value = instance ?? arguments[0]; |
| 81 | + if (value.TypeMapping?.StoreType == NpgsqlHstoreTypeMapping.HstoreType) |
| 82 | + { |
| 83 | + return _sqlExpressionFactory.NotEqual( |
| 84 | + Translate(value, Dictionary_Count, typeof(int), logger)!, |
| 85 | + _sqlExpressionFactory.Constant(0)); |
| 86 | + } |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + if (instance?.TypeMapping is null || instance.TypeMapping.StoreType != NpgsqlHstoreTypeMapping.HstoreType) |
| 91 | + { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + if (method == Dictionary_ContainsKey || method == ImmutableDictionary_ContainsKey) |
| 96 | + { |
| 97 | + return _sqlExpressionFactory.MakePostgresBinary(PgExpressionType.HStoreContainsKey, instance, arguments[0]); |
| 98 | + } |
| 99 | + |
| 100 | + if (method == Dictionary_ContainsValue || method == ImmutableDictionary_ContainsValue) |
| 101 | + { |
| 102 | + return _sqlExpressionFactory.Any( |
| 103 | + arguments[0], |
| 104 | + _sqlExpressionFactory.Function( |
| 105 | + "avals", new[] { instance }, false, FalseArrays[1], typeof(List<string>), _stringListTypeMapping), |
| 106 | + PgAnyOperatorType.Equal); |
| 107 | + } |
| 108 | + |
| 109 | + if (method == Dictionary_Item_Getter || method == ImmutableDictionary_Item_Getter) |
| 110 | + { |
| 111 | + return _sqlExpressionFactory.MakePostgresBinary(PgExpressionType.HStoreValueForKey, instance, arguments[0]); |
| 112 | + } |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 118 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 119 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 120 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 121 | + /// </summary> |
| 122 | + public SqlExpression? Translate( |
| 123 | + SqlExpression? instance, |
| 124 | + MemberInfo member, |
| 125 | + Type returnType, |
| 126 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 127 | + { |
| 128 | + |
| 129 | + if (instance?.TypeMapping is null || instance.TypeMapping.StoreType != NpgsqlHstoreTypeMapping.HstoreType) |
| 130 | + { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + if (member == Dictionary_Count || member == ImmutableDictionary_Count) |
| 135 | + { |
| 136 | + return _sqlExpressionFactory.Function("array_length", new [] |
| 137 | + { |
| 138 | + _sqlExpressionFactory.Function( |
| 139 | + "akeys", new[] { instance }, false, FalseArrays[1], typeof(List<string>), _stringListTypeMapping), |
| 140 | + _sqlExpressionFactory.Constant(1) |
| 141 | + }, false, FalseArrays[2], typeof(int)); |
| 142 | + } |
| 143 | + |
| 144 | + if (member == ImmutableDictionary_IsEmpty) |
| 145 | + { |
| 146 | + return _sqlExpressionFactory.Equal( |
| 147 | + Translate(instance, Dictionary_Count, typeof(int), logger)!, |
| 148 | + _sqlExpressionFactory.Constant(0)); |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | +} |
0 commit comments