-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The change introduces a check_query callable which runs an extensible compose pipeline of query checkers. Note regarding QueryParseException: This custom exception is intended to be a thin wrapper around a pyparsing ParseException that RDFLib raises. This avoids introducing pyparsing as a dependency just to be able to test against this exception. I feel like RDFLib should not raise a pyparsing exception but provide a thin wrapper itself. See RDFLib/rdflib#3057. The check_query function runs in SPARQLModelAdapter to enable fast failures on inapplicable queries; also a run_query_check=False flag is added to the QueryConstructor class so that QueryConstructor could also be used as a standalone class with query checking enabled. Closes #116.
- Loading branch information
Showing
4 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Functionality for performing checks on SPARQL queries.""" | ||
|
||
import logging | ||
from typing import TypeVar | ||
|
||
from rdflib.plugins.sparql.parser import parseQuery | ||
from rdfproxy.utils._exceptions import QueryParseException, UnsupportedQueryException | ||
from rdfproxy.utils.utils import compose_left | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_TQuery = TypeVar("_TQuery", bound=str) | ||
|
||
|
||
def _check_select_query(query: _TQuery) -> _TQuery: | ||
"""Check if a query is parsable and a SELECT query.""" | ||
logger.debug("Running parsable SELECT check on '%s'", query) | ||
|
||
try: | ||
parsed = parseQuery(query) | ||
except Exception as e: | ||
raise QueryParseException from e | ||
else: | ||
_, query_type = parsed | ||
if query_type.name != "SelectQuery": | ||
raise UnsupportedQueryException("Only SELECT queries are applicable.") | ||
|
||
return query | ||
|
||
|
||
def check_query(query: _TQuery) -> _TQuery: | ||
"""Run a series of checks on a query.""" | ||
logger.debug("Running query checks on '%s'", query) | ||
return compose_left(_check_select_query)(query) |