-
Notifications
You must be signed in to change notification settings - Fork 315
Implement SqlConnection.GetSchemaAsync #3005
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
Open
edwardneal
wants to merge
13
commits into
dotnet:main
Choose a base branch
from
edwardneal:issue-646
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7313a6f
Implemented asynchronous schema APIs
edwardneal 63dae61
Remove erroneously-added reference to shared csproj
edwardneal 40d5f66
Implemented SqlDataReader.GetSchemaTableAsync
edwardneal a58b0a4
ref project cleanup
edwardneal b60c6a6
Add default parameter value to ref projects
edwardneal fdc11cd
Merge branch 'main' into issue-646
edwardneal 6bbd433
Revert addition of SqlDataReader API
edwardneal 2d3a93d
Merge main
edwardneal eb164d6
Account for SqlDataReader merge and .NET Standard support
edwardneal 5f05e45
Response to code review
edwardneal a3def44
Merge main
edwardneal 21b7a3b
Merge branch 'main' into issue-646
edwardneal d5f7a92
Merge main
edwardneal 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
|
||
namespace Microsoft.Data.ProviderBase | ||
|
@@ -106,7 +108,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really nice to be seeing proper async/await in SqlClient! |
||
{ | ||
DataTable metaDataCollectionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.MetaDataCollections]; | ||
DataColumn populationStringColumn = metaDataCollectionsTable.Columns[PopulationStringKey]; | ||
|
@@ -125,8 +127,8 @@ private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restri | |
throw ADP.TooManyRestrictions(collectionName); | ||
} | ||
|
||
DbCommand command = connection.CreateCommand(); | ||
SqlConnection castConnection = connection as SqlConnection; | ||
SqlCommand command = castConnection.CreateCommand(); | ||
|
||
command.CommandText = sqlCommand; | ||
command.CommandTimeout = Math.Max(command.CommandTimeout, 180); | ||
|
@@ -152,12 +154,12 @@ private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restri | |
command.Parameters.Add(restrictionParameter); | ||
} | ||
|
||
DbDataReader reader = null; | ||
SqlDataReader reader = null; | ||
try | ||
{ | ||
try | ||
{ | ||
reader = command.ExecuteReader(); | ||
reader = isAsync ? await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false) : command.ExecuteReader(); | ||
} | ||
catch (Exception e) | ||
{ | ||
|
@@ -174,16 +176,25 @@ private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restri | |
Locale = CultureInfo.InvariantCulture | ||
}; | ||
|
||
// We would ordinarily call reader.GetSchemaTableAsync, but this waits synchronously for the reader to receive its type metadata. | ||
// Instead, we invoke reader.ReadAsync outside of the while loop, which will implicitly ensure that the metadata is available. | ||
// ReadAsync/Read will throw an exception if necessary, so we can trust that the list of fields is available if the call returns. | ||
bool firstResultAvailable = isAsync ? await reader.ReadAsync(cancellationToken).ConfigureAwait(false) : reader.Read(); | ||
DataTable schemaTable = reader.GetSchemaTable(); | ||
|
||
foreach (DataRow row in schemaTable.Rows) | ||
{ | ||
resultTable.Columns.Add(row["ColumnName"] as string, (Type)row["DataType"] as Type); | ||
resultTable.Columns.Add((string)row["ColumnName"], (Type)row["DataType"]); | ||
} | ||
object[] values = new object[resultTable.Columns.Count]; | ||
while (reader.Read()) | ||
|
||
if (firstResultAvailable) | ||
{ | ||
reader.GetValues(values); | ||
resultTable.Rows.Add(values); | ||
object[] values = new object[resultTable.Columns.Count]; | ||
do | ||
{ | ||
reader.GetValues(values); | ||
resultTable.Rows.Add(values); | ||
} while (isAsync ? await reader.ReadAsync(cancellationToken).ConfigureAwait(false) : reader.Read()); | ||
} | ||
} | ||
finally | ||
|
@@ -375,6 +386,12 @@ private string GetParameterName(string neededCollectionName, int neededRestricti | |
} | ||
|
||
public virtual DataTable GetSchema(DbConnection connection, string collectionName, string[] restrictions) | ||
=> GetSchemaCore(connection, collectionName, restrictions, false, default).Result; | ||
|
||
public virtual async Task<DataTable> GetSchemaAsync(DbConnection connection, string collectionName, string[] restrictions, CancellationToken cancellationToken) | ||
=> await GetSchemaCore(connection, collectionName, restrictions, true, cancellationToken).ConfigureAwait(false); | ||
|
||
private async ValueTask<DataTable> GetSchemaCore(DbConnection connection, string collectionName, string[] restrictions, bool isAsync, CancellationToken cancellationToken) | ||
{ | ||
Debug.Assert(_metaDataCollectionsDataSet != null); | ||
|
||
|
@@ -384,6 +401,7 @@ public virtual DataTable GetSchema(DbConnection connection, string collectionNam | |
|
||
string[] hiddenColumns; | ||
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
DataRow requestedCollectionRow = FindMetaDataCollectionRow(collectionName); | ||
string exactCollectionName = requestedCollectionRow[collectionNameColumn, DataRowVersion.Current] as string; | ||
|
||
|
@@ -401,6 +419,7 @@ public virtual DataTable GetSchema(DbConnection connection, string collectionNam | |
} | ||
} | ||
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
string populationMechanism = requestedCollectionRow[populationMechanismColumn, DataRowVersion.Current] as string; | ||
|
||
DataTable requestedSchema; | ||
|
@@ -424,16 +443,15 @@ public virtual DataTable GetSchema(DbConnection connection, string collectionNam | |
throw ADP.TooManyRestrictions(exactCollectionName); | ||
} | ||
|
||
|
||
requestedSchema = CloneAndFilterCollection(exactCollectionName, hiddenColumns); | ||
break; | ||
|
||
case SqlCommandKey: | ||
requestedSchema = ExecuteCommand(requestedCollectionRow, restrictions, connection); | ||
requestedSchema = await ExecuteCommandAsync(requestedCollectionRow, restrictions, connection, isAsync, cancellationToken).ConfigureAwait(false); | ||
break; | ||
|
||
case PrepareCollectionKey: | ||
requestedSchema = PrepareCollection(exactCollectionName, restrictions, connection); | ||
requestedSchema = await PrepareCollectionAsync(exactCollectionName, restrictions, connection, isAsync, cancellationToken).ConfigureAwait(false); | ||
break; | ||
|
||
default: | ||
|
@@ -701,7 +719,7 @@ private static DataTable CreateReservedWordsDataTable() | |
} | ||
}; | ||
|
||
protected virtual DataTable PrepareCollection(string collectionName, string[] restrictions, DbConnection connection) | ||
protected virtual ValueTask<DataTable> PrepareCollectionAsync(string collectionName, string[] restrictions, DbConnection connection, bool isAsync, CancellationToken cancellationToken) | ||
{ | ||
throw ADP.NotSupported(); | ||
} | ||
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.