Skip to content

Commit c41b411

Browse files
author
Davoud Eshtehari
committed
Address comments
+ improvement
1 parent 4a1d7f7 commit c41b411

File tree

3 files changed

+62
-104
lines changed

3 files changed

+62
-104
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/Common/DbConnectionStringCommon.cs

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Diagnostics;
78
using System.Globalization;
9+
using System.Linq;
810
using System.Reflection;
911
using Microsoft.Data.SqlClient;
1012

@@ -401,66 +403,46 @@ internal static SqlConnectionAttestationProtocol ConvertToAttestationProtocol(st
401403
#endregion
402404

403405
#region <<IPAddressPreference Utility>>
404-
405406
/// <summary>
406407
/// IP Address Preference.
407408
/// </summary>
408-
const string IPAddrPreference46 = "IPv4First";
409-
const string IPAddrPreference64 = "IPv6First";
410-
const string IPAddrPreferenceOS = "UsePlatformDefault";
411-
409+
private readonly static Dictionary<string, SqlConnectionIPAddressPreference> s_preferenceNames = new(StringComparer.InvariantCultureIgnoreCase);
410+
411+
static DbConnectionStringBuilderUtil()
412+
{
413+
foreach (SqlConnectionIPAddressPreference item in Enum.GetValues(typeof(SqlConnectionIPAddressPreference)))
414+
{
415+
s_preferenceNames.Add(item.ToString(), item);
416+
}
417+
}
418+
412419
/// <summary>
413-
/// Convert a string value to the corresponding IPAddressPreference
420+
/// Convert a string value to the corresponding IPAddressPreference.
414421
/// </summary>
415-
/// <param name="value"></param>
416-
/// <param name="result"></param>
417-
/// <returns></returns>
422+
/// <param name="value">The string representation of the enumeration name to convert.</param>
423+
/// <param name="result">When this method returns, `result` contains an object of type `SqlConnectionIPAddressPreference` whose value is represented by `value` if the operation succeeds.
424+
/// If the parse operation fails, `result` contains the default value of the `SqlConnectionIPAddressPreference` type.</param>
425+
/// <returns>`true` if the value parameter was converted successfully; otherwise, `false`.</returns>
418426
internal static bool TryConvertToIPAddressPreference(string value, out SqlConnectionIPAddressPreference result)
419427
{
420-
if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreference46))
421-
{
422-
result = SqlConnectionIPAddressPreference.IPv4First;
423-
return true;
424-
}
425-
else if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreference64))
426-
{
427-
result = SqlConnectionIPAddressPreference.IPv6First;
428-
return true;
429-
}
430-
else if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreferenceOS))
431-
{
432-
result = SqlConnectionIPAddressPreference.UsePlatformDefault;
433-
return true;
434-
}
435-
else
428+
if (!s_preferenceNames.TryGetValue(value, out result))
436429
{
437430
result = DbConnectionStringDefaults.IPAddressPreference;
438431
return false;
439432
}
433+
return true;
440434
}
441435

436+
/// <summary>
437+
/// Verifies if the `value` is defined in the expected Enum.
438+
/// </summary>
442439
internal static bool IsValidIPAddressPreference(SqlConnectionIPAddressPreference value)
443-
{
444-
Debug.Assert(Enum.GetNames(typeof(SqlConnectionIPAddressPreference)).Length == 3, "SqlConnectionIPAddressPreference enum has changed, update needed");
445-
return value == SqlConnectionIPAddressPreference.IPv4First
440+
=> value == SqlConnectionIPAddressPreference.IPv4First
446441
|| value == SqlConnectionIPAddressPreference.IPv6First
447442
|| value == SqlConnectionIPAddressPreference.UsePlatformDefault;
448-
}
449443

450444
internal static string IPAddressPreferenceToString(SqlConnectionIPAddressPreference value)
451-
{
452-
Debug.Assert(IsValidIPAddressPreference(value), "value is not a valid IP address preference");
453-
454-
switch (value)
455-
{
456-
case SqlConnectionIPAddressPreference.UsePlatformDefault:
457-
return IPAddrPreferenceOS;
458-
case SqlConnectionIPAddressPreference.IPv6First:
459-
return IPAddrPreference64;
460-
default:
461-
return IPAddrPreference46;
462-
}
463-
}
445+
=> Enum.GetName(typeof(SqlConnectionIPAddressPreference), value);
464446

