packages/sql-semantics-mysql/src/Statement/Name/CurrentUser.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Name;
6
7use SqlSemantics\Platform\MySql\Model\Rendering\Output;
8use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
9use SqlSemantics\Platform\MySql\Statement\Expression\OptionalWords;
10
11/**
12 * The account of the session, written `CURRENT_USER` or `CURRENT_USER()` at an account position.
13 *
14 * Source: https://dev.mysql.com/doc/refman/8.4/en/information-functions.html#function_current-user.
15 *
16 * @visibility public
17 * @example Rendering the current user
18 *     $out = new \SqlSemantics\Platform\MySql\Model\Rendering\Output(new \SqlSemantics\Platform\MySql\Rendering\Codec(\SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease::MySql847));
19 *     (new \SqlSemantics\Platform\MySql\Statement\Name\CurrentUser())->render($out);
20 *     $out->pieces()[0]->text // => 'CURRENT_USER'
21 */
22final class CurrentUser implements Account
23{
24    use Snapshot;
25
26    /**
27     * @param OptionalWords $parentheses Whether the optional empty parentheses are written
28     */
29    public function __construct(public readonly OptionalWords $parentheses = OptionalWords::Omitted)
30    {
31    }
32
33    /**
34     * Writes the keyword and the parentheses when they are written.
35     */
36    public function render(Output $out): void
37    {
38        $out->keyword('CURRENT_USER');
39        if ($this->parentheses === OptionalWords::Written) {
40            $out->glue()->symbol('(')->symbol(')');
41        }
42    }
43}
44