author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Tue, 06 Oct 2009 16:56:22 +0200 | |
branch | stable |
changeset 3563 | f1dbb9bf4da3 |
parent 3551 | 5a73db781dac |
child 3589 | a5432f99f2d9 |
child 3625 | f03b55a18c43 |
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 |
||
12 |
from rql import nodes |
|
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: |
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
|
86 |
actions = self.vreg['actions'].possible_vobjects( |
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 |
||
3551
5a73db781dac
[rset] new copy() method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3016
diff
changeset
|
120 |
def copy(self): |
5a73db781dac
[rset] new copy() method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3016
diff
changeset
|
121 |
rset = ResultSet(self.rows[:], self.rql, self.args, self.description[:]) |
5a73db781dac
[rset] new copy() method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3016
diff
changeset
|
122 |
return self.req.decorate_rset(rset) |
5a73db781dac
[rset] new copy() method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3016
diff
changeset
|
123 |
|
0 | 124 |
def _prepare_copy(self, rows, descr): |
125 |
rset = ResultSet(rows, self.rql, self.args, descr) |
|
126 |
return self.req.decorate_rset(rset) |
|
127 |
||
128 |
def transformed_rset(self, transformcb): |
|
129 |
""" the result set according to a given column types |
|
130 |
||
131 |
:type transormcb: callable(row, desc) |
|
132 |
:param transformcb: |
|
133 |
a callable which should take a row and its type description as |
|
134 |
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
|
135 |
|
0 | 136 |
|
137 |
:type col: int |
|
138 |
:param col: the column index |
|
139 |
||
140 |
:rtype: `ResultSet` |
|
141 |
""" |
|
142 |
rows, descr = [], [] |
|
143 |
rset = self._prepare_copy(rows, descr) |
|
144 |
for row, desc in zip(self.rows, self.description): |
|
145 |
nrow, ndesc = transformcb(row, desc) |
|
146 |
if ndesc: # transformcb returns None for ndesc to skip that row |
|
147 |
rows.append(nrow) |
|
148 |
descr.append(ndesc) |
|
149 |
rset.rowcount = len(rows) |
|
150 |
return rset |
|
151 |
||
152 |
def filtered_rset(self, filtercb, col=0): |
|
153 |
"""filter the result set according to a given filtercb |
|
154 |
||
155 |
:type filtercb: callable(entity) |
|
156 |
:param filtercb: |
|
157 |
a callable which should take an entity as argument and return |
|
158 |
False if it should be skipped, else True |
|
159 |
||
160 |
:type col: int |
|
161 |
:param col: the column index |
|
162 |
||
163 |
:rtype: `ResultSet` |
|
164 |
""" |
|
165 |
rows, descr = [], [] |
|
166 |
rset = self._prepare_copy(rows, descr) |
|
167 |
for i in xrange(len(self)): |
|
168 |
if not filtercb(self.get_entity(i, col)): |
|
169 |
continue |
|
170 |
rows.append(self.rows[i]) |
|
171 |
descr.append(self.description[i]) |
|
172 |
rset.rowcount = len(rows) |
|
173 |
return rset |
|
174 |
||
175 |
||
176 |
def sorted_rset(self, keyfunc, reverse=False, col=0): |
|
177 |
"""sorts the result set according to a given keyfunc |
|
178 |
||
179 |
:type keyfunc: callable(entity) |
|
180 |
:param keyfunc: |
|
181 |
a callable which should take an entity as argument and return |
|
182 |
the value used to compare and sort |
|
183 |
||
184 |
:type reverse: bool |
|
185 |
:param reverse: if the result should be reversed |
|
186 |
||
187 |
:type col: int |
|
188 |
:param col: the column index. if col = -1, the whole row are used |
|
189 |
||
190 |
:rtype: `ResultSet` |
|
191 |
""" |
|
192 |
rows, descr = [], [] |
|
193 |
rset = self._prepare_copy(rows, descr) |
|
194 |
if col >= 0: |
|
195 |
entities = sorted(enumerate(self.entities(col)), |
|
196 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
197 |
else: |
|
198 |
entities = sorted(enumerate(self), |
|
199 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
1132 | 200 |
for index, _ in entities: |
0 | 201 |
rows.append(self.rows[index]) |
202 |
descr.append(self.description[index]) |
|
203 |
rset.rowcount = len(rows) |
|
204 |
return rset |
|
205 |
||
206 |
def split_rset(self, keyfunc=None, col=0, return_dict=False): |
|
207 |
"""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
|
208 |
|
0 | 209 |
:type keyfunc: callable(entity or FinalType) |
210 |
:param keyfunc: |
|
211 |
a callable which should take a value of the rset in argument and |
|
212 |
return the value used to group the value. If not define, raw value |
|
213 |
of the specified columns is used. |
|
214 |
||
215 |
:type col: int |
|
216 |
:param col: the column index. if col = -1, the whole row are used |
|
217 |
||
218 |
:type return_dict: Boolean |
|
219 |
:param return_dict: If true, the function return a mapping |
|
220 |
(key -> rset) instead of a list of rset |
|
221 |
||
222 |
:rtype: List of `ResultSet` or mapping of `ResultSet` |
|
223 |
||
224 |
""" |
|
225 |
result = [] |
|
226 |
mapping = {} |
|
227 |
for idx, line in enumerate(self): |
|
228 |
if col >= 0: |
|
229 |
try: |
|
1132 | 230 |
key = self.get_entity(idx, col) |
0 | 231 |
except NotAnEntity: |
232 |
key = line[col] |
|
233 |
else: |
|
234 |
key = line |
|
235 |
if keyfunc is not None: |
|
236 |
key = keyfunc(key) |
|
237 |
||
238 |
if key not in mapping: |
|
239 |
rows, descr = [], [] |
|
240 |
rset = self._prepare_copy(rows, descr) |
|
241 |
mapping[key] = rset |
|
242 |
result.append(rset) |
|
243 |
else: |
|
244 |
rset = mapping[key] |
|
245 |
rset.rows.append(self.rows[idx]) |
|
246 |
rset.description.append(self.description[idx]) |
|
247 |
||
248 |
||
249 |
for rset in result: |
|
250 |
rset.rowcount = len(rset.rows) |
|
251 |
if return_dict: |
|
252 |
return mapping |
|
253 |
else: |
|
254 |
return result |
|
255 |
||
256 |
def limit(self, limit, offset=0, inplace=False): |
|
257 |
"""limit the result set to the given number of rows optionaly starting |
|
258 |
from an index different than 0 |
|
259 |
||
260 |
:type limit: int |
|
261 |
:param limit: the maximum number of results |
|
262 |
||
263 |
:type offset: int |
|
264 |
:param offset: the offset index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
265 |
|
0 | 266 |
:type inplace: bool |
267 |
:param inplace: |
|
268 |
if true, the result set is modified in place, else a new result set |
|
269 |
is returned and the original is left unmodified |
|
270 |
||
271 |
:rtype: `ResultSet` |
|
272 |
""" |
|
273 |
stop = limit+offset |
|
274 |
rows = self.rows[offset:stop] |
|
275 |
descr = self.description[offset:stop] |
|
276 |
if inplace: |
|
277 |
rset = self |
|
278 |
rset.rows, rset.description = rows, descr |
|
279 |
rset.rowcount = len(rows) |
|
280 |
clear_cache(rset, 'description_struct') |
|
281 |
if offset: |
|
282 |
clear_cache(rset, 'get_entity') |
|
283 |
# we also have to fix/remove from the request entity cache entities |
|
284 |
# which get a wrong rset reference by this limit call |
|
285 |
for entity in self.req.cached_entities(): |
|
286 |
if entity.rset is self: |
|
287 |
if offset <= entity.row < stop: |
|
288 |
entity.row = entity.row - offset |
|
289 |
else: |
|
290 |
self.req.drop_entity_cache(entity.eid) |
|
291 |
else: |
|
292 |
rset = self._prepare_copy(rows, descr) |
|
293 |
if not offset: |
|
294 |
# can copy built entity caches |
|
295 |
copy_cache(rset, 'get_entity', self) |
|
296 |
rset.limited = (limit, offset) |
|
297 |
return rset |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
298 |
|
0 | 299 |
def printable_rql(self, encoded=False): |
300 |
"""return the result set's origin rql as a string, with arguments |
|
301 |
substitued |
|
302 |
""" |
|
303 |
encoding = self.req.encoding |
|
304 |
rqlstr = self.syntax_tree().as_string(encoding, self.args) |
|
305 |
# sounds like we get encoded or unicode string due to a bug in as_string |
|
306 |
if not encoded: |
|
307 |
if isinstance(rqlstr, unicode): |
|
308 |
return rqlstr |
|
309 |
return unicode(rqlstr, encoding) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
310 |
else: |
0 | 311 |
if isinstance(rqlstr, unicode): |
312 |
return rqlstr.encode(encoding) |
|
313 |
return rqlstr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
314 |
|
0 | 315 |
# client helper methods ################################################### |
316 |
||
317 |
def entities(self, col=0): |
|
318 |
"""iter on entities with eid in the `col` column of the result set""" |
|
319 |
for i in xrange(len(self)): |
|
320 |
# may have None values in case of outer join (or aggregat on eid |
|
321 |
# hacks) |
|
322 |
if self.rows[i][col] is not None: |
|
323 |
yield self.get_entity(i, col) |
|
324 |
||
325 |
@cached |
|
326 |
def get_entity(self, row, col=None): |
|
327 |
"""special method for query retreiving a single entity, returns a |
|
328 |
partially initialized Entity instance. |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
329 |
|
0 | 330 |
WARNING: due to the cache wrapping this function, you should NEVER |
331 |
give row as a named parameter (i.e. rset.get_entity(req, 0) |
|
332 |
is OK but rset.get_entity(row=0, req=req) isn't |
|
333 |
||
334 |
:type row,col: int, int |
|
335 |
:param row,col: |
|
336 |
row and col numbers localizing the entity among the result's table |
|
337 |
||
338 |
:return: the partially initialized `Entity` instance |
|
339 |
""" |
|
340 |
if col is None: |
|
341 |
from warnings import warn |
|
342 |
msg = 'col parameter will become mandatory in future version' |
|
343 |
warn(msg, DeprecationWarning, stacklevel=3) |
|
344 |
col = 0 |
|
345 |
etype = self.description[row][col] |
|
346 |
try: |
|
347 |
eschema = self.vreg.schema.eschema(etype) |
|
348 |
if eschema.is_final(): |
|
349 |
raise NotAnEntity(etype) |
|
350 |
except KeyError: |
|
351 |
raise NotAnEntity(etype) |
|
352 |
return self._build_entity(row, col) |
|
353 |
||
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
|
354 |
def _build_entity(self, row, col): |
0 | 355 |
"""internal method to get a single entity, returns a |
356 |
partially initialized Entity instance. |
|
357 |
||
358 |
partially means that only attributes selected in the RQL |
|
359 |
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
|
360 |
|
0 | 361 |
:type row,col: int, int |
362 |
:param row,col: |
|
363 |
row and col numbers localizing the entity among the result's table |
|
364 |
||
365 |
:return: the partially initialized `Entity` instance |
|
366 |
""" |
|
367 |
req = self.req |
|
368 |
if req is None: |
|
369 |
raise AssertionError('dont call get_entity with no req on the result set') |
|
370 |
rowvalues = self.rows[row] |
|
371 |
eid = rowvalues[col] |
|
372 |
assert eid is not None |
|
373 |
# return cached entity if exists. This also avoids potential recursion |
|
374 |
# XXX should we consider updating a cached entity with possible |
|
375 |
# new attributes found in this resultset ? |
|
376 |
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
|
377 |
entity = req.entity_cache(eid) |
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
|
378 |
if entity.rset is None: |
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
|
379 |
# entity has no rset set, this means entity has been cached by |
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
|
380 |
# the repository (req is a repository session) which had no rset |
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
|
381 |
# info. Add id. |
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
|
382 |
entity.rset = self |
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
|
383 |
entity.row = row |
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
|
384 |
entity.col = col |
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
|
385 |
return entity |
0 | 386 |
except KeyError: |
387 |
pass |
|
388 |
# build entity instance |
|
389 |
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
|
390 |
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
|
391 |
row=row, col=col) |
0 | 392 |
entity.set_eid(eid) |
393 |
# cache entity |
|
394 |
req.set_entity_cache(entity) |
|
395 |
eschema = entity.e_schema |
|
396 |
# try to complete the entity if there are some additional columns |
|
397 |
if len(rowvalues) > 1: |
|
398 |
rqlst = self.syntax_tree() |
|
399 |
if rqlst.TYPE == 'select': |
|
400 |
# UNION query, find the subquery from which this entity has been |
|
401 |
# found |
|
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
402 |
rqlst, col = rqlst.locate_subquery(col, etype, self.args) |
0 | 403 |
# take care, due to outer join support, we may find None |
404 |
# values for non final relation |
|
405 |
for i, attr, x in attr_desc_iterator(rqlst, col): |
|
406 |
if x == 'subject': |
|
407 |
rschema = eschema.subject_relation(attr) |
|
408 |
if rschema.is_final(): |
|
409 |
entity[attr] = rowvalues[i] |
|
410 |
continue |
|
411 |
tetype = rschema.objects(etype)[0] |
|
412 |
card = rschema.rproperty(etype, tetype, 'cardinality')[0] |
|
413 |
else: |
|
414 |
rschema = eschema.object_relation(attr) |
|
415 |
tetype = rschema.subjects(etype)[0] |
|
416 |
card = rschema.rproperty(tetype, etype, 'cardinality')[1] |
|
417 |
# only keep value if it can't be multivalued |
|
418 |
if card in '1?': |
|
419 |
if rowvalues[i] is None: |
|
420 |
if x == 'subject': |
|
421 |
rql = 'Any Y WHERE X %s Y, X eid %s' |
|
422 |
else: |
|
423 |
rql = 'Any Y WHERE Y %s X, X eid %s' |
|
424 |
rrset = ResultSet([], rql % (attr, entity.eid)) |
|
425 |
req.decorate_rset(rrset) |
|
426 |
else: |
|
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
|
427 |
rrset = self._build_entity(row, i).as_rset() |
0 | 428 |
entity.set_related_cache(attr, x, rrset) |
429 |
return entity |
|
430 |
||
431 |
@cached |
|
432 |
def syntax_tree(self): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
433 |
"""get the syntax tree for the source query. |
0 | 434 |
|
435 |
:rtype: rql.stmts.Statement |
|
436 |
:return: the RQL syntax tree of the originating query |
|
437 |
""" |
|
438 |
if self._rqlst: |
|
439 |
rqlst = self._rqlst.copy() |
|
440 |
# to avoid transport overhead when pyro is used, the schema has been |
|
441 |
# unset from the syntax tree |
|
442 |
rqlst.schema = self.vreg.schema |
|
443 |
self.vreg.rqlhelper.annotate(rqlst) |
|
444 |
else: |
|
445 |
rqlst = self.vreg.parse(self.req, self.rql, self.args) |
|
446 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
447 |
|
0 | 448 |
@cached |
449 |
def column_types(self, col): |
|
450 |
"""return the list of different types in the column with the given col |
|
451 |
index default to 0 (ie the first column) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
452 |
|
0 | 453 |
:type col: int |
454 |
:param col: the index of the desired column |
|
455 |
||
456 |
:rtype: list |
|
457 |
:return: the different entities type found in the column |
|
458 |
""" |
|
459 |
return frozenset(struc[-1][col] for struc in self.description_struct()) |
|
460 |
||
461 |
@cached |
|
462 |
def description_struct(self): |
|
463 |
"""return a list describing sequence of results with the same |
|
464 |
description, e.g. : |
|
465 |
[[0, 4, ('Bug',)] |
|
466 |
[[0, 4, ('Bug',), [5, 8, ('Story',)] |
|
467 |
[[0, 3, ('Project', 'Version',)]] |
|
468 |
""" |
|
469 |
result = [] |
|
470 |
last = None |
|
471 |
for i, row in enumerate(self.description): |
|
472 |
if row != last: |
|
473 |
if last is not None: |
|
474 |
result[-1][1] = i - 1 |
|
475 |
result.append( [i, None, row] ) |
|
476 |
last = row |
|
477 |
if last is not None: |
|
478 |
result[-1][1] = i |
|
479 |
return result |
|
480 |
||
481 |
@cached |
|
482 |
def related_entity(self, row, col): |
|
483 |
"""try to get the related entity to extract format information if any""" |
|
484 |
locate_query_col = col |
|
485 |
rqlst = self.syntax_tree() |
|
486 |
etype = self.description[row][col] |
|
487 |
if self.vreg.schema.eschema(etype).is_final(): |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
170
diff
changeset
|
488 |
# 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
|
489 |
# (ambiguous if possible) |
0 | 490 |
for i in xrange(len(rqlst.children[0].selection)): |
491 |
if i == col: |
|
492 |
continue |
|
493 |
coletype = self.description[row][i] |
|
494 |
if coletype is None: |
|
495 |
continue |
|
496 |
if not self.vreg.schema.eschema(coletype).is_final(): |
|
497 |
etype = coletype |
|
498 |
locate_query_col = i |
|
499 |
if len(self.column_types(i)) > 1: |
|
500 |
break |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
170
diff
changeset
|
501 |
# 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
|
502 |
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
|
503 |
col = rqlst.subquery_selection_index(select, col) |
0 | 504 |
try: |
505 |
myvar = select.selection[col].variable |
|
506 |
except AttributeError: |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
170
diff
changeset
|
507 |
# not a variable |
0 | 508 |
return None, None |
509 |
rel = myvar.main_relation() |
|
510 |
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
|
511 |
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
|
512 |
if index is not None and self.rows[row][index]: |
0 | 513 |
return self.get_entity(row, index), rel.r_type |
514 |
return None, None |
|
515 |
||
516 |
@cached |
|
517 |
def searched_text(self): |
|
518 |
"""returns the searched text in case of full-text search |
|
519 |
||
520 |
:return: searched text or `None` if the query is not |
|
521 |
a full-text query |
|
522 |
""" |
|
523 |
rqlst = self.syntax_tree() |
|
524 |
for rel in rqlst.iget_nodes(nodes.Relation): |
|
525 |
if rel.r_type == 'has_text': |
|
526 |
__, rhs = rel.get_variable_parts() |
|
527 |
return rhs.eval(self.args) |
|
528 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
529 |
|
0 | 530 |
|
531 |
def attr_desc_iterator(rqlst, index=0): |
|
532 |
"""return an iterator on a list of 2-uple (index, attr_relation) |
|
533 |
localizing attribute relations of the main variable in a result's row |
|
534 |
||
535 |
:type rqlst: rql.stmts.Select |
|
536 |
:param rqlst: the RQL syntax tree to describe |
|
537 |
||
538 |
:return: |
|
539 |
a generator on (index, relation, target) describing column being |
|
540 |
attribute of the main variable |
|
541 |
""" |
|
542 |
main = rqlst.selection[index] |
|
543 |
for i, term in enumerate(rqlst.selection): |
|
544 |
if i == index: |
|
545 |
continue |
|
2352
734eb79680e9
handle attribute selection on variable selected using MAX()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
546 |
# XXX rewritten const |
734eb79680e9
handle attribute selection on variable selected using MAX()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
547 |
# 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
|
548 |
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
|
549 |
var = vref.variable |
734eb79680e9
handle attribute selection on variable selected using MAX()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
550 |
break |
734eb79680e9
handle attribute selection on variable selected using MAX()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
551 |
else: |
0 | 552 |
continue |
553 |
#varname = var.name |
|
554 |
for ref in var.references(): |
|
555 |
rel = ref.relation() |
|
556 |
if rel is None or rel.is_types_restriction(): |
|
557 |
continue |
|
558 |
lhs, rhs = rel.get_variable_parts() |
|
559 |
if main.is_equivalent(lhs): |
|
560 |
if rhs.is_equivalent(term): |
|
561 |
yield (i, rel.r_type, 'subject') |
|
562 |
elif main.is_equivalent(rhs): |
|
563 |
if lhs.is_equivalent(term): |
|
564 |
yield (i, rel.r_type, 'object') |