packages/sql-semantics-sqlite/src/Model/Statement/Fact/QueryFact.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Model\Statement\Fact;
6
7use SqlSemantics\Platform\Sqlite\Model\Diagnostic\Check;
8use SqlSemantics\Platform\Sqlite\Model\Diagnostic\InvalidConstruction;
9use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\DependentField;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\FieldLookup;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Fields;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OpenStar;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\RowShape;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
17
18/**
19 * What an operation derived for the output of one query.
20 *
21 * The projection lists the output fields in order. A star that missing
22 * declarations prevent from expanding stays a typed request and leaves the
23 * shape open; no field is invented for it.
24 *
25 * @visibility public
26 * @example Reading the output of a query
27 *     $query = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('SELECT 1 AS a, 2 AS b');
28 *     [count($query->facts->output->projection), $query->facts->output->shape->complete()] // => [2, true]
29 */
30final class QueryFact
31{
32    use Snapshot;
33
34    /**
35     * @var list<Field|OpenStar> The output fields and unexpandable stars in order
36     */
37    public readonly array $projection;
38
39    /**
40     * @var RowShape The output positions; open when a star could not be expanded
41     */
42    public readonly RowShape $shape;
43
44    /**
45     * @param array<array-key, object> $projection The output fields and unexpandable stars in order, as a list of Field and OpenStar
46     * @param Comparison $names How output names are compared
47     *
48     * @throws InvalidConstruction When the projection holds anything but fields and open stars
49     */
50    public function __construct(array $projection, public readonly Comparison $names)
51    {
52        $slots = [];
53        $missing = [];
54        $items = [];
55        foreach ($projection as $item) {
56            if ($item instanceof Field) {
57                $slots[] = $item->slot;
58            } elseif ($item instanceof OpenStar) {
59                array_push($missing, ...$item->missing);
60            } else {
61                throw new InvalidConstruction('A projection holds fields and open stars.');
62            }
63            $items[] = $item;
64        }
65        Check::input(array_is_list($projection), 'A projection is an ordered list.');
66        $this->projection = $items;
67        $this->shape = new RowShape($slots, $missing);
68    }
69
70    /**
71     * Answers the complete field list, or null while a star cannot be expanded.
72     */
73    public function fields(): ?Fields
74    {
75        $fields = [];
76        foreach ($this->projection as $item) {
77            if ($item instanceof OpenStar) {
78                return null;
79            }
80            $fields[] = $item;
81        }
82
83        return new Fields($fields, $this->names);
84    }
85
86    /**
87     * Looks an output field up by name without choosing among several or guessing in an open shape.
88     */
89    public function lookup(string $name): FieldLookup
90    {
91        $fields = $this->fields();
92        if ($fields !== null) {
93            return $fields->lookup($name);
94        }
95        $candidates = [];
96        foreach ($this->projection as $item) {
97            if ($item instanceof Field && $item->name !== null && $this->names->equal($item->name->value, $name)) {
98                $candidates[] = $item;
99            }
100        }
101
102        return new DependentField($name, $candidates, $this->shape->missing);
103    }
104}
105