Skip to content

Commit 7939ea3

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

File tree

3 files changed

+63
-116
lines changed

3 files changed

+63
-116
lines changed

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

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Diagnostics;
77
using System.Globalization;
8+
using System.Linq;
89
using System.Reflection;
910
using Microsoft.Data.SqlClient;
1011

@@ -401,66 +402,41 @@ 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 Type s_IPAddressPreferenceType = typeof(SqlConnectionIPAddressPreference);
409+
private readonly static string[] s_PreferenceNames = Enum.GetNames(s_IPAddressPreferenceType);
410+
412411
/// <summary>
413-
/// Convert a string value to the corresponding IPAddressPreference
412+
/// Convert a string value to the corresponding IPAddressPreference.
414413
/// </summary>
415-
/// <param name="value"></param>
416-
/// <param name="result"></param>
417-
/// <returns></returns>
414+
/// <param name="value">The string representation of the enumeration name to convert.</param>
415+
/// <param name="result">When this method returns, `result` contains an object of type `SqlConnectionIPAddressPreference` whose value is represented by `value` if the operation succeeds.
416+
/// If the parse operation fails, `result` contains the default value of the `SqlConnectionIPAddressPreference` type.</param>
417+
/// <returns>`true` if the value parameter was converted successfully; otherwise, `false`.</returns>
418418
internal static bool TryConvertToIPAddressPreference(string value, out SqlConnectionIPAddressPreference result)
419419
{
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
420+
string temp = s_PreferenceNames.FirstOrDefault(x => x.Equals(value, StringComparison.InvariantCultureIgnoreCase));
421+
if (temp is not null)
436422
{
437-
result = DbConnectionStringDefaults.IPAddressPreference;
438-
return false;
423+
return Enum.TryParse(temp, true, out result);
439424
}
425+
426+
result = DbConnectionStringDefaults.IPAddressPreference;
427+
return false;
440428
}
441429

430+
/// <summary>
431+
/// Verifies if the `value` is defined in the expected Enum.
432+
/// </summary>
442433
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
434+
=> value == SqlConnectionIPAddressPreference.IPv4First
446435
|| value == SqlConnectionIPAddressPreference.IPv6First
447436
|| value == SqlConnectionIPAddressPreference.UsePlatformDefault;
448-
}
449437

450438
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-
}
439+
=> Enum.GetName(s_IPAddressPreferenceType, value);
464440

465441
internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(string keyword, object value)
466442
{
@@ -496,20 +472,20 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
496472
// explicitly block scenarios in which user tries to use wrong enum types, like:
497473
// builder["SqlConnectionIPAddressPreference"] = EnvironmentVariableTarget.Process;
498474
// workaround: explicitly cast non-SqlConnectionIPAddressPreference enums to int
499-
throw ADP.ConvertFailed(value.GetType(), typeof(SqlConnectionIPAddressPreference), null);
475+
throw ADP.ConvertFailed(value.GetType(), s_IPAddressPreferenceType, null);
500476
}
501477
else
502478
{
503479
try
504480
{
505481
// Enum.ToObject allows only integral and enum values (enums are blocked above), raising ArgumentException for the rest
506-
eValue = (SqlConnectionIPAddressPreference)Enum.ToObject(typeof(SqlConnectionIPAddressPreference), value);
482+
eValue = (SqlConnectionIPAddressPreference)Enum.ToObject(s_IPAddressPreferenceType, value);
507483
}
508484
catch (ArgumentException e)
509485
{
510486
// to be consistent with the messages we send in case of wrong type usage, replace
511487
// the error with our exception, and keep the original one as inner one for troubleshooting
512-
throw ADP.ConvertFailed(value.GetType(), typeof(SqlConnectionIPAddressPreference), e);
488+
throw ADP.ConvertFailed(value.GetType(), s_IPAddressPreferenceType, e);
513489
}
514490
}
515491

@@ -519,12 +495,10 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
519495
}
520496
else
521497
{
522-
throw ADP.InvalidEnumerationValue(typeof(SqlConnectionIPAddressPreference), (int)eValue);
498+
throw ADP.InvalidEnumerationValue(s_IPAddressPreferenceType, (int)eValue);
523499
}
524500
}
525501
}
526-
527-
528502
#endregion
529503

