All files / languages/sparql/services sparql-query-service.ts

100% Statements 182/182
96.51% Branches 83/86
100% Functions 36/36
100% Lines 178/178

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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519                                  1x         1x                   59x   59x   59x         59x   59x         59x   59x         59x     59x 59x 59x 59x   59x 27x     59x 1x     59x               3x 2x   2x                     2x 2x 2x   2x                                   1x 1x 1x 1x   1x                         3x 1x   1x           2x         59x   59x 32x   12x         13x 7x     13x                 2x               2x   2x                 7x 1x 1x 1x   6x                 8x 5x   5x   5x   5x   5x   5x   3x                       6x 6x   6x 1x     5x   5x   5x 5x   5x   4x 1x 3x 1x 2x 1x           1x     2x                 6x   6x 6x   6x                     5x 5x 5x   4x 1x 3x 1x   1x 1x     1x   1x 2x 1x   1x 1x     1x     1x   1x                                         10x         10x 9x 9x 9x     10x   8x 2x       1x 6x 2x   2x 4x 2x   2x     2x       21x 2x 2x 2x   2x 1x 1x   1x       19x 3x 3x   3x 1x 1x   1x       16x 3x   3x 1x     2x 1x 1x   1x       13x 2x 2x   2x 1x 1x 1x   1x       11x       10x   10x 9x   2x   2x   2x   1x   1x   1x           11x 6x 5x 2x 1x     2x 1x   1x     3x 1x     3x 1x                   5x   5x               6x   6x             11x   11x 5x     11x   11x                 16x             2x   2x    
import * as vscode from 'vscode';
import { SparqlLexer, RdfToken } from '@faubulous/mentor-rdf-parsers';
import { QueryEngine } from "@comunica/query-sparql";
import { AsyncIterator } from 'asynciterator';
import { Bindings, Quad } from "@rdfjs/types";
import { AuthCredential, EntraClientAuthCredential } from '@src/services/core/credential';
import { ICredentialStorageService } from '@src/services/core';
import { EntraClientCredentialService } from '@src/services/core/entra-client-credential-service';
import { ISparqlConnectionService, ISparqlResultSerializer } from '@src/languages/sparql/services';
import { WorkspaceUri } from "@src/providers/workspace-uri";
import { CancellationError, withCancellation } from '@src/utilities/vscode/cancellation';
import { SparqlQueryExecutionState, SparqlQueryType } from "./sparql-query-state";
import { SparqlConnection } from './sparql-connection';
 
/**
 * The key for storing query history in local storage.
 */
const HISTORY_STORAGE_KEY = 'mentor.sparql.queryHistory';
 
/**
 * The maximum number of entries to keep in the query history.
 */
const HISTORY_MAX_ENTRIES = 10;
 
/**
 * A service for executing SPARQL queries against an RDF endpoint. The service
 * keeps a log of the executed queries in temporal order in memory, and upon
 * disposal, it saves the query history to a workspace-scoped local storage,
 * excluding unsaved documents. This query history is then restored when the 
 * service is instantiated.
 */
export class SparqlQueryService {
	private readonly _history: SparqlQueryExecutionState[] = [];
 
	private readonly _cancellationTokens = new Map<string, vscode.CancellationTokenSource>();
 
	private readonly _onDidHistoryChange = new vscode.EventEmitter<void>();
 
	/**
	 * Event that is triggered when the query history changes.
	 */
	onDidHistoryChange: vscode.Event<void> = this._onDidHistoryChange.event;
 
	private readonly _onDidQueryExecutionStart = new vscode.EventEmitter<SparqlQueryExecutionState>();
 
	/**
	 * Event that is triggered before a SPARQL query is about to be executed.
	 */
	onDidQueryExecutionStart: vscode.Event<SparqlQueryExecutionState> = this._onDidQueryExecutionStart.event;
 
	private readonly _onDidQueryExecutionEnd = new vscode.EventEmitter<SparqlQueryExecutionState>();
 
	/**
	 * Event that is triggered when a SPARQL query execution has ended with any result.
	 */
	onDidQueryExecutionEnd: vscode.Event<SparqlQueryExecutionState> = this._onDidQueryExecutionEnd.event;
 
	constructor(
		private readonly _extensionContext: vscode.ExtensionContext,
		private readonly _credentialStorage: ICredentialStorageService,
		private readonly _connectionService: ISparqlConnectionService,
		private readonly _resultSerializer: ISparqlResultSerializer
	) {
		for (const entry of this._loadQueryHistory()) {
			this._history.push(entry);
		}
 
		const disposables = [
			vscode.workspace.onDidCloseTextDocument((e) => this._onTextDocumentClosed(e))
		];
 
		this._extensionContext.subscriptions.push(...disposables);
	}
 
