mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-15 02:48:30 -05:00
Add database name to attach to (if not connected to master) (#4076)
This commit is contained in:
@@ -12,10 +12,10 @@ import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview
|
||||
import { INotificationService, Severity, INotificationActions } from 'vs/platform/notification/common/notification';
|
||||
|
||||
import { SelectBox, ISelectBoxOptionsWithLabel } from 'sql/base/browser/ui/selectBox/selectBox';
|
||||
import { INotebookModel } from 'sql/parts/notebook/models/modelInterfaces';
|
||||
import { INotebookModel, IDefaultConnection } from 'sql/parts/notebook/models/modelInterfaces';
|
||||
import { CellType } from 'sql/parts/notebook/models/contracts';
|
||||
import { NotebookComponent } from 'sql/parts/notebook/notebook.component';
|
||||
import { getErrorMessage } from 'sql/parts/notebook/notebookUtils';
|
||||
import { getErrorMessage, formatServerNameWithDatabaseNameForAttachTo, getServerFromFormattedAttachToName, getDatabaseFromFormattedAttachToName } from 'sql/parts/notebook/notebookUtils';
|
||||
import { IConnectionManagementService, IConnectionDialogService } from 'sql/platform/connection/common/connectionManagement';
|
||||
import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
@@ -299,8 +299,7 @@ export class AttachToDropdown extends SelectBox {
|
||||
});
|
||||
}
|
||||
this.onDidSelect(e => {
|
||||
let connection = this.model.contexts.otherConnections.find((c) => c.serverName === e.selected);
|
||||
this.doChangeContext(new ConnectionProfile(this._capabilitiesService, connection));
|
||||
this.doChangeContext(new ConnectionProfile(this._capabilitiesService, this.getConnectionWithServerAndDatabaseNames(e.selected)));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -375,15 +374,24 @@ export class AttachToDropdown extends SelectBox {
|
||||
public getConnections(model: INotebookModel): string[] {
|
||||
let otherConnections: ConnectionProfile[] = [];
|
||||
model.contexts.otherConnections.forEach((conn) => { otherConnections.push(conn); });
|
||||
this.selectWithOptionName(model.contexts.defaultConnection.serverName);
|
||||
// If current connection connects to master, select the option in the dropdown that doesn't specify a database
|
||||
if (!model.contexts.defaultConnection.databaseName) {
|
||||
this.selectWithOptionName(model.contexts.defaultConnection.serverName);
|
||||
} else {
|
||||
if (model.contexts.defaultConnection) {
|
||||
this.selectWithOptionName(formatServerNameWithDatabaseNameForAttachTo(model.contexts.defaultConnection));
|
||||
} else {
|
||||
this.select(0);
|
||||
}
|
||||
}
|
||||
otherConnections = this.setConnectionsList(model.contexts.defaultConnection, model.contexts.otherConnections);
|
||||
let connections = otherConnections.map((context) => context.serverName);
|
||||
let connections = otherConnections.map((context) => context.databaseName ? context.serverName + ' (' + context.databaseName + ')' : context.serverName);
|
||||
return connections;
|
||||
}
|
||||
|
||||
private setConnectionsList(defaultConnection: ConnectionProfile, otherConnections: ConnectionProfile[]) {
|
||||
if (defaultConnection.serverName !== msgSelectConnection) {
|
||||
otherConnections = otherConnections.filter(conn => conn.serverName !== defaultConnection.serverName);
|
||||
otherConnections = otherConnections.filter(conn => conn.id !== defaultConnection.id);
|
||||
otherConnections.unshift(defaultConnection);
|
||||
if (otherConnections.length > 1) {
|
||||
otherConnections = otherConnections.filter(val => val.serverName !== msgSelectConnection);
|
||||
@@ -392,6 +400,20 @@ export class AttachToDropdown extends SelectBox {
|
||||
return otherConnections;
|
||||
}
|
||||
|
||||
public getConnectionWithServerAndDatabaseNames(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);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
public doChangeContext(connection?: ConnectionProfile, hideErrorMessage?: boolean): void {
|
||||
if (this.value === msgAddNewConnection) {
|
||||
this.openConnectionDialog();
|
||||
@@ -416,7 +438,7 @@ export class AttachToDropdown extends SelectBox {
|
||||
return;
|
||||
}
|
||||
let connectionProfile = new ConnectionProfile(this._capabilitiesService, connection);
|
||||
let connectedServer = connectionProfile.serverName;
|
||||
let connectedServer = formatServerNameWithDatabaseNameForAttachTo(connectionProfile);
|
||||
//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());
|
||||
|
||||
Reference in New Issue
Block a user