~openerp-dev/openobject-server/saas-3-bug_1291322-ptr

« back to all changes in this revision

Viewing changes to openerp/osv/expression.py

  • Committer: Christophe Simonis
  • Date: 2014-04-04 15:58:58 UTC
  • mfrom: (4743.1.531 unaccentwrapper)
  • Revision ID: chs@openerp.com-20140404155858-mvupqmuvi8ime2tl
[MERGE] forward port of branch 7.0 up to revid 5274 chs@openerp.com-20140404141611-qi1yagltvkd9q8ji

Show diffs side-by-side

added added

removed removed

Lines of Context:
429
429
    cr.execute('SELECT distinct("%s") FROM "%s" where "%s" is not null' % (select_field, from_table, select_field))
430
430
    return [r[0] for r in cr.fetchall()]
431
431
 
 
432
def get_unaccent_wrapper(cr):
 
433
    if openerp.modules.registry.RegistryManager.get(cr.dbname).has_unaccent:
 
434
        return lambda x: "unaccent(%s)" % (x,)
 
435
    return lambda x: x
432
436
 
433
437
# --------------------------------------------------
434
438
# ExtendedLeaf class for managing leafs and contexts
630
634
            :attr list expression: the domain expression, that will be normalized
631
635
                and prepared
632
636
        """
633
 
        self.has_unaccent = openerp.modules.registry.RegistryManager.get(cr.dbname).has_unaccent
 
637
        self._unaccent = get_unaccent_wrapper(cr)
634
638
        self.joins = []
635
639
        self.root_model = table
636
640
 
1019
1023
                    push(create_substitution_leaf(leaf, (left, operator, right), working_model))
1020
1024
 
1021
1025
                elif field.translate and right:
1022
 
                    field = left
1023
1026
                    need_wildcard = operator in ('like', 'ilike', 'not like', 'not ilike')
1024
1027
                    sql_operator = {'=like': 'like', '=ilike': 'ilike'}.get(operator, operator)
1025
1028
                    if need_wildcard:
1031
1034
                        sql_operator = sql_operator[4:] if sql_operator[:3] == 'not' else '='
1032
1035
                        inselect_operator = 'not inselect'
1033
1036
 
1034
 
                    trans_left = 'value'
1035
 
                    left = '"%s"' % (left,)
1036
 
                    instr = '%s'
1037
 
 
1038
 
                    if self.has_unaccent and sql_operator.endswith('like'):
1039
 
                        assert isinstance(right, basestring)
1040
 
                        trans_left = 'unaccent(value)'
1041
 
                        left = 'unaccent(%s)' % (left,)
1042
 
                        instr = 'unaccent(%s)'
1043
 
                    elif sql_operator == 'in':
 
1037
                    unaccent = self._unaccent if sql_operator.endswith('like') else lambda x: x
 
1038
 
 
1039
                    trans_left = unaccent('value')
 
1040
                    quote_left = unaccent(_quote(left))
 
1041
                    instr = unaccent('%s')
 
1042
 
 
1043
                    if sql_operator == 'in':
1044
1044
                        # params will be flatten by to_sql() => expand the placeholders
1045
1045
                        instr = '(%s)' % ', '.join(['%s'] * len(right))
1046
1046
 
1056
1056
                                     WHERE {left} {operator} {right}
1057
1057
                                   )
1058
1058
                                """.format(trans_left=trans_left, operator=sql_operator,
1059
 
                                           right=instr, table=working_model._table, left=left)
 
1059
                                           right=instr, table=working_model._table, left=quote_left)
1060
1060
 
1061
1061
                    params = (
1062
 
                        working_model._name + ',' + field,
 
1062
                        working_model._name + ',' + left,
1063
1063
                        context.get('lang') or 'en_US',
1064
1064
                        'model',
1065
1065
                        right,
1185
1185
 
1186
1186
            if left in model._columns:
1187
1187
                format = need_wildcard and '%s' or model._columns[left]._symbol_set[0]
1188
 
                if self.has_unaccent and sql_operator.endswith('like'):
1189
 
                    query = '(unaccent(%s."%s") %s unaccent(%s))' % (table_alias, left, sql_operator, format)
1190
 
                else:
1191
 
                    query = '(%s."%s" %s %s)' % (table_alias, left, sql_operator, format)
 
1188
                unaccent = self._unaccent if sql_operator.endswith('like') else lambda x: x
 
1189
                column = '%s.%s' % (table_alias, _quote(left))
 
1190
                query = '(%s %s %s)' % (unaccent(column), sql_operator, unaccent(format))
1192
1191
            elif left in MAGIC_COLUMNS:
1193
1192
                    query = "(%s.\"%s\" %s %%s)" % (table_alias, left, sql_operator)
1194
1193
                    params = right