	/**
	 * Handles the closing of a text document and removes unsaved queries from the history.
	 * @param document A text document.
	 */
	private _onTextDocumentClosed(document: vscode.TextDocument) {
		if (document.uri.scheme === 'untitled') {
			const i = this._history.findIndex(q => q.documentIri === document.uri.toString());
 
			this.removeQueryStateAt(i);
		}
	}
 
	/**
	 * Creates a new SPARQL query state from a query string.
	 * @param querySource The source document or notebook cell where the query is stored.
	 * @param query The SPARQL query string.
	 * @returns A new SparqlQueryExecutionState instance.
	 */
	createQuery(querySource: vscode.TextDocument | vscode.NotebookCell, query: string): SparqlQueryExecutionState {
		const source = this._getDocumentFromQuerySource(querySource);
		const workspaceIri = WorkspaceUri.toWorkspaceUri(source.document.uri);
		const queryType = this._getQueryType(query);
 
		return {
			id: crypto.randomUUID(),
			documentIri: source.document.uri.toString(),
			workspaceIri: workspaceIri?.toString(),
			notebookIri: source.notebookIri?.toString(),
			cellIndex: source.cellIndex,
			query,
			queryType,
			startTime: Date.now()
		};
	}
 
	/**
	 * Creates a new SPARQL query state from a document or notebook cell.
	 * @param querySource The source document or notebook cell where the query is stored.
	 * @returns A new SparqlQueryContext instance.
	 */
	createQueryFromDocument(querySource: vscode.TextDocument | vscode.NotebookCell): SparqlQueryExecutionState {
		const source = this._getDocumentFromQuerySource(querySource);
		const workspaceIri = WorkspaceUri.toWorkspaceUri(source.document.uri);
		const query = source.document.getText();
		const queryType = this._getQueryType(query);
 
		return {
			id: crypto.randomUUID(),
			documentIri: source.document.uri.toString(),
			workspaceIri: workspaceIri?.toString(),
			notebookIri: source.notebookIri?.toString(),
			cellIndex: source.cellIndex,
			query,
			queryType,
			startTime: Date.now()
		};
	}
 
	private _getDocumentFromQuerySource(querySource: vscode.TextDocument | vscode.NotebookCell) {
		if ('notebook' in querySource && querySource.notebook) {
			const cell = querySource as vscode.NotebookCell;
 
			return {
				document: cell.document,
				notebookIri: cell.notebook.uri,
				cellIndex: cell.index
			};
		} else {
			return { document: querySource as vscode.TextDocument };
		}
	}
 
	private _loadQueryHistory(limit: number = 10): SparqlQueryExecutionState[] {
		const history = this._extensionContext.workspaceState.get<SparqlQueryExecutionState[]>(HISTORY_STORAGE_KEY, []);
 
		return history
			.filter(q => q)
			.slice(0, limit)
			.sort((a, b) => b.startTime - a.startTime);
	}
 
	private async _persistQueryHistory(): Promise<void> {
		// Filter the query history to exclude execution states that would not be valid after a restart.
		const filteredHistory = this._history
			.filter(q => q && !q.documentIri.startsWith('untitled'))
			.slice(0, HISTORY_MAX_ENTRIES);
 
		await this._extensionContext.workspaceState.update(HISTORY_STORAGE_KEY, filteredHistory);
	}
 
	/**
	 * Get the SPARQL query state for a specific document IRI.
	 * @param documentIri The IRI of the document to retrieve the query state for.
	 * @returns The SparqlQueryState for the specified document, or `undefined` if not found.
	 */
	getQueryStateForDocument(documentIri: string): SparqlQueryExecutionState | undefined {
		return this._history.find(q => q.documentIri === documentIri);
	}
 
	/**
	 * Removes a SPARQL query state from the history and triggers the history change event.
	 * @param state The SparqlQueryState to remove.
	 */
	removeQueryState(state: SparqlQueryExecutionState) {
		const n = this._history.findIndex(q => q === state);
 
		this.removeQueryStateAt(n);
	}
 
	/**
	 * Cancels a running SPARQL query execution.
	 * @param queryStateID Id of the query execution state.
	 * @returns `true` if the query was successfully cancelled, `false` otherwise.
	 */
	cancelQuery(queryStateID: string): boolean {
		if (this._cancellationTokens.has(queryStateID)) {
			this._cancellationTokens.get(queryStateID)?.cancel();
			this._cancellationTokens.delete(queryStateID);
			return true;
		} else {
			return false;
		}
	}
 
	/**
	 * Removes the n-th item from the query history and triggers the history change event.
	 * @param index The index of the item to remove from the query history.
	 */
	removeQueryStateAt(index: number): boolean {
		if (index >= 0 && index < this._history.length) {
			const queryState = this._history[index];
 
			this.cancelQuery(queryState.id);
 
			this._history.splice(index, 1);
 
			this._onDidHistoryChange.fire();
 
			this._persistQueryHistory();
 
			return true;
		} else {
			return false;
		}
	}
 
