2008-08-04 15:32:50 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
from tools import flatten, reverse_enumerate
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-12 14:44:56 +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
|
|
|
|
For more info: http://...
|
2008-08-04 15:32:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def _is_operator(self, element):
|
|
|
|
return isinstance(element, str) \
|
2008-08-14 12:04:41 +00:00
|
|
|
and element in ['&', '|', '!']
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
def _is_leaf(self, element):
|
|
|
|
return isinstance(element, tuple) \
|
|
|
|
and len(element) == 3 \
|
2008-08-07 13:08:07 +00:00
|
|
|
and element[1] in ('=', '!=', '<>', '<=', '<', '>', '>=', '=like', 'like', 'not like', 'ilike', 'not ilike', 'in', 'not in', 'child_of')
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
def __execute_recursive_in(self, cr, s, f, w, ids):
|
2008-08-14 12:04:41 +00:00
|
|
|
#deprecated -> use _left and _right...
|
2008-08-04 15:32:50 +00:00
|
|
|
res = []
|
2008-08-05 08:39:45 +00:00
|
|
|
for i in range(0, len(ids), cr.IN_MAX):
|
|
|
|
subids = ids[i:i+cr.IN_MAX]
|
2008-08-04 15:32:50 +00:00
|
|
|
cr.execute('SELECT "%s"' \
|
|
|
|
' FROM "%s"' \
|
|
|
|
' WHERE "%s" in (%s)' % (s, f, w, ','.join(['%d']*len(subids))),
|
|
|
|
subids)
|
|
|
|
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):
|
|
|
|
raise ValueError('Bad expression: %r' % (exp,))
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__exp = exp
|
|
|
|
self.__tables = {} # used to store the table to use for the sql generation. key = index of the leaf
|
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
|
|
|
|
|
|
|
|
|
|
|
def parse(self, cr, uid, table, context):
|
2008-08-14 12:04:41 +00:00
|
|
|
""" transform the leafs of the expression """
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
def _rec_get(ids, table, parent):
|
|
|
|
if not ids:
|
|
|
|
return []
|
|
|
|
ids2 = table.search(cr, uid, [(parent, 'in', ids)], context=context)
|
|
|
|
return ids + _rec_get(ids2, table, parent)
|
2008-08-07 13:08:07 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
if not self.__exp:
|
|
|
|
return self
|
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__main_table = table
|
|
|
|
working_table = table
|
|
|
|
|
|
|
|
for i, e in enumerate(self.__exp):
|
|
|
|
if self._is_operator(e) or e == self.__DUMMY_LEAF:
|
|
|
|
continue
|
|
|
|
left, operator, right = e
|
|
|
|
|
|
|
|
if left in table._inherit_fields:
|
2008-08-14 15:06:20 +00:00
|
|
|
working_table = table.pool.get(table._inherit_fields[left][0])
|
2008-08-14 12:04:41 +00:00
|
|
|
if working_table not in self.__tables.values():
|
|
|
|
self.__joins.append('%s.%s' % (table._table, table._inherits[working_table._name]))
|
|
|
|
|
|
|
|
self.__tables[i] = working_table
|
|
|
|
|
|
|
|
fargs = left.split('.', 1)
|
|
|
|
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':
|
|
|
|
right += _rec_get(right, working_table, working_table._parent_name)
|
|
|
|
self.__exp[i] = ('id', 'in', right)
|
|
|
|
continue
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.__exp[i] = (fargs[0], 'in', right)
|
|
|
|
continue
|
2008-08-07 13:08:07 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
if field._properties:
|
|
|
|
# this is a function field
|
2008-08-14 12:04:41 +00:00
|
|
|
if not field.store:
|
|
|
|
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:
|
|
|
|
subexp = field.search(cr, uid, table, left, [self.__exp[i]])
|
|
|
|
# we assume that the expression is valid
|
|
|
|
# we create a dummy leaf for forcing the parsing of the resulting expression
|
|
|
|
self.__exp[i] = '&'
|
|
|
|
self.__exp.insert(i + 1, self.__DUMMY_LEAF)
|
|
|
|
for j, se in enumerate(subexp):
|
|
|
|
self.__exp.insert(i + 2 + j, se)
|
|
|
|
|
|
|
|
# else, the value of the field is store in the database, so we search on it
|
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
elif field._type == 'one2many':
|
2008-08-14 12:04:41 +00:00
|
|
|
if isinstance(right, basestring):
|
|
|
|
ids2 = [x[0] for x in field_obj.name_search(cr, uid, right, [], operator)]
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
ids2 = list(right)
|
2008-08-04 15:32:50 +00:00
|
|
|
if not ids2:
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__exp[i] = ('id', '=', '0')
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__exp[i] = ('id', 'in', self.__execute_recursive_in(cr, field._fields_id, field_obj._table, 'id', ids2))
|
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':
|
|
|
|
if isinstance(right, basestring):
|
|
|
|
ids2 = [x[0] for x in field_obj.name_search(cr, uid, right, [], 'like')]
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
ids2 = list(right)
|
2008-08-07 13:08:07 +00:00
|
|
|
|
2008-08-04 15:32:50 +00:00
|
|
|
def _rec_convert(ids):
|
|
|
|
if field_obj == table:
|
|
|
|
return ids
|
|
|
|
return self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, ids)
|
2008-08-07 13:08:07 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
self.__exp[i] = ('id', 'in', _rec_convert(ids2 + _rec_get(ids2, field_obj, working_table._parent_name)))
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
if isinstance(right, basestring):
|
|
|
|
res_ids = [x[0] for x in field_obj.name_search(cr, uid, right, [], operator)]
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
res_ids = list(right)
|
|
|
|
self.__exp[i] = ('id', 'in', self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, res_ids) or [0])
|
2008-08-04 15:32:50 +00:00
|
|
|
elif field._type == 'many2one':
|
2008-08-14 12:04:41 +00:00
|
|
|
if operator == 'child_of':
|
|
|
|
if isinstance(right, basestring):
|
|
|
|
ids2 = [x[0] for x in field_obj.search_name(cr, uid, right, [], 'like')]
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
ids2 = list(right)
|
2008-08-07 13:08:07 +00:00
|
|
|
|
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:
|
|
|
|
right = ids2 + _rec_get(ids2, field_obj, working_table._parent_name)
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
right = ids2 + _rec_get(ids2, working_table, left)
|
|
|
|
left = 'id'
|
|
|
|
self.__exp[i] = (left, 'in', right)
|
2008-08-04 15:32:50 +00:00
|
|
|
else:
|
2008-08-14 12:04:41 +00:00
|
|
|
if isinstance(right, basestring):
|
|
|
|
res_ids = field_obj.name_search(cr, uid, right, [], operator)
|
|
|
|
right = map(lambda x: x[0], res_ids)
|
|
|
|
self.__exp[i] = (left, 'in', right)
|
2008-08-07 13:08:07 +00:00
|
|
|
else:
|
2008-08-04 15:32:50 +00:00
|
|
|
# other field type
|
|
|
|
if field.translate:
|
2008-08-14 12:04:41 +00:00
|
|
|
if operator in ('like', 'ilike', 'not like', 'not ilike'):
|
|
|
|
right = '%%%s%%' % right
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
query1 = '( SELECT res_id' \
|
|
|
|
' FROM ir_translation' \
|
|
|
|
' WHERE name = %s' \
|
|
|
|
' AND lang = %s' \
|
|
|
|
' AND type = %s' \
|
2008-08-14 12:04:41 +00:00
|
|
|
' AND value ' + operator + ' %s' \
|
2008-08-04 15:32:50 +00:00
|
|
|
') UNION (' \
|
|
|
|
' SELECT id' \
|
2008-08-14 12:04:41 +00:00
|
|
|
' FROM "' + working_table._table + '"' \
|
|
|
|
' WHERE "' + left + '" ' + operator + ' %s' \
|
2008-08-04 15:32: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):
|
|
|
|
left, operator, right = leaf
|
|
|
|
|
|
|
|
if operator == 'inselect':
|
|
|
|
query = '(%s.%s in (%s))' % (table._table, left, right[0])
|
|
|
|
params = right[1]
|
|
|
|
elif operator in ['in', 'not in']:
|
|
|
|
params = right[:]
|
|
|
|
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)'
|
|
|
|
|
|
|
|
if len_after:
|
|
|
|
if left == 'id':
|
|
|
|
instr = ','.join(['%d'] * len_after)
|
|
|
|
else:
|
|
|
|
instr = ','.join([table._columns[left]._symbol_set[0]] * len_after)
|
|
|
|
query = '(%s.%s %s (%s))' % (table._table, left, operator, instr)
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
if check_nulls:
|
|
|
|
query = '(%s OR %s IS NULL)' % (query, left)
|
|
|
|
else:
|
|
|
|
params = []
|
|
|
|
if right is False and operator == '=':
|
|
|
|
query = '%s IS NULL' % left
|
|
|
|
elif right is False and operator in ['<>', '!=']:
|
|
|
|
query = '%s IS NOT NULL' % left
|
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:
|
2008-08-14 12:04:41 +00:00
|
|
|
like = operator in ('like', 'ilike', 'not like', 'not ilike')
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
op = operator == '=like' and 'like' or operator
|
|
|
|
if left in table._columns:
|
|
|
|
format = like and '%s' or table._columns[left]._symbol_set[0]
|
|
|
|
query = '(%s.%s %s %s)' % (table._table, left, op, format)
|
|
|
|
else:
|
|
|
|
query = "(%s.%s %s '%s')" % (table._table, left, op, right)
|
|
|
|
|
|
|
|
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:
|
|
|
|
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):
|
|
|
|
if self._is_leaf(e):
|
|
|
|
table = self.__tables.has_key(i) and self.__tables[i] or self.__main_table
|
|
|
|
q, p = self.__leaf_to_sql(e, table)
|
|
|
|
params.insert(0, p)
|
|
|
|
stack.append(q)
|
|
|
|
else:
|
|
|
|
if e == '!':
|
|
|
|
stack.append('(NOT (%s))' % (stack.pop(),))
|
|
|
|
else:
|
|
|
|
ops = {'&': ' AND ', '|': ' OR '}
|
|
|
|
q1 = stack.pop()
|
|
|
|
q2 = stack.pop()
|
|
|
|
stack.append('(%s %s %s)' % (q1, ops[e], q2,))
|
|
|
|
|
|
|
|
query = ' AND '.join(reversed(stack))
|
|
|
|
joins = ' AND '.join(map(lambda j: '%s.id = %s' % (self.__main_table._table, j), self.__joins))
|
|
|
|
if joins:
|
|
|
|
query = '(%s AND (%s))' % (joins, query)
|
|
|
|
return (query, flatten(params))
|
2008-08-04 15:32:50 +00:00
|
|
|
|
2008-08-14 12:04:41 +00:00
|
|
|
def get_tables(self):
|
|
|
|
return ['"%s"' % t._table for t in set(self.__tables.values())]
|
2008-08-04 15:32:50 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|