Added support for most sql connection string properties

This commit is contained in:
Mitchell Sternke
2016-08-30 17:31:34 -07:00
parent 75b3235662
commit 3fe6e330fe
4 changed files with 298 additions and 4 deletions

View File

@@ -353,10 +353,123 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
connectionBuilder["Integrated Security"] = false;
connectionBuilder["User Id"] = connectionDetails.UserName;
connectionBuilder["Password"] = connectionDetails.Password;
if( !String.IsNullOrEmpty(connectionDetails.DatabaseName) )
// Check for any optional parameters
if (!String.IsNullOrEmpty(connectionDetails.DatabaseName))
{
connectionBuilder["Initial Catalog"] = connectionDetails.DatabaseName;
}
if (!String.IsNullOrEmpty(connectionDetails.AuthenticationType))
{
switch(connectionDetails.AuthenticationType)
{
case "Integrated":
connectionBuilder.IntegratedSecurity = true;
break;
case "SqlLogin":
connectionBuilder.IntegratedSecurity = false;
break;
default:
throw new ArgumentException("Invalid value \"" + connectionDetails.AuthenticationType + "\" for AuthenticationType. Valid values are \"Integrated\" and \"SqlLogin\".");
}
}
if (connectionDetails.Encrypt.HasValue)
{
connectionBuilder.Encrypt = connectionDetails.Encrypt.Value;
}
if (connectionDetails.TrustServerCertificate.HasValue)
{
connectionBuilder.TrustServerCertificate = connectionDetails.TrustServerCertificate.Value;
}
if (connectionDetails.PersistSecurityInfo.HasValue)
{
connectionBuilder.PersistSecurityInfo = connectionDetails.PersistSecurityInfo.Value;
}
if (connectionDetails.ConnectTimeout.HasValue)
{
connectionBuilder.ConnectTimeout = connectionDetails.ConnectTimeout.Value;
}
if (connectionDetails.ConnectRetryCount.HasValue)
{
connectionBuilder.ConnectRetryCount = connectionDetails.ConnectRetryCount.Value;
}
if (connectionDetails.ConnectRetryInterval.HasValue)
{
connectionBuilder.ConnectRetryInterval = connectionDetails.ConnectRetryInterval.Value;
}
if (!String.IsNullOrEmpty(connectionDetails.ApplicationName))
{
connectionBuilder.ApplicationName = connectionDetails.ApplicationName;
}
if (!String.IsNullOrEmpty(connectionDetails.WorkstationId))
{
connectionBuilder.WorkstationID = connectionDetails.WorkstationId;
}
if (!String.IsNullOrEmpty(connectionDetails.ApplicationIntent))
{
ApplicationIntent intent;
switch (connectionDetails.ApplicationIntent)
{
case "ReadOnly":
intent = ApplicationIntent.ReadOnly;
break;
case "ReadWrite":
intent = ApplicationIntent.ReadWrite;
break;
default:
throw new ArgumentException("Invalid value \"" + connectionDetails.ApplicationIntent + "\" for ApplicationIntent. Valid values are \"ReadWrite\" and \"ReadOnly\".");
}
connectionBuilder.ApplicationIntent = intent;
}
if (!String.IsNullOrEmpty(connectionDetails.CurrentLanguage))
{
connectionBuilder.CurrentLanguage = connectionDetails.CurrentLanguage;
}
if (connectionDetails.Pooling.HasValue)
{
connectionBuilder.Pooling = connectionDetails.Pooling.Value;
}
if (connectionDetails.MaxPoolSize.HasValue)
{
connectionBuilder.MaxPoolSize = connectionDetails.MaxPoolSize.Value;
}
if (connectionDetails.MinPoolSize.HasValue)
{
connectionBuilder.MinPoolSize = connectionDetails.MinPoolSize.Value;
}
if (connectionDetails.LoadBalanceTimeout.HasValue)
{
connectionBuilder.LoadBalanceTimeout = connectionDetails.LoadBalanceTimeout.Value;
}
if (connectionDetails.Replication.HasValue)
{
connectionBuilder.Replication = connectionDetails.Replication.Value;
}
if (!String.IsNullOrEmpty(connectionDetails.AttachDbFilename))
{
connectionBuilder.AttachDBFilename = connectionDetails.AttachDbFilename;
}
if (!String.IsNullOrEmpty(connectionDetails.FailoverPartner))
{
connectionBuilder.FailoverPartner = connectionDetails.FailoverPartner;
}
if (connectionDetails.MultiSubnetFailover.HasValue)
{
connectionBuilder.MultiSubnetFailover = connectionDetails.MultiSubnetFailover.Value;
}
if (connectionDetails.MultipleActiveResultSets.HasValue)
{
connectionBuilder.MultipleActiveResultSets = connectionDetails.MultipleActiveResultSets.Value;
}
if (connectionDetails.PacketSize.HasValue)
{
connectionBuilder.PacketSize = connectionDetails.PacketSize.Value;
}
if (!String.IsNullOrEmpty(connectionDetails.TypeSystemVersion))
{
connectionBuilder.TypeSystemVersion = connectionDetails.TypeSystemVersion;
}
return connectionBuilder.ToString();
}
}

