Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
92.86% |
13 / 14 |
CRAP | |
50.00% |
1 / 2 |
DatabaseSequenceResult | |
90.91% |
10 / 11 |
|
87.50% |
7 / 8 |
8.05 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
mapping | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResultCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rewind | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
current | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
next | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
LocalSequenceIterator | |
100.00% |
7 / 7 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
rewind | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
current | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
next | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Database; |
6 | |
7 | use Error; |
8 | use Iterator; |
9 | use PDOStatement; |
10 | use Traversable; |
11 | use IteratorIterator; |
12 | use PeServer\Core\Collection\Arr; |
13 | use PeServer\Core\Database\DatabaseResultBase; |
14 | use PeServer\Core\Serialization\IMapper; |
15 | use PeServer\Core\Serialization\Mapper; |
16 | use PeServer\Core\Throws\NotSupportedException; |
17 | use PeServer\Core\Throws\Throws; |
18 | |
19 | /** |
20 | * 逐次問い合わせ結果。 |
21 | * |
22 | * データは保持されない点に注意。 |
23 | * `foreach` 一回回したら終了。 |
24 | * |
25 | * @template TFieldArray of FieldArrayAlias |
26 | * @implements Iterator<TFieldArray> |
27 | */ |
28 | class DatabaseSequenceResult extends DatabaseResultBase implements Iterator |
29 | { |
30 | #region variable |
31 | |
32 | /** |
33 | */ |
34 | private readonly Iterator $iterator; |
35 | /** |
36 | * 影響件数。 |
37 | * |
38 | * 行件数なのか影響件数なのかわけわかんなくなってきた。 |
39 | * |
40 | * @phpstan-var non-negative-int |
41 | */ |
42 | private int $resultCount = 0; |
43 | |
44 | #endregion |
45 | |
46 | /** |
47 | * 生成。 |
48 | */ |
49 | public function __construct( |
50 | array $columns, |
51 | PDOStatement $pdoStatement |
52 | ) { |
53 | parent::__construct($columns, 0); |
54 | |
55 | $this->iterator = new IteratorIterator($pdoStatement); |
56 | $this->resultCount = 0; |
57 | } |
58 | |
59 | #region DatabaseResultBase |
60 | |
61 | /** |
62 | * 結果をマッピングしたイテレータの返却。 |
63 | * |
64 | * @template TObject of object |
65 | * @param string $className |
66 | * @phpstan-param class-string<TObject> $className |
67 | * @param IMapper|null $mapper |
68 | * @return Iterator |
69 | * @phpstan-return Iterator<TObject> |
70 | */ |
71 | public function mapping(string $className, IMapper $mapper = null): Iterator |
72 | { |
73 | return new LocalSequenceIterator($this, $className, $mapper ?? new Mapper()); |
74 | } |
75 | |
76 | #endregion |
77 | |
78 | #region DatabaseResultBase |
79 | |
80 | public function getResultCount(): int |
81 | { |
82 | return $this->resultCount; |
83 | } |
84 | |
85 | #endregion |
86 | |
87 | #region Iterator |
88 | |
89 | public function rewind(): void |
90 | { |
91 | Throws::wrap(Error::class, NotSupportedException::class, fn () => $this->iterator->rewind()); |
92 | } |
93 | |
94 | /** |
95 | * @return int |
96 | * @phpstan-return non-negative-int |
97 | */ |
98 | public function key(): mixed |
99 | { |
100 | return $this->iterator->key(); |
101 | } |
102 | |
103 | /** |
104 | * @phpstan-return TFieldArray |
105 | */ |
106 | public function current(): mixed |
107 | { |
108 | return $this->iterator->current(); |
109 | } |
110 | |
111 | public function next(): void |
112 | { |
113 | $this->iterator->next(); |
114 | $this->resultCount += 1; |
115 | } |
116 | |
117 | public function valid(): bool |
118 | { |
119 | return $this->iterator->valid(); |
120 | } |
121 | |
122 | #endregion |
123 | } |
124 | |
125 | /** |
126 | * @template TFieldArray of FieldArrayAlias |
127 | * @template TObject of object |
128 | * @implements Iterator<TObject> |
129 | */ |
130 | //phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses |
131 | class LocalSequenceIterator extends DatabaseResultBase implements Iterator |
132 | { |
133 | /** |
134 | * 生成。 |
135 | * |
136 | * @param DatabaseSequenceResult $sequence |
137 | * @phpstan-param DatabaseSequenceResult<TFieldArray> $sequence |
138 | * @param string $className |
139 | * @phpstan-param class-string<TObject> $className |
140 | * @param IMapper $mapper |
141 | */ |
142 | public function __construct( |
143 | private DatabaseSequenceResult $sequence, |
144 | private string $className, |
145 | private IMapper $mapper |
146 | ) { |
147 | } |
148 | |
149 | #region Iterator |
150 | |
151 | public function rewind(): void |
152 | { |
153 | $this->sequence->rewind(); |
154 | } |
155 | |
156 | /** |
157 | * @return int |
158 | * @phpstan-return non-negative-int |
159 | */ |
160 | public function key(): mixed |
161 | { |
162 | return $this->sequence->key(); |
163 | } |
164 | |
165 | /** |
166 | * @phpstan-return TObject |
167 | */ |
168 | public function current(): mixed |
169 | { |
170 | $fields = $this->sequence->current(); |
171 | /** @var TObject */ |
172 | return $this->mappingImpl($fields, $this->className, $this->mapper); |
173 | } |
174 | |
175 | public function next(): void |
176 | { |
177 | $this->sequence->next(); |
178 | } |
179 | |
180 | public function valid(): bool |
181 | { |
182 | return $this->sequence->valid(); |
183 | } |
184 | |
185 | #endregion |
186 | } |