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 |
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
|
|
|
public function __call($name, $arguments) |
104
|
|
|
{ |
105
|
|
|
return call_user_func_array(array($this->statement, $name), $arguments); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.