query.py 1015 B

123456789101112131415161718192021222324252627282930313233
  1. import logging
  2. from io import BytesIO
  3. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  4. from lakesuperior.dictionaries.namespaces import ns_mgr as nsm
  5. from lakesuperior.env import env
  6. from lakesuperior.store.ldp_rs.lmdb_store import LmdbStore, TxnManager
  7. logger = logging.getLogger(__name__)
  8. rdfly = env.app_globals.rdfly
  9. rdf_store = env.app_globals.rdf_store
  10. def sparql_query(qry_str, fmt):
  11. """
  12. Send a SPARQL query to the triplestore.
  13. :param str qry_str: SPARQL query string. SPARQL 1.1 Query Language
  14. (https://www.w3.org/TR/sparql11-query/) is supported.
  15. :param str fmt: Serialization format. This varies depending on the
  16. query type (SELECT, ASK, CONSTRUCT, etc.). [TODO Add reference to
  17. RDFLib serialization formats]
  18. :rtype: BytesIO
  19. :return: Serialized SPARQL results.
  20. """
  21. with TxnManager(rdf_store) as txn:
  22. qres = rdfly.raw_query(qry_str)
  23. out_stream = BytesIO(qres.serialize(format=fmt))
  24. return out_stream