mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Allow connections on non-default port (#462)
* Allow custom port * Update unit tests for port property
This commit is contained in:
@@ -83,19 +83,26 @@ namespace Microsoft.SqlTools.ServiceLayer.Admin
|
|||||||
DefaultDatabaseInfoParams optionsParams,
|
DefaultDatabaseInfoParams optionsParams,
|
||||||
RequestContext<DefaultDatabaseInfoResponse> requestContext)
|
RequestContext<DefaultDatabaseInfoResponse> requestContext)
|
||||||
{
|
{
|
||||||
var response = new DefaultDatabaseInfoResponse();
|
try
|
||||||
ConnectionInfo connInfo;
|
|
||||||
AdminService.ConnectionServiceInstance.TryFindConnection(
|
|
||||||
optionsParams.OwnerUri,
|
|
||||||
out connInfo);
|
|
||||||
|
|
||||||
if (taskHelper == null)
|
|
||||||
{
|
{
|
||||||
taskHelper = CreateDatabaseTaskHelper(connInfo);
|
var response = new DefaultDatabaseInfoResponse();
|
||||||
}
|
ConnectionInfo connInfo;
|
||||||
|
AdminService.ConnectionServiceInstance.TryFindConnection(
|
||||||
|
optionsParams.OwnerUri,
|
||||||
|
out connInfo);
|
||||||
|
|
||||||
response.DefaultDatabaseInfo = DatabaseTaskHelper.DatabasePrototypeToDatabaseInfo(taskHelper.Prototype);
|
if (taskHelper == null)
|
||||||
await requestContext.SendResult(response);
|
{
|
||||||
|
taskHelper = CreateDatabaseTaskHelper(connInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
response.DefaultDatabaseInfo = DatabaseTaskHelper.DatabasePrototypeToDatabaseInfo(taskHelper.Prototype);
|
||||||
|
await requestContext.SendResult(response);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await requestContext.SendError(ex.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -105,31 +112,38 @@ namespace Microsoft.SqlTools.ServiceLayer.Admin
|
|||||||
CreateDatabaseParams databaseParams,
|
CreateDatabaseParams databaseParams,
|
||||||
RequestContext<CreateDatabaseResponse> requestContext)
|
RequestContext<CreateDatabaseResponse> requestContext)
|
||||||
{
|
{
|
||||||
var response = new DefaultDatabaseInfoResponse();
|
try
|
||||||
ConnectionInfo connInfo;
|
|
||||||
AdminService.ConnectionServiceInstance.TryFindConnection(
|
|
||||||
databaseParams.OwnerUri,
|
|
||||||
out connInfo);
|
|
||||||
|
|
||||||
if (taskHelper == null)
|
|
||||||
{
|
{
|
||||||
taskHelper = CreateDatabaseTaskHelper(connInfo);
|
var response = new DefaultDatabaseInfoResponse();
|
||||||
|
ConnectionInfo connInfo;
|
||||||
|
AdminService.ConnectionServiceInstance.TryFindConnection(
|
||||||
|
databaseParams.OwnerUri,
|
||||||
|
out connInfo);
|
||||||
|
|
||||||
|
if (taskHelper == null)
|
||||||
|
{
|
||||||
|
taskHelper = CreateDatabaseTaskHelper(connInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabasePrototype prototype = taskHelper.Prototype;
|
||||||
|
DatabaseTaskHelper.ApplyToPrototype(databaseParams.DatabaseInfo, taskHelper.Prototype);
|
||||||
|
|
||||||
|
Database db = prototype.ApplyChanges();
|
||||||
|
if (db != null)
|
||||||
|
{
|
||||||
|
taskHelper = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
await requestContext.SendResult(new CreateDatabaseResponse()
|
||||||
|
{
|
||||||
|
Result = true,
|
||||||
|
TaskId = 0
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
DatabasePrototype prototype = taskHelper.Prototype;
|
|
||||||
DatabaseTaskHelper.ApplyToPrototype(databaseParams.DatabaseInfo, taskHelper.Prototype);
|
|
||||||
|
|
||||||
Database db = prototype.ApplyChanges();
|
|
||||||
if (db != null)
|
|
||||||
{
|
{
|
||||||
taskHelper = null;
|
await requestContext.SendError(ex.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
await requestContext.SendResult(new CreateDatabaseResponse()
|
|
||||||
{
|
|
||||||
Result = true,
|
|
||||||
TaskId = 0
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -139,20 +153,27 @@ namespace Microsoft.SqlTools.ServiceLayer.Admin
|
|||||||
GetDatabaseInfoParams databaseParams,
|
GetDatabaseInfoParams databaseParams,
|
||||||
RequestContext<GetDatabaseInfoResponse> requestContext)
|
RequestContext<GetDatabaseInfoResponse> requestContext)
|
||||||
{
|
{
|
||||||
ConnectionInfo connInfo;
|
try
|
||||||
AdminService.ConnectionServiceInstance.TryFindConnection(
|
|
||||||
databaseParams.OwnerUri,
|
|
||||||
out connInfo);
|
|
||||||
DatabaseInfo info = null;
|
|
||||||
|
|
||||||
if (connInfo != null)
|
|
||||||
{
|
{
|
||||||
info = GetDatabaseInfo(connInfo);
|
ConnectionInfo connInfo;
|
||||||
}
|
AdminService.ConnectionServiceInstance.TryFindConnection(
|
||||||
|
databaseParams.OwnerUri,
|
||||||
|
out connInfo);
|
||||||
|
DatabaseInfo info = null;
|
||||||
|
|
||||||
await requestContext.SendResult(new GetDatabaseInfoResponse(){
|
if (connInfo != null)
|
||||||
DatabaseInfo = info
|
{
|
||||||
});
|
info = GetDatabaseInfo(connInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
await requestContext.SendResult(new GetDatabaseInfoResponse(){
|
||||||
|
DatabaseInfo = info
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await requestContext.SendError(ex.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -177,15 +198,21 @@ namespace Microsoft.SqlTools.ServiceLayer.Admin
|
|||||||
XmlDocument xmlDoc = CreateDataContainerDocument(connInfo, databaseExists);
|
XmlDocument xmlDoc = CreateDataContainerDocument(connInfo, databaseExists);
|
||||||
CDataContainer dataContainer;
|
CDataContainer dataContainer;
|
||||||
|
|
||||||
|
// add alternate port to server name property if provided
|
||||||
|
var connectionDetails = connInfo.ConnectionDetails;
|
||||||
|
string serverName = !connectionDetails.Port.HasValue
|
||||||
|
? connectionDetails.ServerName
|
||||||
|
: string.Format("{0},{1}", connectionDetails.ServerName, connectionDetails.Port.Value);
|
||||||
|
|
||||||
// check if the connection is using SQL Auth or Integrated Auth
|
// check if the connection is using SQL Auth or Integrated Auth
|
||||||
if (string.Equals(connInfo.ConnectionDetails.AuthenticationType, "SqlLogin", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(connectionDetails.AuthenticationType, "SqlLogin", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
var passwordSecureString = BuildSecureStringFromPassword(connInfo.ConnectionDetails.Password);
|
var passwordSecureString = BuildSecureStringFromPassword(connectionDetails.Password);
|
||||||
dataContainer = new CDataContainer(
|
dataContainer = new CDataContainer(
|
||||||
CDataContainer.ServerType.SQL,
|
CDataContainer.ServerType.SQL,
|
||||||
connInfo.ConnectionDetails.ServerName,
|
serverName,
|
||||||
false,
|
false,
|
||||||
connInfo.ConnectionDetails.UserName,
|
connectionDetails.UserName,
|
||||||
passwordSecureString,
|
passwordSecureString,
|
||||||
xmlDoc.InnerXml);
|
xmlDoc.InnerXml);
|
||||||
}
|
}
|
||||||
@@ -193,7 +220,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Admin
|
|||||||
{
|
{
|
||||||
dataContainer = new CDataContainer(
|
dataContainer = new CDataContainer(
|
||||||
CDataContainer.ServerType.SQL,
|
CDataContainer.ServerType.SQL,
|
||||||
connInfo.ConnectionDetails.ServerName,
|
serverName,
|
||||||
true,
|
true,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
|||||||
@@ -886,10 +886,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
{
|
{
|
||||||
connectionBuilder = new SqlConnectionStringBuilder(connectionDetails.ConnectionString);
|
connectionBuilder = new SqlConnectionStringBuilder(connectionDetails.ConnectionString);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
|
// add alternate port to data source property if provided
|
||||||
|
string dataSource = !connectionDetails.Port.HasValue
|
||||||
|
? connectionDetails.ServerName
|
||||||
|
: string.Format("{0},{1}", connectionDetails.ServerName, connectionDetails.Port.Value);
|
||||||
|
|
||||||
connectionBuilder = new SqlConnectionStringBuilder
|
connectionBuilder = new SqlConnectionStringBuilder
|
||||||
{
|
{
|
||||||
["Data Source"] = connectionDetails.ServerName,
|
["Data Source"] = dataSource,
|
||||||
["User Id"] = connectionDetails.UserName,
|
["User Id"] = connectionDetails.UserName,
|
||||||
["Password"] = connectionDetails.Password
|
["Password"] = connectionDetails.Password
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -418,6 +418,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the port to use for the TCP/IP connection
|
||||||
|
/// </summary>
|
||||||
|
public int? Port
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return GetOptionValue<int?>("port");
|
||||||
|
}
|
||||||
|
|
||||||
|
set
|
||||||
|
{
|
||||||
|
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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -43,7 +43,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
MultipleActiveResultSets = details.MultipleActiveResultSets,
|
MultipleActiveResultSets = details.MultipleActiveResultSets,
|
||||||
PacketSize = details.PacketSize,
|
PacketSize = details.PacketSize,
|
||||||
TypeSystemVersion = details.TypeSystemVersion,
|
TypeSystemVersion = details.TypeSystemVersion,
|
||||||
ConnectionString = details.ConnectionString
|
ConnectionString = details.ConnectionString,
|
||||||
|
Port = details.Port
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
|
|||||||
Assert.Equal(details.Pooling, expectedForBoolean);
|
Assert.Equal(details.Pooling, expectedForBoolean);
|
||||||
Assert.Equal(details.Replication, expectedForBoolean);
|
Assert.Equal(details.Replication, expectedForBoolean);
|
||||||
Assert.Equal(details.TrustServerCertificate, expectedForBoolean);
|
Assert.Equal(details.TrustServerCertificate, expectedForBoolean);
|
||||||
|
Assert.Equal(details.Port, expectedForInt);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -87,6 +88,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
|
|||||||
details.Pooling = (index++ % 2 == 0);
|
details.Pooling = (index++ % 2 == 0);
|
||||||
details.Replication = (index++ % 2 == 0);
|
details.Replication = (index++ % 2 == 0);
|
||||||
details.TrustServerCertificate = (index++ % 2 == 0);
|
details.TrustServerCertificate = (index++ % 2 == 0);
|
||||||
|
details.Port = expectedForInt + index++;
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
Assert.Equal(details.ApplicationIntent, expectedForStrings + index++);
|
Assert.Equal(details.ApplicationIntent, expectedForStrings + index++);
|
||||||
@@ -115,6 +117,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
|
|||||||
Assert.Equal(details.Pooling, (index++ % 2 == 0));
|
Assert.Equal(details.Pooling, (index++ % 2 == 0));
|
||||||
Assert.Equal(details.Replication, (index++ % 2 == 0));
|
Assert.Equal(details.Replication, (index++ % 2 == 0));
|
||||||
Assert.Equal(details.TrustServerCertificate, (index++ % 2 == 0));
|
Assert.Equal(details.TrustServerCertificate, (index++ % 2 == 0));
|
||||||
|
Assert.Equal(details.Port, (expectedForInt + index++));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -152,6 +155,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Connection
|
|||||||
details.Pooling = (index++ % 2 == 0);
|
details.Pooling = (index++ % 2 == 0);
|
||||||
details.Replication = (index++ % 2 == 0);
|
details.Replication = (index++ % 2 == 0);
|
||||||
details.TrustServerCertificate = (index++ % 2 == 0);
|
details.TrustServerCertificate = (index++ % 2 == 0);
|
||||||
|
details.Port = expectedForInt + index++;
|
||||||
|
|
||||||
if(optionMetadata.Options.Count() != details.Options.Count)
|
if(optionMetadata.Options.Count() != details.Options.Count)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user