/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as vscode from 'vscode'; import * as azdata from 'azdata'; import { Event, Emitter } from 'vs/base/common/event'; import { IMainContext } from 'vs/workbench/api/common/extHost.protocol'; import { Disposable } from 'vs/workbench/api/common/extHostTypes'; import { SqlMainContext, MainThreadDataProtocolShape, ExtHostDataProtocolShape } from 'sql/workbench/api/node/sqlExtHost.protocol'; import { DataProviderType } from 'sql/workbench/api/common/sqlExtHostTypes'; export class ExtHostDataProtocol extends ExtHostDataProtocolShape { private readonly _onDidChangeLanguageFlavor = new Emitter(); readonly onDidChangeLanguageFlavor: Event = this._onDidChangeLanguageFlavor.event; private _proxy: MainThreadDataProtocolShape; private static _handlePool: number = 0; private _adapter = new Map(); private _providersByType = new Map(); constructor( mainContext: IMainContext ) { super(); this._proxy = mainContext.getProxy(SqlMainContext.MainThreadDataProtocol); } private _createDisposable(handle: number): Disposable { return new Disposable(() => { this._adapter.delete(handle); this._proxy.$unregisterProvider(handle); }); } private _nextHandle(): number { return ExtHostDataProtocol._handlePool++; } private _resolveProvider

(handle: number): P { let provider = this._adapter.get(handle) as P; if (provider) { return provider; } else { throw new Error(`Unfound provider ${handle}`); } } private registerProvider(provider: azdata.DataProvider, providerType: DataProviderType): vscode.Disposable { provider.handle = this._nextHandle(); this._adapter.set(provider.handle, provider); let providersForType = this._providersByType.get(providerType); if (!providersForType) { providersForType = [provider]; } else { providersForType.push(provider); } this._providersByType.set(providerType, providersForType); return this._createDisposable(provider.handle); } public getProvider(providerId: string, providerType: azdata.DataProviderType): T { let providersForType = this._providersByType.get(providerType); if (!providersForType) { return undefined; } return providersForType.find(provider => provider.providerId === providerId) as T; } public getProvidersByType(providerType: azdata.DataProviderType): T[] { let providersForType = this._providersByType.get(providerType); return (providersForType || []) as T[]; } $registerConnectionProvider(provider: azdata.ConnectionProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.ConnectionProvider); this._proxy.$registerConnectionProvider(provider.providerId, provider.handle); return rt; } $registerBackupProvider(provider: azdata.BackupProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.BackupProvider); this._proxy.$registerBackupProvider(provider.providerId, provider.handle); return rt; } $registerRestoreProvider(provider: azdata.RestoreProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.RestoreProvider); this._proxy.$registerRestoreProvider(provider.providerId, provider.handle); return rt; } $registerScriptingProvider(provider: azdata.ScriptingProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.ScriptingProvider); this._proxy.$registerScriptingProvider(provider.providerId, provider.handle); return rt; } $registerQueryProvider(provider: azdata.QueryProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.QueryProvider); this._proxy.$registerQueryProvider(provider.providerId, provider.handle); return rt; } $registerMetadataProvider(provider: azdata.MetadataProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.MetadataProvider); this._proxy.$registerMetadataProvider(provider.providerId, provider.handle); return rt; } $registerTaskServicesProvider(provider: azdata.TaskServicesProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.TaskServicesProvider); this._proxy.$registerTaskServicesProvider(provider.providerId, provider.handle); return rt; } $registerFileBrowserProvider(provider: azdata.FileBrowserProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.FileBrowserProvider); this._proxy.$registerFileBrowserProvider(provider.providerId, provider.handle); return rt; } $registerObjectExplorerProvider(provider: azdata.ObjectExplorerProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.ObjectExplorerProvider); this._proxy.$registerObjectExplorerProvider(provider.providerId, provider.handle); return rt; } $registerObjectExplorerNodeProvider(provider: azdata.ObjectExplorerNodeProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.ObjectExplorerNodeProvider); this._proxy.$registerObjectExplorerNodeProvider(provider.providerId, provider.supportedProviderId, provider.group, provider.handle); return rt; } $registerProfilerProvider(provider: azdata.ProfilerProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.ProfilerProvider); this._proxy.$registerProfilerProvider(provider.providerId, provider.handle); return rt; } $registerAdminServicesProvider(provider: azdata.AdminServicesProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.AdminServicesProvider); this._proxy.$registerAdminServicesProvider(provider.providerId, provider.handle); return rt; } $registerAgentServiceProvider(provider: azdata.AgentServicesProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.AgentServicesProvider); this._proxy.$registerAgentServicesProvider(provider.providerId, provider.handle); return rt; } $registerCapabilitiesServiceProvider(provider: azdata.CapabilitiesProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.CapabilitiesProvider); this._proxy.$registerCapabilitiesServiceProvider(provider.providerId, provider.handle); return rt; } $registerDacFxServiceProvider(provider: azdata.DacFxServicesProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.DacFxServicesProvider); this._proxy.$registerDacFxServicesProvider(provider.providerId, provider.handle); return rt; } $registerSchemaCompareServiceProvider(provider: azdata.SchemaCompareServicesProvider): vscode.Disposable { let rt = this.registerProvider(provider, DataProviderType.SchemaCompareServicesProvider); this._proxy.$registerSchemaCompareServicesProvider(provider.providerId, provider.handle); return rt; } // Capabilities Discovery handlers $getServerCapabilities(handle: number, client: azdata.DataProtocolClientCapabilities): Thenable { return this._resolveProvider(handle).getServerCapabilities(client); } // Connection Management handlers $connect(handle: number, connectionUri: string, connection: azdata.ConnectionInfo): Thenable { return this._resolveProvider(handle).connect(connectionUri, connection); } $disconnect(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).disconnect(connectionUri); } $cancelConnect(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).cancelConnect(connectionUri); } $changeDatabase(handle: number, connectionUri: string, newDatabase: string): Thenable { return this._resolveProvider(handle).changeDatabase(connectionUri, newDatabase); } $listDatabases(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).listDatabases(connectionUri); } $getConnectionString(handle: number, connectionUri: string, includePassword: boolean): Thenable { return this._resolveProvider(handle).getConnectionString(connectionUri, includePassword); } $buildConnectionInfo(handle: number, connectionString: string): Thenable { let provider = this._resolveProvider(handle); if (provider.buildConnectionInfo) { return provider.buildConnectionInfo(connectionString); } else { return Promise.resolve(undefined); } } $rebuildIntelliSenseCache(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).rebuildIntelliSenseCache(connectionUri); } $onConnectComplete(handle: number, connectionInfoSummary: azdata.ConnectionInfoSummary): void { this._proxy.$onConnectionComplete(handle, connectionInfoSummary); } public $onIntelliSenseCacheComplete(handle: number, connectionUri: string): void { this._proxy.$onIntelliSenseCacheComplete(handle, connectionUri); } public $onConnectionChanged(handle: number, changedConnInfo: azdata.ChangedConnectionInfo): void { this._proxy.$onConnectionChangeNotification(handle, changedConnInfo); } // Protocol-wide Event Handlers public $languageFlavorChanged(params: azdata.DidChangeLanguageFlavorParams): void { this._onDidChangeLanguageFlavor.fire(params); } // Query Management handlers $cancelQuery(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).cancelQuery(ownerUri); } $runQuery(handle: number, ownerUri: string, selection: azdata.ISelectionData, runOptions?: azdata.ExecutionPlanOptions): Thenable { return this._resolveProvider(handle).runQuery(ownerUri, selection, runOptions); } $runQueryStatement(handle: number, ownerUri: string, line: number, column: number): Thenable { return this._resolveProvider(handle).runQueryStatement(ownerUri, line, column); } $runQueryString(handle: number, ownerUri: string, queryString: string): Thenable { return this._resolveProvider(handle).runQueryString(ownerUri, queryString); } $runQueryAndReturn(handle: number, ownerUri: string, queryString: string): Thenable { return this._resolveProvider(handle).runQueryAndReturn(ownerUri, queryString); } $parseSyntax(handle: number, ownerUri: string, query: string): Thenable { return this._resolveProvider(handle).parseSyntax(ownerUri, query); } $getQueryRows(handle: number, rowData: azdata.QueryExecuteSubsetParams): Thenable { return this._resolveProvider(handle).getQueryRows(rowData); } $disposeQuery(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).disposeQuery(ownerUri); } $onQueryComplete(handle: number, result: azdata.QueryExecuteCompleteNotificationResult): void { this._proxy.$onQueryComplete(handle, result); } $onBatchStart(handle: number, batchInfo: azdata.QueryExecuteBatchNotificationParams): void { this._proxy.$onBatchStart(handle, batchInfo); } $onBatchComplete(handle: number, batchInfo: azdata.QueryExecuteBatchNotificationParams): void { this._proxy.$onBatchComplete(handle, batchInfo); } $onResultSetAvailable(handle: number, resultSetInfo: azdata.QueryExecuteResultSetNotificationParams): void { this._proxy.$onResultSetAvailable(handle, resultSetInfo); } $onResultSetUpdated(handle: number, resultSetInfo: azdata.QueryExecuteResultSetNotificationParams): void { this._proxy.$onResultSetUpdated(handle, resultSetInfo); } $onQueryMessage(handle: number, message: azdata.QueryExecuteMessageParams): void { this._proxy.$onQueryMessage(handle, message); } $saveResults(handle: number, requestParams: azdata.SaveResultsRequestParams): Thenable { return this._resolveProvider(handle).saveResults(requestParams); } // Edit Data handlers $commitEdit(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).commitEdit(ownerUri); } $createRow(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).createRow(ownerUri); } $deleteRow(handle: number, ownerUri: string, rowId: number): Thenable { return this._resolveProvider(handle).deleteRow(ownerUri, rowId); } $disposeEdit(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).disposeEdit(ownerUri); } $initializeEdit(handle: number, ownerUri: string, schemaName: string, objectName: string, objectType: string, rowLimit: number, queryString: string): Thenable { return this._resolveProvider(handle).initializeEdit(ownerUri, schemaName, objectName, objectType, rowLimit, queryString); } $revertCell(handle: number, ownerUri: string, rowId: number, columnId: number): Thenable { return this._resolveProvider(handle).revertCell(ownerUri, rowId, columnId); } $revertRow(handle: number, ownerUri: string, rowId: number): Thenable { return this._resolveProvider(handle).revertRow(ownerUri, rowId); } $updateCell(handle: number, ownerUri: string, rowId: number, columnId: number, newValue: string): Thenable { return this._resolveProvider(handle).updateCell(ownerUri, rowId, columnId, newValue); } $getEditRows(handle: number, rowData: azdata.EditSubsetParams): Thenable { return this._resolveProvider(handle).getEditRows(rowData); } $onEditSessionReady(handle: number, ownerUri: string, success: boolean, message: string): void { this._proxy.$onEditSessionReady(handle, ownerUri, success, message); } // Metadata handlers public $getMetadata(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).getMetadata(connectionUri); } public $getDatabases(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).getDatabases(connectionUri); } public $getTableInfo(handle: number, connectionUri: string, metadata: azdata.ObjectMetadata): Thenable { return this._resolveProvider(handle).getTableInfo(connectionUri, metadata); } public $getViewInfo(handle: number, connectionUri: string, metadata: azdata.ObjectMetadata): Thenable { return this._resolveProvider(handle).getViewInfo(connectionUri, metadata); } // Object Explorer Service public $createObjectExplorerSession(handle: number, connInfo: azdata.ConnectionInfo): Thenable { return this._resolveProvider(handle).createNewSession(connInfo); } public $createObjectExplorerNodeProviderSession(handle: number, session: azdata.ObjectExplorerSession): Thenable { return this._resolveProvider(handle).handleSessionOpen(session); } public $expandObjectExplorerNode(handle: number, nodeInfo: azdata.ExpandNodeInfo): Thenable { return this._resolveProvider(handle).expandNode(nodeInfo); } public $refreshObjectExplorerNode(handle: number, nodeInfo: azdata.ExpandNodeInfo): Thenable { return this._resolveProvider(handle).refreshNode(nodeInfo); } public $closeObjectExplorerSession(handle: number, closeSessionInfo: azdata.ObjectExplorerCloseSessionInfo): Thenable { return this._resolveProvider(handle).closeSession(closeSessionInfo); } public $handleSessionClose(handle: number, closeSessionInfo: azdata.ObjectExplorerCloseSessionInfo): void { return this._resolveProvider(handle).handleSessionClose(closeSessionInfo); } public $findNodes(handle: number, findNodesInfo: azdata.FindNodesInfo): Thenable { return this._resolveProvider(handle).findNodes(findNodesInfo); } public $onObjectExplorerSessionCreated(handle: number, response: azdata.ObjectExplorerSession): void { this._proxy.$onObjectExplorerSessionCreated(handle, response); } public $onObjectExplorerSessionDisconnected(handle: number, response: azdata.ObjectExplorerSession): void { this._proxy.$onObjectExplorerSessionDisconnected(handle, response); } public $onObjectExplorerNodeExpanded(providerId: string, response: azdata.ObjectExplorerExpandInfo): void { this._proxy.$onObjectExplorerNodeExpanded(providerId, response); } // Task Service public $getAllTasks(handle: number, listTasksParams: azdata.ListTasksParams): Thenable { return this._resolveProvider(handle).getAllTasks(listTasksParams); } public $cancelTask(handle: number, cancelTaskParams: azdata.CancelTaskParams): Thenable { return this._resolveProvider(handle).cancelTask(cancelTaskParams); } public $onTaskStatusChanged(handle: number, response: azdata.TaskProgressInfo): void { this._proxy.$onTaskStatusChanged(handle, response); } public $onTaskCreated(handle: number, response: azdata.TaskInfo): void { this._proxy.$onTaskCreated(handle, response); } // Scripting handlers public $scriptAsOperation(handle: number, connectionUri: string, operation: azdata.ScriptOperation, metadata: azdata.ObjectMetadata, paramDetails: azdata.ScriptingParamDetails): Thenable { return this._resolveProvider(handle).scriptAsOperation(connectionUri, operation, metadata, paramDetails); } public $onScriptingComplete(handle: number, scriptingCompleteResult: azdata.ScriptingCompleteResult): void { this._proxy.$onScriptingComplete(handle, scriptingCompleteResult); } /** * Create a new database on the provided connection */ public $createDatabase(handle: number, connectionUri: string, database: azdata.DatabaseInfo): Thenable { return this._resolveProvider(handle).createDatabase(connectionUri, database); } /** * Create a new database on the provided connection */ public $getDefaultDatabaseInfo(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).getDefaultDatabaseInfo(connectionUri); } /** * Get the info on a database */ public $getDatabaseInfo(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).getDatabaseInfo(connectionUri); } /** * Create a new login on the provided connection */ public $createLogin(handle: number, connectionUri: string, login: azdata.LoginInfo): Thenable { return this._resolveProvider(handle).createLogin(connectionUri, login); } /** * Backup a database */ public $backup(handle: number, connectionUri: string, backupInfo: { [key: string]: any }, taskExecutionMode: azdata.TaskExecutionMode): Thenable { return this._resolveProvider(handle).backup(connectionUri, backupInfo, taskExecutionMode); } /** * Create a new database on the provided connection */ public $getBackupConfigInfo(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).getBackupConfigInfo(connectionUri); } /** * Restores a database */ public $restore(handle: number, connectionUri: string, restoreInfo: azdata.RestoreInfo): Thenable { return this._resolveProvider(handle).restore(connectionUri, restoreInfo); } /** * Gets a plan for restoring a database */ public $getRestorePlan(handle: number, connectionUri: string, restoreInfo: azdata.RestoreInfo): Thenable { return this._resolveProvider(handle).getRestorePlan(connectionUri, restoreInfo); } /** * cancels a restore plan */ public $cancelRestorePlan(handle: number, connectionUri: string, restoreInfo: azdata.RestoreInfo): Thenable { return this._resolveProvider(handle).cancelRestorePlan(connectionUri, restoreInfo); } /** * Gets restore config Info */ public $getRestoreConfigInfo(handle: number, connectionUri: string): Thenable { return this._resolveProvider(handle).getRestoreConfigInfo(connectionUri); } /** * Open a file browser */ public $openFileBrowser(handle: number, ownerUri: string, expandPath: string, fileFilters: string[], changeFilter: boolean): Thenable { return this._resolveProvider(handle).openFileBrowser(ownerUri, expandPath, fileFilters, changeFilter); } /** * Send event when opening browser is complete */ public $onFileBrowserOpened(handle: number, response: azdata.FileBrowserOpenedParams): void { this._proxy.$onFileBrowserOpened(handle, response); } /** * Expand a folder node */ public $expandFolderNode(handle: number, ownerUri: string, expandPath: string): Thenable { return this._resolveProvider(handle).expandFolderNode(ownerUri, expandPath); } /** * Send event when expansion is complete */ public $onFolderNodeExpanded(handle: number, response: azdata.FileBrowserExpandedParams): void { this._proxy.$onFolderNodeExpanded(handle, response); } /** * Validate selected file path */ public $validateFilePaths(handle: number, ownerUri: string, serviceType: string, selectedFiles: string[]): Thenable { return this._resolveProvider(handle).validateFilePaths(ownerUri, serviceType, selectedFiles); } /** * Send event when validation is complete */ public $onFilePathsValidated(handle: number, response: azdata.FileBrowserValidatedParams) { this._proxy.$onFilePathsValidated(handle, response); } /** * Close file browser */ public $closeFileBrowser(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).closeFileBrowser(ownerUri); } /** * Profiler Provider methods */ /** * Create a new profiler session */ public $createSession(handle: number, sessionId: string, createStatement: string, template: azdata.ProfilerSessionTemplate): Thenable { return this._resolveProvider(handle).createSession(sessionId, createStatement, template); } /** * Start a profiler session */ public $startSession(handle: number, sessionId: string, sessionName: string): Thenable { return this._resolveProvider(handle).startSession(sessionId, sessionName); } /** * Stop a profiler session */ public $stopSession(handle: number, sessionId: string): Thenable { return this._resolveProvider(handle).stopSession(sessionId); } /** * Pause a profiler session */ public $pauseSession(handle: number, sessionId: string): Thenable { return this._resolveProvider(handle).pauseSession(sessionId); } /** * Disconnect a profiler session */ public $disconnectSession(handle: number, sessionId: string): Thenable { return this._resolveProvider(handle).disconnectSession(sessionId); } /** * Get list of running XEvent sessions on the session's target server */ public $getXEventSessions(handle: number, sessionId: string): Thenable { return this._resolveProvider(handle).getXEventSessions(sessionId); } /** * Profiler session events available notification */ public $onSessionEventsAvailable(handle: number, response: azdata.ProfilerSessionEvents): void { this._proxy.$onSessionEventsAvailable(handle, response); } /** * Profiler session stopped unexpectedly notification */ public $onSessionStopped(handle: number, response: azdata.ProfilerSessionStoppedParams): void { this._proxy.$onSessionStopped(handle, response); } /** * Profiler session created notification */ public $onProfilerSessionCreated(handle: number, response: azdata.ProfilerSessionCreatedParams): void { this._proxy.$onProfilerSessionCreated(handle, response); } /** * Agent Job Provider methods */ /** * Get Agent Job list */ public $getJobs(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).getJobs(ownerUri); } /** * Get a Agent Job's history */ public $getJobHistory(handle: number, ownerUri: string, jobID: string, jobName: string): Thenable { return this._resolveProvider(handle).getJobHistory(ownerUri, jobID, jobName); } /** * Run an action on a job */ public $jobAction(handle: number, ownerUri: string, jobName: string, action: string): Thenable { return this._resolveProvider(handle).jobAction(ownerUri, jobName, action); } /** * Deletes a job */ $deleteJob(handle: number, ownerUri: string, job: azdata.AgentJobInfo): Thenable { throw this._resolveProvider(handle).deleteJob(ownerUri, job); } /** * Deletes a job step */ $deleteJobStep(handle: number, ownerUri: string, step: azdata.AgentJobStepInfo): Thenable { throw this._resolveProvider(handle).deleteJobStep(ownerUri, step); } /** * Get Agent Alerts list */ $getAlerts(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).getAlerts(ownerUri); } /** * Deletes an alert */ $deleteAlert(handle: number, ownerUri: string, alert: azdata.AgentAlertInfo): Thenable { return this._resolveProvider(handle).deleteAlert(ownerUri, alert); } /** * Get Agent Oeprators list */ $getOperators(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).getOperators(ownerUri); } /** * Deletes an operator */ $deleteOperator(handle: number, ownerUri: string, operator: azdata.AgentOperatorInfo): Thenable { return this._resolveProvider(handle).deleteOperator(ownerUri, operator); } /** * Get Agent Proxies list */ $getProxies(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).getProxies(ownerUri); } /** * Deletes a proxy */ $deleteProxy(handle: number, ownerUri: string, proxy: azdata.AgentProxyInfo): Thenable { return this._resolveProvider(handle).deleteProxy(ownerUri, proxy); } /** * Gets Agent Credentials from server */ $getCredentials(handle: number, ownerUri: string): Thenable { return this._resolveProvider(handle).getCredentials(ownerUri); } /** * SQL Agent job data update notification */ public $onJobDataUpdated(handle: Number): void { this._proxy.$onJobDataUpdated(handle); } }