diff --git a/eds4jinja2/adapters/local_sparql_ds.py b/eds4jinja2/adapters/local_sparql_ds.py index 68ce9e3..8f162ba 100644 --- a/eds4jinja2/adapters/local_sparql_ds.py +++ b/eds4jinja2/adapters/local_sparql_ds.py @@ -30,7 +30,7 @@ def __init__(self, filename): def __reduce_bound_triple_to_string_format(self, dict_of_bound_variables: dict): return {str(k): str(v) for k, v in dict_of_bound_variables.items()} - def with_query(self, sparql_query: str, substitution_variables: dict = None) -> 'RDFFileDataSource': + def with_query(self, sparql_query: str, substitution_variables: dict = None, prefixes: str = "") -> 'RDFFileDataSource': """ Set the query text and return the reference to self for chaining. :return: @@ -44,9 +44,11 @@ def with_query(self, sparql_query: str, substitution_variables: dict = None) -> else: self.__query__ = sparql_query + self.__query__ = (prefixes + " " + self.__query__).strip() + return self - def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None) -> 'RDFFileDataSource': + def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None, prefixes: str = "") -> 'RDFFileDataSource': """ Set the query text and return the reference to self for chaining. :return: @@ -61,6 +63,8 @@ def with_query_from_file(self, sparql_query_file_path: str, substitution_variabl template = SubstitutionTemplate(self.__query__) self.__query__ = template.safe_substitute(substitution_variables) + self.__query__ = (prefixes + " " + self.__query__).strip() + return self def with_file(self, file: str) -> 'RDFFileDataSource': diff --git a/eds4jinja2/adapters/remote_sparql_ds.py b/eds4jinja2/adapters/remote_sparql_ds.py index b9de062..f5c1c56 100644 --- a/eds4jinja2/adapters/remote_sparql_ds.py +++ b/eds4jinja2/adapters/remote_sparql_ds.py @@ -50,7 +50,7 @@ def __init__(self, endpoint_url): self.__can_be_tree = True self.__can_be_tabular = True - def with_query(self, sparql_query: str, substitution_variables: dict = None) -> 'RemoteSPARQLEndpointDataSource': + def with_query(self, sparql_query: str, substitution_variables: dict = None, sparql_prefixes: str = "") -> 'RemoteSPARQLEndpointDataSource': """ Set the query text and return the reference to self for chaining. :return: @@ -59,10 +59,12 @@ def with_query(self, sparql_query: str, substitution_variables: dict = None) -> template_query = SubstitutionTemplate(sparql_query) sparql_query = template_query.safe_substitute(substitution_variables) - self.endpoint.setQuery(sparql_query) + new_query = (sparql_prefixes + " " + sparql_query).strip() + + self.endpoint.setQuery(new_query) return self - def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None) -> 'RemoteSPARQLEndpointDataSource': + def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None, prefixes: str = "") -> 'RemoteSPARQLEndpointDataSource': """ Set the query text and return the reference to self for chaining. :return: @@ -75,7 +77,9 @@ def with_query_from_file(self, sparql_query_file_path: str, substitution_variabl template_query = SubstitutionTemplate(query_from_file) query_from_file = template_query.safe_substitute(substitution_variables) - self.endpoint.setQuery(query_from_file) + new_query = (prefixes + " " + query_from_file).strip() + + self.endpoint.setQuery(new_query) return self def with_uri(self, uri: str, graph_uri: Optional[str] = None) -> 'RemoteSPARQLEndpointDataSource': @@ -90,7 +94,7 @@ def with_uri(self, uri: str, graph_uri: Optional[str] = None) -> 'RemoteSPARQLEn return self def _fetch_tree(self): - if not self.endpoint.queryString: + if not self.endpoint.queryString or self.endpoint.queryString.isspace(): raise Exception("The query is empty.") self.endpoint.setReturnFormat(JSON) @@ -98,7 +102,7 @@ def _fetch_tree(self): return query.convert() def _fetch_tabular(self): - if not self.endpoint.queryString: + if not self.endpoint.queryString or self.endpoint.queryString.isspace(): raise Exception("The query is empty.") self.endpoint.setReturnFormat(CSV) diff --git a/tests/unit/test_local_sparql_ds.py b/tests/unit/test_local_sparql_ds.py index 11c5905..886571b 100644 --- a/tests/unit/test_local_sparql_ds.py +++ b/tests/unit/test_local_sparql_ds.py @@ -58,6 +58,26 @@ def test_load_local_query_from_file_sparql_fetch_tabular(): local_rdf_ds.fetch_tabular() +def test_load_local_query_from_file_and_prefixes(): + local_rdf_ds = RDFFileDataSource( + str(pathlib.Path("../test_data/shacl.example.shapes.ttl"))) + + local_rdf_ds.with_query_from_file(pathlib.Path("./tests/test_data/queries/spo_limit_10.txt"), None, "PREFIX TEST") + assert local_rdf_ds.__query__.startswith("PREFIX TEST") + + +def test_load_local_query_and_prefixes(): + local_rdf_ds = RDFFileDataSource( + str(pathlib.Path("../test_data/shacl.example.shapes.ttl"))) + + local_rdf_ds.with_query("""SELECT * + WHERE { + ?s ?p ?o + } + limit 10""", None, "PREFIX TEST") + assert local_rdf_ds.__query__.startswith("PREFIX TEST") + + def test_load_local_query_from_file_substitution(): local_rdf_ds = RDFFileDataSource( str(pathlib.Path("../test_data/shacl.example.shapes.ttl"))) diff --git a/tests/unit/test_sparql_ds.py b/tests/unit/test_sparql_ds.py index c162e86..31bb067 100644 --- a/tests/unit/test_sparql_ds.py +++ b/tests/unit/test_sparql_ds.py @@ -68,6 +68,19 @@ def test_query_endpoint_and_fetch_tabular(): assert error is None +def test_query_and_prefixes(): + fds = RemoteSPARQLEndpointDataSource(ENDPOINT_REMOTE_CORRECT) + fds.with_query(SPO_LIMIT_10, None, "PREFIX TEST") + assert fds.endpoint.queryString.startswith("PREFIX TEST") + + +def test_query_from_file_and_prefixes(): + fds = RemoteSPARQLEndpointDataSource(ENDPOINT_REMOTE_CORRECT) + fds.with_query_from_file( + pathlib.Path("./tests/test_data/queries/spo_limit_10.txt"), None, "PREFIX TEST") + assert fds.endpoint.queryString.startswith("PREFIX TEST") + + def test_query_endpoint_and_fetch_tabular_without_query(): fds = RemoteSPARQLEndpointDataSource(ENDPOINT_REMOTE_CORRECT) result, error_string = fds.with_query("").fetch_tabular()