1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Micayael\NativeQueryFromFileBuilderBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
7
|
|
|
use Doctrine\ORM\NoResultException; |
8
|
|
|
use Doctrine\ORM\Query\ResultSetMappingBuilder; |
9
|
|
|
use Micayael\NativeQueryFromFileBuilderBundle\Helper\NativeQueryBuilderHelper; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
|
12
|
|
|
class NativeQueryBuilder implements NativeQueryBuilderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var EntityManagerInterface |
16
|
|
|
*/ |
17
|
|
|
private $em; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var EventDispatcherInterface |
21
|
|
|
*/ |
22
|
|
|
private $eventDispatcher; |
23
|
|
|
|
24
|
|
|
private $helper; |
25
|
|
|
|
26
|
|
|
public function __construct(EntityManagerInterface $em, ?EventDispatcherInterface $eventDispatcher, array $config) |
27
|
|
|
{ |
28
|
|
|
$this->em = $em; |
29
|
|
|
$this->eventDispatcher = $eventDispatcher; |
30
|
|
|
|
31
|
|
|
$this->helper = new NativeQueryBuilderHelper($this->eventDispatcher, $config['sql_queries_dir'], $config['file_extension']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function findOneFromSqlKey(string $key, array $params = [], ResultSetMappingBuilder $rsm = null) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$sql = $this->helper->getSqlFromYamlKey($key, $params); |
38
|
|
|
|
39
|
|
|
$ret = []; |
40
|
|
|
|
41
|
|
|
if ($rsm) { |
42
|
|
|
$nativeQuery = $this->em |
43
|
|
|
->createNativeQuery($sql, $rsm); |
44
|
|
|
|
45
|
|
|
foreach ($params as $key => $value) { |
46
|
|
|
$nativeQuery->setParameter($key, $value); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$ret[] = $nativeQuery->getSingleResult(); |
50
|
|
|
} else { |
51
|
|
|
$ret = $this->em |
52
|
|
|
->getConnection() |
53
|
|
|
->fetchAll($sql, $params); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (empty($ret)) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (count($ret) > 1) { |
61
|
|
|
throw new NonUniqueResultException(sprintf('Se han encontrado %d filas', count($ret))); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $ret[0]; |
65
|
|
|
} catch (NoResultException $e) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function findFromSqlKey(string $key, array $params = [], ResultSetMappingBuilder $rsm = null): array |
71
|
|
|
{ |
72
|
|
|
$sql = $this->helper->getSqlFromYamlKey($key, $params); |
73
|
|
|
|
74
|
|
|
if ($rsm) { |
75
|
|
|
$nativeQuery = $this->em |
76
|
|
|
->createNativeQuery($sql, $rsm) |
77
|
|
|
; |
78
|
|
|
|
79
|
|
|
foreach ($params as $key => $value) { |
80
|
|
|
$nativeQuery->setParameter($key, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$ret = $nativeQuery->getResult(); |
84
|
|
|
} else { |
85
|
|
|
$ret = $this->em |
86
|
|
|
->getConnection() |
87
|
|
|
->fetchAll($sql, $params); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $ret; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function findScalarFromSqlKey(string $key, array $params = []) |
94
|
|
|
{ |
95
|
|
|
$sql = $this->helper->getSqlFromYamlKey($key, $params); |
96
|
|
|
|
97
|
|
|
$ret = $this->em |
98
|
|
|
->getConnection() |
99
|
|
|
->fetchColumn($sql, $params); |
100
|
|
|
|
101
|
|
|
return $ret; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|