From 6845a40a4eada08cb9300294b60ce9c08f7d94d9 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Thu, 16 Nov 2023 17:42:43 +0700 Subject: [PATCH] Fix query count (#777) --- .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 +- CHANGELOG.md | 3 ++- src/Query/Query.php | 12 ++++++++---- tests/AbstractQueryTest.php | 14 ++++++++++++++ 10 files changed, 31 insertions(+), 12 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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 293e1f8df..44d5f549f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 1.2.1 under development -- no changes in this release. +- 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 diff --git a/src/Query/Query.php b/src/Query/Query.php index 98e69b962..8aa9cc517 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -293,10 +293,14 @@ 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)"); + + if ($count === null) { + return 0; + } + + return $count <= PHP_INT_MAX ? (int) $count : $count; } public function createCommand(): CommandInterface 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()); + } }