	/**
	 * Executes a SPARQL query against the RDF store and returns the results.
	 * @param query The SPARQL query to execute.
	 * @param documentIri The IRI of the document where the query is run.
	 * @param tokenSource A cancellation token source to cancel the query execution.
	 * @returns A promise that resolves to the results of the query.
	 */
	async executeQuery(context: SparqlQueryExecutionState, tokenSource: vscode.CancellationTokenSource = new vscode.CancellationTokenSource()): Promise<SparqlQueryExecutionState> {
		try {
			const query = this._getQueryText(context);
 
			if (!query) {
				throw new Error('Unable to retrieve query from the document: ' + context.documentIri);
			}
 
			this._cancellationTokens.set(context.id, tokenSource);
 
			this._logQueryExecutionStart(context);
 
			const documentIri = vscode.Uri.parse(context.documentIri);
			const source = await this._connectionService.getQuerySourceForDocument(documentIri);
 
			const result = await this._executeQueryOnSource(query, source, tokenSource.token);
 
			if (result.type === 'bindings') {
				context.result = await this._resultSerializer.serializeBindings(context, result.bindings, tokenSource.token);
			} else if (result.type === 'boolean') {
				context.result = { type: 'boolean', value: result.value };
			} else if (result.type === 'quads') {
				context.result = {
					type: 'quads',
					mimeType: 'text/turtle',
					document: await this._resultSerializer.serializeQuads(context, result.quads, tokenSource.token)
				};
			} else {
				context.result = undefined;
			}
		} catch (error: any) {
			context.error = {
				type: error.name || 'QueryError',
				message: error.message || 'Unknown error occurred while executing the query.',
				stack: error.stack || '',
				statusCode: error.statusCode || 500,
				cancelled: error instanceof CancellationError
			}
		}
 
		context.endTime = Date.now();
 
		this._logQueryExecutionEnd(context);
		this._persistQueryHistory();
 
		return context;
	}
 
	/**
	 * Executes a SPARQL query directly against a connection without requiring a document.
	 * This method does not log the query in history and is intended for internal/programmatic use.
	 * @param query The SPARQL query string to execute.
	 * @param connection The SPARQL connection to execute against.
	 * @returns The query result based on the query type.
	 */
	async executeQueryOnConnection(query: string, connection: SparqlConnection): Promise<{ type: 'boolean'; value: boolean } | { type: 'quads'; data: string } | { type: 'bindings'; bindings: any[] } | null> {
		try {
			const source = await this._connectionService.getQuerySourceForConnection(connection);
			const result = await this._executeQueryOnSource(query, source);
 
			if (result.type === 'boolean') {
				return { type: 'boolean', value: result.value };
			} else if (result.type === 'quads') {
				const quads: Quad[] = [];
 
				for await (const quad of result.quads) {
					quads.push(quad);
				}
 
				const data = await this._resultSerializer.serializeQuadsToString(quads);
 
				return { type: 'quads', data };
			} else if (result.type === 'bindings') {
				const bindings: Bindings[] = [];
 
				for await (const binding of result.bindings) {
					bindings.push(binding);
				}
 
				return { type: 'bindings', bindings: bindings };
			}
 
			return null;
		} catch (error: any) {
			throw new Error(`Query execution failed: ${error.message}`);
		}
	}
 
	/**
	 * Executes a SPARQL query against a Comunica source and returns the raw result.
	 * @param query The SPARQL query string to execute.
	 * @param source The Comunica source to execute against.
	 * @param token Optional cancellation token.
	 * @returns The raw query result with type information.
	 */
	private async _executeQueryOnSource(
		query: string,
		source: any,
		token?: vscode.CancellationToken
	): Promise<
		| { type: 'boolean'; value: boolean }
		| { type: 'quads'; quads: AsyncIterator<Quad> }
		| { type: 'bindings'; bindings: AsyncIterator<Bindings> }
		| { type: 'none' }
	> {
		const options: any = {
			sources: [source],
			unionDefaultGraph: true
		};
 
		if (source.type === 'sparql') {
			const connection = source.connection;
			const credential = await this._credentialStorage.getCredential(connection.id);
			options.fetch = this._getFetchHandler(credential);
		}
 
		const preparedQuery = await new QueryEngine().query(query, options);
 
		if (preparedQuery.resultType === 'boolean') {
			const value = token
				? await withCancellation(preparedQuery.execute(), token)
				: await preparedQuery.execute();
 
			return { type: 'boolean', value };
		} else if (preparedQuery.resultType === 'quads') {
			const quads = await preparedQuery.execute();
 
			return { type: 'quads', quads };
		} else if (preparedQuery.resultType === 'bindings') {
			const bindings = await preparedQuery.execute();
 
			return { type: 'bindings', bindings };
		}
 
		return { type: 'none' };
	}
 
