Repository navigation
Conversation
fe46200 to
1f156c8
Compare
1b26300 to
c4dd6e4
Compare
| /// </summary> | ||
| /// <returns>The first column of the first row in the result set.</returns> | ||
| /// <remarks>Throws an Exception if the result set is empty or if the result is NULL.</remarks> | ||
| [CanBeNull] |
There was a problem hiding this comment.
Such attributes can be removed because we switched to C# 8.0 (:
| public T ExecuteScalar<T>() | ||
| => ExecuteScalar<T>(false, CancellationToken.None).GetAwaiter().GetResult(); | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] |
There was a problem hiding this comment.
Internals of the method won't be inlined because it's a state machine.
|
Thanks @YohDeadfall for your feedback. After watching the proposed API changes in the .NET Design Review for System.Data I thought to myself that it might be a good time (a bit late even) to pick up the discussion in https://github.com/dotnet/corefx/issues/30426, rebase this PR and implement the API as discussed there. I also figured that #2446 is related as I'll try to address the comments in https://github.com/dotnet/corefx/issues/30426 with the following commit(s). |
|
I'll eventually get around to properly reviewing this :) There is a lot of work pressure at the moment. However, one important question - how are you dealing with nulls?
I admit that lately I prefer to propose such general API additions at the corefx level first, rather than implementing something in Npgsql; this allows us to have the discussion with other providers and data people, and avoids the problem of us doing something that will become non-standard later... |
|
I was thinking about both |
One issue with TryExecuteScalar(), is that the Try* pattern is usually for when the operation could fail: Another objection is that this API makes it difficult to work with database scalars when one does not care (at that point) whether the value is null or not (e.g. because one simply wants to pass it to somewhere else, or to assign it to a nullable member). So I'd expect to see this happening: SomeString = reader.TryExecuteScalar<string>("...", out var someString) ? someString : null;Whereas a simple ExecuteScalar() returning a nullable value would have been simpler (and also would allow for coalescing etc.). The one big advantage TryExecuteScalar() does have, is avoiding nullable types altogether, and so not depending on any C# feature. But assuming we could have a nullable ExecuteScalar(), would we still go for TryExecuteScalar()? If not, it's probably worth waiting to see what happens with the C# nullability feature. Finally, it's still probably a good idea to revive the conversation in https://github.com/dotnet/corefx/issues/30426 and reach a consensus there before implementing in Npgsql. |
|
I think we can live without nullable value types here. At least you can assign a string from a potentially null-returning query in one line which isn't possible with the current |
|
@Brar for cases where you really are just reading a nullable string that seems very clunky (imagine repeating this everywhere). Here's a sample for reading a nullable string with the current non-generic API (assuming you know it always returns a string or null). It even looks better than the above: SomeString = reader.ExecuteScalar(0) is string s ? s : null;I still think we should allow something like: SomeString = reader.ExecuteScalar<string>(0);Everywhere I go I see people adding extensions methods such as |
You're right. That would be an extension method for me.
👍
I agree but we need dotnet/csharplang#2194 for this and I've seen little recent discussion or official commitment from roslyn crew over there. And I figure that it is hard to implement in a backwards compatible way. |
Any reason this should be an extension method, whereas the proposed Besides, the idea is to stop forcing people to repeat the same extensions themselves - if there's a clear need for it, it should ideally be taken care of inside corefx.
See my comment here about the non-urgency of this sugar - there doesn't seem to be a real downside to waiting and seeing what happens with the nullability feature. |
|
@Brar am trying to reduce the number of pending open PRs we have, do you think it's ok to close this for now? I think it's more appropriate to have this discussion at the System.Data level as there's nothing here that's PostgreSQL-specific. We can work on this together for .NET 6.0, for example. |
|
Yeah, this isn't really important and definitely something that should be standardized at the ADO.NET-level first. |
Per discussion in #1996 and https://github.com/dotnet/corefx/issues/30426