forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes npgsql#212
- Loading branch information
Showing
9 changed files
with
593 additions
and
3 deletions.
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlHstoreTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using System.Collections.Immutable; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; | ||
using static Npgsql.EntityFrameworkCore.PostgreSQL.Utilities.Statics; | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class NpgsqlHstoreTranslator : IMethodCallTranslator, IMemberTranslator | ||
{ | ||
private static readonly Type DictionaryType = typeof(Dictionary<string, string>); | ||
private static readonly Type ImmutableDictionaryType = typeof(ImmutableDictionary<string, string>); | ||
|
||
private static readonly MethodInfo Dictionary_ContainsKey = | ||
DictionaryType.GetMethod(nameof(Dictionary<string, string>.ContainsKey))!; | ||
|
||
private static readonly MethodInfo ImmutableDictionary_ContainsKey = | ||
ImmutableDictionaryType.GetMethod(nameof(ImmutableDictionary<string, string>.ContainsKey))!; | ||
|
||
private static readonly MethodInfo Dictionary_ContainsValue = | ||
DictionaryType.GetMethod(nameof(Dictionary<string, string>.ContainsValue))!; | ||
|
||
private static readonly MethodInfo ImmutableDictionary_ContainsValue = | ||
ImmutableDictionaryType.GetMethod(nameof(ImmutableDictionary<string, string>.ContainsValue))!; | ||
|
||
private static readonly MethodInfo Dictionary_Item_Getter = | ||
DictionaryType.FindIndexerProperty()!.GetMethod!; | ||
|
||
private static readonly MethodInfo ImmutableDictionary_Item_Getter = | ||
ImmutableDictionaryType.FindIndexerProperty()!.GetMethod!; | ||
|
||
private static readonly MethodInfo Enumerable_Any = | ||
typeof(Enumerable).GetMethod( | ||
nameof(Enumerable.Any), BindingFlags.Public | BindingFlags.Static, | ||
[typeof(IEnumerable<>).MakeGenericType(Type.MakeGenericMethodParameter(0))])! | ||
.MakeGenericMethod(typeof(KeyValuePair<string, string>)); | ||
|
||
private static readonly MethodInfo Enumerable_ToList = | ||
typeof(Enumerable).GetMethod( | ||
nameof(Enumerable.ToList), BindingFlags.Public | BindingFlags.Static, | ||
[typeof(IEnumerable<>).MakeGenericType(Type.MakeGenericMethodParameter(0))])! | ||
.MakeGenericMethod(typeof(string)); | ||
|
||
private static readonly PropertyInfo Dictionary_Count = DictionaryType.GetProperty(nameof(Dictionary<string, string>.Count))!; | ||
|
||
private static readonly PropertyInfo ImmutableDictionary_Count = | ||
ImmutableDictionaryType.GetProperty(nameof(ImmutableDictionary<string, string>.Count))!; | ||
|
||
private static readonly PropertyInfo ImmutableDictionary_IsEmpty = | ||
ImmutableDictionaryType.GetProperty(nameof(ImmutableDictionary<string, string>.IsEmpty))!; | ||
|
||
private static readonly PropertyInfo Dictionary_Keys = DictionaryType.GetProperty(nameof(Dictionary<string, string>.Keys))!; | ||
|
||
private static readonly PropertyInfo ImmutableDictionary_Keys = | ||
ImmutableDictionaryType.GetProperty(nameof(ImmutableDictionary<string, string>.Keys))!; | ||
|
||
private static readonly PropertyInfo Dictionary_Values = DictionaryType.GetProperty(nameof(Dictionary<string, string>.Values))!; | ||
|
||
private static readonly PropertyInfo ImmutableDictionary_Values = | ||
ImmutableDictionaryType.GetProperty(nameof(ImmutableDictionary<string, string>.Values))!; | ||
|
||
private readonly RelationalTypeMapping _stringListTypeMapping; | ||
private readonly RelationalTypeMapping _stringTypeMapping; | ||
private readonly NpgsqlSqlExpressionFactory _sqlExpressionFactory; | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public NpgsqlHstoreTranslator(IRelationalTypeMappingSource typeMappingSource, NpgsqlSqlExpressionFactory sqlExpressionFactory) | ||
{ | ||
_sqlExpressionFactory = sqlExpressionFactory; | ||
_stringListTypeMapping = typeMappingSource.FindMapping(typeof(List<string>))!; | ||
_stringTypeMapping = typeMappingSource.FindMapping(typeof(string))!; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public SqlExpression? Translate( | ||
SqlExpression? instance, | ||
MethodInfo method, | ||
IReadOnlyList<SqlExpression> arguments, | ||
IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
{ | ||
if (method == Enumerable_Any && arguments[0].TypeMapping?.StoreType == "hstore") | ||
{ | ||
return _sqlExpressionFactory.NotEqual( | ||
Translate(arguments[0], Dictionary_Count, typeof(int), logger)!, | ||
_sqlExpressionFactory.Constant(0)); | ||
} | ||
|
||
if (method == Enumerable_ToList && arguments[0] is SqlFunctionExpression { Arguments: [{ TypeMapping.StoreType: "hstore" }] }) | ||
{ | ||
return arguments[0]; | ||
} | ||
|
||
if (instance?.TypeMapping?.StoreType != "hstore") | ||
{ | ||
return null; | ||
} | ||
|
||
if (method == Dictionary_ContainsKey || method == ImmutableDictionary_ContainsKey) | ||
{ | ||
return _sqlExpressionFactory.MakePostgresBinary(PgExpressionType.HStoreContainsKey, instance, arguments[0]); | ||
} | ||
|
||
if (method == Dictionary_ContainsValue || method == ImmutableDictionary_ContainsValue) | ||
{ | ||
return _sqlExpressionFactory.Any( | ||
arguments[0], | ||
Translate(instance, Dictionary_Values, typeof(List<string>), logger)!, | ||
PgAnyOperatorType.Equal); | ||
} | ||
|
||
if (method == Dictionary_Item_Getter || method == ImmutableDictionary_Item_Getter) | ||
{ | ||
return _sqlExpressionFactory.MakePostgresBinary(PgExpressionType.HStoreValueForKey, instance, arguments[0], _stringTypeMapping); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public SqlExpression? Translate( | ||
SqlExpression? instance, | ||
MemberInfo member, | ||
Type returnType, | ||
IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
{ | ||
|
||
if (instance?.TypeMapping?.StoreType != "hstore") | ||
{ | ||
return null; | ||
} | ||
|
||
if (member == Dictionary_Count || member == ImmutableDictionary_Count) | ||
{ | ||
return _sqlExpressionFactory.Function("array_length", | ||
[ | ||
Translate(instance, Dictionary_Keys, typeof(List<string>), logger)!, | ||
_sqlExpressionFactory.Constant(1) | ||
], false, TrueArrays[2], typeof(int)); | ||
} | ||
|
||
if (member == Dictionary_Keys || member == ImmutableDictionary_Keys) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"akeys", [instance], true, TrueArrays[1], typeof(List<string>), _stringListTypeMapping); | ||
} | ||
|
||
if (member == Dictionary_Values || member == ImmutableDictionary_Values) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"avals", [instance], true, TrueArrays[1], typeof(List<string>), _stringListTypeMapping); | ||
} | ||
|
||
if (member == ImmutableDictionary_IsEmpty) | ||
{ | ||
return _sqlExpressionFactory.Equal( | ||
Translate(instance, Dictionary_Count, typeof(int), logger)!, | ||
_sqlExpressionFactory.Constant(0)); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.