[FIX] expression: allow exclusion of records with translated fields
[odoo/odoo.git] / bin / osv / expression.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 #
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from tools import flatten, reverse_enumerate
24 import fields
25
26
27 class expression(object):
28     """
29     parse a domain expression
30     use a real polish notation
31     leafs are still in a ('foo', '=', 'bar') format
32     For more info: http://christophe-simonis-at-tiny.blogspot.com/2008/08/new-new-domain-notation.html
33     """
34
35     def _is_operator(self, element):
36         return isinstance(element, (str, unicode)) and element in ['&', '|', '!']
37
38     def _is_leaf(self, element, internal=False):
39         OPS = ('=', '!=', '<>', '<=', '<', '>', '>=', '=?', '=like', '=ilike', 'like', 'not like', 'ilike', 'not ilike', 'in', 'not in', 'child_of')
40         INTERNAL_OPS = OPS + ('inselect', 'not inselect')
41         return (isinstance(element, tuple) or isinstance(element, list)) \
42            and len(element) == 3 \
43            and (((not internal) and element[1] in OPS) \
44                 or (internal and element[1] in INTERNAL_OPS))
45
46     def __execute_recursive_in(self, cr, s, f, w, ids, op, type):
47         # todo: merge into parent query as sub-query
48         res = []
49         if ids:
50             if op in ['<','>','>=','<=']:
51                 cr.execute('SELECT "%s"'    \
52                                '  FROM "%s"'    \
53                                ' WHERE "%s" %s %%s' % (s, f, w, op), (ids[0],))
54                 res.extend([r[0] for r in cr.fetchall()])
55             else:
56                 for i in range(0, len(ids), cr.IN_MAX):
57                     subids = ids[i:i+cr.IN_MAX]
58                     cr.execute('SELECT "%s"'    \
59                                '  FROM "%s"'    \
60                                '  WHERE "%s" IN %%s' % (s, f, w),(tuple(subids),))
61                     res.extend([r[0] for r in cr.fetchall()])
62         else:
63             cr.execute('SELECT distinct("%s")'    \
64                            '  FROM "%s" where "%s" is not null'  % (s, f, s)),
65             res.extend([r[0] for r in cr.fetchall()])
66         return res
67
68     def __init__(self, exp):
69         # check if the expression is valid
70         if not reduce(lambda acc, val: acc and (self._is_operator(val) or self._is_leaf(val)), exp, True):
71             raise ValueError('Bad domain expression: %r' % (exp,))
72         self.__exp = exp
73         self.__field_tables = {}  # used to store the table to use for the sql generation. key = index of the leaf
74         self.__all_tables = set()
75         self.__joins = []
76         self.__main_table = None # 'root' table. set by parse()
77         self.__DUMMY_LEAF = (1, '=', 1) # a dummy leaf that must not be parsed or sql generated
78
79     @property
80     def exp(self):
81         return self.__exp[:]
82
83     def parse(self, cr, uid, table, context):
84         """ transform the leafs of the expression """
85         if not self.__exp:
86             return self
87
88         def _rec_get(ids, table, parent=None, left='id', prefix=''):
89             if table._parent_store and (not table.pool._init):
90 # TODO: Improve where joins are implemented for many with '.', replace by:
91 # doms += ['&',(prefix+'.parent_left','<',o.parent_right),(prefix+'.parent_left','>=',o.parent_left)]
92                 doms = []
93                 for o in table.browse(cr, uid, ids, context=context):
94                     if doms:
95                         doms.insert(0, '|')
96                     doms += ['&', ('parent_left', '<', o.parent_right), ('parent_left', '>=', o.parent_left)]
97                 if prefix:
98                     return [(left, 'in', table.search(cr, uid, doms, context=context))]
99                 return doms
100             else:
101                 def rg(ids, table, parent):
102                     if not ids:
103                         return []
104                     ids2 = table.search(cr, uid, [(parent, 'in', ids)], context=context)
105                     return ids + rg(ids2, table, parent)
106                 return [(left, 'in', rg(ids, table, parent or table._parent_name))]
107
108         self.__main_table = table
109         self.__all_tables.add(table)
110
111         i = -1
112         while i + 1<len(self.__exp):
113             i += 1
114             e = self.__exp[i]
115             if self._is_operator(e) or e == self.__DUMMY_LEAF:
116                 continue
117             left, operator, right = e
118             operator = operator.lower()
119             working_table = table
120             main_table = table
121             fargs = left.split('.', 1)
122             if fargs[0] in table._inherit_fields:
123                 while True:
124                     field = main_table._columns.get(fargs[0], False)
125                     if field:
126                         working_table = main_table
127                         self.__field_tables[i] = working_table
128                         break
129                     working_table = main_table.pool.get(main_table._inherit_fields[fargs[0]][0])
130                     if working_table not in self.__all_tables:
131                         self.__joins.append('%s.%s=%s.%s' % (working_table._table, 'id', main_table._table, main_table._inherits[working_table._name]))
132                         self.__all_tables.add(working_table)
133                     main_table = working_table
134
135             field = working_table._columns.get(fargs[0], False)
136             if not field:
137                 if left == 'id' and operator == 'child_of':
138                     dom = _rec_get(right, working_table)
139                     self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
140                 continue
141
142             field_obj = table.pool.get(field._obj)
143             if len(fargs) > 1:
144                 if field._type == 'many2one':
145                     right = field_obj.search(cr, uid, [(fargs[1], operator, right)], context=context)
146                     if right == []:
147                         self.__exp[i] = ( 'id', '=', 0 )
148                     else:
149                         self.__exp[i] = (fargs[0], 'in', right)
150                 # Making search easier when there is a left operand as field.o2m or field.m2m
151                 if field._type in ['many2many','one2many']:
152                     right = field_obj.search(cr, uid, [(fargs[1], operator, right)], context=context)
153                     right1 = table.search(cr, uid, [(fargs[0],'in', right)], context=context)
154                     if right1 == []:
155                         self.__exp[i] = ( 'id', '=', 0 )
156                     else:
157                         self.__exp[i] = ('id', 'in', right1)
158
159                 if not isinstance(field,fields.property):
160                     continue
161
162             if field._properties and not field.store:
163                 # this is a function field that is not stored
164                 if not field._fnct_search:
165                     # the function field doesn't provide a search function and doesn't store
166                     # values in the database, so we must ignore it : we generate a dummy leaf
167                     self.__exp[i] = self.__DUMMY_LEAF
168                 else:
169                     subexp = field.search(cr, uid, table, left, [self.__exp[i]], context=context)
170                     if not subexp:
171                         self.__exp[i] = self.__DUMMY_LEAF
172                     else:
173                         # we assume that the expression is valid
174                         # we create a dummy leaf for forcing the parsing of the resulting expression
175                         self.__exp[i] = '&'
176                         self.__exp.insert(i + 1, self.__DUMMY_LEAF)
177                         for j, se in enumerate(subexp):
178                             self.__exp.insert(i + 2 + j, se)
179             # else, the value of the field is store in the database, so we search on it
180
181             elif field._type == 'one2many':
182                 # Applying recursivity on field(one2many)
183                 if operator == 'child_of':
184                     if isinstance(right, basestring):
185                         ids2 = [x[0] for x in field_obj.name_search(cr, uid, right, [], 'like', context=context, limit=None)]
186                     else:
187                         ids2 = list(right)
188                     if field._obj != working_table._name:
189                         dom = _rec_get(ids2, field_obj, left=left, prefix=field._obj)
190                     else:
191                         dom = _rec_get(ids2, working_table, parent=left)
192                     self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
193
194                 else:
195                     call_null = True
196
197                     if right is not False:
198                         if isinstance(right, basestring):
199                             ids2 = [x[0] for x in field_obj.name_search(cr, uid, right, [], operator, context=context, limit=None)]
200                             if ids2:
201                                 operator = 'in'
202                         else:
203                             if not isinstance(right,list):
204                                 ids2 = [right]
205                             else:
206                                 ids2 = right
207                         if not ids2:
208                             if operator in ['like','ilike','in','=']:
209                                 #no result found with given search criteria
210                                 call_null = False
211                                 self.__exp[i] = ('id','=',0)
212                             else:
213                                 call_null = True
214                                 operator = 'in' # operator changed because ids are directly related to main object
215                         else:
216                             call_null = False
217                             o2m_op = 'in'
218                             if operator in  ['not like','not ilike','not in','<>','!=']:
219                                 o2m_op = 'not in'
220                             self.__exp[i] = ('id', o2m_op, self.__execute_recursive_in(cr, field._fields_id, field_obj._table, 'id', ids2, operator, field._type))
221
222                     if call_null:
223                         o2m_op = 'not in'
224                         if operator in  ['not like','not ilike','not in','<>','!=']:
225                             o2m_op = 'in'
226                         self.__exp[i] = ('id', o2m_op, self.__execute_recursive_in(cr, field._fields_id, field_obj._table, 'id', [], operator, field._type) or [0])
227
228             elif field._type == 'many2many':
229                 #FIXME
230                 if operator == 'child_of':
231                     if isinstance(right, basestring):
232                         ids2 = [x[0] for x in field_obj.name_search(cr, uid, right, [], 'like', context=context, limit=None)]
233                     else:
234                         ids2 = list(right)
235
236                     def _rec_convert(ids):
237                         if field_obj == table:
238                             return ids
239                         return self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, ids, operator, field._type)
240
241                     dom = _rec_get(ids2, field_obj)
242                     ids2 = field_obj.search(cr, uid, dom, context=context)
243                     self.__exp[i] = ('id', 'in', _rec_convert(ids2))
244                 else:
245                     call_null_m2m = True
246                     if right is not False:
247                         if isinstance(right, basestring):
248                             res_ids = [x[0] for x in field_obj.name_search(cr, uid, right, [], operator, context=context)]
249                             if res_ids:
250                                 operator = 'in'
251                         else:
252                             if not isinstance(right, list):
253                                 res_ids = [right]
254                             else:
255                                 res_ids = right
256                         if not res_ids:
257                             if operator in ['like','ilike','in','=']:
258                                 #no result found with given search criteria
259                                 call_null_m2m = False
260                                 self.__exp[i] = ('id','=',0)
261                             else:
262                                 call_null_m2m = True
263                                 operator = 'in' # operator changed because ids are directly related to main object
264                         else:
265                             call_null_m2m = False
266                             m2m_op = 'in'
267                             if operator in  ['not like','not ilike','not in','<>','!=']:
268                                 m2m_op = 'not in'
269
270                             self.__exp[i] = ('id', m2m_op, self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, res_ids, operator, field._type) or [0])
271                     if call_null_m2m:
272                         m2m_op = 'not in'
273                         if operator in  ['not like','not ilike','not in','<>','!=']:
274                             m2m_op = 'in'
275                         self.__exp[i] = ('id', m2m_op, self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, [], operator,  field._type) or [0])
276
277             elif field._type == 'many2one':
278                 if operator == 'child_of':
279                     if isinstance(right, basestring):
280                         ids2 = [x[0] for x in field_obj.name_search(cr, uid, right, [], 'like', limit=None)]
281                     elif isinstance(right, (int, long)):
282                         ids2 = list([right])
283                     else:
284                         ids2 = list(right)
285
286                     self.__operator = 'in'
287                     if field._obj != working_table._name:
288                         dom = _rec_get(ids2, field_obj, left=left, prefix=field._obj)
289                     else:
290                         dom = _rec_get(ids2, working_table, parent=left)
291                     self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
292                 else:
293                     def _get_expression(field_obj,cr, uid, left, right, operator, context=None):
294                         if context is None:
295                             context = {}                        
296                         c = context.copy()
297                         c['active_test'] = False
298                         #Special treatment to ill-formed domains
299                         operator = ( operator in ['<','>','<=','>='] ) and 'in' or operator
300                         
301                         dict_op = {'not in':'!=','in':'=','=':'in','!=':'not in','<>':'not in'}
302                         if isinstance(right,tuple):
303                             right = list(right)
304                         if (not isinstance(right,list)) and operator in ['not in','in']:
305                             operator = dict_op[operator]
306                         elif isinstance(right,list) and operator in ['<>','!=','=']: #for domain (FIELD,'=',['value1','value2'])
307                             operator = dict_op[operator]
308                         res_ids = field_obj.name_search(cr, uid, right, [], operator, limit=None, context=c)
309                         if not res_ids:
310                            return ('id','=',0)
311                         else:
312                             right = map(lambda x: x[0], res_ids)
313                             return (left, 'in', right)
314
315                     m2o_str = False
316                     if right:
317                         if isinstance(right, basestring): # and not isinstance(field, fields.related):
318                             m2o_str = True
319                         elif isinstance(right,(list,tuple)):
320                             m2o_str = True
321                             for ele in right:
322                                 if not isinstance(ele, basestring): 
323                                     m2o_str = False
324                                     break
325                     elif right == []:
326                         m2o_str = False
327                         if operator in ('not in', '!=', '<>'):
328                             # (many2one not in []) should return all records
329                             self.__exp[i] = self.__DUMMY_LEAF
330                         else:
331                             self.__exp[i] = ('id','=',0)
332                     else:
333                         new_op = '='
334                         if operator in  ['not like','not ilike','not in','<>','!=']:
335                             new_op = '!='
336                         #Is it ok to put 'left' and not 'id' ?
337                         self.__exp[i] = (left,new_op,False)
338                         
339                     if m2o_str:
340                         self.__exp[i] = _get_expression(field_obj,cr, uid, left, right, operator, context=context)
341             else:
342                 # other field type
343                 # add the time part to datetime field when it's not there:
344                 if field._type == 'datetime' and self.__exp[i][2] and len(self.__exp[i][2]) == 10:
345
346                     self.__exp[i] = list(self.__exp[i])
347
348                     if operator in ('>', '>='):
349                         self.__exp[i][2] += ' 00:00:00'
350                     elif operator in ('<', '<='):
351                         self.__exp[i][2] += ' 23:59:59'
352
353                     self.__exp[i] = tuple(self.__exp[i])
354
355                 if field.translate:
356                     if operator in ('like', 'ilike', 'not like', 'not ilike'):
357                         right = '%%%s%%' % right
358
359                     operator = operator == '=like' and 'like' or operator
360
361                     new_op = 'inselect'
362                     if operator in ['not like', 'not ilike', 'not in', '<>', '!=']:
363                         new_op = 'not inselect'
364                         operator = {'not like': 'like', 'not ilike': 'ilike', 'not in': 'in', '<>': '=', '!=': '='}[operator]
365
366                     query1 = '( SELECT res_id'          \
367                              '    FROM ir_translation'  \
368                              '   WHERE name = %s'       \
369                              '     AND lang = %s'       \
370                              '     AND type = %s'
371                     instr = ' %s'
372                     #Covering in,not in operators with operands (%s,%s) ,etc.
373                     if operator == 'in':
374                         instr = ','.join(['%s'] * len(right))
375                         query1 += '     AND value ' + operator +  ' ' +" (" + instr + ")"   \
376                              ') UNION ('                \
377                              '  SELECT id'              \
378                              '    FROM "' + working_table._table + '"'       \
379                              '   WHERE "' + left + '" ' + operator + ' ' +" (" + instr + "))"
380                     else:
381                         query1 += '     AND value ' + operator + instr +   \
382                              ') UNION ('                \
383                              '  SELECT id'              \
384                              '    FROM "' + working_table._table + '"'       \
385                              '   WHERE "' + left + '" ' + operator + instr + ")"
386
387                     query2 = [working_table._name + ',' + left,
388                               context.get('lang', False) or 'en_US',
389                               'model',
390                               right,
391                               right,
392                              ]
393
394                     self.__exp[i] = ('id', new_op, (query1, query2))
395
396         return self
397
398     def __leaf_to_sql(self, leaf, table):
399         if leaf == self.__DUMMY_LEAF:
400             return ('(1=1)', [])
401         left, operator, right = leaf
402
403         if operator == 'inselect':
404             query = '(%s.%s in (%s))' % (table._table, left, right[0])
405             params = right[1]
406         elif operator == 'not inselect':
407             query = '(%s.%s not in (%s))' % (table._table, left, right[0])
408             params = right[1]
409         elif operator in ['in', 'not in']:
410             params = right and right[:] or []
411             len_before = len(params)
412             for i in range(len_before)[::-1]:
413                 if params[i] == False:
414                     del params[i]
415
416             len_after = len(params)
417             check_nulls = len_after != len_before
418             query = '(1=0)'
419
420             if len_after:
421                 if left == 'id':
422                     instr = ','.join(['%s'] * len_after)
423                 else:
424                     instr = ','.join([table._columns[left]._symbol_set[0]] * len_after)
425                 query = '(%s.%s %s (%s))' % (table._table, left, operator, instr)
426             else:
427                 # the case for [field, 'in', []] or [left, 'not in', []]
428                 if operator == 'in':
429                     query = '(%s.%s IS NULL)' % (table._table, left)
430                 else:
431                     query = '(%s.%s IS NOT NULL)' % (table._table, left)
432             if check_nulls:
433                 query = '(%s OR %s.%s IS NULL)' % (query, table._table, left)
434         else:
435             params = []
436
437             if right == False and (leaf[0] in table._columns)  and table._columns[leaf[0]]._type=="boolean"  and (operator == '='):
438                 query = '(%s.%s IS NULL or %s.%s = false )' % (table._table, left,table._table, left)
439             elif (((right == False) and (type(right)==bool)) or (right is None)) and (operator == '='):
440                 query = '%s.%s IS NULL ' % (table._table, left)
441             elif right == False and (leaf[0] in table._columns)  and table._columns[leaf[0]]._type=="boolean"  and (operator in ['<>', '!=']):
442                 query = '(%s.%s IS NOT NULL and %s.%s != false)' % (table._table, left,table._table, left)
443             elif (((right == False) and (type(right)==bool)) or right is None) and (operator in ['<>', '!=']):
444                 query = '%s.%s IS NOT NULL' % (table._table, left)
445             elif (operator == '=?'):
446                 op = '='
447                 if (right is False or right is None):
448                     return ( 'TRUE',[])
449                 if left in table._columns:
450                         format = table._columns[left]._symbol_set[0]
451                         query = '(%s.%s %s %s)' % (table._table, left, op, format)
452                         params = table._columns[left]._symbol_set[1](right)
453                 else:
454                         query = "(%s.%s %s '%%s')" % (table._table, left, op)
455                         params = right
456
457             else:
458                 if left == 'id':
459                     query = '%s.id %s %%s' % (table._table, operator)
460                     params = right
461                 else:
462                     like = operator in ('like', 'ilike', 'not like', 'not ilike')
463
464                     op = {'=like':'like','=ilike':'ilike'}.get(operator,operator)
465                     if left in table._columns:
466                         format = like and '%s' or table._columns[left]._symbol_set[0]
467                         query = '(%s.%s %s %s)' % (table._table, left, op, format)
468                     else:
469                         query = "(%s.%s %s '%s')" % (table._table, left, op, right)
470
471                     add_null = False
472                     if like:
473                         if isinstance(right, str):
474                             str_utf8 = right
475                         elif isinstance(right, unicode):
476                             str_utf8 = right.encode('utf-8')
477                         else:
478                             str_utf8 = str(right)
479                         params = '%%%s%%' % str_utf8
480                         add_null = not str_utf8
481                     elif left in table._columns:
482                         params = table._columns[left]._symbol_set[1](right)
483
484                     if add_null:
485                         query = '(%s OR %s IS NULL)' % (query, left)
486
487         if isinstance(params, basestring):
488             params = [params]
489         return (query, params)
490
491
492     def to_sql(self):
493         stack = []
494         params = []
495         for i, e in reverse_enumerate(self.__exp):
496             if self._is_leaf(e, internal=True):
497                 table = self.__field_tables.get(i, self.__main_table)
498                 q, p = self.__leaf_to_sql(e, table)
499                 params.insert(0, p)
500                 stack.append(q)
501             else:
502                 if e == '!':
503                     stack.append('(NOT (%s))' % (stack.pop(),))
504                 else:
505                     ops = {'&': ' AND ', '|': ' OR '}
506                     q1 = stack.pop()
507                     q2 = stack.pop()
508                     stack.append('(%s %s %s)' % (q1, ops[e], q2,))
509
510         query = ' AND '.join(reversed(stack))
511         joins = ' AND '.join(self.__joins)
512         if joins:
513             query = '(%s) AND (%s)' % (joins, query)
514         return (query, flatten(params))
515
516     def get_tables(self):
517         return ['"%s"' % t._table for t in self.__all_tables]
518
519 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
520