From b27a71c0cbdfab8a2a503dbad76b354367213198 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 15 Nov 2023 12:21:26 +0700 Subject: [PATCH 1/6] Fix `Query::count()` --- src/Query/Query.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index 98e69b962..4525f0189 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -293,10 +293,10 @@ public function column(): array public function count(string $sql = '*'): int|string { - return match ($this->emulateExecution) { - true => 0, - false => is_numeric($count = $this->queryScalar("COUNT($sql)")) ? (int) $count : 0, - }; + /** @var int|string|null $count */ + $count = $this->queryScalar("COUNT($sql)"); + /** @var int|string */ + return $count <= PHP_INT_MAX ? (int) $count : $count; } public function createCommand(): CommandInterface From 1d82491337c724bdfbb22e94e8911ad45a7ce49b Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 15 Nov 2023 12:35:15 +0700 Subject: [PATCH 2/6] Obviously check if `$count` is `null` --- src/Query/Query.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index 4525f0189..8aa9cc517 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -295,7 +295,11 @@ public function count(string $sql = '*'): int|string { /** @var int|string|null $count */ $count = $this->queryScalar("COUNT($sql)"); - /** @var int|string */ + + if ($count === null) { + return 0; + } + return $count <= PHP_INT_MAX ? (int) $count : $count; } From b36c1c9cef13aebfa1b51358e8e139eded366a8d Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 15 Nov 2023 19:56:25 +0700 Subject: [PATCH 3/6] Update db version for tests --- .github/workflows/active-record.yml | 2 +- .github/workflows/db-mariadb.yml | 2 +- .github/workflows/db-mssql.yml | 2 +- .github/workflows/db-mysql.yml | 2 +- .github/workflows/db-oracle.yml | 2 +- .github/workflows/db-pgsql.yml | 2 +- .github/workflows/db-sqlite.yml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/active-record.yml b/.github/workflows/active-record.yml index 6f7b9cb78..a894fad8a 100644 --- a/.github/workflows/active-record.yml +++ b/.github/workflows/active-record.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-active-record-${{ matrix.os }} env: - COMPOSER_ROOT_VERSION: 1.1.0 + COMPOSER_ROOT_VERSION: 1.2.0 EXTENSIONS: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlite, pdo_sqlsrv-5.10.1 runs-on: ${{ matrix.os }} diff --git a/.github/workflows/db-mariadb.yml b/.github/workflows/db-mariadb.yml index 1ecc6bc67..7f69f4074 100644 --- a/.github/workflows/db-mariadb.yml +++ b/.github/workflows/db-mariadb.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-mariadb-${{ matrix.mariadb }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-mysql EXTENSIONS: pdo, pdo_mysql diff --git a/.github/workflows/db-mssql.yml b/.github/workflows/db-mssql.yml index a3000c876..8fab1e591 100644 --- a/.github/workflows/db-mssql.yml +++ b/.github/workflows/db-mssql.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-mssql-${{ matrix.mssql }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-mssql EXTENSIONS: pdo, pdo_sqlsrv-5.10.1 diff --git a/.github/workflows/db-mysql.yml b/.github/workflows/db-mysql.yml index 6e6b51baa..a56b3f2fb 100644 --- a/.github/workflows/db-mysql.yml +++ b/.github/workflows/db-mysql.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-mysql-${{ matrix.mysql }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-mysql EXTENSIONS: pdo, pdo_mysql diff --git a/.github/workflows/db-oracle.yml b/.github/workflows/db-oracle.yml index 75cdc548e..a37592db0 100644 --- a/.github/workflows/db-oracle.yml +++ b/.github/workflows/db-oracle.yml @@ -27,7 +27,7 @@ jobs: env: CURRENT_PACKAGE: db-oracle - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 EXTENSIONS: pdo, pdo_oci runs-on: ${{ matrix.os }} diff --git a/.github/workflows/db-pgsql.yml b/.github/workflows/db-pgsql.yml index d2db8a700..0a809bdda 100644 --- a/.github/workflows/db-pgsql.yml +++ b/.github/workflows/db-pgsql.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-pgsql-${{ matrix.pgsql }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-pgsql EXTENSIONS: pdo, pdo_pgsql diff --git a/.github/workflows/db-sqlite.yml b/.github/workflows/db-sqlite.yml index cf7c014a6..230ee0989 100644 --- a/.github/workflows/db-sqlite.yml +++ b/.github/workflows/db-sqlite.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-sqlite-${{ matrix.os }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-sqlite EXTENSIONS: pdo, pdo_sqlite From b74335ae86862aebe8a58658b1ec955ac5b3c37f Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 16 Nov 2023 09:10:47 +0700 Subject: [PATCH 4/6] Add line to CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 293e1f8df..06c5c30dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.2.1 under development -- no changes in this release. +- Bug #777: Fix `Query::count()` (@Tigrov) ## 1.2.0 November 12, 2023 From fcb5734fc8f44eac50fdd75006efe1e0383912b2 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 16 Nov 2023 09:17:41 +0300 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c5c30dc..44d5f549f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 1.2.1 under development -- Bug #777: Fix `Query::count()` (@Tigrov) +- Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater + than `PHP_INT_MAX` (@Tigrov) ## 1.2.0 November 12, 2023 From 17c924b3ea624d62e9d65fed38608a4f912a7705 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 16 Nov 2023 14:41:10 +0700 Subject: [PATCH 6/6] Add test for count greater than `PHP_INT_MAX` --- tests/AbstractQueryTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/AbstractQueryTest.php b/tests/AbstractQueryTest.php index 63334ea9a..754cab5a3 100644 --- a/tests/AbstractQueryTest.php +++ b/tests/AbstractQueryTest.php @@ -787,4 +787,18 @@ public function testNormalizeSelect(array|string|Expression $columns, array|stri $query->select($columns); $this->assertEquals($expected, $query->getSelect()); } + + public function testCountGreaterThanPhpIntMax(): void + { + $query = $this->getMockBuilder(Query::class) + ->setConstructorArgs([$this->getConnection()]) + ->onlyMethods(['queryScalar']) + ->getMock(); + + $query->expects($this->once()) + ->method('queryScalar') + ->willReturn('12345678901234567890'); + + $this->assertSame('12345678901234567890', $query->count()); + } }