-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultEntity.php
258 lines (228 loc) · 6.25 KB
/
ResultEntity.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace WebStream\Database;
use Doctrine\DBAL\Driver\Statement;
use WebStream\DI\Injector;
use WebStream\Exception\Extend\CollectionException;
/**
* ResultEntity
* @author Ryuichi TANAKA.
* @since 2015/01/11
* @version 0.7
*/
class ResultEntity implements \Iterator, \SeekableIterator, \ArrayAccess, \Countable
{
use Injector;
/**
* @var Doctrine\DBAL\Statement ステートメント
*/
private $stmt;
/**
* @var array<mixed> 列データ
*/
private array $row;
/**
* @var array<mixed> キャッシュ化列データ
*/
private array $rowCache;
/**
* @var int インデックス位置
*/
private int $position;
/**
* @var EntityManager エンティティマネージャ
*/
private EntityManager $entityManager;
/**
* コンストラクタ
* @param Statement $stmt ステートメントオブジェクト
* @param string $classpath クラスパス
*/
public function __construct(Statement $stmt, string $classpath)
{
$this->stmt = $stmt;
$this->position = 0;
$this->rowCache = [];
$this->entityManager = new EntityManager($classpath);
}
/**
* デストラクタ
*/
public function __destruct()
{
$this->stmt = null;
$this->rowCache = [];
}
/**
* 初期処理
*/
public function initialize()
{
$this->entityManager->inject('logger', $this->logger)
->setColumnMeta($this->getColumnMeta());
}
/**
* Implements Countable#count
* @return integer 結果件数
*/
public function count()
{
if ($this->stmt === null) {
$count = count($this->rowCache);
} else {
$count = $this->stmt->rowCount();
if ($count === 0) {
$this->toArray();
$count = count($this->rowCache);
}
}
return $count;
}
/**
* Implements SeekableIterator#seek
* カーソル位置を移動する
* @param mixed オフセット
* @return mixed
*/
public function seek($offset)
{
if ($this->stmt !== null) {
// Mysql does not support scrollable cursor.
// but if statement to array, it is accessable.
$this->toArray();
}
if (array_key_exists($offset, $this->rowCache)) {
return $this->rowCache[$offset];
} else {
throw new \OutOfBoundsException("Current cursor is out of range: " . $offset);
}
}
/**
* Implements Iterator#current
* 現在の要素を返却する
* @return array<string> 列データ
* @throws \ReflectionException
*/
public function current()
{
if ($this->stmt === null) {
return array_key_exists($this->position, $this->rowCache) ? $this->rowCache[$this->position] : null;
}
return $this->entityManager->getEntity($this->row);
}
/**
* Implements Iterator#key
* 現在の要素のキーを返却する
* @return integer キー
*/
public function key()
{
return $this->position;
}
/**
* Implements Iterator#next
* 次の要素に進む
*/
public function next()
{
if ($this->stmt !== null) {
$row = $this->stmt->fetch(\PDO::FETCH_ASSOC);
$this->row = is_bool($row) && $row === false ? [] : $row;
}
$this->position++;
}
/**
* Implements Iterator#rewind
* イテレータを先頭に巻き戻す
*/
public function rewind()
{
if ($this->stmt !== null) {
$this->row = $this->stmt->fetch(\PDO::FETCH_ASSOC, \PDO::FETCH_ORI_FIRST);
}
$this->position = 0;
}
/**
* Implements Iterator#valid
* 現在位置が有効かどうかを調べる
* @return bool 有効かどうか
*/
public function valid()
{
return !empty($this->row);
}
/**
* Implements ArrayAccess#offsetExists
* オフセットの位置に値が存在するかどうか返却する
* @param $offset
* @return bool 値が存在するかどうか
*/
public function offsetExists($offset)
{
if ($this->stmt !== null) {
$this->toArray();
}
return array_key_exists($offset, $this->rowCache);
}
/**
* Implements ArrayAccess#offsetGet
* オフセットの位置の値を返却する
* @param $offset
* @return mixed 値
*/
public function offsetGet($offset)
{
if ($this->stmt !== null) {
$this->toArray();
}
return $this->rowCache[$offset];
}
/**
* Implements ArrayAccess#offsetSet
* オフセットの位置に値を設定する
* @param mixed オフセット
* @param mixed 値
*/
public function offsetSet($offset, $value)
{
throw new CollectionException("Database results are read only.");
}
/**
* Implements ArrayAccess#offsetUnSet
* オフセットの設定を解除する
* @param mixed オフセット
*/
public function offsetUnSet($offset)
{
throw new CollectionException("Database results are read only.");
}
/**
* 検索結果を全て配列として返却する
* @return array<string> 検索結果
*/
public function toArray()
{
$this->rowCache = $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
$this->logger->debug("All results to array and cached.");
$this->stmt = null;
return $this->rowCache;
}
/**
* テーブルのメタデータを返却する
* @return array<string> メタデータ
*/
private function getColumnMeta()
{
$columnMeta = [];
for ($index = 0; $index < $this->stmt->columnCount(); $index++) {
$column = $this->stmt->getColumnMeta($index);
if (array_key_exists('sqlite:decl_type', $column)) {
// sqlite
$columnMeta[$column['name']] = $column['sqlite:decl_type'];
} else {
// mysql, postgresql
$columnMeta[$column['name']] = $column['native_type'];
}
}
return $columnMeta;
}
}