530504
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: 29 additions & 56 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,55 @@ 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 Type s_IPAddressPreferenceType = typeof(SqlConnectionIPAddressPreference);
1007+
private readonly static string[] s_PreferenceNames = Enum.GetNames(s_IPAddressPreferenceType);
1008+
10101009
/// <summary>
1011-
/// Convert a string value to the corresponding IPAddressPreference
1010+
/// Convert a string value to the corresponding IPAddressPreference.
10121011
/// </summary>
1013-
/// <param name="value"></param>
1014-
/// <param name="result"></param>
1015-
/// <returns></returns>
1012+
/// <param name="value">The string representation of the enumeration name to convert.</param>
1013+
/// <param name="result">When this method returns, `result` contains an object of type `SqlConnectionIPAddressPreference` whose value is represented by `value` if the operation succeeds.
1014+
/// If the parse operation fails, `result` contains the default value of the `SqlConnectionIPAddressPreference` type.</param>
1015+
/// <returns>`true` if the value parameter was converted successfully; otherwise, `false`.</returns>
10161016
internal static bool TryConvertToIPAddressPreference(string value, out SqlConnectionIPAddressPreference result)
10171017
{
1018-
if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreference46))
1019-
{
1020-
result = SqlConnectionIPAddressPreference.IPv4First;
1021-
return true;
1022-
}
1023-
else if (StringComparer.InvariantCultureIgnoreCase.Equals(value, IPAddrPreference64))
1018+
string temp = s_PreferenceNames.FirstOrDefault(x => x.Equals(value, StringComparison.InvariantCultureIgnoreCase));
1019+
if (temp is not null)
10241020
{
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
1034-
{
1035-
result = DbConnectionStringDefaults.IPAddressPreference;
1036-
return false;
1021+
return Enum.TryParse(temp, true, out result);
10371022
}
1023+
1024+
result = DbConnectionStringDefaults.IPAddressPreference;
1025+
return false;
10381026
}
10391027

1028+
/// <summary>
1029+
/// Verifies if the `value` is defined in the expected Enum.
1030+
/// </summary>
10401031
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
1032+
=> value == SqlConnectionIPAddressPreference.IPv4First
10441033
|| value == SqlConnectionIPAddressPreference.IPv6First
10451034
|| value == SqlConnectionIPAddressPreference.UsePlatformDefault;
1046-
}
10471035

10481036
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-
}
1037+
=> Enum.GetName(s_IPAddressPreferenceType, value);
10621038

10631039
internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(string keyword, object value)
10641040
{
1065-
if (null == value)
1041+
if (value is null)
10661042
{
10671043
return DbConnectionStringDefaults.IPAddressPreference; // IPv4First
10681044
}
10691045

10701046
string sValue = (value as string);
1071-
SqlConnectionIPAddressPreference result;
1072-
1073-
if (null != sValue)
1047+
if (sValue is not null)
10741048
{
10751049
// try again after remove leading & trailing whitespaces.
10761050
sValue = sValue.Trim();
1077-
if (TryConvertToIPAddressPreference(sValue, out result))
1051+
if (TryConvertToIPAddressPreference(sValue, out SqlConnectionIPAddressPreference result))
10781052
{
10791053
return result;
10801054
}
@@ -1087,29 +1061,29 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
10871061
// the value is not string, try other options
10881062
SqlConnectionIPAddressPreference eValue;
10891063

1090-
if (value is SqlConnectionIPAddressPreference)
1064+
if (value is SqlConnectionIPAddressPreference preference)
10911065
{
1092-
eValue = (SqlConnectionIPAddressPreference)value;
1066+
eValue = preference;
10931067
}
10941068
else if (value.GetType().IsEnum)
10951069
{
10961070
// explicitly block scenarios in which user tries to use wrong enum types, like:
10971071
// builder["SqlConnectionIPAddressPreference"] = EnvironmentVariableTarget.Process;
10981072
// workaround: explicitly cast non-SqlConnectionIPAddressPreference enums to int
1099-
throw ADP.ConvertFailed(value.GetType(), typeof(SqlConnectionIPAddressPreference), null);
1073+
throw ADP.ConvertFailed(value.GetType(), s_IPAddressPreferenceType, null);
11001074
}
11011075
else
11021076
{
11031077
try
11041078
{
11051079
// Enum.ToObject allows only integral and enum values (enums are blocked above), raising ArgumentException for the rest
1106-
eValue = (SqlConnectionIPAddressPreference)Enum.ToObject(typeof(SqlConnectionIPAddressPreference), value);
1080+
eValue = (SqlConnectionIPAddressPreference)Enum.ToObject(s_IPAddressPreferenceType, value);
11071081
}
11081082
catch (ArgumentException e)
11091083
{
11101084
// to be consistent with the messages we send in case of wrong type usage, replace
11111085
// the error with our exception, and keep the original one as inner one for troubleshooting
1112-
throw ADP.ConvertFailed(value.GetType(), typeof(SqlConnectionIPAddressPreference), e);
1086+
throw ADP.ConvertFailed(value.GetType(), s_IPAddressPreferenceType, e);
11131087
}
11141088
}
11151089

@@ -1119,11 +1093,10 @@ internal static SqlConnectionIPAddressPreference ConvertToIPAddressPreference(st
11191093
}
11201094
else
11211095
{
1122-
throw ADP.InvalidEnumerationValue(typeof(SqlConnectionIPAddressPreference), (int)eValue);
1096+
throw ADP.InvalidEnumerationValue(s_IPAddressPreferenceType, (int)eValue);
11231097
}
11241098
}
11251099
}
1126-
11271100
#endregion
11281101

11291102
internal static bool IsValidCertificateValue(string value)

0 commit comments

Comments
 (0)