Skip to content

Commit

Permalink
perf: improve DBAL QueryBuilderAdapter count performance (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jul 28, 2024
1 parent 0697652 commit 2dd2050
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* feat: DBAL adapter
* fix: ORM `QueryBuilderAdapter` logic
* perf: improve DBAL `QueryBuilderAdapter` count performance

# 0.16.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,22 @@ public function countKeysetItems(

$queryBuilder = $this->getQueryBuilder($offset, $limit, $boundaryValues, $boundaryType);

$result = $queryBuilder->executeQuery()->fetchAllAssociative();
$queryBuilder->select('COUNT(*)');

/** @psalm-suppress MixedAssignment */
$result = $queryBuilder->executeQuery()->fetchOne();

if (!is_numeric($result)) {
throw new UnexpectedValueException('Count must be a number.');
}

$count = (int) $result;

if ($count < 0) {
throw new UnexpectedValueException('Count must be greater than or equal to 0.');
}

return \count($result);
return $count;
}

/**
Expand Down Expand Up @@ -406,12 +419,21 @@ public function countOffsetItems(int $offset = 0, ?int $limit = null): int

$queryBuilder = $this->getQueryBuilder($offset, $limit, null, BoundaryType::Lower);

$result = $queryBuilder->executeQuery()->rowCount();
$queryBuilder->select('COUNT(*)');

/** @psalm-suppress MixedAssignment */
$result = $queryBuilder->executeQuery()->fetchOne();

if (!is_numeric($result)) {
throw new UnexpectedValueException('Count must be a number.');
}

$count = (int) $result;

if ($result < 0) {
if ($count < 0) {
throw new UnexpectedValueException('Count must be greater than or equal to 0.');
}

return $result;
return $count;
}
}

0 comments on commit 2dd2050

Please sign in to comment.