All files / commands/sparql delete-graph.ts

100% Statements 21/21
100% Branches 8/8
100% Functions 1/1
100% Lines 21/21

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                  1x       5x           5x 1x     4x 4x 4x   4x 1x 1x     3x 1x   2x   2x 1x 1x       1x           1x     1x   1x 1x      
import * as vscode from 'vscode';
import { render } from 'triplate';
import { container } from 'tsyringe';
import { Store } from '@faubulous/mentor-rdf';
import { ServiceToken } from '@src/services/tokens';
import { IDocumentConnectionService, ITripleStoreConfigService } from '@src/languages/sparql/services';
import { SparqlResultsController } from '@src/views/webviews';
import { WorkspaceUri } from '@src/providers/workspace-uri';
 
export const deleteGraph = {
	id: 'mentor.command.deleteGraph',
	handler: async (documentIri: string, graphIri: vscode.Uri | string) => {
		// Ask for confirmation before deleting
		const answer = await vscode.window.showWarningMessage(
			`Are you sure you want to delete the graph "${WorkspaceUri.toCanonicalString(graphIri)}"? This action cannot be undone.`,
			{ modal: true },
			'Delete'
		);
 
		if (answer !== 'Delete') {
			return;
		}
 
		const storeConfigService = container.resolve<ITripleStoreConfigService>(ServiceToken.StoreConfigService);
		const documentConnectionService = container.resolve<IDocumentConnectionService>(ServiceToken.DocumentConnectionService);
		const connection = documentConnectionService.getConnectionForDocument(documentIri);
 
		if (!connection) {
			vscode.window.showErrorMessage(`Unable to retrieve SPARQL connection for document: ${documentIri}`);
			return;
		}
 
		if (connection.id === 'workspace') {
			container.resolve<Store>(ServiceToken.Store).deleteGraphs([WorkspaceUri.toCanonicalString(graphIri)]);
		} else {
			const query = storeConfigService.getQueryTemplate(connection, 'dropGraph');
 
			if (!query) {
				vscode.window.showErrorMessage('Could not resolve a "drop graph" query for this connection.');
				return;
			}
 
			// Create an untitled SPARQL document with the drop graph query
			const document = await vscode.workspace.openTextDocument({
				content: render(query, { graphIri: WorkspaceUri.toCanonicalString(graphIri) }),
				language: 'sparql'
			});
 
			// Set the connection for this document
			await documentConnectionService.setQuerySourceForDocument(document.uri, connection.id);
 
			// Show the document and execute the query
			await vscode.window.showTextDocument(document);
			
			const controller = container.resolve<SparqlResultsController>(ServiceToken.SparqlResultsController);
			await controller.executeQueryFromTextDocument(document);
		}
	}
};