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