mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-19 01:25:36 -05:00
SQL Operations Studio Public Preview 1 (0.23) release source code
This commit is contained in:
51
src/sql/workbench/common/actions.contribution.ts
Normal file
51
src/sql/workbench/common/actions.contribution.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import { registerTask } from 'sql/platform/tasks/taskRegistry';
|
||||
import * as Actions from './actions';
|
||||
|
||||
import { IJSONSchema } from 'vs/base/common/jsonSchema';
|
||||
import * as nls from 'vs/nls';
|
||||
|
||||
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
|
||||
import { Registry } from 'vs/platform/registry/common/platform';
|
||||
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actionRegistry';
|
||||
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions';
|
||||
import { ShowCurrentReleaseNotesAction, ProductContribution } from 'sql/workbench/update/releaseNotes';
|
||||
|
||||
const backupSchema: IJSONSchema = {
|
||||
description: nls.localize('carbon.actions.back', 'Open up backup dialog'),
|
||||
type: 'null',
|
||||
default: null
|
||||
};
|
||||
|
||||
const restoreSchema: IJSONSchema = {
|
||||
description: nls.localize('carbon.actions.restore', 'Open up restore dialog'),
|
||||
type: 'null',
|
||||
default: null
|
||||
};
|
||||
|
||||
const newQuerySchema: IJSONSchema = {
|
||||
description: nls.localize('carbon.actions.newQuery', 'Open a new query window'),
|
||||
type: 'null',
|
||||
default: null
|
||||
};
|
||||
|
||||
const configureDashboardSchema: IJSONSchema = {
|
||||
description: nls.localize('carbon.actions.configureDashboard', 'Configure the Management Dashboard'),
|
||||
type: 'null',
|
||||
default: null
|
||||
};
|
||||
|
||||
registerTask('backup', '', backupSchema, Actions.BackupAction);
|
||||
registerTask('restore', '', restoreSchema, Actions.RestoreAction);
|
||||
registerTask('new-query', '', newQuerySchema, Actions.NewQueryAction);
|
||||
registerTask('configure-dashboard', '', configureDashboardSchema, Actions.ConfigureDashboardAction);
|
||||
|
||||
// add product update and release notes contributions
|
||||
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
|
||||
.registerWorkbenchContribution(ProductContribution);
|
||||
|
||||
Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions)
|
||||
.registerWorkbenchAction(new SyncActionDescriptor(ShowCurrentReleaseNotesAction, ShowCurrentReleaseNotesAction.ID, ShowCurrentReleaseNotesAction.LABEL), 'Show Getting Started');
|
||||
369
src/sql/workbench/common/actions.ts
Normal file
369
src/sql/workbench/common/actions.ts
Normal file
@@ -0,0 +1,369 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IConnectionManagementService, IErrorMessageService } from 'sql/parts/connection/common/connectionManagement';
|
||||
import * as TaskUtilities from './taskUtilities';
|
||||
import { IQueryEditorService } from 'sql/parts/query/common/queryEditorService';
|
||||
import { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
|
||||
import { ConnectionManagementInfo } from 'sql/parts/connection/common/connectionManagementInfo';
|
||||
import { IInsightsConfig } from 'sql/parts/dashboard/widgets/insights/interfaces';
|
||||
import { IScriptingService } from 'sql/services/scripting/scriptingService';
|
||||
import { IDisasterRecoveryUiService, IRestoreDialogController } from 'sql/parts/disasterRecovery/common/interfaces';
|
||||
import { IAngularEventingService, AngularEventType } from 'sql/services/angularEventing/angularEventingService';
|
||||
import { IInsightsDialogService } from 'sql/parts/insights/common/interfaces';
|
||||
import { IAdminService } from 'sql/parts/admin/common/adminService';
|
||||
import * as Constants from 'sql/common/constants';
|
||||
import { ObjectMetadata } from 'data';
|
||||
import { ScriptOperation } from 'sql/workbench/common/taskUtilities';
|
||||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { Action } from 'vs/base/common/actions';
|
||||
import { IWindowsService } from 'vs/platform/windows/common/windows';
|
||||
|
||||
import * as nls from 'vs/nls';
|
||||
|
||||
export class TaskAction extends Action {
|
||||
constructor(id: string, label: string, private _icon: string) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
get icon(): string {
|
||||
return this._icon;
|
||||
}
|
||||
}
|
||||
|
||||
export interface BaseActionContext extends ITaskActionContext {
|
||||
uri?: string;
|
||||
object?: ObjectMetadata;
|
||||
connInfo?: ConnectionManagementInfo;
|
||||
}
|
||||
|
||||
export interface ITaskActionContext {
|
||||
profile: IConnectionProfile;
|
||||
}
|
||||
|
||||
export interface InsightActionContext extends BaseActionContext {
|
||||
insight: IInsightsConfig;
|
||||
}
|
||||
|
||||
// --- actions
|
||||
export class NewQueryAction extends TaskAction {
|
||||
public static ID = 'newQuery';
|
||||
public static LABEL = nls.localize('newQuery', 'New Query');
|
||||
public static ICON = 'new-query';
|
||||
|
||||
constructor(
|
||||
id: string, label: string, icon: string,
|
||||
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
|
||||
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService
|
||||
) {
|
||||
super(id, label, icon);
|
||||
}
|
||||
|
||||
public run(actionContext: ITaskActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.newQuery(
|
||||
actionContext.profile,
|
||||
this._connectionManagementService,
|
||||
this._queryEditorService
|
||||
).then(
|
||||
result => {
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ScriptSelectAction extends Action {
|
||||
public static ID = 'selectTop';
|
||||
public static LABEL = nls.localize('scriptSelect', 'Select Top 1000');
|
||||
|
||||
constructor(
|
||||
id: string, label: string,
|
||||
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
|
||||
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
|
||||
@IScriptingService protected _scriptingService: IScriptingService
|
||||
) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
public run(actionContext: BaseActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.scriptSelect(
|
||||
actionContext.profile,
|
||||
actionContext.object,
|
||||
actionContext.uri,
|
||||
this._connectionManagementService,
|
||||
this._queryEditorService,
|
||||
this._scriptingService
|
||||
).then(
|
||||
result => {
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class EditDataAction extends Action {
|
||||
public static ID = 'editData';
|
||||
public static LABEL = nls.localize('editData', 'Edit Data');
|
||||
|
||||
constructor(
|
||||
id: string, label: string,
|
||||
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
|
||||
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService
|
||||
) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
public run(actionContext: BaseActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.editData(
|
||||
actionContext.profile,
|
||||
actionContext.object.name,
|
||||
actionContext.object.schema,
|
||||
this._connectionManagementService,
|
||||
this._queryEditorService
|
||||
).then(
|
||||
result => {
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ScriptCreateAction extends Action {
|
||||
public static ID = 'scriptCreate';
|
||||
public static LABEL = nls.localize('scriptCreate', "Script as Create");
|
||||
|
||||
constructor(
|
||||
id: string, label: string,
|
||||
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
|
||||
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
|
||||
@IScriptingService protected _scriptingService: IScriptingService,
|
||||
@IErrorMessageService protected _errorMessageService: IErrorMessageService
|
||||
) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
public run(actionContext: BaseActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.script(
|
||||
actionContext.profile,
|
||||
actionContext.object,
|
||||
actionContext.uri,
|
||||
this._connectionManagementService,
|
||||
this._queryEditorService,
|
||||
this._scriptingService,
|
||||
ScriptOperation.Create,
|
||||
this._errorMessageService
|
||||
).then(
|
||||
result => {
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ScriptDeleteAction extends Action {
|
||||
public static ID = 'scriptDelete';
|
||||
public static LABEL = nls.localize('scriptDelete', 'Script as Delete');
|
||||
|
||||
constructor(
|
||||
id: string, label: string,
|
||||
@IQueryEditorService protected _queryEditorService: IQueryEditorService,
|
||||
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
|
||||
@IScriptingService protected _scriptingService: IScriptingService,
|
||||
@IErrorMessageService protected _errorMessageService: IErrorMessageService
|
||||
) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
public run(actionContext: BaseActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.script(
|
||||
actionContext.profile,
|
||||
actionContext.object,
|
||||
actionContext.uri,
|
||||
this._connectionManagementService,
|
||||
this._queryEditorService,
|
||||
this._scriptingService,
|
||||
ScriptOperation.Delete,
|
||||
this._errorMessageService
|
||||
).then(
|
||||
result => {
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class BackupAction extends TaskAction {
|
||||
public static ID = Constants.BackupFeatureName;
|
||||
public static LABEL = nls.localize('backup', 'Backup');
|
||||
public static ICON = Constants.BackupFeatureName;
|
||||
|
||||
constructor(
|
||||
id: string, label: string, icon: string,
|
||||
@IDisasterRecoveryUiService protected _disasterRecoveryService: IDisasterRecoveryUiService
|
||||
) {
|
||||
super(id, label, icon);
|
||||
}
|
||||
|
||||
run(actionContext: ITaskActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.showBackup(
|
||||
actionContext.profile,
|
||||
this._disasterRecoveryService,
|
||||
).then(
|
||||
result => {
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class RestoreAction extends TaskAction {
|
||||
public static ID = Constants.RestoreFeatureName;
|
||||
public static LABEL = nls.localize('restore', 'Restore');
|
||||
public static ICON = Constants.RestoreFeatureName;
|
||||
|
||||
constructor(
|
||||
id: string, label: string, icon: string,
|
||||
@IRestoreDialogController protected _restoreService: IRestoreDialogController
|
||||
) {
|
||||
super(id, label, icon);
|
||||
}
|
||||
|
||||
run(actionContext: ITaskActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.showRestore(
|
||||
actionContext.profile,
|
||||
this._restoreService
|
||||
).then(
|
||||
result => {
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ManageAction extends Action {
|
||||
public static ID = 'manage';
|
||||
public static LABEL = nls.localize('manage', 'Manage');
|
||||
|
||||
constructor(
|
||||
id: string, label: string,
|
||||
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
|
||||
@IAngularEventingService protected _angularEventingService: IAngularEventingService
|
||||
) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
run(actionContext: BaseActionContext): TPromise<boolean> {
|
||||
let self = this;
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
self._connectionManagementService.connect(actionContext.profile, actionContext.uri, { showDashboard: true, saveTheConnection: false, params: undefined, showConnectionDialogOnError: false, showFirewallRuleOnError: true }).then(
|
||||
() => {
|
||||
self._angularEventingService.sendAngularEvent(actionContext.uri, AngularEventType.NAV_DATABASE);
|
||||
resolve(true);
|
||||
},
|
||||
error => {
|
||||
resolve(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class InsightAction extends Action {
|
||||
public static ID = 'showInsight';
|
||||
public static LABEL = nls.localize('showDetails', 'Show Details');
|
||||
|
||||
constructor(
|
||||
id: string, label: string,
|
||||
@IInsightsDialogService protected _insightsDialogService: IInsightsDialogService
|
||||
) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
run(actionContext: InsightActionContext): TPromise<boolean> {
|
||||
let self = this;
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
self._insightsDialogService.show(actionContext.insight, actionContext.profile);
|
||||
resolve(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class NewDatabaseAction extends TaskAction {
|
||||
public static ID = 'newDatabase';
|
||||
public static LABEL = nls.localize('newDatabase', 'New Database');
|
||||
public static ICON = 'new-database';
|
||||
|
||||
constructor(
|
||||
id: string, label: string, icon: string,
|
||||
@IAdminService private _adminService: IAdminService,
|
||||
@IErrorMessageService private _errorMessageService: IErrorMessageService,
|
||||
) {
|
||||
super(id, label, icon);
|
||||
}
|
||||
|
||||
run(actionContext: ITaskActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
TaskUtilities.showCreateDatabase(actionContext.profile, this._adminService, this._errorMessageService);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ConfigureDashboardAction extends TaskAction {
|
||||
public static ID = 'configureDashboard';
|
||||
public static LABEL = nls.localize('configureDashboard', 'Configure');
|
||||
public static ICON = 'configure-dashboard';
|
||||
private static readonly configHelpUri = 'https://aka.ms/sqldashboardconfig';
|
||||
constructor(
|
||||
id: string, label: string, icon: string,
|
||||
@IWindowsService private _windowsService
|
||||
) {
|
||||
super(id, label, icon);
|
||||
}
|
||||
|
||||
run(actionContext: ITaskActionContext): TPromise<boolean> {
|
||||
return new TPromise<boolean>((resolve, reject) => {
|
||||
this._windowsService.openExternal(ConfigureDashboardAction.configHelpUri).then((result) => {
|
||||
resolve(result);
|
||||
}, err => {
|
||||
resolve(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
67
src/sql/workbench/common/sqlWorkbenchUtils.ts
Normal file
67
src/sql/workbench/common/sqlWorkbenchUtils.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 ConnectionConstants = require('sql/parts/connection/common/constants');
|
||||
import { QueryInput } from 'sql/parts/query/common/queryInput';
|
||||
|
||||
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
|
||||
import { IEditorInput } from 'vs/platform/editor/common/editor';
|
||||
import URI from 'vs/base/common/uri';
|
||||
|
||||
/**
|
||||
* Gets the 'sql' configuration section for use in looking up settings. Note that configs under
|
||||
* 'mssql' or other sections are not available from this.
|
||||
*
|
||||
* @export
|
||||
* @param {IWorkspaceConfigurationService} workspaceConfigService
|
||||
* @param {string} sectionName
|
||||
* @returns {*}
|
||||
*/
|
||||
export function getSqlConfigSection(workspaceConfigService: IWorkspaceConfigurationService, sectionName: string): any {
|
||||
let config = workspaceConfigService.getConfiguration(ConnectionConstants.sqlConfigSectionName);
|
||||
return config ? config[sectionName] : {};
|
||||
}
|
||||
|
||||
export function getSqlConfigValue<T>(workspaceConfigService: IWorkspaceConfigurationService, configName: string): T {
|
||||
let config = workspaceConfigService.getConfiguration(ConnectionConstants.sqlConfigSectionName);
|
||||
return config[configName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a copy operation for an arbitrary string, by creating a temp div, copying
|
||||
* the text to it, and calling copy on the document. This will clear any existing selection
|
||||
* so if being called where selection state needs to be preserved, it's recommended to
|
||||
* cache the existing selection first and re-set it after this method is called
|
||||
*
|
||||
* @export
|
||||
* @param {string} text
|
||||
*/
|
||||
export function executeCopy(text: string): void {
|
||||
let input = document.createElement('textarea');
|
||||
document.body.appendChild(input);
|
||||
input.value = text;
|
||||
input.style.position = 'absolute';
|
||||
input.style.bottom = '100%';
|
||||
input.focus();
|
||||
input.select();
|
||||
document.execCommand('copy');
|
||||
input.remove();
|
||||
}
|
||||
|
||||
export function getEditorUri(input: IEditorInput): string{
|
||||
let uri: URI;
|
||||
if (input instanceof QueryInput) {
|
||||
let queryCast: QueryInput = <QueryInput> input;
|
||||
if (queryCast) {
|
||||
uri = queryCast.getResource();
|
||||
}
|
||||
}
|
||||
|
||||
if (uri) {
|
||||
return uri.toString();
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
374
src/sql/workbench/common/taskUtilities.ts
Normal file
374
src/sql/workbench/common/taskUtilities.ts
Normal file
@@ -0,0 +1,374 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
|
||||
import {
|
||||
IConnectableInput, IConnectionManagementService,
|
||||
IConnectionCompletionOptions, ConnectionType, IErrorMessageService,
|
||||
RunQueryOnConnectionMode, IConnectionResult
|
||||
} from 'sql/parts/connection/common/connectionManagement';
|
||||
import { IQueryEditorService } from 'sql/parts/query/common/queryEditorService';
|
||||
import { IScriptingService } from 'sql/services/scripting/scriptingService';
|
||||
import { EditDataInput } from 'sql/parts/editData/common/editDataInput';
|
||||
import { IAdminService } from 'sql/parts/admin/common/adminService';
|
||||
import { IDisasterRecoveryUiService, IRestoreDialogController } from 'sql/parts/disasterRecovery/common/interfaces';
|
||||
import { IInsightsConfig } from 'sql/parts/dashboard/widgets/insights/interfaces';
|
||||
import { IInsightsDialogService } from 'sql/parts/insights/common/interfaces';
|
||||
import { ConnectionManagementInfo } from 'sql/parts/connection/common/connectionManagementInfo';
|
||||
import Severity from 'vs/base/common/severity';
|
||||
import data = require('data');
|
||||
import nls = require('vs/nls');
|
||||
import os = require('os');
|
||||
import path = require('path');
|
||||
|
||||
// map for the version of SQL Server (default is 140)
|
||||
let scriptCompatibilityOptionMap = new Map<number, string>();
|
||||
scriptCompatibilityOptionMap.set(90, "Script90Compat");
|
||||
scriptCompatibilityOptionMap.set(100, "Script100Compat");
|
||||
scriptCompatibilityOptionMap.set(105, "Script105Compat");
|
||||
scriptCompatibilityOptionMap.set(110, "Script110Compat");
|
||||
scriptCompatibilityOptionMap.set(120, "Script120Compat");
|
||||
scriptCompatibilityOptionMap.set(130, "Script130Compat");
|
||||
scriptCompatibilityOptionMap.set(140, "Script140Compat");
|
||||
|
||||
// map for the target database engine edition (default is Enterprise)
|
||||
let targetDatabaseEngineEditionMap = new Map<number, string>();
|
||||
targetDatabaseEngineEditionMap.set(0, "SqlServerEnterpriseEdition");
|
||||
targetDatabaseEngineEditionMap.set(1, "SqlServerPersonalEdition");
|
||||
targetDatabaseEngineEditionMap.set(2, "SqlServerStandardEdition");
|
||||
targetDatabaseEngineEditionMap.set(3, "SqlServerEnterpriseEdition");
|
||||
targetDatabaseEngineEditionMap.set(4, "SqlServerExpressEdition");
|
||||
targetDatabaseEngineEditionMap.set(5, "SqlAzureDatabaseEdition");
|
||||
targetDatabaseEngineEditionMap.set(6, "SqlDatawarehouseEdition");
|
||||
targetDatabaseEngineEditionMap.set(7, "SqlServerStretchEdition");
|
||||
|
||||
// map for object types for scripting
|
||||
let objectScriptMap = new Map<string, string>();
|
||||
objectScriptMap.set("Table", "Table");
|
||||
objectScriptMap.set("View", "View");
|
||||
objectScriptMap.set("StoredProcedure", "Procedure");
|
||||
objectScriptMap.set("UserDefinedFunction", "Function");
|
||||
objectScriptMap.set("UserDefinedDataType", "Type");
|
||||
objectScriptMap.set("User", "User");
|
||||
objectScriptMap.set("Default", "Default");
|
||||
objectScriptMap.set("Rule", "Rule");
|
||||
objectScriptMap.set("DatabaseRole", "Role");
|
||||
objectScriptMap.set("ApplicationRole", "Application Role");
|
||||
objectScriptMap.set("SqlAssembly", "Assembly");
|
||||
objectScriptMap.set("DdlTrigger", "Trigger");
|
||||
objectScriptMap.set("Synonym", "Synonym");
|
||||
objectScriptMap.set("XmlSchemaCollection", "Xml Schema Collection");
|
||||
objectScriptMap.set("Schema", "Schema");
|
||||
objectScriptMap.set("PlanGuide", "sp_create_plan_guide");
|
||||
objectScriptMap.set("UserDefinedType", "Type");
|
||||
objectScriptMap.set("UserDefinedAggregate", "Aggregate");
|
||||
objectScriptMap.set("FullTextCatalog", "Fulltext Catalog");
|
||||
objectScriptMap.set("UserDefinedTableType", "Type");
|
||||
objectScriptMap.set("MaterializedView", "Materialized View");
|
||||
|
||||
export enum ScriptOperation {
|
||||
Select = 0,
|
||||
Create = 1,
|
||||
Insert = 2,
|
||||
Update = 3,
|
||||
Delete = 4
|
||||
}
|
||||
|
||||
export function GetScriptOperationName(operation: ScriptOperation) {
|
||||
let defaultName: string = ScriptOperation[operation];
|
||||
switch(operation) {
|
||||
case ScriptOperation.Select:
|
||||
return nls.localize('selectOperationName', 'Select');
|
||||
case ScriptOperation.Create:
|
||||
return nls.localize('createOperationName', 'Create');
|
||||
case ScriptOperation.Insert:
|
||||
return nls.localize('insertOperationName', 'Insert');
|
||||
case ScriptOperation.Update:
|
||||
return nls.localize('updateOperationName', 'Update');
|
||||
case ScriptOperation.Delete:
|
||||
return nls.localize('deleteOperationName', 'Delete');
|
||||
default:
|
||||
// return the raw, non-localized string name
|
||||
return defaultName;
|
||||
}
|
||||
}
|
||||
|
||||
export function connectIfNotAlreadyConnected(connectionProfile: IConnectionProfile, connectionService: IConnectionManagementService): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
let connectionID = connectionService.getConnectionId(connectionProfile);
|
||||
let uri: string = connectionService.getFormattedUri(connectionID, connectionProfile);
|
||||
if (!connectionService.isConnected(uri)) {
|
||||
let options: IConnectionCompletionOptions = {
|
||||
params: { connectionType: ConnectionType.editor, runQueryOnCompletion: RunQueryOnConnectionMode.executeQuery, input: undefined },
|
||||
saveTheConnection: false,
|
||||
showDashboard: false,
|
||||
showConnectionDialogOnError: false,
|
||||
showFirewallRuleOnError: true
|
||||
};
|
||||
connectionService.connect(connectionProfile, uri, options).then(() => {
|
||||
setTimeout(function () {
|
||||
resolve();
|
||||
}, 2000);
|
||||
}).catch(connectionError => {
|
||||
reject(connectionError);
|
||||
});
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the top rows from an object
|
||||
*/
|
||||
export function scriptSelect(connectionProfile: IConnectionProfile, metadata: data.ObjectMetadata, ownerUri: string, connectionService: IConnectionManagementService, queryEditorService: IQueryEditorService, scriptingService: IScriptingService): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
connectIfNotAlreadyConnected(connectionProfile, connectionService).then(connectionResult => {
|
||||
let paramDetails: data.ScriptingParamDetails = getScriptingParamDetails(connectionService, ownerUri, metadata);
|
||||
scriptingService.script(ownerUri, metadata, ScriptOperation.Select, paramDetails).then(result => {
|
||||
if (result.script) {
|
||||
queryEditorService.newSqlEditor(result.script).then((owner: IConnectableInput) => {
|
||||
// Connect our editor to the input connection
|
||||
let options: IConnectionCompletionOptions = {
|
||||
params: { connectionType: ConnectionType.editor, runQueryOnCompletion: RunQueryOnConnectionMode.executeQuery, input: owner },
|
||||
saveTheConnection: false,
|
||||
showDashboard: false,
|
||||
showConnectionDialogOnError: true,
|
||||
showFirewallRuleOnError: true
|
||||
};
|
||||
connectionService.connect(connectionProfile, owner.uri, options).then(() => {
|
||||
resolve();
|
||||
});
|
||||
}).catch(editorError => {
|
||||
reject(editorError);
|
||||
});
|
||||
} else {
|
||||
let errMsg: string = nls.localize('scriptSelectNotFound', 'No script was returned when calling select script on object ');
|
||||
reject(errMsg.concat(metadata.metadataTypeName));
|
||||
}
|
||||
}, scriptError => {
|
||||
reject(scriptError);
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a new Edit Data session
|
||||
*/
|
||||
export function editData(connectionProfile: IConnectionProfile, tableName: string, schemaName: string, connectionService: IConnectionManagementService, queryEditorService: IQueryEditorService): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
queryEditorService.newEditDataEditor(schemaName, tableName).then((owner: EditDataInput) => {
|
||||
// Connect our editor
|
||||
let options: IConnectionCompletionOptions = {
|
||||
params: { connectionType: ConnectionType.editor, runQueryOnCompletion: RunQueryOnConnectionMode.none, input: owner },
|
||||
saveTheConnection: false,
|
||||
showDashboard: false,
|
||||
showConnectionDialogOnError: true,
|
||||
showFirewallRuleOnError: true
|
||||
};
|
||||
connectionService.connect(connectionProfile, owner.uri, options).then(() => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Script the object as a statement based on the provided action (except Select)
|
||||
*/
|
||||
export function script(connectionProfile: IConnectionProfile, metadata: data.ObjectMetadata, ownerUri: string,
|
||||
connectionService: IConnectionManagementService,
|
||||
queryEditorService: IQueryEditorService,
|
||||
scriptingService: IScriptingService,
|
||||
operation: ScriptOperation,
|
||||
errorMessageService: IErrorMessageService): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
connectIfNotAlreadyConnected(connectionProfile, connectionService).then(connectionResult => {
|
||||
let paramDetails = getScriptingParamDetails(connectionService, ownerUri, metadata);
|
||||
scriptingService.script(ownerUri, metadata, operation, paramDetails).then(result => {
|
||||
if (result) {
|
||||
let script: string = result.script;
|
||||
let startPos: number = 0;
|
||||
if (connectionProfile.providerName === "MSSQL") {
|
||||
startPos = getStartPos(script, operation, metadata.metadataTypeName);
|
||||
}
|
||||
if (startPos >= 0) {
|
||||
script = script.substring(startPos);
|
||||
queryEditorService.newSqlEditor(script, connectionProfile.providerName).then(() => {
|
||||
resolve();
|
||||
}).catch(editorError => {
|
||||
reject(editorError);
|
||||
});
|
||||
}
|
||||
else {
|
||||
let scriptNotFoundMsg = nls.localize('scriptNotFoundForObject', 'No script was returned when scripting as {0} on object {1}',
|
||||
GetScriptOperationName(operation), metadata.metadataTypeName);
|
||||
let operationResult = scriptingService.getOperationFailedResult(result.operationId);
|
||||
if (operationResult && operationResult.hasError && operationResult.errorMessage) {
|
||||
scriptNotFoundMsg = operationResult.errorMessage;
|
||||
}
|
||||
if (errorMessageService) {
|
||||
let title = nls.localize('scriptingFailed', 'Scripting Failed');
|
||||
errorMessageService.showDialog(Severity.Error, title, scriptNotFoundMsg);
|
||||
}
|
||||
reject(scriptNotFoundMsg);
|
||||
}
|
||||
} else {
|
||||
reject(nls.localize('scriptNotFound', 'No script was returned when scripting as {0}', GetScriptOperationName(operation)));
|
||||
}
|
||||
}, scriptingError => {
|
||||
reject(scriptingError);
|
||||
});
|
||||
}).catch(connectionError => {
|
||||
reject(connectionError);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function newQuery(
|
||||
connectionProfile: IConnectionProfile,
|
||||
connectionService: IConnectionManagementService,
|
||||
queryEditorService: IQueryEditorService,
|
||||
sqlContent?: string,
|
||||
executeOnOpen: RunQueryOnConnectionMode = RunQueryOnConnectionMode.none
|
||||
): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
queryEditorService.newSqlEditor(sqlContent).then((owner: IConnectableInput) => {
|
||||
// Connect our editor to the input connection
|
||||
let options: IConnectionCompletionOptions = {
|
||||
params: { connectionType: ConnectionType.editor, runQueryOnCompletion: executeOnOpen, input: owner },
|
||||
saveTheConnection: false,
|
||||
showDashboard: false,
|
||||
showConnectionDialogOnError: true,
|
||||
showFirewallRuleOnError: true
|
||||
};
|
||||
connectionService.connect(connectionProfile, owner.uri, options).then(() => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function replaceConnection(oldUri: string, newUri: string, connectionService: IConnectionManagementService): Promise<IConnectionResult> {
|
||||
return new Promise<IConnectionResult>((resolve, reject) => {
|
||||
let defaultResult: IConnectionResult = {
|
||||
connected: false,
|
||||
errorMessage: undefined,
|
||||
errorCode: undefined
|
||||
};
|
||||
if (connectionService) {
|
||||
let connectionProfile = connectionService.getConnectionProfile(oldUri);
|
||||
if (connectionProfile) {
|
||||
let options: IConnectionCompletionOptions = {
|
||||
params: { connectionType: ConnectionType.editor, runQueryOnCompletion: RunQueryOnConnectionMode.none },
|
||||
saveTheConnection: false,
|
||||
showDashboard: false,
|
||||
showConnectionDialogOnError: true,
|
||||
showFirewallRuleOnError: true
|
||||
};
|
||||
connectionService.disconnect(oldUri).then(() => {
|
||||
connectionService.connect(connectionProfile, newUri, options).then(result => {
|
||||
resolve(result);
|
||||
}, connectError => {
|
||||
reject(connectError);
|
||||
});
|
||||
}, disconnectError => {
|
||||
reject(disconnectError);
|
||||
});
|
||||
|
||||
} else {
|
||||
resolve(defaultResult);
|
||||
}
|
||||
} else {
|
||||
resolve(defaultResult);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function showCreateDatabase(
|
||||
connection: IConnectionProfile,
|
||||
adminService: IAdminService,
|
||||
errorMessageService: IErrorMessageService): Promise<void> {
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
// show not implemented
|
||||
errorMessageService.showDialog(Severity.Info,
|
||||
'Coming Soon',
|
||||
'This feature is not yet implemented. It will be available in an upcoming release.');
|
||||
|
||||
// adminService.showCreateDatabaseWizard(uri, connection);
|
||||
});
|
||||
}
|
||||
|
||||
export function showCreateLogin(uri: string, connection: IConnectionProfile, adminService: IAdminService): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
adminService.showCreateLoginWizard(uri, connection);
|
||||
});
|
||||
}
|
||||
|
||||
export function showBackup(connection: IConnectionProfile, disasterRecoveryUiService: IDisasterRecoveryUiService): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
disasterRecoveryUiService.showBackup(connection);
|
||||
});
|
||||
}
|
||||
|
||||
export function showRestore(connection: IConnectionProfile, restoreDialogService: IRestoreDialogController): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
restoreDialogService.showDialog(connection);
|
||||
});
|
||||
}
|
||||
|
||||
export function openInsight(query: IInsightsConfig, profile: IConnectionProfile, insightDialogService: IInsightsDialogService) {
|
||||
insightDialogService.show(query, profile);
|
||||
}
|
||||
|
||||
/* Helper Methods */
|
||||
function getStartPos(script: string, operation: ScriptOperation, typeName: string): number {
|
||||
let objectTypeName = objectScriptMap.get(typeName);
|
||||
if (objectTypeName && script) {
|
||||
let scriptTypeName = objectTypeName.toLowerCase();
|
||||
switch (operation) {
|
||||
case (ScriptOperation.Create):
|
||||
return script.toLowerCase().indexOf(`create ${scriptTypeName}`);
|
||||
case (ScriptOperation.Delete):
|
||||
return script.toLowerCase().indexOf(`drop ${scriptTypeName}`);
|
||||
default:
|
||||
/* script wasn't found for that object */
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getScriptingParamDetails(connectionService: IConnectionManagementService, ownerUri: string, metadata: data.ObjectMetadata): data.ScriptingParamDetails {
|
||||
let serverInfo: data.ServerInfo = getServerInfo(connectionService, ownerUri);
|
||||
let paramDetails: data.ScriptingParamDetails = {
|
||||
filePath: getFilePath(metadata),
|
||||
scriptCompatibilityOption: scriptCompatibilityOptionMap[serverInfo.serverMajorVersion],
|
||||
targetDatabaseEngineEdition: targetDatabaseEngineEditionMap[serverInfo.engineEditionId],
|
||||
targetDatabaseEngineType: serverInfo.isCloud ? 'SqlAzure' : 'SingleInstance'
|
||||
}
|
||||
return paramDetails;
|
||||
}
|
||||
|
||||
function getFilePath(metadata: data.ObjectMetadata): string {
|
||||
let schemaName: string = metadata.schema;
|
||||
let objectName: string = metadata.name;
|
||||
let timestamp = Date.now().toString();
|
||||
if (schemaName !== null) {
|
||||
return path.join(os.tmpdir(), `${schemaName}.${objectName}_${timestamp}.txt`);
|
||||
} else {
|
||||
return path.join(os.tmpdir(), `${objectName}_${timestamp}.txt`);
|
||||
}
|
||||
}
|
||||
|
||||
function getServerInfo(connectionService: IConnectionManagementService, ownerUri: string): data.ServerInfo {
|
||||
let connection: ConnectionManagementInfo = connectionService.getConnectionInfo(ownerUri);
|
||||
return connection.serverInfo;
|
||||
}
|
||||
Reference in New Issue
Block a user