equal
deleted
inserted
replaced
21 |
21 |
22 from logilab.common.decorators import cached, clear_cache, copy_cache |
22 from logilab.common.decorators import cached, clear_cache, copy_cache |
23 |
23 |
24 from rql import nodes, stmts |
24 from rql import nodes, stmts |
25 |
25 |
26 from cubicweb import NotAnEntity |
26 from cubicweb import NotAnEntity, NoResultError, MultipleResultsError |
27 |
27 |
28 |
28 |
29 class ResultSet(object): |
29 class ResultSet(object): |
30 """A result set wraps a RQL query result. This object implements |
30 """A result set wraps a RQL query result. This object implements |
31 partially the list protocol to allow direct use as a list of |
31 partially the list protocol to allow direct use as a list of |
434 if eschema.final: |
434 if eschema.final: |
435 raise NotAnEntity(etype) |
435 raise NotAnEntity(etype) |
436 except KeyError: |
436 except KeyError: |
437 raise NotAnEntity(etype) |
437 raise NotAnEntity(etype) |
438 return self._build_entity(row, col) |
438 return self._build_entity(row, col) |
|
439 |
|
440 def one(self, col=0): |
|
441 """Retrieve exactly one entity from the query. |
|
442 |
|
443 If the result set is empty, raises :exc:`NoResultError`. |
|
444 If the result set has more than one row, raises |
|
445 :exc:`MultipleResultsError`. |
|
446 |
|
447 :type col: int |
|
448 :param col: The column localising the entity in the unique row |
|
449 |
|
450 :return: the partially initialized `Entity` instance |
|
451 """ |
|
452 if len(self) == 1: |
|
453 return self.get_entity(0, col) |
|
454 elif len(self) == 0: |
|
455 raise NoResultError("No row was found for one()") |
|
456 else: |
|
457 raise MultipleResultsError("Multiple rows were found for one()") |
439 |
458 |
440 def _build_entity(self, row, col): |
459 def _build_entity(self, row, col): |
441 """internal method to get a single entity, returns a partially |
460 """internal method to get a single entity, returns a partially |
442 initialized Entity instance. |
461 initialized Entity instance. |
443 |
462 |