1 """The `ResultSet` class which is returned as result of a rql query |
1 """The `ResultSet` class which is returned as result of a rql query |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 """ |
6 """ |
7 __docformat__ = "restructuredtext en" |
7 __docformat__ = "restructuredtext en" |
8 |
8 |
9 from logilab.common.decorators import cached, clear_cache, copy_cache |
9 from logilab.common.decorators import cached, clear_cache, copy_cache |
58 return '<resultset %s (%s rows)>' % (self.rql, len(self.rows)) |
58 return '<resultset %s (%s rows)>' % (self.rql, len(self.rows)) |
59 |
59 |
60 def __repr__(self): |
60 def __repr__(self): |
61 if not self.rows: |
61 if not self.rows: |
62 return '<empty resultset for %r>' % self.rql |
62 return '<empty resultset for %r>' % self.rql |
|
63 rows = self.rows |
|
64 if len(rows) > 10: |
|
65 rows = rows[:10] + ['...'] |
63 if not self.description: |
66 if not self.description: |
64 return '<resultset %r: %s>' % (self.rql, '\n'.join(str(r) for r in self.rows)) |
67 return '<resultset %r (%s rows): %s>' % (self.rql, len(self.rows), |
65 return '<resultset %r: %s>' % (self.rql, |
68 '\n'.join(str(r) for r in rows)) |
66 '\n'.join('%s (%s)' % (r, d) |
69 return '<resultset %r (%s rows): %s>' % (self.rql, len(self.rows), |
67 for r, d in zip(self.rows, self.description))) |
70 '\n'.join('%s (%s)' % (r, d) |
|
71 for r, d in zip(rows, self.description))) |
68 |
72 |
69 @cached |
73 @cached |
70 def possible_actions(self): |
74 def possible_actions(self): |
71 return self.vreg.possible_vobjects('actions', self.req, self) |
75 return self.vreg.possible_vobjects('actions', self.req, self) |
72 |
76 |
462 """try to get the related entity to extract format information if any""" |
466 """try to get the related entity to extract format information if any""" |
463 locate_query_col = col |
467 locate_query_col = col |
464 rqlst = self.syntax_tree() |
468 rqlst = self.syntax_tree() |
465 etype = self.description[row][col] |
469 etype = self.description[row][col] |
466 if self.vreg.schema.eschema(etype).is_final(): |
470 if self.vreg.schema.eschema(etype).is_final(): |
467 # final type, find a better (ambiguous) one |
471 # final type, find a better one to locate the correct subquery |
|
472 # (ambiguous if possible) |
468 for i in xrange(len(rqlst.children[0].selection)): |
473 for i in xrange(len(rqlst.children[0].selection)): |
469 if i == col: |
474 if i == col: |
470 continue |
475 continue |
471 coletype = self.description[row][i] |
476 coletype = self.description[row][i] |
472 if coletype is None: |
477 if coletype is None: |
474 if not self.vreg.schema.eschema(coletype).is_final(): |
479 if not self.vreg.schema.eschema(coletype).is_final(): |
475 etype = coletype |
480 etype = coletype |
476 locate_query_col = i |
481 locate_query_col = i |
477 if len(self.column_types(i)) > 1: |
482 if len(self.column_types(i)) > 1: |
478 break |
483 break |
479 # UNION query, find the subquery from which this entity has been |
484 # UNION query, find the subquery from which this entity has been found |
480 # found |
|
481 select = rqlst.locate_subquery(locate_query_col, etype, self.args) |
485 select = rqlst.locate_subquery(locate_query_col, etype, self.args) |
482 try: |
486 try: |
483 myvar = select.selection[col].variable |
487 myvar = select.selection[col].variable |
484 except AttributeError: |
488 except AttributeError: |
485 # no .selection attribute is available |
489 # not a variable |
486 return None, None |
490 return None, None |
487 rel = myvar.main_relation() |
491 rel = myvar.main_relation() |
488 if rel is not None: |
492 if rel is not None: |
489 index = rel.children[0].variable.selected_index() |
493 index = rel.children[0].variable.selected_index() |
490 if index is not None: |
494 if index is not None and self.rows[row][index]: |
491 return self.get_entity(row, index), rel.r_type |
495 return self.get_entity(row, index), rel.r_type |
492 return None, None |
496 return None, None |
493 |
497 |
494 @cached |
498 @cached |
495 def searched_text(self): |
499 def searched_text(self): |