/telescope/sparql/queryforms.py

https://bitbucket.org/ww/telescope · Python · 73 lines · 36 code · 20 blank · 17 comment · 4 complexity · 50d4cab80b994b4ccd1848c37460d269 MD5 · raw file

  1. from telescope.exceptions import *
  2. from telescope.sparql.query import *
  3. __all__ = ['Ask', 'Construct', 'Select', 'Describe']
  4. class Ask(SPARQLQuery):
  5. """Programmatically build a SPARQL ASK query."""
  6. query_form = 'ASK'
  7. class Construct(SolutionModifierSupportingQuery):
  8. """Programmatically build a SPARQL CONSTRUCT query."""
  9. query_form = 'CONSTRUCT'
  10. def __init__(self, template, pattern=None, order_by=None, limit=None,
  11. offset=None):
  12. super(Construct, self).__init__(pattern, order_by=order_by,
  13. limit=limit, offset=offset)
  14. self._template = template
  15. def _get_compiler_class(self):
  16. from telescope.sparql.compiler import ConstructCompiler
  17. return ConstructCompiler
  18. def template(self, template):
  19. return self._clone(_template=template)
  20. class Select(ProjectionSupportingQuery):
  21. """Programmatically build a SPARQL SELECT query."""
  22. query_form = 'SELECT'
  23. def __init__(self, projection, pattern=None, distinct=False,
  24. reduced=False, order_by=None, limit=None, offset=None):
  25. super(Select, self).__init__(projection, pattern, order_by=order_by,
  26. limit=limit, offset=offset)
  27. if distinct and reduced:
  28. raise InvalidRequestError("DISTINCT and REDUCED are mutually exclusive.")
  29. self._distinct = distinct
  30. self._reduced = reduced
  31. def _get_compiler_class(self):
  32. from telescope.sparql.compiler import SelectCompiler
  33. return SelectCompiler
  34. def distinct(self, flag=True):
  35. """
  36. Return a new `Select` with the DISTINCT modifier (or without it if
  37. `flag` is false).
  38. If `flag` is true (the default), then `reduced` is forced to False.
  39. """
  40. return self._clone(_distinct=flag, _reduced=not flag and self._reduced)
  41. def reduced(self, flag=True):
  42. """Return a new `Select` with the REDUCED modifier (or without it if
  43. `flag` is false).
  44. If `flag` is true (the default), then `distinct` is forced to False.
  45. """
  46. return self._clone(_reduced=flag, _distinct=not flag and self._distinct)
  47. class Describe(ProjectionSupportingQuery):
  48. """Programmatically build a SPARQL DESCRIBE query."""
  49. query_form = 'DESCRIBE'