Fix Comparable implementation for ConnectionDetails (#1739)

This commit is contained in:
Cheena Malhotra
2022-10-31 18:32:40 -07:00
committed by GitHub
parent 2f2f65c89f
commit 36965f6355
2 changed files with 67 additions and 32 deletions

View File

@@ -22,7 +22,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
/// <summary> /// <summary>
/// Gets or sets the connection password /// Gets or sets the connection password
/// </summary> /// </summary>
public string Password public string Password
{ {
get get
{ {
@@ -155,7 +155,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
get get
{ {
string? value = GetOptionValue<string?>("encrypt"); string? value = GetOptionValue<string?>("encrypt");
if(string.IsNullOrEmpty(value)) if (string.IsNullOrEmpty(value))
{ {
// Accept boolean values for backwards compatibility. // Accept boolean values for backwards compatibility.
value = GetOptionValue<bool?>("encrypt")?.ToString(); value = GetOptionValue<bool?>("encrypt")?.ToString();
@@ -503,7 +503,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
{ {
SetOptionValue("port", value); SetOptionValue("port", value);
} }
} }
/// <summary> /// <summary>
/// Gets or sets a string value that indicates the type system the application expects. /// Gets or sets a string value that indicates the type system the application expects.
@@ -540,7 +540,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
/// <summary> /// <summary>
/// Gets or sets the group ID /// Gets or sets the group ID
/// </summary> /// </summary>
public string GroupId public string GroupId
{ {
get get
{ {
@@ -555,7 +555,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
/// <summary> /// <summary>
/// Gets or sets the database display name /// Gets or sets the database display name
/// </summary> /// </summary>
public string DatabaseDisplayName public string DatabaseDisplayName
{ {
get get
{ {
@@ -566,7 +566,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
SetOptionValue("databaseDisplayName", value); SetOptionValue("databaseDisplayName", value);
} }
} }
public string AzureAccountToken public string AzureAccountToken
{ {
get get
@@ -591,32 +591,47 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
} }
} }
/// <summary>
/// Compares all SQL Server Connection properties to be able to identify differences in current instance and provided instance appropriately.
/// </summary>
/// <param name="other">Instance to compare with.</param>
/// <returns>True if comparison yeilds no differences, otherwise false.</returns>
public bool IsComparableTo(ConnectionDetails other) public bool IsComparableTo(ConnectionDetails other)
{ => other != null
if (other == null) && string.Equals(ApplicationIntent, other.ApplicationIntent, System.StringComparison.InvariantCultureIgnoreCase)
{ && string.Equals(ApplicationName, other.ApplicationName, System.StringComparison.InvariantCultureIgnoreCase)
return false; && string.Equals(AttachDbFilename, other.AttachDbFilename, System.StringComparison.InvariantCultureIgnoreCase)
} && string.Equals(AuthenticationType, other.AuthenticationType, System.StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(AzureAccountToken, other.AzureAccountToken, System.StringComparison.InvariantCultureIgnoreCase)
if (ServerName != other.ServerName && string.Equals(ColumnEncryptionSetting, other.ColumnEncryptionSetting, System.StringComparison.InvariantCultureIgnoreCase)
|| AuthenticationType != other.AuthenticationType && string.Equals(ConnectionString, other.ConnectionString, System.StringComparison.InvariantCultureIgnoreCase)
|| UserName != other.UserName && ConnectRetryCount == other.ConnectRetryCount
|| AzureAccountToken != other.AzureAccountToken) && ConnectRetryInterval == other.ConnectRetryInterval
{ && ConnectTimeout == other.ConnectTimeout
return false; && string.Equals(CurrentLanguage, other.CurrentLanguage, System.StringComparison.InvariantCultureIgnoreCase)
} && string.Equals(DatabaseDisplayName, other.DatabaseDisplayName, System.StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(DatabaseName, other.DatabaseName, System.StringComparison.InvariantCultureIgnoreCase)
// For database name, only compare if neither is empty. This is important && string.Equals(EnclaveAttestationProtocol, other.EnclaveAttestationProtocol, System.StringComparison.InvariantCultureIgnoreCase)
// Since it allows for handling of connections to the default database, but is && string.Equals(EnclaveAttestationUrl, other.EnclaveAttestationUrl, System.StringComparison.InvariantCultureIgnoreCase)
// not a 100% accurate heuristic. && string.Equals(Encrypt, other.Encrypt, System.StringComparison.InvariantCultureIgnoreCase)
if (!string.IsNullOrEmpty(DatabaseName) && ExpiresOn == other.ExpiresOn
&& !string.IsNullOrEmpty(other.DatabaseName) && string.Equals(FailoverPartner, other.FailoverPartner, System.StringComparison.InvariantCultureIgnoreCase)
&& DatabaseName != other.DatabaseName) && string.Equals(HostNameInCertificate, other.HostNameInCertificate, System.StringComparison.InvariantCultureIgnoreCase)
{ && LoadBalanceTimeout == other.LoadBalanceTimeout
return false; && MaxPoolSize == other.MaxPoolSize
} && MinPoolSize == other.MinPoolSize
&& MultipleActiveResultSets == other.MultipleActiveResultSets
return true; && MultiSubnetFailover == other.MultiSubnetFailover
} && PacketSize == other.PacketSize
&& string.Equals(Password, other.Password, System.StringComparison.InvariantCultureIgnoreCase)
&& PersistSecurityInfo == other.PersistSecurityInfo
&& Pooling == other.Pooling
&& Port == other.Port
&& Replication == other.Replication
&& string.Equals(ServerName, other.ServerName, System.StringComparison.InvariantCultureIgnoreCase)
&& TrustServerCertificate == other.TrustServerCertificate
&& string.Equals(TypeSystemVersion, other.TypeSystemVersion, System.StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(UserName, other.UserName, System.StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(WorkstationId, other.WorkstationId, System.StringComparison.InvariantCultureIgnoreCase);
} }
} }

View File

@@ -261,5 +261,25 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
Assert.That(details.ConnectTimeout, Is.EqualTo(expectedValue), "Connect Timeout not as expected"); Assert.That(details.ConnectTimeout, Is.EqualTo(expectedValue), "Connect Timeout not as expected");
Assert.That(details.Encrypt, Is.EqualTo("Mandatory"), "Encrypt should be mandatory."); Assert.That(details.Encrypt, Is.EqualTo("Mandatory"), "Encrypt should be mandatory.");
} }
[Test]
public void ConnectionSettingsComparableShouldConsiderAdvancedOptions()
{
ConnectionDetails details1 = new ConnectionDetails()
{
ServerName = "Server1",
DatabaseName = "Database",
AuthenticationType = "Integrated",
Encrypt = "Mandatory",
TrustServerCertificate = true
};
ConnectionDetails details2 = details1.Clone();
details2.ConnectTimeout = 60;
Assert.That(details1.IsComparableTo(details2), Is.False, "Different Connection Settings must not be comparable.");
details1 = details2.Clone();
Assert.That(details1.IsComparableTo(details2), Is.True, "Same Connection Settings must be comparable.");
}
} }
} }