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