Skip to content

Commit 0eec7e2

Browse files
authored
Merge | SqlConnection cosmetic changes (#3275)
* Reorganise fields * Reformat attribute syntax * Reorder properties * Cleanup syntax and comments * Adjust usings, if/else chain in constructor * Replace ADP.IsEmpty, use nameof, variable rename * Remove optional default parameters * Align implementation property attributes with ref * Switch Attestation properties to => syntax * Remove unnecessary assignments * Follow-up of merge
1 parent f68b60e commit 0eec7e2

File tree

4 files changed

+425
-602
lines changed

4 files changed

+425
-602
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ private object ConvertValue(object value, _SqlMetaData metadata, bool isNull, re
16201620
// in byte[] form.
16211621
if (!(value is byte[]))
16221622
{
1623-
value = _connection.GetBytes(value);
1623+
value = _connection.GetBytes(value, out _, out _);
16241624
typeChanged = true;
16251625
}
16261626
break;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private static readonly Dictionary<string, SqlColumnEncryptionKeyStoreProvider>
9999

100100
/// <summary>
101101
/// Global custom provider list should be provided by the user. We shallow copy the user supplied dictionary into a ReadOnlyDictionary.
102-
/// Global custom provider list can only supplied once per application.
102+
/// Global custom provider list can only be supplied once per application.
103103
/// </summary>
104104
private static IReadOnlyDictionary<string, SqlColumnEncryptionKeyStoreProvider> s_globalCustomColumnEncryptionKeyStoreProviders;
105105

@@ -164,7 +164,7 @@ public SqlConnection(string connectionString) : this()
164164
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ctorConnectionStringCredential/*' />
165165
public SqlConnection(string connectionString, SqlCredential credential) : this()
166166
{
167-
ConnectionString = connectionString;
167+
ConnectionString = connectionString; // setting connection string first so that ConnectionOption is available
168168
if (credential != null)
169169
{
170170
// The following checks are necessary as setting Credential property will call CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential
@@ -370,7 +370,6 @@ private static void ValidateCustomProviders(IDictionary<string, SqlColumnEncrypt
370370
foreach (string key in customProviders.Keys)
371371
{
372372
// Validate the provider name
373-
//
374373
// Check for null or empty
375374
if (string.IsNullOrWhiteSpace(key))
376375
{
@@ -394,18 +393,17 @@ private static void ValidateCustomProviders(IDictionary<string, SqlColumnEncrypt
394393
/// <summary>
395394
/// Get enclave attestation url to be used with enclave based Always Encrypted
396395
/// </summary>
397-
internal string EnclaveAttestationUrl => ((SqlConnectionString)ConnectionOptions).EnclaveAttestationUrl;
396+
internal string EnclaveAttestationUrl
397+
{
398+
get => ((SqlConnectionString)ConnectionOptions).EnclaveAttestationUrl;
399+
}
398400

399401
/// <summary>
400402
/// Get attestation protocol
401403
/// </summary>
402404
internal SqlConnectionAttestationProtocol AttestationProtocol
403405
{
404-
get
405-
{
406-
SqlConnectionString opt = (SqlConnectionString)ConnectionOptions;
407-
return opt.AttestationProtocol;
408-
}
406+
get => ((SqlConnectionString)ConnectionOptions).AttestationProtocol;
409407
}
410408

411409
/// <summary>
@@ -658,6 +656,7 @@ public override string ConnectionString
658656
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ConnectionTimeout/*' />
659657
[ResDescription(StringsHelper.ResourceNames.SqlConnection_ConnectionTimeout)]
660658
[ResCategory(StringsHelper.ResourceNames.SqlConnection_DataSource)]
659+
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
661660
public override int ConnectionTimeout
662661
{
663662
get
@@ -688,7 +687,6 @@ public string AccessToken
688687
{
689688
get
690689
{
691-
string result = _accessToken;
692690
// When a connection is connecting or is ever opened, make AccessToken available only if "Persist Security Info" is set to true
693691
// otherwise, return null
694692
SqlConnectionString connectionOptions = (SqlConnectionString)UserConnectionOptions;
@@ -740,6 +738,7 @@ public Func<SqlAuthenticationParameters, CancellationToken, Task<SqlAuthenticati
740738
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/Database/*' />
741739
[ResDescription(StringsHelper.ResourceNames.SqlConnection_Database)]
742740
[ResCategory(StringsHelper.ResourceNames.SqlConnection_DataSource)]
741+
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
743742
public override string Database
744743
{
745744
// if the connection is open, we need to ask the inner connection what it's
@@ -766,6 +765,7 @@ public override string Database
766765
///
767766
/// To indicate the IsSupported flag sent by the server for DNS Caching. This property is for internal testing only.
768767
///
768+
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
769769
internal string SQLDNSCachingSupportedState
770770
{
771771
get
@@ -789,6 +789,7 @@ internal string SQLDNSCachingSupportedState
789789
///
790790
/// To indicate the IsSupported flag sent by the server for DNS Caching before redirection. This property is for internal testing only.
791791
///
792+
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
792793
internal string SQLDNSCachingSupportedStateBeforeRedirect
793794
{
794795
get
@@ -1079,7 +1080,7 @@ private void CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessToken(S
10791080
throw ADP.InvalidMixedUsageOfCredentialAndAccessToken();
10801081
}
10811082

1082-
if(_accessTokenCallback != null)
1083+
if (_accessTokenCallback != null)
10831084
{
10841085
throw ADP.InvalidMixedUsageOfAccessTokenAndTokenCallback();
10851086
}
@@ -1101,7 +1102,7 @@ private void CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessTokenCa
11011102
throw ADP.InvalidMixedUsageOfAccessTokenCallbackAndAuthentication();
11021103
}
11031104

1104-
if(_accessToken != null)
1105+
if (_accessToken != null)
11051106
{
11061107
throw ADP.InvalidMixedUsageOfAccessTokenAndTokenCallback();
11071108
}
@@ -1113,8 +1114,6 @@ protected override DbProviderFactory DbProviderFactory
11131114
get => SqlClientFactory.Instance;
11141115
}
11151116

1116-
// SqlCredential: Pair User Id and password in SecureString which are to be used for SQL authentication
1117-
11181117
//
11191118
// PUBLIC EVENTS
11201119
//
@@ -1155,11 +1154,11 @@ protected override void OnStateChange(StateChangeEventArgs stateChange)
11551154
new public SqlTransaction BeginTransaction()
11561155
{
11571156
// this is just a delegate. The actual method tracks executiontime
1158-
return BeginTransaction(System.Data.IsolationLevel.Unspecified, null);
1157+
return BeginTransaction(IsolationLevel.Unspecified, null);
11591158
}
11601159

11611160
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/BeginTransactionIso/*' />
1162-
new public SqlTransaction BeginTransaction(System.Data.IsolationLevel iso)
1161+
new public SqlTransaction BeginTransaction(IsolationLevel iso)
11631162
{
11641163
// this is just a delegate. The actual method tracks executiontime
11651164
return BeginTransaction(iso, null);
@@ -1172,18 +1171,18 @@ public SqlTransaction BeginTransaction(string transactionName)
11721171
// BEGIN...COMMIT or BEGIN...ROLLBACK statements. Transaction names
11731172
// are ignored for nested BEGIN's. The only way to rollback a nested
11741173
// transaction is to have a save point from a SAVE TRANSACTION call.
1175-
return BeginTransaction(System.Data.IsolationLevel.Unspecified, transactionName);
1174+
return BeginTransaction(IsolationLevel.Unspecified, transactionName);
11761175
}
11771176

11781177
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/BeginDbTransaction/*' />
11791178
[SuppressMessage("Microsoft.Reliability", "CA2004:RemoveCallsToGCKeepAlive")]
1180-
override protected DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
1179+
override protected DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
11811180
{
11821181
using (TryEventScope.Create("SqlConnection.BeginDbTransaction | API | Object Id {0}, Isolation Level {1}", ObjectID, (int)isolationLevel))
11831182
{
11841183
DbTransaction transaction = BeginTransaction(isolationLevel);
11851184

1186-
// InnerConnection doesn't maintain a ref on the outer connection (this) and
1185+
// VSTFDEVDIV# 560355 - InnerConnection doesn't maintain a ref on the outer connection (this) and
11871186
// subsequently leaves open the possibility that the outer connection could be GC'ed before the SqlTransaction
11881187
// is fully hooked up (leaving a DbTransaction with a null connection property). Ensure that this is reachable
11891188
// until the completion of BeginTransaction with KeepAlive
@@ -1194,7 +1193,7 @@ override protected DbTransaction BeginDbTransaction(System.Data.IsolationLevel i
11941193
}
11951194

11961195
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/BeginTransactionIsoTransactionName/*' />
1197-
public SqlTransaction BeginTransaction(System.Data.IsolationLevel iso, string transactionName)
1196+
public SqlTransaction BeginTransaction(IsolationLevel iso, string transactionName)
11981197
{
11991198
WaitForPendingReconnection();
12001199
SqlStatistics statistics = null;
@@ -1287,7 +1286,6 @@ public static void ClearPool(SqlConnection connection)
12871286
}
12881287
}
12891288

1290-
12911289
private void CloseInnerConnection()
12921290
{
12931291
// CloseConnection() now handles the lock
@@ -1625,6 +1623,7 @@ internal Task ValidateAndReconnect(Action beforeDisconnect, int timeout)
16251623
_originalConnectionId = ClientConnectionId;
16261624
SqlClientEventSource.Log.TryTraceEvent("SqlConnection.ValidateAndReconnect | Info | Connection Client Connection Id {0} is invalid, reconnecting", _originalConnectionId);
16271625
_recoverySessionData = cData;
1626+
16281627
if (beforeDisconnect != null)
16291628
{
16301629
beforeDisconnect();
@@ -1877,6 +1876,7 @@ internal void Retry(Task<DbConnectionInternal> retryTask)
18771876
{
18781877
SqlClientEventSource.Log.TryTraceEvent("SqlConnection.Retry | Info | Object Id {0}", _parent?.ObjectID);
18791878
_registration.Dispose();
1879+
18801880
try
18811881
{
18821882
SqlStatistics statistics = null;
@@ -2078,7 +2078,6 @@ internal bool HasLocalTransactionFromAPI
20782078
}
20792079
}
20802080

2081-
20822081
internal bool Is2008OrNewer
20832082
{
20842083
get
@@ -2100,7 +2099,6 @@ internal TdsParser Parser
21002099
}
21012100
}
21022101

2103-
21042102
//
21052103
// INTERNAL METHODS
21062104
//
@@ -2152,7 +2150,6 @@ internal void OnError(SqlException exception, bool breakConnection, Action<Actio
21522150
{
21532151
Debug.Assert(exception != null && exception.Errors.Count != 0, "SqlConnection: OnError called with null or empty exception!");
21542152

2155-
21562153
if (breakConnection && (ConnectionState.Open == State))
21572154
{
21582155
if (wrapCloseInAction != null)
@@ -2483,6 +2480,7 @@ private Assembly ResolveTypeAssembly(AssemblyName asmRef, bool throwOnError)
24832480
}
24842481
asmRef.Version = TypeSystemAssemblyVersion;
24852482
}
2483+
24862484
try
24872485
{
24882486
return Assembly.Load(asmRef);
@@ -2546,12 +2544,6 @@ internal object GetUdtValue(object value, SqlMetaDataPriv metaData, bool returnD
25462544
}
25472545
}
25482546

2549-
internal byte[] GetBytes(object o)
2550-
{
2551-
Format format = Format.Native;
2552-
return GetBytes(o, out format, out int maxSize);
2553-
}
2554-
25552547
internal byte[] GetBytes(object o, out Format format, out int maxSize)
25562548
{
25572549
SqlUdtInfo attr = GetInfoFromType(o.GetType());
@@ -2575,7 +2567,7 @@ internal byte[] GetBytes(object o, out Format format, out int maxSize)
25752567

25762568
private SqlUdtInfo GetInfoFromType(Type t)
25772569
{
2578-
Debug.Assert(t != null, "Type object cant be NULL");
2570+
Debug.Assert(t != null, "Type object can't be NULL");
25792571
Type orig = t;
25802572
do
25812573
{
@@ -2584,10 +2576,8 @@ private SqlUdtInfo GetInfoFromType(Type t)
25842576
{
25852577
return attr;
25862578
}
2587-
else
2588-
{
2589-
t = t.BaseType;
2590-
}
2579+
2580+
t = t.BaseType;
25912581
}
25922582
while (t != null);
25932583

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ private object ConvertValue(object value, _SqlMetaData metadata, bool isNull, re
16341634
// in byte[] form.
16351635
if (!(value is byte[]))
16361636
{
1637-
value = _connection.GetBytes(value);
1637+
value = _connection.GetBytes(value, out _, out _);
16381638
typeChanged = true;
16391639
}
16401640
break;

0 commit comments

Comments
 (0)