packages/sql-semantics-mysql/src/Statement/Table/Column/CommentAttribute.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Table\Column;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\MySql\Statement\Literal\Text;
12
13/**
14 * The COMMENT of a column.
15 *
16 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-table.html.
17 *
18 * @visibility public
19 * @example Reading the attribute of a column
20 *     $create = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('CREATE TABLE t (a INT COMMENT \'x\')');
21 *     $create->statement->elements[0]->specification->attributes[0] instanceof \SqlSemantics\Platform\MySql\Statement\Table\Column\CommentAttribute // => true
22 */
23final class CommentAttribute implements ColumnAttribute
24{
25    use Snapshot;
26
27    /**
28     * @param Text $comment The comment
29     */
30    public function __construct(public readonly Text $comment)
31    {
32    }
33
34    /**
35     * Derives nothing: the attribute holds no expression.
36     */
37    public function deriveAttribute(Derivation $derivation, Environment $scope): void
38    {
39    }
40
41    /**
42     * Writes the attribute.
43     */
44    public function render(Output $out): void
45    {
46        $out->keyword('COMMENT')->node($this->comment);
47    }
48}
49