1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection\Result; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Exception\DBALException; |
6
|
|
|
use Doctrine\DBAL\Driver\Statement; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Adapt PDO statement to result set |
10
|
|
|
* |
11
|
|
|
* @deprecated Use DoctrineResultSet |
|
|
|
|
12
|
|
|
*/ |
13
|
|
|
/*final*/ class PdoResultSet implements ResultSetInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Statement|\PDOStatement |
17
|
|
|
*/ |
18
|
|
|
private $statement; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
private $key = 0; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var mixed |
27
|
|
|
*/ |
28
|
|
|
private $current; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* PdoResultSet constructor. |
33
|
|
|
* |
34
|
|
|
* @param Statement|\PDOStatement $statement |
35
|
|
|
*/ |
36
|
607 |
|
public function __construct($statement) |
|
|
|
|
37
|
|
|
{ |
38
|
607 |
|
$this->statement = $statement; |
39
|
607 |
|
$this->statement->setFetchMode(\PDO::FETCH_ASSOC); |
|
|
|
|
40
|
607 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
4 |
|
public function current() |
46
|
|
|
{ |
47
|
4 |
|
if ($this->current === null) { |
48
|
2 |
|
$this->rewind(); |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
return $this->current; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
2 |
|
public function next() |
58
|
|
|
{ |
59
|
2 |
|
$this->current = $this->statement->fetch(); |
|
|
|
|
60
|
2 |
|
++$this->key; |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
* |
66
|
|
|
* @psalm-suppress InvalidReturnType |
67
|
|
|
* @psalm-suppress InvalidReturnStatement |
68
|
|
|
*/ |
69
|
2 |
|
public function key() |
70
|
|
|
{ |
71
|
2 |
|
return $this->key; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
2 |
|
public function valid() |
78
|
|
|
{ |
79
|
2 |
|
return $this->current !== false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
|
|
|
|
85
|
6 |
|
public function fetchMode($mode, $options = null) |
86
|
|
|
{ |
87
|
|
|
switch ($mode) { |
88
|
6 |
|
case self::FETCH_ASSOC: |
|
|
|
|
89
|
|
|
$this->statement->setFetchMode(\PDO::FETCH_ASSOC); |
|
|
|
|
90
|
|
|
break; |
|
|
|
|
91
|
|
|
|
92
|
6 |
|
case self::FETCH_NUM: |
|
|
|
|
93
|
1 |
|
$this->statement->setFetchMode(\PDO::FETCH_NUM); |
|
|
|
|
94
|
1 |
|
break; |
|
|
|
|
95
|
|
|
|
96
|
5 |
|
case self::FETCH_OBJECT: |
|
|
|
|
97
|
2 |
|
$this->statement->setFetchMode(\PDO::FETCH_OBJ); |
|
|
|
|
98
|
2 |
|
break; |
|
|
|
|
99
|
|
|
|
100
|
3 |
|
case self::FETCH_COLUMN: |
|
|
|
|
101
|
2 |
|
$this->statement->setFetchMode(\PDO::FETCH_COLUMN, $options ?: 0); |
|
|
|
|
102
|
2 |
|
break; |
|
|
|
|
103
|
|
|
|
104
|
1 |
|
case self::FETCH_CLASS: |
|
|
|
|
105
|
1 |
|
$this->statement->setFetchMode(\PDO::FETCH_CLASS, (string) $options); |
|
|
|
|
106
|
1 |
|
break; |
|
|
|
|
107
|
|
|
|
108
|
|
|
default: |
109
|
|
|
throw new DBALException('Unsupported fetch mode '.$mode); |
|
|
|
|
110
|
|
|
} |
|
|
|
|
111
|
|
|
|
112
|
6 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function asAssociative(): ResultSetInterface |
119
|
|
|
{ |
120
|
|
|
return $this->fetchMode(self::FETCH_ASSOC); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function asList(): ResultSetInterface |
127
|
|
|
{ |
128
|
|
|
return $this->fetchMode(self::FETCH_NUM); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function asObject(): ResultSetInterface |
135
|
|
|
{ |
136
|
|
|
return $this->fetchMode(self::FETCH_OBJECT); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
|
|
|
|
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function asClass(string $className): ResultSetInterface |
143
|
|
|
{ |
144
|
|
|
return $this->fetchMode(self::FETCH_CLASS, $className); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
|
|
|
|
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function asColumn(int $column = 0): ResultSetInterface |
151
|
|
|
{ |
152
|
|
|
return $this->fetchMode(self::FETCH_COLUMN, $column); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
585 |
|
public function all() |
159
|
|
|
{ |
160
|
585 |
|
return $this->statement->fetchAll(); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
4 |
|
public function rewind() |
167
|
|
|
{ |
168
|
4 |
|
$this->current = $this->statement->fetch(); |
|
|
|
|
169
|
4 |
|
$this->key = 0; |
170
|
4 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
457 |
|
public function count() |
176
|
|
|
{ |
177
|
457 |
|
return $this->statement->rowCount(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function isRead(): bool |
184
|
|
|
{ |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public function isWrite(): bool |
192
|
|
|
{ |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function hasWrite(): bool |
200
|
|
|
{ |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Close the cursor on result set destruction |
206
|
|
|
*/ |
207
|
607 |
|
public function __destruct() |
208
|
|
|
{ |
209
|
607 |
|
$this->statement->closeCursor(); |
|
|
|
|
210
|
607 |
|
} |
211
|
|
|
} |
212
|
|
|
|