packages/sql-semantics-sqlite/src/Statement/Expression/Window/WindowDefinition.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Statement\Expression\Window;
6
7use SqlSemantics\Platform\Sqlite\Model\Contract\NameUse;
8use SqlSemantics\Platform\Sqlite\Model\Rendering\Output;
9use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Node;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
12
13/**
14 * A named window of the WINDOW clause of a selection.
15 *
16 * Source: https://sqlite.org/windowfunctions.html#window_chaining.
17 *
18 * @visibility public
19 * @example Reading a named window
20 * $query = (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('SELECT rank() OVER w FROM t WINDOW w AS (ORDER BY a)');
21 * [$query->statement->windows[0]->name->value, count($query->statement->windows[0]->window->order)] // => ['w', 1]
22 */
23final class WindowDefinition implements Node
24{
25 use Snapshot;
26
27 /**
28 * @param Name $name The window name
29 * @param WindowSpec $window The specification
30 */
31 public function __construct(public readonly Name $name, public readonly WindowSpec $window)
32 {
33 }
34
35 /**
36 * Writes the definition.
37 */
38 public function render(Output $out): void
39 {
40 $out->name($this->name, NameUse::Label)->keyword('AS')->symbol('(')->node($this->window)->symbol(')');
41 }
42}
43