2008-08-04 15:32:50 +00:00
|
|
|
#!/usr/bin/env python
|
2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-11-03 18:27:16 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2008-11-03 18:27:16 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 12:32:15 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2008-11-03 18:27:16 +00:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 12:32:15 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2008-11-03 18:27:16 +00:00
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2008-11-03 18:27:16 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-08-11 09:31:36 +00:00
|
|
|
""" Domain expression processing
|
|
|
|
|
|
|
|
The main duty of this module is to compile a domain expression into a SQL
|
|
|
|
query. A lot of things should be documented here, but as a first step in the
|
|
|
|
right direction, some tests in test_osv_expression.yml might give you some
|
|
|
|
additional information.
|
|
|
|
|
|
|
|
For legacy reasons, a domain uses an inconsistent two-levels abstract syntax
|
|
|
|
(domains are regular Python data structures). At the first level, a domain
|
|
|
|
is an expression made of terms (sometimes called leaves) and (domain) operators
|
|
|
|
used in prefix notation. The available operators at this level are '!', '&',
|
|
|
|
and '|'. '!' is a unary 'not', '&' is a binary 'and', and '|' is a binary 'or'.
|
|
|
|
For instance, here is a possible domain. (<term> stands for an arbitrary term,
|
|
|
|
more on this later.)
|
|
|
|
|
|
|
|
['&', '!', <term1>, '|', <term2>, <term3>]
|
|
|
|
|
|
|
|
It is equivalent to this pseudo code using infix notation:
|
|
|
|
|
|
|
|
(not <term1>) and (<term2> or <term3>)
|
|
|
|
|
|
|
|
The second level of syntax deals with the term representation. A term is
|
|
|
|
a triple of the form (left, operator, right). That is, a term uses an infix
|
|
|
|
notation, and the available operators, and possible left and right operands
|
|
|
|
differ with those of the previous level. Here is a possible term:
|
|
|
|
|
|
|
|
('company_id.name', '=', 'OpenERP')
|
|
|
|
|
|
|
|
The left and right operand don't have the same possible values. The left
|
|
|
|
operand is field name (related to the model for which the domain applies).
|
|
|
|
Actually, the field name can use the dot-notation to traverse relationships.
|
|
|
|
The right operand is a Python value whose type should match the used operator
|
|
|
|
and field type. In the above example, a string is used because the name field
|
|
|
|
of a company has type string, and because we use the '=' operator. When
|
|
|
|
appropriate, a 'in' operator can be used, and thus the right operand should be
|
|
|
|
a list.
|
|
|
|
|
|
|
|
Note: the non-uniform syntax could have been more uniform, but this would hide
|
|
|
|
an important limitation of the domain syntax. Say that the term representation
|
|
|
|
was ['=', 'company_id.name', 'OpenERP']. Used in a complete domain, this would
|
|
|
|
look like:
|
|
|
|
|
|
|
|
['!', ['=', 'company_id.name', 'OpenERP']]
|
|
|
|
|
|
|
|
and you would be tempted to believe something like this would be possible:
|
|
|
|
|
|
|
|
['!', ['=', 'company_id.name', ['&', ..., ...]]]
|
|
|
|
|
|
|
|
That is, a domain could be a valid operand. But this is not the case. A domain
|
|
|
|
is really limited to a two-level nature, and can not takes a recursive form: a
|
|
|
|
domain is not a valid second-level operand.
|
|
|
|
|
2011-08-11 11:01:18 +00:00
|
|
|
Unaccent - Accent-insensitive search
|
|
|
|
|
|
|
|
OpenERP will use the SQL function 'unaccent' when available for the 'ilike' and
|
2011-09-21 22:44:23 +00:00
|
|
|
'not ilike' operators, and enabled in the configuration.
|
|
|
|
Normally the 'unaccent' function is obtained from the PostgreSQL 'unaccent'
|
|
|
|
contrib module[0].
|
|
|
|
|
|
|
|
|
|
|
|
..todo: The following explanation should be moved in some external installation
|
|
|
|
guide
|
|
|
|
|
|
|
|
The steps to install the module might differ on specific PostgreSQL versions.
|
|
|
|
We give here some instruction for PostgreSQL 9.x on a Ubuntu system.
|
2011-08-11 11:01:18 +00:00
|
|
|
|
2011-09-22 14:08:56 +00:00
|
|
|
Ubuntu doesn't come yet with PostgreSQL 9.x, so an alternative package source
|
2011-08-11 11:01:18 +00:00
|
|
|
is used. We use Martin Pitt's PPA available at ppa:pitti/postgresql[1]. See
|
|
|
|
[2] for instructions. Basically:
|
|
|
|
|
|
|
|
> sudo add-apt-repository ppa:pitti/postgresql
|
|
|
|
> sudo apt-get update
|
|
|
|
|
|
|
|
Once the package list is up-to-date, you have to install PostgreSQL 9.0 and
|
|
|
|
its contrib modules.
|
|
|
|
|
|
|
|
> sudo apt-get install postgresql-9.0 postgresql-contrib-9.0
|
|
|
|
|
|
|
|
When you want to enable unaccent on some database:
|
|
|
|
|
|
|
|
> psql9 <database> -f /usr/share/postgresql/9.0/contrib/unaccent.sql
|
|
|
|
|
|
|
|
Here 'psql9' is an alias for the newly installed PostgreSQL 9.0 tool, together
|
|
|
|
with the correct port if necessary (for instance if PostgreSQL 8.4 is running
|
|
|
|
on 5432). (Other aliases can be used for createdb and dropdb.)
|
|
|
|
|
|
|
|
> alias psql9='/usr/lib/postgresql/9.0/bin/psql -p 5433'
|
|
|
|
|
|
|
|
You can check unaccent is working:
|
|
|
|
|
|
|
|
> psql9 <database> -c"select unaccent('hélène')"
|
|
|
|
|
|
|
|
Finally, to instruct OpenERP to really use the unaccent function, you have to
|
|
|
|
start the server specifying the --unaccent flag.
|
|
|
|
|
|
|
|
[0] http://developer.postgresql.org/pgdocs/postgres/unaccent.html
|
|
|
|
[1] https://launchpad.net/~pitti/+archive/postgresql
|
|
|
|
[2] https://launchpad.net/+help/soyuz/ppa-sources-list.html
|
|
|
|
|
2011-08-11 09:31:36 +00:00
|
|
|
"""
|
|
|
|
|
2011-08-09 17:48:14 +00:00
|
|
|
import logging
|
|
|
|
|
2011-02-07 12:57:23 +00:00
|
|
|
from openerp.tools import flatten, reverse_enumerate
|
2008-12-09 08:03:55 +00:00
|
|
|
import fields
|
2011-08-11 09:31:36 +00:00
|
|
|
import openerp.modules
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-06-23 09:03:57 +00:00
|
|
|
#.apidoc title: Domain Expressions
|
|
|
|
|
2011-08-11 11:01:18 +00:00
|
|
|
# Domain operators.
|
2011-03-24 11:17:57 +00:00
|
|
|
NOT_OPERATOR = '!'
|
|
|
|
OR_OPERATOR = '|'
|
|
|
|
AND_OPERATOR = '&'
|
2011-09-21 22:44:23 +00:00
|
|
|
DOMAIN_OPERATORS = (NOT_OPERATOR, OR_OPERATOR, AND_OPERATOR)
|
2008-08-12 14:44:56 +00:00
|
|
|
|
2011-08-11 09:31:36 +00:00
|
|
|
# List of available term operators. It is also possible to use the '<>'
|
|
|
|
# operator, which is strictly the same as '!='; the later should be prefered
|
|
|
|
# for consistency. This list doesn't contain '<>' as it is simpified to '!='
|
|
|
|
# by the normalize_operator() function (so later part of the code deals with
|
|
|
|
# only one representation).
|
|
|
|
# An internal (i.e. not available to the user) 'inselect' operator is also
|
|
|
|
# used. In this case its right operand has the form (subselect, params).
|
2011-09-21 22:44:23 +00:00
|
|
|
TERM_OPERATORS = ('=', '!=', '<=', '<', '>', '>=', '=?', '=like', '=ilike',
|
|
|
|
'like', 'not like', 'ilike', 'not ilike', 'in', 'not in',
|
|
|
|
'child_of')
|
2011-08-11 09:31:36 +00:00
|
|
|
|
|
|
|
# A subset of the above operators, with a 'negative' semantic. When the
|
2011-09-21 22:44:23 +00:00
|
|
|
# expressions 'in NEGATIVE_TERM_OPERATORS' or 'not in NEGATIVE_TERM_OPERATORS' are used in the code
|
|
|
|
# below, this doesn't necessarily mean that any of those NEGATIVE_TERM_OPERATORS is
|
2011-08-11 09:31:36 +00:00
|
|
|
# legal in the processed term.
|
2011-09-21 22:44:23 +00:00
|
|
|
NEGATIVE_TERM_OPERATORS = ('!=', 'not like', 'not ilike', 'not in')
|
2008-08-12 14:44:56 +00:00
|
|
|
|
2011-07-22 15:36:49 +00:00
|
|
|
TRUE_LEAF = (1, '=', 1)
|
|
|
|
FALSE_LEAF = (0, '=', 1)
|
|
|
|
|
|
|
|
TRUE_DOMAIN = [TRUE_LEAF]
|
|
|
|
FALSE_DOMAIN = [FALSE_LEAF]
|
2011-05-31 14:43:18 +00:00
|
|
|
|
2011-08-09 17:48:14 +00:00
|
|
|
_logger = logging.getLogger('expression')
|
2011-05-31 14:43:18 +00:00
|
|
|
|
|
|
|
def normalize(domain):
|
|
|
|
"""Returns a normalized version of ``domain_expr``, where all implicit '&' operators
|
|
|
|
have been made explicit. One property of normalized domain expressions is that they
|
|
|
|
can be easily combined together as if they were single domain components.
|
|
|
|
"""
|
|
|
|
assert isinstance(domain, (list, tuple)), "Domains to normalize must have a 'domain' form: a list or tuple of domain components"
|
|
|
|
if not domain:
|
|
|
|
return TRUE_DOMAIN
|
|
|
|
result = []
|
|
|
|
expected = 1 # expected number of expressions
|
|
|
|
op_arity = {NOT_OPERATOR: 1, AND_OPERATOR: 2, OR_OPERATOR: 2}
|
|
|
|
for token in domain:
|
|
|
|
if expected == 0: # more than expected, like in [A, B]
|
2011-09-21 22:44:23 +00:00
|
|
|
result[0:0] = [AND_OPERATOR] # put an extra '&' in front
|
2011-05-31 14:43:18 +00:00
|
|
|
expected = 1
|
|
|
|
result.append(token)
|
2011-07-26 17:24:58 +00:00
|
|
|
if isinstance(token, (list, tuple)): # domain term
|
2011-05-31 14:43:18 +00:00
|
|
|
expected -= 1
|
|
|
|
else:
|
|
|
|
expected += op_arity.get(token, 0) - 1
|
|
|
|
assert expected == 0
|
|
|
|
return result
|
|
|
|
|
|
|
|
def combine(operator, unit, zero, domains):
|
|
|
|
"""Returns a new domain expression where all domain components from ``domains``
|
2011-07-26 17:28:53 +00:00
|
|
|
have been added together using the binary operator ``operator``. The given
|
|
|
|
domains must be normalized.
|
2011-05-31 14:43:18 +00:00
|
|
|
|
|
|
|
:param unit: the identity element of the domains "set" with regard to the operation
|
|
|
|
performed by ``operator``, i.e the domain component ``i`` which, when
|
|
|
|
combined with any domain ``x`` via ``operator``, yields ``x``.
|
|
|
|
E.g. [(1,'=',1)] is the typical unit for AND_OPERATOR: adding it
|
|
|
|
to any domain component gives the same domain.
|
|
|
|
:param zero: the absorbing element of the domains "set" with regard to the operation
|
|
|
|
performed by ``operator``, i.e the domain component ``z`` which, when
|
|
|
|
combined with any domain ``x`` via ``operator``, yields ``z``.
|
|
|
|
E.g. [(1,'=',1)] is the typical zero for OR_OPERATOR: as soon as
|
|
|
|
you see it in a domain component the resulting domain is the zero.
|
2011-07-26 17:28:53 +00:00
|
|
|
:param domains: a list of normalized domains.
|
2011-05-31 14:43:18 +00:00
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
count = 0
|
|
|
|
for domain in domains:
|
|
|
|
if domain == unit:
|
|
|
|
continue
|
|
|
|
if domain == zero:
|
|
|
|
return zero
|
|
|
|
if domain:
|
|
|
|
result += domain
|
|
|
|
count += 1
|
|
|
|
result = [operator] * (count - 1) + result
|
|
|
|
return result
|
|
|
|
|
|
|
|
def AND(domains):
|
2011-09-21 22:44:23 +00:00
|
|
|
"""AND([D1,D2,...]) returns a domain representing D1 and D2 and ... """
|
2011-05-31 14:43:18 +00:00
|
|
|
return combine(AND_OPERATOR, TRUE_DOMAIN, FALSE_DOMAIN, domains)
|
|
|
|
|
|
|
|
def OR(domains):
|
2011-09-21 22:44:23 +00:00
|
|
|
"""OR([D1,D2,...]) returns a domain representing D1 or D2 or ... """
|
2011-05-31 14:43:18 +00:00
|
|
|
return combine(OR_OPERATOR, FALSE_DOMAIN, TRUE_DOMAIN, domains)
|
|
|
|
|
2011-07-22 15:36:49 +00:00
|
|
|
def is_operator(element):
|
2011-09-21 22:44:23 +00:00
|
|
|
"""Test whether an object is a valid domain operator. """
|
|
|
|
return isinstance(element, basestring) and element in DOMAIN_OPERATORS
|
2011-07-22 15:36:49 +00:00
|
|
|
|
|
|
|
# TODO change the share wizard to use this function.
|
|
|
|
def is_leaf(element, internal=False):
|
2011-08-11 09:31:36 +00:00
|
|
|
""" Test whether an object is a valid domain term.
|
|
|
|
|
|
|
|
:param internal: allow or not the 'inselect' internal operator in the term.
|
|
|
|
This normally should be always left to False.
|
|
|
|
"""
|
2011-09-21 22:44:23 +00:00
|
|
|
INTERNAL_OPS = TERM_OPERATORS + ('inselect',)
|
2011-07-22 15:36:49 +00:00
|
|
|
return (isinstance(element, tuple) or isinstance(element, list)) \
|
|
|
|
and len(element) == 3 \
|
2011-09-21 22:44:23 +00:00
|
|
|
and (((not internal) and element[1] in TERM_OPERATORS + ('<>',)) \
|
2011-08-10 09:28:59 +00:00
|
|
|
or (internal and element[1] in INTERNAL_OPS + ('<>',)))
|
2011-07-22 15:36:49 +00:00
|
|
|
|
2011-08-10 12:00:57 +00:00
|
|
|
def normalize_leaf(left, operator, right):
|
2011-08-11 09:31:36 +00:00
|
|
|
""" Change a term's operator to some canonical form, simplifying later
|
|
|
|
processing.
|
|
|
|
"""
|
2011-08-10 12:00:57 +00:00
|
|
|
original = operator
|
2011-08-09 17:48:14 +00:00
|
|
|
operator = operator.lower()
|
|
|
|
if operator == '<>':
|
|
|
|
operator = '!='
|
2011-08-10 12:00:57 +00:00
|
|
|
if isinstance(right, bool) and operator in ('in', 'not in'):
|
|
|
|
_logger.warning("The domain term '%s' should use the '=' or '!=' operator." % ((left, original, right),))
|
|
|
|
operator = '=' if operator == 'in' else '!='
|
|
|
|
if isinstance(right, (list, tuple)) and operator in ('=', '!='):
|
|
|
|
_logger.warning("The domain term '%s' should use the 'in' or 'not in' operator." % ((left, original, right),))
|
|
|
|
operator = 'in' if operator == '=' else 'not in'
|
|
|
|
return left, operator, right
|
2011-08-09 17:48:14 +00:00
|
|
|
|
2011-08-10 14:38:38 +00:00
|
|
|
def distribute_not(domain):
|
2011-09-21 22:44:23 +00:00
|
|
|
""" Distribute any '!' domain operators found inside a normalized domain.
|
2011-08-11 09:31:36 +00:00
|
|
|
|
|
|
|
Because we don't use SQL semantic for processing a 'left not in right'
|
|
|
|
query (i.e. our 'not in' is not simply translated to a SQL 'not in'),
|
|
|
|
it means that a '! left in right' can not be simply processed
|
|
|
|
by __leaf_to_sql by first emitting code for 'left in right' then wrapping
|
|
|
|
the result with 'not (...)', as it would result in a 'not in' at the SQL
|
|
|
|
level.
|
|
|
|
|
2011-09-21 22:44:23 +00:00
|
|
|
This function is thus responsible for pushing any '!' domain operators
|
|
|
|
inside the terms themselves. For example::
|
|
|
|
|
|
|
|
['!','&',('user_id','=',4),('partner_id','in',[1,2])]
|
|
|
|
will be turned into:
|
|
|
|
['|',('user_id','!=',4),('partner_id','not in',[1,2])]
|
2011-08-11 09:31:36 +00:00
|
|
|
|
2011-08-10 14:38:38 +00:00
|
|
|
"""
|
|
|
|
def negate(leaf):
|
2011-09-21 22:44:23 +00:00
|
|
|
"""Negates and returns a single domain leaf term,
|
|
|
|
using the opposite operator if possible"""
|
2011-08-10 14:38:38 +00:00
|
|
|
left, operator, right = leaf
|
|
|
|
mapping = {
|
|
|
|
'<': '>=',
|
|
|
|
'>': '<=',
|
|
|
|
'<=': '>',
|
|
|
|
'>=': '<',
|
|
|
|
'=': '!=',
|
|
|
|
'!=': '=',
|
|
|
|
}
|
|
|
|
if operator in ('in', 'like', 'ilike'):
|
|
|
|
operator = 'not ' + operator
|
|
|
|
return [(left, operator, right)]
|
|
|
|
if operator in ('not in', 'not like', 'not ilike'):
|
|
|
|
operator = operator[4:]
|
|
|
|
return [(left, operator, right)]
|
|
|
|
if operator in mapping:
|
|
|
|
operator = mapping[operator]
|
|
|
|
return [(left, operator, right)]
|
2011-09-21 22:44:23 +00:00
|
|
|
return [NOT_OPERATOR, (left, operator, right)]
|
|
|
|
def distribute_negate(domain):
|
|
|
|
"""Negate the domain ``subtree`` rooted at domain[0],
|
|
|
|
leaving the rest of the domain intact, and return
|
|
|
|
(negated_subtree, untouched_domain_rest)
|
|
|
|
"""
|
2011-08-10 14:38:38 +00:00
|
|
|
if is_leaf(domain[0]):
|
|
|
|
return negate(domain[0]), domain[1:]
|
2011-09-21 22:44:23 +00:00
|
|
|
if domain[0] == AND_OPERATOR:
|
|
|
|
done1, todo1 = distribute_negate(domain[1:])
|
|
|
|
done2, todo2 = distribute_negate(todo1)
|
|
|
|
return [OR_OPERATOR] + done1 + done2, todo2
|
|
|
|
if domain[0] == OR_OPERATOR:
|
|
|
|
done1, todo1 = distribute_negate(domain[1:])
|
|
|
|
done2, todo2 = distribute_negate(todo1)
|
|
|
|
return [AND_OPERATOR] + done1 + done2, todo2
|
2011-08-10 14:38:38 +00:00
|
|
|
if not domain:
|
|
|
|
return []
|
2011-09-21 22:44:23 +00:00
|
|
|
if domain[0] != NOT_OPERATOR:
|
2011-08-10 14:38:38 +00:00
|
|
|
return [domain[0]] + distribute_not(domain[1:])
|
2011-09-21 22:44:23 +00:00
|
|
|
if domain[0] == NOT_OPERATOR:
|
|
|
|
done, todo = distribute_negate(domain[1:])
|
2011-08-10 14:38:38 +00:00
|
|
|
return done + distribute_not(todo)
|
|
|
|
|
2011-09-21 22:44:23 +00:00
|
|
|
def select_from_where(cr, select_field, from_table, where_field, where_ids, where_operator):
|
2011-07-22 15:36:49 +00:00
|
|
|
# todo: merge into parent query as sub-query
|
|
|
|
res = []
|
2011-09-21 22:44:23 +00:00
|
|
|
if where_ids:
|
|
|
|
if where_operator in ['<','>','>=','<=']:
|
2011-07-26 14:22:44 +00:00
|
|
|
cr.execute('SELECT "%s" FROM "%s" WHERE "%s" %s %%s' % \
|
2011-09-21 22:44:23 +00:00
|
|
|
(select_field, from_table, where_field, where_operator),
|
|
|
|
(where_ids[0],)) # TODO shouldn't this be min/max(where_ids) ?
|
2011-07-26 14:22:44 +00:00
|
|
|
res = [r[0] for r in cr.fetchall()]
|
2011-09-21 22:44:23 +00:00
|
|
|
else: # TODO where_operator is supposed to be 'in'? It is called with child_of...
|
|
|
|
for i in range(0, len(where_ids), cr.IN_MAX):
|
|
|
|
subids = where_ids[i:i+cr.IN_MAX]
|
2011-07-26 14:22:44 +00:00
|
|
|
cr.execute('SELECT "%s" FROM "%s" WHERE "%s" IN %%s' % \
|
2011-09-21 22:44:23 +00:00
|
|
|
(select_field, from_table, where_field), (tuple(subids),))
|
2011-07-22 15:36:49 +00:00
|
|
|
res.extend([r[0] for r in cr.fetchall()])
|
|
|
|
return res
|
2011-05-31 14:43:18 +00:00
|
|
|
|
2011-09-21 22:44:23 +00:00
|
|
|
def select_distinct_from_where_not_null(cr, select_field, from_table):
|
|
|
|
cr.execute('SELECT distinct("%s") FROM "%s" where "%s" is not null' % \
|
|
|
|
(select_field, from_table, select_field))
|
2011-07-26 14:22:44 +00:00
|
|
|
return [r[0] for r in cr.fetchall()]
|
2011-05-31 14:43:18 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
class expression(object):
|
|
|
|
"""
|
|
|
|
parse a domain expression
|
2008-08-14 12:04:41 +00:00
|
|
|
use a real polish notation
|
|
|
|
leafs are still in a ('foo', '=', 'bar') format
|
2010-06-15 13:27:22 +00:00
|
|
|
For more info: http://christophe-simonis-at-tiny.blogspot.com/2008/08/new-new-domain-notation.html
|
2008-08-04 15:32:50 +00:00
|
|
|
"""
|
|
|
|
|
2011-07-22 15:36:49 +00:00
|
|
|
def __init__(self, cr, uid, exp, table, context):
|
2011-08-11 09:31:36 +00:00
|
|
|
self.has_unaccent = openerp.modules.registry.RegistryManager.get(cr.dbname).has_unaccent
|
2010-05-31 15:05:17 +00:00
|
|
|
self.__field_tables = {} # used to store the table to use for the sql generation. key = index of the leaf
|
2010-08-26 11:45:31 +00:00
|
|
|
self.__all_tables = set()
|
2008-08-04 15:32:50 +00:00
|
|
|
self.__joins = []
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__main_table = None # 'root' table. set by parse()
|
2011-07-22 15:36:49 +00:00
|
|
|
# assign self.__exp with the normalized, parsed domain.
|
2011-08-10 14:38:38 +00:00
|
|
|
self.parse(cr, uid, distribute_not(normalize(exp)), table, context)
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-07-25 15:07:21 +00:00
|
|
|
# TODO used only for osv_memory
|
2010-06-24 15:56:02 +00:00
|
|
|
@property
|
|
|
|
def exp(self):
|
|
|
|
return self.__exp[:]
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-07-22 15:36:49 +00:00
|
|
|
def parse(self, cr, uid, exp, table, context):
|
2011-09-21 22:44:23 +00:00
|
|
|
""" transform the leaves of the expression """
|
2011-07-22 15:36:49 +00:00
|
|
|
self.__exp = exp
|
2011-08-11 09:31:36 +00:00
|
|
|
self.__main_table = table
|
|
|
|
self.__all_tables.add(table)
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-09-21 22:44:23 +00:00
|
|
|
def child_of_domain(left, ids, left_model, parent=None, prefix=''):
|
|
|
|
"""Returns a domain implementing the child_of operator for [(left,child_of,ids)],
|
|
|
|
either as a range using the parent_left/right tree lookup fields (when available),
|
|
|
|
or as an expanded [(left,in,child_ids)]"""
|
|
|
|
if left_model._parent_store and (not left_model.pool._init):
|
|
|
|
# TODO: Improve where joins are implemented for many with '.', replace by:
|
|
|
|
# doms += ['&',(prefix+'.parent_left','<',o.parent_right),(prefix+'.parent_left','>=',o.parent_left)]
|
2008-08-14 17:05:08 +00:00
|
|
|
doms = []
|
2011-09-21 22:44:23 +00:00
|
|
|
for o in left_model.browse(cr, uid, ids, context=context):
|
2008-08-14 17:05:08 +00:00
|
|
|
if doms:
|
2011-03-24 11:17:57 +00:00
|
|
|
doms.insert(0, OR_OPERATOR)
|
|
|
|
doms += [AND_OPERATOR, ('parent_left', '<', o.parent_right), ('parent_left', '>=', o.parent_left)]
|
2008-08-17 18:28:29 +00:00
|
|
|
if prefix:
|
2011-09-21 22:44:23 +00:00
|
|
|
return [(left, 'in', left_model.search(cr, uid, doms, context=context))]
|
2008-08-17 18:28:29 +00:00
|
|
|
return doms
|
2008-08-14 17:05:08 +00:00
|
|
|
else:
|
2011-09-21 22:44:23 +00:00
|
|
|
def recursive_children(ids, model, parent_field):
|
2008-08-19 23:15:37 +00:00
|
|
|
if not ids:
|
|
|
|
return []
|
2011-09-21 22:44:23 +00:00
|
|
|
ids2 = model.search(cr, uid, [(parent_field, 'in', ids)], context=context)
|
|
|
|
return ids + recursive_children(ids2, model, parent_field)
|
|
|
|
return [(left, 'in', recursive_children(ids, left_model, parent or left_model._parent_name))]
|
2011-06-24 08:43:11 +00:00
|
|
|
|
2011-09-21 22:44:23 +00:00
|
|
|
def to_ids(value, field_obj):
|
2011-09-22 00:54:31 +00:00
|
|
|
"""Normalize a single id or name, or a list of those, into a list of ids"""
|
|
|
|
names = []
|
2011-05-26 09:28:53 +00:00
|
|
|
if isinstance(value, basestring):
|
2011-09-22 00:54:31 +00:00
|
|
|
names = [value]
|
|
|
|
if value and isinstance(value, (tuple, list)) and isinstance(value[0], basestring):
|
|
|
|
names = value
|
|
|
|
if names:
|
|
|
|
return flatten([[x[0] for x in field_obj.name_search(cr, uid, n, [], 'ilike', context=context, limit=None)] \
|
|
|
|
for n in names])
|
2011-05-26 09:28:53 +00:00
|
|
|
elif isinstance(value, (int, long)):
|
2011-06-08 05:26:03 +00:00
|
|
|
return [value]
|
2011-09-22 00:54:31 +00:00
|
|
|
return list(value)
|
2008-08-16 16:30:06 +00:00
|
|
|
|
2008-08-17 18:28:29 +00:00
|
|
|
i = -1
|
2008-10-07 14:51:30 +00:00
|
|
|
while i + 1<len(self.__exp):
|
|
|
|
i += 1
|
2008-08-17 18:28:29 +00:00
|
|
|
e = self.__exp[i]
|
2011-07-22 15:36:49 +00:00
|
|
|
if is_operator(e) or e == TRUE_LEAF or e == FALSE_LEAF:
|
2008-08-14 12:04:41 +00:00
|
|
|
continue
|
2011-08-10 08:56:04 +00:00
|
|
|
|
|
|
|
# check if the expression is valid
|
|
|
|
if not is_leaf(e):
|
|
|
|
raise ValueError('Bad domain expression: %r, %r is not a valid term.' % (exp, e))
|
|
|
|
|
2011-08-10 12:00:57 +00:00
|
|
|
# normalize the leaf's operator
|
|
|
|
e = normalize_leaf(*e)
|
|
|
|
self.__exp[i] = e
|
2008-08-14 12:04:41 +00:00
|
|
|
left, operator, right = e
|
2011-08-10 08:56:04 +00:00
|
|
|
|
2011-07-25 15:07:21 +00:00
|
|
|
working_table = table # The table containing the field (the name provided in the left operand)
|
2009-08-12 13:06:26 +00:00
|
|
|
fargs = left.split('.', 1)
|
2011-07-25 15:07:21 +00:00
|
|
|
|
|
|
|
# If the field is _inherits'd, search for the working_table,
|
|
|
|
# and extract the field.
|
2009-12-22 19:01:58 +00:00
|
|
|
if fargs[0] in table._inherit_fields:
|
2009-08-18 13:44:45 +00:00
|
|
|
while True:
|
2011-07-25 15:07:21 +00:00
|
|
|
field = working_table._columns.get(fargs[0])
|
2009-08-18 13:44:45 +00:00
|
|
|
if field:
|
2010-05-31 15:05:17 +00:00
|
|
|
self.__field_tables[i] = working_table
|
2009-08-18 13:44:45 +00:00
|
|
|
break
|
2011-07-25 15:07:21 +00:00
|
|
|
next_table = working_table.pool.get(working_table._inherit_fields[fargs[0]][0])
|
|
|
|
if next_table not in self.__all_tables:
|
2011-09-21 16:21:28 +00:00
|
|
|
self.__joins.append('%s."%s"=%s."%s"' % (next_table._table, 'id', working_table._table, working_table._inherits[next_table._name]))
|
2011-07-25 15:07:21 +00:00
|
|
|
self.__all_tables.add(next_table)
|
|
|
|
working_table = next_table
|
|
|
|
# Or (try to) directly extract the field.
|
|
|
|
else:
|
|
|
|
field = working_table._columns.get(fargs[0])
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
if not field:
|
2008-08-14 12:04:41 +00:00
|
|
|
if left == 'id' and operator == 'child_of':
|
2011-09-21 22:44:23 +00:00
|
|
|
ids2 = to_ids(right, table)
|
2011-07-25 15:07:21 +00:00
|
|
|
dom = child_of_domain(left, ids2, working_table)
|
2008-08-17 18:28:29 +00:00
|
|
|
self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
|
2008-08-14 12:04:41 +00:00
|
|
|
continue
|
2008-08-16 16:30:06 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
field_obj = table.pool.get(field._obj)
|
2008-08-04 15:32:50 +00:00
|
|
|
if len(fargs) > 1:
|
|
|
|
if field._type == 'many2one':
|
2008-08-14 12:04:41 +00:00
|
|
|
right = field_obj.search(cr, uid, [(fargs[1], operator, right)], context=context)
|
2011-08-10 12:00:57 +00:00
|
|
|
self.__exp[i] = (fargs[0], 'in', right)
|
2009-11-23 12:27:36 +00:00
|
|
|
# Making search easier when there is a left operand as field.o2m or field.m2m
|
2011-07-26 17:24:58 +00:00
|
|
|
if field._type in ['many2many', 'one2many']:
|
2009-11-23 12:27:36 +00:00
|
|
|
right = field_obj.search(cr, uid, [(fargs[1], operator, right)], context=context)
|
2011-07-26 17:24:58 +00:00
|
|
|
right1 = table.search(cr, uid, [(fargs[0], 'in', right)], context=context)
|
2011-08-10 12:00:57 +00:00
|
|
|
self.__exp[i] = ('id', 'in', right1)
|
2010-12-29 17:42:23 +00:00
|
|
|
|
2011-07-26 17:24:58 +00:00
|
|
|
if not isinstance(field, fields.property):
|
2010-10-29 20:39:37 +00:00
|
|
|
continue
|
2008-08-07 13:08:07 +00:00
|
|
|
|
2010-12-23 15:02:53 +00:00
|
|
|
if field._properties and not field.store:
|
|
|
|
# this is a function field that is not stored
|
2009-12-22 20:56:00 +00:00
|
|
|
if not field._fnct_search:
|
|
|
|
# the function field doesn't provide a search function and doesn't store
|
|
|
|
# values in the database, so we must ignore it : we generate a dummy leaf
|
2011-07-22 15:36:49 +00:00
|
|
|
self.__exp[i] = TRUE_LEAF
|
2009-12-22 20:56:00 +00:00
|
|
|
else:
|
2010-01-12 10:48:30 +00:00
|
|
|
subexp = field.search(cr, uid, table, left, [self.__exp[i]], context=context)
|
expression: fix function.search() when it does not restrict
Triggered by [Bug 702340]
When we try to search a composite expression like:
[( 'ourfoo.subfoo', '=' , True) ]
.. where subfoo is a function field of the model ofourfoo field,
the framework will call subfoo.search([('subfoo','=',True)]).
The latter shall return a domain expression that will restrict
the search on 'ourfoo'.
When the returned expression is empty, meaning "match all", the algebra
on the parent expression would come up with an invalid [..., '&', ( 1, '=', 1) ]
sequence.
bzr revid: p_christ@hol.gr-20110117084108-vt1qe72lhw8ldxqm
2011-01-17 08:41:08 +00:00
|
|
|
if not subexp:
|
2011-07-22 15:36:49 +00:00
|
|
|
self.__exp[i] = TRUE_LEAF
|
expression: fix function.search() when it does not restrict
Triggered by [Bug 702340]
When we try to search a composite expression like:
[( 'ourfoo.subfoo', '=' , True) ]
.. where subfoo is a function field of the model ofourfoo field,
the framework will call subfoo.search([('subfoo','=',True)]).
The latter shall return a domain expression that will restrict
the search on 'ourfoo'.
When the returned expression is empty, meaning "match all", the algebra
on the parent expression would come up with an invalid [..., '&', ( 1, '=', 1) ]
sequence.
bzr revid: p_christ@hol.gr-20110117084108-vt1qe72lhw8ldxqm
2011-01-17 08:41:08 +00:00
|
|
|
else:
|
|
|
|
# we assume that the expression is valid
|
|
|
|
# we create a dummy leaf for forcing the parsing of the resulting expression
|
2011-03-24 11:17:57 +00:00
|
|
|
self.__exp[i] = AND_OPERATOR
|
2011-07-22 15:36:49 +00:00
|
|
|
self.__exp.insert(i + 1, TRUE_LEAF)
|
expression: fix function.search() when it does not restrict
Triggered by [Bug 702340]
When we try to search a composite expression like:
[( 'ourfoo.subfoo', '=' , True) ]
.. where subfoo is a function field of the model ofourfoo field,
the framework will call subfoo.search([('subfoo','=',True)]).
The latter shall return a domain expression that will restrict
the search on 'ourfoo'.
When the returned expression is empty, meaning "match all", the algebra
on the parent expression would come up with an invalid [..., '&', ( 1, '=', 1) ]
sequence.
bzr revid: p_christ@hol.gr-20110117084108-vt1qe72lhw8ldxqm
2011-01-17 08:41:08 +00:00
|
|
|
for j, se in enumerate(subexp):
|
|
|
|
self.__exp.insert(i + 2 + j, se)
|
2010-01-12 10:48:30 +00:00
|
|
|
# else, the value of the field is store in the database, so we search on it
|
2008-08-14 12:04:41 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
elif field._type == 'one2many':
|
2009-11-23 12:27:36 +00:00
|
|
|
# Applying recursivity on field(one2many)
|
|
|
|
if operator == 'child_of':
|
2011-09-21 22:44:23 +00:00
|
|
|
ids2 = to_ids(right, field_obj)
|
2009-11-23 12:27:36 +00:00
|
|
|
if field._obj != working_table._name:
|
2011-07-25 15:07:21 +00:00
|
|
|
dom = child_of_domain(left, ids2, field_obj, prefix=field._obj)
|
2009-10-08 07:53:35 +00:00
|
|
|
else:
|
2011-07-25 15:07:21 +00:00
|
|
|
dom = child_of_domain('id', ids2, working_table, parent=left)
|
2009-11-23 12:27:36 +00:00
|
|
|
self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
|
2010-06-15 13:27:22 +00:00
|
|
|
|
|
|
|
else:
|
2009-11-23 12:27:36 +00:00
|
|
|
call_null = True
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2010-11-29 18:34:13 +00:00
|
|
|
if right is not False:
|
2009-11-23 12:27:36 +00:00
|
|
|
if isinstance(right, basestring):
|
2010-01-05 12:03:39 +00:00
|
|
|
ids2 = [x[0] for x in field_obj.name_search(cr, uid, right, [], operator, context=context, limit=None)]
|
2010-01-19 14:04:05 +00:00
|
|
|
if ids2:
|
2010-06-15 13:27:22 +00:00
|
|
|
operator = 'in'
|
2009-11-23 12:27:36 +00:00
|
|
|
else:
|
2011-07-26 17:24:58 +00:00
|
|
|
if not isinstance(right, list):
|
2009-11-23 12:27:36 +00:00
|
|
|
ids2 = [right]
|
|
|
|
else:
|
2010-06-15 13:27:22 +00:00
|
|
|
ids2 = right
|
2009-11-23 12:27:36 +00:00
|
|
|
if not ids2:
|
2010-01-19 14:04:05 +00:00
|
|
|
if operator in ['like','ilike','in','=']:
|
|
|
|
#no result found with given search criteria
|
|
|
|
call_null = False
|
2011-07-22 15:36:49 +00:00
|
|
|
self.__exp[i] = FALSE_LEAF
|
2009-11-23 12:27:36 +00:00
|
|
|
else:
|
2011-09-22 00:54:31 +00:00
|
|
|
ids2 = select_from_where(cr, field._fields_id, field_obj._table, 'id', ids2, operator)
|
|
|
|
if ids2:
|
|
|
|
call_null = False
|
|
|
|
self.__exp[i] = ('id', 'in', ids2)
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-11-23 12:27:36 +00:00
|
|
|
if call_null:
|
2011-09-21 22:44:23 +00:00
|
|
|
o2m_op = 'in' if operator in NEGATIVE_TERM_OPERATORS else 'not in'
|
2011-08-10 12:00:57 +00:00
|
|
|
self.__exp[i] = ('id', o2m_op, select_distinct_from_where_not_null(cr, field._fields_id, field_obj._table))
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
elif field._type == 'many2many':
|
|
|
|
#FIXME
|
2008-08-14 12:04:41 +00:00
|
|
|
if operator == 'child_of':
|
2008-08-04 15:32:50 +00:00
|
|
|
def _rec_convert(ids):
|
|
|
|
if field_obj == table:
|
|
|
|
return ids
|
2011-07-26 14:22:44 +00:00
|
|
|
return select_from_where(cr, field._id1, field._rel, field._id2, ids, operator)
|
2008-10-07 14:51:30 +00:00
|
|
|
|
2011-09-21 22:44:23 +00:00
|
|
|
ids2 = to_ids(right, field_obj)
|
2011-07-25 15:07:21 +00:00
|
|
|
dom = child_of_domain('id', ids2, field_obj)
|
2008-08-25 19:36:47 +00:00
|
|
|
ids2 = field_obj.search(cr, uid, dom, context=context)
|
2008-08-18 12:58:00 +00:00
|
|
|
self.__exp[i] = ('id', 'in', _rec_convert(ids2))
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2009-10-08 07:53:35 +00:00
|
|
|
call_null_m2m = True
|
2010-11-29 18:34:13 +00:00
|
|
|
if right is not False:
|
2009-10-08 07:53:35 +00:00
|
|
|
if isinstance(right, basestring):
|
2010-01-05 12:03:39 +00:00
|
|
|
res_ids = [x[0] for x in field_obj.name_search(cr, uid, right, [], operator, context=context)]
|
2010-01-19 14:04:05 +00:00
|
|
|
if res_ids:
|
2010-08-05 18:12:20 +00:00
|
|
|
operator = 'in'
|
2009-10-08 07:53:35 +00:00
|
|
|
else:
|
|
|
|
if not isinstance(right, list):
|
|
|
|
res_ids = [right]
|
|
|
|
else:
|
|
|
|
res_ids = right
|
|
|
|
if not res_ids:
|
2010-01-19 14:04:05 +00:00
|
|
|
if operator in ['like','ilike','in','=']:
|
|
|
|
#no result found with given search criteria
|
|
|
|
call_null_m2m = False
|
2011-07-22 15:36:49 +00:00
|
|
|
self.__exp[i] = FALSE_LEAF
|
2010-06-15 13:27:22 +00:00
|
|
|
else:
|
2010-01-19 14:04:05 +00:00
|
|
|
operator = 'in' # operator changed because ids are directly related to main object
|
2009-10-08 07:53:35 +00:00
|
|
|
else:
|
|
|
|
call_null_m2m = False
|
2011-09-21 22:44:23 +00:00
|
|
|
m2m_op = 'not in' if operator in NEGATIVE_TERM_OPERATORS else 'in'
|
2011-07-26 14:22:44 +00:00
|
|
|
self.__exp[i] = ('id', m2m_op, select_from_where(cr, field._id1, field._rel, field._id2, res_ids, operator) or [0])
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-10-08 07:53:35 +00:00
|
|
|
if call_null_m2m:
|
2011-09-21 22:44:23 +00:00
|
|
|
m2m_op = 'in' if operator in NEGATIVE_TERM_OPERATORS else 'not in'
|
2011-08-10 12:00:57 +00:00
|
|
|
self.__exp[i] = ('id', m2m_op, select_distinct_from_where_not_null(cr, field._id1, field._rel))
|
2010-01-19 14:04:05 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
elif field._type == 'many2one':
|
2008-08-14 12:04:41 +00:00
|
|
|
if operator == 'child_of':
|
2011-09-21 22:44:23 +00:00
|
|
|
ids2 = to_ids(right, field_obj)
|
2008-08-14 12:04:41 +00:00
|
|
|
if field._obj != working_table._name:
|
2011-07-25 15:07:21 +00:00
|
|
|
dom = child_of_domain(left, ids2, field_obj, prefix=field._obj)
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2011-07-25 15:07:21 +00:00
|
|
|
dom = child_of_domain('id', ids2, working_table, parent=left)
|
2008-08-17 18:28:29 +00:00
|
|
|
self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2011-07-26 17:24:58 +00:00
|
|
|
def _get_expression(field_obj, cr, uid, left, right, operator, context=None):
|
2010-08-20 10:20:12 +00:00
|
|
|
if context is None:
|
2011-05-18 12:41:50 +00:00
|
|
|
context = {}
|
2009-01-15 17:41:03 +00:00
|
|
|
c = context.copy()
|
|
|
|
c['active_test'] = False
|
2010-10-06 18:04:32 +00:00
|
|
|
#Special treatment to ill-formed domains
|
|
|
|
operator = ( operator in ['<','>','<=','>='] ) and 'in' or operator
|
2011-05-18 12:41:50 +00:00
|
|
|
|
2011-08-09 17:48:14 +00:00
|
|
|
dict_op = {'not in':'!=','in':'=','=':'in','!=':'not in'}
|
2011-07-26 17:24:58 +00:00
|
|
|
if isinstance(right, tuple):
|
2010-08-20 10:20:12 +00:00
|
|
|
right = list(right)
|
2011-07-26 17:24:58 +00:00
|
|
|
if (not isinstance(right, list)) and operator in ['not in','in']:
|
2010-08-20 10:20:12 +00:00
|
|
|
operator = dict_op[operator]
|
2011-08-09 17:48:14 +00:00
|
|
|
elif isinstance(right, list) and operator in ['!=','=']: #for domain (FIELD,'=',['value1','value2'])
|
2010-10-06 18:04:32 +00:00
|
|
|
operator = dict_op[operator]
|
2011-07-26 12:56:05 +00:00
|
|
|
res_ids = [x[0] for x in field_obj.name_search(cr, uid, right, [], operator, limit=None, context=c)]
|
2011-09-21 22:44:23 +00:00
|
|
|
if operator in NEGATIVE_TERM_OPERATORS:
|
2011-08-10 13:06:46 +00:00
|
|
|
res_ids.append(False) # TODO this should not be appended if False was in 'right'
|
2011-08-10 10:29:22 +00:00
|
|
|
return (left, 'in', res_ids)
|
2010-08-20 10:20:12 +00:00
|
|
|
|
|
|
|
m2o_str = False
|
2010-10-06 18:04:32 +00:00
|
|
|
if right:
|
|
|
|
if isinstance(right, basestring): # and not isinstance(field, fields.related):
|
|
|
|
m2o_str = True
|
2011-07-26 17:24:58 +00:00
|
|
|
elif isinstance(right, (list, tuple)):
|
2010-10-06 18:04:32 +00:00
|
|
|
m2o_str = True
|
|
|
|
for ele in right:
|
2011-05-18 12:41:50 +00:00
|
|
|
if not isinstance(ele, basestring):
|
2010-10-06 18:04:32 +00:00
|
|
|
m2o_str = False
|
|
|
|
break
|
2011-08-09 15:42:21 +00:00
|
|
|
if m2o_str:
|
|
|
|
self.__exp[i] = _get_expression(field_obj, cr, uid, left, right, operator, context=context)
|
expression: fix m2o handling of empty lists [Bug 598454]
The domain expressions wouldn't yield the expected results in the
following cases (eg. for res.partner):
[('company_id', 'in', [])] : should return [], not all partners
[('company_id.partner_id', 'in', [])] : again, empty set
After some deliberation and discussion, we *change the behavior* for them.
Notes:
1. This comes after commit 913e95856f80, bug 626806 which solved the
corresponding cases for m2m, o2m
2. This commit _will_ have side-effects. The result set for domains now
is indeed different. But, we believe, now it is the proper one, fixes
cases of unexpected results that have been reported
3. It makes the server a bit faster, because empty results are quick to
compute
4. The "not in []" operator will NOT return results with NULL values in
the corresponding field (aka. empty). This may not be the one expected
by users, but it follows the SQL standard (par. 9.20.3):
http://www.postgresql.org/docs/9.0/static/functions-subquery.html
bzr revid: p_christ@hol.gr-20101223112938-ore0dmmasekdcyfn
2010-12-23 11:29:38 +00:00
|
|
|
elif right == []:
|
2011-08-10 08:56:04 +00:00
|
|
|
pass # Handled by __leaf_to_sql().
|
2011-08-09 17:48:14 +00:00
|
|
|
else: # right is False
|
2011-08-10 10:29:22 +00:00
|
|
|
pass # Handled by __leaf_to_sql().
|
2011-05-18 12:41:50 +00:00
|
|
|
|
2008-08-07 13:08:07 +00:00
|
|
|
else:
|
2008-08-04 15:32:50 +00:00
|
|
|
# other field type
|
2009-07-24 09:36:50 +00:00
|
|
|
# add the time part to datetime field when it's not there:
|
2009-07-27 07:27:51 +00:00
|
|
|
if field._type == 'datetime' and self.__exp[i][2] and len(self.__exp[i][2]) == 10:
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-07-30 08:51:45 +00:00
|
|
|
self.__exp[i] = list(self.__exp[i])
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-07-24 09:36:50 +00:00
|
|
|
if operator in ('>', '>='):
|
|
|
|
self.__exp[i][2] += ' 00:00:00'
|
|
|
|
elif operator in ('<', '<='):
|
|
|
|
self.__exp[i][2] += ' 23:59:59'
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-07-30 08:51:45 +00:00
|
|
|
self.__exp[i] = tuple(self.__exp[i])
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
if field.translate:
|
2011-09-21 22:42:08 +00:00
|
|
|
need_wildcard = operator in ('like', 'ilike', 'not like', 'not ilike')
|
|
|
|
sql_operator = {'=like':'like','=ilike':'ilike'}.get(operator,operator)
|
|
|
|
if need_wildcard:
|
2008-08-14 12:04:41 +00:00
|
|
|
right = '%%%s%%' % right
|
2011-09-21 22:42:08 +00:00
|
|
|
|
2011-08-11 09:31:36 +00:00
|
|
|
subselect = '( SELECT res_id' \
|
2008-08-04 15:32:50 +00:00
|
|
|
' FROM ir_translation' \
|
|
|
|
' WHERE name = %s' \
|
|
|
|
' AND lang = %s' \
|
2009-07-06 10:46:15 +00:00
|
|
|
' AND type = %s'
|
|
|
|
instr = ' %s'
|
|
|
|
#Covering in,not in operators with operands (%s,%s) ,etc.
|
2011-09-21 22:42:08 +00:00
|
|
|
if sql_operator in ['in','not in']:
|
2009-07-06 10:46:15 +00:00
|
|
|
instr = ','.join(['%s'] * len(right))
|
2011-09-21 22:52:25 +00:00
|
|
|
subselect += ' AND value ' + sql_operator + ' ' +" (" + instr + ")" \
|
2008-08-04 15:32:50 +00:00
|
|
|
') UNION (' \
|
|
|
|
' SELECT id' \
|
2008-08-14 12:04:41 +00:00
|
|
|
' FROM "' + working_table._table + '"' \
|
2011-09-21 22:42:08 +00:00
|
|
|
' WHERE "' + left + '" ' + sql_operator + ' ' +" (" + instr + "))"
|
2009-07-06 10:46:15 +00:00
|
|
|
else:
|
2011-09-21 22:52:25 +00:00
|
|
|
subselect += ' AND value ' + sql_operator + instr + \
|
2009-07-06 10:46:15 +00:00
|
|
|
') UNION (' \
|
|
|
|
' SELECT id' \
|
|
|
|
' FROM "' + working_table._table + '"' \
|
2011-09-21 22:42:08 +00:00
|
|
|
' WHERE "' + left + '" ' + sql_operator + instr + ")"
|
2009-07-24 09:36:50 +00:00
|
|
|
|
2011-08-11 09:31:36 +00:00
|
|
|
params = [working_table._name + ',' + left,
|
2008-08-04 15:32:50 +00:00
|
|
|
context.get('lang', False) or 'en_US',
|
|
|
|
'model',
|
2008-08-14 12:04:41 +00:00
|
|
|
right,
|
|
|
|
right,
|
2008-08-04 15:32:50 +00:00
|
|
|
]
|
|
|
|
|
2011-08-11 09:31:36 +00:00
|
|
|
self.__exp[i] = ('id', 'inselect', (subselect, params))
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
def __leaf_to_sql(self, leaf, table):
|
|
|
|
left, operator, right = leaf
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2011-07-26 17:24:58 +00:00
|
|
|
if leaf == TRUE_LEAF:
|
|
|
|
query = 'TRUE'
|
|
|
|
params = []
|
|
|
|
|
|
|
|
elif leaf == FALSE_LEAF:
|
|
|
|
query = 'FALSE'
|
|
|
|
params = []
|
|
|
|
|
|
|
|
elif operator == 'inselect':
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" in (%s))' % (table._table, left, right[0])
|
2008-08-14 12:04:41 +00:00
|
|
|
params = right[1]
|
2011-07-26 17:24:58 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
elif operator in ['in', 'not in']:
|
2011-08-10 08:56:04 +00:00
|
|
|
# Two cases: right is a boolean or a list. The boolean case is an
|
|
|
|
# abuse and handled for backward compatibility.
|
|
|
|
if isinstance(right, bool):
|
|
|
|
_logger.warning("The domain term '%s' should use the '=' or '!=' operator." % (leaf,))
|
2010-01-08 08:14:08 +00:00
|
|
|
if operator == 'in':
|
2011-08-10 08:56:04 +00:00
|
|
|
r = 'NOT NULL' if right else 'NULL'
|
2010-01-08 08:14:08 +00:00
|
|
|
else:
|
2011-08-10 08:56:04 +00:00
|
|
|
r = 'NULL' if right else 'NOT NULL'
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s."%s" IS %s)' % (table._table, left, r)
|
2011-08-10 08:56:04 +00:00
|
|
|
params = []
|
|
|
|
elif isinstance(right, (list, tuple)):
|
|
|
|
params = right[:]
|
|
|
|
check_nulls = False
|
|
|
|
for i in range(len(params))[::-1]:
|
|
|
|
if params[i] == False:
|
|
|
|
check_nulls = True
|
|
|
|
del params[i]
|
|
|
|
|
|
|
|
if params:
|
|
|
|
if left == 'id':
|
|
|
|
instr = ','.join(['%s'] * len(params))
|
|
|
|
else:
|
|
|
|
instr = ','.join([table._columns[left]._symbol_set[0]] * len(params))
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s."%s" %s (%s))' % (table._table, left, operator, instr)
|
2011-08-10 08:56:04 +00:00
|
|
|
else:
|
|
|
|
# The case for (left, 'in', []) or (left, 'not in', []).
|
2011-08-10 10:29:22 +00:00
|
|
|
query = 'FALSE' if operator == 'in' else 'TRUE'
|
2011-08-10 13:06:46 +00:00
|
|
|
|
|
|
|
if check_nulls and operator == 'in':
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s OR %s."%s" IS NULL)' % (query, table._table, left)
|
2011-08-10 13:06:46 +00:00
|
|
|
elif not check_nulls and operator == 'not in':
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s OR %s."%s" IS NULL)' % (query, table._table, left)
|
2011-08-10 13:06:46 +00:00
|
|
|
elif check_nulls and operator == 'not in':
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s AND %s."%s" IS NOT NULL)' % (query, table._table, left) # needed only for TRUE.
|
2011-08-10 08:56:04 +00:00
|
|
|
else: # Must not happen.
|
|
|
|
pass
|
2011-07-26 17:24:58 +00:00
|
|
|
|
2011-07-26 18:48:06 +00:00
|
|
|
elif right == False and (left in table._columns) and table._columns[left]._type=="boolean" and (operator == '='):
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s."%s" IS NULL or %s."%s" = false )' % (table._table, left, table._table, left)
|
2008-08-14 12:04:41 +00:00
|
|
|
params = []
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2011-08-10 09:28:59 +00:00
|
|
|
elif (right is False or right is None) and (operator == '='):
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '%s."%s" IS NULL ' % (table._table, left)
|
2011-07-26 18:48:06 +00:00
|
|
|
params = []
|
|
|
|
|
2011-08-09 17:48:14 +00:00
|
|
|
elif right == False and (left in table._columns) and table._columns[left]._type=="boolean" and (operator == '!='):
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s."%s" IS NOT NULL and %s."%s" != false)' % (table._table, left, table._table, left)
|
2011-07-26 18:48:06 +00:00
|
|
|
params = []
|
2009-07-09 18:17:18 +00:00
|
|
|
|
2011-08-10 09:28:59 +00:00
|
|
|
elif (right is False or right is None) and (operator == '!='):
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '%s."%s" IS NOT NULL' % (table._table, left)
|
2011-07-26 18:48:06 +00:00
|
|
|
params = []
|
2009-07-09 18:17:18 +00:00
|
|
|
|
2011-07-26 18:48:06 +00:00
|
|
|
elif (operator == '=?'):
|
|
|
|
if (right is False or right is None):
|
|
|
|
query = 'TRUE'
|
|
|
|
params = []
|
|
|
|
elif left in table._columns:
|
|
|
|
format = table._columns[left]._symbol_set[0]
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s."%s" = %s)' % (table._table, left, format)
|
2011-07-26 18:48:06 +00:00
|
|
|
params = table._columns[left]._symbol_set[1](right)
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2011-09-21 16:21:28 +00:00
|
|
|
query = "(%s.\"%s\" = '%%s')" % (table._table, left)
|
2011-07-26 18:48:06 +00:00
|
|
|
params = right
|
2008-08-14 12:04:41 +00:00
|
|
|
|
2011-07-26 18:48:06 +00:00
|
|
|
elif left == 'id':
|
|
|
|
query = '%s.id %s %%s' % (table._table, operator)
|
|
|
|
params = right
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-07-26 18:48:06 +00:00
|
|
|
else:
|
2011-09-21 22:52:25 +00:00
|
|
|
need_wildcard = operator in ('like', 'ilike', 'not like', 'not ilike')
|
|
|
|
sql_operator = {'=like':'like','=ilike':'ilike'}.get(operator,operator)
|
2011-07-26 18:48:06 +00:00
|
|
|
|
|
|
|
if left in table._columns:
|
2011-09-21 22:52:25 +00:00
|
|
|
format = need_wildcard and '%s' or table._columns[left]._symbol_set[0]
|
|
|
|
if self.has_unaccent and sql_operator in ('ilike', 'not ilike'):
|
|
|
|
query = '(unaccent(%s."%s") %s unaccent(%s))' % (table._table, left, sql_operator, format)
|
2011-08-11 11:01:18 +00:00
|
|
|
else:
|
2011-09-21 22:52:25 +00:00
|
|
|
query = '(%s."%s" %s %s)' % (table._table, left, sql_operator, format)
|
2011-07-26 18:48:06 +00:00
|
|
|
else:
|
2011-09-23 10:18:39 +00:00
|
|
|
# Ugly case to support columns present in database but not in
|
|
|
|
# _columns. This will probably be removed in the future, but
|
|
|
|
# we have to keep it for now.
|
|
|
|
_logger.warning("The domain term '%s' specify a column not declared in _columns." % ((left, operator, right),))
|
2011-09-21 22:52:25 +00:00
|
|
|
if self.has_unaccent and sql_operator in ('ilike', 'not ilike'):
|
|
|
|
query = "(unaccent(%s.\"%s\") %s unaccent('%s'))" % (table._table, left, sql_operator, right)
|
2011-08-11 11:01:18 +00:00
|
|
|
else:
|
2011-09-21 22:52:25 +00:00
|
|
|
query = "(%s.\"%s\" %s '%s')" % (table._table, left, sql_operator, right)
|
2011-07-26 18:48:06 +00:00
|
|
|
|
|
|
|
add_null = False
|
2011-09-21 22:52:25 +00:00
|
|
|
if need_wildcard:
|
2011-07-26 18:48:06 +00:00
|
|
|
if isinstance(right, str):
|
|
|
|
str_utf8 = right
|
|
|
|
elif isinstance(right, unicode):
|
|
|
|
str_utf8 = right.encode('utf-8')
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2011-07-26 18:48:06 +00:00
|
|
|
str_utf8 = str(right)
|
|
|
|
params = '%%%s%%' % str_utf8
|
|
|
|
add_null = not str_utf8
|
|
|
|
elif left in table._columns:
|
|
|
|
params = table._columns[left]._symbol_set[1](right)
|
2011-09-23 10:18:39 +00:00
|
|
|
else:
|
|
|
|
# Matching else clause for the above else clause, where the
|
|
|
|
# params are actually already in the query.
|
|
|
|
params = []
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-07-26 18:48:06 +00:00
|
|
|
if add_null:
|
2011-09-21 16:21:28 +00:00
|
|
|
query = '(%s OR %s."%s" IS NULL)' % (query, table._table, left)
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
if isinstance(params, basestring):
|
|
|
|
params = [params]
|
|
|
|
return (query, params)
|
2008-08-07 13:08:07 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
def to_sql(self):
|
|
|
|
stack = []
|
|
|
|
params = []
|
2011-07-22 15:36:49 +00:00
|
|
|
# Process the domain from right to left, using a stack, to generate a SQL expression.
|
2008-08-14 12:04:41 +00:00
|
|
|
for i, e in reverse_enumerate(self.__exp):
|
2011-07-22 15:36:49 +00:00
|
|
|
if is_leaf(e, internal=True):
|
2010-05-31 15:05:17 +00:00
|
|
|
table = self.__field_tables.get(i, self.__main_table)
|
2008-08-14 12:04:41 +00:00
|
|
|
q, p = self.__leaf_to_sql(e, table)
|
|
|
|
params.insert(0, p)
|
|
|
|
stack.append(q)
|
2011-08-11 09:31:36 +00:00
|
|
|
elif e == NOT_OPERATOR:
|
|
|
|
stack.append('(NOT (%s))' % (stack.pop(),))
|
2008-08-14 12:04:41 +00:00
|
|
|
else:
|
2011-08-11 09:31:36 +00:00
|
|
|
ops = {AND_OPERATOR: ' AND ', OR_OPERATOR: ' OR '}
|
|
|
|
q1 = stack.pop()
|
|
|
|
q2 = stack.pop()
|
|
|
|
stack.append('(%s %s %s)' % (q1, ops[e], q2,))
|
2008-10-07 14:51:30 +00:00
|
|
|
|
2011-07-22 15:36:49 +00:00
|
|
|
assert len(stack) == 1
|
|
|
|
query = stack[0]
|
2010-05-31 15:05:17 +00:00
|
|
|
joins = ' AND '.join(self.__joins)
|
2008-08-14 12:04:41 +00:00
|
|
|
if joins:
|
2011-07-22 15:36:49 +00:00
|
|
|
query = '(%s) AND %s' % (joins, query)
|
2008-08-14 12:04:41 +00:00
|
|
|
return (query, flatten(params))
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
def get_tables(self):
|
2010-05-31 15:05:17 +00:00
|
|
|
return ['"%s"' % t._table for t in self.__all_tables]
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|