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