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

Implement SqlConnection.GetSchemaAsync and SqlDataReader.GetSchemaTableAsync #3005

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

edwardneal
Copy link
Contributor

@edwardneal edwardneal commented Nov 14, 2024

Contributes to #646, adding SqlConnection.GetSchemaAsync and SqlDataReader.GetSchemaTableAsync. SqlConnection.GetSchemaAsync is the only .NET 5.0+ method which would benefit from end-to-end async plumbing to remove the async-over-sync which is currently present. The other two schema methods on DbDataReader and IDbColumnSchemaGenerator already operate from the cached metadata. I've copied the .NET 5.0 base implementation to SqlDataReader to harmonise this part of the API surface between .NET Framework and .NET Core.

I can't add the IDbColumnSchemaGenerator method because the .NET Framework version of SqlDataReader doesn't yet implement that interface. #2967 tracks this.

I've added some basic documentation, but this effectively follows the SqlCommand.[Something]Async pattern of "An asynchronous version of [Something], which <etc.>"

I've also moved the netfx GetSchema methods from SqlConnectionHelper.cs to SqlConnection.cs, to align that part of it with netcore. This contributes to the merge slightly.

cancellationToken.ThrowIfCancellationRequested();
#if NET
DataTable schemaTable = isAsync ? await reader.GetSchemaTableAsync(cancellationToken).ConfigureAwait(false) : reader.GetSchemaTable();
#else
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to have the .NET Framework path also use an async API here? Unless I'm missing something, the exact same methods/code could be define for both, with the sole extension that in .NET Framework it wouldn't override?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do that; I thought it might look a little clumsy for the .NET Framework version of Microsoft.Data.SqlClient to have a dedicated async method which isn't present on System.Data.SqlClient and which doesn't actually run asynchronously.

(GetSchemaTable just constructs a DataTable from the cached metadata - there's nothing which we can make async.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Running this through CI now...

The IDbColumnSchemaGenerator method will need to be done after (or possibly as part of) #2967.

Copy link
Member

Choose a reason for hiding this comment

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

GetSchemaTable just constructs a DataTable from the cached metadata - there's nothing which we can make async

Oh I must be misunderstanding what this is about then - I thought the idea here is to actually get the schema information (asynchronously).

In any case, the idea is to have the least amount of separate code paths here for .NET Framework vs. modern .NET...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented this, but on re-checking, we access a property within SqlDataReader which performs a read from the network. I'm going to revert the SqlDataReader changes tomorrow so that I don't introduce async-over-sync within the new API. I'll submit the new SqlDataReader API in a subsequent PR, which will need more review since it'll involve touching the state machine.

SqlConnection.GetSchemaAsync is much simpler, since it just layers atop SqlCommand. This PR can still introduce that safely.

@@ -107,7 +109,7 @@ protected virtual void Dispose(bool disposing)
}
}

private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restrictions, DbConnection connection)
private async ValueTask<DataTable> ExecuteCommandAsync(DataRow requestedCollectionRow, string[] restrictions, DbConnection connection, bool isAsync, CancellationToken cancellationToken)
Copy link
Member

Choose a reason for hiding this comment

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

Really nice to be seeing proper async/await in SqlClient!

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 88.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 72.53%. Comparing base (4052186) to head (b60c6a6).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...core/src/Microsoft/Data/SqlClient/SqlDataReader.cs 50.00% 3 Missing ⚠️
...etfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs 50.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3005      +/-   ##
==========================================
+ Coverage   72.48%   72.53%   +0.04%     
==========================================
  Files         288      288              
  Lines       59493    59519      +26     
==========================================
+ Hits        43123    43170      +47     
+ Misses      16370    16349      -21     
Flag Coverage Δ
addons 92.58% <ø> (ø)
netcore 75.43% <91.66%> (+<0.01%) ⬆️
netfx 71.00% <92.50%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@edwardneal edwardneal changed the title Implement SqlConnection.GetSchemaAsync Implement SqlConnection.GetSchemaAsync and SqlDataReader.GetSchemaTableAsync Nov 15, 2024
@edwardneal edwardneal marked this pull request as draft November 15, 2024 22:38
This introduces async-over-sync, removing such requires work with the state machine and separate review.
@edwardneal edwardneal marked this pull request as ready for review November 16, 2024 08:56
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