All files / commands/sparql execute-sparql-query-from-string.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                        1x     3x   3x 1x 1x     2x 1x 1x   1x 1x        
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { SparqlResultsController } from '@src/views/webviews';
import { NotebookController } from '@src/services/notebook/notebook-controller';
import { resolveQueryContext, isNotebookCell } from '@src/utilities/query-context';
 
/**
 * Executes a SPARQL query given as a string against the connection of an optional
 * context document or notebook cell.
 *
 * The context determines where results are shown: when it is a notebook cell, the
 * results are rendered in the cell output (like an ordinary query cell); otherwise
 * they are shown in the SPARQL results panel, inheriting the context document's
 * connection. The query string is never opened as a document.
 * 
 * @param query The SPARQL query string to execute.
 * @param contextUri An optional URI of a document or notebook cell to use as the context for the query.
 * @param isGenerated An optional flag indicating whether the query is generated from a template (default: false).
 */
export const executeSparqlQueryFromString = {
	id: 'mentor.command.executeSparqlQueryFromString',
	handler: async (query: string, contextUri?: vscode.Uri | string, isGenerated?: boolean): Promise<void> => {
		const context = resolveQueryContext(contextUri);
 
		if (!context) {
			vscode.window.showWarningMessage('No document context found to execute the query.');
			return;
		}
 
		if (isNotebookCell(context)) {
			const controller = container.resolve<NotebookController>(ServiceToken.NotebookController);
			await controller.executeQueryInCell(context, query);
		} else {
			const controller = container.resolve<SparqlResultsController>(ServiceToken.SparqlResultsController);
			await controller.executeQuery(context, query, { isGenerated });
		}
	}
};