author | Julien Cristau <julien.cristau@logilab.fr> |
Wed, 29 Jan 2014 16:03:42 +0100 | |
changeset 9501 | 2904e2ba81a0 |
parent 9347 | bd841d6ae723 |
child 9780 | 8e687be43d84 |
permissions | -rw-r--r-- |
7395
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5314
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5671
3a2063b752f3
[editform] consistent [do]reledit between attributes and relations: check if rdef is hidden in main section of uicfg.afs for both attributes and relations, and in the reledit view, not in doreledit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5667
diff
changeset
|
18 |
"""The `ResultSet` class which is returned as result of an rql query""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
from logilab.common.decorators import cached, clear_cache, copy_cache |
|
23 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
24 |
from rql import nodes, stmts |
0 | 25 |
|
9347
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
26 |
from cubicweb import NotAnEntity, NoResultError, MultipleResultsError |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
27 |
|
0 | 28 |
|
29 |
class ResultSet(object): |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
30 |
"""A result set wraps a RQL query result. This object implements |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
31 |
partially the list protocol to allow direct use as a list of |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
32 |
result rows. |
0 | 33 |
|
34 |
:type rowcount: int |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
35 |
:param rowcount: number of rows in the result |
0 | 36 |
|
37 |
:type rows: list |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
38 |
:param rows: list of rows of result |
0 | 39 |
|
40 |
:type description: list |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
41 |
:param description: |
0 | 42 |
result's description, using the same structure as the result itself |
43 |
||
44 |
:type rql: str or unicode |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
45 |
:param rql: the original RQL query string |
0 | 46 |
""" |
5740
46207f491a51
[rset] make rsets picklable (again ?) #1056422
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5671
diff
changeset
|
47 |
|
9281
49a7392bb5b5
[rset] make sure rset.description is always a list
Julien Cristau <julien.cristau@logilab.fr>
parents:
8695
diff
changeset
|
48 |
def __init__(self, results, rql, args=None, description=None, rqlst=None): |
0 | 49 |
self.rows = results |
50 |
self.rowcount = results and len(results) or 0 |
|
51 |
# original query and arguments |
|
52 |
self.rql = rql |
|
53 |
self.args = args |
|
54 |
# entity types for each cell (same shape as rows) |
|
55 |
# maybe discarded if specified when the query has been executed |
|
9331
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
56 |
if description is None: |
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
57 |
self.description = [] |
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
58 |
else: |
c6f54ed6b60a
[rset] Fix regression with rset copying (closes #3344410)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9281
diff
changeset
|
59 |
self.description = description |
0 | 60 |
# parsed syntax tree |
61 |
if rqlst is not None: |
|
62 |
rqlst.schema = None # reset schema in case of pyro transfert |
|
63 |
self._rqlst = rqlst |
|
64 |
# set to (limit, offset) when a result set is limited using the |
|
65 |
# .limit method |
|
66 |
self.limited = None |
|
67 |
# set by the cursor which returned this resultset |
|
68 |
self.req = None |
|
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
69 |
# actions cache |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
70 |
self._rsetactions = None |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
71 |
|
0 | 72 |
def __str__(self): |
73 |
if not self.rows: |
|
74 |
return '<empty resultset %s>' % self.rql |
|
75 |
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
|
76 |
|
0 | 77 |
def __repr__(self): |
78 |
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
|
79 |
return '<empty resultset for %r>' % self.rql |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
80 |
rows = self.rows |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
81 |
if len(rows) > 10: |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
82 |
rows = rows[:10] + ['...'] |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
83 |
if len(rows) > 1: |
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
84 |
# add a line break before first entity if more that one. |
7395
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
85 |
pattern = '<resultset %r (%s rows):\n%s>' |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
86 |
else: |
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
87 |
pattern = '<resultset %r (%s rows): %s>' |
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
88 |
|
0 | 89 |
if not self.description: |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
90 |
return pattern % (self.rql, len(self.rows), |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
91 |
'\n'.join(str(r) for r in rows)) |
5687
3ea39709b50e
[rset] Add a line break the first result in repr of multiple rows rset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5679
diff
changeset
|
92 |
return pattern % (self.rql, len(self.rows), |
616
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
93 |
'\n'.join('%s (%s)' % (r, d) |
545a7e18c47f
don't display too much rset rows
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
94 |
for r, d in zip(rows, self.description))) |
0 | 95 |
|
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
96 |
def possible_actions(self, **kwargs): |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
97 |
if self._rsetactions is None: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
98 |
self._rsetactions = {} |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
99 |
if kwargs: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
100 |
key = tuple(sorted(kwargs.iteritems())) |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
101 |
else: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
102 |
key = None |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
103 |
try: |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
104 |
return self._rsetactions[key] |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
105 |
except KeyError: |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
106 |
actions = self.req.vreg['actions'].poss_visible_objects( |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2648
diff
changeset
|
107 |
self.req, rset=self, **kwargs) |
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
108 |
self._rsetactions[key] = actions |
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
616
diff
changeset
|
109 |
return actions |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
110 |
|
0 | 111 |
def __len__(self): |
112 |
"""returns the result set's size""" |
|
113 |
return self.rowcount |
|
114 |
||
115 |
def __nonzero__(self): |
|
116 |
return self.rowcount |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
117 |
|
0 | 118 |
def __getitem__(self, i): |
119 |
"""returns the ith element of the result set""" |
|
120 |
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
|
121 |
|
0 | 122 |
def __getslice__(self, i, j): |
123 |
"""returns slice [i:j] of the result set""" |
|
124 |
return self.rows[i:j] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
125 |
|
0 | 126 |
def __iter__(self): |
127 |
"""Returns an iterator over rows""" |
|
128 |
return iter(self.rows) |
|
129 |
||
130 |
def __add__(self, rset): |
|
131 |
# XXX buggy implementation (.rql and .args attributes at least much |
|
132 |
# probably differ) |
|
133 |
# at least rql could be fixed now that we have union and sub-queries |
|
134 |
# but I tend to think that since we have that, we should not need this |
|
135 |
# method anymore (syt) |
|
136 |
rset = ResultSet(self.rows+rset.rows, self.rql, self.args, |
|
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
137 |
self.description + rset.description) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
138 |
rset.req = self.req |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
139 |
return rset |
0 | 140 |
|
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
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
descr = self.description[:] |
0 | 145 |
rset = ResultSet(rows, self.rql, self.args, descr) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
146 |
rset.req = self.req |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
147 |
return rset |
0 | 148 |
|
149 |
def transformed_rset(self, transformcb): |
|
150 |
""" the result set according to a given column types |
|
151 |
||
152 |
:type transormcb: callable(row, desc) |
|
153 |
:param transformcb: |
|
154 |
a callable which should take a row and its type description as |
|
155 |
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
|
156 |
|
0 | 157 |
|
158 |
:type col: int |
|
159 |
:param col: the column index |
|
160 |
||
161 |
:rtype: `ResultSet` |
|
162 |
""" |
|
163 |
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
|
164 |
rset = self.copy(rows, descr) |
0 | 165 |
for row, desc in zip(self.rows, self.description): |
166 |
nrow, ndesc = transformcb(row, desc) |
|
167 |
if ndesc: # transformcb returns None for ndesc to skip that row |
|
168 |
rows.append(nrow) |
|
169 |
descr.append(ndesc) |
|
170 |
rset.rowcount = len(rows) |
|
171 |
return rset |
|
172 |
||
173 |
def filtered_rset(self, filtercb, col=0): |
|
174 |
"""filter the result set according to a given filtercb |
|
175 |
||
176 |
:type filtercb: callable(entity) |
|
177 |
:param filtercb: |
|
178 |
a callable which should take an entity as argument and return |
|
179 |
False if it should be skipped, else True |
|
180 |
||
181 |
:type col: int |
|
182 |
:param col: the column index |
|
183 |
||
184 |
:rtype: `ResultSet` |
|
185 |
""" |
|
186 |
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
|
187 |
rset = self.copy(rows, descr) |
0 | 188 |
for i in xrange(len(self)): |
189 |
if not filtercb(self.get_entity(i, col)): |
|
190 |
continue |
|
191 |
rows.append(self.rows[i]) |
|
192 |
descr.append(self.description[i]) |
|
193 |
rset.rowcount = len(rows) |
|
194 |
return rset |
|
195 |
||
196 |
||
197 |
def sorted_rset(self, keyfunc, reverse=False, col=0): |
|
198 |
"""sorts the result set according to a given keyfunc |
|
199 |
||
200 |
:type keyfunc: callable(entity) |
|
201 |
:param keyfunc: |
|
202 |
a callable which should take an entity as argument and return |
|
203 |
the value used to compare and sort |
|
204 |
||
205 |
:type reverse: bool |
|
206 |
:param reverse: if the result should be reversed |
|
207 |
||
208 |
:type col: int |
|
209 |
:param col: the column index. if col = -1, the whole row are used |
|
210 |
||
211 |
:rtype: `ResultSet` |
|
212 |
""" |
|
213 |
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
|
214 |
rset = self.copy(rows, descr) |
0 | 215 |
if col >= 0: |
216 |
entities = sorted(enumerate(self.entities(col)), |
|
217 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
218 |
else: |
|
219 |
entities = sorted(enumerate(self), |
|
220 |
key=lambda (i, e): keyfunc(e), reverse=reverse) |
|
1132 | 221 |
for index, _ in entities: |
0 | 222 |
rows.append(self.rows[index]) |
223 |
descr.append(self.description[index]) |
|
224 |
rset.rowcount = len(rows) |
|
225 |
return rset |
|
226 |
||
227 |
def split_rset(self, keyfunc=None, col=0, return_dict=False): |
|
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
228 |
"""splits the result set in multiple result sets according to |
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
229 |
a given key |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
230 |
|
0 | 231 |
:type keyfunc: callable(entity or FinalType) |
232 |
:param keyfunc: |
|
233 |
a callable which should take a value of the rset in argument and |
|
234 |
return the value used to group the value. If not define, raw value |
|
235 |
of the specified columns is used. |
|
236 |
||
237 |
:type col: int |
|
238 |
:param col: the column index. if col = -1, the whole row are used |
|
239 |
||
240 |
:type return_dict: Boolean |
|
241 |
:param return_dict: If true, the function return a mapping |
|
242 |
(key -> rset) instead of a list of rset |
|
243 |
||
244 |
:rtype: List of `ResultSet` or mapping of `ResultSet` |
|
245 |
||
246 |
""" |
|
247 |
result = [] |
|
248 |
mapping = {} |
|
249 |
for idx, line in enumerate(self): |
|
250 |
if col >= 0: |
|
251 |
try: |
|
1132 | 252 |
key = self.get_entity(idx, col) |
0 | 253 |
except NotAnEntity: |
254 |
key = line[col] |
|
255 |
else: |
|
256 |
key = line |
|
257 |
if keyfunc is not None: |
|
258 |
key = keyfunc(key) |
|
259 |
||
260 |
if key not in mapping: |
|
261 |
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
|
262 |
rset = self.copy(rows, descr) |
0 | 263 |
mapping[key] = rset |
264 |
result.append(rset) |
|
265 |
else: |
|
266 |
rset = mapping[key] |
|
267 |
rset.rows.append(self.rows[idx]) |
|
268 |
rset.description.append(self.description[idx]) |
|
269 |
for rset in result: |
|
270 |
rset.rowcount = len(rset.rows) |
|
271 |
if return_dict: |
|
272 |
return mapping |
|
273 |
else: |
|
274 |
return result |
|
275 |
||
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
|
276 |
def limited_rql(self): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
277 |
"""returns a printable rql for the result set associated to the object, |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
278 |
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
|
279 |
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
|
280 |
""" |
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 |
# 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
|
282 |
# 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
|
283 |
# cubicweb) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
284 |
nav = self.req.vreg['components'].select_or_none('navigation', self.req, |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
285 |
rset=self) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
286 |
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
|
287 |
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
|
288 |
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
|
289 |
# 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
|
290 |
# 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
|
291 |
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
|
292 |
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
|
293 |
# 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
|
294 |
# 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
|
295 |
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
|
296 |
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
|
297 |
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
|
298 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
299 |
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
|
300 |
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
|
301 |
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
|
302 |
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
|
303 |
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
|
304 |
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
|
305 |
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
|
306 |
# 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
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
newselect.offset = offset |
4939
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
312 |
aliases = [nodes.VariableRef(newselect.get_variable(chr(65+i), i)) |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
313 |
for i in xrange(len(rqlst.children[0].selection))] |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
314 |
for vref in aliases: |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
315 |
newselect.append_selected(nodes.VariableRef(vref.variable)) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
316 |
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
|
317 |
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
|
318 |
newunion.append(newselect) |
4939
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
319 |
rql = newunion.as_string(kwargs=self.args) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
320 |
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
|
321 |
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
|
322 |
|
0 | 323 |
def limit(self, limit, offset=0, inplace=False): |
324 |
"""limit the result set to the given number of rows optionaly starting |
|
325 |
from an index different than 0 |
|
326 |
||
327 |
:type limit: int |
|
328 |
:param limit: the maximum number of results |
|
329 |
||
330 |
:type offset: int |
|
331 |
:param offset: the offset index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
332 |
|
0 | 333 |
:type inplace: bool |
334 |
:param inplace: |
|
335 |
if true, the result set is modified in place, else a new result set |
|
336 |
is returned and the original is left unmodified |
|
337 |
||
338 |
:rtype: `ResultSet` |
|
339 |
""" |
|
340 |
stop = limit+offset |
|
341 |
rows = self.rows[offset:stop] |
|
342 |
descr = self.description[offset:stop] |
|
343 |
if inplace: |
|
344 |
rset = self |
|
345 |
rset.rows, rset.description = rows, descr |
|
346 |
rset.rowcount = len(rows) |
|
347 |
clear_cache(rset, 'description_struct') |
|
348 |
if offset: |
|
349 |
clear_cache(rset, 'get_entity') |
|
350 |
# we also have to fix/remove from the request entity cache entities |
|
351 |
# which get a wrong rset reference by this limit call |
|
352 |
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
|
353 |
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
|
354 |
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
|
355 |
entity.cw_row = entity.cw_row - offset |
0 | 356 |
else: |
8048
10a0f73d834d
[rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
357 |
entity.cw_rset = entity.as_rset() |
10a0f73d834d
[rset] replace entity cw_rset on .limit(inplace=True) will avoid pb if the entity is still referenced by some other entity relations cache. Closes #2065643
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
358 |
entity.cw_row = entity.cw_col = 0 |
0 | 359 |
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
|
360 |
rset = self.copy(rows, descr) |
0 | 361 |
if not offset: |
362 |
# can copy built entity caches |
|
363 |
copy_cache(rset, 'get_entity', self) |
|
364 |
rset.limited = (limit, offset) |
|
365 |
return rset |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
366 |
|
0 | 367 |
def printable_rql(self, encoded=False): |
368 |
"""return the result set's origin rql as a string, with arguments |
|
369 |
substitued |
|
370 |
""" |
|
371 |
encoding = self.req.encoding |
|
372 |
rqlstr = self.syntax_tree().as_string(encoding, self.args) |
|
373 |
# sounds like we get encoded or unicode string due to a bug in as_string |
|
374 |
if not encoded: |
|
375 |
if isinstance(rqlstr, unicode): |
|
376 |
return rqlstr |
|
377 |
return unicode(rqlstr, encoding) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
378 |
else: |
0 | 379 |
if isinstance(rqlstr, unicode): |
380 |
return rqlstr.encode(encoding) |
|
381 |
return rqlstr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
382 |
|
0 | 383 |
# client helper methods ################################################### |
384 |
||
385 |
def entities(self, col=0): |
|
386 |
"""iter on entities with eid in the `col` column of the result set""" |
|
387 |
for i in xrange(len(self)): |
|
388 |
# may have None values in case of outer join (or aggregat on eid |
|
389 |
# hacks) |
|
390 |
if self.rows[i][col] is not None: |
|
391 |
yield self.get_entity(i, col) |
|
392 |
||
6857
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
393 |
def iter_rows_with_entities(self): |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
394 |
""" iterates over rows, and for each row |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
395 |
eids are converted to plain entities |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
396 |
""" |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
397 |
for i, row in enumerate(self): |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
398 |
_row = [] |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
399 |
for j, col in enumerate(row): |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
400 |
try: |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
401 |
_row.append(self.get_entity(i, j) if col is not None else col) |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
402 |
except NotAnEntity: |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
403 |
_row.append(col) |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
404 |
yield _row |
8d2062387134
[rset] add utility method to get entities and attributes from any rset
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6225
diff
changeset
|
405 |
|
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
|
406 |
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
|
407 |
"""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
|
408 |
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
|
409 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
410 |
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
|
411 |
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
|
412 |
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
|
413 |
|
0 | 414 |
@cached |
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
415 |
def get_entity(self, row, col): |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
416 |
"""convenience method for query retrieving a single entity, returns a |
0 | 417 |
partially initialized Entity instance. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
418 |
|
5314
86e5abbebfaf
[doc/book] refresh the autoform section with uicfg content (moved there)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5312
diff
changeset
|
419 |
.. warning:: |
5312
d2dbba898a96
[doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5311
diff
changeset
|
420 |
|
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
421 |
Due to the cache wrapping this function, you should NEVER give row as |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
422 |
a named parameter (i.e. `rset.get_entity(0, 1)` is OK but |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
423 |
`rset.get_entity(row=0, col=1)` isn't) |
0 | 424 |
|
425 |
:type row,col: int, int |
|
426 |
:param row,col: |
|
427 |
row and col numbers localizing the entity among the result's table |
|
428 |
||
429 |
:return: the partially initialized `Entity` instance |
|
430 |
""" |
|
431 |
etype = self.description[row][col] |
|
432 |
try: |
|
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
433 |
eschema = self.req.vreg.schema.eschema(etype) |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3625
diff
changeset
|
434 |
if eschema.final: |
0 | 435 |
raise NotAnEntity(etype) |
436 |
except KeyError: |
|
437 |
raise NotAnEntity(etype) |
|
438 |
return self._build_entity(row, col) |
|
439 |
||
9347
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
440 |
def one(self, col=0): |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
441 |
"""Retrieve exactly one entity from the query. |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
442 |
|
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
443 |
If the result set is empty, raises :exc:`NoResultError`. |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
444 |
If the result set has more than one row, raises |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
445 |
:exc:`MultipleResultsError`. |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
446 |
|
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
447 |
:type col: int |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
448 |
:param col: The column localising the entity in the unique row |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
449 |
|
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
450 |
:return: the partially initialized `Entity` instance |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
451 |
""" |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
452 |
if len(self) == 1: |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
453 |
return self.get_entity(0, col) |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
454 |
elif len(self) == 0: |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
455 |
raise NoResultError("No row was found for one()") |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
456 |
else: |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
457 |
raise MultipleResultsError("Multiple rows were found for one()") |
bd841d6ae723
[rset] New method: ResultSet.one()
Christophe de Vienne <cdevienne@gmail.com>
parents:
9331
diff
changeset
|
458 |
|
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
|
459 |
def _build_entity(self, row, col): |
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
460 |
"""internal method to get a single entity, returns a partially |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
461 |
initialized Entity instance. |
0 | 462 |
|
6859
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
463 |
partially means that only attributes selected in the RQL query will be |
ace0b991e17b
[entity] fix some docstrings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6857
diff
changeset
|
464 |
directly assigned to the entity. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
465 |
|
0 | 466 |
:type row,col: int, int |
467 |
:param row,col: |
|
468 |
row and col numbers localizing the entity among the result's table |
|
469 |
||
470 |
:return: the partially initialized `Entity` instance |
|
471 |
""" |
|
472 |
req = self.req |
|
473 |
if req is None: |
|
474 |
raise AssertionError('dont call get_entity with no req on the result set') |
|
475 |
rowvalues = self.rows[row] |
|
476 |
eid = rowvalues[col] |
|
477 |
assert eid is not None |
|
478 |
# return cached entity if exists. This also avoids potential recursion |
|
479 |
# XXX should we consider updating a cached entity with possible |
|
480 |
# new attributes found in this resultset ? |
|
481 |
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
|
482 |
entity = req.entity_cache(eid) |
0 | 483 |
except KeyError: |
484 |
pass |
|
2832
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
485 |
else: |
3379
9192ba07890d
use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3023
diff
changeset
|
486 |
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
|
487 |
# 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
|
488 |
# 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
|
489 |
# info. Add it. |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
490 |
entity.cw_rset = self |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
491 |
entity.cw_row = row |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
492 |
entity.cw_col = col |
7fb67c54ffb9
[rset] better explanation, refactor try except
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2813
diff
changeset
|
493 |
return entity |
0 | 494 |
# build entity instance |
495 |
etype = self.description[row][col] |
|
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
496 |
entity = self.req.vreg['etypes'].etype_class(etype)(req, rset=self, |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
497 |
row=row, col=col) |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5532
diff
changeset
|
498 |
entity.eid = eid |
0 | 499 |
# cache entity |
500 |
req.set_entity_cache(entity) |
|
501 |
# try to complete the entity if there are some additional columns |
|
502 |
if len(rowvalues) > 1: |
|
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
503 |
eschema = entity.e_schema |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
504 |
eid_col, attr_cols, rel_cols = self._rset_structure(eschema, col) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
505 |
entity.eid = rowvalues[eid_col] |
7968
92303e2ed77a
[rset] use .iteritems() instead of items()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
506 |
for attr, col_idx in attr_cols.iteritems(): |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
507 |
entity.cw_attr_cache[attr] = rowvalues[col_idx] |
7968
92303e2ed77a
[rset] use .iteritems() instead of items()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7595
diff
changeset
|
508 |
for (rtype, role), col_idx in rel_cols.iteritems(): |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
509 |
value = rowvalues[col_idx] |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
510 |
if value is None: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
511 |
if role == 'subject': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
512 |
rql = 'Any Y WHERE X %s Y, X eid %s' |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
513 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
514 |
rql = 'Any Y WHERE Y %s X, X eid %s' |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
515 |
rrset = ResultSet([], rql % (rtype, entity.eid)) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
516 |
rrset.req = req |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
517 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
518 |
rrset = self._build_entity(row, col_idx).as_rset() |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
519 |
entity.cw_set_relation_cache(rtype, role, rrset) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
520 |
return entity |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
521 |
|
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
522 |
@cached |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
523 |
def _rset_structure(self, eschema, entity_col): |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
524 |
eid_col = col = entity_col |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
525 |
rqlst = self.syntax_tree() |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
526 |
attr_cols = {} |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
527 |
rel_cols = {} |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
528 |
if rqlst.TYPE == 'select': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
529 |
# UNION query, find the subquery from which this entity has been |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
530 |
# found |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
531 |
select, col = rqlst.locate_subquery(entity_col, eschema.type, self.args) |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
532 |
else: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
533 |
select = rqlst |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
534 |
# take care, due to outer join support, we may find None |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
535 |
# values for non final relation |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
536 |
for i, attr, role in attr_desc_iterator(select, col, entity_col): |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
537 |
if role == 'subject': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
538 |
rschema = eschema.subjrels[attr] |
3625
f03b55a18c43
fix bug w/ get_entity and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3551
diff
changeset
|
539 |
else: |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
540 |
rschema = eschema.objrels[attr] |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
541 |
if rschema.final: |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
542 |
if attr == 'eid': |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
543 |
eid_col = i |
0 | 544 |
else: |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
545 |
attr_cols[attr] = i |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
546 |
else: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3766
diff
changeset
|
547 |
rdef = eschema.rdef(attr, role) |
0 | 548 |
# only keep value if it can't be multivalued |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3766
diff
changeset
|
549 |
if rdef.role_cardinality(role) in '1?': |
7285
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
550 |
rel_cols[(attr, role)] = i |
39437617f3f0
[optim] cache rset column structures
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6915
diff
changeset
|
551 |
return eid_col, attr_cols, rel_cols |
0 | 552 |
|
553 |
@cached |
|
554 |
def syntax_tree(self): |
|
7592
c84436b79793
[rset] syntax_tree impl. consistency: don't return an annotated syntax tree
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7395
diff
changeset
|
555 |
"""return the syntax tree (:class:`rql.stmts.Union`) for the originating |
c84436b79793
[rset] syntax_tree impl. consistency: don't return an annotated syntax tree
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7395
diff
changeset
|
556 |
query. You can expect it to have solutions computed but it won't be |
c84436b79793
[rset] syntax_tree impl. consistency: don't return an annotated syntax tree
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7395
diff
changeset
|
557 |
annotated (you usually don't need that for simple introspection). |
0 | 558 |
""" |
559 |
if self._rqlst: |
|
560 |
rqlst = self._rqlst.copy() |
|
561 |
# to avoid transport overhead when pyro is used, the schema has been |
|
562 |
# unset from the syntax tree |
|
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
563 |
rqlst.schema = self.req.vreg.schema |
0 | 564 |
else: |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
565 |
rqlst = self.req.vreg.parse(self.req, self.rql, self.args) |
0 | 566 |
return rqlst |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
567 |
|
0 | 568 |
@cached |
569 |
def column_types(self, col): |
|
570 |
"""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
|
571 |
|
0 | 572 |
:type col: int |
573 |
:param col: the index of the desired column |
|
574 |
||
575 |
:rtype: list |
|
576 |
:return: the different entities type found in the column |
|
577 |
""" |
|
578 |
return frozenset(struc[-1][col] for struc in self.description_struct()) |
|
579 |
||
580 |
@cached |
|
581 |
def description_struct(self): |
|
582 |
"""return a list describing sequence of results with the same |
|
583 |
description, e.g. : |
|
584 |
[[0, 4, ('Bug',)] |
|
585 |
[[0, 4, ('Bug',), [5, 8, ('Story',)] |
|
586 |
[[0, 3, ('Project', 'Version',)]] |
|
587 |
""" |
|
588 |
result = [] |
|
589 |
last = None |
|
590 |
for i, row in enumerate(self.description): |
|
591 |
if row != last: |
|
592 |
if last is not None: |
|
593 |
result[-1][1] = i - 1 |
|
594 |
result.append( [i, None, row] ) |
|
595 |
last = row |
|
596 |
if last is not None: |
|
597 |
result[-1][1] = i |
|
598 |
return result |
|
599 |
||
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
600 |
def _locate_query_params(self, rqlst, row, col): |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
601 |
locate_query_col = col |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
602 |
etype = self.description[row][col] |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
603 |
# final type, find a better one to locate the correct subquery |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
604 |
# (ambiguous if possible) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
605 |
eschema = self.req.vreg.schema.eschema |
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
606 |
if eschema(etype).final: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
607 |
for select in rqlst.children: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
608 |
try: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
609 |
myvar = select.selection[col].variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
610 |
except AttributeError: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
611 |
# not a variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
612 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
613 |
for i in xrange(len(select.selection)): |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
614 |
if i == col: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
615 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
616 |
coletype = self.description[row][i] |
6196 | 617 |
# None description possible on column resulting from an |
618 |
# outer join |
|
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
619 |
if coletype is None or eschema(coletype).final: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
620 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
621 |
try: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
622 |
ivar = select.selection[i].variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
623 |
except AttributeError: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
624 |
# not a variable |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
625 |
continue |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
626 |
# check variables don't comes from a subquery or are both |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
627 |
# coming from the same subquery |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
628 |
if getattr(ivar, 'query', None) is getattr(myvar, 'query', None): |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
629 |
etype = coletype |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
630 |
locate_query_col = i |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
631 |
if len(self.column_types(i)) > 1: |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
632 |
return etype, locate_query_col |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
633 |
return etype, locate_query_col |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
634 |
|
0 | 635 |
@cached |
636 |
def related_entity(self, row, col): |
|
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
637 |
"""given an cell of the result set, try to return a (entity, relation |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
638 |
name) tuple to which this cell is linked. |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
639 |
|
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
640 |
This is especially useful when the cell is an attribute of an entity, |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
641 |
to get the entity to which this attribute belongs to. |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
642 |
""" |
0 | 643 |
rqlst = self.syntax_tree() |
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
644 |
# UNION query, we've first to find a 'pivot' column to use to get the |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
645 |
# actual query from which the row is coming |
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
646 |
etype, locate_query_col = self._locate_query_params(rqlst, row, col) |
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
647 |
# now find the query from which this entity has been found. Returned |
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
648 |
# select node may be a subquery with different column indexes. |
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
649 |
select = rqlst.locate_subquery(locate_query_col, etype, self.args)[0] |
6195
a0c4441e6ddf
[rset]Â add test for #1251252, actually fixed in rql; fix docstring and add comments in rset.related_entity to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5752
diff
changeset
|
650 |
# then get the index of root query's col in the subquery |
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
651 |
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
|
652 |
if col is None: |
9e5dc4b1ada4
work around a pb. with subquery_selection_index
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3764
diff
changeset
|
653 |
# 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
|
654 |
return None, None |
0 | 655 |
try: |
656 |
myvar = select.selection[col].variable |
|
657 |
except AttributeError: |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
170
diff
changeset
|
658 |
# not a variable |
0 | 659 |
return None, None |
660 |
rel = myvar.main_relation() |
|
661 |
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
|
662 |
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
|
663 |
if index is not None and self.rows[row][index]: |
5667
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
664 |
try: |
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
665 |
entity = self.get_entity(row, index) |
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
666 |
return entity, rel.r_type |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8087
diff
changeset
|
667 |
except NotAnEntity as exc: |
5667
04cbd80fd5dc
[rset] do not break when rset.related_entity(x,y) is not an entity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5532
diff
changeset
|
668 |
return None, None |
0 | 669 |
return None, None |
670 |
||
671 |
@cached |
|
672 |
def searched_text(self): |
|
673 |
"""returns the searched text in case of full-text search |
|
674 |
||
675 |
:return: searched text or `None` if the query is not |
|
676 |
a full-text query |
|
677 |
""" |
|
678 |
rqlst = self.syntax_tree() |
|
679 |
for rel in rqlst.iget_nodes(nodes.Relation): |
|
680 |
if rel.r_type == 'has_text': |
|
681 |
__, rhs = rel.get_variable_parts() |
|
682 |
return rhs.eval(self.args) |
|
683 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1477
diff
changeset
|
684 |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
685 |
def _get_variable(term): |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
686 |
# XXX rewritten const |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
687 |
# use iget_nodes for (hack) case where we have things like MAX(V) |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
688 |
for vref in term.iget_nodes(nodes.VariableRef): |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
689 |
return vref.variable |
0 | 690 |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
691 |
def attr_desc_iterator(select, selectidx, rootidx): |
0 | 692 |
"""return an iterator on a list of 2-uple (index, attr_relation) |
693 |
localizing attribute relations of the main variable in a result's row |
|
694 |
||
695 |
:type rqlst: rql.stmts.Select |
|
696 |
:param rqlst: the RQL syntax tree to describe |
|
697 |
||
698 |
:return: |
|
699 |
a generator on (index, relation, target) describing column being |
|
700 |
attribute of the main variable |
|
701 |
""" |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
702 |
rootselect = select |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
703 |
while rootselect.parent.parent is not None: |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
704 |
rootselect = rootselect.parent.parent.parent |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
705 |
rootmain = rootselect.selection[selectidx] |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
706 |
rootmainvar = _get_variable(rootmain) |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
707 |
assert rootmainvar |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
708 |
root = rootselect.parent |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
709 |
selectmain = select.selection[selectidx] |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
710 |
for i, term in enumerate(rootselect.selection): |
7395
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
711 |
try: |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
712 |
# don't use _get_variable here: if the term isn't a variable |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
713 |
# (function...), we don't want it to be used as an entity attribute |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
714 |
# or relation's value (XXX beside MAX/MIN trick?) |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
715 |
rootvar = term.variable |
09ffcc04bd21
[rset] close #1683703: rset.get_entity crash on rql query with subquery and aggregat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6915
diff
changeset
|
716 |
except AttributeError: |
0 | 717 |
continue |
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
718 |
if rootvar.name == rootmainvar.name: |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
719 |
continue |
8085
51929d531aff
[rset] fix crash while building entity from rset w/ some subquery. Closes #2089055
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8048
diff
changeset
|
720 |
if select is not rootselect and isinstance(rootvar, nodes.ColumnAlias): |
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
721 |
term = select.selection[root.subquery_selection_index(select, i)] |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
722 |
var = _get_variable(term) |
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
723 |
if var is None: |
0 | 724 |
continue |
725 |
for ref in var.references(): |
|
726 |
rel = ref.relation() |
|
727 |
if rel is None or rel.is_types_restriction(): |
|
728 |
continue |
|
729 |
lhs, rhs = rel.get_variable_parts() |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
730 |
if selectmain.is_equivalent(lhs): |
0 | 731 |
if rhs.is_equivalent(term): |
732 |
yield (i, rel.r_type, 'subject') |
|
6915
99eb71b311e4
[rset] fix entity building for some result set with UNION and subqueries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6859
diff
changeset
|
733 |
elif selectmain.is_equivalent(rhs): |
0 | 734 |
if lhs.is_equivalent(term): |
735 |
yield (i, rel.r_type, 'object') |