Improve ModelStorage.search_count by using no order and COUNT(*)

issue5877
review27651003
This commit is contained in:
Cédric Krier committed 2016-09-27 12:18:16 +02:00
1 parent 892e79da1e
commit 6261fa486f
3 files changed
+15 -2

No files matched your search

+1 -1
View File
@@ -1136,7 +1136,7 @@ class ModelSQL(ModelStorage):
table = convert_from(None, tables)
if count:
cursor.execute(*table.select(Count(Literal(1)),
cursor.execute(*table.select(Count(Literal('*')),
where=expression, limit=limit, offset=offset))
return cursor.fetchone()[0]
# execute the "main" query to fetch the ids we were searching for
+1 -1
View File
@@ -366,7 +366,7 @@ class ModelStorage(Model):
'''
Return the number of records that match the domain.
'''
res = cls.search(domain, count=True)
res = cls.search(domain, order=[], count=True)
if isinstance(res, list):
return len(res)
return res
+13
View File
@@ -36,6 +36,19 @@ class ModelStorageTestCase(unittest.TestCase):
self.assertTrue(
all(x['name'] >= y['name'] for x, y in zip(rows, rows[1:])))
@with_transaction()
def test_search_count(self):
"Test search_count"
pool = Pool()
ModelStorage = pool.get('test.modelstorage')
ModelStorage.create([{'name': 'Test %s' % i} for i in range(10)])
count = ModelStorage.search_count([])
self.assertEqual(count, 10)
count = ModelStorage.search_count([('name', '=', 'Test 5')])
self.assertEqual(count, 1)
@with_transaction()
def test_browse_context(self):
'Test context when browsing'