|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 |
| -import { AzExtTreeItem, IActionContext, ITreeItemPickerContext, registerCommandWithTreeNodeUnwrapping } from "@microsoft/vscode-azext-utils"; |
7 |
| -import { commands } from "vscode"; |
| 6 | +import { AzExtTreeItem, IActionContext, ITreeItemPickerContext, registerCommand, registerCommandWithTreeNodeUnwrapping } from "@microsoft/vscode-azext-utils"; |
| 7 | +import { ViewColumn, commands, languages } from "vscode"; |
| 8 | +import { KeyValueStore } from "../KeyValueStore"; |
8 | 9 | import { doubleClickDebounceDelay, sqlFilter } from "../constants";
|
9 | 10 | import { ext } from "../extensionVariables";
|
| 11 | +import * as vscodeUtil from "../utils/vscodeUtils"; |
| 12 | +import { NoSqlCodeLensProvider, NoSqlQueryConnection, noSqlQueryConnectionKey } from "./NoSqlCodeLensProvider"; |
| 13 | +import { getCosmosClient } from "./getCosmosClient"; |
10 | 14 | import { DocDBAccountTreeItem } from "./tree/DocDBAccountTreeItem";
|
11 | 15 | import { DocDBCollectionTreeItem } from "./tree/DocDBCollectionTreeItem";
|
12 | 16 | import { DocDBDatabaseTreeItem } from "./tree/DocDBDatabaseTreeItem";
|
13 |
| -import { DocDBDocumentsTreeItem } from "./tree/DocDBDocumentsTreeItem"; |
14 | 17 | import { DocDBDocumentTreeItem } from "./tree/DocDBDocumentTreeItem";
|
15 |
| -import { DocDBStoredProceduresTreeItem } from "./tree/DocDBStoredProceduresTreeItem"; |
| 18 | +import { DocDBDocumentsTreeItem } from "./tree/DocDBDocumentsTreeItem"; |
16 | 19 | import { DocDBStoredProcedureTreeItem } from "./tree/DocDBStoredProcedureTreeItem";
|
| 20 | +import { DocDBStoredProceduresTreeItem } from "./tree/DocDBStoredProceduresTreeItem"; |
| 21 | + |
| 22 | +const nosqlLanguageId = "nosql"; |
17 | 23 |
|
18 | 24 | export function registerDocDBCommands(): void {
|
| 25 | + ext.noSqlCodeLensProvider = new NoSqlCodeLensProvider(); |
| 26 | + ext.context.subscriptions.push(languages.registerCodeLensProvider(nosqlLanguageId, ext.noSqlCodeLensProvider)); |
| 27 | + |
| 28 | + registerCommand("cosmosDB.connectNoSqlContainer", connectNoSqlContainer); |
| 29 | + registerCommand("cosmosDB.executeNoSqlQuery", executeNoSqlQuery); |
| 30 | + registerCommand("cosmosDB.getNoSqlQueryPlan", getNoSqlQueryPlan); |
19 | 31 | registerCommandWithTreeNodeUnwrapping('cosmosDB.createDocDBDatabase', createDocDBDatabase);
|
| 32 | + registerCommandWithTreeNodeUnwrapping('cosmosDB.writeNoSqlQuery', writeNoSqlQuery); |
20 | 33 | registerCommandWithTreeNodeUnwrapping('cosmosDB.createDocDBCollection', createDocDBCollection);
|
21 | 34 | registerCommandWithTreeNodeUnwrapping('cosmosDB.createDocDBDocument', async (context: IActionContext, node?: DocDBDocumentsTreeItem) => {
|
22 | 35 | if (!node) {
|
@@ -61,6 +74,72 @@ export function registerDocDBCommands(): void {
|
61 | 74 | });
|
62 | 75 | }
|
63 | 76 |
|
| 77 | +function setConnectedNoSqlContainer(node: DocDBCollectionTreeItem): void { |
| 78 | + const noSqlQueryConnection: NoSqlQueryConnection = { |
| 79 | + databaseId: node.parent.id, |
| 80 | + containerId: node.id, |
| 81 | + endpoint: node.root.endpoint, |
| 82 | + masterKey: node.root.masterKey, |
| 83 | + isEmulator: !!node.root.isEmulator |
| 84 | + }; |
| 85 | + KeyValueStore.instance.set(noSqlQueryConnectionKey, noSqlQueryConnection); |
| 86 | + ext.noSqlCodeLensProvider.updateCodeLens(); |
| 87 | +} |
| 88 | + |
| 89 | +async function writeNoSqlQuery(_context: IActionContext, node: DocDBCollectionTreeItem): Promise<void> { |
| 90 | + setConnectedNoSqlContainer(node); |
| 91 | + const sampleQuery = `SELECT * FROM ${node.id}`; |
| 92 | + await vscodeUtil.showNewFile(sampleQuery, `query for ${node.label}`, ".nosql"); |
| 93 | +} |
| 94 | + |
| 95 | +async function connectNoSqlContainer(context: IActionContext): Promise<void> { |
| 96 | + const node = await pickDocDBAccount<DocDBCollectionTreeItem>(context, DocDBCollectionTreeItem.contextValue); |
| 97 | + setConnectedNoSqlContainer(node); |
| 98 | +} |
| 99 | + |
| 100 | +async function executeNoSqlQuery(_context: IActionContext, args: { queryText: string, populateQueryMetrics?: boolean }): Promise<void> { |
| 101 | + if (!args) { |
| 102 | + throw new Error("Unable to execute query due to missing args. Please connect to a Cosmos DB collection."); |
| 103 | + } |
| 104 | + const { queryText, populateQueryMetrics } = args; |
| 105 | + const connectedCollection = KeyValueStore.instance.get(noSqlQueryConnectionKey); |
| 106 | + if (!connectedCollection) { |
| 107 | + throw new Error("Unable to execute query due to missing node data. Please connect to a Cosmos DB collection node."); |
| 108 | + } else { |
| 109 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 110 | + const { databaseId, containerId, endpoint, masterKey, isEmulator } = connectedCollection as NoSqlQueryConnection; |
| 111 | + const client = getCosmosClient(endpoint, masterKey, isEmulator); |
| 112 | + const options = { populateQueryMetrics }; |
| 113 | + const response = await client.database(databaseId).container(containerId).items.query(queryText, options).fetchAll(); |
| 114 | + const resultDocumentTitle = `query results for ${containerId}`; |
| 115 | + if (populateQueryMetrics === true) { |
| 116 | + await vscodeUtil.showNewFile(JSON.stringify({ |
| 117 | + result: response.resources, |
| 118 | + queryMetrics: response.queryMetrics |
| 119 | + }, undefined, 2), resultDocumentTitle, ".json", ViewColumn.Beside); |
| 120 | + } else { |
| 121 | + await vscodeUtil.showNewFile(JSON.stringify(response.resources, undefined, 2), resultDocumentTitle, ".json", ViewColumn.Beside); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +async function getNoSqlQueryPlan(_context: IActionContext, args: { queryText: string } | undefined): Promise<void> { |
| 127 | + if (!args) { |
| 128 | + throw new Error("Unable to get query plan due to missing args. Please connect to a Cosmos DB collection node."); |
| 129 | + } |
| 130 | + const queryText = args.queryText; |
| 131 | + const connectedCollection = KeyValueStore.instance.get(noSqlQueryConnectionKey); |
| 132 | + if (!connectedCollection) { |
| 133 | + throw new Error("Unable to get query plan due to missing node data. Please connect to a Cosmos DB collection."); |
| 134 | + } else { |
| 135 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 136 | + const { databaseId, containerId, endpoint, masterKey, isEmulator } = connectedCollection as NoSqlQueryConnection; |
| 137 | + const client = getCosmosClient(endpoint, masterKey, isEmulator); |
| 138 | + const response = await client.database(databaseId).container(containerId).getQueryPlan(queryText); |
| 139 | + await vscodeUtil.showNewFile(JSON.stringify(response.result, undefined, 2), `query results for ${containerId}`, ".json", ViewColumn.Beside); |
| 140 | + } |
| 141 | +} |
| 142 | + |
64 | 143 | export async function createDocDBDatabase(context: IActionContext, node?: DocDBAccountTreeItem): Promise<void> {
|
65 | 144 | if (!node) {
|
66 | 145 | node = await pickDocDBAccount<DocDBAccountTreeItem>(context);
|
|
0 commit comments