Skip to content

Commit 21dc7a8

Browse files
committed
Do not use parameters for COUNT(*)
Closes #95
1 parent 7f00365 commit 21dc7a8

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Do not use parameters for COUNT(*)
2+
13
Version 1.6.0 - 2025-05-02
24
* Fix position of order_by parameters in Select query
35
* Add support for weak reference on SQL objects

sql/aggregate.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
__all__ = ['Avg', 'BitAnd', 'BitOr', 'BoolAnd', 'BoolOr', 'Count', 'Every',
66
'Max', 'Min', 'Stddev', 'Sum', 'Variance']
7-
_sentinel = object()
87

98

109
class Aggregate(Expression):
@@ -169,20 +168,31 @@ class BoolOr(Aggregate):
169168
_sql = 'BOOL_OR'
170169

171170

171+
class _Star(Expression):
172+
__slots__ = ()
173+
174+
def __str__(self):
175+
return '*'
176+
177+
@property
178+
def params(self):
179+
return ()
180+
181+
172182
class Count(Aggregate):
173183
__slots__ = ()
174184
_sql = 'COUNT'
175185

176-
def __init__(self, expression=_sentinel, **kwargs):
177-
if expression is _sentinel:
178-
expression = Literal('*')
186+
def __init__(self, expression=_Star(), **kwargs):
179187
super().__init__(expression, **kwargs)
180188

181189
@property
182190
def _case_expression(self):
183191
expression = super(Count, self)._case_expression
184-
if (isinstance(self.expression, Literal)
185-
and expression.value == '*'):
192+
if (isinstance(self.expression, _Star)
193+
# Keep testing Literal('*') for backward compatibility
194+
or (isinstance(self.expression, Literal)
195+
and expression.value == '*')):
186196
expression = Literal(1)
187197
return expression
188198

sql/tests/test_aggregate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_avg(self):
4242

4343
def test_count_without_expression(self):
4444
count = Count()
45-
self.assertEqual(str(count), 'COUNT(%s)')
46-
self.assertEqual(count.params, ('*',))
45+
self.assertEqual(str(count), 'COUNT(*)')
46+
self.assertEqual(count.params, ())
4747

4848
def test_order_by_one_column(self):
4949
avg = Avg(self.table.a, order_by=self.table.b)

0 commit comments

Comments
 (0)