465447
internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(string keyword, object value)
466448
{
@@ -523,8 +505,6 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
523505
}
524506
}
525507
}
526-
527-
528508
#endregion
529509

530510
internal static bool IsValidApplicationIntentValue(ApplicationIntent value)

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,26 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel
176176
int portRetry = string.IsNullOrEmpty(cachedDNSInfo.Port) ? port : int.Parse(cachedDNSInfo.Port);
177177
SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Retrying with cached DNS IP Address {1} and port {2}", args0: _connectionId, args1: cachedDNSInfo.AddrIPv4, args2: cachedDNSInfo.Port);
178178

179-
string cachedIPA;
180-
string cachedIPB;
179+
string firstCachedIP;
180+
string secondCachedIP;
181181

182182
if (SqlConnectionIPAddressPreference.IPv6First == ipPreference) {
183-
cachedIPA = cachedDNSInfo.AddrIPv6;
184-
cachedIPB = cachedDNSInfo.AddrIPv4;
183+
firstCachedIP = cachedDNSInfo.AddrIPv6;
184+
secondCachedIP = cachedDNSInfo.AddrIPv4;
185185
} else {
186-
cachedIPA = cachedDNSInfo.AddrIPv4;
187-
cachedIPB = cachedDNSInfo.AddrIPv6;
186+
firstCachedIP = cachedDNSInfo.AddrIPv4;
187+
secondCachedIP = cachedDNSInfo.AddrIPv6;
188188
}
189189

190190
try
191191
{
192192
if (parallel)
193193
{
194-
_socket = TryConnectParallel(cachedIPA, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo);
194+
_socket = TryConnectParallel(firstCachedIP, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo);
195195
}
196196
else
197197
{
198-
_socket = Connect(cachedIPA, portRetry, ts, isInfiniteTimeOut, ipPreference, cachedFQDN, ref pendingDNSInfo);
198+
_socket = Connect(firstCachedIP, portRetry, ts, isInfiniteTimeOut, ipPreference, cachedFQDN, ref pendingDNSInfo);
199199
}
200200
}
201201
catch (Exception exRetry)
@@ -206,11 +206,11 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel
206206
SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Retrying exception {1}", args0: _connectionId, args1: exRetry?.Message);
207207
if (parallel)
208208
{
209-
_socket = TryConnectParallel(cachedIPB, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo);
209+
_socket = TryConnectParallel(secondCachedIP, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo);
210210
}
211211
else
212212
{
213-
_socket = Connect(cachedIPB, portRetry, ts, isInfiniteTimeOut, ipPreference, cachedFQDN, ref pendingDNSInfo);
213+
_socket = Connect(secondCachedIP, portRetry, ts, isInfiniteTimeOut, ipPreference, cachedFQDN, ref pendingDNSInfo);
214214
}
215215
}
216216
else

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/DbConnectionStringCommon.cs

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.Globalization;
9+
using System.Linq;
910
using Microsoft.Data.SqlClient;
1011

