Repository navigation
Added support for writing nullable values - #1993
YohDeadfall wants to merge 1 commit into
Conversation
|
@roji rerun Travis-CI, please. |
roji
left a comment
There was a problem hiding this comment.
Thanks @YohDeadfall, it's nice to see this.
I wrote some comments below, but here's a general comment - I think I already wrote this when you were implementing read support. Your current implementation checks up-front whether a type is nullable (NullableHandler<T>.Exists), and then dispatches either to NullableHandler<TAny>.WriteAsync or to the handler's async (same for validation). I'd prefer it if we moved nullable handling to after we tried to write in the normal way. If you look at the current WriteWithLength<TAny>(), we could change the code to the following:
if (this is INpgsqlTypeHandler<TAny> typedHandler)
{
buf.WriteInt32(typedHandler.ValidateAndGetLength(value, ref lengthCache, parameter));
return typedHandler.Write(value, buf, lengthCache, parameter, async);
}
else if (NullableHandler<T>.Exists)
{
// Write it as nullable
} else
throw new InvalidCastException(...);The advantage of this approach is the it eliminates the upfront conditional check on each and write (actually twice - once for validate, once for write). It also makes the code clearer by removing the need for the extra *Entry methods which basically dispatch between nullable/non-nullable.
The only disadvantage I can see is that WriteWithLength<TAny>() is overridden in some very special cases, and so the logic would have to be duplicated there. But that seems pretty minor to me.
What do you think?
|
|
||
| namespace Npgsql.TypeHandling | ||
| { | ||
| internal delegate T ReadDelegate<T>(NpgsqlReadBuffer buffer, int columnLength, FieldDescription fieldDescription); |
There was a problem hiding this comment.
nit: remove internal here and below, it's the default for types.
There was a problem hiding this comment.
I copied it as is and forgot about the modifier. Will do.
| internal delegate int ValidateDelegate<T>(NpgsqlTypeHandler handler, T value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter); | ||
| internal delegate Task WriteAsyncDelegate<T>(NpgsqlTypeHandler handler, T value, NpgsqlWriteBuffer buffer, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async); | ||
|
|
||
| internal static class NullableHandler<T> |
There was a problem hiding this comment.
nit: please add the following line at the start of the file to silence the Resharper warning:
// ReSharper disable StaticMemberInGenericType
|
|
||
| internal static class NullableHandler<T> | ||
| { | ||
| public static readonly Type UnderlyingType; |
There was a problem hiding this comment.
How about making this into an UnwrapNullableType extension method on type, like EF Core does (only faster)? At the moment every access to NullableHandler.UnderlyingType seems to have ?? type right after it.
| /// <returns>The fully-read value.</returns> | ||
| internal abstract TAny Read<TAny>(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null); | ||
|
|
||
| internal TAny ReadEntry<TAny>(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) |
There was a problem hiding this comment.
The new Entry suffix isn't very clear naming-wise - what it really seems to mean is "read this thing which may be a nullable". Maybe just ReadNullable or ReadWrapper?
There was a problem hiding this comment.
I know, but the best thing came to my mind was an entry point. Will do.
| internal abstract Task WriteWithLengthInternal<TAny>([CanBeNull] TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async); | ||
| protected internal abstract Task WriteWithLength<TAny>([CanBeNull] TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async); | ||
|
|
||
| internal Task WriteWithLengthEntry<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async) |
There was a problem hiding this comment.
It's nice that you lifted this from NpgsqlTypeHandler<T> to NpgsqlTypeHandler.
|
Will think about it and test, but two weeks later. I'm on vacation far away from the PC (: |
|
No problem, this PR can definitely wait. The idea is simply to make this 100% pay-per-play. |
|
;( |
|
@wiktor-golonka even if this PR were merged today, it would only be released for the next minor version (4.1) which is months away. In the meantime you're welcome to use the nugets from the unstable feed if you're OK with running on prerelease. Even if this PR is modified in the way I suggest, it's a purely internal implementation detail which shouldn't affect the API in any way. Regardless, it may be better to wait for @YohDeadfall on this one, it modifies some fairly core parts of Npgsql and if you're not familiar with the Npgsql codebase it's not the ideal place to start. |
|
@roji: I really, really hope we can get it working for 4.0.1. I am happy to spend more time on this if needed to better understand the core code. |
|
@wiktor-golonka it's not really a matter of getting it working for 4.0.1 - this change is a non-trivial feature, and patch versions such as 4.0.1 in general are really only about bug fixes, not adding new features. This is how we generally version Npgsql, following the spirit of semver (even if we're not strict at all). If we start adding features in 4.0.1, well, the version doesn't really mean anything anymore... Of course this doesn't stop you from running with your patched version of Npgsql until 4.1 is released. Finally, it's a bit strange for me that this feature would be so critical - it just allows you to write nullables. You can just as easily do a small and simple wrapper today that accepts the nullable and either sets the parameter value to |
|
To be clear, what I'm trying to say is that strictly speaking this is really a convenience feature - there is nothing added by this feature that cannot be done with a little more code outside of Npgsql. |
|
@roji: Right, I agree, it can be worked around. I said critical in the meaning of reliability as my only concern is if that's enough from the core code point of view. |
There are no checks at all. The JIT elides them because it treats |
fe46200 to
1f156c8
Compare
|
Closing it due to a lot of merge conflicts. Later I will open a new PR to continue the work. |
Fixes #1965