Support connecting with a connection string (#334)

- Add support for connecting with a connection string by passing it as one of the connection parameters
- If a connection string is present, it will override any other parameters that are present
This commit is contained in:
Matt Irvine
2017-05-01 21:01:26 -07:00
committed by GitHub
parent 0b39408eae
commit fb239ac956
6 changed files with 1184 additions and 1099 deletions

View File

@@ -14,7 +14,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
/// or a virtual file representing an object in a database.
/// </summary>
public string OwnerUri { get; set; }
public string OwnerUri { get; set; }
/// <summary>
/// Contains the required parameters to initialize a connection to a database.
/// A connection will identified by its server name, database name and user name.

View File

@@ -24,6 +24,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
{
errorMessage = SR.ConnectionParamsValidateNullConnection;
}
else if (!string.IsNullOrEmpty(parameters.Connection.ConnectionString))
{
// Do not check other connection parameters if a connection string is present
return string.IsNullOrEmpty(errorMessage);
}
else if (string.IsNullOrEmpty(parameters.Connection.ServerName))
{
errorMessage = SR.ConnectionParamsValidateNullServerName;

View File

@@ -5,7 +5,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Globalization;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
@@ -443,6 +443,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
}
}
/// <summary>
/// Gets or sets a string value to be used as the connection string. If given, all other options will be ignored.
/// </summary>
public string ConnectionString
{
get
{
return GetOptionValue<string>("connectionString");
}
set
{
SetOptionValue("connectionString", value);
}
}
private T GetOptionValue<T>(string name)
{
T result = default(T);
@@ -485,33 +501,33 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
{
Options.Add(name, value);
}
}
public bool IsComparableTo(ConnectionDetails other)
{
if (other == null)
{
return false;
}
if (!string.Equals(ServerName, other.ServerName)
|| !string.Equals(AuthenticationType, other.AuthenticationType)
|| !string.Equals(UserName, other.UserName))
{
return false;
}
// For database name, only compare if neither is empty. This is important
// Since it allows for handling of connections to the default database, but is
// not a 100% accurate heuristic.
if (!string.IsNullOrEmpty(DatabaseName)
&& !string.IsNullOrEmpty(other.DatabaseName)
&& !string.Equals(DatabaseName, other.DatabaseName))
{
return false;
}
return true;
}
}
public bool IsComparableTo(ConnectionDetails other)
{
if (other == null)
{
return false;
}
if (ServerName != other.ServerName
|| AuthenticationType != other.AuthenticationType
|| UserName != other.UserName)
{
return false;
}
// For database name, only compare if neither is empty. This is important
// Since it allows for handling of connections to the default database, but is
// not a 100% accurate heuristic.
if (!string.IsNullOrEmpty(DatabaseName)
&& !string.IsNullOrEmpty(other.DatabaseName)
&& DatabaseName != other.DatabaseName)
{
return false;
}
return true;
}
}
}