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

Support a few more type mappings in the compiled model #2961

Merged
merged 1 commit into from
Nov 18, 2023

Conversation

roji
Copy link
Member

@roji roji commented Nov 17, 2023

Part of #2949

ValueComparer? keyValueComparer = null,
ValueComparer? providerValueComparer = null)
{
var result = base.Create(typeMapping, parameters, valueComparer, keyValueComparer, providerValueComparer);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndriySvyryd here's some Npgsql-specific customizations to type mapping code generation in the compiled model, this causes an additional Clone() to be generated with the required stuff:

foo.TypeMapping = NpgsqlRangeTypeMapping.Default.Clone(
    comparer: new ValueComparer<NpgsqlRange<DateTime>>(
        (NpgsqlRange<DateTime> v1, NpgsqlRange<DateTime> v2) => v1.Equals(v2),
        (NpgsqlRange<DateTime> v) => v.GetHashCode(),
        (NpgsqlRange<DateTime> v) => v),
    keyComparer: new ValueComparer<NpgsqlRange<DateTime>>(
        (NpgsqlRange<DateTime> v1, NpgsqlRange<DateTime> v2) => v1.Equals(v2),
        (NpgsqlRange<DateTime> v) => v.GetHashCode(),
        (NpgsqlRange<DateTime> v) => v),
    providerValueComparer: new ValueComparer<NpgsqlRange<DateTime>>(
        (NpgsqlRange<DateTime> v1, NpgsqlRange<DateTime> v2) => v1.Equals(v2),
        (NpgsqlRange<DateTime> v) => v.GetHashCode(),
        (NpgsqlRange<DateTime> v) => v),
    mappingInfo: new RelationalTypeMappingInfo(
        storeTypeName: "tstzrange"),
    clrType: typeof(NpgsqlRange<DateTime>));
foo.TypeMapping = ((NpgsqlRangeTypeMapping)foo.TypeMapping).Clone(
    NpgsqlDbType.TimestampTzRange,
    NpgsqlTimestampTzTypeMapping.Default);

We may want to make this better in the future. For example, a type mapping could have an API which allows it to add a code fragment (or otherwise customize) its own initialization in the compiled model (a bit like how it has GenerateCodeLiteral, which generates a code fragment for instantiating a literal in C#).

@roji roji force-pushed the MoreCompiledModelMappings branch 2 times, most recently from e6fb475 to f4f5ac7 Compare November 17, 2023 23:52
@roji roji merged commit fa5ba2e into npgsql:main Nov 18, 2023
17 checks passed
@roji roji deleted the MoreCompiledModelMappings branch November 18, 2023 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants