mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-17 02:51:45 -05:00
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:
@@ -277,12 +277,26 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
// Update with the actual database name in connectionInfo and result
|
// Update with the actual database name in connectionInfo and result
|
||||||
// Doing this here as we know the connection is open - expect to do this only on connecting
|
// Doing this here as we know the connection is open - expect to do this only on connecting
|
||||||
connectionInfo.ConnectionDetails.DatabaseName = connection.Database;
|
connectionInfo.ConnectionDetails.DatabaseName = connection.Database;
|
||||||
|
if (!string.IsNullOrEmpty(connectionInfo.ConnectionDetails.ConnectionString))
|
||||||
|
{
|
||||||
|
// If the connection was set up with a connection string, use the connection string to get the details
|
||||||
|
var connectionString = new SqlConnectionStringBuilder(connection.ConnectionString);
|
||||||
|
response.ConnectionSummary = new ConnectionSummary
|
||||||
|
{
|
||||||
|
ServerName = connectionString.DataSource,
|
||||||
|
DatabaseName = connectionString.InitialCatalog,
|
||||||
|
UserName = connectionString.UserID
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
response.ConnectionSummary = new ConnectionSummary
|
response.ConnectionSummary = new ConnectionSummary
|
||||||
{
|
{
|
||||||
ServerName = connectionInfo.ConnectionDetails.ServerName,
|
ServerName = connectionInfo.ConnectionDetails.ServerName,
|
||||||
DatabaseName = connectionInfo.ConnectionDetails.DatabaseName,
|
DatabaseName = connectionInfo.ConnectionDetails.DatabaseName,
|
||||||
UserName = connectionInfo.ConnectionDetails.UserName,
|
UserName = connectionInfo.ConnectionDetails.UserName
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
response.ConnectionId = connectionInfo.ConnectionId.ToString();
|
response.ConnectionId = connectionInfo.ConnectionId.ToString();
|
||||||
|
|
||||||
@@ -821,7 +835,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
/// <param name="connectionDetails"></param>
|
/// <param name="connectionDetails"></param>
|
||||||
public static string BuildConnectionString(ConnectionDetails connectionDetails)
|
public static string BuildConnectionString(ConnectionDetails connectionDetails)
|
||||||
{
|
{
|
||||||
SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder
|
SqlConnectionStringBuilder connectionBuilder;
|
||||||
|
|
||||||
|
// If connectionDetails has a connection string already, just validate and return it
|
||||||
|
if (!string.IsNullOrEmpty(connectionDetails.ConnectionString))
|
||||||
|
{
|
||||||
|
connectionBuilder = new SqlConnectionStringBuilder(connectionDetails.ConnectionString);
|
||||||
|
return connectionBuilder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
connectionBuilder = new SqlConnectionStringBuilder
|
||||||
{
|
{
|
||||||
["Data Source"] = connectionDetails.ServerName,
|
["Data Source"] = connectionDetails.ServerName,
|
||||||
["User Id"] = connectionDetails.UserName,
|
["User Id"] = connectionDetails.UserName,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
/// or a virtual file representing an object in a database.
|
/// or a virtual file representing an object in a database.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string OwnerUri { get; set; }
|
public string OwnerUri { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains the required parameters to initialize a connection to a database.
|
/// Contains the required parameters to initialize a connection to a database.
|
||||||
/// A connection will identified by its server name, database name and user name.
|
/// A connection will identified by its server name, database name and user name.
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
{
|
{
|
||||||
errorMessage = SR.ConnectionParamsValidateNullConnection;
|
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))
|
else if (string.IsNullOrEmpty(parameters.Connection.ServerName))
|
||||||
{
|
{
|
||||||
errorMessage = SR.ConnectionParamsValidateNullServerName;
|
errorMessage = SR.ConnectionParamsValidateNullServerName;
|
||||||
|
|||||||
@@ -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)
|
private T GetOptionValue<T>(string name)
|
||||||
{
|
{
|
||||||
T result = default(T);
|
T result = default(T);
|
||||||
@@ -494,9 +510,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.Equals(ServerName, other.ServerName)
|
if (ServerName != other.ServerName
|
||||||
|| !string.Equals(AuthenticationType, other.AuthenticationType)
|
|| AuthenticationType != other.AuthenticationType
|
||||||
|| !string.Equals(UserName, other.UserName))
|
|| UserName != other.UserName)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -506,7 +522,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
// not a 100% accurate heuristic.
|
// not a 100% accurate heuristic.
|
||||||
if (!string.IsNullOrEmpty(DatabaseName)
|
if (!string.IsNullOrEmpty(DatabaseName)
|
||||||
&& !string.IsNullOrEmpty(other.DatabaseName)
|
&& !string.IsNullOrEmpty(other.DatabaseName)
|
||||||
&& !string.Equals(DatabaseName, other.DatabaseName))
|
&& DatabaseName != other.DatabaseName)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1210,5 +1210,37 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
|
|||||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||||
() => service.GetOrOpenConnection(TestObjects.ScriptUri, ConnectionType.Query));
|
() => service.GetOrOpenConnection(TestObjects.ScriptUri, ConnectionType.Query));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ConnectionWithConnectionStringSucceeds()
|
||||||
|
{
|
||||||
|
var connectionParameters = TestObjects.GetTestConnectionParams(true);
|
||||||
|
var connectionResult = await TestObjects.GetTestConnectionService().Connect(connectionParameters);
|
||||||
|
|
||||||
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ConnectionWithBadConnectionStringFails()
|
||||||
|
{
|
||||||
|
var connectionParameters = TestObjects.GetTestConnectionParams(true);
|
||||||
|
connectionParameters.Connection.ConnectionString = "thisisnotavalidconnectionstring";
|
||||||
|
var connectionResult = await TestObjects.GetTestConnectionService().Connect(connectionParameters);
|
||||||
|
|
||||||
|
Assert.NotEmpty(connectionResult.ErrorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ConnectionWithConnectionStringOverridesParameters()
|
||||||
|
{
|
||||||
|
var connectionParameters = TestObjects.GetTestConnectionParams();
|
||||||
|
connectionParameters.Connection.ServerName = "overriddenServerName";
|
||||||
|
var connectionString = TestObjects.GetTestConnectionParams(true).Connection.ConnectionString;
|
||||||
|
connectionParameters.Connection.ConnectionString = connectionString;
|
||||||
|
|
||||||
|
// Connect and verify that the server name has been overridden
|
||||||
|
var connectionResult = await TestObjects.GetTestConnectionService().Connect(connectionParameters);
|
||||||
|
Assert.NotEqual(connectionParameters.Connection.ServerName, connectionResult.ConnectionSummary.ServerName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,12 +44,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
|||||||
GetTestConnectionDetails());
|
GetTestConnectionDetails());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ConnectParams GetTestConnectionParams()
|
public static ConnectParams GetTestConnectionParams(bool useConnectionString = false)
|
||||||
{
|
{
|
||||||
return new ConnectParams()
|
return new ConnectParams()
|
||||||
{
|
{
|
||||||
OwnerUri = ScriptUri,
|
OwnerUri = ScriptUri,
|
||||||
Connection = GetTestConnectionDetails()
|
Connection = GetTestConnectionDetails(useConnectionString)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,8 +80,16 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a test connection details object
|
/// Creates a test connection details object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static ConnectionDetails GetTestConnectionDetails()
|
public static ConnectionDetails GetTestConnectionDetails(bool useConnectionString = false)
|
||||||
{
|
{
|
||||||
|
if (useConnectionString)
|
||||||
|
{
|
||||||
|
return new ConnectionDetails()
|
||||||
|
{
|
||||||
|
ConnectionString = "User ID=user;PWD=password;Database=databaseName;Server=serverName"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return new ConnectionDetails()
|
return new ConnectionDetails()
|
||||||
{
|
{
|
||||||
UserName = "user",
|
UserName = "user",
|
||||||
|
|||||||
Reference in New Issue
Block a user