acdh-oeaw / rdfproxy

Functionality for mapping SPARQL query result sets to Pydantic models
GNU General Public License v3.0
2 stars 0 forks source link

Encapsulate query construction logic in QueryConstructor class #134

Open lu-pl opened 3 weeks ago

lu-pl commented 3 weeks ago

Dynamic SPARQL query construction is essential for how rdfproxy works and also the (conceptually) most difficult and potentially error-prone part of the library.

Basically, the SPARQL adapter has to construct two queries, an items_query and a count_query - exactly how those queries are constructed and what modifications and injections have to happen depends on the query and on the supplied model.

Currently, the logic for query construction is expressed as a set of functions in rdfproxy.utils.sparql_utils.

Query construction logic should utilize a protocol-based constructor dispatch mechanism and could be bundled in a QueryConstructor class.

sparqlmodeladapter drawio

This would significantly help with keeping the query modification logic organized and also allow for much cleaner code in rdfproxy.SPARQLModelAdapter, e.g.:

class SPARQLModelAdapter(Generic[_TModelInstance]):
    # ...
    def query(self, *, page: int = 1, size: int = 100) -> Page[_TModelInstance]:
        query_constructor = QueryConstructor(query=self._query, model=self._model)

        count_query: str = query_constructor.get_count_query()
        items_query: str = query_constructor.get_items_query()

        # ...
lu-pl commented 2 weeks ago

Depends on #125.

lu-pl commented 2 weeks ago

Quick sketch of the general query construction and constructor dispatch mechanism:

constructor drawio