Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translator for hstore type #3070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;

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
{
private readonly NpgsqlSqlExpressionFactory _sqlExpressionFactory;
private readonly RelationalTypeMapping? _stringTypeMapping;

/// <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,
IModel model)
{
_sqlExpressionFactory = sqlExpressionFactory;
_stringTypeMapping = typeMappingSource.FindMapping(typeof(string), model)!;
}

private static readonly MethodInfo _methodInfo = typeof(Dictionary<string, string>).GetRuntimeMethod(
"get_Item",
[
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 != _methodInfo
|| method.DeclaringType != typeof(Dictionary<string, string>)
|| instance?.TypeMapping is not NpgsqlHstoreTypeMapping)
{
return null;
}

return _sqlExpressionFactory.JsonTraversal(instance, arguments, false, typeof(string), _stringTypeMapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public NpgsqlMethodCallTranslatorProvider(
new NpgsqlJsonDomTranslator(typeMappingSource, sqlExpressionFactory, model),
new NpgsqlJsonDbFunctionsTranslator(typeMappingSource, sqlExpressionFactory, model),
new NpgsqlJsonPocoTranslator(typeMappingSource, sqlExpressionFactory, model),
new NpgsqlHstoreTranslator(typeMappingSource, sqlExpressionFactory, model),
new NpgsqlLikeTranslator(sqlExpressionFactory),
LTreeTranslator,
new NpgsqlMathTranslator(typeMappingSource, sqlExpressionFactory, model),
Expand Down