Add global current connection (#505)

This commit is contained in:
Matt Irvine
2018-01-24 10:11:34 -08:00
committed by GitHub
parent e432884e25
commit a63800deb1
12 changed files with 326 additions and 15 deletions

View File

@@ -10,6 +10,8 @@ import * as TaskUtilities from 'sql/workbench/common/taskUtilities';
import { RunQueryOnConnectionMode, IConnectionManagementService } from 'sql/parts/connection/common/connectionManagement';
import { IQueryEditorService } from 'sql/parts/query/common/queryEditorService';
import { InsightActionContext } from 'sql/workbench/common/actions';
import { IObjectExplorerService } from 'sql/parts/registeredServer/common/objectExplorerService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
export class RunInsightQueryAction extends Action {
public static ID = 'runQuery';
@@ -18,7 +20,9 @@ export class RunInsightQueryAction extends Action {
constructor(
id: string, label: string,
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
@IObjectExplorerService protected _objectExplorerService: IObjectExplorerService,
@IWorkbenchEditorService protected _workbenchEditorService: IWorkbenchEditorService
) {
super(id, label);
}
@@ -29,6 +33,8 @@ export class RunInsightQueryAction extends Action {
context.profile,
this._connectionManagementService,
this._queryEditorService,
this._objectExplorerService,
this._workbenchEditorService,
context.insight.query as string,
RunQueryOnConnectionMode.executeQuery
).then(() => resolve(true), () => resolve(false));

View File

@@ -18,6 +18,7 @@ import * as TelemetryKeys from 'sql/common/telemetryKeys';
import * as TelemetryUtils from 'sql/common/telemetryUtilities';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { warn, error } from 'sql/base/common/log';
import { ServerTreeView } from 'sql/parts/registeredServer/viewlet/serverTreeView';
export const SERVICE_ID = 'ObjectExplorerService';
@@ -54,6 +55,12 @@ export interface IObjectExplorerService {
deleteObjectExplorerNode(connection: IConnectionProfile): void;
onUpdateObjectExplorerNodes: Event<ObjectExplorerNodeEventArgs>;
registerServerTreeView(view: ServerTreeView): void;
getSelectedProfileAndDatabase(): { profile: ConnectionProfile, databaseName: string };
isFocused(): boolean;
}
interface SessionStatus {
@@ -85,6 +92,8 @@ export class ObjectExplorerService implements IObjectExplorerService {
private _onUpdateObjectExplorerNodes: Emitter<ObjectExplorerNodeEventArgs>;
private _serverTreeView: ServerTreeView;
constructor(
@IConnectionManagementService private _connectionManagementService: IConnectionManagementService,
@ITelemetryService private _telemetryService: ITelemetryService
@@ -355,4 +364,44 @@ export class ObjectExplorerService implements IObjectExplorerService {
return new TreeNode(nodeInfo.nodeType, nodeInfo.label, isLeaf, nodeInfo.nodePath,
nodeInfo.nodeSubType, nodeInfo.nodeStatus, parent, nodeInfo.metadata);
}
public registerServerTreeView(view: ServerTreeView): void {
if (this._serverTreeView) {
throw new Error('The object explorer server tree view is already registered');
}
this._serverTreeView = view;
}
/**
* Returns the connection profile corresponding to the current Object Explorer selection,
* or undefined if there are multiple selections or no such connection
*/
public getSelectedProfileAndDatabase(): { profile: ConnectionProfile, databaseName: string } {
if (!this._serverTreeView) {
return undefined;
}
let selection = this._serverTreeView.getSelection();
if (selection.length === 1) {
let selectedNode = selection[0];
if (selectedNode instanceof ConnectionProfile) {
return { profile: selectedNode, databaseName: undefined };
} else if (selectedNode instanceof TreeNode) {
let profile = selectedNode.getConnectionProfile();
let database = selectedNode.getDatabaseName();
// If the database is unavailable, use the server connection
if (selectedNode.nodeTypeId === 'Database' && selectedNode.isAlwaysLeaf) {
database = undefined;
}
return { profile: profile, databaseName: database };
}
}
return undefined;
}
/**
* Returns a boolean indicating whether the Object Explorer tree has focus
*/
public isFocused(): boolean {
return this._serverTreeView.isFocused();
}
}

View File

@@ -21,6 +21,7 @@ import { IObjectExplorerService } from 'sql/parts/registeredServer/common/object
import { TreeNode } from 'sql/parts/registeredServer/common/treeNode';
import Severity from 'vs/base/common/severity';
import { ObjectExplorerActionsContext, ObjectExplorerActionUtilities } from 'sql/parts/registeredServer/viewlet/objectExplorerActions';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
export class RefreshAction extends Action {
@@ -373,7 +374,9 @@ export class NewQueryAction extends Action {
id: string,
label: string,
@IQueryEditorService private queryEditorService: IQueryEditorService,
@IConnectionManagementService private connectionManagementService: IConnectionManagementService
@IConnectionManagementService private connectionManagementService: IConnectionManagementService,
@IObjectExplorerService protected _objectExplorerService: IObjectExplorerService,
@IWorkbenchEditorService protected _workbenchEditorService: IWorkbenchEditorService
) {
super(id, label);
this.class = 'extension-action update';
@@ -384,7 +387,7 @@ export class NewQueryAction extends Action {
this._connectionProfile = actionContext.connectionProfile;
}
TaskUtilities.newQuery(this._connectionProfile, this.connectionManagementService, this.queryEditorService);
TaskUtilities.newQuery(this._connectionProfile, this.connectionManagementService, this.queryEditorService, this._objectExplorerService, this._workbenchEditorService);
return TPromise.as(true);
}
}

View File

@@ -26,6 +26,7 @@ import { InputBox } from 'vs/base/browser/ui/inputbox/inputBox';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { ClearSearchAction, AddServerAction, AddServerGroupAction, ActiveConnectionsFilterAction } from 'sql/parts/registeredServer/viewlet/connectionTreeAction';
import { warn } from 'sql/base/common/log';
import { IObjectExplorerService } from 'sql/parts/registeredServer/common/objectExplorerService';
export class ConnectionViewlet extends Viewlet implements IConnectionsViewlet {
@@ -48,7 +49,8 @@ export class ConnectionViewlet extends Viewlet implements IConnectionsViewlet {
@IConnectionManagementService private connectionManagementService: IConnectionManagementService,
@IInstantiationService private _instantiationService: IInstantiationService,
@IViewletService private viewletService: IViewletService,
@IMessageService private messageService: IMessageService
@IMessageService private messageService: IMessageService,
@IObjectExplorerService private objectExplorerService: IObjectExplorerService
) {
super(VIEWLET_ID, telemetryService, _themeService);
this._searchDelayer = new ThrottledDelayer(500);
@@ -62,6 +64,7 @@ export class ConnectionViewlet extends Viewlet implements IConnectionsViewlet {
AddServerGroupAction.LABEL);
this._serverTreeView = this._instantiationService.createInstance(ServerTreeView);
this._activeConnectionsFilterAction = this._serverTreeView.activeConnectionsFilterAction;
this.objectExplorerService.registerServerTreeView(this._serverTreeView);
}
private onError(err: any): void {

View File

@@ -21,6 +21,7 @@ import { IScriptingService } from 'sql/services/scripting/scriptingService';
import { IQueryEditorService } from 'sql/parts/query/common/queryEditorService';
import { IObjectExplorerService } from 'sql/parts/registeredServer/common/objectExplorerService';
import * as Constants from 'sql/parts/connection/common/constants';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
export class ObjectExplorerActionsContext {
public treeNode: TreeNode;
@@ -39,9 +40,11 @@ export class OENewQueryAction extends NewQueryAction {
id: string, label: string, icon: string,
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
@IInstantiationService private _instantiationService: IInstantiationService
@IInstantiationService private _instantiationService: IInstantiationService,
@IObjectExplorerService protected _objectExplorerService: IObjectExplorerService,
@IWorkbenchEditorService protected _workbenchEditorService: IWorkbenchEditorService
) {
super(id, label, icon, _queryEditorService, _connectionManagementService);
super(id, label, icon, _queryEditorService, _connectionManagementService, _objectExplorerService, _workbenchEditorService);
}
public run(actionContext: any): TPromise<boolean> {

View File

@@ -406,6 +406,20 @@ export class ServerTreeView {
}
}
/**
* Get the list of selected nodes in the tree
*/
public getSelection(): any[] {
return this._tree.getSelection();
}
/**
* Get whether the tree view currently has focus
*/
public isFocused(): boolean {
return this._tree.isDOMFocused();
}
/**
* dispose the server tree view
*/

View File

@@ -24,6 +24,8 @@ import { Action } from 'vs/base/common/actions';
import { IWindowsService } from 'vs/platform/windows/common/windows';
import * as nls from 'vs/nls';
import { IObjectExplorerService } from 'sql/parts/registeredServer/common/objectExplorerService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
export interface BaseActionContext {
object?: ObjectMetadata;
@@ -47,7 +49,9 @@ export class NewQueryAction extends TaskAction {
constructor(
id: string, label: string, icon: string,
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
@IObjectExplorerService protected _objectExplorerService: IObjectExplorerService,
@IWorkbenchEditorService protected _workbenchEditorService: IWorkbenchEditorService
) {
super(id, label, icon);
}
@@ -57,7 +61,9 @@ export class NewQueryAction extends TaskAction {
TaskUtilities.newQuery(
actionContext.profile,
this._connectionManagementService,
this._queryEditorService
this._queryEditorService,
this._objectExplorerService,
this._workbenchEditorService
).then(
result => {
resolve(true);

View File

@@ -24,6 +24,10 @@ import data = require('data');
import nls = require('vs/nls');
import os = require('os');
import path = require('path');
import { IObjectExplorerService } from 'sql/parts/registeredServer/common/objectExplorerService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { QueryInput } from 'sql/parts/query/common/queryInput';
import { DashboardInput } from 'sql/parts/dashboard/dashboardInput';
// map for the version of SQL Server (default is 140)
const scriptCompatibilityOptionMap = {
@@ -237,10 +241,15 @@ export function newQuery(
connectionProfile: IConnectionProfile,
connectionService: IConnectionManagementService,
queryEditorService: IQueryEditorService,
objectExplorerService: IObjectExplorerService,
workbenchEditorService: IWorkbenchEditorService,
sqlContent?: string,
executeOnOpen: RunQueryOnConnectionMode = RunQueryOnConnectionMode.none
): Promise<void> {
return new Promise<void>((resolve) => {
if (!connectionProfile) {
connectionProfile = getCurrentGlobalConnection(objectExplorerService, connectionService, workbenchEditorService);
}
queryEditorService.newSqlEditor(sqlContent).then((owner: IConnectableInput) => {
// Connect our editor to the input connection
let options: IConnectionCompletionOptions = {
@@ -250,9 +259,13 @@ export function newQuery(
showConnectionDialogOnError: true,
showFirewallRuleOnError: true
};
connectionService.connect(connectionProfile, owner.uri, options).then(() => {
if (connectionProfile) {
connectionService.connect(connectionProfile, owner.uri, options).then(() => {
resolve();
});
} else {
resolve();
});
}
});
});
}
@@ -331,6 +344,39 @@ export function openInsight(query: IInsightsConfig, profile: IConnectionProfile,
insightDialogService.show(query, profile);
}
/**
* Get the current global connection, which is the connection from the active editor, unless OE
* is focused or there is no such editor, in which case it comes from the OE selection. Returns
* undefined when there is no such connection.
*/
export function getCurrentGlobalConnection(objectExplorerService: IObjectExplorerService, connectionManagementService: IConnectionManagementService, workbenchEditorService: IWorkbenchEditorService): IConnectionProfile {
let connection: IConnectionProfile;
let objectExplorerSelection = objectExplorerService.getSelectedProfileAndDatabase();
if (objectExplorerSelection) {
let objectExplorerProfile = objectExplorerSelection.profile;
if (connectionManagementService.isProfileConnected(objectExplorerProfile)) {
if (objectExplorerSelection.databaseName) {
connection = objectExplorerProfile.cloneWithDatabase(objectExplorerSelection.databaseName);
} else {
connection = objectExplorerProfile;
}
}
if (objectExplorerService.isFocused()) {
return connection;
}
}
let activeInput = workbenchEditorService.getActiveEditorInput();
if (activeInput) {
if (activeInput instanceof QueryInput || activeInput instanceof EditDataInput || activeInput instanceof DashboardInput) {
connection = connectionManagementService.getConnectionProfile(activeInput.uri);
}
}
return connection;
}
/* Helper Methods */
function getStartPos(script: string, operation: ScriptOperation, typeName: string): number {
let objectTypeName = objectScriptMap[typeName];