|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\DataReader; |
|
6
|
|
|
|
|
7
|
|
|
use Countable; |
|
8
|
|
|
use Cycle\ORM\Select; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use IteratorAggregate; |
|
11
|
|
|
use Spiral\Database\Query\SelectQuery; |
|
12
|
|
|
use Spiral\Pagination\PaginableInterface; |
|
13
|
|
|
use Yiisoft\Data\Reader\CountableDataInterface; |
|
14
|
|
|
use Yiisoft\Data\Reader\DataReaderInterface; |
|
15
|
|
|
use Yiisoft\Data\Reader\OffsetableDataInterface; |
|
16
|
|
|
use Yiisoft\Data\Reader\Sort; |
|
17
|
|
|
use Yiisoft\Data\Reader\SortableDataInterface; |
|
18
|
|
|
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCount; |
|
19
|
|
|
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCollection; |
|
20
|
|
|
|
|
21
|
|
|
final class SelectDataReader implements |
|
22
|
|
|
DataReaderInterface, |
|
23
|
|
|
OffsetableDataInterface, |
|
24
|
|
|
CountableDataInterface, |
|
25
|
|
|
SortableDataInterface, |
|
26
|
|
|
IteratorAggregate |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var Select|SelectQuery */ |
|
29
|
|
|
private $query; |
|
30
|
|
|
private ?int $limit = null; |
|
31
|
|
|
private ?int $offset = null; |
|
32
|
|
|
private ?Sort $sorting = null; |
|
33
|
|
|
private CachedCount $countCache; |
|
34
|
|
|
private CachedCollection $itemsCache; |
|
35
|
|
|
private CachedCollection $oneItemCache; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Select|SelectQuery $query |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($query) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$query instanceof Countable) { |
|
|
|
|
|
|
43
|
|
|
throw new InvalidArgumentException(sprintf('Query should implement %s interface', Countable::class)); |
|
44
|
|
|
} |
|
45
|
|
|
if (!$query instanceof PaginableInterface) { |
|
|
|
|
|
|
46
|
|
|
throw new InvalidArgumentException( |
|
47
|
|
|
sprintf('Query should implement %s interface', PaginableInterface::class) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
$this->query = clone $query; |
|
51
|
|
|
$this->countCache = new CachedCount($this->query); |
|
52
|
|
|
$this->itemsCache = new CachedCollection(); |
|
53
|
|
|
$this->oneItemCache = new CachedCollection(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getSort(): ?Sort |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->sorting; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function withLimit(int $limit): self |
|
62
|
|
|
{ |
|
63
|
|
|
$clone = clone $this; |
|
64
|
|
|
$clone->setLimit($limit); |
|
65
|
|
|
return $clone; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function withOffset(int $offset): self |
|
69
|
|
|
{ |
|
70
|
|
|
$clone = clone $this; |
|
71
|
|
|
$clone->setOffset($offset); |
|
72
|
|
|
return $clone; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function withSort(?Sort $sorting): self |
|
76
|
|
|
{ |
|
77
|
|
|
$clone = clone $this; |
|
78
|
|
|
$clone->setSort($sorting); |
|
79
|
|
|
return $clone; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function count(): int |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->countCache->getCount(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function read(): iterable |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->itemsCache->getCollection() !== null) { |
|
90
|
|
|
return $this->itemsCache->getCollection(); |
|
91
|
|
|
} |
|
92
|
|
|
$query = $this->buildQuery(); |
|
93
|
|
|
$this->itemsCache->setCollection($query->fetchAll()); |
|
94
|
|
|
return $this->itemsCache->getCollection(); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function readOne() |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$this->oneItemCache->isCollected()) { |
|
103
|
|
|
$item = $this->itemsCache->isCollected() |
|
104
|
|
|
// get first item from cached collection |
|
105
|
|
|
? $this->itemsCache->getGenerator()->current() |
|
106
|
|
|
// read data with limit 1 |
|
107
|
|
|
: $this->withLimit(1)->getIterator()->current(); |
|
108
|
|
|
$this->oneItemCache->setCollection($item === null ? [] : [$item]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->oneItemCache->getGenerator()->current(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get Iterator without caching |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getIterator(): \Generator |
|
118
|
|
|
{ |
|
119
|
|
|
if ($this->itemsCache->getCollection() !== null) { |
|
120
|
|
|
yield from $this->itemsCache->getCollection(); |
|
121
|
|
|
} else { |
|
122
|
|
|
yield from $this->buildQuery()->getIterator(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function setSort(?Sort $sorting): void |
|
127
|
|
|
{ |
|
128
|
|
|
if ($this->sorting !== $sorting) { |
|
129
|
|
|
$this->sorting = $sorting; |
|
130
|
|
|
$this->itemsCache = new CachedCollection(); |
|
131
|
|
|
$this->oneItemCache = new CachedCollection(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function setLimit(?int $limit): void |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->limit !== $limit) { |
|
138
|
|
|
$this->limit = $limit; |
|
139
|
|
|
$this->itemsCache = new CachedCollection(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function setOffset(?int $offset): void |
|
144
|
|
|
{ |
|
145
|
|
|
if ($this->offset !== $offset) { |
|
146
|
|
|
$this->offset = $offset; |
|
147
|
|
|
$this->itemsCache = new CachedCollection(); |
|
148
|
|
|
$this->oneItemCache = new CachedCollection(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return Select|SelectQuery |
|
154
|
|
|
*/ |
|
155
|
|
|
private function buildQuery() |
|
156
|
|
|
{ |
|
157
|
|
|
$newQuery = clone $this->query; |
|
158
|
|
|
if ($this->offset !== null) { |
|
159
|
|
|
$newQuery->offset($this->offset); |
|
160
|
|
|
} |
|
161
|
|
|
if ($this->sorting !== null) { |
|
162
|
|
|
$newQuery->orderBy($this->sorting->getOrder()); |
|
163
|
|
|
} |
|
164
|
|
|
if ($this->limit !== null) { |
|
165
|
|
|
$newQuery->limit($this->limit); |
|
166
|
|
|
} |
|
167
|
|
|
return $newQuery; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|