mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-18 17:22:45 -05:00
More layering and strictness (#9004)
* move handling generated files to the serilization classes * remove unneeded methods * add more folders to strictire compile, add more strict compile options * update ci * wip * add more layering and fix issues * add more strictness * remove unnecessary assertion * add missing checks * fix indentation * wip * remove jsdoc * fix layering * fix compile * fix compile errors * wip * wip * finish layering * fix css * more layering * remove no longer good parts * fix issues with startup * another try * fix startup
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as azdata from 'azdata';
|
||||
import { INodeContextValue } from 'sql/workbench/browser/parts/views/nodeContext';
|
||||
import { RawContextKey, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService';
|
||||
import { mssqlProviderName } from 'sql/platform/connection/common/constants';
|
||||
import { NodeType } from 'sql/workbench/services/objectExplorer/common/nodeType';
|
||||
import { DatabaseEngineEdition } from 'sql/workbench/api/common/sqlExtHostTypes';
|
||||
import { isWindows } from 'vs/base/common/platform';
|
||||
|
||||
export class MssqlNodeContext extends Disposable {
|
||||
|
||||
static readonly canSelect = new Set([NodeType.Table, NodeType.View]);
|
||||
static readonly canEditData = new Set([NodeType.Table]);
|
||||
static readonly canCreateOrDelete = new Set([NodeType.AggregateFunction, NodeType.PartitionFunction, NodeType.ScalarValuedFunction,
|
||||
NodeType.Schema, NodeType.StoredProcedure, NodeType.Table, NodeType.TableValuedFunction,
|
||||
NodeType.User, NodeType.UserDefinedTableType, NodeType.View]);
|
||||
static readonly canExecute = new Set([NodeType.StoredProcedure]);
|
||||
static readonly canAlter = new Set([NodeType.AggregateFunction, NodeType.PartitionFunction, NodeType.ScalarValuedFunction,
|
||||
NodeType.StoredProcedure, NodeType.TableValuedFunction, NodeType.View]);
|
||||
|
||||
// General node context keys
|
||||
static NodeProvider = new RawContextKey<string>('nodeProvider', undefined);
|
||||
static IsDatabaseOrServer = new RawContextKey<boolean>('isDatabaseOrServer', false);
|
||||
static IsWindows = new RawContextKey<boolean>('isWindows', isWindows);
|
||||
static IsCloud = new RawContextKey<boolean>('isCloud', false);
|
||||
static NodeType = new RawContextKey<string>('nodeType', undefined);
|
||||
static NodeLabel = new RawContextKey<string>('nodeLabel', undefined);
|
||||
static EngineEdition = new RawContextKey<number>('engineEdition', DatabaseEngineEdition.Unknown);
|
||||
|
||||
// Scripting context keys
|
||||
static CanScriptAsSelect = new RawContextKey<boolean>('canScriptAsSelect', false);
|
||||
static CanEditData = new RawContextKey<boolean>('canEditData', false);
|
||||
static CanScriptAsCreateOrDelete = new RawContextKey<boolean>('canScriptAsCreateOeDelete', false);
|
||||
static CanScriptAsExecute = new RawContextKey<boolean>('canScriptAsExecute', false);
|
||||
static CanScriptAsAlter = new RawContextKey<boolean>('canScriptAsAlter', false);
|
||||
|
||||
private nodeProviderKey: IContextKey<string>;
|
||||
private isCloudKey: IContextKey<boolean>;
|
||||
private nodeTypeKey: IContextKey<string>;
|
||||
private nodeLabelKey: IContextKey<string>;
|
||||
private isDatabaseOrServerKey: IContextKey<boolean>;
|
||||
private engineEditionKey: IContextKey<number>;
|
||||
|
||||
private canScriptAsSelectKey: IContextKey<boolean>;
|
||||
private canEditDataKey: IContextKey<boolean>;
|
||||
private canScriptAsCreateOrDeleteKey: IContextKey<boolean>;
|
||||
private canScriptAsExecuteKey: IContextKey<boolean>;
|
||||
private canScriptAsAlterKey: IContextKey<boolean>;
|
||||
|
||||
constructor(
|
||||
private nodeContextValue: INodeContextValue,
|
||||
@IContextKeyService private contextKeyService: IContextKeyService,
|
||||
@IConnectionManagementService private connectionManagementService: IConnectionManagementService,
|
||||
@ICapabilitiesService private capabilitiesService: ICapabilitiesService
|
||||
) {
|
||||
super();
|
||||
this.bindContextKeys();
|
||||
|
||||
// Set additional node context keys
|
||||
if (this.nodeContextValue.node) {
|
||||
const node = this.nodeContextValue.node;
|
||||
if (node.payload) {
|
||||
this.setNodeProvider();
|
||||
this.setIsCloud();
|
||||
this.setEngineEdition();
|
||||
if (node.type) {
|
||||
this.setIsDatabaseOrServer();
|
||||
this.nodeTypeKey.set(node.type);
|
||||
} else if (node.contextValue && node.providerHandle === mssqlProviderName) {
|
||||
this.setIsDatabaseOrServer();
|
||||
this.setScriptingContextKeys();
|
||||
this.nodeTypeKey.set(node.contextValue);
|
||||
}
|
||||
}
|
||||
if (node.label) {
|
||||
this.nodeLabelKey.set(node.label.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bindContextKeys(): void {
|
||||
this.isCloudKey = MssqlNodeContext.IsCloud.bindTo(this.contextKeyService);
|
||||
this.engineEditionKey = MssqlNodeContext.EngineEdition.bindTo(this.contextKeyService);
|
||||
this.nodeTypeKey = MssqlNodeContext.NodeType.bindTo(this.contextKeyService);
|
||||
this.nodeLabelKey = MssqlNodeContext.NodeLabel.bindTo(this.contextKeyService);
|
||||
this.isDatabaseOrServerKey = MssqlNodeContext.IsDatabaseOrServer.bindTo(this.contextKeyService);
|
||||
this.canScriptAsSelectKey = MssqlNodeContext.CanScriptAsSelect.bindTo(this.contextKeyService);
|
||||
this.canEditDataKey = MssqlNodeContext.CanEditData.bindTo(this.contextKeyService);
|
||||
this.canScriptAsCreateOrDeleteKey = MssqlNodeContext.CanScriptAsCreateOrDelete.bindTo(this.contextKeyService);
|
||||
this.canScriptAsExecuteKey = MssqlNodeContext.CanScriptAsExecute.bindTo(this.contextKeyService);
|
||||
this.canScriptAsAlterKey = MssqlNodeContext.CanScriptAsAlter.bindTo(this.contextKeyService);
|
||||
this.nodeProviderKey = MssqlNodeContext.NodeProvider.bindTo(this.contextKeyService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the node provider
|
||||
*/
|
||||
private setNodeProvider(): void {
|
||||
if (this.nodeContextValue.node.payload.providerName) {
|
||||
this.nodeProviderKey.set(this.nodeContextValue.node.payload.providerName);
|
||||
} else if (this.nodeContextValue.node.childProvider) {
|
||||
this.nodeProviderKey.set(this.nodeContextValue.node.childProvider);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to tell whether a connected node is cloud or not
|
||||
*/
|
||||
private setIsCloud(): void {
|
||||
let serverInfo: azdata.ServerInfo = this.getServerInfo();
|
||||
if (serverInfo && serverInfo.isCloud) {
|
||||
this.isCloudKey.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to set engine edition
|
||||
*/
|
||||
private setEngineEdition(): void {
|
||||
|
||||
let serverInfo: azdata.ServerInfo = this.getServerInfo();
|
||||
if (serverInfo && serverInfo.engineEditionId) {
|
||||
this.engineEditionKey.set(serverInfo.engineEditionId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function fetching the server info
|
||||
*/
|
||||
private getServerInfo(): azdata.ServerInfo | undefined {
|
||||
const profile = new ConnectionProfile(this.capabilitiesService,
|
||||
this.nodeContextValue.node.payload);
|
||||
const connection = this.connectionManagementService.findExistingConnection(profile);
|
||||
if (connection) {
|
||||
return this.connectionManagementService.getServerInfo(connection.id);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to tell whether a connected node is a database or a
|
||||
* server or not. Added this key because this is easier to write than
|
||||
* writing an OR statement in ContextKeyExpr
|
||||
*/
|
||||
private setIsDatabaseOrServer(): void {
|
||||
const isDatabaseOrServer = (this.nodeContextValue.node.contextValue === NodeType.Server ||
|
||||
this.nodeContextValue.node.contextValue === NodeType.Database ||
|
||||
this.nodeContextValue.node.type === NodeType.Server ||
|
||||
this.nodeContextValue.node.type === NodeType.Database);
|
||||
this.isDatabaseOrServerKey.set(isDatabaseOrServer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the correct context from node for showing
|
||||
* scripting context menu actions
|
||||
*/
|
||||
private setScriptingContextKeys(): void {
|
||||
const nodeType = this.nodeContextValue.node.contextValue;
|
||||
if (MssqlNodeContext.canCreateOrDelete.has(nodeType)) {
|
||||
this.canScriptAsCreateOrDeleteKey.set(true);
|
||||
}
|
||||
if (MssqlNodeContext.canEditData.has(nodeType)) {
|
||||
this.canEditDataKey.set(true);
|
||||
}
|
||||
if (MssqlNodeContext.canAlter.has(nodeType)) {
|
||||
this.canScriptAsAlterKey.set(true);
|
||||
}
|
||||
if (MssqlNodeContext.canExecute.has(nodeType)) {
|
||||
this.canScriptAsExecuteKey.set(true);
|
||||
}
|
||||
if (MssqlNodeContext.canSelect.has(nodeType)) {
|
||||
this.canScriptAsSelectKey.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { NodeType } from 'sql/workbench/contrib/objectExplorer/common/nodeType';
|
||||
import { NodeType } from 'sql/workbench/services/objectExplorer/common/nodeType';
|
||||
import { TreeNode, TreeItemCollapsibleState } from 'sql/workbench/contrib/objectExplorer/common/treeNode';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as azdata from 'azdata';
|
||||
import { TreeNode } from 'sql/workbench/contrib/objectExplorer/common/treeNode';
|
||||
import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService';
|
||||
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
import { ITreeItem } from 'sql/workbench/common/views';
|
||||
import { IObjectExplorerService } from 'sql/workbench/services/objectExplorer/browser/objectExplorerService';
|
||||
import { hash } from 'vs/base/common/hash';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { generateUuid } from 'vs/base/common/uuid';
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { TreeItemCollapsibleState } from 'vs/workbench/common/views';
|
||||
import { localize } from 'vs/nls';
|
||||
import { NodeType } from 'sql/workbench/services/objectExplorer/common/nodeType';
|
||||
import { UserCancelledConnectionError } from 'sql/base/common/errors';
|
||||
import { assign } from 'vs/base/common/objects';
|
||||
|
||||
export const SERVICE_ID = 'oeShimService';
|
||||
export const IOEShimService = createDecorator<IOEShimService>(SERVICE_ID);
|
||||
|
||||
export interface IOEShimService {
|
||||
_serviceBrand: undefined;
|
||||
getChildren(node: ITreeItem, viewId: string): Promise<ITreeItem[]>;
|
||||
disconnectNode(viewId: string, node: ITreeItem): Promise<boolean>;
|
||||
providerExists(providerId: string): boolean;
|
||||
isNodeConnected(viewId: string, node: ITreeItem): boolean;
|
||||
getNodeInfoForTreeItem(treeItem: ITreeItem): azdata.NodeInfo;
|
||||
}
|
||||
|
||||
export class OEShimService extends Disposable implements IOEShimService {
|
||||
_serviceBrand: undefined;
|
||||
|
||||
private sessionMap = new Map<number, string>();
|
||||
private nodeHandleMap = new Map<number, string>();
|
||||
private nodeInfoMap = new Map<ITreeItem, azdata.NodeInfo>();
|
||||
|
||||
constructor(
|
||||
@IObjectExplorerService private oe: IObjectExplorerService,
|
||||
@IConnectionManagementService private cm: IConnectionManagementService,
|
||||
@ICapabilitiesService private capabilities: ICapabilitiesService
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
private async createSession(viewId: string, providerId: string, node: ITreeItem): Promise<string> {
|
||||
let connProfile = new ConnectionProfile(this.capabilities, node.payload);
|
||||
connProfile.saveProfile = false;
|
||||
if (this.cm.providerRegistered(providerId)) {
|
||||
connProfile = await this.connectOrPrompt(connProfile);
|
||||
} else {
|
||||
// Throw and expect upstream handler to notify about the error
|
||||
// TODO: In the future should use extension recommendations to prompt for correct extension
|
||||
throw new Error(localize('noProviderFound', "Cannot expand as the required connection provider '{0}' was not found", providerId));
|
||||
}
|
||||
let sessionResp = await this.oe.createNewSession(providerId, connProfile);
|
||||
let sessionId = sessionResp.sessionId;
|
||||
await new Promise((resolve, reject) => {
|
||||
let listener = this.oe.onUpdateObjectExplorerNodes(e => {
|
||||
if (e.connection.id === connProfile.id) {
|
||||
if (e.errorMessage) {
|
||||
listener.dispose();
|
||||
reject(new Error(e.errorMessage));
|
||||
return;
|
||||
}
|
||||
let rootNode = this.oe.getSession(sessionResp.sessionId).rootNode;
|
||||
// this is how we know it was shimmed
|
||||
if (rootNode.nodePath) {
|
||||
this.nodeHandleMap.set(generateNodeMapKey(viewId, node), rootNode.nodePath);
|
||||
}
|
||||
}
|
||||
listener.dispose();
|
||||
resolve(sessionResp.sessionId);
|
||||
});
|
||||
});
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
private async connectOrPrompt(connProfile: ConnectionProfile): Promise<ConnectionProfile> {
|
||||
connProfile = await new Promise(async (resolve, reject) => {
|
||||
let result = await this.cm.connect(connProfile, undefined, { showConnectionDialogOnError: true, showFirewallRuleOnError: true, saveTheConnection: false, showDashboard: false, params: undefined }, {
|
||||
onConnectSuccess: async (e, profile) => {
|
||||
let existingConnection = this.cm.findExistingConnection(profile);
|
||||
connProfile = new ConnectionProfile(this.capabilities, existingConnection);
|
||||
connProfile = <ConnectionProfile>await this.cm.addSavedPassword(connProfile);
|
||||
resolve(connProfile);
|
||||
},
|
||||
onConnectCanceled: () => {
|
||||
reject(new UserCancelledConnectionError(localize('loginCanceled', "User canceled")));
|
||||
},
|
||||
onConnectReject: undefined,
|
||||
onConnectStart: undefined,
|
||||
onDisconnect: undefined
|
||||
});
|
||||
// connection cancelled from firewall dialog
|
||||
if (!result) {
|
||||
reject(new UserCancelledConnectionError(localize('firewallCanceled', "Firewall dialog canceled")));
|
||||
}
|
||||
});
|
||||
return connProfile;
|
||||
}
|
||||
|
||||
public async disconnectNode(viewId: string, node: ITreeItem): Promise<boolean> {
|
||||
// we assume only nodes with payloads can be connected
|
||||
// check to make sure we have an existing connection
|
||||
let key = generateSessionMapKey(viewId, node);
|
||||
let session = this.sessionMap.get(key);
|
||||
if (session) {
|
||||
let closed = (await this.oe.closeSession(node.childProvider, this.oe.getSession(session))).success;
|
||||
if (closed) {
|
||||
this.sessionMap.delete(key);
|
||||
}
|
||||
return closed;
|
||||
}
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
private async getOrCreateSession(viewId: string, node: ITreeItem): Promise<string> {
|
||||
// verify the map is correct
|
||||
let key = generateSessionMapKey(viewId, node);
|
||||
if (!this.sessionMap.has(key)) {
|
||||
this.sessionMap.set(key, await this.createSession(viewId, node.childProvider, node));
|
||||
}
|
||||
return this.sessionMap.get(key);
|
||||
}
|
||||
|
||||
public async getChildren(node: ITreeItem, viewId: string): Promise<ITreeItem[]> {
|
||||
if (node.payload) {
|
||||
let sessionId = await this.getOrCreateSession(viewId, node);
|
||||
let requestHandle = this.nodeHandleMap.get(generateNodeMapKey(viewId, node)) || node.handle;
|
||||
let treeNode = new TreeNode(undefined, undefined, undefined, requestHandle, undefined, undefined, undefined, undefined, undefined, undefined);
|
||||
treeNode.connection = new ConnectionProfile(this.capabilities, node.payload);
|
||||
return this.oe.resolveTreeNodeChildren({
|
||||
success: undefined,
|
||||
sessionId,
|
||||
rootNode: undefined,
|
||||
errorMessage: undefined
|
||||
}, treeNode).then(e => e.map(n => this.treeNodeToITreeItem(viewId, n, node)));
|
||||
} else {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
}
|
||||
|
||||
private treeNodeToITreeItem(viewId: string, node: TreeNode, parentNode: ITreeItem): ITreeItem {
|
||||
let handle = generateUuid();
|
||||
let icon: string = '';
|
||||
let nodePath = node.nodePath;
|
||||
if (node.iconType) {
|
||||
icon = (typeof node.iconType === 'string') ? node.iconType : node.iconType.id;
|
||||
} else {
|
||||
icon = node.nodeTypeId;
|
||||
if (node.nodeStatus) {
|
||||
icon = node.nodeTypeId + '_' + node.nodeStatus;
|
||||
}
|
||||
if (node.nodeSubType) {
|
||||
icon = node.nodeTypeId + '_' + node.nodeSubType;
|
||||
}
|
||||
}
|
||||
icon = icon.toLowerCase();
|
||||
// Change the database if the node has a different database
|
||||
// than its parent
|
||||
let databaseChanged = false;
|
||||
let updatedPayload: azdata.IConnectionProfile | any = {};
|
||||
if (node.nodeTypeId === NodeType.Database) {
|
||||
const database = node.getDatabaseName();
|
||||
if (database) {
|
||||
databaseChanged = true;
|
||||
updatedPayload = assign(updatedPayload, parentNode.payload);
|
||||
updatedPayload.databaseName = node.getDatabaseName();
|
||||
}
|
||||
}
|
||||
const nodeInfo: azdata.NodeInfo = {
|
||||
nodePath: nodePath,
|
||||
nodeType: node.nodeTypeId,
|
||||
nodeSubType: node.nodeSubType,
|
||||
nodeStatus: node.nodeStatus,
|
||||
label: node.label,
|
||||
isLeaf: node.isAlwaysLeaf,
|
||||
metadata: node.metadata,
|
||||
errorMessage: node.errorStateMessage,
|
||||
iconType: icon,
|
||||
childProvider: node.childProvider || parentNode.childProvider,
|
||||
payload: node.payload || (databaseChanged ? updatedPayload : parentNode.payload)
|
||||
};
|
||||
let newTreeItem: ITreeItem = {
|
||||
parentHandle: node.parent.id,
|
||||
handle,
|
||||
collapsibleState: node.isAlwaysLeaf ? TreeItemCollapsibleState.None : TreeItemCollapsibleState.Collapsed,
|
||||
label: {
|
||||
label: node.label
|
||||
},
|
||||
childProvider: node.childProvider || parentNode.childProvider,
|
||||
providerHandle: parentNode.childProvider,
|
||||
payload: node.payload || (databaseChanged ? updatedPayload : parentNode.payload),
|
||||
contextValue: node.nodeTypeId,
|
||||
sqlIcon: icon
|
||||
};
|
||||
this.nodeHandleMap.set(generateNodeMapKey(viewId, newTreeItem), nodePath);
|
||||
this.nodeInfoMap.set(newTreeItem, nodeInfo);
|
||||
return newTreeItem;
|
||||
}
|
||||
|
||||
public providerExists(providerId: string): boolean {
|
||||
return this.oe.providerRegistered(providerId);
|
||||
}
|
||||
|
||||
public isNodeConnected(viewId: string, node: ITreeItem): boolean {
|
||||
return this.sessionMap.has(generateSessionMapKey(viewId, node));
|
||||
}
|
||||
|
||||
public getNodeInfoForTreeItem(treeItem: ITreeItem): azdata.NodeInfo {
|
||||
if (this.nodeInfoMap.has(treeItem)) {
|
||||
return this.nodeInfoMap.get(treeItem);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function generateSessionMapKey(viewId: string, node: ITreeItem): number {
|
||||
return hash([viewId, node.childProvider, node.payload]);
|
||||
}
|
||||
|
||||
function generateNodeMapKey(viewId: string, node: ITreeItem): number {
|
||||
return hash([viewId, node.handle]);
|
||||
}
|
||||
Reference in New Issue
Block a user