-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathIdRangePartitionTrait.php
More file actions
53 lines (44 loc) · 1.69 KB
/
Copy pathIdRangePartitionTrait.php
File metadata and controls
53 lines (44 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace Setono\SyliusFeedPlugin\DataSource;
use Doctrine\ORM\QueryBuilder;
use Setono\SyliusFeedPlugin\Generator\ChunkRange;
/**
* Shared id-range partitioning for the Doctrine-backed data sources (§6.3): resolves the `[min, max]`
* id bounds of a source's filtered query and constrains a query to a single chunk range, ordered by
* id, so a fan-out chunk yields exactly its slice and its ordered concatenation matches the inline
* stream byte-for-byte.
*/
trait IdRangePartitionTrait
{
/**
* The inclusive `[min, max]` id bounds of the given query, or null when it would yield nothing.
*/
private function resolveIdRange(QueryBuilder $queryBuilder, string $alias): ?ChunkRange
{
$row = (clone $queryBuilder)
->select(sprintf('MIN(%1$s.id) AS min_id, MAX(%1$s.id) AS max_id', $alias))
->getQuery()
->getSingleResult();
if (!is_array($row)) {
return null;
}
$min = $row['min_id'] ?? null;
$max = $row['max_id'] ?? null;
if (!is_numeric($min) || !is_numeric($max)) {
return null;
}
return new ChunkRange((int) $min, (int) $max);
}
/**
* Constrains the query to a single chunk range, ordered by id ascending.
*/
private function constrainToRange(QueryBuilder $queryBuilder, string $alias, ChunkRange $range): QueryBuilder
{
return $queryBuilder
->andWhere(sprintf('%s.id BETWEEN :chunkStart AND :chunkEnd', $alias))
->addOrderBy(sprintf('%s.id', $alias), 'ASC')
->setParameter('chunkStart', $range->start)
->setParameter('chunkEnd', $range->end);
}
}