Skip to content

Draft: NpgsqlCommand.ExecuteScalar<T>() - #1997

Closed
Brar wants to merge 1 commit into
npgsql:devfrom
Brar:GenericExecuteScalar
Closed

Brar wants to merge 1 commit into
npgsql:devfrom
Brar:GenericExecuteScalar

Conversation

@Brar

@Brar Brar commented Jun 15, 2018

Copy link
Copy Markdown
Member

@roji
roji force-pushed the dev branch 2 times, most recently from fe46200 to 1f156c8 Compare July 20, 2018 12:57
@Brar
Brar force-pushed the GenericExecuteScalar branch from 1b26300 to c4dd6e4 Compare May 21, 2019 18:49
/// </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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Internals of the method won't be inlined because it's a state machine.

@Brar

Brar commented May 22, 2019 •

Copy link
Copy Markdown
Member Author

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 ExecuteScalar<T>() is nothing but a very minimal wrapper over DbDataReader.GetFieldValue<T>(0) but I think that supporting Stream/TextReader will happen automatically when addressing #2446.

I'll try to address the comments in https://github.com/dotnet/corefx/issues/30426 with the following commit(s).

@roji

roji commented May 29, 2019

Copy link
Copy Markdown
Member

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?

  1. Unlike non-generic ExecuteScalar(), this can't return DBNull.Value.
  2. To be consistent with other GetFieldValue(), this would throw on nulls. However, unlike with that API, there's no way to first check for null with another API (i.e. IsDBNull()). So throwing here would mean this API is basically unusable unless you're 100% sure no nulls are coming back, which doesn't seem good.
  3. It could (and probably should) simply return regular null. The only issue is that the current implementation of the C# 8 nullable references doesn't allow specifying that (see Proposal: allow generic methods to specify T? without constraining to class or struct dotnet/csharplang#2194). We therefore may want to wait a little before doing this.

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...

@Brar

Brar commented May 29, 2019

Copy link
Copy Markdown
Member Author

I was thinking about both ExecuteScalar<T>() and TryExecuteScalar<T>(out T result).

@roji

roji commented May 29, 2019 •

Copy link
Copy Markdown
Member

I was thinking about both ExecuteScalar() and TryExecuteScalar(out T result).

One issue with TryExecuteScalar(), is that the Try* pattern is usually for when the operation could fail: int.TryParse() return false when the string could not be parsed, Dictionary.TryGetValue() returns false when the given key was not found. This is not the same as the operation returning null, which is after all a completely valid return value. In other words, a false here makes it sounds like the execute failed (i.e. updates did not take place), rather than that it executed successfully but returned null. This may seem like a petty semantic objection, but it still seems important for API consistency etc...

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.

@Brar

Brar commented May 29, 2019

Copy link
Copy Markdown
Member Author

I think we can live without nullable value types here.
I actually like this pattern: SomeString = (reader.TryExecuteScalar<string>(out var someString) ? someString : null);

At least you can assign a string from a potentially null-returning query in one line which isn't possible with the current ExecuteScalar().

@roji

roji commented May 30, 2019 •

Copy link
Copy Markdown
Member

@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 GetValueOrDefault<T>() to deal precisely with this kind of difficulty.

@Brar

Brar commented May 30, 2019

Copy link
Copy Markdown
Member Author

imagine repeating this everywhere

You're right. That would be an extension method for me.

Here's a sample for reading a nullable string with the current non-generic API

👍
I tend to forget about safe-casting via is.

I still think we should allow something like:

SomeSring = reader.ExecuteScalar<string>(0);

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.

@roji

roji commented May 30, 2019

Copy link
Copy Markdown
Member

You're right. That would be an extension method for me.

Any reason this should be an extension method, whereas the proposed ExecuteScalar<T>()/TryExecuteScalar<T> shouldn't? All of these be implemented as extensions over ExecuteReader(), which should a version returning null be an extension and not the others?

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.

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.

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.

@roji

roji commented Sep 8, 2020

Copy link
Copy Markdown
Member

@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.

@Brar

Brar commented Sep 9, 2020

Copy link
Copy Markdown
Member Author

Yeah, this isn't really important and definitely something that should be standardized at the ADO.NET-level first.

@Brar Brar closed this Sep 9, 2020
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.

3 participants