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-02-07 12:57:23 +00:00
|
|
|
from openerp.tools import flatten, reverse_enumerate
|
2008-12-09 08:03:55 +00:00
|
|
|
import fields
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-06-23 09:03:57 +00:00
|
|
|
#.apidoc title: Domain Expressions
|
|
|
|
|
2011-03-24 11:17:57 +00:00
|
|
|
NOT_OPERATOR = '!'
|
|
|
|
OR_OPERATOR = '|'
|
|
|
|
AND_OPERATOR = '&'
|
2008-08-12 14:44:56 +00:00
|
|
|
|
2011-05-31 14:43:18 +00:00
|
|
|
TRUE_DOMAIN = [(1,'=',1)]
|
|
|
|
FALSE_DOMAIN = [(0,'=',1)]
|
|
|
|
|
|
|
|
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]
|
|
|
|
result[0:0] = ['&'] # put an extra '&' in front
|
|
|
|
expected = 1
|
|
|
|
result.append(token)
|
|
|
|
if isinstance(token, (list,tuple)): # domain term
|
|
|
|
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``
|
|
|
|
have been added together using the binary operator ``operator``.
|
|
|
|
|
|
|
|
: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.
|
|
|
|
"""
|
|
|
|
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):
|
|
|
|
""" AND([D1,D2,...]) returns a domain representing D1 and D2 and ... """
|
|
|
|
return combine(AND_OPERATOR, TRUE_DOMAIN, FALSE_DOMAIN, domains)
|
|
|
|
|
|
|
|
def OR(domains):
|
|
|
|
""" OR([D1,D2,...]) returns a domain representing D1 or D2 or ... """
|
|
|
|
return combine(OR_OPERATOR, FALSE_DOMAIN, TRUE_DOMAIN, domains)
|
|
|
|
|
|
|
|
|
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-03-24 11:17:57 +00:00
|
|
|
@classmethod
|
|
|
|
def _is_operator(cls, element):
|
|
|
|
return isinstance(element, (str, unicode)) and element in [AND_OPERATOR, OR_OPERATOR, NOT_OPERATOR]
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2011-03-24 11:17:57 +00:00
|
|
|
@classmethod
|
|
|
|
def _is_leaf(cls, element, internal=False):
|
2009-11-20 14:31:04 +00:00
|
|
|
OPS = ('=', '!=', '<>', '<=', '<', '>', '>=', '=?', '=like', '=ilike', 'like', 'not like', 'ilike', 'not ilike', 'in', 'not in', 'child_of')
|
2008-08-18 12:13:49 +00:00
|
|
|
INTERNAL_OPS = OPS + ('inselect',)
|
2008-08-17 18:28:29 +00:00
|
|
|
return (isinstance(element, tuple) or isinstance(element, list)) \
|
2008-08-04 15:32:50 +00:00
|
|
|
and len(element) == 3 \
|
2008-08-18 12:13:49 +00:00
|
|
|
and (((not internal) and element[1] in OPS) \
|
|
|
|
or (internal and element[1] in INTERNAL_OPS))
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2009-10-08 07:53:35 +00:00
|
|
|
def __execute_recursive_in(self, cr, s, f, w, ids, op, type):
|
2009-09-03 07:32:40 +00:00
|
|
|
# todo: merge into parent query as sub-query
|
2008-08-04 15:32:50 +00:00
|
|
|
res = []
|
2009-10-08 07:53:35 +00:00
|
|
|
if ids:
|
|
|
|
if op in ['<','>','>=','<=']:
|
|
|
|
cr.execute('SELECT "%s"' \
|
|
|
|
' FROM "%s"' \
|
2010-06-15 13:27:22 +00:00
|
|
|
' WHERE "%s" %s %%s' % (s, f, w, op), (ids[0],))
|
2009-10-08 07:53:35 +00:00
|
|
|
res.extend([r[0] for r in cr.fetchall()])
|
|
|
|
else:
|
|
|
|
for i in range(0, len(ids), cr.IN_MAX):
|
|
|
|
subids = ids[i:i+cr.IN_MAX]
|
|
|
|
cr.execute('SELECT "%s"' \
|
|
|
|
' FROM "%s"' \
|
2010-06-18 10:16:41 +00:00
|
|
|
' WHERE "%s" IN %%s' % (s, f, w),(tuple(subids),))
|
2010-06-15 13:27:22 +00:00
|
|
|
res.extend([r[0] for r in cr.fetchall()])
|
2009-10-08 07:53:35 +00:00
|
|
|
else:
|
|
|
|
cr.execute('SELECT distinct("%s")' \
|
|
|
|
' FROM "%s" where "%s" is not null' % (s, f, s)),
|
2008-08-04 15:32:50 +00:00
|
|
|
res.extend([r[0] for r in cr.fetchall()])
|
|
|
|
return res
|
|
|
|
|
|
|
|
def __init__(self, exp):
|
2008-08-14 12:04:41 +00:00
|
|
|
# check if the expression is valid
|
|
|
|
if not reduce(lambda acc, val: acc and (self._is_operator(val) or self._is_leaf(val)), exp, True):
|
2008-08-16 16:30:06 +00:00
|
|
|
raise ValueError('Bad domain expression: %r' % (exp,))
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__exp = exp
|
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()
|
|
|
|
self.__DUMMY_LEAF = (1, '=', 1) # a dummy leaf that must not be parsed or sql generated
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2010-06-24 15:56:02 +00:00
|
|
|
@property
|
|
|
|
def exp(self):
|
|
|
|
return self.__exp[:]
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
def parse(self, cr, uid, table, context):
|
2008-08-14 12:04:41 +00:00
|
|
|
""" transform the leafs of the expression """
|
2008-08-16 16:30:06 +00:00
|
|
|
if not self.__exp:
|
|
|
|
return self
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2009-02-17 15:01:35 +00:00
|
|
|
def _rec_get(ids, table, parent=None, left='id', prefix=''):
|
2008-08-17 18:28:29 +00:00
|
|
|
if table._parent_store and (not table.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 = []
|
|
|
|
for o in table.browse(cr, uid, ids, context=context):
|
|
|
|
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:
|
|
|
|
return [(left, 'in', table.search(cr, uid, doms, context=context))]
|
|
|
|
return doms
|
2008-08-14 17:05:08 +00:00
|
|
|
else:
|
2008-08-19 23:15:37 +00:00
|
|
|
def rg(ids, table, parent):
|
|
|
|
if not ids:
|
|
|
|
return []
|
|
|
|
ids2 = table.search(cr, uid, [(parent, 'in', ids)], context=context)
|
2008-10-07 14:51:30 +00:00
|
|
|
return ids + rg(ids2, table, parent)
|
2009-02-17 15:01:35 +00:00
|
|
|
return [(left, 'in', rg(ids, table, parent or table._parent_name))]
|
2008-08-07 13:08:07 +00:00
|
|
|
|
2011-06-08 05:26:03 +00:00
|
|
|
def child_of_right_to_ids(value):
|
2011-06-24 08:43:11 +00:00
|
|
|
""" Normalize a single id, or a string, or a list of ids to a list of ids.
|
|
|
|
|
|
|
|
This function is always used with _rec_get() above, so it should be
|
|
|
|
called directly from _rec_get instead of repeatedly before _rec_get.
|
|
|
|
|
|
|
|
"""
|
2011-05-26 09:28:53 +00:00
|
|
|
if isinstance(value, basestring):
|
|
|
|
return [x[0] for x in field_obj.name_search(cr, uid, value, [], 'ilike', context=context, limit=None)]
|
|
|
|
elif isinstance(value, (int, long)):
|
2011-06-08 05:26:03 +00:00
|
|
|
return [value]
|
2011-05-26 09:28:53 +00:00
|
|
|
else:
|
|
|
|
return list(value)
|
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__main_table = table
|
2010-08-26 11:45:31 +00:00
|
|
|
self.__all_tables.add(table)
|
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]
|
2008-08-14 12:04:41 +00:00
|
|
|
if self._is_operator(e) or e == self.__DUMMY_LEAF:
|
|
|
|
continue
|
|
|
|
left, operator, right = e
|
2010-08-20 10:20:12 +00:00
|
|
|
operator = operator.lower()
|
2008-08-14 16:41:17 +00:00
|
|
|
working_table = table
|
2009-08-18 13:44:45 +00:00
|
|
|
main_table = table
|
2009-08-12 13:06:26 +00:00
|
|
|
fargs = left.split('.', 1)
|
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:
|
|
|
|
field = main_table._columns.get(fargs[0], False)
|
|
|
|
if field:
|
|
|
|
working_table = main_table
|
2010-05-31 15:05:17 +00:00
|
|
|
self.__field_tables[i] = working_table
|
2009-08-18 13:44:45 +00:00
|
|
|
break
|
2009-12-22 19:01:58 +00:00
|
|
|
working_table = main_table.pool.get(main_table._inherit_fields[fargs[0]][0])
|
2010-05-31 15:05:17 +00:00
|
|
|
if working_table not in self.__all_tables:
|
2011-09-19 20:18:45 +00:00
|
|
|
self.__joins.append('%s."%s"=%s."%s"' % (working_table._table, 'id', main_table._table, main_table._inherits[working_table._name]))
|
2010-08-26 11:45:31 +00:00
|
|
|
self.__all_tables.add(working_table)
|
2009-08-18 13:44:45 +00:00
|
|
|
main_table = working_table
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
field = working_table._columns.get(fargs[0], False)
|
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-06-24 08:43:11 +00:00
|
|
|
ids2 = child_of_right_to_ids(right)
|
|
|
|
dom = _rec_get(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)
|
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
|
|
|
if right == []:
|
|
|
|
self.__exp[i] = ( 'id', '=', 0 )
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
if field._type in ['many2many','one2many']:
|
|
|
|
right = field_obj.search(cr, uid, [(fargs[1], operator, right)], context=context)
|
|
|
|
right1 = table.search(cr, uid, [(fargs[0],'in', right)], 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
|
|
|
if right1 == []:
|
|
|
|
self.__exp[i] = ( 'id', '=', 0 )
|
|
|
|
else:
|
|
|
|
self.__exp[i] = ('id', 'in', right1)
|
2010-12-29 17:42:23 +00:00
|
|
|
|
2010-10-29 20:39:37 +00:00
|
|
|
if not isinstance(field,fields.property):
|
|
|
|
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
|
|
|
|
self.__exp[i] = self.__DUMMY_LEAF
|
|
|
|
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:
|
|
|
|
self.__exp[i] = self.__DUMMY_LEAF
|
|
|
|
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
|
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
|
|
|
self.__exp.insert(i + 1, self.__DUMMY_LEAF)
|
|
|
|
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-06-08 05:26:03 +00:00
|
|
|
ids2 = child_of_right_to_ids(right)
|
2009-11-23 12:27:36 +00:00
|
|
|
if field._obj != working_table._name:
|
|
|
|
dom = _rec_get(ids2, field_obj, left=left, prefix=field._obj)
|
2009-10-08 07:53:35 +00:00
|
|
|
else:
|
2009-11-23 12:27:36 +00:00
|
|
|
dom = _rec_get(ids2, working_table, parent=left)
|
|
|
|
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:
|
|
|
|
if not isinstance(right,list):
|
|
|
|
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
|
|
|
|
self.__exp[i] = ('id','=',0)
|
|
|
|
else:
|
|
|
|
call_null = True
|
|
|
|
operator = 'in' # operator changed because ids are directly related to main object
|
2009-11-23 12:27:36 +00:00
|
|
|
else:
|
|
|
|
call_null = False
|
|
|
|
o2m_op = 'in'
|
|
|
|
if operator in ['not like','not ilike','not in','<>','!=']:
|
|
|
|
o2m_op = 'not in'
|
|
|
|
self.__exp[i] = ('id', o2m_op, self.__execute_recursive_in(cr, field._fields_id, field_obj._table, 'id', ids2, operator, field._type))
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-11-23 12:27:36 +00:00
|
|
|
if call_null:
|
|
|
|
o2m_op = 'not in'
|
|
|
|
if operator in ['not like','not ilike','not in','<>','!=']:
|
2010-06-15 13:27:22 +00:00
|
|
|
o2m_op = 'in'
|
|
|
|
self.__exp[i] = ('id', o2m_op, self.__execute_recursive_in(cr, field._fields_id, field_obj._table, 'id', [], operator, field._type) or [0])
|
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
|
2009-10-08 07:53:35 +00:00
|
|
|
return self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, ids, operator, field._type)
|
2008-10-07 14:51:30 +00:00
|
|
|
|
2011-06-24 08:43:11 +00:00
|
|
|
ids2 = child_of_right_to_ids(right)
|
2009-02-17 15:01:35 +00:00
|
|
|
dom = _rec_get(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
|
|
|
|
self.__exp[i] = ('id','=',0)
|
2010-06-15 13:27:22 +00:00
|
|
|
else:
|
2010-01-19 14:04:05 +00:00
|
|
|
call_null_m2m = True
|
|
|
|
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
|
2010-06-15 13:27:22 +00:00
|
|
|
m2m_op = 'in'
|
2009-10-08 07:53:35 +00:00
|
|
|
if operator in ['not like','not ilike','not in','<>','!=']:
|
|
|
|
m2m_op = 'not in'
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-10-08 07:53:35 +00:00
|
|
|
self.__exp[i] = ('id', m2m_op, self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, res_ids, operator, field._type) or [0])
|
|
|
|
if call_null_m2m:
|
|
|
|
m2m_op = 'not in'
|
|
|
|
if operator in ['not like','not ilike','not in','<>','!=']:
|
2010-06-15 13:27:22 +00:00
|
|
|
m2m_op = 'in'
|
2009-10-08 07:53:35 +00:00
|
|
|
self.__exp[i] = ('id', m2m_op, self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, [], operator, field._type) or [0])
|
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-06-08 05:26:03 +00:00
|
|
|
ids2 = child_of_right_to_ids(right)
|
2008-08-04 15:32:50 +00:00
|
|
|
self.__operator = 'in'
|
2008-08-14 12:04:41 +00:00
|
|
|
if field._obj != working_table._name:
|
2009-02-17 15:01:35 +00:00
|
|
|
dom = _rec_get(ids2, field_obj, left=left, prefix=field._obj)
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2009-02-17 15:01:35 +00:00
|
|
|
dom = _rec_get(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:
|
2010-08-20 10:20:12 +00:00
|
|
|
def _get_expression(field_obj,cr, uid, left, right, operator, context=None):
|
|
|
|
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
|
|
|
|
2010-10-06 18:04:32 +00:00
|
|
|
dict_op = {'not in':'!=','in':'=','=':'in','!=':'not in','<>':'not in'}
|
2010-08-20 10:20:12 +00:00
|
|
|
if isinstance(right,tuple):
|
|
|
|
right = list(right)
|
|
|
|
if (not isinstance(right,list)) and operator in ['not in','in']:
|
|
|
|
operator = dict_op[operator]
|
2010-10-06 18:04:32 +00:00
|
|
|
elif isinstance(right,list) and operator in ['<>','!=','=']: #for domain (FIELD,'=',['value1','value2'])
|
|
|
|
operator = dict_op[operator]
|
2009-01-15 17:41:03 +00:00
|
|
|
res_ids = field_obj.name_search(cr, uid, right, [], operator, limit=None, context=c)
|
2010-05-12 11:26:20 +00:00
|
|
|
if not res_ids:
|
2010-08-20 10:20:12 +00:00
|
|
|
return ('id','=',0)
|
2010-05-12 11:26:20 +00:00
|
|
|
else:
|
|
|
|
right = map(lambda x: x[0], res_ids)
|
2010-08-20 10:20:12 +00:00
|
|
|
return (left, 'in', right)
|
|
|
|
|
|
|
|
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
|
|
|
|
elif isinstance(right,(list,tuple)):
|
|
|
|
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
|
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 == []:
|
|
|
|
m2o_str = False
|
|
|
|
if operator in ('not in', '!=', '<>'):
|
|
|
|
# (many2one not in []) should return all records
|
|
|
|
self.__exp[i] = self.__DUMMY_LEAF
|
|
|
|
else:
|
|
|
|
self.__exp[i] = ('id','=',0)
|
2010-10-06 18:04:32 +00:00
|
|
|
else:
|
|
|
|
new_op = '='
|
|
|
|
if operator in ['not like','not ilike','not in','<>','!=']:
|
|
|
|
new_op = '!='
|
|
|
|
#Is it ok to put 'left' and not 'id' ?
|
|
|
|
self.__exp[i] = (left,new_op,False)
|
2011-05-18 12:41:50 +00:00
|
|
|
|
2010-08-20 10:20:12 +00:00
|
|
|
if m2o_str:
|
|
|
|
self.__exp[i] = _get_expression(field_obj,cr, uid, left, right, operator, context=context)
|
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-06-10 13:36:58 +00:00
|
|
|
operator = {'=like':'like','=ilike':'ilike'}.get(operator,operator)
|
2008-08-14 12:04:41 +00:00
|
|
|
if operator in ('like', 'ilike', 'not like', 'not ilike'):
|
|
|
|
right = '%%%s%%' % right
|
2011-06-10 13:36:58 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
query1 = '( SELECT res_id' \
|
|
|
|
' 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.
|
|
|
|
if operator in ['in','not in']:
|
|
|
|
instr = ','.join(['%s'] * len(right))
|
|
|
|
query1 += ' AND value ' + 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 + '"' \
|
2009-07-06 10:46:15 +00:00
|
|
|
' WHERE "' + left + '" ' + operator + ' ' +" (" + instr + "))"
|
|
|
|
else:
|
|
|
|
query1 += ' AND value ' + operator + instr + \
|
|
|
|
') UNION (' \
|
|
|
|
' SELECT id' \
|
|
|
|
' FROM "' + working_table._table + '"' \
|
|
|
|
' WHERE "' + left + '" ' + operator + instr + ")"
|
2009-07-24 09:36:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
query2 = [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
|
|
|
]
|
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__exp[i] = ('id', 'inselect', (query1, query2))
|
2008-08-04 15:32:50 +00:00
|
|
|
return self
|
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
def __leaf_to_sql(self, leaf, table):
|
2008-08-28 14:51:49 +00:00
|
|
|
if leaf == self.__DUMMY_LEAF:
|
2008-10-07 14:51:30 +00:00
|
|
|
return ('(1=1)', [])
|
2008-08-14 12:04:41 +00:00
|
|
|
left, operator, right = leaf
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
if 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]
|
|
|
|
elif operator in ['in', 'not in']:
|
2010-01-08 08:14:08 +00:00
|
|
|
params = right and right[:] or []
|
2008-08-14 12:04:41 +00:00
|
|
|
len_before = len(params)
|
|
|
|
for i in range(len_before)[::-1]:
|
|
|
|
if params[i] == False:
|
|
|
|
del params[i]
|
|
|
|
|
|
|
|
len_after = len(params)
|
|
|
|
check_nulls = len_after != len_before
|
|
|
|
query = '(1=0)'
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
if len_after:
|
|
|
|
if left == 'id':
|
2009-01-21 13:17:25 +00:00
|
|
|
instr = ','.join(['%s'] * len_after)
|
2008-08-14 12:04:41 +00:00
|
|
|
else:
|
|
|
|
instr = ','.join([table._columns[left]._symbol_set[0]] * len_after)
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" %s (%s))' % (table._table, left, operator, instr)
|
2010-01-08 08:14:08 +00:00
|
|
|
else:
|
|
|
|
# the case for [field, 'in', []] or [left, 'not in', []]
|
|
|
|
if operator == 'in':
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" IS NULL)' % (table._table, left)
|
2010-01-08 08:14:08 +00:00
|
|
|
else:
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" IS NOT NULL)' % (table._table, left)
|
2008-08-14 12:04:41 +00:00
|
|
|
if check_nulls:
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s OR %s."%s" IS NULL)' % (query, table._table, left)
|
2008-08-14 12:04:41 +00:00
|
|
|
else:
|
|
|
|
params = []
|
2010-06-15 13:27:22 +00:00
|
|
|
|
2009-05-13 05:18:46 +00:00
|
|
|
if right == False and (leaf[0] in table._columns) and table._columns[leaf[0]]._type=="boolean" and (operator == '='):
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" IS NULL or %s."%s" = false )' % (table._table, left,table._table, left)
|
2009-05-11 06:44:16 +00:00
|
|
|
elif (((right == False) and (type(right)==bool)) or (right is None)) and (operator == '='):
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '%s."%s" IS NULL ' % (table._table, left)
|
2009-05-13 05:18:46 +00:00
|
|
|
elif right == False and (leaf[0] in table._columns) and table._columns[leaf[0]]._type=="boolean" and (operator in ['<>', '!=']):
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" IS NOT NULL and %s."%s" != false)' % (table._table, left,table._table, left)
|
2008-10-27 09:23:52 +00:00
|
|
|
elif (((right == False) and (type(right)==bool)) or right is None) and (operator in ['<>', '!=']):
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '%s."%s" IS NOT NULL' % (table._table, left)
|
2009-07-09 18:17:18 +00:00
|
|
|
elif (operator == '=?'):
|
|
|
|
op = '='
|
|
|
|
if (right is False or right is None):
|
|
|
|
return ( 'TRUE',[])
|
|
|
|
if left in table._columns:
|
|
|
|
format = table._columns[left]._symbol_set[0]
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" %s %s)' % (table._table, left, op, format)
|
2009-07-09 18:17:18 +00:00
|
|
|
params = table._columns[left]._symbol_set[1](right)
|
|
|
|
else:
|
2011-09-19 20:18:45 +00:00
|
|
|
query = "(%s.\"%s\" %s '%%s')" % (table._table, left, op)
|
2009-07-09 18:17:18 +00:00
|
|
|
params = right
|
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
if left == 'id':
|
|
|
|
query = '%s.id %s %%s' % (table._table, operator)
|
|
|
|
params = right
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2011-06-10 13:36:58 +00:00
|
|
|
operator = {'=like':'like','=ilike':'ilike'}.get(operator,operator)
|
2008-08-14 12:04:41 +00:00
|
|
|
like = operator in ('like', 'ilike', 'not like', 'not ilike')
|
2011-06-10 13:36:58 +00:00
|
|
|
|
2009-09-17 07:27:12 +00:00
|
|
|
op = {'=like':'like','=ilike':'ilike'}.get(operator,operator)
|
2008-08-14 12:04:41 +00:00
|
|
|
if left in table._columns:
|
|
|
|
format = like and '%s' or table._columns[left]._symbol_set[0]
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s."%s" %s %s)' % (table._table, left, op, format)
|
2008-08-14 12:04:41 +00:00
|
|
|
else:
|
2011-09-19 20:18:45 +00:00
|
|
|
query = "(%s.\"%s\" %s '%s')" % (table._table, left, op, right)
|
2008-08-14 12:04:41 +00:00
|
|
|
|
|
|
|
add_null = False
|
|
|
|
if like:
|
|
|
|
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:
|
2008-08-14 12:04:41 +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)
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
if add_null:
|
2011-09-19 20:18:45 +00:00
|
|
|
query = '(%s OR \"%s\" IS NULL)' % (query, 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 = []
|
|
|
|
for i, e in reverse_enumerate(self.__exp):
|
2008-08-18 07:52:32 +00:00
|
|
|
if self._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)
|
|
|
|
else:
|
2011-03-24 11:17:57 +00:00
|
|
|
if e == NOT_OPERATOR:
|
2008-08-14 12:04:41 +00:00
|
|
|
stack.append('(NOT (%s))' % (stack.pop(),))
|
|
|
|
else:
|
2011-03-24 11:17:57 +00:00
|
|
|
ops = {AND_OPERATOR: ' AND ', OR_OPERATOR: ' OR '}
|
2008-08-14 12:04:41 +00:00
|
|
|
q1 = stack.pop()
|
|
|
|
q2 = stack.pop()
|
|
|
|
stack.append('(%s %s %s)' % (q1, ops[e], q2,))
|
2008-10-07 14:51:30 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
query = ' AND '.join(reversed(stack))
|
2010-05-31 15:05:17 +00:00
|
|
|
joins = ' AND '.join(self.__joins)
|
2008-08-14 12:04:41 +00:00
|
|
|
if joins:
|
2008-08-19 21:42:53 +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:
|
|
|
|
|