Change attach to show user name. Use connectionProfile.title to displ… (#4794)

* Change attach to show user name. Use connectionProfile.title to display the same info as OE server node

* Fixed some potential profile leak

* Removed unused import

* Resolve PR comments
This commit is contained in:
Yurong He
2019-04-01 21:18:49 -07:00
committed by GitHub
parent a766e5d334
commit fdbfbb9238
2 changed files with 34 additions and 31 deletions

View File

@@ -637,34 +637,40 @@ export class NotebookModel extends Disposable implements INotebookModel {
return spec; return spec;
} }
public async changeContext(server: string, newConnection?: IConnectionProfile, hideErrorMessage?: boolean): Promise<void> { public async changeContext(server: string, newConnection?: ConnectionProfile, hideErrorMessage?: boolean): Promise<void> {
try { try {
if (!newConnection) { if (!newConnection) {
newConnection = this._activeContexts.otherConnections.find((connection) => connection.serverName === server); 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; newConnection = this._activeContexts.defaultConnection;
} }
let newConnectionProfile = new ConnectionProfile(this._notebookOptions.capabilitiesService, newConnection);
if (this._activeConnection) { if (this._activeConnection) {
this._otherConnections.push(this._activeConnection); this._otherConnections.push(this._activeConnection);
} }
this._activeConnection = newConnectionProfile; if (newConnection) {
this.refreshConnections(newConnectionProfile); this._activeConnection = newConnection;
this._activeClientSession.updateConnection(this._activeConnection.toIConnectionProfile()).then( this.refreshConnections(newConnection);
result => { console.log(this._activeConnection);
//Remove 'Select connection' from 'Attach to' drop-down since its a valid connection this._activeClientSession.updateConnection(newConnection.toIConnectionProfile()).then(
this._onValidConnectionSelected.fire(true); result => {
}, //Remove 'Select connection' from 'Attach to' drop-down since its a valid connection
error => { this._onValidConnectionSelected.fire(true);
if (error) { },
if (!hideErrorMessage) { error => {
this.notifyError(notebookUtils.getErrorMessage(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) { } catch (err) {
let msg = notebookUtils.getErrorMessage(err); let msg = notebookUtils.getErrorMessage(err);
this.notifyError(localize('changeContextFailed', "Changing context failed: {0}", msg)); 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); let kernelDisplayName = this.getDisplayNameFromSpecName(kernelChangedArgs.newValue);
this._activeContexts = await NotebookContexts.getContextsForKernel(this._notebookOptions.connectionService, this.getApplicableConnectionProviderIds(kernelDisplayName), kernelChangedArgs, this.connectionProfile); this._activeContexts = await NotebookContexts.getContextsForKernel(this._notebookOptions.connectionService, this.getApplicableConnectionProviderIds(kernelDisplayName), kernelChangedArgs, this.connectionProfile);
this._contextsChangedEmitter.fire(); this._contextsChangedEmitter.fire();
if (this.contexts.defaultConnection !== undefined && this.contexts.defaultConnection.serverName !== undefined) { if (this.contexts.defaultConnection !== undefined && this.contexts.defaultConnection.serverName !== undefined && this.contexts.defaultConnection.title !== undefined) {
await this.changeContext(this.contexts.defaultConnection.serverName); await this.changeContext(this.contexts.defaultConnection.title, this.contexts.defaultConnection);
} }
} }
} }

View File

@@ -14,7 +14,7 @@ import { SelectBox, ISelectBoxOptionsWithLabel } from 'sql/base/browser/ui/selec
import { INotebookModel } from 'sql/parts/notebook/models/modelInterfaces'; import { INotebookModel } from 'sql/parts/notebook/models/modelInterfaces';
import { CellType, CellTypes } from 'sql/parts/notebook/models/contracts'; import { CellType, CellTypes } from 'sql/parts/notebook/models/contracts';
import { NotebookComponent } from 'sql/parts/notebook/notebook.component'; 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 { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService'; import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService';
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile'; import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
@@ -295,7 +295,7 @@ export class AttachToDropdown extends SelectBox {
}); });
} }
this.onDidSelect(e => { 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); this.selectWithOptionName(model.contexts.defaultConnection.serverName);
} else { } else {
if (model.contexts.defaultConnection) { 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 { } else {
this.select(0); this.select(0);
} }
} }
otherConnections = this.setConnectionsList(model.contexts.defaultConnection, model.contexts.otherConnections); 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; return connections;
} }
@@ -402,17 +402,14 @@ export class AttachToDropdown extends SelectBox {
return otherConnections; return otherConnections;
} }
public getConnectionWithServerAndDatabaseNames(selection: string): ConnectionProfile { public getSelectedConnection(selection: string): ConnectionProfile {
// Find all connections with the the same server as the selected option // 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 only one connection exists with the same server name, use that one
if (connections.length === 1) { if (connections.length === 1) {
return connections[0]; return connections[0];
} else { } else {
// Extract server and database name return this.model.contexts.otherConnections.find((c) => selection === c.title);
let serverName = getServerFromFormattedAttachToName(selection);
let databaseName = getDatabaseFromFormattedAttachToName(selection);
return this.model.contexts.otherConnections.find((c) => serverName === c.serverName && databaseName === c.databaseName);
} }
} }
@@ -440,7 +437,7 @@ export class AttachToDropdown extends SelectBox {
return; return;
} }
let connectionProfile = new ConnectionProfile(this._capabilitiesService, connection); 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 //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)) { if (attachToConnections.some(val => val === connectedServer)) {
this.loadAttachToDropdown(this.model, this.getKernelDisplayName()); this.loadAttachToDropdown(this.model, this.getKernelDisplayName());