2020-05-25 21:30:55 -05:00
|
|
|
|
import * as ko from "knockout";
|
|
|
|
|
import * as ViewModels from "../../Contracts/ViewModels";
|
|
|
|
|
import * as Constants from "../../Common/Constants";
|
|
|
|
|
import * as DataModels from "../../Contracts/DataModels";
|
|
|
|
|
import { Action, ActionModifiers } from "../../Shared/Telemetry/TelemetryConstants";
|
|
|
|
|
|
|
|
|
|
import StoredProcedureTab from "../Tabs/StoredProcedureTab";
|
2020-09-08 12:44:46 -05:00
|
|
|
|
import * as TelemetryProcessor from "../../Shared/Telemetry/TelemetryProcessor";
|
2020-07-20 12:59:40 -05:00
|
|
|
|
import Explorer from "../Explorer";
|
2020-07-27 12:58:27 -05:00
|
|
|
|
import { deleteStoredProcedure, executeStoredProcedure } from "../../Common/DocumentClientUtilityBase";
|
2020-07-27 16:05:25 -05:00
|
|
|
|
import TabsBase from "../Tabs/TabsBase";
|
2020-05-25 21:30:55 -05:00
|
|
|
|
|
|
|
|
|
const sampleStoredProcedureBody: string = `// SAMPLE STORED PROCEDURE
|
|
|
|
|
function sample(prefix) {
|
|
|
|
|
var collection = getContext().getCollection();
|
|
|
|
|
|
|
|
|
|
// Query documents and take 1st item.
|
|
|
|
|
var isAccepted = collection.queryDocuments(
|
|
|
|
|
collection.getSelfLink(),
|
|
|
|
|
'SELECT * FROM root r',
|
|
|
|
|
function (err, feed, options) {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
|
|
// Check the feed and if empty, set the body to 'no docs found',
|
|
|
|
|
// else take 1st element from feed
|
|
|
|
|
if (!feed || !feed.length) {
|
|
|
|
|
var response = getContext().getResponse();
|
|
|
|
|
response.setBody('no docs found');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var response = getContext().getResponse();
|
|
|
|
|
var body = { prefix: prefix, feed: feed[0] };
|
|
|
|
|
response.setBody(JSON.stringify(body));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!isAccepted) throw new Error('The query was not accepted by the server.');
|
|
|
|
|
}`;
|
|
|
|
|
|
2020-07-21 13:50:51 -05:00
|
|
|
|
export default class StoredProcedure {
|
2020-05-25 21:30:55 -05:00
|
|
|
|
public nodeKind: string;
|
2020-07-20 12:59:40 -05:00
|
|
|
|
public container: Explorer;
|
2020-05-25 21:30:55 -05:00
|
|
|
|
public collection: ViewModels.Collection;
|
|
|
|
|
public self: string;
|
|
|
|
|
public rid: string;
|
|
|
|
|
public id: ko.Observable<string>;
|
|
|
|
|
public body: ko.Observable<string>;
|
|
|
|
|
public isExecuteEnabled: boolean;
|
|
|
|
|
|
2020-07-20 12:59:40 -05:00
|
|
|
|
constructor(container: Explorer, collection: ViewModels.Collection, data: DataModels.StoredProcedure) {
|
2020-05-25 21:30:55 -05:00
|
|
|
|
this.nodeKind = "StoredProcedure";
|
|
|
|
|
this.container = container;
|
|
|
|
|
this.collection = collection;
|
|
|
|
|
this.self = data._self;
|
|
|
|
|
this.rid = data._rid;
|
|
|
|
|
this.id = ko.observable(data.id);
|
|
|
|
|
this.body = ko.observable(data.body);
|
|
|
|
|
this.isExecuteEnabled = this.container.isFeatureEnabled(Constants.Features.executeSproc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static create(source: ViewModels.Collection, event: MouseEvent) {
|
2020-07-09 13:53:37 -07:00
|
|
|
|
const id = source.container.tabsManager.getTabs(ViewModels.CollectionTabKind.StoredProcedures).length + 1;
|
2020-05-25 21:30:55 -05:00
|
|
|
|
const storedProcedure = <DataModels.StoredProcedure>{
|
|
|
|
|
id: "",
|
|
|
|
|
body: sampleStoredProcedureBody
|
|
|
|
|
};
|
2020-07-09 13:53:37 -07:00
|
|
|
|
|
|
|
|
|
const storedProcedureTab: StoredProcedureTab = new StoredProcedureTab({
|
2020-05-25 21:30:55 -05:00
|
|
|
|
resource: storedProcedure,
|
|
|
|
|
isNew: true,
|
|
|
|
|
tabKind: ViewModels.CollectionTabKind.StoredProcedures,
|
|
|
|
|
title: `New Stored Procedure ${id}`,
|
|
|
|
|
tabPath: `${source.databaseId}>${source.id()}>New Stored Procedure ${id}`,
|
|
|
|
|
collection: source,
|
|
|
|
|
node: source,
|
|
|
|
|
hashLocation: `${Constants.HashRoutePrefixes.collectionsWithIds(source.databaseId, source.id())}/sproc`,
|
|
|
|
|
selfLink: "",
|
|
|
|
|
isActive: ko.observable(false),
|
2020-07-09 13:53:37 -07:00
|
|
|
|
onUpdateTabsButtons: source.container.onUpdateTabsButtons
|
2020-05-25 21:30:55 -05:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-09 13:53:37 -07:00
|
|
|
|
source.container.tabsManager.activateNewTab(storedProcedureTab);
|
2020-05-25 21:30:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public select() {
|
|
|
|
|
this.container.selectedNode(this);
|
|
|
|
|
TelemetryProcessor.trace(Action.SelectItem, ActionModifiers.Mark, {
|
|
|
|
|
description: "Stored procedure node",
|
|
|
|
|
databaseAccountName: this.container.databaseAccount().name,
|
|
|
|
|
defaultExperience: this.container.defaultExperience(),
|
|
|
|
|
dataExplorerArea: Constants.Areas.ResourceTree
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public open = () => {
|
|
|
|
|
this.select();
|
|
|
|
|
|
2020-07-09 13:53:37 -07:00
|
|
|
|
const storedProcedureTabs: StoredProcedureTab[] = this.container.tabsManager.getTabs(
|
|
|
|
|
ViewModels.CollectionTabKind.StoredProcedures,
|
2020-07-27 16:05:25 -05:00
|
|
|
|
(tab: TabsBase) => tab.node && tab.node.rid === this.rid
|
2020-07-09 13:53:37 -07:00
|
|
|
|
) as StoredProcedureTab[];
|
|
|
|
|
let storedProcedureTab: StoredProcedureTab = storedProcedureTabs && storedProcedureTabs[0];
|
|
|
|
|
|
|
|
|
|
if (storedProcedureTab) {
|
|
|
|
|
this.container.tabsManager.activateTab(storedProcedureTab);
|
|
|
|
|
} else {
|
2020-05-25 21:30:55 -05:00
|
|
|
|
const storedProcedureData = <DataModels.StoredProcedure>{
|
|
|
|
|
_rid: this.rid,
|
|
|
|
|
_self: this.self,
|
|
|
|
|
id: this.id(),
|
|
|
|
|
body: this.body()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
storedProcedureTab = new StoredProcedureTab({
|
|
|
|
|
resource: storedProcedureData,
|
|
|
|
|
isNew: false,
|
|
|
|
|
tabKind: ViewModels.CollectionTabKind.StoredProcedures,
|
|
|
|
|
title: storedProcedureData.id,
|
|
|
|
|
tabPath: `${this.collection.databaseId}>${this.collection.id()}>${storedProcedureData.id}`,
|
|
|
|
|
collection: this.collection,
|
|
|
|
|
node: this,
|
|
|
|
|
hashLocation: `${Constants.HashRoutePrefixes.collectionsWithIds(
|
|
|
|
|
this.collection.databaseId,
|
|
|
|
|
this.collection.id()
|
|
|
|
|
)}/sprocs/${this.id()}`,
|
|
|
|
|
selfLink: this.self,
|
|
|
|
|
isActive: ko.observable(false),
|
2020-07-09 13:53:37 -07:00
|
|
|
|
onUpdateTabsButtons: this.container.onUpdateTabsButtons
|
2020-05-25 21:30:55 -05:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-09 13:53:37 -07:00
|
|
|
|
this.container.tabsManager.activateNewTab(storedProcedureTab);
|
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
|
};
|
|
|
|
|
|
2020-06-15 12:16:52 +02:00
|
|
|
|
public delete() {
|
2020-05-25 21:30:55 -05:00
|
|
|
|
if (!window.confirm("Are you sure you want to delete the stored procedure?")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const storedProcedureData = <DataModels.StoredProcedure>{
|
|
|
|
|
_rid: this.rid,
|
|
|
|
|
_self: this.self,
|
|
|
|
|
id: this.id(),
|
|
|
|
|
body: this.body()
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-27 12:58:27 -05:00
|
|
|
|
deleteStoredProcedure(this.collection, storedProcedureData).then(
|
2020-05-25 21:30:55 -05:00
|
|
|
|
() => {
|
2020-07-27 16:05:25 -05:00
|
|
|
|
this.container.tabsManager.removeTabByComparator((tab: TabsBase) => tab.node && tab.node.rid === this.rid);
|
2020-05-25 21:30:55 -05:00
|
|
|
|
this.collection.children.remove(this);
|
|
|
|
|
},
|
|
|
|
|
reason => {}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public execute(params: string[], partitionKeyValue?: string): void {
|
2020-07-27 16:05:25 -05:00
|
|
|
|
const sprocTabs = this.container.tabsManager.getTabs(
|
2020-07-09 13:53:37 -07:00
|
|
|
|
ViewModels.CollectionTabKind.StoredProcedures,
|
2020-07-27 16:05:25 -05:00
|
|
|
|
(tab: TabsBase) => tab.node && tab.node.rid === this.rid
|
|
|
|
|
) as StoredProcedureTab[];
|
|
|
|
|
const sprocTab = sprocTabs && sprocTabs.length > 0 && sprocTabs[0];
|
2020-05-25 21:30:55 -05:00
|
|
|
|
sprocTab.isExecuting(true);
|
|
|
|
|
this.container &&
|
2020-07-27 12:58:27 -05:00
|
|
|
|
executeStoredProcedure(this.collection, this, partitionKeyValue, params)
|
2020-05-25 21:30:55 -05:00
|
|
|
|
.then(
|
|
|
|
|
(result: any) => {
|
|
|
|
|
sprocTab.onExecuteSprocsResult(result, result.scriptLogs);
|
|
|
|
|
},
|
|
|
|
|
(error: any) => {
|
|
|
|
|
sprocTab.onExecuteSprocsError(JSON.stringify(error));
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.finally(() => {
|
|
|
|
|
sprocTab.isExecuting(false);
|
|
|
|
|
this.onFocusAfterExecute();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onFocusAfterExecute(): void {
|
|
|
|
|
const focusElement = document.getElementById("execute-storedproc-toggles");
|
|
|
|
|
focusElement && focusElement.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|