diff --git a/src/sql/parts/connection/common/connectionActions.ts b/src/sql/parts/connection/common/connectionActions.ts index 61a14470ec..2164e9a130 100644 --- a/src/sql/parts/connection/common/connectionActions.ts +++ b/src/sql/parts/connection/common/connectionActions.ts @@ -159,7 +159,8 @@ export class GetCurrentConnectionStringAction extends Action { if (activeInput && (activeInput instanceof QueryInput || activeInput instanceof EditDataInput || activeInput instanceof DashboardInput) && this._connectionManagementService.isConnected(activeInput.uri)) { let includePassword = false; - this._connectionManagementService.getConnectionString(activeInput.uri, includePassword).then(result => { + let connectionProfile = this._connectionManagementService.getConnectionProfile(activeInput.uri); + this._connectionManagementService.getConnectionString(connectionProfile.id, includePassword).then(result => { let message = result ? result : nls.localize('connectionAction.connectionString', "Connection string not available"); diff --git a/src/sql/parts/connection/common/connectionManagement.ts b/src/sql/parts/connection/common/connectionManagement.ts index d2234ad5b9..724d735d49 100644 --- a/src/sql/parts/connection/common/connectionManagement.ts +++ b/src/sql/parts/connection/common/connectionManagement.ts @@ -263,9 +263,9 @@ export interface IConnectionManagementService { getActiveConnectionCredentials(profileId: string): { [name: string]: string }; /** - * Get the connection string for the provided connection profile + * Get the connection string for the provided connection ID */ - getConnectionString(ownerUri: string, includePassword: boolean): Thenable; + getConnectionString(connectionId: string, includePassword: boolean): Thenable; /** * Serialize connection string with optional provider diff --git a/src/sql/parts/connection/common/connectionManagementService.ts b/src/sql/parts/connection/common/connectionManagementService.ts index 15bb2a3a35..9bbf94893f 100644 --- a/src/sql/parts/connection/common/connectionManagementService.ts +++ b/src/sql/parts/connection/common/connectionManagementService.ts @@ -649,9 +649,9 @@ export class ConnectionManagementService extends Disposable implements IConnecti } public getConnectionUriFromId(connectionId: string): string { - let connection = this.getActiveConnections().find(connection => connection.id === connectionId); - if (connection) { - return this.getConnectionUri(connection); + let connectionInfo = this._connectionStatusManager.findConnectionByProfileId(connectionId); + if (connectionInfo) { + return connectionInfo.ownerUri; } else { return undefined; } @@ -1348,9 +1348,11 @@ export class ConnectionManagementService extends Disposable implements IConnecti } /** - * Get the connection string for the provided connection profile + * Get the connection string for the provided connection ID */ - public getConnectionString(ownerUri: string, includePassword: boolean = false): Thenable { + public getConnectionString(connectionId: string, includePassword: boolean = false): Thenable { + let ownerUri = this.getConnectionUriFromId(connectionId); + if (!ownerUri) { return Promise.resolve(undefined); } diff --git a/src/sql/parts/connection/common/connectionStatusManager.ts b/src/sql/parts/connection/common/connectionStatusManager.ts index 6ef4daa2e9..6ec7b3b9b5 100644 --- a/src/sql/parts/connection/common/connectionStatusManager.ts +++ b/src/sql/parts/connection/common/connectionStatusManager.ts @@ -22,14 +22,18 @@ export class ConnectionStatusManager { this._providerCapabilitiesMap = {}; } - public findConnection(id: string): ConnectionManagementInfo { - if (id in this._connections) { - return this._connections[id]; + public findConnection(uri: string): ConnectionManagementInfo { + if (uri in this._connections) { + return this._connections[uri]; } else { return undefined; } } + public findConnectionByProfileId(profileId: string): ConnectionManagementInfo { + return Object.values(this._connections).find((connection: ConnectionManagementInfo) => connection.connectionProfile.id === profileId); + } + public findConnectionProfile(connectionProfile: IConnectionProfile): ConnectionManagementInfo { let id = Utils.generateUri(connectionProfile); return this.findConnection(id); diff --git a/src/sql/workbench/api/node/mainThreadConnectionManagement.ts b/src/sql/workbench/api/node/mainThreadConnectionManagement.ts index 1ea1f98166..832b763329 100644 --- a/src/sql/workbench/api/node/mainThreadConnectionManagement.ts +++ b/src/sql/workbench/api/node/mainThreadConnectionManagement.ts @@ -61,15 +61,13 @@ export class MainThreadConnectionManagement implements MainThreadConnectionManag } public async $listDatabases(connectionId: string): Promise { - let connection = this._connectionManagementService.getActiveConnections().find(profile => profile.id === connectionId); - let connectionUri = this._connectionManagementService.getConnectionUri(connection); + let connectionUri = await this.$getUriForConnection(connectionId); let result = await this._connectionManagementService.listDatabases(connectionUri); return result.databaseNames; } public async $getConnectionString(connectionId: string, includePassword: boolean): Promise { - let connection = this._connectionManagementService.getActiveConnections().find(profile => profile.id === connectionId); - return await this._connectionManagementService.getConnectionString(connectionId, includePassword); + return this._connectionManagementService.getConnectionString(connectionId, includePassword); } public $getUriForConnection(connectionId: string): Thenable { diff --git a/src/sqltest/parts/connection/connectionManagementService.test.ts b/src/sqltest/parts/connection/connectionManagementService.test.ts index d09f6d44db..ac40cd977f 100644 --- a/src/sqltest/parts/connection/connectionManagementService.test.ts +++ b/src/sqltest/parts/connection/connectionManagementService.test.ts @@ -797,7 +797,7 @@ suite('SQL ConnectionManagementService tests', () => { test('getActiveConnectionCredentials returns the credentials dictionary for a connection profile', () => { let profile = Object.assign({}, connectionProfile); - profile.options = {password: profile.password}; + profile.options = { password: profile.password }; profile.id = 'test_id'; connectionStatusManager.addConnection(profile, 'test_uri'); (connectionManagementService as any)._connectionStatusManager = connectionStatusManager; @@ -807,7 +807,7 @@ suite('SQL ConnectionManagementService tests', () => { test('getConnectionUriFromId returns a URI of an active connection with the given id', () => { let profile = Object.assign({}, connectionProfile); - profile.options = {password: profile.password}; + profile.options = { password: profile.password }; profile.id = 'test_id'; let uri = 'test_initial_uri'; connectionStatusManager.addConnection(profile, uri); @@ -816,13 +816,13 @@ suite('SQL ConnectionManagementService tests', () => { // If I call getConnectionUriFromId on the given connection let foundUri = connectionManagementService.getConnectionUriFromId(profile.id); - // Then the returned URI matches the connection's - assert.equal(foundUri, Utils.generateUri(new ConnectionProfile(capabilitiesService, profile))); + // Then the returned URI matches the connection's original URI + assert.equal(foundUri, uri); }); test('getConectionUriFromId returns undefined if the given connection is not active', () => { let profile = Object.assign({}, connectionProfile); - profile.options = {password: profile.password}; + profile.options = { password: profile.password }; profile.id = 'test_id'; connectionStatusManager.addConnection(profile, Utils.generateUri(profile)); (connectionManagementService as any)._connectionStatusManager = connectionStatusManager; diff --git a/src/sqltest/stubs/connectionManagementService.test.ts b/src/sqltest/stubs/connectionManagementService.test.ts index ebb864b29f..f894b2930e 100644 --- a/src/sqltest/stubs/connectionManagementService.test.ts +++ b/src/sqltest/stubs/connectionManagementService.test.ts @@ -254,7 +254,7 @@ export class TestConnectionManagementService implements IConnectionManagementSer return undefined; } - getConnectionString(ownerUri: string): Thenable { + getConnectionString(connectionId: string): Thenable { return undefined; }