1112
namespace Microsoft.Data.Common
@@ -999,82 +1000,60 @@ internal static SqlConnectionAttestationProtocol ConvertToAttestationProtocol(st
9991000
#endregion
10001001

10011002
#region <<IPAddressPreference Utility>>
1002-
10031003
/// <summary>
10041004
/// IP Address Preference.
10051005
/// </summary>
1006-
const string IPAddrPreference46 = "IPv4First";
1007-
const string IPAddrPreference64 = "IPv6First";
1008-
const string IPAddrPreferenceOS = "UsePlatformDefault";
1009-
1006+
private readonly static Dictionary<string, SqlConnectionIPAddressPreference> s_preferenceNames = new(StringComparer.InvariantCultureIgnoreCase);
1007+
1008+
static DbConnectionStringBuilderUtil()
1009+
{
1010+
foreach (SqlConnectionIPAddressPreference item in Enum.GetValues(typeof(SqlConnectionIPAddressPreference)))
1011+
{
1012+
s_preferenceNames.Add(item.ToString(), item);
1013+
}
1014+
}
1015+
10101016
/// <summary>
1011-
/// Convert a string value to the corresponding IPAddressPreference
1017+
/// Convert a string value to the corresponding IPAddressPreference.
10121018
/// </summary>
1013-
/// <param name="value"></param>
1014-
/// <param name="result"></param>
1015-
/// <returns></returns>
1019+
/// <param name="value">The string representation of the enumeration name to convert.</param>
1020+
/// <param name="result">When this method returns, `result` contains an object of type `SqlConnectionIPAddressPreference` whose value is represented by `value` if the operation succeeds.
1021+
/// If the parse operation fails, `result` contains the default value of the `SqlConnectionIPAddressPreference` type.</param>
1022+
/// <returns>`true` if the value parameter was converted successfully; otherwise, `false`.</returns>
10161023
internal static bool TryConvertToIPAddressPreference(string value, out SqlConnectionIPAddressPreference result)
10171024
{
1018-
if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreference46))
1019-
{
1020-
result = SqlConnectionIPAddressPreference.IPv4First;
1021-
return true;
1022-
}
1023-
else if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreference64))
1024-
{
1025-
result = SqlConnectionIPAddressPreference.IPv6First;
1026-
return true;
1027-
}
1028-
else if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreferenceOS))
1029-
{
1030-
result = SqlConnectionIPAddressPreference.UsePlatformDefault;
1031-
return true;
1032-
}
1033-
else
1025+
if (!s_preferenceNames.TryGetValue(value, out result))
10341026
{
10351027
result = DbConnectionStringDefaults.IPAddressPreference;
10361028
return false;
10371029
}
1030+
return true;
10381031
}
10391032

1033+
/// <summary>
1034+
/// Verifies if the `value` is defined in the expected Enum.
1035+
/// </summary>
10401036
internal static bool IsValidIPAddressPreference(SqlConnectionIPAddressPreference value)
1041-
{
1042-
Debug.Assert(Enum.GetNames(typeof(SqlConnectionIPAddressPreference)).Length == 3, "SqlConnectionIPAddressPreference enum has changed, update needed");
1043-
return value == SqlConnectionIPAddressPreference.IPv4First
1037+
=> value == SqlConnectionIPAddressPreference.IPv4First
10441038
|| value == SqlConnectionIPAddressPreference.IPv6First
10451039
|| value == SqlConnectionIPAddressPreference.UsePlatformDefault;
1046-
}
10471040

10481041
internal static string IPAddressPreferenceToString(SqlConnectionIPAddressPreference value)
1049-
{
1050-
Debug.Assert(IsValidIPAddressPreference(value), "value is not a valid IP address preference");
1051-
1052-
switch (value)
1053-
{
1054-
case SqlConnectionIPAddressPreference.UsePlatformDefault:
1055-
return IPAddrPreferenceOS;
1056-
case SqlConnectionIPAddressPreference.IPv6First:
1057-
return IPAddrPreference64;
1058-
default:
1059-
return IPAddrPreference46;
1060-
}
1061-
}
1042+
=> Enum.GetName(typeof(SqlConnectionIPAddressPreference), value);
10621043

10631044
internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(string keyword, object value)
10641045
{
1065-
if (null == value)
1046+
if (value is null)
10661047
{
10671048
return DbConnectionStringDefaults.IPAddressPreference; // IPv4First
10681049
}
10691050

10701051
string sValue = (value as string);
1071-
SqlConnectionIPAddressPreference result;
1072-
1073-
if (null != sValue)
1052+
if (sValue is not null)
10741053
{
10751054
// try again after remove leading & trailing whitespaces.
10761055
sValue = sValue.Trim();
1077-
if (TryConvertToIPAddressPreference(sValue, out result))
1056+
if (TryConvertToIPAddressPreference(sValue, out SqlConnectionIPAddressPreference result))
10781057
{
10791058
return result;
10801059
}
@@ -1087,9 +1066,9 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
10871066
// the value is not string, try other options
10881067
SqlConnectionIPAddressPreference eValue;
10891068

1090-
if (value is SqlConnectionIPAddressPreference)
1069+
if (value is SqlConnectionIPAddressPreference preference)
10911070
{
1092-
eValue = (SqlConnectionIPAddressPreference)value;
1071+
eValue = preference;
10931072
}
10941073
else if (value.GetType().IsEnum)
10951074
{
@@ -1123,7 +1102,6 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
11231102
}
11241103
}
11251104
}
1126-
11271105
#endregion
11281106

11291107
internal static bool IsValidCertificateValue(string value)

0 commit comments

Comments
 (0)