|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Port\Dbal; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Statement; |
|
7
|
|
|
use Port\Reader\CountableReader; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Reads data through the Doctrine DBAL |
|
11
|
|
|
*/ |
|
12
|
|
|
class DbalReader implements CountableReader |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Connection |
|
16
|
|
|
*/ |
|
17
|
|
|
private $connection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $data; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Statement |
|
26
|
|
|
*/ |
|
27
|
|
|
private $stmt; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $sql; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $params; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var integer |
|
41
|
|
|
*/ |
|
42
|
|
|
private $rowCount; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var boolean |
|
46
|
|
|
*/ |
|
47
|
|
|
private $rowCountCalculated = true; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $key; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param Connection $connection |
|
56
|
|
|
* @param string $sql |
|
57
|
|
|
* @param array $params |
|
58
|
|
|
*/ |
|
59
|
10 |
|
public function __construct(Connection $connection, $sql, array $params = []) |
|
60
|
|
|
{ |
|
61
|
10 |
|
$this->connection = $connection; |
|
62
|
|
|
|
|
63
|
10 |
|
$this->setSql($sql, $params); |
|
64
|
10 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Do calculate row count? |
|
68
|
|
|
* |
|
69
|
|
|
* @param boolean $calculate |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function setRowCountCalculated($calculate = true) |
|
72
|
|
|
{ |
|
73
|
2 |
|
$this->rowCountCalculated = (bool) $calculate; |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Is row count calculated? |
|
78
|
|
|
* |
|
79
|
|
|
* @return boolean |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function isRowCountCalculated() |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->rowCountCalculated; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Set Query string with Parameters |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $sql |
|
90
|
|
|
* @param array $params |
|
91
|
|
|
*/ |
|
92
|
10 |
|
public function setSql($sql, array $params = []) |
|
93
|
|
|
{ |
|
94
|
10 |
|
$this->sql = (string) $sql; |
|
95
|
|
|
|
|
96
|
10 |
|
$this->setSqlParameters($params); |
|
97
|
10 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set SQL parameters |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $params |
|
103
|
|
|
*/ |
|
104
|
10 |
|
public function setSqlParameters(array $params) |
|
105
|
|
|
{ |
|
106
|
10 |
|
$this->params = $params; |
|
107
|
|
|
|
|
108
|
10 |
|
$this->stmt = null; |
|
109
|
10 |
|
$this->rowCount = null; |
|
110
|
10 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
5 |
|
public function current() |
|
116
|
|
|
{ |
|
117
|
5 |
|
if (null === $this->data) { |
|
118
|
3 |
|
$this->rewind(); |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
5 |
|
return $this->data; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function next() |
|
128
|
|
|
{ |
|
129
|
2 |
|
$this->key++; |
|
130
|
2 |
|
$this->data = $this->stmt->fetch(\PDO::FETCH_ASSOC); |
|
|
|
|
|
|
131
|
2 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function key() |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $this->key; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public function valid() |
|
145
|
|
|
{ |
|
146
|
3 |
|
if (null === $this->data) { |
|
147
|
1 |
|
$this->rewind(); |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
3 |
|
return (false !== $this->data); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
7 |
|
public function rewind() |
|
157
|
|
|
{ |
|
158
|
7 |
|
if (null === $this->stmt) { |
|
159
|
7 |
|
$this->stmt = $this->prepare($this->sql, $this->params); |
|
160
|
7 |
|
} |
|
161
|
7 |
|
if (0 !== $this->key) { |
|
|
|
|
|
|
162
|
7 |
|
$this->stmt->execute(); |
|
163
|
7 |
|
$this->data = $this->stmt->fetch(\PDO::FETCH_ASSOC); |
|
|
|
|
|
|
164
|
7 |
|
$this->key = 0; |
|
|
|
|
|
|
165
|
7 |
|
} |
|
166
|
7 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
3 |
|
public function count() |
|
172
|
|
|
{ |
|
173
|
3 |
|
if (null === $this->rowCount) { |
|
174
|
3 |
|
if ($this->rowCountCalculated) { |
|
175
|
2 |
|
$this->doCalcRowCount(); |
|
176
|
2 |
|
} else { |
|
177
|
1 |
|
if (null === $this->stmt) { |
|
178
|
1 |
|
$this->rewind(); |
|
179
|
1 |
|
} |
|
180
|
1 |
|
$this->rowCount = $this->stmt->rowCount(); |
|
181
|
|
|
} |
|
182
|
3 |
|
} |
|
183
|
|
|
|
|
184
|
3 |
|
return $this->rowCount; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
2 |
|
private function doCalcRowCount() |
|
188
|
|
|
{ |
|
189
|
2 |
|
$statement = $this->prepare(sprintf('SELECT COUNT(*) FROM (%s) AS port_cnt', $this->sql), $this->params); |
|
190
|
2 |
|
$statement->execute(); |
|
191
|
|
|
|
|
192
|
2 |
|
$this->rowCount = (int) $statement->fetchColumn(0); |
|
|
|
|
|
|
193
|
2 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Prepare given statement |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $sql |
|
199
|
|
|
* @param array $params |
|
200
|
|
|
* |
|
201
|
|
|
* @return Statement |
|
202
|
|
|
*/ |
|
203
|
8 |
|
private function prepare($sql, array $params) |
|
204
|
|
|
{ |
|
205
|
8 |
|
$statement = $this->connection->prepare($sql); |
|
206
|
8 |
|
foreach ($params as $key => $value) { |
|
207
|
8 |
|
$statement->bindValue($key, $value); |
|
208
|
8 |
|
} |
|
209
|
|
|
|
|
210
|
8 |
|
return $statement; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..