author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 31 Jul 2013 13:56:00 +0200 | |
branch | stable |
changeset 9206 | bf642b50135b |
parent 8695 | 358d8bed9626 |
child 9281 | 49a7392bb5b5 |
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 |
||
22 |
from logilab.common.decorators import cached, clear_cache, copy_cache |
|
23 |
||
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
|
24 |
from rql import nodes, stmts |
0 | 25 |
|
26 |
from cubicweb import NotAnEntity |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
27 |
|
0 | 28 |
|
29 |
class ResultSet(object): |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
30 |
"""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
|
31 |
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
|
32 |
result rows. |
0 | 33 |
|
34 |
:type rowcount: int |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
35 |
:param rowcount: number of rows in the result |
0 | 36 |
|
37 |
:type rows: list |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
38 |
:param rows: list of rows of result |
0 | 39 |
|
40 |
:type description: list |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
41 |
:param description: |
0 | 42 |
result's description, using the same structure as the result itself |
43 |
||
44 |
:type rql: str or unicode |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
45 |
:param rql: the original RQL query string |
0 | 46 |
""" |
5740
46207f491a51
[rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5671
diff
changeset
|
47 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
48 |
def __init__(self, results, rql, args=None, description=(), rqlst=None): |
0 | 49 |
self.rows = results |
50 |
self.rowcount = results and len(results) or 0 |
|
51 |
# original query and arguments |
|
52 |
self.rql = rql |
|
53 |
self.args = args |
|
54 |
# entity types for each cell (same shape as rows) |
|
55 |
# maybe discarded if specified when the query has been executed |
|
56 |
self.description = description |
|
57 |
# parsed syntax tree |
|
58 |
if rqlst is not None: |
|
59 |
rqlst.schema = None # reset schema in case of pyro transfert |
|
60 |
self._rqlst = rqlst |
|
61 |
# set to (limit, offset) when a result set is limited using the |
|
62 |
# .limit method |
|
63 |
self.limited = None |
|
64 |
# set by the cursor which returned this resultset |
|
65 |
self.req = None |
|
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
66 |
# actions cache |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
67 |
self._rsetactions = None |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
68 |
|
0 | 69 |
def __str__(self): |
70 |
if not self.rows: |
|
71 |
return '<empty resultset %s>' % self.rql |
|
72 |
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
|
73 |
|
0 | 74 |
def __repr__(self): |
75 |
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
|
76 |
return '<empty resultset for %r>' % self.rql |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
77 |
rows = self.rows |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
78 |
if len(rows) > 10: |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
79 |
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
|
80 |
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
|
81 |
# 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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
|
0 | 86 |
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
|
87 |
return pattern % (self.rql, len(self.rows), |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
88 |
'\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
|
89 |
return pattern % (self.rql, len(self.rows), |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
90 |
'\n'.join('%s (%s)' % (r, d) |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
91 |
for r, d in zip(rows, self.description))) |
0 | 92 |
|
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
93 |
def possible_actions(self, **kwargs): |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
94 |
if self._rsetactions is None: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
95 |
self._rsetactions = {} |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
96 |
if kwargs: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
97 |
key = tuple(sorted(kwargs.iteritems())) |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
98 |
else: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
99 |
key = None |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
100 |
try: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
101 |
return self._rsetactions[key] |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
self.req, rset=self, **kwargs) |
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
105 |
self._rsetactions[key] = actions |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
106 |
return actions |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
107 |
|
0 | 108 |
def __len__(self): |
109 |
"""returns the result set's size""" |
|
110 |
return self.rowcount |
|
111 |
||
112 |
def __nonzero__(self): |
|
113 |
return self.rowcount |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
114 |
|
0 | 115 |
def __getitem__(self, i): |
116 |
"""returns the ith element of the result set""" |
|
117 |
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
|
118 |
|
0 | 119 |
def __getslice__(self, i, j): |
120 |
"""returns slice [i:j] of the result set""" |
|
121 |
return self.rows[i:j] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
122 |
|
0 | 123 |
def __iter__(self): |
124 |
"""Returns an iterator over rows""" |
|
125 |
return iter(self.rows) |
|
126 |
||
127 |
def __add__(self, rset): |
|
128 |
# XXX buggy implementation (.rql and .args attributes at least much |
|
129 |
# probably differ) |
|
130 |
# at least rql could be fixed now that we have union and sub-queries |
|
131 |
# but I tend to think that since we have that, we should not need this |
|
132 |
# method anymore (syt) |
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
return rset |
0 | 137 |
|
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
descr = self.description[:] |
0 | 142 |
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
|
143 |
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
|
144 |
return rset |
0 | 145 |
|
146 |
def transformed_rset(self, transformcb): |
|
147 |
""" the result set according to a given column types |
|
148 |
||
149 |
:type transormcb: callable(row, desc) |
|
150 |
:param transformcb: |
|
151 |
a callable which should take a row and its type description as |
|
152 |
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
|
153 |
|
0 | 154 |
|
155 |
:type col: int |
|
156 |
:param col: the column index |
|
157 |
||
158 |
:rtype: `ResultSet` |
|
159 |
""" |
|
160 |
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
|
161 |
rset = self.copy(rows, descr) |
0 | 162 |
for row, desc in zip(self.rows, self.description): |
163 |
nrow, ndesc = transformcb(row, desc) |
|
164 |
if ndesc: # transformcb returns None for ndesc to skip that row |
|
165 |
rows.append(nrow) |
|
166 |
descr.append(ndesc) |
|
167 |
rset.rowcount = len(rows) |
|
168 |
return rset |
|
169 |
||
170 |
def filtered_rset(self, filtercb, col=0): |
|
171 |
"""filter the result set according to a given filtercb |
|
172 |
||
173 |
:type filtercb: callable(entity) |
|
174 |
:param filtercb: |
|
175 |
a callable which should take an entity as argument and return |
|
176 |
False if it should be skipped, else True |
|
177 |
||
178 |
:type col: int |
|
179 |
:param col: the column index |
|
180 |
||
181 |
:rtype: `ResultSet` |
|
182 |
""" |
|
183 |
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
|
184 |
rset = self.copy(rows, descr) |
0 | 185 |
for i in xrange(len(self)): |
186 |
if not filtercb(self.get_entity(i, col)): |
|
187 |
continue |
|
188 |
rows.append(self.rows[i]) |
|
189 |
descr.append(self.description[i]) |
|
190 |
rset.rowcount = len(rows) |
|
191 |
return rset |
|
192 |
||
193 |
||
194 |
def sorted_rset(self, keyfunc, reverse=False, col=0): |
|
195 |
"""sorts the result set according to a given keyfunc |
|
196 |
||
197 |
:type keyfunc: callable(entity) |
|
198 |
:param keyfunc: |
|
199 |
a callable which should take an entity as argument and return |
|
200 |
the value used to compare and sort |
|
201 |
||
202 |
:type reverse: bool |
|
203 |
:param reverse: if the result should be reversed |
|
204 |
||
205 |
:type col: int |
|
206 |
:param col: the column index. if col = -1, the whole row are used |
|
207 |
||
208 |
:rtype: `ResultSet` |
|
209 |
""" |
|
210 |
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
|
211 |
rset = self.copy(rows, descr) |
0 | 212 |
if col >= 0: |
213 |
entities = sorted(enumerate(self.entities(col)), |
|
214 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
215 |
else: |
|
216 |
entities = sorted(enumerate(self), |
|
217 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
1132 | 218 |
for index, _ in entities: |
0 | 219 |
rows.append(self.rows[index]) |
220 |
descr.append(self.description[index]) |
|
221 |
rset.rowcount = len(rows) |
|
222 |
return rset |
|
223 |
||
224 |
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
|
225 |
"""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
|
226 |
a given key |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
227 |
|
0 | 228 |
:type keyfunc: callable(entity or FinalType) |
229 |
:param keyfunc: |
|
230 |
a callable which should take a value of the rset in argument and |
|
231 |
return the value used to group the value. If not define, raw value |
|
232 |
of the specified columns is used. |
|
233 |
||
234 |
:type col: int |
|
235 |
:param col: the column index. if col = -1, the whole row are used |
|
236 |
||
237 |
:type return_dict: Boolean |
|
238 |
:param return_dict: If true, the function return a mapping |
|
239 |
(key -> rset) instead of a list of rset |
|
240 |
||
241 |
:rtype: List of `ResultSet` or mapping of `ResultSet` |
|
242 |
||
243 |
""" |
|
244 |
result = [] |
|
245 |
mapping = {} |
|
246 |
for idx, line in enumerate(self): |
|
247 |
if col >= 0: |
|
248 |
try: |
|
1132 | 249 |
key = self.get_entity(idx, col) |
0 | 250 |
except NotAnEntity: |
251 |
key = line[col] |
|
252 |
else: |
|
253 |
key = line |
|
254 |
if keyfunc is not None: |
|
255 |
key = keyfunc(key) |
|
256 |
||
257 |
if key not in mapping: |
|
258 |
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
|
259 |
rset = self.copy(rows, descr) |
0 | 260 |
mapping[key] = rset |
261 |
result.append(rset) |
|
262 |
else: |
|
263 |
rset = mapping[key] |
|
264 |
rset.rows.append(self.rows[idx]) |
|
265 |
rset.description.append(self.description[idx]) |
|
266 |
for rset in result: |
|
267 |
rset.rowcount = len(rset.rows) |
|
268 |
if return_dict: |
|
269 |
return mapping |
|
270 |
else: |
|
271 |
return result |
|
272 |
||
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
|
273 |
def limited_rql(self): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
274 |
"""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
|
275 |
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
|
276 |
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
|
277 |
""" |
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
|
278 |
# 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
|
279 |
# 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
|
280 |
# 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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
# 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
|
287 |
# 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
|
288 |
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
|
289 |
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
|
290 |
# 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
|
291 |
# 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
|
292 |
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
|
293 |
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
|
294 |
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
|
295 |
|
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 |
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
|
297 |
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
|
298 |
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
|
299 |
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
|
300 |
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
|
301 |
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
|
302 |
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
|
303 |
# 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
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
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
|
308 |
newselect.offset = offset |
4939
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
309 |
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
|
310 |
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
|
311 |
for vref in aliases: |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
newunion.append(newselect) |
4939
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
316 |
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
|
317 |
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
|
318 |
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
|
319 |
|
0 | 320 |
def limit(self, limit, offset=0, inplace=False): |
321 |
"""limit the result set to the given number of rows optionaly starting |
|
322 |
from an index different than 0 |
|
323 |
||
324 |
:type limit: int |
|
325 |
:param limit: the maximum number of results |
|
326 |
||
327 |
:type offset: int |
|
328 |
:param offset: the offset index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
329 |
|
0 | 330 |
:type inplace: bool |
331 |
:param inplace: |
|
332 |
if true, the result set is modified in place, else a new result set |
|
333 |
is returned and the original is left unmodified |
|
334 |
||
335 |
:rtype: `ResultSet` |
|
336 |
""" |
|
337 |
stop = limit+offset |
|
338 |
rows = self.rows[offset:stop] |
|
339 |
descr = self.description[offset:stop] |
|
340 |
if inplace: |
|
341 |
rset = self |
|
342 |
rset.rows, rset.description = rows, descr |
|
343 |
rset.rowcount = len(rows) |
|
344 |
clear_cache(rset, 'description_struct') |
|
345 |
if offset: |
|
346 |
clear_cache(rset, 'get_entity') |
|
347 |
# we also have to fix/remove from the request entity cache entities |
|
348 |
# which get a wrong rset reference by this limit call |
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
entity.cw_row = entity.cw_row - offset |
0 | 353 |
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
|
354 |
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
|
355 |
entity.cw_row = entity.cw_col = 0 |
0 | 356 |
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
|
357 |
rset = self.copy(rows, descr) |
0 | 358 |
if not offset: |
359 |
# can copy built entity caches |
|
360 |
copy_cache(rset, 'get_entity', self) |
|
361 |
rset.limited = (limit, offset) |
|
362 |
return rset |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
363 |
|
0 | 364 |
def printable_rql(self, encoded=False): |
365 |
"""return the result set's origin rql as a string, with arguments |
|
366 |
substitued |
|
367 |
""" |
|
368 |
encoding = self.req.encoding |
|
369 |
rqlstr = self.syntax_tree().as_string(encoding, self.args) |
|
370 |
# sounds like we get encoded or unicode string due to a bug in as_string |
|
371 |
if not encoded: |
|
372 |
if isinstance(rqlstr, unicode): |
|
373 |
return rqlstr |
|
374 |
return unicode(rqlstr, encoding) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
375 |
else: |
0 | 376 |
if isinstance(rqlstr, unicode): |
377 |
return rqlstr.encode(encoding) |
|
378 |
return rqlstr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
379 |
|
0 | 380 |
# client helper methods ################################################### |
381 |
||
382 |
def entities(self, col=0): |
|
383 |
"""iter on entities with eid in the `col` column of the result set""" |
|
384 |
for i in xrange(len(self)): |
|
385 |
# may have None values in case of outer join (or aggregat on eid |
|
386 |
# hacks) |
|
387 |
if self.rows[i][col] is not None: |
|
388 |
yield self.get_entity(i, col) |
|
389 |
||
6857
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
390 |
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
|
391 |
""" 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
|
392 |
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
|
393 |
""" |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
394 |
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
|
395 |
_row = [] |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
396 |
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
|
397 |
try: |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
398 |
_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
|
399 |
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
|
400 |
_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
|
401 |
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
|
402 |
|
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
|
403 |
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
|
404 |
"""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
|
405 |
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
|
406 |
""" |
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
|
407 |
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
|
408 |
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
|
409 |
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
|
410 |
|
0 | 411 |
@cached |
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
412 |
def get_entity(self, row, col): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
413 |
"""convenience method for query retrieving a single entity, returns a |
0 | 414 |
partially initialized Entity instance. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
415 |
|
5314
86e5abbebfaf
[doc/book] refresh the autoform section with uicfg content (moved there)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5312
diff
changeset
|
416 |
.. warning:: |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
417 |
|
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
418 |
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
|
419 |
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
|
420 |
`rset.get_entity(row=0, col=1)` isn't) |
0 | 421 |
|
422 |
:type row,col: int, int |
|
423 |
:param row,col: |
|
424 |
row and col numbers localizing the entity among the result's table |
|
425 |
||
426 |
:return: the partially initialized `Entity` instance |
|
427 |
""" |
|
428 |
etype = self.description[row][col] |
|
429 |
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
|
430 |
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
|
431 |
if eschema.final: |
0 | 432 |
raise NotAnEntity(etype) |
433 |
except KeyError: |
|
434 |
raise NotAnEntity(etype) |
|
435 |
return self._build_entity(row, col) |
|
436 |
||
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
|
437 |
def _build_entity(self, row, col): |
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
438 |
"""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
|
439 |
initialized Entity instance. |
0 | 440 |
|
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
441 |
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
|
442 |
directly assigned to the entity. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
443 |
|
0 | 444 |
:type row,col: int, int |
445 |
:param row,col: |
|
446 |
row and col numbers localizing the entity among the result's table |
|
447 |
||
448 |
:return: the partially initialized `Entity` instance |
|
449 |
""" |
|
450 |
req = self.req |
|
451 |
if req is None: |
|
452 |
raise AssertionError('dont call get_entity with no req on the result set') |
|
453 |
rowvalues = self.rows[row] |
|
454 |
eid = rowvalues[col] |
|
455 |
assert eid is not None |
|
456 |
# return cached entity if exists. This also avoids potential recursion |
|
457 |
# XXX should we consider updating a cached entity with possible |
|
458 |
# new attributes found in this resultset ? |
|
459 |
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
|
460 |
entity = req.entity_cache(eid) |
0 | 461 |
except KeyError: |
462 |
pass |
|
2832
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
463 |
else: |
3379
9192ba07890d
use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3023
diff
changeset
|
464 |
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
|
465 |
# 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
|
466 |
# 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
|
467 |
# info. Add it. |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
468 |
entity.cw_rset = self |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
469 |
entity.cw_row = row |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
470 |
entity.cw_col = col |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
471 |
return entity |
0 | 472 |
# build entity instance |
473 |
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
|
474 |
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
|
475 |
row=row, col=col) |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5532
diff
changeset
|
476 |
entity.eid = eid |
0 | 477 |
# cache entity |
478 |
req.set_entity_cache(entity) |
|
479 |
# try to complete the entity if there are some additional columns |
|
480 |
if len(rowvalues) > 1: |
|
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
481 |
eschema = entity.e_schema |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
482 |
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
|
483 |
entity.eid = rowvalues[eid_col] |
7968
92303e2ed77a
[rset] use .iteritems() instead of items()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
484 |
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
|
485 |
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
|
486 |
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
|
487 |
value = rowvalues[col_idx] |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
488 |
if value is None: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
489 |
if role == 'subject': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
490 |
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
|
491 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
492 |
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
|
493 |
rrset = ResultSet([], rql % (rtype, entity.eid)) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
494 |
rrset.req = req |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
495 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
496 |
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
|
497 |
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
|
498 |
return entity |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
499 |
|
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
500 |
@cached |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
501 |
def _rset_structure(self, eschema, entity_col): |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
502 |
eid_col = col = entity_col |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
503 |
rqlst = self.syntax_tree() |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
504 |
attr_cols = {} |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
505 |
rel_cols = {} |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
506 |
if rqlst.TYPE == 'select': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
507 |
# 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
|
508 |
# found |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
509 |
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
|
510 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
511 |
select = rqlst |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
512 |
# 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
|
513 |
# values for non final relation |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
514 |
for i, attr, role in attr_desc_iterator(select, col, entity_col): |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
515 |
if role == 'subject': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
516 |
rschema = eschema.subjrels[attr] |
3625
f03b55a18c43
fix bug w/ get_entity and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3551
diff
changeset
|
517 |
else: |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
518 |
rschema = eschema.objrels[attr] |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
519 |
if rschema.final: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
520 |
if attr == 'eid': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
521 |
eid_col = i |
0 | 522 |
else: |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
523 |
attr_cols[attr] = i |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
524 |
else: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3766
diff
changeset
|
525 |
rdef = eschema.rdef(attr, role) |
0 | 526 |
# only keep value if it can't be multivalued |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3766
diff
changeset
|
527 |
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
|
528 |
rel_cols[(attr, role)] = i |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
529 |
return eid_col, attr_cols, rel_cols |
0 | 530 |
|
531 |
@cached |
|
532 |
def syntax_tree(self): |
|
7592
c84436b79793
[rset] syntax_tree impl. consistency: don't return an annotated syntax tree
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7395
diff
changeset
|
533 |
"""return the syntax tree (:class:`rql.stmts.Union`) for the originating |
c84436b79793
[rset] syntax_tree impl. consistency: don't return an annotated syntax tree
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7395
diff
changeset
|
534 |
query. You can expect it to have solutions computed but it won't be |
c84436b79793
[rset] syntax_tree impl. consistency: don't return an annotated syntax tree
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7395
diff
changeset
|
535 |
annotated (you usually don't need that for simple introspection). |
0 | 536 |
""" |
537 |
if self._rqlst: |
|
538 |
rqlst = self._rqlst.copy() |
|
539 |
# to avoid transport overhead when pyro is used, the schema has been |
|
540 |
# unset from the syntax tree |
|
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
|
541 |
rqlst.schema = self.req.vreg.schema |
0 | 542 |
else: |
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
|
543 |
rqlst = self.req.vreg.parse(self.req, self.rql, self.args) |
0 | 544 |
return rqlst |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
545 |
|
0 | 546 |
@cached |
547 |
def column_types(self, col): |
|
548 |
"""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
|
549 |
|
0 | 550 |
:type col: int |
551 |
:param col: the index of the desired column |
|
552 |
||
553 |
:rtype: list |
|
554 |
:return: the different entities type found in the column |
|
555 |
""" |
|
556 |
return frozenset(struc[-1][col] for struc in self.description_struct()) |
|
557 |
||
558 |
@cached |
|
559 |
def description_struct(self): |
|
560 |
"""return a list describing sequence of results with the same |
|
561 |
description, e.g. : |
|
562 |
[[0, 4, ('Bug',)] |
|
563 |
[[0, 4, ('Bug',), [5, 8, ('Story',)] |
|
564 |
[[0, 3, ('Project', 'Version',)]] |
|
565 |
""" |
|
566 |
result = [] |
|
567 |
last = None |
|
568 |
for i, row in enumerate(self.description): |
|
569 |
if row != last: |
|
570 |
if last is not None: |
|
571 |
result[-1][1] = i - 1 |
|
572 |
result.append( [i, None, row] ) |
|
573 |
last = row |
|
574 |
if last is not None: |
|
575 |
result[-1][1] = i |
|
576 |
return result |
|
577 |
||
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
|
578 |
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
|
579 |
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
|
580 |
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
|
581 |
# 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
|
582 |
# (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
|
583 |
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
|
584 |
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
|
585 |
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
|
586 |
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
|
587 |
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
|
588 |
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
|
589 |
# 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
|
590 |
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
|
591 |
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
|
592 |
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
|
593 |
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
|
594 |
coletype = self.description[row][i] |
6196 | 595 |
# None description possible on column resulting from an |
596 |
# 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
|
597 |
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
|
598 |
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
|
599 |
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
|
600 |
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
|
601 |
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
|
602 |
# 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
|
603 |
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
|
604 |
# 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
|
605 |
# 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
|
606 |
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
|
607 |
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
|
608 |
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
|
609 |
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
|
610 |
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
|
611 |
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
|
612 |
|
0 | 613 |
@cached |
614 |
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
|
615 |
"""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
|
616 |
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
|
617 |
|
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
|
618 |
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
|
619 |
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
|
620 |
""" |
0 | 621 |
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
|
622 |
# 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
|
623 |
# 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
|
624 |
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
|
625 |
# 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
|
626 |
# 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
|
627 |
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
|
628 |
# 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
|
629 |
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
|
630 |
if col is None: |
9e5dc4b1ada4
work around a pb. with subquery_selection_index
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3764
diff
changeset
|
631 |
# 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
|
632 |
return None, None |
0 | 633 |
try: |
634 |
myvar = select.selection[col].variable |
|
635 |
except AttributeError: |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
170
diff
changeset
|
636 |
# not a variable |
0 | 637 |
return None, None |
638 |
rel = myvar.main_relation() |
|
639 |
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
|
640 |
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
|
641 |
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
|
642 |
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
|
643 |
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
|
644 |
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
|
645 |
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
|
646 |
return None, None |
0 | 647 |
return None, None |
648 |
||
649 |
@cached |
|
650 |
def searched_text(self): |
|
651 |
"""returns the searched text in case of full-text search |
|
652 |
||
653 |
:return: searched text or `None` if the query is not |
|
654 |
a full-text query |
|
655 |
""" |
|
656 |
rqlst = self.syntax_tree() |
|
657 |
for rel in rqlst.iget_nodes(nodes.Relation): |
|
658 |
if rel.r_type == 'has_text': |
|
659 |
__, rhs = rel.get_variable_parts() |
|
660 |
return rhs.eval(self.args) |
|
661 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
662 |
|
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
|
663 |
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
|
664 |
# 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
|
665 |
# 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
|
666 |
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
|
667 |
return vref.variable |
0 | 668 |
|
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
|
669 |
def attr_desc_iterator(select, selectidx, rootidx): |
0 | 670 |
"""return an iterator on a list of 2-uple (index, attr_relation) |
671 |
localizing attribute relations of the main variable in a result's row |
|
672 |
||
673 |
:type rqlst: rql.stmts.Select |
|
674 |
:param rqlst: the RQL syntax tree to describe |
|
675 |
||
676 |
:return: |
|
677 |
a generator on (index, relation, target) describing column being |
|
678 |
attribute of the main variable |
|
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 |
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
|
681 |
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
|
682 |
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
|
683 |
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
|
684 |
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
|
685 |
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
|
686 |
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
|
687 |
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
|
688 |
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
|
689 |
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
|
690 |
# 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
|
691 |
# (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
|
692 |
# 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
|
693 |
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
|
694 |
except AttributeError: |
0 | 695 |
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
|
696 |
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
|
697 |
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
|
698 |
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
|
699 |
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
|
700 |
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
|
701 |
if var is None: |
0 | 702 |
continue |
703 |
for ref in var.references(): |
|
704 |
rel = ref.relation() |
|
705 |
if rel is None or rel.is_types_restriction(): |
|
706 |
continue |
|
707 |
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
|
708 |
if selectmain.is_equivalent(lhs): |
0 | 709 |
if rhs.is_equivalent(term): |
710 |
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
|
711 |
elif selectmain.is_equivalent(rhs): |
0 | 712 |
if lhs.is_equivalent(term): |
713 |
yield (i, rel.r_type, 'object') |