mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-02 09:35:38 -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:
File diff suppressed because it is too large
Load Diff
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,10 +325,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
|
||||
.Returns((string connString) =>
|
||||
{
|
||||
dummySqlConnection.ConnectionString = connString;
|
||||
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(connString);
|
||||
|
||||
// Database name is respected. Follow heuristic where empty DB name really means Master
|
||||
var dbName = string.IsNullOrEmpty(scsb.InitialCatalog) ? masterDbName : scsb.InitialCatalog;
|
||||
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(connString);
|
||||
|
||||
// Database name is respected. Follow heuristic where empty DB name really means Master
|
||||
var dbName = string.IsNullOrEmpty(scsb.InitialCatalog) ? masterDbName : scsb.InitialCatalog;
|
||||
dummySqlConnection.SetDatabase(dbName);
|
||||
return dummySqlConnection;
|
||||
});
|
||||
@@ -1210,5 +1210,37 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => 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());
|
||||
}
|
||||
|
||||
public static ConnectParams GetTestConnectionParams()
|
||||
public static ConnectParams GetTestConnectionParams(bool useConnectionString = false)
|
||||
{
|
||||
return new ConnectParams()
|
||||
{
|
||||
OwnerUri = ScriptUri,
|
||||
Connection = GetTestConnectionDetails()
|
||||
Connection = GetTestConnectionDetails(useConnectionString)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -66,8 +66,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
ServerEdition = "Developer Edition",
|
||||
ServerLevel = ""
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test sql connection factory instance
|
||||
/// </summary>
|
||||
@@ -80,8 +80,16 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
/// <summary>
|
||||
/// Creates a test connection details object
|
||||
/// </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()
|
||||
{
|
||||
UserName = "user",
|
||||
@@ -214,9 +222,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
}
|
||||
|
||||
public override string ConnectionString { get; set; }
|
||||
public override string Database
|
||||
public override string Database
|
||||
{
|
||||
get { return _database; }
|
||||
get { return _database; }
|
||||
}
|
||||
|
||||
public override ConnectionState State { get; }
|
||||
@@ -233,13 +241,13 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
// No Op
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test helper method to set the database value
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
/// Test helper method to set the database value
|
||||
/// </summary>
|
||||
/// <param name="database"></param>
|
||||
public void SetDatabase(string database)
|
||||
{
|
||||
this._database = database;
|
||||
public void SetDatabase(string database)
|
||||
{
|
||||
this._database = database;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user