Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Add having and groupBy clauses to pagination count (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlakis authored Nov 16, 2020
1 parent 37da806 commit 5b47fb0
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions src/Repository/AbstractPaginatorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,44 @@ public function __construct(NativeSqlHelper $nativeSqlHelper, ManagerRegistry $r
}

/**
* @param string $countSQL
* @param Filter $filters
* @param string $countSQL
* @param Filter $filters
* @param string|null $groupBy
* @param string|null $having
* @param bool $useCountWrapper
*
* @return int
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function getPaginationCount(string $countSQL, Filter $filters): int
{
protected function getPaginationCount(
string $countSQL,
Filter $filters,
?string $groupBy = null,
?string $having = null,
bool $useCountWrapper = false
): int {
// filters
$countSQL .= $filters->getFilterString();

if (null !== $groupBy) {
$countSQL .= $groupBy;
}

if (null !== $having) {
$countSQL .= $having;
}

if ($useCountWrapper) {
$countSQL = \sprintf('
SELECT
COUNT(*) AS total
FROM (
%s
) AS total
', $countSQL);
}

$statement = $this->getConnection()->prepare($countSQL);

foreach ($filters->getBindMaps() as $bind) {
Expand All @@ -52,6 +78,7 @@ protected function getPaginationCount(string $countSQL, Filter $filters): int
* @param int $page
* @param int $limit
* @param string|null $groupBy
* @param string|null $having
*
* @return array
*
Expand All @@ -63,7 +90,8 @@ protected function getPaginationItemsResult(
array $sort,
int $page,
int $limit,
?string $groupBy = null
?string $groupBy = null,
?string $having = null
): array {
// filters
$itemsSQL .= $filters->getFilterString();
Expand All @@ -72,6 +100,10 @@ protected function getPaginationItemsResult(
$itemsSQL .= $groupBy;
}

if (null !== $having) {
$itemsSQL .= $having;
}

// Sort
$itemsSQL .= $this->nativeSqlHelper->getSortString($sort);

Expand Down

0 comments on commit 5b47fb0

Please sign in to comment.