spa2rql.py
changeset 2431 93c061eac647
parent 2430 7d9ed6c740ec
child 2435 85be7a811afe
equal deleted inserted replaced
2430:7d9ed6c740ec 2431:93c061eac647
    13 from cubicweb.xy import xy
    13 from cubicweb.xy import xy
    14 
    14 
    15 
    15 
    16 class UnsupportedQuery(Exception): pass
    16 class UnsupportedQuery(Exception): pass
    17 
    17 
       
    18 def order_limit_offset(sparqlst):
       
    19     addons = ''
       
    20     if sparqlst.orderby:
       
    21         sortterms = ', '.join('%s %s' % (var.name.upper(), ascdesc.upper())
       
    22                               for var, ascdesc in sparqlst.orderby)
       
    23         addons += ' ORDERBY %s' % sortterms
       
    24     if sparqlst.limit:
       
    25         addons += ' LIMIT %s' % sparqlst.limit
       
    26     if sparqlst.offset:
       
    27         addons += ' OFFSET %s' % sparqlst.offset
       
    28     return addons
    18 
    29 
    19 class QueryInfo(object):
    30 class QueryInfo(object):
    20     """wrapper class containing necessary information to generate a RQL query
    31     """wrapper class containing necessary information to generate a RQL query
    21     from a sparql syntax tree
    32     from a sparql syntax tree
    22     """
    33     """
    47                     thisunions[-1].append('%s is %s' % (objvar, ot))
    58                     thisunions[-1].append('%s is %s' % (objvar, ot))
    48             if not unions:
    59             if not unions:
    49                 unions = thisunions
    60                 unions = thisunions
    50             else:
    61             else:
    51                 unions = zip(*make_domains([unions, thisunions]))
    62                 unions = zip(*make_domains([unions, thisunions]))
    52         baserql = 'Any %s WHERE %s' % (', '.join(self.selection),
    63         selection = 'Any ' + ', '.join(self.selection)
    53                                        ', '.join(self.restrictions))
    64         sparqlst = self.sparqlst
    54         if self.sparqlst.distinct:
    65         if sparqlst.distinct:
    55             baserql = 'DISTINCT ' + baserql
    66             selection = 'DISTINCT ' + selection
    56         if not unions:
    67         if not unions:
    57             return baserql
    68             return '%s%s WHERE %s' % (selection, order_limit_offset(sparqlst),
       
    69                                       ', '.join(self.restrictions))
       
    70         baserql = '%s WHERE %s' % (selection, ', '.join(self.restrictions))
    58         rqls = ['(%s, %s)' % (baserql, ', '.join(unionrestrs))
    71         rqls = ['(%s, %s)' % (baserql, ', '.join(unionrestrs))
    59                 for unionrestrs in unions]
    72                 for unionrestrs in unions]
    60         return ' UNION '.join(rqls)
    73         rql = ' UNION '.join(rqls)
       
    74         if sparqlst.orderby or sparqlst.limit or sparqlst.offset:
       
    75             rql = '%s%s WITH %s BEING (%s)' % (
       
    76                 selection, order_limit_offset(sparqlst),
       
    77                 ', '.join(self.selection), rql)
       
    78         return rql
    61 
    79 
    62     def set_possible_types(self, var, varpossibletypes):
    80     def set_possible_types(self, var, varpossibletypes):
    63         """set/restrict possible types for the given variable.
    81         """set/restrict possible types for the given variable.
    64 
    82 
    65         :return: True if something changed, else false.
    83         :return: True if something changed, else false.