author | Julien Cristau <julien.cristau@logilab.fr> |
Thu, 29 Jan 2015 10:54:41 +0100 | |
changeset 10321 | a4af502191d5 |
parent 10314 | 8f3783dc6358 |
child 10354 | 635cfac73d28 |
permissions | -rw-r--r-- |
7395
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5671
3a2063b752f3
[editform] consistent [do]reledit between attributes and relations: check if rdef is hidden in main section of uicfg.afs for both attributes and relations, and in the reledit view, not in doreledit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5667
diff
changeset
|
18 |
"""The `ResultSet` class which is returned as result of an rql query""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
10087
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
22 |
from warnings import warn |
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
23 |
|
10313
ae9e23cf8790
[rset] Deprecate the 'encoded' argument to ResultSet.printable_rql()
Rémi Cardona <remi.cardona@free.fr>
parents:
10187
diff
changeset
|
24 |
from logilab.common import nullobject |
0 | 25 |
from logilab.common.decorators import cached, clear_cache, copy_cache |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
26 |
from rql import nodes, stmts |
0 | 27 |
|
9347
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
28 |
from cubicweb import NotAnEntity, NoResultError, MultipleResultsError |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
29 |
|
0 | 30 |
|
10313
ae9e23cf8790
[rset] Deprecate the 'encoded' argument to ResultSet.printable_rql()
Rémi Cardona <remi.cardona@free.fr>
parents:
10187
diff
changeset
|
31 |
_MARKER = nullobject() |
ae9e23cf8790
[rset] Deprecate the 'encoded' argument to ResultSet.printable_rql()
Rémi Cardona <remi.cardona@free.fr>
parents:
10187
diff
changeset
|
32 |
|
ae9e23cf8790
[rset] Deprecate the 'encoded' argument to ResultSet.printable_rql()
Rémi Cardona <remi.cardona@free.fr>
parents:
10187
diff
changeset
|
33 |
|
0 | 34 |
class ResultSet(object): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
35 |
"""A result set wraps a RQL query result. This object implements |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
36 |
partially the list protocol to allow direct use as a list of |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
37 |
result rows. |
0 | 38 |
|
39 |
:type rowcount: int |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
40 |
:param rowcount: number of rows in the result |
0 | 41 |
|
42 |
:type rows: list |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
43 |
:param rows: list of rows of result |
0 | 44 |
|
45 |
:type description: list |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
46 |
:param description: |
0 | 47 |
result's description, using the same structure as the result itself |
48 |
||
49 |
:type rql: str or unicode |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
50 |
:param rql: the original RQL query string |
0 | 51 |
""" |
5740
46207f491a51
[rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5671
diff
changeset
|
52 |
|
9281
49a7392bb5b5
[rset] make sure rset.description is always a list
Julien Cristau <julien.cristau@logilab.fr>
parents:
8695
diff
changeset
|
53 |
def __init__(self, results, rql, args=None, description=None, rqlst=None): |
10087
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
54 |
if rqlst is not None: |
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
55 |
warn('[3.20] rqlst parameter is deprecated', |
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
56 |
DeprecationWarning, stacklevel=2) |
0 | 57 |
self.rows = results |
58 |
self.rowcount = results and len(results) or 0 |
|
59 |
# original query and arguments |
|
60 |
self.rql = rql |
|
61 |
self.args = args |
|
62 |
# entity types for each cell (same shape as rows) |
|
63 |
# maybe discarded if specified when the query has been executed |
|
9331
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
64 |
if description is None: |
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
65 |
self.description = [] |
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
66 |
else: |
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
67 |
self.description = description |
0 | 68 |
# set to (limit, offset) when a result set is limited using the |
69 |
# .limit method |
|
70 |
self.limited = None |
|
71 |
# set by the cursor which returned this resultset |
|
72 |
self.req = None |
|
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
73 |
# actions cache |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
74 |
self._rsetactions = None |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
75 |
|
0 | 76 |
def __str__(self): |
77 |
if not self.rows: |
|
78 |
return '<empty resultset %s>' % self.rql |
|
79 |
return '<resultset %s (%s rows)>' % (self.rql, len(self.rows)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
80 |
|
0 | 81 |
def __repr__(self): |
82 |
if not self.rows: |
|
170
455ff18ef28e
use repr (instead of str) to print rql in __repr__ method of a result set
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
0
diff
changeset
|
83 |
return '<empty resultset for %r>' % self.rql |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
84 |
rows = self.rows |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
85 |
if len(rows) > 10: |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
86 |
rows = rows[:10] + ['...'] |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
87 |
if len(rows) > 1: |
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
88 |
# add a line break before first entity if more that one. |
7395
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
89 |
pattern = '<resultset %r (%s rows):\n%s>' |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
90 |
else: |
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
91 |
pattern = '<resultset %r (%s rows): %s>' |
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
92 |
|
0 | 93 |
if not self.description: |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
94 |
return pattern % (self.rql, len(self.rows), |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
95 |
'\n'.join(str(r) for r in rows)) |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
96 |
return pattern % (self.rql, len(self.rows), |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
97 |
'\n'.join('%s (%s)' % (r, d) |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
98 |
for r, d in zip(rows, self.description))) |
0 | 99 |
|
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
100 |
def possible_actions(self, **kwargs): |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
101 |
if self._rsetactions is None: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
102 |
self._rsetactions = {} |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
103 |
if kwargs: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
104 |
key = tuple(sorted(kwargs.iteritems())) |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
105 |
else: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
106 |
key = None |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
107 |
try: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
108 |
return self._rsetactions[key] |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
109 |
except KeyError: |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
110 |
actions = self.req.vreg['actions'].poss_visible_objects( |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2648
diff
changeset
|
111 |
self.req, rset=self, **kwargs) |
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
112 |
self._rsetactions[key] = actions |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
113 |
return actions |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
114 |
|
0 | 115 |
def __len__(self): |
116 |
"""returns the result set's size""" |
|
117 |
return self.rowcount |
|
118 |
||
119 |
def __getitem__(self, i): |
|
120 |
"""returns the ith element of the result set""" |
|
121 |
return self.rows[i] #ResultSetRow(self.rows[i]) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
122 |
|
0 | 123 |
def __getslice__(self, i, j): |
124 |
"""returns slice [i:j] of the result set""" |
|
125 |
return self.rows[i:j] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
126 |
|
0 | 127 |
def __iter__(self): |
128 |
"""Returns an iterator over rows""" |
|
129 |
return iter(self.rows) |
|
130 |
||
131 |
def __add__(self, rset): |
|
132 |
# XXX buggy implementation (.rql and .args attributes at least much |
|
133 |
# probably differ) |
|
134 |
# at least rql could be fixed now that we have union and sub-queries |
|
135 |
# but I tend to think that since we have that, we should not need this |
|
136 |
# method anymore (syt) |
|
137 |
rset = ResultSet(self.rows+rset.rows, self.rql, self.args, |
|
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
138 |
self.description + rset.description) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
139 |
rset.req = self.req |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
140 |
return rset |
0 | 141 |
|
3764
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
142 |
def copy(self, rows=None, descr=None): |
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
143 |
if rows is None: |
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
144 |
rows = self.rows[:] |
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
145 |
descr = self.description[:] |
0 | 146 |
rset = ResultSet(rows, self.rql, self.args, descr) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
147 |
rset.req = self.req |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
148 |
return rset |
0 | 149 |
|
150 |
def transformed_rset(self, transformcb): |
|
151 |
""" the result set according to a given column types |
|
152 |
||
153 |
:type transormcb: callable(row, desc) |
|
154 |
:param transformcb: |
|
155 |
a callable which should take a row and its type description as |
|
156 |
parameters, and return the transformed row and type description. |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
157 |
|
0 | 158 |
|
159 |
:type col: int |
|
160 |
:param col: the column index |
|
161 |
||
162 |
:rtype: `ResultSet` |
|
163 |
""" |
|
164 |
rows, descr = [], [] |
|
3764
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
165 |
rset = self.copy(rows, descr) |
0 | 166 |
for row, desc in zip(self.rows, self.description): |
167 |
nrow, ndesc = transformcb(row, desc) |
|
168 |
if ndesc: # transformcb returns None for ndesc to skip that row |
|
169 |
rows.append(nrow) |
|
170 |
descr.append(ndesc) |
|
171 |
rset.rowcount = len(rows) |
|
172 |
return rset |
|
173 |
||
174 |
def filtered_rset(self, filtercb, col=0): |
|
175 |
"""filter the result set according to a given filtercb |
|
176 |
||
177 |
:type filtercb: callable(entity) |
|
178 |
:param filtercb: |
|
179 |
a callable which should take an entity as argument and return |
|
180 |
False if it should be skipped, else True |
|
181 |
||
182 |
:type col: int |
|
183 |
:param col: the column index |
|
184 |
||
185 |
:rtype: `ResultSet` |
|
186 |
""" |
|
187 |
rows, descr = [], [] |
|
3764
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
188 |
rset = self.copy(rows, descr) |
0 | 189 |
for i in xrange(len(self)): |
190 |
if not filtercb(self.get_entity(i, col)): |
|
191 |
continue |
|
192 |
rows.append(self.rows[i]) |
|
193 |
descr.append(self.description[i]) |
|
194 |
rset.rowcount = len(rows) |
|
195 |
return rset |
|
196 |
||
197 |
||
198 |
def sorted_rset(self, keyfunc, reverse=False, col=0): |
|
199 |
"""sorts the result set according to a given keyfunc |
|
200 |
||
201 |
:type keyfunc: callable(entity) |
|
202 |
:param keyfunc: |
|
203 |
a callable which should take an entity as argument and return |
|
204 |
the value used to compare and sort |
|
205 |
||
206 |
:type reverse: bool |
|
207 |
:param reverse: if the result should be reversed |
|
208 |
||
209 |
:type col: int |
|
210 |
:param col: the column index. if col = -1, the whole row are used |
|
211 |
||
212 |
:rtype: `ResultSet` |
|
213 |
""" |
|
214 |
rows, descr = [], [] |
|
3764
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
215 |
rset = self.copy(rows, descr) |
0 | 216 |
if col >= 0: |
217 |
entities = sorted(enumerate(self.entities(col)), |
|
218 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
219 |
else: |
|
220 |
entities = sorted(enumerate(self), |
|
221 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
1132 | 222 |
for index, _ in entities: |
0 | 223 |
rows.append(self.rows[index]) |
224 |
descr.append(self.description[index]) |
|
225 |
rset.rowcount = len(rows) |
|
226 |
return rset |
|
227 |
||
228 |
def split_rset(self, keyfunc=None, col=0, return_dict=False): |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
229 |
"""splits the result set in multiple result sets according to |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
230 |
a given key |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
231 |
|
0 | 232 |
:type keyfunc: callable(entity or FinalType) |
233 |
:param keyfunc: |
|
234 |
a callable which should take a value of the rset in argument and |
|
235 |
return the value used to group the value. If not define, raw value |
|
236 |
of the specified columns is used. |
|
237 |
||
238 |
:type col: int |
|
239 |
:param col: the column index. if col = -1, the whole row are used |
|
240 |
||
241 |
:type return_dict: Boolean |
|
242 |
:param return_dict: If true, the function return a mapping |
|
243 |
(key -> rset) instead of a list of rset |
|
244 |
||
245 |
:rtype: List of `ResultSet` or mapping of `ResultSet` |
|
246 |
||
247 |
""" |
|
248 |
result = [] |
|
249 |
mapping = {} |
|
250 |
for idx, line in enumerate(self): |
|
251 |
if col >= 0: |
|
252 |
try: |
|
1132 | 253 |
key = self.get_entity(idx, col) |
0 | 254 |
except NotAnEntity: |
255 |
key = line[col] |
|
256 |
else: |
|
257 |
key = line |
|
258 |
if keyfunc is not None: |
|
259 |
key = keyfunc(key) |
|
260 |
||
261 |
if key not in mapping: |
|
262 |
rows, descr = [], [] |
|
3764
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
263 |
rset = self.copy(rows, descr) |
0 | 264 |
mapping[key] = rset |
265 |
result.append(rset) |
|
266 |
else: |
|
267 |
rset = mapping[key] |
|
268 |
rset.rows.append(self.rows[idx]) |
|
269 |
rset.description.append(self.description[idx]) |
|
270 |
for rset in result: |
|
271 |
rset.rowcount = len(rset.rows) |
|
272 |
if return_dict: |
|
273 |
return mapping |
|
274 |
else: |
|
275 |
return result |
|
276 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
277 |
def limited_rql(self): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
278 |
"""returns a printable rql for the result set associated to the object, |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
279 |
with limit/offset correctly set according to maximum page size and |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
280 |
currently displayed page when necessary |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
281 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
282 |
# try to get page boundaries from the navigation component |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
283 |
# XXX we should probably not have a ref to this component here (eg in |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
284 |
# cubicweb) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
285 |
nav = self.req.vreg['components'].select_or_none('navigation', self.req, |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
286 |
rset=self) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
287 |
if nav: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
288 |
start, stop = nav.page_boundaries() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
289 |
rql = self._limit_offset_rql(stop - start, start) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
290 |
# result set may have be limited manually in which case navigation won't |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
291 |
# apply |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
292 |
elif self.limited: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
293 |
rql = self._limit_offset_rql(*self.limited) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
294 |
# navigation component doesn't apply and rset has not been limited, no |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
295 |
# need to limit query |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
296 |
else: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
297 |
rql = self.printable_rql() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
298 |
return rql |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
299 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
300 |
def _limit_offset_rql(self, limit, offset): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
301 |
rqlst = self.syntax_tree() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
302 |
if len(rqlst.children) == 1: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
303 |
select = rqlst.children[0] |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
304 |
olimit, ooffset = select.limit, select.offset |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
305 |
select.limit, select.offset = limit, offset |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
306 |
rql = rqlst.as_string(kwargs=self.args) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
307 |
# restore original limit/offset |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
308 |
select.limit, select.offset = olimit, ooffset |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
309 |
else: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
310 |
newselect = stmts.Select() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
311 |
newselect.limit = limit |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
312 |
newselect.offset = offset |
4939
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
313 |
aliases = [nodes.VariableRef(newselect.get_variable(chr(65+i), i)) |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
314 |
for i in xrange(len(rqlst.children[0].selection))] |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
315 |
for vref in aliases: |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
316 |
newselect.append_selected(nodes.VariableRef(vref.variable)) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
317 |
newselect.set_with([nodes.SubQuery(aliases, rqlst)], check=False) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
318 |
newunion = stmts.Union() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
319 |
newunion.append(newselect) |
4939
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
320 |
rql = newunion.as_string(kwargs=self.args) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
321 |
rqlst.parent = None |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
322 |
return rql |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
323 |
|
0 | 324 |
def limit(self, limit, offset=0, inplace=False): |
325 |
"""limit the result set to the given number of rows optionaly starting |
|
326 |
from an index different than 0 |
|
327 |
||
328 |
:type limit: int |
|
329 |
:param limit: the maximum number of results |
|
330 |
||
331 |
:type offset: int |
|
332 |
:param offset: the offset index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
333 |
|
0 | 334 |
:type inplace: bool |
335 |
:param inplace: |
|
336 |
if true, the result set is modified in place, else a new result set |
|
337 |
is returned and the original is left unmodified |
|
338 |
||
339 |
:rtype: `ResultSet` |
|
340 |
""" |
|
341 |
stop = limit+offset |
|
342 |
rows = self.rows[offset:stop] |
|
343 |
descr = self.description[offset:stop] |
|
344 |
if inplace: |
|
345 |
rset = self |
|
346 |
rset.rows, rset.description = rows, descr |
|
347 |
rset.rowcount = len(rows) |
|
348 |
clear_cache(rset, 'description_struct') |
|
349 |
if offset: |
|
350 |
clear_cache(rset, 'get_entity') |
|
351 |
# we also have to fix/remove from the request entity cache entities |
|
352 |
# which get a wrong rset reference by this limit call |
|
353 |
for entity in self.req.cached_entities(): |
|
3379
9192ba07890d
use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3023
diff
changeset
|
354 |
if entity.cw_rset is self: |
4150
2835482b8daf
don't try to write .row/.col, use new cw_row/cw_col attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
355 |
if offset <= entity.cw_row < stop: |
2835482b8daf
don't try to write .row/.col, use new cw_row/cw_col attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
356 |
entity.cw_row = entity.cw_row - offset |
0 | 357 |
else: |
8048
10a0f73d834d
[rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
358 |
entity.cw_rset = entity.as_rset() |
10a0f73d834d
[rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
359 |
entity.cw_row = entity.cw_col = 0 |
0 | 360 |
else: |
3764
034aa14b740a
drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
361 |
rset = self.copy(rows, descr) |
0 | 362 |
if not offset: |
363 |
# can copy built entity caches |
|
364 |
copy_cache(rset, 'get_entity', self) |
|
365 |
rset.limited = (limit, offset) |
|
366 |
return rset |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
367 |
|
10313
ae9e23cf8790
[rset] Deprecate the 'encoded' argument to ResultSet.printable_rql()
Rémi Cardona <remi.cardona@free.fr>
parents:
10187
diff
changeset
|
368 |
def printable_rql(self, encoded=_MARKER): |
0 | 369 |
"""return the result set's origin rql as a string, with arguments |
370 |
substitued |
|
371 |
""" |
|
10313
ae9e23cf8790
[rset] Deprecate the 'encoded' argument to ResultSet.printable_rql()
Rémi Cardona <remi.cardona@free.fr>
parents:
10187
diff
changeset
|
372 |
if encoded is not _MARKER: |
ae9e23cf8790
[rset] Deprecate the 'encoded' argument to ResultSet.printable_rql()
Rémi Cardona <remi.cardona@free.fr>
parents:
10187
diff
changeset
|
373 |
warn('[3.21] the "encoded" argument is deprecated', DeprecationWarning) |
0 | 374 |
encoding = self.req.encoding |
10314
8f3783dc6358
Don't pass an encoding to rqlst.as_string()
Julien Cristau <julien.cristau@logilab.fr>
parents:
10313
diff
changeset
|
375 |
rqlstr = self.syntax_tree().as_string(kwargs=self.args) |
0 | 376 |
# sounds like we get encoded or unicode string due to a bug in as_string |
377 |
if not encoded: |
|
378 |
if isinstance(rqlstr, unicode): |
|
379 |
return rqlstr |
|
380 |
return unicode(rqlstr, encoding) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
381 |
else: |
0 | 382 |
if isinstance(rqlstr, unicode): |
383 |
return rqlstr.encode(encoding) |
|
384 |
return rqlstr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
385 |
|
0 | 386 |
# client helper methods ################################################### |
387 |
||
388 |
def entities(self, col=0): |
|
389 |
"""iter on entities with eid in the `col` column of the result set""" |
|
390 |
for i in xrange(len(self)): |
|
391 |
# may have None values in case of outer join (or aggregat on eid |
|
392 |
# hacks) |
|
393 |
if self.rows[i][col] is not None: |
|
394 |
yield self.get_entity(i, col) |
|
395 |
||
6857
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
396 |
def iter_rows_with_entities(self): |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
397 |
""" iterates over rows, and for each row |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
398 |
eids are converted to plain entities |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
399 |
""" |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
400 |
for i, row in enumerate(self): |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
401 |
_row = [] |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
402 |
for j, col in enumerate(row): |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
403 |
try: |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
404 |
_row.append(self.get_entity(i, j) if col is not None else col) |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
405 |
except NotAnEntity: |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
406 |
_row.append(col) |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
407 |
yield _row |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
408 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
409 |
def complete_entity(self, row, col=0, skip_bytes=True): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
410 |
"""short cut to get an completed entity instance for a particular |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
411 |
row (all instance's attributes have been fetched) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
412 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
413 |
entity = self.get_entity(row, col) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
414 |
entity.complete(skip_bytes=skip_bytes) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
415 |
return entity |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
416 |
|
0 | 417 |
@cached |
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
418 |
def get_entity(self, row, col): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
419 |
"""convenience method for query retrieving a single entity, returns a |
0 | 420 |
partially initialized Entity instance. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
421 |
|
5314
86e5abbebfaf
[doc/book] refresh the autoform section with uicfg content (moved there)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5312
diff
changeset
|
422 |
.. warning:: |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
423 |
|
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
424 |
Due to the cache wrapping this function, you should NEVER give row as |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
425 |
a named parameter (i.e. `rset.get_entity(0, 1)` is OK but |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
426 |
`rset.get_entity(row=0, col=1)` isn't) |
0 | 427 |
|
428 |
:type row,col: int, int |
|
429 |
:param row,col: |
|
430 |
row and col numbers localizing the entity among the result's table |
|
431 |
||
432 |
:return: the partially initialized `Entity` instance |
|
433 |
""" |
|
434 |
etype = self.description[row][col] |
|
435 |
try: |
|
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
436 |
eschema = self.req.vreg.schema.eschema(etype) |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3625
diff
changeset
|
437 |
if eschema.final: |
0 | 438 |
raise NotAnEntity(etype) |
439 |
except KeyError: |
|
440 |
raise NotAnEntity(etype) |
|
441 |
return self._build_entity(row, col) |
|
442 |
||
9347
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
443 |
def one(self, col=0): |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
444 |
"""Retrieve exactly one entity from the query. |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
445 |
|
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
446 |
If the result set is empty, raises :exc:`NoResultError`. |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
447 |
If the result set has more than one row, raises |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
448 |
:exc:`MultipleResultsError`. |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
449 |
|
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
450 |
:type col: int |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
451 |
:param col: The column localising the entity in the unique row |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
452 |
|
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
453 |
:return: the partially initialized `Entity` instance |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
454 |
""" |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
455 |
if len(self) == 1: |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
456 |
return self.get_entity(0, col) |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
457 |
elif len(self) == 0: |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
458 |
raise NoResultError("No row was found for one()") |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
459 |
else: |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
460 |
raise MultipleResultsError("Multiple rows were found for one()") |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
461 |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
462 |
def _build_entity(self, row, col): |
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
463 |
"""internal method to get a single entity, returns a partially |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
464 |
initialized Entity instance. |
0 | 465 |
|
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
466 |
partially means that only attributes selected in the RQL query will be |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
467 |
directly assigned to the entity. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
468 |
|
0 | 469 |
:type row,col: int, int |
470 |
:param row,col: |
|
471 |
row and col numbers localizing the entity among the result's table |
|
472 |
||
473 |
:return: the partially initialized `Entity` instance |
|
474 |
""" |
|
475 |
req = self.req |
|
476 |
if req is None: |
|
477 |
raise AssertionError('dont call get_entity with no req on the result set') |
|
478 |
rowvalues = self.rows[row] |
|
479 |
eid = rowvalues[col] |
|
480 |
assert eid is not None |
|
481 |
# return cached entity if exists. This also avoids potential recursion |
|
482 |
# XXX should we consider updating a cached entity with possible |
|
483 |
# new attributes found in this resultset ? |
|
484 |
try: |
|
2648
4ae7d02ce063
F [rset repo cache] set entity.rset when no set on entities retreived from the cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
485 |
entity = req.entity_cache(eid) |
0 | 486 |
except KeyError: |
487 |
pass |
|
2832
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
488 |
else: |
3379
9192ba07890d
use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3023
diff
changeset
|
489 |
if entity.cw_rset is None: |
2832
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
490 |
# entity has no rset set, this means entity has been created by |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
491 |
# the querier (req is a repository session) and so jas no rset |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
492 |
# info. Add it. |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
493 |
entity.cw_rset = self |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
494 |
entity.cw_row = row |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
495 |
entity.cw_col = col |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
496 |
return entity |
0 | 497 |
# build entity instance |
498 |
etype = self.description[row][col] |
|
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
499 |
entity = self.req.vreg['etypes'].etype_class(etype)(req, rset=self, |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
500 |
row=row, col=col) |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5532
diff
changeset
|
501 |
entity.eid = eid |
0 | 502 |
# cache entity |
503 |
req.set_entity_cache(entity) |
|
504 |
# try to complete the entity if there are some additional columns |
|
505 |
if len(rowvalues) > 1: |
|
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
506 |
eschema = entity.e_schema |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
507 |
eid_col, attr_cols, rel_cols = self._rset_structure(eschema, col) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
508 |
entity.eid = rowvalues[eid_col] |
7968
92303e2ed77a
[rset] use .iteritems() instead of items()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
509 |
for attr, col_idx in attr_cols.iteritems(): |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
510 |
entity.cw_attr_cache[attr] = rowvalues[col_idx] |
7968
92303e2ed77a
[rset] use .iteritems() instead of items()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
511 |
for (rtype, role), col_idx in rel_cols.iteritems(): |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
512 |
value = rowvalues[col_idx] |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
513 |
if value is None: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
514 |
if role == 'subject': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
515 |
rql = 'Any Y WHERE X %s Y, X eid %s' |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
516 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
517 |
rql = 'Any Y WHERE Y %s X, X eid %s' |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
518 |
rrset = ResultSet([], rql % (rtype, entity.eid)) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
519 |
rrset.req = req |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
520 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
521 |
rrset = self._build_entity(row, col_idx).as_rset() |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
522 |
entity.cw_set_relation_cache(rtype, role, rrset) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
523 |
return entity |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
524 |
|
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
525 |
@cached |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
526 |
def _rset_structure(self, eschema, entity_col): |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
527 |
eid_col = col = entity_col |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
528 |
rqlst = self.syntax_tree() |
10180
ea10572fccfe
[rset] fix crash on displaying rset where some symmetric relation is used. Closes #4739253
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9820
diff
changeset
|
529 |
get_rschema = eschema.schema.rschema |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
530 |
attr_cols = {} |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
531 |
rel_cols = {} |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
532 |
if rqlst.TYPE == 'select': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
533 |
# UNION query, find the subquery from which this entity has been |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
534 |
# found |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
535 |
select, col = rqlst.locate_subquery(entity_col, eschema.type, self.args) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
536 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
537 |
select = rqlst |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
538 |
# take care, due to outer join support, we may find None |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
539 |
# values for non final relation |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
540 |
for i, attr, role in attr_desc_iterator(select, col, entity_col): |
10180
ea10572fccfe
[rset] fix crash on displaying rset where some symmetric relation is used. Closes #4739253
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9820
diff
changeset
|
541 |
rschema = get_rschema(attr) |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
542 |
if rschema.final: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
543 |
if attr == 'eid': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
544 |
eid_col = i |
0 | 545 |
else: |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
546 |
attr_cols[attr] = i |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
547 |
else: |
9820
3ad221fe04aa
[entity/rset] replace scary warnings about ambiguous rdefs by XXX in the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9780
diff
changeset
|
548 |
# XXX takefirst=True to remove warning triggered by ambiguous relations |
3ad221fe04aa
[entity/rset] replace scary warnings about ambiguous rdefs by XXX in the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9780
diff
changeset
|
549 |
rdef = eschema.rdef(attr, role, takefirst=True) |
0 | 550 |
# only keep value if it can't be multivalued |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3766
diff
changeset
|
551 |
if rdef.role_cardinality(role) in '1?': |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
552 |
rel_cols[(attr, role)] = i |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
553 |
return eid_col, attr_cols, rel_cols |
0 | 554 |
|
555 |
@cached |
|
556 |
def syntax_tree(self): |
|
10087
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
557 |
"""return the syntax tree (:class:`rql.stmts.Union`) for the |
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
558 |
originating query. You can expect it to have solutions |
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
559 |
computed and it will be properly annotated. |
0 | 560 |
""" |
10087
ed0b076c119b
[rset] kill the rset._rqlst cache
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9820
diff
changeset
|
561 |
return self.req.vreg.parse(self.req, self.rql, self.args) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
562 |
|
0 | 563 |
@cached |
564 |
def column_types(self, col): |
|
565 |
"""return the list of different types in the column with the given col |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
566 |
|
0 | 567 |
:type col: int |
568 |
:param col: the index of the desired column |
|
569 |
||
570 |
:rtype: list |
|
571 |
:return: the different entities type found in the column |
|
572 |
""" |
|
573 |
return frozenset(struc[-1][col] for struc in self.description_struct()) |
|
574 |
||
575 |
@cached |
|
576 |
def description_struct(self): |
|
577 |
"""return a list describing sequence of results with the same |
|
578 |
description, e.g. : |
|
579 |
[[0, 4, ('Bug',)] |
|
580 |
[[0, 4, ('Bug',), [5, 8, ('Story',)] |
|
581 |
[[0, 3, ('Project', 'Version',)]] |
|
582 |
""" |
|
583 |
result = [] |
|
584 |
last = None |
|
585 |
for i, row in enumerate(self.description): |
|
586 |
if row != last: |
|
587 |
if last is not None: |
|
588 |
result[-1][1] = i - 1 |
|
589 |
result.append( [i, None, row] ) |
|
590 |
last = row |
|
591 |
if last is not None: |
|
592 |
result[-1][1] = i |
|
593 |
return result |
|
594 |
||
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
595 |
def _locate_query_params(self, rqlst, row, col): |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
596 |
locate_query_col = col |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
597 |
etype = self.description[row][col] |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
598 |
# final type, find a better one to locate the correct subquery |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
599 |
# (ambiguous if possible) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
600 |
eschema = self.req.vreg.schema.eschema |
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
601 |
if eschema(etype).final: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
602 |
for select in rqlst.children: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
603 |
try: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
604 |
myvar = select.selection[col].variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
605 |
except AttributeError: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
606 |
# not a variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
607 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
608 |
for i in xrange(len(select.selection)): |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
609 |
if i == col: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
610 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
611 |
coletype = self.description[row][i] |
6196 | 612 |
# None description possible on column resulting from an |
613 |
# outer join |
|
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
614 |
if coletype is None or eschema(coletype).final: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
615 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
616 |
try: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
617 |
ivar = select.selection[i].variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
618 |
except AttributeError: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
619 |
# not a variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
620 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
621 |
# check variables don't comes from a subquery or are both |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
622 |
# coming from the same subquery |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
623 |
if getattr(ivar, 'query', None) is getattr(myvar, 'query', None): |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
624 |
etype = coletype |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
625 |
locate_query_col = i |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
626 |
if len(self.column_types(i)) > 1: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
627 |
return etype, locate_query_col |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
628 |
return etype, locate_query_col |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
629 |
|
0 | 630 |
@cached |
631 |
def related_entity(self, row, col): |
|
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
632 |
"""given an cell of the result set, try to return a (entity, relation |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
633 |
name) tuple to which this cell is linked. |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
634 |
|
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
635 |
This is especially useful when the cell is an attribute of an entity, |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
636 |
to get the entity to which this attribute belongs to. |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
637 |
""" |
0 | 638 |
rqlst = self.syntax_tree() |
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
639 |
# UNION query, we've first to find a 'pivot' column to use to get the |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
640 |
# actual query from which the row is coming |
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
641 |
etype, locate_query_col = self._locate_query_params(rqlst, row, col) |
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
642 |
# now find the query from which this entity has been found. Returned |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
643 |
# select node may be a subquery with different column indexes. |
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
644 |
select = rqlst.locate_subquery(locate_query_col, etype, self.args)[0] |
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
645 |
# then get the index of root query's col in the subquery |
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
646 |
col = rqlst.subquery_selection_index(select, col) |
3766
9e5dc4b1ada4
work around a pb. with subquery_selection_index
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3764
diff
changeset
|
647 |
if col is None: |
9e5dc4b1ada4
work around a pb. with subquery_selection_index
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3764
diff
changeset
|
648 |
# XXX unexpected, should fix subquery_selection_index ? |
9e5dc4b1ada4
work around a pb. with subquery_selection_index
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3764
diff
changeset
|
649 |
return None, None |
0 | 650 |
try: |
651 |
myvar = select.selection[col].variable |
|
652 |
except AttributeError: |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
170
diff
changeset
|
653 |
# not a variable |
0 | 654 |
return None, None |
655 |
rel = myvar.main_relation() |
|
656 |
if rel is not None: |
|
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
657 |
index = rel.children[0].root_selection_index() |
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
170
diff
changeset
|
658 |
if index is not None and self.rows[row][index]: |
5667
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
659 |
try: |
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
660 |
entity = self.get_entity(row, index) |
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
661 |
return entity, rel.r_type |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8087
diff
changeset
|
662 |
except NotAnEntity as exc: |
5667
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
663 |
return None, None |
0 | 664 |
return None, None |
665 |
||
666 |
@cached |
|
667 |
def searched_text(self): |
|
668 |
"""returns the searched text in case of full-text search |
|
669 |
||
670 |
:return: searched text or `None` if the query is not |
|
671 |
a full-text query |
|
672 |
""" |
|
673 |
rqlst = self.syntax_tree() |
|
674 |
for rel in rqlst.iget_nodes(nodes.Relation): |
|
675 |
if rel.r_type == 'has_text': |
|
676 |
__, rhs = rel.get_variable_parts() |
|
677 |
return rhs.eval(self.args) |
|
678 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
679 |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
680 |
def _get_variable(term): |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
681 |
# XXX rewritten const |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
682 |
# use iget_nodes for (hack) case where we have things like MAX(V) |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
683 |
for vref in term.iget_nodes(nodes.VariableRef): |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
684 |
return vref.variable |
0 | 685 |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
686 |
def attr_desc_iterator(select, selectidx, rootidx): |
0 | 687 |
"""return an iterator on a list of 2-uple (index, attr_relation) |
688 |
localizing attribute relations of the main variable in a result's row |
|
689 |
||
690 |
:type rqlst: rql.stmts.Select |
|
691 |
:param rqlst: the RQL syntax tree to describe |
|
692 |
||
693 |
:return: |
|
694 |
a generator on (index, relation, target) describing column being |
|
695 |
attribute of the main variable |
|
696 |
""" |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
697 |
rootselect = select |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
698 |
while rootselect.parent.parent is not None: |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
699 |
rootselect = rootselect.parent.parent.parent |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
700 |
rootmain = rootselect.selection[selectidx] |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
701 |
rootmainvar = _get_variable(rootmain) |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
702 |
assert rootmainvar |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
703 |
root = rootselect.parent |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
704 |
selectmain = select.selection[selectidx] |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
705 |
for i, term in enumerate(rootselect.selection): |
7395
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
706 |
try: |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
707 |
# don't use _get_variable here: if the term isn't a variable |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
708 |
# (function...), we don't want it to be used as an entity attribute |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
709 |
# or relation's value (XXX beside MAX/MIN trick?) |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
710 |
rootvar = term.variable |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
711 |
except AttributeError: |
0 | 712 |
continue |
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
713 |
if rootvar.name == rootmainvar.name: |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
714 |
continue |
8085
51929d531aff
[rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8048
diff
changeset
|
715 |
if select is not rootselect and isinstance(rootvar, nodes.ColumnAlias): |
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
716 |
term = select.selection[root.subquery_selection_index(select, i)] |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
717 |
var = _get_variable(term) |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
718 |
if var is None: |
0 | 719 |
continue |
720 |
for ref in var.references(): |
|
721 |
rel = ref.relation() |
|
722 |
if rel is None or rel.is_types_restriction(): |
|
723 |
continue |
|
724 |
lhs, rhs = rel.get_variable_parts() |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
725 |
if selectmain.is_equivalent(lhs): |
0 | 726 |
if rhs.is_equivalent(term): |
727 |
yield (i, rel.r_type, 'subject') |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
728 |
elif selectmain.is_equivalent(rhs): |
0 | 729 |
if lhs.is_equivalent(term): |
730 |
yield (i, rel.r_type, 'object') |