author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 20 Apr 2010 15:55:50 +0200 | |
changeset 5352 | be44c58d3ed9 |
parent 5302 | dfd147de06b2 |
child 5423 | e15abfdcce38 |
permissions | -rw-r--r-- |
0 | 1 |
"""RQL to SQL generator for native sources. |
2 |
||
3 |
||
4 |
SQL queries optimization |
|
5 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
6 |
1. CWUser X WHERE X in_group G, G name 'users': |
0 | 7 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
8 |
CWUser is the only subject entity type for the in_group relation, |
0 | 9 |
which allow us to do :: |
10 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
11 |
SELECT eid_from FROM in_group, CWGroup |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
12 |
WHERE in_group.eid_to = CWGroup.eid_from |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
13 |
AND CWGroup.name = 'users' |
0 | 14 |
|
15 |
||
16 |
2. Any X WHERE X nonfinal1 Y, Y nonfinal2 Z |
|
17 |
||
18 |
-> direct join between nonfinal1 and nonfinal2, whatever X,Y, Z (unless |
|
19 |
inlined...) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
20 |
|
0 | 21 |
NOT IMPLEMENTED (and quite hard to implement) |
22 |
||
23 |
Potential optimization information is collected by the querier, sql generation |
|
24 |
is done according to this information |
|
25 |
||
5280
7e13bb484a19
added note about interesting page about SQL implementation differences
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
26 |
cross RDMS note : read `Comparison of different SQL implementations`_ |
7e13bb484a19
added note about interesting page about SQL implementation differences
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
27 |
by Troels Arvin. Features SQL ISO Standard, PG, mysql, Oracle, MS SQL, DB2 |
7e13bb484a19
added note about interesting page about SQL implementation differences
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
28 |
and Informix. |
7e13bb484a19
added note about interesting page about SQL implementation differences
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
29 |
|
7e13bb484a19
added note about interesting page about SQL implementation differences
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
30 |
.. _Comparison of different SQL implementations: http://www.troels.arvin.dk/db/rdbms |
7e13bb484a19
added note about interesting page about SQL implementation differences
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
31 |
|
0 | 32 |
|
33 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4124
diff
changeset
|
34 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 35 |
: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:
1862
diff
changeset
|
36 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 37 |
""" |
38 |
__docformat__ = "restructuredtext en" |
|
39 |
||
40 |
import threading |
|
41 |
||
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
42 |
from logilab.database import FunctionDescr, SQL_FUNCTIONS_REGISTRY |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
43 |
|
0 | 44 |
from rql import BadRQLQuery, CoercionError |
45 |
from rql.stmts import Union, Select |
|
46 |
from rql.nodes import (SortTerm, VariableRef, Constant, Function, Not, |
|
47 |
Variable, ColumnAlias, Relation, SubQuery, Exists) |
|
48 |
||
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
49 |
from cubicweb import QueryError |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
50 |
from cubicweb.server.sqlutils import SQL_PREFIX |
0 | 51 |
from cubicweb.server.utils import cleanup_solutions |
52 |
||
4794
81075b09ebf1
[rql2sql] fix potential crash when testing _q_invariant on a ColumnAlias instead of a Variable by always considering _q_invariant to false for them
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
53 |
ColumnAlias._q_invariant = False # avoid to check for ColumnAlias / Variable |
81075b09ebf1
[rql2sql] fix potential crash when testing _q_invariant on a ColumnAlias instead of a Variable by always considering _q_invariant to false for them
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
54 |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
55 |
FunctionDescr.source_execute = None |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
56 |
|
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
57 |
def default_update_cb_stack(self, stack): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
58 |
stack.append(self.source_execute) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
59 |
FunctionDescr.update_cb_stack = default_update_cb_stack |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
60 |
|
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
61 |
LENGTH = SQL_FUNCTIONS_REGISTRY.get_function('LENGTH') |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
62 |
def length_source_execute(source, value): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
63 |
return len(value.getvalue()) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
64 |
LENGTH.source_execute = length_source_execute |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
65 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
66 |
def _new_var(select, varname): |
0 | 67 |
newvar = select.get_variable(varname) |
68 |
if not 'relations' in newvar.stinfo: |
|
69 |
# not yet initialized |
|
70 |
newvar.prepare_annotation() |
|
71 |
newvar.stinfo['scope'] = select |
|
72 |
newvar._q_invariant = False |
|
73 |
return newvar |
|
74 |
||
75 |
def _fill_to_wrap_rel(var, newselect, towrap, schema): |
|
76 |
for rel in var.stinfo['relations'] - var.stinfo['rhsrelations']: |
|
77 |
rschema = schema.rschema(rel.r_type) |
|
78 |
if rschema.inlined: |
|
79 |
towrap.add( (var, rel) ) |
|
80 |
for vref in rel.children[1].iget_nodes(VariableRef): |
|
81 |
newivar = _new_var(newselect, vref.name) |
|
82 |
newselect.selection.append(VariableRef(newivar)) |
|
83 |
_fill_to_wrap_rel(vref.variable, newselect, towrap, schema) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3245
diff
changeset
|
84 |
elif rschema.final: |
0 | 85 |
towrap.add( (var, rel) ) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
86 |
|
0 | 87 |
def rewrite_unstable_outer_join(select, solutions, unstable, schema): |
88 |
"""if some optional variables are unstable, they should be selected in a |
|
89 |
subquery. This function check this and rewrite the rql syntax tree if |
|
90 |
necessary (in place). Return a boolean telling if the tree has been modified |
|
91 |
""" |
|
92 |
torewrite = set() |
|
93 |
modified = False |
|
94 |
for varname in tuple(unstable): |
|
95 |
var = select.defined_vars[varname] |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
96 |
if not var.stinfo.get('optrelations'): |
0 | 97 |
continue |
98 |
modified = True |
|
99 |
unstable.remove(varname) |
|
100 |
torewrite.add(var) |
|
101 |
newselect = Select() |
|
339
c0a0ce6c0428
in some cases (eg ambiguous neged relations), INTERSECT should be used instead of DISTINCT
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
102 |
newselect.need_distinct = newselect.need_intersect = False |
0 | 103 |
myunion = Union() |
104 |
myunion.append(newselect) |
|
105 |
# extract aliases / selection |
|
106 |
newvar = _new_var(newselect, var.name) |
|
107 |
newselect.selection = [VariableRef(newvar)] |
|
108 |
for avar in select.defined_vars.itervalues(): |
|
109 |
if avar.stinfo['attrvar'] is var: |
|
110 |
newavar = _new_var(newselect, avar.name) |
|
111 |
newavar.stinfo['attrvar'] = newvar |
|
112 |
newselect.selection.append(VariableRef(newavar)) |
|
113 |
towrap_rels = set() |
|
114 |
_fill_to_wrap_rel(var, newselect, towrap_rels, schema) |
|
115 |
# extract relations |
|
116 |
for var, rel in towrap_rels: |
|
117 |
newrel = rel.copy(newselect) |
|
118 |
newselect.add_restriction(newrel) |
|
119 |
select.remove_node(rel) |
|
120 |
var.stinfo['relations'].remove(rel) |
|
121 |
newvar.stinfo['relations'].add(newrel) |
|
122 |
if rel.optional in ('left', 'both'): |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
123 |
newvar.add_optional_relation(newrel) |
0 | 124 |
for vref in newrel.children[1].iget_nodes(VariableRef): |
125 |
var = vref.variable |
|
126 |
var.stinfo['relations'].add(newrel) |
|
127 |
var.stinfo['rhsrelations'].add(newrel) |
|
128 |
if rel.optional in ('right', 'both'): |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
129 |
var.add_optional_relation(newrel) |
0 | 130 |
# extract subquery solutions |
3736
07196bda2456
take care to already existant solutions local variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
131 |
mysolutions = [sol.copy() for sol in solutions] |
07196bda2456
take care to already existant solutions local variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
132 |
cleanup_solutions(newselect, mysolutions) |
0 | 133 |
newselect.set_possible_types(solutions) |
134 |
# full sub-query |
|
135 |
aliases = [VariableRef(select.get_variable(avar.name, i)) |
|
136 |
for i, avar in enumerate(newselect.selection)] |
|
137 |
select.add_subquery(SubQuery(aliases, myunion), check=False) |
|
138 |
return modified |
|
139 |
||
140 |
def _new_solutions(rqlst, solutions): |
|
141 |
"""first filter out subqueries variables from solutions""" |
|
142 |
newsolutions = [] |
|
143 |
for origsol in solutions: |
|
144 |
asol = {} |
|
145 |
for vname in rqlst.defined_vars: |
|
146 |
asol[vname] = origsol[vname] |
|
147 |
if not asol in newsolutions: |
|
148 |
newsolutions.append(asol) |
|
149 |
return newsolutions |
|
150 |
||
151 |
def remove_unused_solutions(rqlst, solutions, varmap, schema): |
|
152 |
"""cleanup solutions: remove solutions where invariant variables are taking |
|
153 |
different types |
|
154 |
""" |
|
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
155 |
newsols = _new_solutions(rqlst, solutions) |
0 | 156 |
existssols = {} |
157 |
unstable = set() |
|
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
158 |
invariants = {} |
0 | 159 |
for vname, var in rqlst.defined_vars.iteritems(): |
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
160 |
vtype = newsols[0][vname] |
0 | 161 |
if var._q_invariant or vname in varmap: |
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
162 |
# remove invariant variable from solutions to remove duplicates |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
163 |
# later, then reinserting a type for the variable even later |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
164 |
for sol in newsols: |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
165 |
invariants.setdefault(id(sol), {})[vname] = sol.pop(vname) |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
166 |
elif var.scope is not rqlst: |
0 | 167 |
# move appart variables which are in a EXISTS scope and are variating |
168 |
try: |
|
169 |
thisexistssols, thisexistsvars = existssols[var.scope] |
|
170 |
except KeyError: |
|
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
171 |
thisexistssols = [newsols[0]] |
0 | 172 |
thisexistsvars = set() |
173 |
existssols[var.scope] = thisexistssols, thisexistsvars |
|
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
174 |
for i in xrange(len(newsols)-1, 0, -1): |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
175 |
if vtype != newsols[i][vname]: |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
176 |
thisexistssols.append(newsols.pop(i)) |
0 | 177 |
thisexistsvars.add(vname) |
178 |
else: |
|
179 |
# remember unstable variables |
|
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
180 |
for i in xrange(1, len(newsols)): |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
181 |
if vtype != newsols[i][vname]: |
0 | 182 |
unstable.add(vname) |
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
183 |
if invariants: |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
184 |
# filter out duplicates |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
185 |
newsols_ = [] |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
186 |
for sol in newsols: |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
187 |
if not sol in newsols_: |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
188 |
newsols_.append(sol) |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
189 |
newsols = newsols_ |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
190 |
# reinsert solutions for invariants |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
191 |
for sol in newsols: |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
192 |
for invvar, vartype in invariants[id(sol)].iteritems(): |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
193 |
sol[invvar] = vartype |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
194 |
for sol in existssols: |
3866
2783c166ad1a
fix potential key error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3852
diff
changeset
|
195 |
try: |
2783c166ad1a
fix potential key error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3852
diff
changeset
|
196 |
for invvar, vartype in invariants[id(sol)].iteritems(): |
2783c166ad1a
fix potential key error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3852
diff
changeset
|
197 |
sol[invvar] = vartype |
2783c166ad1a
fix potential key error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3852
diff
changeset
|
198 |
except KeyError: |
2783c166ad1a
fix potential key error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3852
diff
changeset
|
199 |
continue |
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
200 |
if len(newsols) > 1: |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
201 |
if rewrite_unstable_outer_join(rqlst, newsols, unstable, schema): |
0 | 202 |
# remove variables extracted to subqueries from solutions |
3852
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
203 |
newsols = _new_solutions(rqlst, newsols) |
03121ca1f85e
test and fix case where remove_unsused_solutions remove some solutions that should be kept
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3815
diff
changeset
|
204 |
return newsols, existssols, unstable |
0 | 205 |
|
206 |
def relation_info(relation): |
|
207 |
lhs, rhs = relation.get_variable_parts() |
|
208 |
try: |
|
209 |
lhs = lhs.variable |
|
210 |
lhsconst = lhs.stinfo['constnode'] |
|
211 |
except AttributeError: |
|
212 |
lhsconst = lhs |
|
213 |
lhs = None |
|
214 |
except KeyError: |
|
215 |
lhsconst = None # ColumnAlias |
|
216 |
try: |
|
217 |
rhs = rhs.variable |
|
218 |
rhsconst = rhs.stinfo['constnode'] |
|
219 |
except AttributeError: |
|
220 |
rhsconst = rhs |
|
221 |
rhs = None |
|
222 |
except KeyError: |
|
223 |
rhsconst = None # ColumnAlias |
|
224 |
return lhs, lhsconst, rhs, rhsconst |
|
225 |
||
226 |
def switch_relation_field(sql, table=''): |
|
227 |
switchedsql = sql.replace(table + '.eid_from', '__eid_from__') |
|
228 |
switchedsql = switchedsql.replace(table + '.eid_to', |
|
229 |
table + '.eid_from') |
|
230 |
return switchedsql.replace('__eid_from__', table + '.eid_to') |
|
231 |
||
232 |
def sort_term_selection(sorts, selectedidx, rqlst, groups): |
|
233 |
# XXX beurk |
|
234 |
if isinstance(rqlst, list): |
|
235 |
def append(term): |
|
236 |
rqlst.append(term) |
|
237 |
else: |
|
238 |
def append(term): |
|
239 |
rqlst.selection.append(term.copy(rqlst)) |
|
240 |
for sortterm in sorts: |
|
241 |
term = sortterm.term |
|
242 |
if not isinstance(term, Constant) and not str(term) in selectedidx: |
|
243 |
selectedidx.append(str(term)) |
|
244 |
append(term) |
|
245 |
if groups: |
|
246 |
for vref in term.iget_nodes(VariableRef): |
|
247 |
if not vref in groups: |
|
248 |
groups.append(vref) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
249 |
|
3752
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
250 |
def fix_selection_and_group(rqlst, selectedidx, needwrap, selectsortterms, |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
251 |
sorts, groups, having): |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
252 |
if selectsortterms and sorts: |
0 | 253 |
sort_term_selection(sorts, selectedidx, rqlst, not needwrap and groups) |
3752
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
254 |
if sorts and groups: |
3754 | 255 |
# when a query is grouped, ensure sort terms are grouped as well |
3752
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
256 |
for sortterm in sorts: |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
257 |
term = sortterm.term |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
258 |
if not isinstance(term, Constant): |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
259 |
for vref in term.iget_nodes(VariableRef): |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
260 |
if not vref in groups: |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
261 |
groups.append(vref) |
0 | 262 |
if needwrap: |
263 |
if groups: |
|
264 |
for vref in groups: |
|
265 |
if not vref.name in selectedidx: |
|
266 |
selectedidx.append(vref.name) |
|
267 |
rqlst.selection.append(vref) |
|
268 |
if having: |
|
269 |
for term in having: |
|
270 |
for vref in term.iget_nodes(VariableRef): |
|
271 |
if not vref.name in selectedidx: |
|
272 |
selectedidx.append(vref.name) |
|
273 |
rqlst.selection.append(vref) |
|
274 |
||
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
275 |
def iter_mapped_var_sels(stmt, variable): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
276 |
# variable is a Variable or ColumnAlias node mapped to a source side |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
277 |
# callback |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
278 |
if not (len(variable.stinfo['rhsrelations']) <= 1 and # < 1 on column alias |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
279 |
variable.stinfo['selected']): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
280 |
raise QueryError("can't use %s as a restriction variable" |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
281 |
% variable.name) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
282 |
for selectidx in variable.stinfo['selected']: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
283 |
vrefs = stmt.selection[selectidx].get_nodes(VariableRef) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
284 |
if len(vrefs) != 1: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
285 |
raise QueryError() |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
286 |
yield selectidx, vrefs[0] |
0 | 287 |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
288 |
def update_source_cb_stack(state, stmt, node, stack): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
289 |
while True: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
290 |
node = node.parent |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
291 |
if node is stmt: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
292 |
break |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
293 |
if not isinstance(node, Function): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
294 |
raise QueryError() |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
295 |
func = SQL_FUNCTIONS_REGISTRY.get_function(node.name) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
296 |
if func.source_execute is None: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
297 |
raise QueryError('%s can not be called on mapped attribute' |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
298 |
% node.name) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
299 |
state.source_cb_funcs.add(node) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
300 |
func.update_cb_stack(stack) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
301 |
|
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
302 |
|
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
303 |
# IGenerator implementation for RQL->SQL ####################################### |
0 | 304 |
|
305 |
class StateInfo(object): |
|
306 |
def __init__(self, existssols, unstablevars): |
|
307 |
self.existssols = existssols |
|
308 |
self.unstablevars = unstablevars |
|
309 |
self.subtables = {} |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
310 |
self.needs_source_cb = None |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
311 |
self.subquery_source_cb = None |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
312 |
self.source_cb_funcs = set() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
313 |
|
0 | 314 |
def reset(self, solution): |
315 |
"""reset some visit variables""" |
|
316 |
self.solution = solution |
|
317 |
self.count = 0 |
|
318 |
self.done = set() |
|
319 |
self.tables = self.subtables.copy() |
|
320 |
self.actual_tables = [[]] |
|
321 |
for _, tsql in self.tables.itervalues(): |
|
322 |
self.actual_tables[-1].append(tsql) |
|
323 |
self.outer_tables = {} |
|
324 |
self.duplicate_switches = [] |
|
325 |
self.aliases = {} |
|
326 |
self.restrictions = [] |
|
327 |
self._restr_stack = [] |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
328 |
self.ignore_varmap = False |
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
329 |
self._needs_source_cb = {} |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
330 |
|
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
331 |
def merge_source_cbs(self, needs_source_cb): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
332 |
if self.needs_source_cb is None: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
333 |
self.needs_source_cb = needs_source_cb |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
334 |
elif needs_source_cb != self.needs_source_cb: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
335 |
raise QueryError('query fetch some source mapped attribute, some not') |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
336 |
|
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
337 |
def finalize_source_cbs(self): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
338 |
if self.subquery_source_cb is not None: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
339 |
self.needs_source_cb.update(self.subquery_source_cb) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
340 |
|
0 | 341 |
def add_restriction(self, restr): |
342 |
if restr: |
|
343 |
self.restrictions.append(restr) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
344 |
|
0 | 345 |
def iter_exists_sols(self, exists): |
346 |
if not exists in self.existssols: |
|
347 |
yield 1 |
|
348 |
return |
|
349 |
thisexistssols, thisexistsvars = self.existssols[exists] |
|
350 |
origsol = self.solution |
|
351 |
origtables = self.tables |
|
352 |
done = self.done |
|
353 |
for thisexistssol in thisexistssols: |
|
354 |
for vname in self.unstablevars: |
|
355 |
if thisexistssol[vname] != origsol[vname] and vname in thisexistsvars: |
|
356 |
break |
|
357 |
else: |
|
358 |
self.tables = origtables.copy() |
|
359 |
self.solution = thisexistssol |
|
360 |
yield 1 |
|
361 |
# cleanup self.done from stuff specific to exists |
|
362 |
for var in thisexistsvars: |
|
363 |
if var in done: |
|
364 |
done.remove(var) |
|
365 |
for rel in exists.iget_nodes(Relation): |
|
366 |
if rel in done: |
|
367 |
done.remove(rel) |
|
368 |
self.solution = origsol |
|
369 |
self.tables = origtables |
|
370 |
||
371 |
def push_scope(self): |
|
372 |
self.actual_tables.append([]) |
|
373 |
self._restr_stack.append(self.restrictions) |
|
374 |
self.restrictions = [] |
|
375 |
||
376 |
def pop_scope(self): |
|
377 |
restrictions = self.restrictions |
|
378 |
self.restrictions = self._restr_stack.pop() |
|
379 |
return restrictions, self.actual_tables.pop() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
380 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
381 |
|
0 | 382 |
class SQLGenerator(object): |
383 |
""" |
|
384 |
generation of SQL from the fully expanded RQL syntax tree |
|
385 |
SQL is designed to be used with a CubicWeb SQL schema |
|
386 |
||
387 |
Groups and sort are not handled here since they should not be handled at |
|
388 |
this level (see cubicweb.server.querier) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
389 |
|
0 | 390 |
we should not have errors here ! |
391 |
||
392 |
WARNING: a CubicWebSQLGenerator instance is not thread safe, but generate is |
|
393 |
protected by a lock |
|
394 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
395 |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
396 |
def __init__(self, schema, dbhelper, attrmap=None): |
0 | 397 |
self.schema = schema |
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
398 |
self.dbhelper = dbhelper |
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
399 |
self.dbencoding = dbhelper.dbencoding |
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
400 |
self.keyword_map = {'NOW' : self.dbhelper.sql_current_timestamp, |
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
401 |
'TODAY': self.dbhelper.sql_current_date, |
0 | 402 |
} |
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
403 |
if not self.dbhelper.union_parentheses_support: |
0 | 404 |
self.union_sql = self.noparen_union_sql |
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
405 |
if self.dbhelper.fti_need_distinct: |
4836
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
406 |
self.__union_sql = self.union_sql |
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
407 |
self.union_sql = self.has_text_need_distinct_union_sql |
0 | 408 |
self._lock = threading.Lock() |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
409 |
if attrmap is None: |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
410 |
attrmap = {} |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
411 |
self.attr_map = attrmap |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
412 |
|
0 | 413 |
def generate(self, union, args=None, varmap=None): |
414 |
"""return SQL queries and a variable dictionnary from a RQL syntax tree |
|
415 |
||
416 |
:partrqls: a list of couple (rqlst, solutions) |
|
417 |
:args: optional dictionary with values of substitutions used in the query |
|
418 |
:varmap: optional dictionary mapping variable name to a special table |
|
419 |
name, in case the query as to fetch data from temporary tables |
|
420 |
||
421 |
return an sql string and a dictionary with substitutions values |
|
422 |
""" |
|
423 |
if args is None: |
|
424 |
args = {} |
|
425 |
if varmap is None: |
|
426 |
varmap = {} |
|
427 |
self._lock.acquire() |
|
428 |
self._args = args |
|
429 |
self._varmap = varmap |
|
430 |
self._query_attrs = {} |
|
431 |
self._state = None |
|
3238
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
432 |
self._not_scope_offset = 0 |
0 | 433 |
try: |
434 |
# union query for each rqlst / solution |
|
435 |
sql = self.union_sql(union) |
|
436 |
# we are done |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
437 |
return sql, self._query_attrs, self._state.needs_source_cb |
0 | 438 |
finally: |
439 |
self._lock.release() |
|
440 |
||
4836
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
441 |
def has_text_need_distinct_union_sql(self, union, needalias=False): |
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
442 |
if getattr(union, 'has_text_query', False): |
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
443 |
for select in union.children: |
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
444 |
select.need_distinct = True |
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
445 |
return self.__union_sql(union, needalias) |
3e3c4917e94e
[sql generation] fti_need_distinct quere was not anymore used, potentially causing duplicates on has_text queries with sqlite backend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4794
diff
changeset
|
446 |
|
0 | 447 |
def union_sql(self, union, needalias=False): # pylint: disable-msg=E0202 |
448 |
if len(union.children) == 1: |
|
449 |
return self.select_sql(union.children[0], needalias) |
|
450 |
sqls = ('(%s)' % self.select_sql(select, needalias) |
|
451 |
for select in union.children) |
|
452 |
return '\nUNION ALL\n'.join(sqls) |
|
453 |
||
454 |
def noparen_union_sql(self, union, needalias=False): |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
455 |
# needed for sqlite backend which doesn't like parentheses around union |
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
456 |
# query. This may cause bug in some condition (sort in one of the |
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
457 |
# subquery) but will work in most case |
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
458 |
# |
0 | 459 |
# see http://www.sqlite.org/cvstrac/tktview?tn=3074 |
460 |
sqls = (self.select_sql(select, needalias) |
|
461 |
for i, select in enumerate(union.children)) |
|
462 |
return '\nUNION ALL\n'.join(sqls) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
463 |
|
0 | 464 |
def select_sql(self, select, needalias=False): |
465 |
"""return SQL queries and a variable dictionnary from a RQL syntax tree |
|
466 |
||
467 |
:select: a selection statement of the syntax tree (`rql.stmts.Select`) |
|
468 |
:solution: a dictionnary containing variables binding. |
|
469 |
A solution's dictionnary has variable's names as key and variable's |
|
470 |
types as values |
|
471 |
:needwrap: boolean telling if the query will be wrapped in an outer |
|
472 |
query (to deal with aggregat and/or grouping) |
|
473 |
""" |
|
474 |
distinct = selectsortterms = select.need_distinct |
|
475 |
sorts = select.orderby |
|
476 |
groups = select.groupby |
|
477 |
having = select.having |
|
478 |
# remember selection, it may be changed and have to be restored |
|
479 |
origselection = select.selection[:] |
|
480 |
# check if the query will have union subquery, if it need sort term |
|
481 |
# selection (union or distinct query) and wrapping (union with groups) |
|
482 |
needwrap = False |
|
483 |
sols = select.solutions |
|
484 |
if len(sols) > 1: |
|
485 |
# remove invariant from solutions |
|
486 |
sols, existssols, unstable = remove_unused_solutions( |
|
487 |
select, sols, self._varmap, self.schema) |
|
488 |
if len(sols) > 1: |
|
489 |
# if there is still more than one solution, a UNION will be |
|
490 |
# generated and so sort terms have to be selected |
|
491 |
selectsortterms = True |
|
492 |
# and if select is using group by or aggregat, a wrapping |
|
493 |
# query will be necessary |
|
494 |
if groups or select.has_aggregat: |
|
495 |
select.select_only_variables() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
496 |
needwrap = True |
0 | 497 |
else: |
498 |
existssols, unstable = {}, () |
|
499 |
state = StateInfo(existssols, unstable) |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
500 |
if self._state is not None: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
501 |
# state from a previous unioned select |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
502 |
state.merge_source_cbs(self._state.needs_source_cb) |
0 | 503 |
# treat subqueries |
504 |
self._subqueries_sql(select, state) |
|
505 |
# generate sql for this select node |
|
506 |
selectidx = [str(term) for term in select.selection] |
|
507 |
if needwrap: |
|
508 |
outerselection = origselection[:] |
|
509 |
if sorts and selectsortterms: |
|
510 |
outerselectidx = [str(term) for term in outerselection] |
|
511 |
if distinct: |
|
512 |
sort_term_selection(sorts, outerselectidx, |
|
513 |
outerselection, groups) |
|
514 |
else: |
|
515 |
outerselectidx = selectidx[:] |
|
3752
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
516 |
fix_selection_and_group(select, selectidx, needwrap, |
4c77a1653374
when a query is grouped, ensure sort terms are grouped as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3736
diff
changeset
|
517 |
selectsortterms, sorts, groups, having) |
0 | 518 |
if needwrap: |
519 |
fselectidx = outerselectidx |
|
520 |
fneedwrap = len(outerselection) != len(origselection) |
|
521 |
else: |
|
522 |
fselectidx = selectidx |
|
523 |
fneedwrap = len(select.selection) != len(origselection) |
|
524 |
if fneedwrap: |
|
525 |
needalias = True |
|
526 |
self._in_wrapping_query = False |
|
527 |
self._state = state |
|
528 |
try: |
|
529 |
sql = self._solutions_sql(select, sols, distinct, needalias or needwrap) |
|
530 |
# generate groups / having before wrapping query selection to |
|
531 |
# get correct column aliases |
|
532 |
self._in_wrapping_query = needwrap |
|
533 |
if groups: |
|
534 |
# no constant should be inserted in GROUP BY else the backend will |
|
535 |
# interpret it as a positional index in the selection |
|
536 |
groups = ','.join(vref.accept(self) for vref in groups |
|
537 |
if not isinstance(vref, Constant)) |
|
538 |
if having: |
|
539 |
# filter out constants as for GROUP BY |
|
540 |
having = ','.join(vref.accept(self) for vref in having |
|
541 |
if not isinstance(vref, Constant)) |
|
542 |
if needwrap: |
|
543 |
sql = '%s FROM (%s) AS T1' % (self._selection_sql(outerselection, distinct, |
|
544 |
needalias), |
|
545 |
sql) |
|
546 |
if groups: |
|
547 |
sql += '\nGROUP BY %s' % groups |
|
548 |
if having: |
|
549 |
sql += '\nHAVING %s' % having |
|
550 |
# sort |
|
551 |
if sorts: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
552 |
sql += '\nORDER BY %s' % ','.join(self._sortterm_sql(sortterm, |
0 | 553 |
fselectidx) |
554 |
for sortterm in sorts) |
|
555 |
if fneedwrap: |
|
556 |
selection = ['T1.C%s' % i for i in xrange(len(origselection))] |
|
557 |
sql = 'SELECT %s FROM (%s) AS T1' % (','.join(selection), sql) |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
558 |
state.finalize_source_cbs() |
0 | 559 |
finally: |
560 |
select.selection = origselection |
|
561 |
# limit / offset |
|
562 |
limit = select.limit |
|
563 |
if limit: |
|
564 |
sql += '\nLIMIT %s' % limit |
|
565 |
offset = select.offset |
|
566 |
if offset: |
|
567 |
sql += '\nOFFSET %s' % offset |
|
568 |
return sql |
|
569 |
||
570 |
def _subqueries_sql(self, select, state): |
|
571 |
for i, subquery in enumerate(select.with_): |
|
572 |
sql = self.union_sql(subquery.query, needalias=True) |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
573 |
tablealias = '_T%s' % i # XXX nested subqueries |
0 | 574 |
sql = '(%s) AS %s' % (sql, tablealias) |
575 |
state.subtables[tablealias] = (0, sql) |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
576 |
latest_state = self._state |
0 | 577 |
for vref in subquery.aliases: |
578 |
alias = vref.variable |
|
579 |
alias._q_sqltable = tablealias |
|
580 |
alias._q_sql = '%s.C%s' % (tablealias, alias.colnum) |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
581 |
try: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
582 |
stack = latest_state.needs_source_cb[alias.colnum] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
583 |
if state.subquery_source_cb is None: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
584 |
state.subquery_source_cb = {} |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
585 |
for selectidx, vref in iter_mapped_var_sels(select, alias): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
586 |
stack = stack[:] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
587 |
update_source_cb_stack(state, select, vref, stack) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
588 |
state.subquery_source_cb[selectidx] = stack |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
589 |
except KeyError: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
590 |
continue |
0 | 591 |
|
592 |
def _solutions_sql(self, select, solutions, distinct, needalias): |
|
593 |
sqls = [] |
|
594 |
for solution in solutions: |
|
595 |
self._state.reset(solution) |
|
596 |
# visit restriction subtree |
|
597 |
if select.where is not None: |
|
598 |
self._state.add_restriction(select.where.accept(self)) |
|
599 |
sql = [self._selection_sql(select.selection, distinct, needalias)] |
|
600 |
if self._state.restrictions: |
|
601 |
sql.append('WHERE %s' % ' AND '.join(self._state.restrictions)) |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
602 |
self._state.merge_source_cbs(self._state._needs_source_cb) |
0 | 603 |
# add required tables |
604 |
assert len(self._state.actual_tables) == 1, self._state.actual_tables |
|
605 |
tables = self._state.actual_tables[-1] |
|
606 |
if tables: |
|
607 |
# sort for test predictability |
|
608 |
sql.insert(1, 'FROM %s' % ', '.join(sorted(tables))) |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
609 |
elif self._state.restrictions and self.dbhelper.needs_from_clause: |
0 | 610 |
sql.insert(1, 'FROM (SELECT 1) AS _T') |
611 |
sqls.append('\n'.join(sql)) |
|
339
c0a0ce6c0428
in some cases (eg ambiguous neged relations), INTERSECT should be used instead of DISTINCT
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
612 |
if select.need_intersect: |
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
613 |
#if distinct or not self.dbhelper.intersect_all_support: |
2199
bd0a0f219751
fix sql generated on NOT inlined_relation queries. Use exists, so no more needs for extra DISTINCT
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2073
diff
changeset
|
614 |
return '\nINTERSECT\n'.join(sqls) |
bd0a0f219751
fix sql generated on NOT inlined_relation queries. Use exists, so no more needs for extra DISTINCT
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2073
diff
changeset
|
615 |
#else: |
bd0a0f219751
fix sql generated on NOT inlined_relation queries. Use exists, so no more needs for extra DISTINCT
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2073
diff
changeset
|
616 |
# return '\nINTERSECT ALL\n'.join(sqls) |
339
c0a0ce6c0428
in some cases (eg ambiguous neged relations), INTERSECT should be used instead of DISTINCT
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
617 |
elif distinct: |
0 | 618 |
return '\nUNION\n'.join(sqls) |
619 |
else: |
|
620 |
return '\nUNION ALL\n'.join(sqls) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
621 |
|
0 | 622 |
def _selection_sql(self, selected, distinct, needaliasing=False): |
623 |
clause = [] |
|
624 |
for term in selected: |
|
625 |
sql = term.accept(self) |
|
626 |
if needaliasing: |
|
627 |
colalias = 'C%s' % len(clause) |
|
628 |
clause.append('%s AS %s' % (sql, colalias)) |
|
629 |
if isinstance(term, VariableRef): |
|
630 |
self._state.aliases[term.name] = colalias |
|
631 |
else: |
|
632 |
clause.append(sql) |
|
633 |
if distinct: |
|
634 |
return 'SELECT DISTINCT %s' % ', '.join(clause) |
|
635 |
return 'SELECT %s' % ', '.join(clause) |
|
636 |
||
637 |
def _sortterm_sql(self, sortterm, selectidx): |
|
638 |
term = sortterm.term |
|
639 |
try: |
|
640 |
sqlterm = str(selectidx.index(str(term)) + 1) |
|
641 |
except ValueError: |
|
642 |
# Constant node or non selected term |
|
643 |
sqlterm = str(term.accept(self)) |
|
644 |
if sortterm.asc: |
|
645 |
return sqlterm |
|
646 |
else: |
|
647 |
return '%s DESC' % sqlterm |
|
648 |
||
649 |
def visit_and(self, et): |
|
650 |
"""generate SQL for a AND subtree""" |
|
651 |
res = [] |
|
652 |
for c in et.children: |
|
653 |
part = c.accept(self) |
|
654 |
if part: |
|
655 |
res.append(part) |
|
656 |
return ' AND '.join(res) |
|
657 |
||
658 |
def visit_or(self, ou): |
|
659 |
"""generate SQL for a OR subtree""" |
|
660 |
res = [] |
|
661 |
for c in ou.children: |
|
662 |
part = c.accept(self) |
|
663 |
if part: |
|
664 |
res.append('(%s)' % part) |
|
665 |
if res: |
|
666 |
if len(res) > 1: |
|
667 |
return '(%s)' % ' OR '.join(res) |
|
668 |
return res[0] |
|
669 |
return '' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
670 |
|
0 | 671 |
def visit_not(self, node): |
672 |
self._state.push_scope() |
|
3238
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
673 |
if isinstance(node.children[0], Relation): |
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
674 |
self._not_scope_offset += 1 |
0 | 675 |
csql = node.children[0].accept(self) |
3238
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
676 |
if isinstance(node.children[0], Relation): |
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
677 |
self._not_scope_offset -= 1 |
0 | 678 |
sqls, tables = self._state.pop_scope() |
679 |
if node in self._state.done or not csql: |
|
680 |
# already processed or no sql generated by children |
|
681 |
self._state.actual_tables[-1] += tables |
|
682 |
self._state.restrictions += sqls |
|
683 |
return csql |
|
684 |
if isinstance(node.children[0], Exists): |
|
685 |
assert not sqls, (sqls, str(node.stmt)) |
|
686 |
assert not tables, (tables, str(node.stmt)) |
|
687 |
return 'NOT %s' % csql |
|
688 |
sqls.append(csql) |
|
689 |
if tables: |
|
690 |
select = 'SELECT 1 FROM %s' % ','.join(tables) |
|
691 |
else: |
|
692 |
select = 'SELECT 1' |
|
693 |
if sqls: |
|
694 |
sql = 'NOT EXISTS(%s WHERE %s)' % (select, ' AND '.join(sqls)) |
|
695 |
else: |
|
696 |
sql = 'NOT EXISTS(%s)' % select |
|
697 |
return sql |
|
698 |
||
699 |
def visit_exists(self, exists): |
|
700 |
"""generate SQL name for a exists subquery""" |
|
701 |
sqls = [] |
|
702 |
for dummy in self._state.iter_exists_sols(exists): |
|
703 |
sql = self._visit_exists(exists) |
|
704 |
if sql: |
|
705 |
sqls.append(sql) |
|
706 |
if not sqls: |
|
707 |
return '' |
|
708 |
return 'EXISTS(%s)' % ' UNION '.join(sqls) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
709 |
|
0 | 710 |
def _visit_exists(self, exists): |
711 |
self._state.push_scope() |
|
712 |
restriction = exists.children[0].accept(self) |
|
713 |
restrictions, tables = self._state.pop_scope() |
|
714 |
if restriction: |
|
715 |
restrictions.append(restriction) |
|
716 |
restriction = ' AND '.join(restrictions) |
|
717 |
if not restriction: |
|
718 |
return '' |
|
719 |
if not tables: |
|
720 |
# XXX could leave surrounding EXISTS() in this case no? |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
721 |
sql = 'SELECT 1 WHERE %s' % restriction |
0 | 722 |
else: |
723 |
sql = 'SELECT 1 FROM %s WHERE %s' % (', '.join(tables), restriction) |
|
724 |
return sql |
|
725 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
726 |
|
0 | 727 |
def visit_relation(self, relation): |
728 |
"""generate SQL for a relation""" |
|
729 |
rtype = relation.r_type |
|
730 |
# don't care of type constraint statement (i.e. relation_type = 'is') |
|
731 |
if relation.is_types_restriction(): |
|
732 |
return '' |
|
733 |
lhs, rhs = relation.get_parts() |
|
734 |
rschema = self.schema.rschema(rtype) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3245
diff
changeset
|
735 |
if rschema.final: |
0 | 736 |
if rtype == 'eid' and lhs.variable._q_invariant and \ |
737 |
lhs.variable.stinfo['constnode']: |
|
738 |
# special case where this restriction is already generated by |
|
739 |
# some other relation |
|
740 |
return '' |
|
741 |
# attribute relation |
|
742 |
if rtype == 'has_text': |
|
743 |
sql = self._visit_has_text_relation(relation) |
|
744 |
else: |
|
745 |
rhs_vars = rhs.get_nodes(VariableRef) |
|
746 |
if rhs_vars: |
|
747 |
# if variable(s) in the RHS |
|
748 |
sql = self._visit_var_attr_relation(relation, rhs_vars) |
|
749 |
else: |
|
750 |
# no variables in the RHS |
|
751 |
sql = self._visit_attribute_relation(relation) |
|
752 |
if relation.neged(strict=True): |
|
753 |
self._state.done.add(relation.parent) |
|
754 |
sql = 'NOT (%s)' % sql |
|
755 |
else: |
|
756 |
if rtype == 'is' and rhs.operator == 'IS': |
|
757 |
# special case "C is NULL" |
|
758 |
if lhs.name in self._varmap: |
|
759 |
lhssql = self._varmap[lhs.name] |
|
760 |
else: |
|
761 |
lhssql = lhs.accept(self) |
|
762 |
return '%s%s' % (lhssql, rhs.accept(self)) |
|
763 |
if '%s.%s' % (lhs, relation.r_type) in self._varmap: |
|
764 |
# relation has already been processed by a previous step |
|
765 |
return |
|
766 |
if relation.optional: |
|
767 |
# check it has not already been treaten (to get necessary |
|
768 |
# information to add an outer join condition) |
|
769 |
if relation in self._state.done: |
|
770 |
return |
|
771 |
# OPTIONAL relation, generate a left|right outer join |
|
772 |
sql = self._visit_outer_join_relation(relation, rschema) |
|
773 |
elif rschema.inlined: |
|
774 |
sql = self._visit_inlined_relation(relation) |
|
775 |
else: |
|
776 |
# regular (non final) relation |
|
777 |
sql = self._visit_relation(relation, rschema) |
|
778 |
return sql |
|
779 |
||
780 |
def _visit_inlined_relation(self, relation): |
|
781 |
lhsvar, _, rhsvar, rhsconst = relation_info(relation) |
|
782 |
# we are sure here to have a lhsvar |
|
783 |
assert lhsvar is not None |
|
3989
8b471739fb33
héhé, get the same result with a slightly enhanced condition and all this code removed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3988
diff
changeset
|
784 |
if isinstance(relation.parent, Not) \ |
8b471739fb33
héhé, get the same result with a slightly enhanced condition and all this code removed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3988
diff
changeset
|
785 |
and len(lhsvar.stinfo['relations']) > 1 \ |
8b471739fb33
héhé, get the same result with a slightly enhanced condition and all this code removed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3988
diff
changeset
|
786 |
and (rhsvar is None or rhsvar._q_invariant): |
0 | 787 |
self._state.done.add(relation.parent) |
3989
8b471739fb33
héhé, get the same result with a slightly enhanced condition and all this code removed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3988
diff
changeset
|
788 |
return '%s IS NULL' % self._inlined_var_sql(lhsvar, relation.r_type) |
2199
bd0a0f219751
fix sql generated on NOT inlined_relation queries. Use exists, so no more needs for extra DISTINCT
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2073
diff
changeset
|
789 |
lhssql = self._inlined_var_sql(lhsvar, relation.r_type) |
0 | 790 |
if rhsconst is not None: |
791 |
return '%s=%s' % (lhssql, rhsconst.accept(self)) |
|
792 |
if isinstance(rhsvar, Variable) and not rhsvar.name in self._varmap: |
|
793 |
# if the rhs variable is only linked to this relation, this mean we |
|
794 |
# only want the relation to exists, eg NOT NULL in case of inlined |
|
795 |
# relation |
|
796 |
if len(rhsvar.stinfo['relations']) == 1 and rhsvar._q_invariant: |
|
797 |
return '%s IS NOT NULL' % lhssql |
|
798 |
if rhsvar._q_invariant: |
|
799 |
return self._extra_join_sql(relation, lhssql, rhsvar) |
|
800 |
return '%s=%s' % (lhssql, rhsvar.accept(self)) |
|
801 |
||
802 |
def _process_relation_term(self, relation, rid, termvar, termconst, relfield): |
|
4794
81075b09ebf1
[rql2sql] fix potential crash when testing _q_invariant on a ColumnAlias instead of a Variable by always considering _q_invariant to false for them
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
803 |
if termconst or not termvar._q_invariant: |
0 | 804 |
termsql = termconst and termconst.accept(self) or termvar.accept(self) |
805 |
yield '%s.%s=%s' % (rid, relfield, termsql) |
|
806 |
elif termvar._q_invariant: |
|
807 |
# if the variable is mapped, generate restriction anyway |
|
808 |
if termvar.name in self._varmap: |
|
809 |
termsql = termvar.accept(self) |
|
810 |
yield '%s.%s=%s' % (rid, relfield, termsql) |
|
811 |
extrajoin = self._extra_join_sql(relation, '%s.%s' % (rid, relfield), termvar) |
|
812 |
if extrajoin: |
|
813 |
yield extrajoin |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
814 |
|
0 | 815 |
def _visit_relation(self, relation, rschema): |
816 |
"""generate SQL for a relation |
|
817 |
||
818 |
implements optimization 1. |
|
819 |
""" |
|
820 |
if relation.r_type == 'identity': |
|
821 |
# special case "X identity Y" |
|
822 |
lhs, rhs = relation.get_parts() |
|
823 |
if isinstance(relation.parent, Not): |
|
824 |
self._state.done.add(relation.parent) |
|
825 |
return 'NOT %s%s' % (lhs.accept(self), rhs.accept(self)) |
|
826 |
return '%s%s' % (lhs.accept(self), rhs.accept(self)) |
|
827 |
lhsvar, lhsconst, rhsvar, rhsconst = relation_info(relation) |
|
828 |
rid = self._relation_table(relation) |
|
829 |
sqls = [] |
|
830 |
sqls += self._process_relation_term(relation, rid, lhsvar, lhsconst, 'eid_from') |
|
831 |
sqls += self._process_relation_term(relation, rid, rhsvar, rhsconst, 'eid_to') |
|
832 |
sql = ' AND '.join(sqls) |
|
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4286
diff
changeset
|
833 |
if rschema.symmetric: |
0 | 834 |
sql = '(%s OR %s)' % (sql, switch_relation_field(sql)) |
835 |
return sql |
|
836 |
||
837 |
def _visit_outer_join_relation(self, relation, rschema): |
|
838 |
""" |
|
839 |
left outer join syntax (optional=='right'): |
|
840 |
X relation Y? |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
841 |
|
0 | 842 |
right outer join syntax (optional=='left'): |
843 |
X? relation Y |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
844 |
|
0 | 845 |
full outer join syntaxes (optional=='both'): |
846 |
X? relation Y? |
|
847 |
||
848 |
if relation is inlined: |
|
849 |
if it's a left outer join: |
|
850 |
-> X LEFT OUTER JOIN Y ON (X.relation=Y.eid) |
|
851 |
elif it's a right outer join: |
|
852 |
-> Y LEFT OUTER JOIN X ON (X.relation=Y.eid) |
|
853 |
elif it's a full outer join: |
|
854 |
-> X FULL OUTER JOIN Y ON (X.relation=Y.eid) |
|
855 |
else: |
|
856 |
if it's a left outer join: |
|
857 |
-> X LEFT OUTER JOIN relation ON (relation.eid_from=X.eid) |
|
858 |
LEFT OUTER JOIN Y ON (relation.eid_to=Y.eid) |
|
859 |
elif it's a right outer join: |
|
860 |
-> Y LEFT OUTER JOIN relation ON (relation.eid_to=Y.eid) |
|
861 |
LEFT OUTER JOIN X ON (relation.eid_from=X.eid) |
|
862 |
elif it's a full outer join: |
|
863 |
-> X FULL OUTER JOIN Y ON (X.relation=Y.eid) |
|
864 |
""" |
|
865 |
lhsvar, lhsconst, rhsvar, rhsconst = relation_info(relation) |
|
866 |
if relation.optional == 'right': |
|
867 |
joinattr, restrattr = 'eid_from', 'eid_to' |
|
868 |
else: |
|
869 |
lhsvar, rhsvar = rhsvar, lhsvar |
|
870 |
lhsconst, rhsconst = rhsconst, lhsconst |
|
871 |
joinattr, restrattr = 'eid_to', 'eid_from' |
|
872 |
if relation.optional == 'both': |
|
873 |
outertype = 'FULL' |
|
874 |
else: |
|
875 |
outertype = 'LEFT' |
|
876 |
if rschema.inlined or relation.r_type == 'identity': |
|
877 |
self._state.done.add(relation) |
|
878 |
t1 = self._var_table(lhsvar) |
|
879 |
if relation.r_type == 'identity': |
|
880 |
attr = 'eid' |
|
881 |
else: |
|
882 |
attr = relation.r_type |
|
883 |
# reset lhs/rhs, we need the initial order now |
|
884 |
lhs, rhs = relation.get_variable_parts() |
|
885 |
if '%s.%s' % (lhs.name, attr) in self._varmap: |
|
886 |
lhssql = self._varmap['%s.%s' % (lhs.name, attr)] |
|
887 |
else: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
888 |
lhssql = '%s.%s%s' % (self._var_table(lhs.variable), SQL_PREFIX, attr) |
0 | 889 |
if not rhsvar is None: |
890 |
t2 = self._var_table(rhsvar) |
|
891 |
if t2 is None: |
|
892 |
if rhsconst is not None: |
|
893 |
# inlined relation with invariant as rhs |
|
894 |
condition = '%s=%s' % (lhssql, rhsconst.accept(self)) |
|
895 |
if relation.r_type != 'identity': |
|
896 |
condition = '(%s OR %s IS NULL)' % (condition, lhssql) |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
897 |
if not lhsvar.stinfo.get('optrelations'): |
0 | 898 |
return condition |
899 |
self.add_outer_join_condition(lhsvar, t1, condition) |
|
900 |
return |
|
901 |
else: |
|
902 |
condition = '%s=%s' % (lhssql, rhsconst.accept(self)) |
|
903 |
self.add_outer_join_condition(lhsvar, t1, condition) |
|
904 |
join = '%s OUTER JOIN %s ON (%s=%s)' % ( |
|
905 |
outertype, self._state.tables[t2][1], lhssql, rhs.accept(self)) |
|
906 |
self.replace_tables_by_outer_join(join, t1, t2) |
|
907 |
return '' |
|
908 |
lhssql = lhsconst and lhsconst.accept(self) or lhsvar.accept(self) |
|
909 |
rhssql = rhsconst and rhsconst.accept(self) or rhsvar.accept(self) |
|
910 |
rid = self._relation_table(relation) |
|
911 |
if not lhsvar: |
|
912 |
join = '' |
|
913 |
toreplace = [] |
|
914 |
maintable = rid |
|
915 |
else: |
|
916 |
join = '%s OUTER JOIN %s ON (%s.%s=%s' % ( |
|
917 |
outertype, self._state.tables[rid][1], rid, joinattr, lhssql) |
|
918 |
toreplace = [rid] |
|
919 |
maintable = self._var_table(lhsvar) |
|
920 |
if rhsconst: |
|
921 |
join += ' AND %s.%s=%s)' % (rid, restrattr, rhssql) |
|
922 |
else: |
|
923 |
join += ')' |
|
924 |
if not rhsconst: |
|
1122
9f37de24251f
fix rql2sq w/ outer join on subquery result
sylvain.thenault@logilab.fr
parents:
438
diff
changeset
|
925 |
rhstable = rhsvar._q_sqltable |
0 | 926 |
if rhstable: |
927 |
assert rhstable is not None, rhsvar |
|
928 |
join += ' %s OUTER JOIN %s ON (%s.%s=%s)' % ( |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
929 |
outertype, self._state.tables[rhstable][1], rid, restrattr, |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
930 |
rhssql) |
0 | 931 |
toreplace.append(rhstable) |
932 |
self.replace_tables_by_outer_join(join, maintable, *toreplace) |
|
933 |
return '' |
|
934 |
||
935 |
def _visit_var_attr_relation(self, relation, rhs_vars): |
|
936 |
"""visit an attribute relation with variable(s) in the RHS |
|
937 |
||
938 |
attribute variables are used either in the selection or for |
|
939 |
unification (eg X attr1 A, Y attr2 A). In case of selection, |
|
940 |
nothing to do here. |
|
941 |
""" |
|
942 |
contextrels = {} |
|
943 |
for var in rhs_vars: |
|
2073
173c646981a7
fix missing from close when using a var map
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
944 |
if var.name in self._varmap: |
173c646981a7
fix missing from close when using a var map
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
945 |
# ensure table is added |
173c646981a7
fix missing from close when using a var map
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
946 |
self._var_info(var.variable) |
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
947 |
principal = var.variable.stinfo.get('principal') |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
948 |
if principal is not None and principal is not relation: |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
949 |
contextrels[var.name] = relation |
0 | 950 |
if not contextrels: |
951 |
return '' |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
952 |
# we have to generate unification expression |
0 | 953 |
lhssql = self._inlined_var_sql(relation.children[0].variable, |
954 |
relation.r_type) |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
955 |
try: |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
956 |
self._state.ignore_varmap = True |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
957 |
return '%s%s' % (lhssql, relation.children[1].accept(self)) |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
958 |
finally: |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
959 |
self._state.ignore_varmap = False |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
960 |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
961 |
def _visit_attribute_relation(self, rel): |
0 | 962 |
"""generate SQL for an attribute relation""" |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
963 |
lhs, rhs = rel.get_parts() |
0 | 964 |
rhssql = rhs.accept(self) |
965 |
table = self._var_table(lhs.variable) |
|
966 |
if table is None: |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
967 |
assert rel.r_type == 'eid' |
0 | 968 |
lhssql = lhs.accept(self) |
969 |
else: |
|
970 |
try: |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
971 |
lhssql = self._varmap['%s.%s' % (lhs.name, rel.r_type)] |
0 | 972 |
except KeyError: |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
973 |
mapkey = '%s.%s' % (self._state.solution[lhs.name], rel.r_type) |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
974 |
if mapkey in self.attr_map: |
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
975 |
cb, sourcecb = self.attr_map[mapkey] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
976 |
if sourcecb: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
977 |
# callback is a source callback, we can't use this |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
978 |
# attribute in restriction |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
979 |
raise QueryError("can't use %s (%s) in restriction" |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
980 |
% (mapkey, rel.as_string())) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
981 |
lhssql = cb(self, lhs.variable, rel) |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
982 |
elif rel.r_type == 'eid': |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
983 |
lhssql = lhs.variable._q_sql |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
984 |
else: |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
985 |
lhssql = '%s.%s%s' % (table, SQL_PREFIX, rel.r_type) |
0 | 986 |
try: |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
987 |
if rel._q_needcast == 'TODAY': |
0 | 988 |
sql = 'DATE(%s)%s' % (lhssql, rhssql) |
989 |
# XXX which cast function should be used |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
990 |
#elif rel._q_needcast == 'NOW': |
0 | 991 |
# sql = 'TIMESTAMP(%s)%s' % (lhssql, rhssql) |
992 |
else: |
|
993 |
sql = '%s%s' % (lhssql, rhssql) |
|
994 |
except AttributeError: |
|
995 |
sql = '%s%s' % (lhssql, rhssql) |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
996 |
if lhs.variable.stinfo.get('optrelations'): |
0 | 997 |
self.add_outer_join_condition(lhs.variable, table, sql) |
998 |
else: |
|
999 |
return sql |
|
1000 |
||
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
1001 |
def _visit_has_text_relation(self, rel): |
0 | 1002 |
"""generate SQL for a has_text relation""" |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
1003 |
lhs, rhs = rel.get_parts() |
0 | 1004 |
const = rhs.children[0] |
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
1005 |
alias = self._fti_table(rel) |
0 | 1006 |
jointo = lhs.accept(self) |
1007 |
restriction = '' |
|
1008 |
lhsvar = lhs.variable |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
1009 |
me_is_principal = lhsvar.stinfo.get('principal') is rel |
0 | 1010 |
if me_is_principal: |
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1011 |
if lhsvar.stinfo['typerel'] is None: |
0 | 1012 |
# the variable is using the fti table, no join needed |
1013 |
jointo = None |
|
1014 |
elif not lhsvar.name in self._varmap: |
|
1015 |
# join on entities instead of etype's table to get result for |
|
1016 |
# external entities on multisources configurations |
|
3762
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1017 |
ealias = lhsvar._q_sqltable = '_' + lhsvar.name |
0 | 1018 |
jointo = lhsvar._q_sql = '%s.eid' % ealias |
1019 |
self.add_table('entities AS %s' % ealias, ealias) |
|
1020 |
if not lhsvar._q_invariant or len(lhsvar.stinfo['possibletypes']) == 1: |
|
1021 |
restriction = " AND %s.type='%s'" % (ealias, self._state.solution[lhs.name]) |
|
1022 |
else: |
|
1023 |
etypes = ','.join("'%s'" % etype for etype in lhsvar.stinfo['possibletypes']) |
|
1024 |
restriction = " AND %s.type IN (%s)" % (ealias, etypes) |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
1025 |
if isinstance(rel.parent, Not): |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
1026 |
self._state.done.add(rel.parent) |
0 | 1027 |
not_ = True |
1028 |
else: |
|
1029 |
not_ = False |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1030 |
return self.dbhelper.fti_restriction_sql(alias, const.eval(self._args), |
0 | 1031 |
jointo, not_) + restriction |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1032 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1033 |
def visit_comparison(self, cmp): |
3787
82bb2c7f083b
C - fix typo. make `lgp check` happy.
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3762
diff
changeset
|
1034 |
"""generate SQL for a comparison""" |
0 | 1035 |
if len(cmp.children) == 2: |
1862
94dc8ccd320b
#343322: should generate IS NULL in sql w/ None values in substitution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
1036 |
# XXX occurs ? |
0 | 1037 |
lhs, rhs = cmp.children |
1038 |
else: |
|
1039 |
lhs = None |
|
1040 |
rhs = cmp.children[0] |
|
1041 |
operator = cmp.operator |
|
1042 |
if operator in ('IS', 'LIKE', 'ILIKE'): |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1043 |
if operator == 'ILIKE' and not self.dbhelper.ilike_support: |
0 | 1044 |
operator = ' LIKE ' |
1045 |
else: |
|
1046 |
operator = ' %s ' % operator |
|
1862
94dc8ccd320b
#343322: should generate IS NULL in sql w/ None values in substitution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
1047 |
elif (operator == '=' and isinstance(rhs, Constant) |
94dc8ccd320b
#343322: should generate IS NULL in sql w/ None values in substitution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
1048 |
and rhs.eval(self._args) is None): |
94dc8ccd320b
#343322: should generate IS NULL in sql w/ None values in substitution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
1049 |
if lhs is None: |
94dc8ccd320b
#343322: should generate IS NULL in sql w/ None values in substitution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
1050 |
return ' IS NULL' |
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1051 |
return '%s IS NULL' % lhs.accept(self) |
0 | 1052 |
elif isinstance(rhs, Function) and rhs.name == 'IN': |
1053 |
assert operator == '=' |
|
1054 |
operator = ' ' |
|
1055 |
if lhs is None: |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1056 |
return '%s%s'% (operator, rhs.accept(self)) |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1057 |
return '%s%s%s'% (lhs.accept(self), operator, rhs.accept(self)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1058 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1059 |
def visit_mathexpression(self, mexpr): |
0 | 1060 |
"""generate SQL for a mathematic expression""" |
1061 |
lhs, rhs = mexpr.get_parts() |
|
1062 |
# check for string concatenation |
|
1063 |
operator = mexpr.operator |
|
1064 |
try: |
|
1065 |
if mexpr.operator == '+' and mexpr.get_type(self._state.solution, self._args) == 'String': |
|
1066 |
operator = '||' |
|
1067 |
except CoercionError: |
|
1068 |
pass |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1069 |
return '(%s %s %s)'% (lhs.accept(self), operator, rhs.accept(self)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1070 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1071 |
def visit_function(self, func): |
0 | 1072 |
"""generate SQL name for a function""" |
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1073 |
args = [c.accept(self) for c in func.children] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1074 |
if func in self._state.source_cb_funcs: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1075 |
# function executed as a callback on the source |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1076 |
assert len(args) == 1 |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1077 |
return args[0] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1078 |
# func_as_sql will check function is supported by the backend |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1079 |
return self.dbhelper.func_as_sql(func.name, args) |
0 | 1080 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1081 |
def visit_constant(self, constant): |
0 | 1082 |
"""generate SQL name for a constant""" |
1083 |
value = constant.value |
|
1084 |
if constant.type is None: |
|
1085 |
return 'NULL' |
|
1086 |
if constant.type == 'Int' and isinstance(constant.parent, SortTerm): |
|
1087 |
return constant.value |
|
1088 |
if constant.type in ('Date', 'Datetime'): |
|
1089 |
rel = constant.relation() |
|
1090 |
if rel is not None: |
|
1091 |
rel._q_needcast = value |
|
1092 |
return self.keyword_map[value]() |
|
1497
54fc5cc52210
use dbmshelper to generate correct boolean value in rql2sql
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1382
diff
changeset
|
1093 |
if constant.type == 'Boolean': |
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1094 |
value = self.dbhelper.boolean_value(value) |
4124
8f2f5f0a89e7
fix backout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4121
diff
changeset
|
1095 |
if constant.type == 'Substitute': |
0 | 1096 |
_id = constant.value |
1097 |
if isinstance(_id, unicode): |
|
1098 |
_id = _id.encode() |
|
1099 |
else: |
|
1100 |
_id = str(id(constant)).replace('-', '', 1) |
|
1101 |
if isinstance(value, unicode): |
|
1102 |
value = value.encode(self.dbencoding) |
|
1103 |
self._query_attrs[_id] = value |
|
1104 |
return '%%(%s)s' % _id |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1105 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1106 |
def visit_variableref(self, variableref): |
0 | 1107 |
"""get the sql name for a variable reference""" |
1108 |
# use accept, .variable may be a variable or a columnalias |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1109 |
return variableref.variable.accept(self) |
0 | 1110 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1111 |
def visit_columnalias(self, colalias): |
0 | 1112 |
"""get the sql name for a subquery column alias""" |
1113 |
if colalias.name in self._varmap: |
|
1114 |
sql = self._varmap[colalias.name] |
|
1122
9f37de24251f
fix rql2sq w/ outer join on subquery result
sylvain.thenault@logilab.fr
parents:
438
diff
changeset
|
1115 |
table = sql.split('.', 1)[0] |
9f37de24251f
fix rql2sq w/ outer join on subquery result
sylvain.thenault@logilab.fr
parents:
438
diff
changeset
|
1116 |
colalias._q_sqltable = table |
9f37de24251f
fix rql2sq w/ outer join on subquery result
sylvain.thenault@logilab.fr
parents:
438
diff
changeset
|
1117 |
colalias._q_sql = sql |
9f37de24251f
fix rql2sq w/ outer join on subquery result
sylvain.thenault@logilab.fr
parents:
438
diff
changeset
|
1118 |
self.add_table(table) |
0 | 1119 |
return sql |
1120 |
return colalias._q_sql |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1121 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1122 |
def visit_variable(self, variable): |
0 | 1123 |
"""get the table name and sql string for a variable""" |
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1124 |
#if contextrels is None and variable.name in self._state.done: |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1125 |
if variable.name in self._state.done: |
0 | 1126 |
if self._in_wrapping_query: |
1127 |
return 'T1.%s' % self._state.aliases[variable.name] |
|
1128 |
return variable._q_sql |
|
1129 |
self._state.done.add(variable.name) |
|
1130 |
vtablename = None |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1131 |
if not self._state.ignore_varmap and variable.name in self._varmap: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1132 |
sql, vtablename = self._var_info(variable) |
0 | 1133 |
elif variable.stinfo['attrvar']: |
1134 |
# attribute variable (systematically used in rhs of final |
|
1135 |
# relation(s)), get table name and sql from any rhs relation |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1136 |
sql = self._linked_var_sql(variable) |
0 | 1137 |
elif variable._q_invariant: |
1138 |
# since variable is invariant, we know we won't found final relation |
|
1139 |
principal = variable.stinfo['principal'] |
|
1140 |
if principal is None: |
|
3762
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1141 |
vtablename = '_' + variable.name |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
1142 |
self.add_table('entities AS %s' % vtablename, vtablename) |
0 | 1143 |
sql = '%s.eid' % vtablename |
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1144 |
if variable.stinfo['typerel'] is not None: |
0 | 1145 |
# add additional restriction on entities.type column |
1146 |
pts = variable.stinfo['possibletypes'] |
|
1147 |
if len(pts) == 1: |
|
1148 |
etype = iter(variable.stinfo['possibletypes']).next() |
|
1149 |
restr = "%s.type='%s'" % (vtablename, etype) |
|
1150 |
else: |
|
1151 |
etypes = ','.join("'%s'" % et for et in pts) |
|
1152 |
restr = '%s.type IN (%s)' % (vtablename, etypes) |
|
1153 |
self._state.add_restriction(restr) |
|
1154 |
elif principal.r_type == 'has_text': |
|
1155 |
sql = '%s.%s' % (self._fti_table(principal), |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1156 |
self.dbhelper.fti_uid_attr) |
0 | 1157 |
elif principal in variable.stinfo['rhsrelations']: |
1158 |
if self.schema.rschema(principal.r_type).inlined: |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1159 |
sql = self._linked_var_sql(variable) |
0 | 1160 |
else: |
1161 |
sql = '%s.eid_to' % self._relation_table(principal) |
|
1162 |
else: |
|
1163 |
sql = '%s.eid_from' % self._relation_table(principal) |
|
1164 |
else: |
|
1165 |
# standard variable: get table name according to etype and use .eid |
|
1166 |
# attribute |
|
1167 |
sql, vtablename = self._var_info(variable) |
|
1168 |
variable._q_sqltable = vtablename |
|
1169 |
variable._q_sql = sql |
|
1170 |
return sql |
|
1171 |
||
1172 |
# various utilities ####################################################### |
|
1173 |
||
1174 |
def _extra_join_sql(self, relation, sql, var): |
|
1175 |
# if rhs var is invariant, and this relation is not its principal, |
|
1176 |
# generate extra join |
|
1177 |
try: |
|
1178 |
if not var.stinfo['principal'] is relation: |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1179 |
op = relation.operator() |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1180 |
if op == '=': |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1181 |
# need a predicable result for tests |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1182 |
args = sorted( (sql, var.accept(self)) ) |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1183 |
args.insert(1, op) |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1184 |
else: |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1185 |
args = (sql, op, var.accept(self)) |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1186 |
return '%s%s%s' % tuple(args) |
0 | 1187 |
except KeyError: |
1188 |
# no principal defined, relation is necessarily the principal and |
|
1189 |
# so nothing to return here |
|
1190 |
pass |
|
1191 |
return '' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1192 |
|
0 | 1193 |
def _var_info(self, var): |
1194 |
# if current var or one of its attribute is selected , it *must* |
|
1195 |
# appear in the toplevel's FROM even if we're currently visiting |
|
1196 |
# a EXISTS node |
|
1197 |
if var.sqlscope is var.stmt: |
|
1198 |
scope = 0 |
|
3238
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
1199 |
# don't consider not_scope_offset if the variable is only used in one |
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
1200 |
# relation |
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
1201 |
elif len(var.stinfo['relations']) > 1: |
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
1202 |
scope = -1 - self._not_scope_offset |
0 | 1203 |
else: |
1204 |
scope = -1 |
|
1205 |
try: |
|
1206 |
sql = self._varmap[var.name] |
|
3762
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1207 |
tablealias = sql.split('.', 1)[0] |
3238
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
1208 |
if scope < 0: |
3762
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1209 |
scope = self._varmap_table_scope(var.stmt, tablealias) |
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1210 |
self.add_table(tablealias, scope=scope) |
0 | 1211 |
except KeyError: |
1212 |
etype = self._state.solution[var.name] |
|
1213 |
# XXX this check should be moved in rql.stcheck |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3245
diff
changeset
|
1214 |
if self.schema.eschema(etype).final: |
0 | 1215 |
raise BadRQLQuery(var.stmt.root) |
3762
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1216 |
tablealias = '_' + var.name |
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1217 |
sql = '%s.%seid' % (tablealias, SQL_PREFIX) |
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1218 |
self.add_table('%s%s AS %s' % (SQL_PREFIX, etype, tablealias), |
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1219 |
tablealias, scope=scope) |
e416186fb91c
prefix sql aliases for entity table by '_' to avoid pb with variable such as 'AS' (eg a keyword in SQL)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3754
diff
changeset
|
1220 |
return sql, tablealias |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1221 |
|
0 | 1222 |
def _inlined_var_sql(self, var, rtype): |
1223 |
try: |
|
1224 |
sql = self._varmap['%s.%s' % (var.name, rtype)] |
|
1225 |
scope = var.sqlscope is var.stmt and 0 or -1 |
|
1226 |
self.add_table(sql.split('.', 1)[0], scope=scope) |
|
1227 |
except KeyError: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
1228 |
sql = '%s.%s%s' % (self._var_table(var), SQL_PREFIX, rtype) |
0 | 1229 |
#self._state.done.add(var.name) |
1230 |
return sql |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1231 |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1232 |
def _linked_var_sql(self, variable): |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1233 |
if not self._state.ignore_varmap: |
0 | 1234 |
try: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1235 |
return self._varmap[variable.name] |
0 | 1236 |
except KeyError: |
1237 |
pass |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3787
diff
changeset
|
1238 |
rel = (variable.stinfo.get('principal') or |
0 | 1239 |
iter(variable.stinfo['rhsrelations']).next()) |
1240 |
linkedvar = rel.children[0].variable |
|
1241 |
if rel.r_type == 'eid': |
|
1242 |
return linkedvar.accept(self) |
|
1243 |
if isinstance(linkedvar, ColumnAlias): |
|
1244 |
raise BadRQLQuery('variable %s should be selected by the subquery' |
|
1245 |
% variable.name) |
|
1246 |
try: |
|
1247 |
sql = self._varmap['%s.%s' % (linkedvar.name, rel.r_type)] |
|
1248 |
except KeyError: |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1249 |
mapkey = '%s.%s' % (self._state.solution[linkedvar.name], rel.r_type) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1250 |
if mapkey in self.attr_map: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1251 |
cb, sourcecb = self.attr_map[mapkey] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1252 |
if not sourcecb: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1253 |
return cb(self, linkedvar, rel) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1254 |
# attribute mapped at the source level (bfss for instance) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1255 |
stmt = rel.stmt |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1256 |
for selectidx, vref in iter_mapped_var_sels(stmt, variable): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1257 |
stack = [cb] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1258 |
update_source_cb_stack(self._state, stmt, vref, stack) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5010
diff
changeset
|
1259 |
self._state._needs_source_cb[selectidx] = stack |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1260 |
linkedvar.accept(self) |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1124
diff
changeset
|
1261 |
sql = '%s.%s%s' % (linkedvar._q_sqltable, SQL_PREFIX, rel.r_type) |
0 | 1262 |
return sql |
1263 |
||
1264 |
# tables handling ######################################################### |
|
1265 |
||
1266 |
def alias_and_add_table(self, tablename): |
|
1267 |
alias = '%s%s' % (tablename, self._state.count) |
|
1268 |
self._state.count += 1 |
|
1269 |
self.add_table('%s AS %s' % (tablename, alias), alias) |
|
1270 |
return alias |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1271 |
|
0 | 1272 |
def add_table(self, table, key=None, scope=-1): |
1273 |
if key is None: |
|
1274 |
key = table |
|
1275 |
if key in self._state.tables: |
|
1276 |
return |
|
3238
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
1277 |
if scope < 0: |
988a72e59b2b
[querier] fix sql generated w/ NOT relation and shared variable: ensure variable's table is in parent select'scope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2915
diff
changeset
|
1278 |
scope = len(self._state.actual_tables) + scope |
2915
651bbe1526b6
[rql2sql] test and fix a bug triggered when editing a ticket in jpl
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
1279 |
self._state.tables[key] = (scope, table) |
0 | 1280 |
self._state.actual_tables[scope].append(table) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1281 |
|
0 | 1282 |
def replace_tables_by_outer_join(self, substitute, lefttable, *tables): |
1283 |
for table in tables: |
|
1284 |
try: |
|
1285 |
scope, alias = self._state.tables[table] |
|
1286 |
self._state.actual_tables[scope].remove(alias) |
|
1287 |
except ValueError: # huum, not sure about what should be done here |
|
1288 |
msg = "%s already used in an outer join, don't know what to do!" |
|
1289 |
raise Exception(msg % table) |
|
1290 |
try: |
|
1291 |
tablealias = self._state.outer_tables[lefttable] |
|
1292 |
actualtables = self._state.actual_tables[-1] |
|
1293 |
except KeyError: |
|
1294 |
tablescope, tablealias = self._state.tables[lefttable] |
|
1295 |
actualtables = self._state.actual_tables[tablescope] |
|
1296 |
outerjoin = '%s %s' % (tablealias, substitute) |
|
1297 |
self._update_outer_tables(lefttable, actualtables, tablealias, outerjoin) |
|
1298 |
for table in tables: |
|
1299 |
self._state.outer_tables[table] = outerjoin |
|
1300 |
||
1301 |
def add_outer_join_condition(self, var, table, condition): |
|
1302 |
try: |
|
1303 |
tablealias = self._state.outer_tables[table] |
|
1304 |
actualtables = self._state.actual_tables[-1] |
|
1305 |
except KeyError: |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1306 |
for rel in var.stinfo.get('optrelations'): |
0 | 1307 |
self.visit_relation(rel) |
1308 |
assert self._state.outer_tables |
|
1309 |
self.add_outer_join_condition(var, table, condition) |
|
1310 |
return |
|
1311 |
before, after = tablealias.split(' AS %s ' % table, 1) |
|
1312 |
beforep, afterp = after.split(')', 1) |
|
1313 |
outerjoin = '%s AS %s %s AND %s) %s' % (before, table, beforep, |
|
1314 |
condition, afterp) |
|
1315 |
self._update_outer_tables(table, actualtables, tablealias, outerjoin) |
|
1316 |
||
1317 |
def _update_outer_tables(self, table, actualtables, oldalias, newalias): |
|
1318 |
actualtables.remove(oldalias) |
|
1319 |
actualtables.append(newalias) |
|
4286
6801093af29c
fix bug: the 'table' argument is overwritten by the loop variable, causing latter crash due to this missing information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
1320 |
self._state.outer_tables[table] = newalias |
0 | 1321 |
# some tables which have already been used as outer table and replaced |
1322 |
# by <oldalias> may not be reused here, though their associated value |
|
1323 |
# in the outer_tables dict has to be updated as well |
|
1324 |
for table, outerexpr in self._state.outer_tables.iteritems(): |
|
1325 |
if outerexpr == oldalias: |
|
1326 |
self._state.outer_tables[table] = newalias |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1327 |
|
0 | 1328 |
def _var_table(self, var): |
1329 |
var.accept(self)#.visit_variable(var) |
|
1330 |
return var._q_sqltable |
|
1331 |
||
1332 |
def _relation_table(self, relation): |
|
1333 |
"""return the table alias used by the given relation""" |
|
1334 |
if relation in self._state.done: |
|
1335 |
return relation._q_sqltable |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3245
diff
changeset
|
1336 |
assert not self.schema.rschema(relation.r_type).final, relation.r_type |
0 | 1337 |
rid = 'rel_%s%s' % (relation.r_type, self._state.count) |
1338 |
# relation's table is belonging to the root scope if it is the principal |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1339 |
# table of one of it's variable and if that variable belong's to parent |
0 | 1340 |
# scope |
1341 |
for varref in relation.iget_nodes(VariableRef): |
|
1342 |
var = varref.variable |
|
1343 |
if isinstance(var, ColumnAlias): |
|
1344 |
scope = 0 |
|
1345 |
break |
|
1346 |
# XXX may have a principal without being invariant for this generation, |
|
1347 |
# not sure this is a pb or not |
|
1348 |
if var.stinfo.get('principal') is relation and var.sqlscope is var.stmt: |
|
1349 |
scope = 0 |
|
1350 |
break |
|
1351 |
else: |
|
1352 |
scope = -1 |
|
1353 |
self._state.count += 1 |
|
1354 |
self.add_table('%s_relation AS %s' % (relation.r_type, rid), rid, scope=scope) |
|
1355 |
relation._q_sqltable = rid |
|
1356 |
self._state.done.add(relation) |
|
1357 |
return rid |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1358 |
|
0 | 1359 |
def _fti_table(self, relation): |
1360 |
if relation in self._state.done: |
|
1361 |
try: |
|
1362 |
return relation._q_sqltable |
|
1363 |
except AttributeError: |
|
1364 |
pass |
|
1365 |
self._state.done.add(relation) |
|
5010
b2c5aee8ca3f
[cleanup] rename dbms_helper to dbhelper for consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
1366 |
alias = self.alias_and_add_table(self.dbhelper.fti_table) |
0 | 1367 |
relation._q_sqltable = alias |
1368 |
return alias |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1522
diff
changeset
|
1369 |
|
0 | 1370 |
def _varmap_table_scope(self, select, table): |
1371 |
"""since a varmap table may be used for multiple variable, its scope is |
|
1372 |
the most outer scope of each variables |
|
1373 |
""" |
|
1374 |
scope = -1 |
|
1375 |
for varname, alias in self._varmap.iteritems(): |
|
1376 |
# check '.' in varname since there are 'X.attribute' keys in varmap |
|
1377 |
if not '.' in varname and alias.split('.', 1)[0] == table: |
|
1378 |
if select.defined_vars[varname].sqlscope is select: |
|
1379 |
return 0 |
|
1380 |
return scope |