	_getFetchHandler(credential?: AuthCredential) {
		if (credential?.type === 'basic') {
			const username = credential.username;
			const password = credential.password;
			const encoded = btoa(`${username}:${password}`);
 
			return (input: RequestInfo | URL, init?: RequestInit) => {
				const headers = new Headers(init?.headers || {});
				headers.set("Authorization", `Basic ${encoded}`);
 
				return fetch(input, { ...init, headers });
			};
		}
 
		if (credential?.type === 'bearer') {
			const prefix = credential.prefix || 'Bearer';
			const token = credential.token;
 
			return (input: RequestInfo | URL, init?: RequestInit) => {
				const headers = new Headers(init?.headers || {});
				headers.set("Authorization", `${prefix} ${token}`);
 
				return fetch(input, { ...init, headers });
			};
		}
 
		if (credential?.type === 'microsoft') {
			const accessToken = credential.accessToken;
 
			if (!accessToken) {
				return undefined;
			}
 
			return (input: RequestInfo | URL, init?: RequestInit) => {
				const headers = new Headers(init?.headers || {});
				headers.set("Authorization", `Bearer ${accessToken}`);
 
				return fetch(input, { ...init, headers });
			};
		}
 
		if (credential?.type === 'entra-client-credentials') {
			const entraCredential = credential as EntraClientAuthCredential;
			const tokenService = new EntraClientCredentialService();
 
			return async (input: RequestInfo | URL, init?: RequestInit) => {
				const accessToken = await tokenService.acquireToken(entraCredential);
				const headers = new Headers(init?.headers || {});
				headers.set("Authorization", `Bearer ${accessToken}`);
 
				return fetch(input, { ...init, headers });
			};
		}
 
		return undefined;
	}
 
	_getQueryType(query: string): SparqlQueryType | undefined {
		const lexingResult = new SparqlLexer().tokenize(query);
 
		for (const token of lexingResult.tokens) {
			switch (token.tokenType.name) {
				case RdfToken.ASK.name:
					return 'boolean';
				case RdfToken.SELECT.name:
					return 'bindings';
				case RdfToken.CONSTRUCT.name:
					return 'quads';
				case RdfToken.DESCRIBE.name:
					return 'quads';
				case RdfToken.FROM.name:
					return undefined;
				case RdfToken.WHERE.name:
					return undefined;
			}
		}
	}
 
	private _getQueryText(context: SparqlQueryExecutionState): string | undefined {
		if (context.query) {
			return context.query;
		} else if (context.notebookIri) {
			const notebook = vscode.workspace.notebookDocuments.find(
				n => n.uri.toString() === context.notebookIri
			);
 
			if (notebook) {
				const cell = notebook.cellAt(context.cellIndex || 0);
 
				return cell.document.getText();
			}
		} else {
			const document = vscode.workspace.textDocuments.find(
				d => d.uri.toString() === context.documentIri
			);
 
			if (document) {
				return document.getText();
			}
		}
	}
 
	/**
	 * Update the SPARQL history and fire the appropriate events when a query is executed.
	 * @param context The context of the SPARQL query execution.
	 */
	private async _logQueryExecutionStart(context: SparqlQueryExecutionState) {
		await this._logQueryExecution(context);
 
		this._onDidQueryExecutionStart.fire(context);
	}
 
	/**
	 * Update the SPARQL history and fire the appropriate events when a query finished executing.
	 * @param context The context of the SPARQL query execution.
	 */
	private async _logQueryExecutionEnd(context: SparqlQueryExecutionState) {
		await this._logQueryExecution(context);
 
		this._onDidQueryExecutionEnd.fire(context);
	}
 
	/**
	 * Tracks query execution in history and persists to storage.
	 */
	private async _logQueryExecution(context: SparqlQueryExecutionState) {
		const n = this._history.findIndex(q => q.documentIri === context.documentIri);
 
		if (n >= 0) {
			this._history.splice(n, 1);
		}
 
		this._history.unshift(context);
 
		this._onDidHistoryChange.fire();
	}
 
	/**
	 * Gets recent queries across all documents, ordered by execution time in descending order.
	 * @param limit The maximum number of recent queries to return.
	 * @returns A promise that resolves to an array of recent query entries.
	 */
	getQueryHistory(): SparqlQueryExecutionState[] {
		return this._history;
	}
 
	/**
	 * Clears the persisted query history.
	 */
	clearQueryHistory(): void {
		this._history.length = 0;
 
		this._persistQueryHistory();
	}
}