diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php')
| -rw-r--r-- | vendor/doctrine/dbal/src/SQL/Builder/DefaultSelectSQLBuilder.php | 94 |
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 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\SQL\Builder; | ||
| 6 | |||
| 7 | use Doctrine\DBAL\Exception; | ||
| 8 | use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
| 9 | use Doctrine\DBAL\Platforms\Exception\NotSupported; | ||
| 10 | use Doctrine\DBAL\Query\ForUpdate\ConflictResolutionMode; | ||
| 11 | use Doctrine\DBAL\Query\SelectQuery; | ||
| 12 | |||
| 13 | use function count; | ||
| 14 | use function implode; | ||
| 15 | |||
| 16 | final 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 | } | ||
