1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Micayael\NativeQueryFromFileBuilderBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Result; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\ORM\NoResultException; |
8
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
9
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
10
|
|
|
use Micayael\NativeQueryFromFileBuilderBundle\Helper\NativeQueryBuilderHelper; |
11
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
class NativeQueryBuilder implements NativeQueryBuilderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EntityManagerInterface |
18
|
|
|
*/ |
19
|
|
|
private $entityManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ManagerRegistry |
23
|
|
|
*/ |
24
|
|
|
private $doctrine; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EventDispatcherInterface|null |
28
|
|
|
*/ |
29
|
|
|
private $eventDispatcher; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CacheItemPoolInterface|null |
33
|
|
|
*/ |
34
|
|
|
private $cache; |
35
|
|
|
|
36
|
|
|
private $helper; |
37
|
|
|
|
38
|
|
|
private $bundleConfig; |
39
|
|
|
|
40
|
|
|
public function __construct(EntityManagerInterface $entityManager, ManagerRegistry $doctrine, ?EventDispatcherInterface $eventDispatcher, ?CacheItemPoolInterface $cache, array $bundleConfig) |
41
|
|
|
{ |
42
|
|
|
$this->entityManager = $entityManager; |
43
|
|
|
$this->doctrine = $doctrine; |
44
|
|
|
$this->eventDispatcher = $eventDispatcher; |
45
|
|
|
$this->cache = $cache; |
46
|
|
|
$this->bundleConfig = $bundleConfig; |
47
|
|
|
|
48
|
|
|
$this->helper = new NativeQueryBuilderHelper($this->eventDispatcher, $this->cache, $bundleConfig); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function changeEntityManager(EntityManagerInterface $entityManager) |
52
|
|
|
{ |
53
|
|
|
$this->entityManager = $entityManager; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function findFromSqlKey(string $key, array $params = [], ?string $orderBy = null, string $connectionName = null, ResultSetMapping $rsm = null): array |
57
|
|
|
{ |
58
|
|
|
if ($orderBy) { |
59
|
|
|
$params['orderby'] = $orderBy; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$sql = $this->helper->getSqlFromYamlKey($key, $params); |
63
|
|
|
|
64
|
|
|
if ($rsm) { |
65
|
|
|
$nativeQuery = $this->entityManager |
66
|
|
|
->createNativeQuery($sql, $rsm) |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
foreach ($params as $key => $value) { |
70
|
|
|
$nativeQuery->setParameter($key, $value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$ret = $nativeQuery->getResult(); |
74
|
|
|
} else { |
75
|
|
|
$result = $this->getResultFromConnection($connectionName, $sql, $params); |
76
|
|
|
|
77
|
|
|
$ret = $result->fetchAllAssociative(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $ret; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function findOneFromSqlKey(string $key, array $params = [], string $connectionName = null, ResultSetMapping $rsm = null) |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$sql = $this->helper->getSqlFromYamlKey($key, $params); |
87
|
|
|
|
88
|
|
|
$ret = []; |
89
|
|
|
|
90
|
|
|
if ($rsm) { |
91
|
|
|
$nativeQuery = $this->entityManager |
92
|
|
|
->createNativeQuery($sql, $rsm); |
93
|
|
|
|
94
|
|
|
foreach ($params as $key => $value) { |
95
|
|
|
$nativeQuery->setParameter($key, $value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$ret[] = $nativeQuery->getSingleResult(); |
99
|
|
|
} else { |
100
|
|
|
$result = $this->getResultFromConnection($connectionName, $sql, $params); |
101
|
|
|
|
102
|
|
|
$ret = $result->fetchAssociative(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (empty($ret)) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $ret; |
110
|
|
|
} catch (NoResultException $e) { |
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function findScalarFromSqlKey(string $key, array $params = [], string $connectionName = null) |
116
|
|
|
{ |
117
|
|
|
$sql = $this->helper->getSqlFromYamlKey($key, $params); |
118
|
|
|
|
119
|
|
|
$result = $this->getResultFromConnection($connectionName, $sql, $params); |
120
|
|
|
|
121
|
|
|
$ret = $result->fetchOne(); |
122
|
|
|
|
123
|
|
|
return $ret; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function getResultFromConnection(?string $connectionName, string $sql, array $params = []): Result |
127
|
|
|
{ |
128
|
|
|
$conn = $this->doctrine->getConnection($connectionName ?: $this->bundleConfig['default_connection']); |
129
|
|
|
|
130
|
|
|
$stmt = $conn->prepare($sql); |
131
|
|
|
|
132
|
|
|
foreach ($params as $key => $value) { |
133
|
|
|
$stmt->bindValue($key, $value); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$ret = $stmt->executeQuery(); |
137
|
|
|
|
138
|
|
|
return $ret; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|