-
Notifications
You must be signed in to change notification settings - Fork 877
Rewrite user type mappings #4970
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using Npgsql.Internal; | ||
| using Npgsql.Internal.TypeHandling; | ||
| using Npgsql.Internal.TypeMapping; | ||
| using Npgsql.PostgresTypes; | ||
|
|
||
| namespace Npgsql.TypeMapping; | ||
|
|
||
| sealed class UserMappedTypeHandlerResolver : TypeHandlerResolver | ||
| { | ||
| readonly NpgsqlConnector _connector; | ||
| readonly IUserTypeMapping _mapping; | ||
| readonly uint? _typeOID; | ||
| readonly TypeMappingInfo? _typeMapping; | ||
|
|
||
| NpgsqlTypeHandler? _handler; | ||
|
|
||
| public UserMappedTypeHandlerResolver(NpgsqlConnector connector, IUserTypeMapping mapping) | ||
| { | ||
| _connector = connector; | ||
| _mapping = mapping; | ||
|
|
||
| if (connector.DatabaseInfo.TryGetPostgresTypeByName(mapping.PgTypeName, out var pgType)) | ||
| { | ||
| _typeOID = pgType.OID; | ||
| _typeMapping = new(npgsqlDbType: null, pgType.Name, mapping.ClrType); | ||
| } | ||
| } | ||
|
|
||
| public override NpgsqlTypeHandler? ResolveByDataTypeName(string typeName) | ||
| { | ||
| if (_connector.DatabaseInfo.TryGetPostgresTypeByName(_mapping.PgTypeName, out var pgType) | ||
| && (pgType.FullName == typeName || pgType.Name == typeName)) | ||
| return GetHandler(pgType); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public override NpgsqlTypeHandler? ResolveByClrType(Type type) | ||
| { | ||
| if (_connector.DatabaseInfo.TryGetPostgresTypeByName(_mapping.PgTypeName, out var pgType) && _mapping.ClrType == type) | ||
| return GetHandler(pgType); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| NpgsqlTypeHandler GetHandler(PostgresType pgType) => _handler ??= _mapping.CreateHandler(pgType, _connector); | ||
|
|
||
| public override NpgsqlTypeHandler? ResolveByPostgresType(PostgresType type) | ||
| => ResolveByDataTypeName(type.FullName); | ||
|
|
||
| public override TypeMappingInfo? GetMappingByPostgresType(PostgresType type) | ||
| { | ||
| if (_typeOID != type.OID) | ||
| return null; | ||
| Debug.Assert(_typeMapping is not null); | ||
| return _typeMapping; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally I thought of the resolvers list as always being quite small, e.g. how many plugins could there possibly be.. I'm not sure if we have places where perf would be problematic if this list starts growing; IIRC we should only actually access the resolvers for NpgsqlDbType/Type that we haven't seen before (we cache). Maybe on connection startup (when the cache is empty) this could in theory become problematic, if the user has e.g. 200 enums/composites or something.
To be on the safe side, we could have a single resolver handle them all; though if you're convinced this doesn't matter I'm OK with it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do have a point here. A few of my thoughts below:
In light of the above I propose:
What do you say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though that's probably going to complicate things for the global type mapper...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NinoFloris what do you think of the above? We've discussed simplying our type handler concepts by bringing ideas across from Slon; if we do that, it may not be worth worrying too much about the current situation...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, Slon's model also relies on resolvers for this work so in that respect nothing changes. If there are a huge number of (user) resolvers to check before ending up at the builtin set it would equally slow things down until the cache is properly filled.
I would probably have something like the following order:
In Slon the only time you need another resolver to be first is for overriding the given converter for some clr type or overriding info (default clr type for a pg type, text writing preferred etc).
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.