Files
azuredatastudio/src/sql/workbench/api/node/mainThreadQueryEditor.ts
Abbie Petchtes 965458ca74 Add Query Editor API to sqlops.proposed (#1196)
* add support query editor API

* remove sqlops.proposed.d.ts in sp_whoIsActive

* address comments

* add catch error when connect
2018-04-23 13:42:27 -07:00

79 lines
3.1 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { SqlExtHostContext, SqlMainContext, ExtHostQueryEditorShape, MainThreadQueryEditorShape } from 'sql/workbench/api/node/sqlExtHost.protocol';
import * as sqlops from 'sqlops';
import * as vscode from 'vscode';
import { IExtHostContext } from 'vs/workbench/api/node/extHost.protocol';
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
import { IConnectableInput, IConnectionManagementService, IConnectionCompletionOptions,
ConnectionType , RunQueryOnConnectionMode
} from 'sql/parts/connection/common/connectionManagement';
import { IQueryEditorService } from 'sql/parts/query/common/queryEditorService';
import { QueryEditor } from 'sql/parts/query/editor/queryEditor';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
@extHostNamedCustomer(SqlMainContext.MainThreadQueryEditor)
export class MainThreadQueryEditor implements MainThreadQueryEditorShape {
private _proxy: ExtHostQueryEditorShape;
private _toDispose: IDisposable[];
constructor(
extHostContext: IExtHostContext,
@IConnectionManagementService private _connectionManagementService: IConnectionManagementService,
@IQueryEditorService private _queryEditorService: IQueryEditorService,
@IWorkbenchEditorService private _editorService: IWorkbenchEditorService
) {
if (extHostContext) {
this._proxy = extHostContext.getProxy(SqlExtHostContext.ExtHostQueryEditor);
}
this._toDispose = [];
}
public dispose(): void {
this._toDispose = dispose(this._toDispose);
}
public $connect(fileUri: string, connectionId: string): Thenable<void> {
return new Promise<void>((resolve, reject) => {
let options: IConnectionCompletionOptions = {
params: { connectionType: ConnectionType.editor, runQueryOnCompletion: RunQueryOnConnectionMode.none },
saveTheConnection: false,
showDashboard: false,
showConnectionDialogOnError: true,
showFirewallRuleOnError: true
};
if (connectionId) {
let connection = this._connectionManagementService.getActiveConnections().filter(c => c.id === connectionId);
if (connection && connection.length > 0) {
this._connectionManagementService.connect(connection[0], fileUri, options).then(() => {
resolve();
}).catch(error => {
reject();
});
} else {
resolve();
}
} else {
resolve();
}
});
}
public $runQuery(fileUri: string): void {
let filteredEditors = this._editorService.getVisibleEditors().filter(editor => editor.input.getResource().toString() === fileUri);
if (filteredEditors && filteredEditors.length > 0) {
let editor = filteredEditors[0];
if (editor instanceof QueryEditor) {
let queryEditor: QueryEditor = editor;
queryEditor.runCurrentQuery();
}
}
}
}