packages/sql-semantics-postgres/src/Statement/Query/Clause/IntoPersistence.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Query\Clause;
6
7/**
8 * The kind of table SELECT INTO creates, as written.
9 *
10 * TEMP and TEMPORARY are the same request, and LOCAL and GLOBAL change
11 * nothing; the spelling is kept because the grammar keeps it. Permanent is the
12 * absence of any of these words.
13 * Source: https://www.postgresql.org/docs/17/sql-selectinto.html, https://www.postgresql.org/docs/17/sql-createtable.html.
14 *
15 * @visibility public
16 * @example Reading the persistence of a temporary target
17 *     \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\IntoPersistence::LocalTemp->temporary() // => true
18 */
19enum IntoPersistence: string
20{
21    case Permanent = '';
22    case Temporary = 'TEMPORARY';
23    case Temp = 'TEMP';
24    case LocalTemporary = 'LOCAL TEMPORARY';
25    case LocalTemp = 'LOCAL TEMP';
26    case GlobalTemporary = 'GLOBAL TEMPORARY';
27    case GlobalTemp = 'GLOBAL TEMP';
28    case Unlogged = 'UNLOGGED';
29
30    /**
31     * Tells whether the new table is temporary.
32     */
33    public function temporary(): bool
34    {
35        return match ($this) {
36            self::Permanent, self::Unlogged => false,
37            self::Temporary, self::Temp, self::LocalTemporary, self::LocalTemp, self::GlobalTemporary, self::GlobalTemp => true,
38        };
39    }
40
41    /**
42     * Answers the keywords as written, none for a permanent table.
43     *
44     * @return list<string>
45     */
46    public function keywords(): array
47    {
48        return $this === self::Permanent ? [] : explode(' ', $this->value);
49    }
50}
51