diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs index 57a7ba6e..1de51051 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs @@ -169,6 +169,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection ownerToConnectionMap[connectionParams.OwnerUri] = connectionInfo; + // 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 + connectionInfo.ConnectionDetails.DatabaseName = connectionInfo.SqlConnection.Database; + response.ConnectionSummary = new ConnectionSummary() + { + ServerName = connectionInfo.ConnectionDetails.ServerName, + DatabaseName = connectionInfo.ConnectionDetails.DatabaseName, + UserName = connectionInfo.ConnectionDetails.UserName, + }; + // invoke callback notifications foreach (var activity in this.onConnectionActivities) { diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectResponse.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectResponse.cs index c325c64f..2dd1b2fa 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectResponse.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/Contracts/ConnectResponse.cs @@ -19,5 +19,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts /// Gets or sets any connection error messages /// public string Messages { get; set; } + + /// + /// Gets or sets the actual Connection established, including Database Name + /// + public ConnectionSummary ConnectionSummary { get; set; } } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs index 72bafabf..e027e58f 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs @@ -52,6 +52,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection return connectionMock.Object; } + /// /// Verify that we can connect to the default database when no database name is /// provided as a parameter. /// @@ -75,6 +76,43 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection Assert.NotEmpty(connectionResult.ConnectionId); } + /// + /// Verify that we can connect to the default database when no database name is + /// provided as a parameter. + /// + [Theory] + [InlineDataAttribute("master")] + [InlineDataAttribute("nonMasterDb")] + public void ConnectToDefaultDatabaseRespondsWithActualDbName(string expectedDbName) + { + // Given connecting with empty database name will return the expected DB name + var connectionMock = new Mock { CallBase = true }; + connectionMock.Setup(c => c.Database).Returns(expectedDbName); + + var mockFactory = new Mock(); + mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny())) + .Returns(connectionMock.Object); + + var connectionService = new ConnectionService(mockFactory.Object); + + // When I connect with an empty DB name + var connectionDetails = TestObjects.GetTestConnectionDetails(); + connectionDetails.DatabaseName = string.Empty; + + var connectionResult = + connectionService + .Connect(new ConnectParams() + { + OwnerUri = "file:///my/test/file.sql", + Connection = connectionDetails + }); + + // Then I expect connection to succeed and the Summary to include the correct DB name + Assert.NotEmpty(connectionResult.ConnectionId); + Assert.NotNull(connectionResult.ConnectionSummary); + Assert.Equal(expectedDbName, connectionResult.ConnectionSummary.DatabaseName); + } + /// /// Verify that when a connection is started for a URI with an already existing /// connection, we disconnect first before connecting.