1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RunOpenCode\Bundle\QueryResourcesLoader\Executor; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Statement; |
6
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\NonUniqueResultException; |
7
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\NoResultException; |
8
|
|
|
|
9
|
|
|
final class DoctrineDbalExecutorResult implements Statement |
10
|
|
|
{ |
11
|
|
|
private $statement; |
12
|
|
|
|
13
|
|
|
public function __construct(Statement $statement) |
14
|
|
|
{ |
15
|
|
|
$this->statement = $statement; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
View Code Duplication |
public function getSingleScalarResult() |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$scalar = $this->statement->fetchColumn(0); |
21
|
|
|
|
22
|
|
|
if (false === $scalar) { |
23
|
|
|
throw new NoResultException('Expected on result for given query.'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (false !== $this->statement->fetch()) { |
27
|
|
|
throw new NonUniqueResultException('Expected only ine result for given query.'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $scalar; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getSingleScalarResultOrDefault($default) |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
return $this->getSingleScalarResult(); |
37
|
|
|
} catch (NoResultException $e) { |
38
|
|
|
return $default; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getSingleScalarResultOrNull() |
43
|
|
|
{ |
44
|
|
|
return $this->getSingleScalarResultOrDefault(null); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getScalarResult() |
48
|
|
|
{ |
49
|
|
|
$result = []; |
50
|
|
|
|
51
|
|
|
while ($val = $this->statement->fetchColumn()) { |
52
|
|
|
$result[] = $val; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $result; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getScalarResultOrDefault($default) |
59
|
|
|
{ |
60
|
|
|
$result = $this->getScalarResult(); |
61
|
|
|
|
62
|
|
|
if (count($result) < 0) { |
63
|
|
|
return $default; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $result; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getScalarResultOrNull() |
70
|
|
|
{ |
71
|
|
|
return $this->getScalarResultOrDefault(null); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function getSingleRowResult() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$row = $this->statement->fetch(\PDO::FETCH_BOTH); |
77
|
|
|
|
78
|
|
|
if (false === $row) { |
79
|
|
|
throw new NoResultException('Expected on result for given query.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (false !== $this->statement->fetch()) { |
83
|
|
|
throw new NonUniqueResultException('Expected only ine result for given query.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $row; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getSingleRowOrDefault($default) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
return $this->getSingleRowResult(); |
93
|
|
|
} catch (NoResultException $e) { |
94
|
|
|
return $default; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getSingleRowOrNull() |
99
|
|
|
{ |
100
|
|
|
$this->getSingleRowOrDefault(null); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function closeCursor() |
107
|
|
|
{ |
108
|
|
|
return $this->statement->closeCursor(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function columnCount() |
115
|
|
|
{ |
116
|
|
|
return $this->statement->columnCount(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
123
|
|
|
{ |
124
|
|
|
return $this->statement->setFetchMode($fetchMode, $arg2, $arg3); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function fetch($fetchMode = null) |
131
|
|
|
{ |
132
|
|
|
return $this->statement->fetch($fetchMode); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function fetchAll($fetchMode = null) |
139
|
|
|
{ |
140
|
|
|
return $this->statement->fetchAll($fetchMode); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function fetchColumn($columnIndex = 0) |
147
|
|
|
{ |
148
|
|
|
return $this->statement->fetchColumn($columnIndex); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function bindValue($param, $value, $type = null) |
155
|
|
|
{ |
156
|
|
|
return $this->statement->bindValue($param, $value, $type); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function bindParam($column, &$variable, $type = null, $length = null) |
163
|
|
|
{ |
164
|
|
|
return $this->statement->bindParam($column, $variable, $type, $length); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
public function errorCode() |
171
|
|
|
{ |
172
|
|
|
return $this->statement->errorCode(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function errorInfo() |
179
|
|
|
{ |
180
|
|
|
return $this->statement->errorInfo(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function execute($params = null) |
187
|
|
|
{ |
188
|
|
|
return $this->statement->execute($params); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function rowCount() |
195
|
|
|
{ |
196
|
|
|
return $this->statement->rowCount(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function __get($name) |
203
|
|
|
{ |
204
|
|
|
return $this->statement->{$name}; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function __set($name, $value) |
211
|
|
|
{ |
212
|
|
|
$this->statement->{$name} = $value; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function __isset($name) |
219
|
|
|
{ |
220
|
|
|
return isset($this->statement[$name]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritdoc} |
225
|
|
|
*/ |
226
|
|
|
public function __call($name, $arguments) |
227
|
|
|
{ |
228
|
|
|
return call_user_func_array(array($this->statement, $name), $arguments); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.