Skip to content

Commit

Permalink
Fix query count (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 16, 2023
1 parent 4629a8c commit 6845a40
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/active-record.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/db-mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/db-mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/db-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/db-oracle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/db-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/db-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/AbstractQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 6845a40

Please sign in to comment.