diff --git a/src/sql/parts/notebook/models/notebookModel.ts b/src/sql/parts/notebook/models/notebookModel.ts index 17d7e40df5..97c92f88e7 100644 --- a/src/sql/parts/notebook/models/notebookModel.ts +++ b/src/sql/parts/notebook/models/notebookModel.ts @@ -637,34 +637,40 @@ export class NotebookModel extends Disposable implements INotebookModel { return spec; } - public async changeContext(server: string, newConnection?: IConnectionProfile, hideErrorMessage?: boolean): Promise { + public async changeContext(server: string, newConnection?: ConnectionProfile, hideErrorMessage?: boolean): Promise { try { if (!newConnection) { newConnection = this._activeContexts.otherConnections.find((connection) => connection.serverName === server); } - if (!newConnection && (this._activeContexts.defaultConnection.serverName === server)) { + if ((!newConnection) && (this._activeContexts.defaultConnection.serverName === server)) { newConnection = this._activeContexts.defaultConnection; } - let newConnectionProfile = new ConnectionProfile(this._notebookOptions.capabilitiesService, newConnection); + if (this._activeConnection) { this._otherConnections.push(this._activeConnection); } - this._activeConnection = newConnectionProfile; - this.refreshConnections(newConnectionProfile); - this._activeClientSession.updateConnection(this._activeConnection.toIConnectionProfile()).then( - result => { - //Remove 'Select connection' from 'Attach to' drop-down since its a valid connection - this._onValidConnectionSelected.fire(true); - }, - error => { - if (error) { - if (!hideErrorMessage) { - this.notifyError(notebookUtils.getErrorMessage(error)); + if (newConnection) { + this._activeConnection = newConnection; + this.refreshConnections(newConnection); + console.log(this._activeConnection); + this._activeClientSession.updateConnection(newConnection.toIConnectionProfile()).then( + result => { + //Remove 'Select connection' from 'Attach to' drop-down since its a valid connection + this._onValidConnectionSelected.fire(true); + }, + error => { + if (error) { + if (!hideErrorMessage) { + this.notifyError(notebookUtils.getErrorMessage(error)); + } + //Selected a wrong connection, Attach to should be defaulted with 'Select connection' + this._onValidConnectionSelected.fire(false); } - //Selected a wrong connection, Attach to should be defaulted with 'Select connection' - this._onValidConnectionSelected.fire(false); - } - }); + }); + } else { + this._onValidConnectionSelected.fire(false); + throw new Error('No valid connection'); + } } catch (err) { let msg = notebookUtils.getErrorMessage(err); this.notifyError(localize('changeContextFailed', "Changing context failed: {0}", msg)); @@ -796,8 +802,8 @@ export class NotebookModel extends Disposable implements INotebookModel { let kernelDisplayName = this.getDisplayNameFromSpecName(kernelChangedArgs.newValue); this._activeContexts = await NotebookContexts.getContextsForKernel(this._notebookOptions.connectionService, this.getApplicableConnectionProviderIds(kernelDisplayName), kernelChangedArgs, this.connectionProfile); this._contextsChangedEmitter.fire(); - if (this.contexts.defaultConnection !== undefined && this.contexts.defaultConnection.serverName !== undefined) { - await this.changeContext(this.contexts.defaultConnection.serverName); + if (this.contexts.defaultConnection !== undefined && this.contexts.defaultConnection.serverName !== undefined && this.contexts.defaultConnection.title !== undefined) { + await this.changeContext(this.contexts.defaultConnection.title, this.contexts.defaultConnection); } } } diff --git a/src/sql/parts/notebook/notebookActions.ts b/src/sql/parts/notebook/notebookActions.ts index e7cf7d5c5e..76c8434828 100644 --- a/src/sql/parts/notebook/notebookActions.ts +++ b/src/sql/parts/notebook/notebookActions.ts @@ -14,7 +14,7 @@ import { SelectBox, ISelectBoxOptionsWithLabel } from 'sql/base/browser/ui/selec import { INotebookModel } from 'sql/parts/notebook/models/modelInterfaces'; import { CellType, CellTypes } from 'sql/parts/notebook/models/contracts'; import { NotebookComponent } from 'sql/parts/notebook/notebook.component'; -import { getErrorMessage, formatServerNameWithDatabaseNameForAttachTo, getServerFromFormattedAttachToName, getDatabaseFromFormattedAttachToName } from 'sql/parts/notebook/notebookUtils'; +import { getErrorMessage, getServerFromFormattedAttachToName, getDatabaseFromFormattedAttachToName } from 'sql/parts/notebook/notebookUtils'; import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement'; import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService'; import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile'; @@ -295,7 +295,7 @@ export class AttachToDropdown extends SelectBox { }); } this.onDidSelect(e => { - this.doChangeContext(new ConnectionProfile(this._capabilitiesService, this.getConnectionWithServerAndDatabaseNames(e.selected))); + this.doChangeContext(this.getSelectedConnection(e.selected)); }); } @@ -381,13 +381,13 @@ export class AttachToDropdown extends SelectBox { this.selectWithOptionName(model.contexts.defaultConnection.serverName); } else { if (model.contexts.defaultConnection) { - this.selectWithOptionName(formatServerNameWithDatabaseNameForAttachTo(model.contexts.defaultConnection)); + this.selectWithOptionName(model.contexts.defaultConnection.title ? model.contexts.defaultConnection.title : model.contexts.defaultConnection.serverName); } else { this.select(0); } } otherConnections = this.setConnectionsList(model.contexts.defaultConnection, model.contexts.otherConnections); - let connections = otherConnections.map((context) => context.databaseName ? context.serverName + ' (' + context.databaseName + ')' : context.serverName); + let connections = otherConnections.map((context) => context.title ? context.title : context.serverName); return connections; } @@ -402,17 +402,14 @@ export class AttachToDropdown extends SelectBox { return otherConnections; } - public getConnectionWithServerAndDatabaseNames(selection: string): ConnectionProfile { + public getSelectedConnection(selection: string): ConnectionProfile { // Find all connections with the the same server as the selected option - let connections = this.model.contexts.otherConnections.filter((c) => selection === c.serverName); + let connections = this.model.contexts.otherConnections.filter((c) => selection === c.title); // If only one connection exists with the same server name, use that one if (connections.length === 1) { return connections[0]; } else { - // Extract server and database name - let serverName = getServerFromFormattedAttachToName(selection); - let databaseName = getDatabaseFromFormattedAttachToName(selection); - return this.model.contexts.otherConnections.find((c) => serverName === c.serverName && databaseName === c.databaseName); + return this.model.contexts.otherConnections.find((c) => selection === c.title); } } @@ -440,7 +437,7 @@ export class AttachToDropdown extends SelectBox { return; } let connectionProfile = new ConnectionProfile(this._capabilitiesService, connection); - let connectedServer = formatServerNameWithDatabaseNameForAttachTo(connectionProfile); + let connectedServer = connectionProfile.title? connectionProfile.title : connectionProfile.serverName; //Check to see if the same server is already there in dropdown. We only have server names in dropdown if (attachToConnections.some(val => val === connectedServer)) { this.loadAttachToDropdown(this.model, this.getKernelDisplayName());