1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Pagination\WalkStrategy; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Collection\CollectionInterface; |
6
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
7
|
|
|
use Bdf\Prime\Query\Clause; |
8
|
|
|
use Bdf\Prime\Query\CompilableClause; |
9
|
|
|
use Bdf\Prime\Query\Contract\Limitable; |
10
|
|
|
use Bdf\Prime\Query\Contract\Orderable; |
11
|
|
|
use Bdf\Prime\Query\Contract\ReadOperation; |
12
|
|
|
use Bdf\Prime\Query\Contract\Whereable; |
13
|
|
|
use Bdf\Prime\Query\ReadCommandInterface; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
use function method_exists; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Walk strategy using a primary key (or any unique key) as cursor |
20
|
|
|
* This strategy supports deleting entities during the walk, but the entity must contains a single primary key, and the query must be ordered by this key |
21
|
|
|
* Any sort on other attribute are not supported |
22
|
|
|
* |
23
|
|
|
* @template E as object |
24
|
|
|
* @implements WalkStrategyInterface<E> |
25
|
|
|
*/ |
26
|
|
|
final class KeyWalkStrategy implements WalkStrategyInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var KeyInterface<E> |
30
|
|
|
*/ |
31
|
|
|
private $key; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* PrimaryKeyWalkStrategy constructor. |
35
|
|
|
* |
36
|
|
|
* @param KeyInterface<E> $key |
37
|
|
|
*/ |
38
|
19 |
|
public function __construct(KeyInterface $key) |
39
|
|
|
{ |
40
|
19 |
|
$this->key = $key; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
16 |
|
public function initialize(ReadCommandInterface $query, int $chunkSize, int $startPage): WalkCursor |
47
|
|
|
{ |
48
|
16 |
|
if (!self::supports($query, $startPage, $this->key->name())) { |
49
|
3 |
|
throw new InvalidArgumentException('KeyWalkStrategy is not supported by this query'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @var Limitable&Orderable&ReadCommandInterface<ConnectionInterface, E> $query */ |
53
|
13 |
|
$query = clone $query; |
54
|
|
|
|
55
|
13 |
|
if (!isset($query->getOrders()[$this->key->name()])) { |
56
|
13 |
|
$query->order($this->key->name(), Orderable::ORDER_ASC); |
57
|
|
|
} |
58
|
|
|
|
59
|
13 |
|
$query->limit($chunkSize); |
60
|
|
|
|
61
|
|
|
/** @var WalkCursor<E> */ |
62
|
13 |
|
return new WalkCursor($query); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
#[ReadOperation] |
69
|
13 |
|
public function next(WalkCursor $cursor): WalkCursor |
70
|
|
|
{ |
71
|
13 |
|
$cursor = clone $cursor; |
72
|
|
|
|
73
|
13 |
|
if ($cursor->entities) { |
74
|
11 |
|
$cursor->cursor = $this->getLastKeyOfEntities($cursor); |
75
|
|
|
} |
76
|
|
|
|
77
|
13 |
|
if ($cursor->cursor !== null) { |
78
|
|
|
/** @var ReadCommandInterface<ConnectionInterface, E>&Orderable&Whereable $query */ |
79
|
11 |
|
$query = $cursor->query; |
80
|
11 |
|
$column = $this->key->name(); |
81
|
11 |
|
$operator = $query->getOrders()[$column] === Orderable::ORDER_ASC ? '>' : '<'; |
|
|
|
|
82
|
|
|
|
83
|
|
|
// #FRAM-86 : reset where clause |
84
|
|
|
// @todo remove method_exists check on prime 3.0 |
85
|
11 |
|
if (method_exists($query, 'whereReplace')) { |
86
|
11 |
|
$query->whereReplace($column, $operator, $cursor->cursor); |
87
|
|
|
} else { |
88
|
|
|
$query->where($column, $operator, $cursor->cursor); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
13 |
|
$cursor->entities = $cursor->query->all(); |
|
|
|
|
93
|
|
|
|
94
|
13 |
|
if ($cursor->entities instanceof CollectionInterface) { |
95
|
|
|
$cursor->entities = $cursor->entities->all(); |
96
|
|
|
} |
97
|
|
|
|
98
|
13 |
|
return $cursor; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check if the strategy supports the given parameters |
103
|
|
|
* |
104
|
|
|
* @param ReadCommandInterface $query The query |
105
|
|
|
* @param int|null $startPage The start page |
106
|
|
|
* @param string $key The cursor key |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
* |
110
|
|
|
* @psalm-assert-if-true Orderable&Limitable&Whereable $query |
111
|
|
|
*/ |
112
|
19 |
|
public static function supports(ReadCommandInterface $query, ?int $startPage, string $key): bool |
113
|
|
|
{ |
114
|
19 |
|
if ($startPage !== null && $startPage !== 1) { |
115
|
3 |
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
17 |
|
if (!($query instanceof Orderable && $query instanceof Limitable && $query instanceof Whereable)) { |
119
|
2 |
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
16 |
|
$orders = $query->getOrders(); |
123
|
|
|
|
124
|
16 |
|
return empty($orders) || (count($orders) === 1 && isset($orders[$key])); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Find the last key to be set as cursor value |
129
|
|
|
* |
130
|
|
|
* @param WalkCursor $cursor |
131
|
|
|
* @return mixed |
132
|
|
|
* |
133
|
|
|
* @see WalkCursor::$cursor |
134
|
|
|
*/ |
135
|
11 |
|
private function getLastKeyOfEntities(WalkCursor $cursor) |
136
|
|
|
{ |
137
|
11 |
|
$lastEntity = end($cursor->entities); |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Basic select query : results are an ordered list, so the last key is always the key of the last entity |
140
|
11 |
|
if (array_is_list($cursor->entities)) { |
141
|
8 |
|
return $this->key->get($lastEntity); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// group by query |
145
|
|
|
// Because index can be overridden (or value are added), order is not guaranteed |
146
|
|
|
// So we should iterate other entities to find the "max" key |
147
|
|
|
// In case of "by combine", values of each key are ordered list, so we simply need to take the last entity's key of each index |
148
|
3 |
|
$lastKey = $this->key->get(is_array($lastEntity) ? end($lastEntity) : $lastEntity); |
149
|
|
|
|
150
|
|
|
/** @var ReadCommandInterface<ConnectionInterface, E>&Orderable&Whereable $query */ |
151
|
3 |
|
$query = $cursor->query; |
152
|
3 |
|
$asc = $query->getOrders()[$this->key->name()] === Orderable::ORDER_ASC; |
153
|
|
|
|
154
|
3 |
|
foreach ($cursor->entities as $entity) { |
155
|
3 |
|
$key = $this->key->get(is_array($entity) ? end($entity) : $entity); |
156
|
3 |
|
$gt = $key > $lastKey; |
157
|
|
|
|
158
|
|
|
// order is ascendant and key is > lastKey |
159
|
|
|
// or order is descendant and key is < lastKey |
160
|
3 |
|
if ($asc === $gt) { |
161
|
2 |
|
$lastKey = $key; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
return $lastKey; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|