1 | <?php |
||
24 | final class DoctrineDbalExecutorResult implements \IteratorAggregate, Statement |
||
25 | { |
||
26 | /** |
||
27 | * @var Statement |
||
28 | */ |
||
29 | private $statement; |
||
30 | |||
31 | /** |
||
32 | * DoctrineDbalExecutorResult constructor. |
||
33 | * |
||
34 | * @param Statement $statement Wrapped statement. |
||
35 | */ |
||
36 | 15 | public function __construct(Statement $statement) |
|
40 | |||
41 | /** |
||
42 | * Get single scalar result. |
||
43 | * |
||
44 | * @return mixed A single scalar value. |
||
45 | * |
||
46 | * @throws NoResultException |
||
47 | * @throws NonUniqueResultException |
||
48 | */ |
||
49 | 5 | public function getSingleScalarResult() |
|
50 | { |
||
51 | 5 | $scalar = $this->statement->fetchColumn(0); |
|
52 | |||
53 | 5 | if (false === $scalar) { |
|
54 | 3 | throw new NoResultException('Expected on result for given query.'); |
|
55 | } |
||
56 | |||
57 | 2 | if (false !== $this->statement->fetch()) { |
|
58 | 1 | throw new NonUniqueResultException('Expected only one result for given query.'); |
|
59 | } |
||
60 | |||
61 | 1 | return $scalar; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get single scalar result or default value if there are no results of executed |
||
66 | * SELECT statement. |
||
67 | * |
||
68 | * @param mixed $default A default single scalar value. |
||
69 | * @return mixed A single scalar value. |
||
70 | */ |
||
71 | 2 | public function getSingleScalarResultOrDefault($default) |
|
72 | { |
||
73 | try { |
||
74 | 2 | return $this->getSingleScalarResult(); |
|
75 | 2 | } catch (NoResultException $e) { |
|
76 | 2 | return $default; |
|
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get single scalar result or NULL value if there are no results of executed |
||
82 | * SELECT statement. |
||
83 | * |
||
84 | * @return mixed|null A single scalar value. |
||
85 | */ |
||
86 | 1 | public function getSingleScalarResultOrNull() |
|
87 | { |
||
88 | 1 | return $this->getSingleScalarResultOrDefault(null); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get collection of scalar values. |
||
93 | * |
||
94 | * @return array A collection of scalar values. |
||
95 | */ |
||
96 | 3 | public function getScalarResult() |
|
97 | { |
||
98 | 3 | $result = []; |
|
99 | |||
100 | 3 | while ($val = $this->statement->fetchColumn()) { |
|
101 | 1 | $result[] = $val; |
|
102 | } |
||
103 | |||
104 | 3 | return $result; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get collection of scalar vales, or default value if collection is empty. |
||
109 | * |
||
110 | * @param mixed $default A default value. |
||
111 | * @return array|mixed A collection of scalar values or default value. |
||
112 | */ |
||
113 | 2 | public function getScalarResultOrDefault($default) |
|
114 | { |
||
115 | 2 | $result = $this->getScalarResult(); |
|
116 | |||
117 | 2 | if (0 === count($result)) { |
|
118 | 2 | return $default; |
|
119 | } |
||
120 | |||
121 | return $result; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get collection of scalar vales, or NULL value if collection is empty. |
||
126 | * |
||
127 | * @return array|mixed A collection of NULL value. |
||
128 | */ |
||
129 | 1 | public function getScalarResultOrNull() |
|
130 | { |
||
131 | 1 | return $this->getScalarResultOrDefault(null); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get single (first) row result from result set. |
||
136 | * |
||
137 | * @return array A single (first) row of result set. |
||
138 | * |
||
139 | * @throws NoResultException |
||
140 | * @throws NonUniqueResultException |
||
141 | */ |
||
142 | 5 | public function getSingleRowResult() |
|
143 | { |
||
144 | 5 | $row = $this->statement->fetch(\PDO::FETCH_BOTH); |
|
145 | |||
146 | 5 | if (false === $row) { |
|
147 | 3 | throw new NoResultException('Expected on result for given query.'); |
|
148 | } |
||
149 | |||
150 | 2 | if (false !== $this->statement->fetch()) { |
|
151 | 1 | throw new NonUniqueResultException('Expected only ine result for given query.'); |
|
152 | } |
||
153 | |||
154 | 1 | return $row; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Get single (first) row result from result set or default value if result set is empty. |
||
159 | * |
||
160 | * @param mixed $default Default value if result set is empty. |
||
161 | * @return array|mixed A single (first) row of result set. |
||
162 | * |
||
163 | * @throws NoResultException |
||
164 | * @throws NonUniqueResultException |
||
165 | */ |
||
166 | 2 | public function getSingleRowOrDefault($default) |
|
167 | { |
||
168 | try { |
||
169 | 2 | return $this->getSingleRowResult(); |
|
170 | 2 | } catch (NoResultException $e) { |
|
171 | 2 | return $default; |
|
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get single (first) row result from result set or NULL value if result set is empty. |
||
177 | * |
||
178 | * @return array A single (first) row of result set. |
||
179 | * |
||
180 | * @throws NoResultException |
||
181 | * @throws NonUniqueResultException |
||
182 | */ |
||
183 | 1 | public function getSingleRowOrNull() |
|
184 | { |
||
185 | 1 | return $this->getSingleRowOrDefault(null); |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | public function closeCursor() |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function columnCount() |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
||
216 | { |
||
217 | return $this->statement->fetch($fetchMode, $cursorOrientation, $cursorOffset); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
||
224 | { |
||
225 | return $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function fetchColumn($columnIndex = 0) |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function bindValue($param, $value, $type = null) |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function bindParam($column, &$variable, $type = null, $length = null) |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function errorCode() |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function errorInfo() |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function execute($params = null) |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | public function rowCount() |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function __get($name) |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function __set($name, $value) |
||
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | public function __isset($name) |
||
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function __call($name, $arguments) |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function getIterator() |
||
320 | { |
||
321 | while (false !== ($row = $this->statement->fetch(\PDO::FETCH_BOTH))) { |
||
325 | } |
||
326 |