From dad082f9991b97ad75f54e84689c074e2a578247 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 26 Feb 2010 02:03:45 +0100 Subject: [PATCH] [FIX] attempt at supporting inherited fields (through _inherits) as group_by in read_group() bzr revid: odo@openerp.com-20100226010345-mg1700e0btgf852a --- bin/osv/orm.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 63235e158d4..47112ddff7d 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -1836,18 +1836,22 @@ class orm(orm_template): self.pool.get('ir.model.access').check(cr, uid, self._name, 'read', context=context) if not fields: fields = self._columns.keys() - - (qu1, qu2, tables) = self._where_calc(cr, uid, domain, context=context) + + (where_clause, where_params, tables) = self._where_calc(cr, uid, domain, context=context) dom = self.pool.get('ir.rule').domain_get(cr, uid, self._name, context=context) - qu1 = qu1 + dom[0] - qu2 = qu2 + dom[1] + where_clause = where_clause + dom[0] + where_params = where_params + dom[1] for t in dom[2]: if t not in tables: tables.append(t) - if len(qu1): - qu1 = ' where '+string.join(qu1, ' and ') + + # Take care of adding join(s) if groupby is an '_inherits'ed field + tables, where_clause = self._inherits_join_calc(groupby,tables,where_clause) + + if len(where_clause): + where_clause = ' where '+string.join(where_clause, ' and ') else: - qu1 = '' + where_clause = '' limit_str = limit and ' limit %d' % limit or '' offset_str = offset and ' offset %d' % offset or '' @@ -1872,7 +1876,8 @@ class orm(orm_template): flist += ',avg('+f+') as '+f else: flist += ',sum('+f+') as '+f - cr.execute('select min(id) as id,'+flist+' from ' + self._table +qu1+' group by '+ groupby + limit_str + offset_str,qu2) + + cr.execute('select min(%s.id) as id,' % self._table + flist + ' from ' + ','.join(tables) + where_clause + ' group by '+ groupby + limit_str + offset_str, where_params) alldata = {} groupby = group_by for r in cr.dictfetchall(): @@ -1903,6 +1908,22 @@ class orm(orm_template): del d['id'] return data + def _inherits_join_calc(self, field, tables, where_clause): + """ Adds missing table select and join clause(s) for reaching + the field coming from an '_inherits' parent table. + @param tables: list of table._table names enclosed in double quotes as returned + by _where_calc() + """ + current_table = self + while field in current_table._inherit_fields and not field in current_table._columns: + parent_table = self.pool.get(current_table._inherit_fields[field][0]) + parent_table_name = parent_table._table + if '"%s"'%parent_table_name not in tables: + tables.append('"%s"'%parent_table_name) + where_clause.append('(%s.%s = %s.id)' % (current_table._table, current_table._inherits[parent_table._name], parent_table_name)) + current_table = parent_table + return (tables, where_clause) + def _parent_store_compute(self, cr): logger = netsvc.Logger() logger.notifyChannel('orm', netsvc.LOG_INFO, 'Computing parent left and right for table %s...' % (self._table, ))