rset.py
branchstable
changeset 5312 d2dbba898a96
parent 5311 34dc38456376
child 5314 86e5abbebfaf
equal deleted inserted replaced
5311:34dc38456376 5312:d2dbba898a96
    13 
    13 
    14 from cubicweb import NotAnEntity
    14 from cubicweb import NotAnEntity
    15 
    15 
    16 
    16 
    17 class ResultSet(object):
    17 class ResultSet(object):
    18     """A result set wraps a RQL query result. This object implements a partial
    18     """A result set wraps a RQL query result. This object implements
    19     list protocol to allow direct use as a list of result rows.
    19     partially the list protocol to allow direct use as a list of
       
    20     result rows.
    20 
    21 
    21     :type rowcount: int
    22     :type rowcount: int
    22     :ivar rowcount: number of rows in the result
    23     :param rowcount: number of rows in the result
    23 
    24 
    24     :type rows: list
    25     :type rows: list
    25     :ivar rows: list of rows of result
    26     :param rows: list of rows of result
    26 
    27 
    27     :type description: list
    28     :type description: list
    28     :ivar description:
    29     :param description:
    29       result's description, using the same structure as the result itself
    30       result's description, using the same structure as the result itself
    30 
    31 
    31     :type rql: str or unicode
    32     :type rql: str or unicode
    32     :ivar rql: the original RQL query string
    33     :param rql: the original RQL query string
    33     """
    34     """
    34     def __init__(self, results, rql, args=None, description=(), cachekey=None,
    35     def __init__(self, results, rql, args=None, description=(), cachekey=None,
    35                  rqlst=None):
    36                  rqlst=None):
    36         self.rows = results
    37         self.rows = results
    37         self.rowcount = results and len(results) or 0
    38         self.rowcount = results and len(results) or 0
   202             descr.append(self.description[index])
   203             descr.append(self.description[index])
   203         rset.rowcount = len(rows)
   204         rset.rowcount = len(rows)
   204         return rset
   205         return rset
   205 
   206 
   206     def split_rset(self, keyfunc=None, col=0, return_dict=False):
   207     def split_rset(self, keyfunc=None, col=0, return_dict=False):
   207         """splits the result set in multiple result set according to a given key
   208         """splits the result set in multiple result sets according to
       
   209         a given key
   208 
   210 
   209         :type keyfunc: callable(entity or FinalType)
   211         :type keyfunc: callable(entity or FinalType)
   210         :param keyfunc:
   212         :param keyfunc:
   211           a callable which should take a value of the rset in argument and
   213           a callable which should take a value of the rset in argument and
   212           return the value used to group the value. If not define, raw value
   214           return the value used to group the value. If not define, raw value
   250             return mapping
   252             return mapping
   251         else:
   253         else:
   252             return result
   254             return result
   253 
   255 
   254     def limited_rql(self):
   256     def limited_rql(self):
   255         """return a printable rql for the result set associated to the object,
   257         """returns a printable rql for the result set associated to the object,
   256         with limit/offset correctly set according to maximum page size and
   258         with limit/offset correctly set according to maximum page size and
   257         currently displayed page when necessary
   259         currently displayed page when necessary
   258         """
   260         """
   259         # try to get page boundaries from the navigation component
   261         # try to get page boundaries from the navigation component
   260         # XXX we should probably not have a ref to this component here (eg in
   262         # XXX we should probably not have a ref to this component here (eg in
   375         entity.complete(skip_bytes=skip_bytes)
   377         entity.complete(skip_bytes=skip_bytes)
   376         return entity
   378         return entity
   377 
   379 
   378     @cached
   380     @cached
   379     def get_entity(self, row, col):
   381     def get_entity(self, row, col):
   380         """special method for query retreiving a single entity, returns a
   382         """convenience method for query retrieving a single entity, returns a
   381         partially initialized Entity instance.
   383         partially initialized Entity instance.
   382 
   384 
   383         WARNING: due to the cache wrapping this function, you should NEVER
   385         .. warning:
   384                  give row as a named parameter (i.e. rset.get_entity(req, 0)
   386 
   385                  is OK but rset.get_entity(row=0, req=req) isn't
   387           Due to the cache wrapping this function, you should NEVER
       
   388           give row as a named parameter (i.e. rset.get_entity(req, 0)
       
   389           is OK but rset.get_entity(row=0, req=req) isn't)
   386 
   390 
   387         :type row,col: int, int
   391         :type row,col: int, int
   388         :param row,col:
   392         :param row,col:
   389           row and col numbers localizing the entity among the result's table
   393           row and col numbers localizing the entity among the result's table
   390 
   394