View File

@@ -8,6 +8,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
/// <summary>
/// Message format for the initial connection request
/// </summary>
/// <remarks>
/// If this contract is ever changed, be sure to update ConnectionDetailsExtensions methods.
/// </remarks>
public class ConnectionDetails : ConnectionSummary
{
/// <summary>
@@ -16,6 +19,114 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
/// <returns></returns>
public string Password { get; set; }
// TODO Handle full set of properties
/// <summary>
/// Gets or sets the authentication to use.
/// </summary>
public string AuthenticationType { get; set; }
/// <summary>
/// Gets or sets a Boolean value that indicates whether SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed.
/// </summary>
public bool? Encrypt { get; set; }
/// <summary>
/// Gets or sets a value that indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust.
/// </summary>
public bool? TrustServerCertificate { get; set; }
/// <summary>
/// Gets or sets a Boolean value that indicates if security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state.
/// </summary>
public bool? PersistSecurityInfo { get; set; }
/// <summary>
/// Gets or sets the length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
/// </summary>
public int? ConnectTimeout { get; set; }
/// <summary>
/// The number of reconnections attempted after identifying that there was an idle connection failure.
/// </summary>
public int? ConnectRetryCount { get; set; }
/// <summary>
/// Amount of time (in seconds) between each reconnection attempt after identifying that there was an idle connection failure.
/// </summary>
public int? ConnectRetryInterval { get; set; }
/// <summary>
/// Gets or sets the name of the application associated with the connection string.
/// </summary>
public string ApplicationName { get; set; }
/// <summary>
/// Gets or sets the name of the workstation connecting to SQL Server.
/// </summary>
public string WorkstationId { get; set; }
/// <summary>
/// Declares the application workload type when connecting to a database in an SQL Server Availability Group.
/// </summary>
public string ApplicationIntent { get; set; }
/// <summary>
/// Gets or sets the SQL Server Language record name.
/// </summary>
public string CurrentLanguage { get; set; }
/// <summary>
/// Gets or sets a Boolean value that indicates whether the connection will be pooled or explicitly opened every time that the connection is requested.
/// </summary>
public bool? Pooling { get; set; }
/// <summary>
/// Gets or sets the maximum number of connections allowed in the connection pool for this specific connection string.
/// </summary>
public int? MaxPoolSize { get; set; }
/// <summary>
/// Gets or sets the minimum number of connections allowed in the connection pool for this specific connection string.
/// </summary>
public int? MinPoolSize { get; set; }
/// <summary>
/// Gets or sets the minimum time, in seconds, for the connection to live in the connection pool before being destroyed.
/// </summary>
public int? LoadBalanceTimeout { get; set; }
/// <summary>
/// Gets or sets a Boolean value that indicates whether replication is supported using the connection.
/// </summary>
public bool? Replication { get; set; }
/// <summary>
/// Gets or sets a string that contains the name of the primary data file. This includes the full path name of an attachable database.
/// </summary>
public string AttachDbFilename { get; set; }
/// <summary>
/// Gets or sets the name or address of the partner server to connect to if the primary server is down.
/// </summary>
public string FailoverPartner { get; set; }
/// <summary>
/// If your application is connecting to an AlwaysOn availability group (AG) on different subnets, setting MultiSubnetFailover=true provides faster detection of and connection to the (currently) active server.
/// </summary>
public bool? MultiSubnetFailover { get; set; }
/// <summary>
/// When true, an application can maintain multiple active result sets (MARS).
/// </summary>
public bool? MultipleActiveResultSets { get; set; }
/// <summary>
/// Gets or sets the size in bytes of the network packets used to communicate with an instance of SQL Server.
/// </summary>
public int? PacketSize { get; set; }
/// <summary>
/// Gets or sets a string value that indicates the type system the application expects.
/// </summary>
public string TypeSystemVersion { get; set; }
}
}

