/telescope/sparql/queryforms.py
Python | 73 lines | 36 code | 20 blank | 17 comment | 4 complexity | 50d4cab80b994b4ccd1848c37460d269 MD5 | raw file
1from telescope.exceptions import * 2from telescope.sparql.query import * 3 4__all__ = ['Ask', 'Construct', 'Select', 'Describe'] 5 6class Ask(SPARQLQuery): 7 """Programmatically build a SPARQL ASK query.""" 8 9 query_form = 'ASK' 10 11 12class Construct(SolutionModifierSupportingQuery): 13 """Programmatically build a SPARQL CONSTRUCT query.""" 14 15 query_form = 'CONSTRUCT' 16 17 def __init__(self, template, pattern=None, order_by=None, limit=None, 18 offset=None): 19 super(Construct, self).__init__(pattern, order_by=order_by, 20 limit=limit, offset=offset) 21 self._template = template 22 23 def _get_compiler_class(self): 24 from telescope.sparql.compiler import ConstructCompiler 25 return ConstructCompiler 26 27 def template(self, template): 28 return self._clone(_template=template) 29 30 31class Select(ProjectionSupportingQuery): 32 """Programmatically build a SPARQL SELECT query.""" 33 34 query_form = 'SELECT' 35 36 def __init__(self, projection, pattern=None, distinct=False, 37 reduced=False, order_by=None, limit=None, offset=None): 38 super(Select, self).__init__(projection, pattern, order_by=order_by, 39 limit=limit, offset=offset) 40 if distinct and reduced: 41 raise InvalidRequestError("DISTINCT and REDUCED are mutually exclusive.") 42 self._distinct = distinct 43 self._reduced = reduced 44 45 def _get_compiler_class(self): 46 from telescope.sparql.compiler import SelectCompiler 47 return SelectCompiler 48 49 def distinct(self, flag=True): 50 """ 51 Return a new `Select` with the DISTINCT modifier (or without it if 52 `flag` is false). 53 54 If `flag` is true (the default), then `reduced` is forced to False. 55 56 """ 57 return self._clone(_distinct=flag, _reduced=not flag and self._reduced) 58 59 def reduced(self, flag=True): 60 """Return a new `Select` with the REDUCED modifier (or without it if 61 `flag` is false). 62 63 If `flag` is true (the default), then `distinct` is forced to False. 64 65 """ 66 return self._clone(_reduced=flag, _distinct=not flag and self._distinct) 67 68 69class Describe(ProjectionSupportingQuery): 70 """Programmatically build a SPARQL DESCRIBE query.""" 71 72 query_form = 'DESCRIBE' 73