move, rename and refactor use of ID_MAX
[odoo/odoo.git] / bin / osv / orm.py
index 9173ecf..0f0ec2f 100644 (file)
@@ -62,9 +62,6 @@ import tools
 
 from tools.config import config
 
-prof = 0
-ID_MAX = 1000
-
 regex_order = re.compile('^([a-zA-Z0-9_]+( desc)?( asc)?,?)+$', re.I)
 
 def intersect(la, lb):
@@ -1664,8 +1661,8 @@ class orm(orm_template):
         res = []
         if len(fields_pre) :
             fields_pre2 = map(lambda x: (x in ('create_date', 'write_date')) and ('date_trunc(\'second\', '+x+') as '+x) or '"'+x+'"', fields_pre)
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
                 if d1:
                     cr.execute('SELECT %s FROM \"%s\" WHERE id IN (%s) AND %s ORDER BY %s' % \
                             (','.join(fields_pre2 + ['id']), self._table,
@@ -1807,8 +1804,8 @@ class orm(orm_template):
 
         delta= context.get('read_delta',False)
         if delta and self._log_access:
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
                 cr.execute("select  (now()  - min(write_date)) <= '%s'::interval " \
                         "from \"%s\" where id in (%s)" %
                         (delta, self._table, ",".join(map(str, sub_ids))) )
@@ -1833,8 +1830,8 @@ class orm(orm_template):
         if d1:
             d1 = ' AND '+d1
 
-        for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-            sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
+        for i in range(0, len(ids), cr.IN_MAX):
+            sub_ids = ids[i:i+cr.IN_MAX]
             str_d = string.join(('%d',)*len(sub_ids),',')
             if d1:
                 cr.execute('SELECT id FROM "'+self._table+'" ' \
@@ -1898,8 +1895,8 @@ class orm(orm_template):
             ids = [ids]
         delta= context.get('read_delta',False)
         if delta and self._log_access:
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
                 cr.execute("select  (now()  - min(write_date)) <= '%s'::interval " \
                         "from %s where id in (%s)" %
                         (delta,self._table, ",".join(map(str, sub_ids))))
@@ -1961,8 +1958,8 @@ class orm(orm_template):
             if d1:
                 d1 = ' and '+d1
 
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
                 ids_str = string.join(map(str, sub_ids),',')
                 if d1:
                     cr.execute('SELECT id FROM "'+self._table+'" ' \
@@ -1998,8 +1995,8 @@ class orm(orm_template):
         for table in self._inherits:
             col = self._inherits[table]
             nids = []
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids = ids[ID_MAX * i:ID_MAX * (i + 1)]
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids = ids[i:i+cr.IN_MAX]
                 ids_str = string.join(map(str, sub_ids),',')
                 cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \
                         'where id in ('+ids_str+')', upd1)
@@ -2188,9 +2185,6 @@ class orm(orm_template):
         # records unless they were explicitely asked for
         if 'active' in self._columns and (active_test and context.get('active_test', True)):
             args = [('&', ('active', '=', 1), tuple(args))]
-            #active_found = reduce( lambda x, y: x or y == 'active', args, False )
-            #if not active_found:
-            #    args.append(('active', '=', 1))
 
         if args:
             import expression
@@ -2202,239 +2196,6 @@ class orm(orm_template):
         else:
             qu1, qu2, tables = [], [], ['"%s"' % self._table]
 
-        print "expression: %r\n%r\n%r\n%r" % (args, qu1, qu2, tables)
-        """
-        tables=['"'+self._table+'"']
-        joins=[]
-        for i, argument in zip(range(len(args)), args):
-            table=self
-            assert argument[1] in ('like','!=','ilike','=like', 'not like', 'not ilike', 'not in','inselect','child_of','in','=','<>','<','>','>=','<='), _('Error ! Bad clause operand "%s".') % (argument[1],)
-            if argument[1] == 'inselect':
-                raise except_orm(_('ValidateError'),
-                        _("The clause 'inselect' can not be used outside the orm!"))
-            if argument[0] in self._inherit_fields:
-                table=self.pool.get(self._inherit_fields[argument[0]][0])
-                if ('"'+table._table+'"' not in tables):
-                    tables.append('"'+table._table+'"')
-                    joins.append(('id', 'join', '%s.%s' % (self._table, self._inherits[table._name]), table))
-            fargs = argument[0].split('.', 1)
-            field = table._columns.get(fargs[0],False)
-            if not field:
-                if argument[0] == 'id' and argument[1] == 'child_of':
-                    ids2 = argument[2]
-                    def _rec_get(ids, table, parent):
-                        if not ids:
-                            return []
-                        ids2 = table.search(cr, user, [(parent, 'in', ids)], context=context)
-                        return ids + _rec_get(ids2, table, parent)
-                    args[i] = (argument[0], 'in', ids2 + _rec_get(ids2, table, table._parent_name), table)
-                continue
-            if len(fargs) > 1:
-                if field._type == 'many2one':
-                    args[i] = (fargs[0], 'in', self.pool.get(field._obj).search(cr, user, [(fargs[1], argument[1], argument[2])], context=context))
-                continue
-            if field._properties: # = fields function
-                arg = [args.pop(i)]
-                j = i
-                while j<len(args):
-                    if args[j][0]==arg[0][0]:
-                        arg.append(args.pop(j))
-                    else:
-                        j+=1
-                if field._fnct_search:
-                    args.extend(field.search(cr, user, self, arg[0][0], arg))
-                if field.store:
-                    args.extend(arg)
-            elif field._type=='one2many':
-                field_obj = self.pool.get(field._obj)
-
-                if isinstance(argument[2], basestring):
-                    # get the ids of the records of the "distant" resource
-                    ids2 = [x[0] for x in field_obj.name_search(cr, user, argument[2], [], argument[1])]
-                else:
-                    ids2 = argument[2]
-                if not ids2:
-                    args[i] = ('id','=','0')
-                else:
-                    ids3 = []
-                    for j in range((len(ids2) / ID_MAX) + (len(ids2) % ID_MAX)):
-                        sub_ids2 = ids2[ID_MAX * j:ID_MAX * (j + 1)]
-                        if sub_ids2:
-                            cr.execute('SELECT "'+field._fields_id+'" ' \
-                                    'FROM "'+field_obj._table+'" ' \
-                                    'WHERE id in ('+','.join(map(str,sub_ids2))+')')
-                            ids3.extend([x[0] for x in cr.fetchall()])
-
-                    args[i] = ('id', 'in', ids3)
-
-            elif field._type=='many2many':
-                #FIXME
-                if argument[1]=='child_of':
-                    if isinstance(argument[2], basestring):
-                        ids2 = [x[0] for x in self.pool.get(field._obj).name_search(cr, user, argument[2], [], 'like')]
-                    else:
-                        ids2 = argument[2]
-                    def _rec_get(ids, table, parent):
-                        if not ids:
-                            return []
-                        ids2 = table.search(cr, user, [(parent, 'in', ids)], context=context)
-                        return ids + _rec_get(ids2, table, parent)
-                    def _rec_convert(ids):
-                        if self.pool.get(field._obj)==self:
-                            return ids
-                        if not len(ids): return []
-                        cr.execute('select "'+field._id1+'" from "'+field._rel+'" where "'+field._id2+'" in ('+','.join(map(str,ids))+')')
-                        ids = [x[0] for x in cr.fetchall()]
-                        return ids
-                    args[i] = ('id','in',_rec_convert(ids2+_rec_get(ids2, self.pool.get(field._obj), table._parent_name)))
-                else:
-                    if isinstance(argument[2], basestring):
-                        res_ids = [x[0] for x in self.pool.get(field._obj).name_search(cr, user, argument[2], [], argument[1])]
-                    else:
-                        res_ids = argument[2]
-                    if not len(res_ids):
-                        args[i] = ('id', 'in', [0])
-                    else:
-                        cr.execute('select "'+field._id1+'" from "'+field._rel+'" where "'+field._id2+'" in ('+','.join(map(str, res_ids))+')')
-                        args[i] = ('id', 'in', map(lambda x: x[0], cr.fetchall()))
-
-            elif field._type=='many2one':
-                if argument[1]=='child_of':
-                    if isinstance(argument[2], basestring):
-                        ids2 = [x[0] for x in self.pool.get(field._obj).name_search(cr, user, argument[2], [], 'like')]
-                    else:
-                        ids2 = argument[2]
-                    def _rec_get(ids, table, parent):
-                        if not ids:
-                            return []
-                        ids2 = table.search(cr, user, [(parent, 'in', ids)], context=context)
-                        return ids + _rec_get(ids2, table, parent)
-                    if field._obj <> table._name:
-                        args[i] = (argument[0],'in',ids2+_rec_get(ids2, self.pool.get(field._obj), table._parent_name), table)
-                    else:
-                        args[i] = ('id','in',ids2+_rec_get(ids2, table, argument[0]), table)
-                else:
-                    if isinstance(argument[2], basestring):
-                        res_ids = self.pool.get(field._obj).name_search(cr, user, argument[2], [], argument[1])
-                        args[i] = (argument[0],'in',map(lambda x: x[0], res_ids), table)
-                    else:
-                        args[i] += (table,)
-            else:
-                if field.translate:
-                    if argument[1] in ('like', 'ilike', 'not like', 'not ilike'):
-                        args[i] = (argument[0], argument[1], '%%%s%%' % argument[2])
-                    query1 = '(SELECT res_id FROM ir_translation ' \
-                            'WHERE name = %s AND lang = %s ' \
-                                'AND type = %s ' \
-                                'AND VALUE ' + argument[1] + ' %s)'
-                    query2 = [table._name + ',' + argument[0],
-                            context.get('lang', False) or 'en_US',
-                            'model',
-                            argument[2]]
-                    query1 += ' UNION '
-                    query1 += '(SELECT id FROM "' + table._table + '" ' \
-                            'WHERE "' + argument[0] + '" ' + argument[1] + ' %s)'
-                    query2 += [argument[2]]
-                    args[i] = ('id', 'inselect', (query1, query2), table)
-                else:
-                    args[i] += (table,)
-        args.extend(joins)
-
-        qu1, qu2 = [], []
-        for x in args:
-            table=self
-            if len(x) > 3:
-                table=x[3]
-            if x[1] == 'inselect':
-                qu1.append('(%s.%s in (%s))' % (table._table, x[0], x[2][0]))
-                qu2 += x[2][1]
-            elif x[1]=='not in':
-                    if len(x[2])>0:
-                        todel = []
-                        for xitem in range(len(x[2])):
-                            if x[2][xitem]==False and isinstance(x[2][xitem],bool):
-                                todel.append(xitem)
-                        for xitem in todel[::-1]:
-                            del x[2][xitem]
-                        if x[0]=='id':
-                            qu1.append('(%s.id not in (%s))' % (table._table, ','.join(['%d'] * len(x[2])),))
-                        else:
-                            qu1.append('(%s.%s not in (%s))' % (table._table, x[0], ','.join([table._columns[x[0]]._symbol_set[0]]*len(x[2]))))
-                        if todel:
-                            qu1[-1] = '('+qu1[-1]+' or '+x[0]+' is null)'
-                        qu2+=x[2]
-                    else:
-                        qu1.append(' (1=0)')
-            elif x[1] != 'in':
-#FIXME: this replace all (..., '=', False) values with 'is null' and this is
-# not what we want for real boolean fields. The problem is, we can't change it
-# easily because we use False everywhere instead of None
-# NOTE FAB: we can't use None because it is not accepted by XML-RPC, that's why
-# boolean (0-1), None -> False
-# Ged> boolean fields are not always = 0 or 1
-                if (x[2] is False) and (x[1]=='='):
-                    qu1.append(x[0]+' is null')
-                elif (x[2] is False) and (x[1]=='<>' or x[1]=='!='):
-                    qu1.append(x[0]+' is not null')
-                else:
-                    if x[0]=='id':
-                        if x[1]=='join':
-                            qu1.append('(%s.%s = %s)' % (table._table, x[0], x[2]))
-                        else:
-                            qu1.append('(%s.%s %s %%s)' % (table._table, x[0], x[1]))
-                            qu2.append(x[2])
-                    else:
-                        add_null = False
-                        if x[1] in ('like', 'ilike', 'not like', 'not ilike'):
-                            if isinstance(x[2], str):
-                                str_utf8 = x[2]
-                            elif isinstance(x[2], unicode):
-                                str_utf8 = x[2].encode('utf-8')
-                            else:
-                                str_utf8 = str(x[2])
-                            qu2.append('%%%s%%' % str_utf8)
-                            if not str_utf8:
-                                add_null = True
-                        else:
-                            if x[0] in table._columns:
-                                qu2.append(table._columns[x[0]]._symbol_set[1](x[2]))
-                        if x[1]=='=like':
-                            x1 = 'like'
-                        else:
-                            x1 = x[1]
-                        if x[0] in table._columns:
-                            if x[1] in ('like', 'ilike', 'not like', 'not ilike'):
-
-                                qu1.append('(%s.%s %s %s)' % (table._table,
-                                    x[0], x1, '%s'))
-                            else:
-                                qu1.append('(%s.%s %s %s)' % (table._table,
-                                    x[0], x1,
-                                    table._columns[x[0]]._symbol_set[0]))
-                        else:
-                            qu1.append('(%s.%s %s \'%s\')' % (table._table, x[0], x1, x[2]))
-
-                        if add_null:
-                            qu1[-1] = '('+qu1[-1]+' or '+x[0]+' is null)'
-            elif x[1]=='in':
-                if len(x[2])>0:
-                    todel = []
-                    for xitem in range(len(x[2])):
-                        if x[2][xitem]==False and isinstance(x[2][xitem],bool):
-                            todel.append(xitem)
-                    for xitem in todel[::-1]:
-                        del x[2][xitem]
-                    #TODO fix max_stack_depth
-                    if x[0]=='id':
-                        qu1.append('(%s.id in (%s))' % (table._table, ','.join(['%d'] * len(x[2])),))
-                    else:
-                        qu1.append('(%s.%s in (%s))' % (table._table, x[0], ','.join([table._columns[x[0]]._symbol_set[0]]*len(x[2]))))
-                    if todel:
-                        qu1[-1] = '('+qu1[-1]+' or '+x[0]+' is null)'
-                    qu2+=x[2]
-                else:
-                    qu1.append(' (1=0)')
-"""
         return (qu1,qu2,tables)
 
     def _check_qorder(self, word):
@@ -2599,8 +2360,8 @@ class orm(orm_template):
         ids_parent = ids[:]
         while len(ids_parent):
             ids_parent2 = []
-            for i in range((len(ids) / ID_MAX) + ((len(ids) % ID_MAX) and 1 or 0)):
-                sub_ids_parent = ids_parent[ID_MAX * i:ID_MAX * (i + 1)]
+            for i in range(0, len(ids), cr.IN_MAX):
+                sub_ids_parent = ids_parent[i:i+cr.IN_MAX]
                 cr.execute('SELECT distinct "'+parent+'"'+
                     ' FROM "'+self._table+'" ' \
                     'WHERE id in ('+','.join(map(str, sub_ids_parent))+')')