-
Notifications
You must be signed in to change notification settings - Fork 316
Flatten | SqlInternalConnectionTds and SqlInternalConnection #3773
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
base: main
Are you sure you want to change the base?
Changes from 30 commits
66c2cd6
2d664a6
c796212
13c373c
a176e2f
c5863e3
8002ec7
202beb5
bb3d541
4d39842
58c358f
07750f8
dbe9d9e
355f2d2
74b0c92
ce46515
79cf052
0a45c97
7b5a637
ba4c7df
a0a1f24
41b7f2a
1b52999
8fb7b0e
0e78a6e
858d98e
6dfbd5d
a83bd7c
c4db055
d010d48
4d02cc2
04e5cdf
64766f4
89b92b9
3b795ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.Data.SqlClient.Connection | ||
| { | ||
| /// <summary> | ||
| /// Provides cached asynchronous call contexts shared between objects in a connection's context. | ||
| /// </summary> | ||
| internal class CachedContexts | ||
| { | ||
| #region Fields | ||
|
|
||
| /// <summary> | ||
| /// Stores reusable context for ExecuteNonQueryAsync invocations. | ||
| /// </summary> | ||
| private SqlCommand.ExecuteNonQueryAsyncCallContext? _commandExecuteNonQueryAsyncContext; | ||
|
|
||
| /// <summary> | ||
| /// Stores reusable context for ExecuteReaderAsync invocations. | ||
| /// </summary> | ||
| private SqlCommand.ExecuteReaderAsyncCallContext? _commandExecuteReaderAsyncContext; | ||
|
|
||
| /// <summary> | ||
| /// Stores reusable context for ExecuteXmlReaderAsync invocations. | ||
| /// </summary> | ||
| private SqlCommand.ExecuteXmlReaderAsyncCallContext? _commandExecuteXmlReaderAsyncContext; | ||
|
|
||
| /// <summary> | ||
| /// Stores reusable context for IsDBNullAsync invocations. | ||
| /// </summary> | ||
| private SqlDataReader.IsDBNullAsyncCallContext? _dataReaderIsDbNullContext; | ||
|
|
||
| /// <summary> | ||
| /// Stores reusable context for ReadAsync invocations. | ||
| /// </summary> | ||
| private SqlDataReader.ReadAsyncCallContext? _dataReaderReadAsyncContext; | ||
|
|
||
| /// <summary> | ||
| /// Stores a data reader snapshot. | ||
| /// </summary> | ||
| private SqlDataReader.Snapshot? _dataReaderSnapshot; | ||
|
|
||
| #endregion | ||
|
|
||
| #region Access Methods | ||
|
|
||
| /// <summary> | ||
| /// Removes and returns the cached ExecuteNonQueryAsync context. | ||
| /// </summary> | ||
| /// <returns>The previously cached context or null when empty.</returns> | ||
| internal SqlCommand.ExecuteNonQueryAsyncCallContext? ClearCommandExecuteNonQueryAsyncContext() => | ||
| Interlocked.Exchange(ref _commandExecuteNonQueryAsyncContext, null); | ||
|
|
||
| /// <summary> | ||
| /// Removes and returns the cached ExecuteReaderAsync context. | ||
| /// </summary> | ||
| /// <returns>The previously cached context or null when empty.</returns> | ||
| internal SqlCommand.ExecuteReaderAsyncCallContext? ClearCommandExecuteReaderAsyncContext() => | ||
| Interlocked.Exchange(ref _commandExecuteReaderAsyncContext, null); | ||
|
|
||
| /// <summary> | ||
| /// Removes and returns the cached ExecuteXmlReaderAsync context. | ||
| /// </summary> | ||
| /// <returns>The previously cached context or null when empty.</returns> | ||
| internal SqlCommand.ExecuteXmlReaderAsyncCallContext? ClearCommandExecuteXmlReaderAsyncContext() => | ||
| Interlocked.Exchange(ref _commandExecuteXmlReaderAsyncContext, null); | ||
|
|
||
| /// <summary> | ||
| /// Removes and returns the cached ReadAsync context. | ||
| /// </summary> | ||
| /// <returns>The previously cached context or null when empty.</returns> | ||
| internal SqlDataReader.ReadAsyncCallContext? ClearDataReaderReadAsyncContext() => | ||
| Interlocked.Exchange(ref _dataReaderReadAsyncContext, null); | ||
|
|
||
| /// <summary> | ||
| /// Removes and returns the cached IsDBNullAsync context. | ||
| /// </summary> | ||
| /// <returns>The previously cached context or null when empty.</returns> | ||
| internal SqlDataReader.IsDBNullAsyncCallContext? ClearDataReaderIsDbNullContext() => | ||
| Interlocked.Exchange(ref _dataReaderIsDbNullContext, null); | ||
|
|
||
| /// <summary> | ||
| /// Removes and returns the cached data reader snapshot. | ||
| /// </summary> | ||
| /// <returns>The previously cached snapshot or null when empty.</returns> | ||
| internal SqlDataReader.Snapshot? ClearDataReaderSnapshot() => | ||
| Interlocked.Exchange(ref _dataReaderSnapshot, null); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to cache the provided ExecuteNonQueryAsync context. | ||
| /// </summary> | ||
| /// <param name="value">Context instance to store.</param> | ||
| /// <returns> | ||
| /// True when the context is cached; false if an existing value is preserved. | ||
| /// </returns> | ||
| internal bool TrySetCommandExecuteNonQueryAsyncContext(SqlCommand.ExecuteNonQueryAsyncCallContext value) => | ||
| TrySetContext(value, ref _commandExecuteNonQueryAsyncContext); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to cache the provided ExecuteReaderAsync context. | ||
| /// </summary> | ||
| /// <param name="value">Context instance to store.</param> | ||
| /// <returns> | ||
| /// True when the context is cached; false if an existing value is preserved. | ||
| /// </returns> | ||
| internal bool TrySetCommandExecuteReaderAsyncContext(SqlCommand.ExecuteReaderAsyncCallContext value) => | ||
| TrySetContext(value, ref _commandExecuteReaderAsyncContext); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to cache the provided ExecuteXmlReaderAsync context. | ||
| /// </summary> | ||
| /// <param name="value">Context instance to store.</param> | ||
| /// <returns> | ||
| /// True when the context is cached; false if an existing value is preserved. | ||
| /// </returns> | ||
| internal bool TrySetCommandExecuteXmlReaderAsyncContext(SqlCommand.ExecuteXmlReaderAsyncCallContext value) => | ||
| TrySetContext(value, ref _commandExecuteXmlReaderAsyncContext); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to cache the provided ReadAsync context. | ||
| /// </summary> | ||
| /// <param name="value">Context instance to store.</param> | ||
| /// <returns> | ||
| /// True when the context is cached; false if an existing value is preserved. | ||
| /// </returns> | ||
| internal bool TrySetDataReaderReadAsyncContext(SqlDataReader.ReadAsyncCallContext value) => | ||
| TrySetContext(value, ref _dataReaderReadAsyncContext); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to cache the provided IsDBNullAsync context. | ||
| /// </summary> | ||
| /// <param name="value">Context instance to store.</param> | ||
| /// <returns> | ||
| /// True when the context is cached; false if an existing value is preserved. | ||
| /// </returns> | ||
| internal bool TrySetDataReaderIsDbNullContext(SqlDataReader.IsDBNullAsyncCallContext value) => | ||
| TrySetContext(value, ref _dataReaderIsDbNullContext); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to cache the provided data reader snapshot context. | ||
| /// </summary> | ||
| /// <param name="value">Context instance to store.</param> | ||
| /// <returns> | ||
| /// True when the snapshot is cached; false if an existing snapshot is preserved. | ||
| /// </returns> | ||
| internal bool TrySetDataReaderSnapshot(SqlDataReader.Snapshot value) => | ||
| TrySetContext(value, ref _dataReaderSnapshot); | ||
|
|
||
| #endregion | ||
|
|
||
| private static bool TrySetContext<TContext>(TContext value, ref TContext? location) | ||
| where TContext : class | ||
| { | ||
| if (value is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(value)); | ||
| } | ||
|
|
||
| return Interlocked.CompareExchange(ref location, value, null) is null; | ||
| } | ||
|
Comment on lines
+169
to
+178
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Missing XML documentation for the
CachedContextsclass. As a newly introduced internal class with public-facing methods, it should have a summary describing its purpose and usage pattern, especially since it deals with thread-safe context caching usingInterlockedoperations.