Skip to content

Commit c71143c

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

File tree

3 files changed

+60
-104
lines changed

3 files changed

+60
-104
lines changed

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

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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;
89
using System.Reflection;
@@ -401,66 +402,46 @@ internal static SqlConnectionAttestationProtocol ConvertToAttestationProtocol(st
401402
#endregion
402403

403404
#region <<IPAddressPreference Utility>>
404-
405405
/// <summary>
406406
/// IP Address Preference.
407407
/// </summary>
408-
const string IPAddrPreference46 = "IPv4First";
409-
const string IPAddrPreference64 = "IPv6First";
410-
const string IPAddrPreferenceOS = "UsePlatformDefault";
411-
408+
private readonly static Dictionary<string, SqlConnectionIPAddressPreference> s_preferenceNames = new(StringComparer.InvariantCultureIgnoreCase);
409+
410+
static DbConnectionStringBuilderUtil()
411+
{
412+
foreach (SqlConnectionIPAddressPreference item in Enum.GetValues(typeof(SqlConnectionIPAddressPreference)))
413+
{
414+
s_preferenceNames.Add(item.ToString(), item);
415+
}
416+
}
417+
412418
/// <summary>
413-
/// Convert a string value to the corresponding IPAddressPreference
419+
/// Convert a string value to the corresponding IPAddressPreference.
414420
/// </summary>
415-
/// <param name="value"></param>
416-
/// <param name="result"></param>
417-
/// <returns></returns>
421+
/// <param name="value">The string representation of the enumeration name to convert.</param>
422+
/// <param name="result">When this method returns, `result` contains an object of type `SqlConnectionIPAddressPreference` whose value is represented by `value` if the operation succeeds.
423+
/// If the parse operation fails, `result` contains the default value of the `SqlConnectionIPAddressPreference` type.</param>
424+
/// <returns>`true` if the value parameter was converted successfully; otherwise, `false`.</returns>
418425
internal static bool TryConvertToIPAddressPreference(string value, out SqlConnectionIPAddressPreference result)
419426
{
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
427+
if (!s_preferenceNames.TryGetValue(value, out result))
436428
{
437429
result = DbConnectionStringDefaults.IPAddressPreference;
438430
return false;
439431
}
432+
return true;
440433
}
441434

435+
/// <summary>
436+
/// Verifies if the `value` is defined in the expected Enum.
437+
/// </summary>
442438
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
439+
=> value == SqlConnectionIPAddressPreference.IPv4First
446440
|| value == SqlConnectionIPAddressPreference.IPv6First
447441
|| value == SqlConnectionIPAddressPreference.UsePlatformDefault;
448-
}
449442

450443
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-
}
444+
=> Enum.GetName(typeof(SqlConnectionIPAddressPreference), value);
464445

465446
internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(string keyword, object value)
466447
{
@@ -523,8 +504,6 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
523504
}
524505
}
525506
}
526-
527-
528507
#endregion
529508

530509
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: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -999,82 +999,60 @@ internal static SqlConnectionAttestationProtocol ConvertToAttestationProtocol(st
999999
#endregion
10001000

10011001
#region <<IPAddressPreference Utility>>
1002-
10031002
/// <summary>
10041003
/// IP Address Preference.
10051004
/// </summary>
1006-
const string IPAddrPreference46 = "IPv4First";
1007-
const string IPAddrPreference64 = "IPv6First";
1008-
const string IPAddrPreferenceOS = "UsePlatformDefault";
1009-
1005+
private readonly static Dictionary<string, SqlConnectionIPAddressPreference> s_preferenceNames = new(StringComparer.InvariantCultureIgnoreCase);
1006+
1007+
static DbConnectionStringBuilderUtil()
1008+
{
1009+
foreach (SqlConnectionIPAddressPreference item in Enum.GetValues(typeof(SqlConnectionIPAddressPreference)))
1010+
{
1011+
s_preferenceNames.Add(item.ToString(), item);
1012+
}
1013+
}
1014+
10101015
/// <summary>
1011-
/// Convert a string value to the corresponding IPAddressPreference
1016+
/// Convert a string value to the corresponding IPAddressPreference.
10121017
/// </summary>
1013-
/// <param name="value"></param>
1014-
/// <param name="result"></param>
1015-
/// <returns></returns>
1018+
/// <param name="value">The string representation of the enumeration name to convert.</param>
1019+
/// <param name="result">When this method returns, `result` contains an object of type `SqlConnectionIPAddressPreference` whose value is represented by `value` if the operation succeeds.
1020+
/// If the parse operation fails, `result` contains the default value of the `SqlConnectionIPAddressPreference` type.</param>
1021+
/// <returns>`true` if the value parameter was converted successfully; otherwise, `false`.</returns>
10161022
internal static bool TryConvertToIPAddressPreference(string value, out SqlConnectionIPAddressPreference result)
10171023
{
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
1024+
if (!s_preferenceNames.TryGetValue(value, out result))
10341025
{
10351026
result = DbConnectionStringDefaults.IPAddressPreference;
10361027
return false;
10371028
}
1029+
return true;
10381030
}
10391031

1032+
/// <summary>
1033+
/// Verifies if the `value` is defined in the expected Enum.
1034+
/// </summary>
10401035
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
1036+
=> value == SqlConnectionIPAddressPreference.IPv4First
10441037
|| value == SqlConnectionIPAddressPreference.IPv6First
10451038
|| value == SqlConnectionIPAddressPreference.UsePlatformDefault;
1046-
}
10471039

10481040
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-
}
1041+
=> Enum.GetName(typeof(SqlConnectionIPAddressPreference), value);
10621042

10631043
internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(string keyword, object value)
10641044
{
1065-
if (null == value)
1045+
if (value is null)
10661046
{
10671047
return DbConnectionStringDefaults.IPAddressPreference; // IPv4First
10681048
}
10691049

10701050
string sValue = (value as string);
1071-
SqlConnectionIPAddressPreference result;
1072-
1073-
if (null != sValue)
1051+
if (sValue is not null)
10741052
{
10751053
// try again after remove leading & trailing whitespaces.
10761054
sValue = sValue.Trim();
1077-
if (TryConvertToIPAddressPreference(sValue, out result))
1055+
if (TryConvertToIPAddressPreference(sValue, out SqlConnectionIPAddressPreference result))
10781056
{
10791057
return result;
10801058
}
@@ -1087,9 +1065,9 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
10871065
// the value is not string, try other options
10881066
SqlConnectionIPAddressPreference eValue;
10891067

1090-
if (value is SqlConnectionIPAddressPreference)
1068+
if (value is SqlConnectionIPAddressPreference preference)
10911069
{
1092-
eValue = (SqlConnectionIPAddressPreference)value;
1070+
eValue = preference;
10931071
}
10941072
else if (value.GetType().IsEnum)
10951073
{
@@ -1123,7 +1101,6 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
11231101
}
11241102
}
11251103
}
1126-
11271104
#endregion
11281105

11291106
internal static bool IsValidCertificateValue(string value)

0 commit comments

Comments
 (0)