packages/sql-semantics-postgres/src/Statement/Name/DottedName.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Name;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
14use SqlSemantics\Platform\PostgreSql\Rendering\Spelling;
15
16/**
17 * A name of one or more parts separated by dots, as `any_name` and `func_name` write it.
18 *
19 * The grammar does not limit the number of parts; how many an object kind
20 * accepts is decided when the name is looked up, so the parts are kept as
21 * written. Up to three parts read as a relation-style name.
22 * Source: https://www.postgresql.org/docs/17/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS.
23 *
24 * @visibility public
25 * @example Reading a schema-qualified type name
26 *     $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("SELECT pg_catalog.int4 '1'");
27 *     $name = $query->statement->targets[0]->expression->type->designation->name;
28 *     [$name->qualified()?->schema?->value, $name->last()->value] // => ['pg_catalog', 'int4']
29 */
30final class DottedName implements ObjectReference
31{
32    use Snapshot;
33
34    /**
35     * @var non-empty-list<Name> The parts from the outermost qualifier to the object name
36     */
37    public readonly array $parts;
38
39    /**
40     * @param list<Name> $parts The parts from the outermost qualifier to the object name; at least one
41     */
42    public function __construct(array $parts)
43    {
44        $this->parts = Check::listOf($parts, Name::class, 'A dotted name has at least one part.', 1);
45    }
46
47    /**
48     * Answers the object name: the last part.
49     */
50    public function last(): Name
51    {
52        return $this->parts[count($this->parts) - 1];
53    }
54
55    /**
56     * Answers the name as object, schema and catalog, or null when it has more than three parts.
57     */
58    public function qualified(): ?QualifiedName
59    {
60        return match (count($this->parts)) {
61            1 => new QualifiedName($this->parts[0]),
62            2 => new QualifiedName($this->parts[1], $this->parts[0]),
63            3 => new QualifiedName($this->parts[2], $this->parts[1], $this->parts[0]),
64            default => null,
65        };
66    }
67
68    /**
69     * Derives nothing: a name holds no expression.
70     */
71    public function deriveClause(Derivation $derivation, Environment $environment): void
72    {
73    }
74
75    /**
76     * Writes the parts separated by dots.
77     */
78    public function render(Output $out): void
79    {
80        (new Spelling())->dotted($out, $this->parts);
81    }
82}
83