Lines
100.00%
6 / 6
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| PostgresGrammar | 100.00% | 6 / 6 | 100.00% | 3 / 3 | 6 | 100.00% | 1 / 1 | |||
| wrap | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| usesReturning | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| compileLock | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace BlueprintAU\Radiant\Database\Grammars; | |
| 6 | ||
| 7 | use BlueprintAU\Radiant\Database\Query\Enums\LockType; | |
| 8 | use BlueprintAU\Radiant\Database\Query\QueryBuilder; | |
| 9 | ||
| 10 | /** | |
| 11 | * The Postgres dialect of the SQL Grammar. | |
| 12 | * | |
| 13 | * Identifiers are quoted with double quotes (embedded quotes doubled). | |
| 14 | * Postgres supports `INSERT ... RETURNING`, so {@see usesReturning()} returns | |
| 15 | * true and {@see compileInsertForId()} compiles the clause in. Row locks | |
| 16 | * render as `for update` and `for share`; a bare offset is | |
| 17 | * valid without a limit. | |
| 18 | */ | |
| 19 | final class PostgresGrammar extends Grammar | |
| 20 | { | |
| 21 | /** | |
| 22 | * Wrap an identifier in Postgres double quotes. | |
| 23 | * | |
| 24 | * @param string $value | |
| 25 | * @return string | |
| 26 | */ | |
| 27 | protected function wrap(string $value): string | |
| 28 | { | |
| 29 | return '"' . str_replace('"', '""', $value) . '"'; | |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| 33 | * Whether the dialect supports `INSERT ... RETURNING`. | |
| 34 | * | |
| 35 | * @return bool | |
| 36 | */ | |
| 37 | public function usesReturning(): bool | |
| 38 | { | |
| 39 | return true; | |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * Compile the row lock for Postgres. | |
| 44 | * | |
| 45 | * @param QueryBuilder $builder | |
| 46 | * @return string | |
| 47 | */ | |
| 48 | protected function compileLock(QueryBuilder $builder): string | |
| 49 | { | |
| 50 | return match ($builder->getLock()) { | |
| 51 | null => '', | |
| 52 | LockType::Update => 'FOR UPDATE', | |
| 53 | LockType::Shared => 'FOR SHARE', | |
| 54 | }; | |
| 55 | } | |
| 56 | } |