[MERGE] search: when count=True, execute main query as a subquery to avoid side effects with offset and limit

Granted, calling search() with both count=True and
offset or limit is not extremely useful, but
it is still better to accept it and apply it
to the inner query rather than have silly
errors

bzr revid: odo@openerp.com-20130220174416-yoos2py8dc9jc3md
This commit is contained in:
Olivier Dony 2013-02-20 18:44:16 +01:00
commit a961e5e377
1 changed files with 8 additions and 4 deletions

View File

@ -4870,12 +4870,16 @@ class BaseModel(object):
limit_str = limit and ' limit %d' % limit or ''
offset_str = offset and ' offset %d' % offset or ''
where_str = where_clause and (" WHERE %s" % where_clause) or ''
query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str
if count:
cr.execute('SELECT count("%s".id) FROM ' % self._table + from_clause + where_str + limit_str + offset_str, where_clause_params)
res = cr.fetchall()
return res[0][0]
cr.execute('SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str, where_clause_params)
# /!\ the main query must be executed as a subquery, otherwise
# offset and limit apply to the result of count()!
cr.execute('SELECT count(*) FROM (%s) AS count' % query_str, where_clause_params)
res = cr.fetchone()
return res[0]
cr.execute(query_str, where_clause_params)
res = cr.fetchall()
# TDE note: with auto_join, we could have several lines about the same result