Query
SPARQL query execution - pure-library module (no Flask dependency).
This module provides structured SPARQL query execution built on top of
SparqlHelper. It adds:
Pydantic result models (
ResultCell,QueryResult) that give strongly-typed access to SPARQL JSON result bindings.A single
execute_sparql()convenience function with a clean dict-return signature suitable for the public API.
All HTTP / retry / GET->POST logic is delegated to SparqlHelper.
- class ResultCell(*, value: str, type: str, lang: str | None = None, datatype: str | None = None)[source]
Bases:
BaseModelOne cell in a SPARQL result row.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class QueryResult(*, query: str, endpoint: str, variables: list[str], rows: list[dict[str, ~rdfsolve.query.ResultCell]], variable_map: dict[str, str] = <factory>, row_count: int, duration_ms: int, error: str | None = None)[source]
Bases:
BaseModelStructured result from a SPARQL query execution.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- rows: list[dict[str, ResultCell]]
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- execute_sparql(query: str, endpoint: str, *, method: str = 'GET', timeout: int = 30, variable_map: dict[str, str] | None = None) QueryResult[source]
Execute a SPARQL SELECT query and return a
QueryResult.- Parameters:
query – Full SPARQL query string.
endpoint – URL of the SPARQL endpoint.
method – HTTP method (
"GET"or"POST"). If"GET"fails the underlyingSparqlHelperwill automatically retry with POST.timeout – Request timeout in seconds.
variable_map – Optional mapping of SPARQL
?variablenames to schema URIs.
- Returns:
Pydantic model with
query,endpoint,variables,rows,variable_map,row_count,duration_ms, and optionallyerror.- Return type: