Skip to content

Commit 6845a40

Browse files
authored
Fix query count (#777)
1 parent 4629a8c commit 6845a40

10 files changed

+31
-12
lines changed

.github/workflows/active-record.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
name: PHP ${{ matrix.php }}-active-record-${{ matrix.os }}
2727

2828
env:
29-
COMPOSER_ROOT_VERSION: 1.1.0
29+
COMPOSER_ROOT_VERSION: 1.2.0
3030
EXTENSIONS: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlite, pdo_sqlsrv-5.10.1
3131

3232
runs-on: ${{ matrix.os }}

.github/workflows/db-mariadb.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
name: PHP ${{ matrix.php }}-mariadb-${{ matrix.mariadb }}
2727

2828
env:
29-
COMPOSER_ROOT_VERSION: 1.0.0
29+
COMPOSER_ROOT_VERSION: 1.2.0
3030
CURRENT_PACKAGE: db-mysql
3131
EXTENSIONS: pdo, pdo_mysql
3232

.github/workflows/db-mssql.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
name: PHP ${{ matrix.php }}-mssql-${{ matrix.mssql }}
2727

2828
env:
29-
COMPOSER_ROOT_VERSION: 1.0.0
29+
COMPOSER_ROOT_VERSION: 1.2.0
3030
CURRENT_PACKAGE: db-mssql
3131
EXTENSIONS: pdo, pdo_sqlsrv-5.10.1
3232

.github/workflows/db-mysql.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
name: PHP ${{ matrix.php }}-mysql-${{ matrix.mysql }}
2727

2828
env:
29-
COMPOSER_ROOT_VERSION: 1.0.0
29+
COMPOSER_ROOT_VERSION: 1.2.0
3030
CURRENT_PACKAGE: db-mysql
3131
EXTENSIONS: pdo, pdo_mysql
3232

.github/workflows/db-oracle.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727

2828
env:
2929
CURRENT_PACKAGE: db-oracle
30-
COMPOSER_ROOT_VERSION: 1.0.0
30+
COMPOSER_ROOT_VERSION: 1.2.0
3131
EXTENSIONS: pdo, pdo_oci
3232

3333
runs-on: ${{ matrix.os }}

.github/workflows/db-pgsql.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
name: PHP ${{ matrix.php }}-pgsql-${{ matrix.pgsql }}
2727

2828
env:
29-
COMPOSER_ROOT_VERSION: 1.0.0
29+
COMPOSER_ROOT_VERSION: 1.2.0
3030
CURRENT_PACKAGE: db-pgsql
3131
EXTENSIONS: pdo, pdo_pgsql
3232

.github/workflows/db-sqlite.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
name: PHP ${{ matrix.php }}-sqlite-${{ matrix.os }}
2727

2828
env:
29-
COMPOSER_ROOT_VERSION: 1.0.0
29+
COMPOSER_ROOT_VERSION: 1.2.0
3030
CURRENT_PACKAGE: db-sqlite
3131
EXTENSIONS: pdo, pdo_sqlite
3232

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 1.2.1 under development
44

5-
- no changes in this release.
5+
- Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater
6+
than `PHP_INT_MAX` (@Tigrov)
67

78
## 1.2.0 November 12, 2023
89

src/Query/Query.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,14 @@ public function column(): array
293293

294294
public function count(string $sql = '*'): int|string
295295
{
296-
return match ($this->emulateExecution) {
297-
true => 0,
298-
false => is_numeric($count = $this->queryScalar("COUNT($sql)")) ? (int) $count : 0,
299-
};
296+
/** @var int|string|null $count */
297+
$count = $this->queryScalar("COUNT($sql)");
298+
299+
if ($count === null) {
300+
return 0;
301+
}
302+
303+
return $count <= PHP_INT_MAX ? (int) $count : $count;
300304
}
301305

302306
public function createCommand(): CommandInterface

tests/AbstractQueryTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -787,4 +787,18 @@ public function testNormalizeSelect(array|string|Expression $columns, array|stri
787787
$query->select($columns);
788788
$this->assertEquals($expected, $query->getSelect());
789789
}
790+
791+
public function testCountGreaterThanPhpIntMax(): void
792+
{
793+
$query = $this->getMockBuilder(Query::class)
794+
->setConstructorArgs([$this->getConnection()])
795+
->onlyMethods(['queryScalar'])
796+
->getMock();
797+
798+
$query->expects($this->once())
799+
->method('queryScalar')
800+
->willReturn('12345678901234567890');
801+
802+
$this->assertSame('12345678901234567890', $query->count());
803+
}
790804
}

0 commit comments

Comments
 (0)