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

100% Statements 8/8
91.66% Branches 11/12
100% Functions 1/1
100% Lines 8/8

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                                                                                                                                                                                              2x             15x 3x 3x         12x   15x 3x   9x                                                                                                                
import { PrefixMap } from "@src/utilities";
import { Term } from "@rdfjs/types";
import { getFileName } from "@src/utilities/uri";
 
export type SparqlQueryType = 'bindings' | 'boolean' | 'quads' | 'void';
 
/**
 * The state of a SPARQL query execution.
 */
export interface SparqlQueryExecutionState {
	/**
	 * A unique identifier for the query execution.
	 */
	id: string;
	
	/**
	 * The TextDocument where the SPARQL query is defined. In case of a Notebook, this 
	 * is the IRI of the document that is associated with the cell.
	 * For background queries this is initially `undefined` and set only after the user clicks Edit.
	 */
	documentIri?: string;
 
	/**
	 * A human-readable label for the query, used when no document is associated (background queries).
	 * For example: 'List Graphs'.
	 */
	label?: string;
 
	/**
	 * The ID of the SPARQL connection used to execute the query.
	 * Set for background queries that are not associated with a document.
	 */
	connectionId?: string;
 
	/**
	 * The display name of the SPARQL connection used to execute the query.
	 * Used as the tab title for background queries.
	 */
	connectionName?: string;
 
	/**
	 * When `true`, the query was executed as a background query without an open editor document.
	 * Background queries are excluded from the Recent Queries history list.
	 */
	background?: boolean;
 
	/**
	 * The workspace relative URI of the `documentIri`.
	 */
	workspaceIri?: string;
 
	/**
	 * The IRI of the notebook file where the SPARQL query is run, if applicable.
	 */
	notebookIri?: string;
 
	/**
	 * Id of the notebook cell associated with the SPARQL query, if any.
	 */
	cellIndex?: number;
 
	/**
	 * The SPARQL query text.
	 */
	query?: string;
 
	/**
	 * The SPARQL query type.
	 */
	queryType?: SparqlQueryType;
 
	/**
	 * The time when the query was executed in milliseconds since midnight, January 1, 1970 UTC.
	 */
	startTime: number;
 
	/**
	 * The time when the query finished executing in milliseconds since midnight, January 1, 1970 UTC.
	 */
	endTime?: number;
 
	/**
	 * The error that occurred during query execution, if any.
	 */
	error?: any;
 
	/**
	 * The results of the query execution, if any.
	 */
	result?: BindingsResult | BooleanResult | QuadsResult;
}
 
/**
 * Maximum length for a truncated connection name used as a tab title.
 */
const MAX_CONNECTION_NAME_LENGTH = 20;
 
/**
 * Get the formatted file name of the associated document or notebook cell.
 * For background queries, returns the truncated connection name.
 */
export function getDisplayName(queryState: SparqlQueryExecutionState): string {
	if (queryState.background && queryState.connectionName) {
		const name = queryState.connectionName;
		return name.length > MAX_CONNECTION_NAME_LENGTH
			? name.slice(0, MAX_CONNECTION_NAME_LENGTH - 1) + '…'
			: name;
	}
 
	const fileName = getFileName(queryState.documentIri ?? '');
 
	if (queryState.notebookIri && queryState.cellIndex !== undefined) {
		return `${fileName.split('#')[0]}:Cell-${queryState.cellIndex}`;
	} else {
		return fileName;
	}
}
 
/**
 * Represents the results of a SPARQL query that returns bindings.
 */
export interface BindingsResult {
	/**
	 * The type of the result, which is always 'bindings' for SELECT queries.
	 */
	type: 'bindings';
 
	/**
	 * The column headers of the result table.
	 */
	columns: string[];
 
	/**
	 * The rows of the result table containing the data.
	 */
	rows: Record<string, Term>[];
 
	/**
	 * A map of namespace IRIs to prefixes defined in the document or the workspace.
	 */
	namespaceMap: PrefixMap;
}
 
export interface BooleanResult {
	/**
	 * The type of the result, which is always 'boolean' for ASK queries.
	 */
	type: 'boolean';
 
	/**
	 * Boolean result value for ASK queries.
	 */
	value: boolean;
}
 
export interface QuadsResult {
	/**
	 * The type of the result, which is always 'quads' for CONSTRUCT or DESCRIBE queries.
	 */
	type: 'quads';
 
	/**
	 * The returned RDF document in the given serialization format.
	 */
	document: string;
 
	/**
	 * The MIME type of the returned document.
	 */
	mimeType: string;
}