View File

@@ -20,7 +20,29 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
ServerName = details.ServerName,
DatabaseName = details.DatabaseName,
UserName = details.UserName,
Password = details.Password
Password = details.Password,
AuthenticationType = details.AuthenticationType,
Encrypt = details.Encrypt,
TrustServerCertificate = details.TrustServerCertificate,
PersistSecurityInfo = details.PersistSecurityInfo,
ConnectTimeout = details.ConnectTimeout,
ConnectRetryCount = details.ConnectRetryCount,
ConnectRetryInterval = details.ConnectRetryInterval,
ApplicationName = details.ApplicationName,
WorkstationId = details.WorkstationId,
ApplicationIntent = details.ApplicationIntent,
CurrentLanguage = details.CurrentLanguage,
Pooling = details.Pooling,
MaxPoolSize = details.MaxPoolSize,
MinPoolSize = details.MinPoolSize,
LoadBalanceTimeout = details.LoadBalanceTimeout,
Replication = details.Replication,
AttachDbFilename = details.AttachDbFilename,
FailoverPartner = details.FailoverPartner,
MultiSubnetFailover = details.MultiSubnetFailover,
MultipleActiveResultSets = details.MultipleActiveResultSets,
PacketSize = details.PacketSize,
TypeSystemVersion = details.TypeSystemVersion
};
}
}

View File

@@ -7,10 +7,10 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
using Microsoft.SqlTools.Test.Utility;
using Moq;
@@ -197,6 +197,54 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
Assert.NotEqual(String.Empty, connectionResult.Messages);
}
/// <summary>
/// Verify that optional parameters can be built into a connection string for connecting.
/// </summary>
[Theory]
[InlineDataAttribute("AuthenticationType", "Integrated")]
[InlineDataAttribute("AuthenticationType", "SqlLogin")]
[InlineDataAttribute("Encrypt", true)]
[InlineDataAttribute("Encrypt", false)]
[InlineDataAttribute("TrustServerCertificate", true)]
[InlineDataAttribute("TrustServerCertificate", false)]
[InlineDataAttribute("PersistSecurityInfo", true)]
[InlineDataAttribute("PersistSecurityInfo", false)]
[InlineDataAttribute("ConnectTimeout", 15)]
[InlineDataAttribute("ConnectRetryCount", 1)]
[InlineDataAttribute("ConnectRetryInterval", 10)]
[InlineDataAttribute("ApplicationName", "vscode-mssql")]
[InlineDataAttribute("WorkstationId", "mycomputer")]
[InlineDataAttribute("ApplicationIntent", "ReadWrite")]
[InlineDataAttribute("ApplicationIntent", "ReadOnly")]
[InlineDataAttribute("CurrentLanguage", "test")]
[InlineDataAttribute("Pooling", false)]
[InlineDataAttribute("Pooling", true)]
[InlineDataAttribute("MaxPoolSize", 100)]
[InlineDataAttribute("MinPoolSize", 0)]
[InlineDataAttribute("LoadBalanceTimeout", 0)]
[InlineDataAttribute("Replication", true)]
[InlineDataAttribute("Replication", false)]
[InlineDataAttribute("AttachDbFilename", "myfile")]
[InlineDataAttribute("FailoverPartner", "partner")]
[InlineDataAttribute("MultiSubnetFailover", true)]
[InlineDataAttribute("MultiSubnetFailover", false)]
[InlineDataAttribute("MultipleActiveResultSets", false)]
[InlineDataAttribute("MultipleActiveResultSets", true)]
[InlineDataAttribute("PacketSize", 8192)]
[InlineDataAttribute("TypeSystemVersion", "Latest")]
public void ConnectingWithOptionalParametersBuildsConnectionString(string propertyName, object propertyValue)
{
// Create a test connection details object and set the property to a specific value
ConnectionDetails details = TestObjects.GetTestConnectionDetails();
PropertyInfo info = details.GetType().GetProperty(propertyName);
info.SetValue(details, propertyValue);
// Test that a connection string can be created without exceptions
string connectionString = ConnectionService.BuildConnectionString(details);
Assert.NotNull(connectionString);
Assert.NotEmpty(connectionString);
}
/// <summary>
/// Verify that the SQL parser correctly detects errors in text
/// </summary>