|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ReadModel\Bridge\Doctrine\Query; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
|
7
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
8
|
|
|
use ReadModel\Bridge\Doctrine\ApplyFilters; |
|
9
|
|
|
use ReadModel\Bridge\Doctrine\DbalPaginator; |
|
10
|
|
|
use ReadModel\Filters\Filters; |
|
11
|
|
|
use ReadModel\NotFoundException; |
|
12
|
|
|
use ReadModel\Paginator; |
|
13
|
|
|
|
|
14
|
|
|
abstract class DbalQuery |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Connection */ |
|
17
|
|
|
protected $connection; |
|
18
|
|
|
|
|
19
|
|
|
/** @var int */ |
|
20
|
|
|
protected $defaultLimit = null; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
protected $columnMapping = []; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(Connection $connection) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->connection = $connection; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function createQueryBuilder(): QueryBuilder |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->connection->createQueryBuilder(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function createPaginator(QueryBuilder $qb, int $limit = null, $offset = 0, string ...$parameters): Paginator |
|
36
|
|
|
{ |
|
37
|
|
|
$limit = $limit ?? $this->defaultLimit; |
|
38
|
|
|
$this->transformParameters($qb, ...$parameters); |
|
39
|
|
|
|
|
40
|
|
|
return new DbalPaginator($qb, $limit, $offset); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function createPaginatorForFilters(QueryBuilder $qb, Filters $filters, string ...$parameters): Paginator |
|
44
|
|
|
{ |
|
45
|
|
|
$paginator = $this->createPaginator($qb, $filters->limit(), $filters->offset(), ...$parameters); |
|
46
|
|
|
$filters->addMetaToPaginator($paginator); |
|
47
|
|
|
|
|
48
|
|
|
return $paginator; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function applyFilters(QueryBuilder $qb, Filters $filters): void |
|
52
|
|
|
{ |
|
53
|
|
|
(new ApplyFilters($this->columnMapping))($qb, $filters); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function getResult(QueryBuilder $qb, string ...$parameters): array |
|
57
|
|
|
{ |
|
58
|
|
|
$this->transformParameters($qb, ...$parameters); |
|
59
|
|
|
|
|
60
|
|
|
return $qb->execute()->fetchAll(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function getScalarResult(QueryBuilder $qb, string ...$parameters): array |
|
64
|
|
|
{ |
|
65
|
|
|
return array_map(function (array $row) { |
|
66
|
|
|
return $this->getColumn($row); |
|
67
|
|
|
}, $this->getResult($qb, ...$parameters)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function getSingleResult(QueryBuilder $qb, string ...$parameters): array |
|
71
|
|
|
{ |
|
72
|
|
|
$this->transformParameters($qb, ...$parameters); |
|
73
|
|
|
$qb->setMaxResults(1); |
|
74
|
|
|
$result = $qb->execute()->fetch(); |
|
75
|
|
|
|
|
76
|
|
|
if ($result === false) { |
|
77
|
|
|
throw new NotFoundException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function getSingleScalarResult(QueryBuilder $qb, ...$parameters) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getColumn( |
|
86
|
|
|
$this->getSingleResult($qb, ...$parameters) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function transformParameters(QueryBuilder $qb, string ...$parameters): void |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
// do nothing, you can override this method or use trait TransformUuidToBytes |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function getColumn(array $result) |
|
96
|
|
|
{ |
|
97
|
|
|
if (count($result) > 1) { |
|
98
|
|
|
throw new TooManyColumnsException(array_keys($result)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return array_shift($result); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.