query.py 968 B

1234567891011121314151617181920212223242526272829303132
  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 qry_str (str) SPARQL query string. SPARQL 1.1 Query Language
  14. (https://www.w3.org/TR/sparql11-query/) is supported.
  15. @param fmt(string) Serialization format. This varies depending on the
  16. query type (SELECT, ASK, CONSTRUCT, etc.). [@TODO Add reference to RDFLib
  17. serialization formats]
  18. @return BytesIO
  19. '''
  20. with TxnManager(rdf_store) as txn:
  21. qres = rdfly.raw_query(qry_str)
  22. out_stream = BytesIO(qres.serialize(format=fmt))
  23. return out_stream