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