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