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
|
|
|
public function __construct(Connection $connection, $sql, array $params = []) |
60
|
|
|
{ |
61
|
|
|
$this->connection = $connection; |
62
|
|
|
|
63
|
|
|
$this->setSql($sql, $params); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Do calculate row count? |
68
|
|
|
* |
69
|
|
|
* @param boolean $calculate |
70
|
|
|
*/ |
71
|
|
|
public function setRowCountCalculated($calculate = true) |
72
|
|
|
{ |
73
|
|
|
$this->rowCountCalculated = (bool) $calculate; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Is row count calculated? |
78
|
|
|
* |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
public function isRowCountCalculated() |
82
|
|
|
{ |
83
|
|
|
return $this->rowCountCalculated; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set Query string with Parameters |
88
|
|
|
* |
89
|
|
|
* @param string $sql |
90
|
|
|
* @param array $params |
91
|
|
|
*/ |
92
|
|
|
public function setSql($sql, array $params = []) |
93
|
|
|
{ |
94
|
|
|
$this->sql = (string) $sql; |
95
|
|
|
|
96
|
|
|
$this->setSqlParameters($params); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set SQL parameters |
101
|
|
|
* |
102
|
|
|
* @param array $params |
103
|
|
|
*/ |
104
|
|
|
public function setSqlParameters(array $params) |
105
|
|
|
{ |
106
|
|
|
$this->params = $params; |
107
|
|
|
|
108
|
|
|
$this->stmt = null; |
109
|
|
|
$this->rowCount = null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function current() |
116
|
|
|
{ |
117
|
|
|
if (null === $this->data) { |
118
|
|
|
$this->rewind(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->data; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function next() |
128
|
|
|
{ |
129
|
|
|
$this->key++; |
130
|
|
|
$this->data = $this->stmt->fetch(\PDO::FETCH_ASSOC); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function key() |
137
|
|
|
{ |
138
|
|
|
return $this->key; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function valid() |
145
|
|
|
{ |
146
|
|
|
if (null === $this->data) { |
147
|
|
|
$this->rewind(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return (false !== $this->data); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function rewind() |
157
|
|
|
{ |
158
|
|
|
if (null === $this->stmt) { |
159
|
|
|
$this->stmt = $this->prepare($this->sql, $this->params); |
160
|
|
|
} |
161
|
|
|
if (0 !== $this->key) { |
|
|
|
|
162
|
|
|
$this->stmt->execute(); |
163
|
|
|
$this->data = $this->stmt->fetch(\PDO::FETCH_ASSOC); |
|
|
|
|
164
|
|
|
$this->key = 0; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function count() |
172
|
|
|
{ |
173
|
|
|
if (null === $this->rowCount) { |
174
|
|
|
if ($this->rowCountCalculated) { |
175
|
|
|
$this->doCalcRowCount(); |
176
|
|
|
} else { |
177
|
|
|
if (null === $this->stmt) { |
178
|
|
|
$this->rewind(); |
179
|
|
|
} |
180
|
|
|
$this->rowCount = $this->stmt->rowCount(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this->rowCount; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function doCalcRowCount() |
188
|
|
|
{ |
189
|
|
|
$statement = $this->prepare(sprintf('SELECT COUNT(*) FROM (%s) AS port_cnt', $this->sql), $this->params); |
190
|
|
|
$statement->execute(); |
191
|
|
|
|
192
|
|
|
$this->rowCount = (int) $statement->fetchColumn(0); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Prepare given statement |
197
|
|
|
* |
198
|
|
|
* @param string $sql |
199
|
|
|
* @param array $params |
200
|
|
|
* |
201
|
|
|
* @return Statement |
202
|
|
|
*/ |
203
|
|
|
private function prepare($sql, array $params) |
204
|
|
|
{ |
205
|
|
|
$statement = $this->connection->prepare($sql); |
206
|
|
|
foreach ($params as $key => $value) { |
207
|
|
|
$statement->bindValue($key, $value); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
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..