summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php')
-rw-r--r--vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php94
1 files changed, 94 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php b/vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php
new file mode 100644
index 0000000..a30120e
--- /dev/null
+++ b/vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php
@@ -0,0 +1,94 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\SQL\Builder;
6
7use Doctrine\DBAL\Exception;
8use Doctrine\DBAL\Platforms\AbstractPlatform;
9use Doctrine\DBAL\Platforms\Exception\NotSupported;
10use Doctrine\DBAL\Query\ForUpdate\ConflictResolutionMode;
11use Doctrine\DBAL\Query\SelectQuery;
12
13use function count;
14use function implode;
15
16final class DefaultSelectSQLBuilder implements SelectSQLBuilder
17{
18 /** @internal The SQL builder should be instantiated only by database platforms. */
19 public function __construct(
20 private readonly AbstractPlatform $platform,
21 private readonly ?string $forUpdateSQL,
22 private readonly ?string $skipLockedSQL,
23 ) {
24 }
25
26 /** @throws Exception */
27 public function buildSQL(SelectQuery $query): string
28 {
29 $parts = ['SELECT'];
30
31 if ($query->isDistinct()) {
32 $parts[] = 'DISTINCT';
33 }
34
35 $parts[] = implode(', ', $query->getColumns());
36
37 $from = $query->getFrom();
38
39 if (count($from) > 0) {
40 $parts[] = 'FROM ' . implode(', ', $from);
41 }
42
43 $where = $query->getWhere();
44
45 if ($where !== null) {
46 $parts[] = 'WHERE ' . $where;
47 }
48
49 $groupBy = $query->getGroupBy();
50
51 if (count($groupBy) > 0) {
52 $parts[] = 'GROUP BY ' . implode(', ', $groupBy);
53 }
54
55 $having = $query->getHaving();
56
57 if ($having !== null) {
58 $parts[] = 'HAVING ' . $having;
59 }
60
61 $orderBy = $query->getOrderBy();
62
63 if (count($orderBy) > 0) {
64 $parts[] = 'ORDER BY ' . implode(', ', $orderBy);
65 }
66
67 $sql = implode(' ', $parts);
68 $limit = $query->getLimit();
69
70 if ($limit->isDefined()) {
71 $sql = $this->platform->modifyLimitQuery($sql, $limit->getMaxResults(), $limit->getFirstResult());
72 }
73
74 $forUpdate = $query->getForUpdate();
75
76 if ($forUpdate !== null) {
77 if ($this->forUpdateSQL === null) {
78 throw NotSupported::new('FOR UPDATE');
79 }
80
81 $sql .= ' ' . $this->forUpdateSQL;
82
83 if ($forUpdate->getConflictResolutionMode() === ConflictResolutionMode::SKIP_LOCKED) {
84 if ($this->skipLockedSQL === null) {
85 throw NotSupported::new('SKIP LOCKED');
86 }
87
88 $sql .= ' ' . $this->skipLockedSQL;
89 }
90 }
91
92 return $sql;
93 }
94}