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