packages/sql-semantics-postgres/resources/models/Value/WithinGroupClauseWithWithinGroupPSortClause_6155d1e8.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Statement\Model\PostgreSql\Value;
6
7/**
8 * The within_group_clause SQL value; generated by bin/build-models.php.
9 *
10 * @visibility public
11 * @example Accept a structured SQL value
12 * $write = static fn (\SqlSemantics\Statement\Model\PostgreSql\Value\WithinGroupClauseWithWithinGroupPSortClause_6155d1e8 $value): string => \SqlSemantics\Statement\Writer::render($value);
13 * $write instanceof \Closure // => true
14 */
15final class WithinGroupClauseWithWithinGroupPSortClause_6155d1e8 implements \SqlSemantics\Statement\Model\PostgreSql\Role\WithinGroupClauseForm
16{
17 use \SqlSemantics\Statement\Assertion;
18
19 /**
20 * Supplies the SQL values of this form; comments are kept by the position of the symbol each precedes.
21 */
22 public function __construct(
23 public readonly \SqlSemantics\Statement\Model\PostgreSql\Role\SortClauseForm $orderBy,
24 public readonly \SqlSemantics\Statement\Comments $comments = new \SqlSemantics\Statement\Comments(),
25 ) {
26 $this->assert(\SqlSemantics\Statement\Model\PostgreSql\Contract\Contracts::contains($orderBy), 'The orderBy must be a generated immutable SQL value.');
27 }
28
29 /**
30 * Writes SQL entirely from this value's fields.
31 */
32 public function write(\SqlSemantics\Statement\Writer $writer): void
33 {
34 $writer->comments($this->comments, 0);
35 $writer->append('WITHIN');
36 $writer->comments($this->comments, 1);
37 $writer->append('GROUP');
38 $writer->comments($this->comments, 2);
39 $writer->append('(');
40 $writer->comments($this->comments, 3);
41 $this->orderBy->write($writer);
42 $writer->comments($this->comments, 4);
43 $writer->append(')');
44 }
45
46 /**
47 * Returns a copy with a new orderBy, preserving every other field.
48 */
49 public function withOrderBy(\SqlSemantics\Statement\Model\PostgreSql\Role\SortClauseForm $orderBy): self
50 {
51 return new self($orderBy, $this->comments);
52 }
53
54 /**
55 * Returns a copy with a new comments, preserving every other field.
56 */
57 public function withComments(\SqlSemantics\Statement\Comments $comments): self
58 {
59 return new self($this->orderBy, $comments);
60 }
61}
62