From 275a14ab05cc77a7670cb89faf79dcc5d2803a43 Mon Sep 17 00:00:00 2001 From: Alex Standiford Date: Tue, 26 May 2026 13:27:14 -0400 Subject: [PATCH] fix(query-builder): default alias for count('*') is no longer invalid SQL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `count('*')` without an explicit alias produced `as *_count`, which isn't a valid SQL identifier: SELECT COUNT(*) as *_count FROM users AS u ^^^^^^^^ syntax error near "*" Default to the bare identifier `count` when the field is `*`; existing behavior for non-star fields is preserved. Caught while porting the QueryBuilder to phpnomad/sqlite-integration — the SQLite port carries the same fix. --- lib/Builders/QueryBuilder.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/Builders/QueryBuilder.php b/lib/Builders/QueryBuilder.php index f11d24a..ce24c0e 100644 --- a/lib/Builders/QueryBuilder.php +++ b/lib/Builders/QueryBuilder.php @@ -166,7 +166,12 @@ public function sum(string $fieldToSum, ?string $alias = null) /** @inheritDoc */ public function count(string $fieldToCount, ?string $alias = null) { - $alias = $alias ?: $fieldToCount . '_count'; + // Default alias is "_count", with a special case for `*` — + // `*_count` isn't a valid SQL identifier and produces a syntax error + // when the query is executed. + if ($alias === null) { + $alias = $fieldToCount === '*' ? 'count' : $fieldToCount . '_count'; + } if($fieldToCount !== '*'){ $fieldToCount = $this->prependField($fieldToCount);