packages/sql-semantics-mysql/src/Statement/Server/Transaction/Savepoint.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Server\Transaction;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Model\Statement\Statement;
13
14/**
15 * `SAVEPOINT name`: a request to set a named savepoint in the current transaction.
16 *
17 * Mirrors SQLCOM_SAVEPOINT. Rule: MYSQL-SAVEPOINT-001. The savepoint is a name of the
18 * session's current transaction, which no declaration context holds; the
19 * server compares savepoint names case-insensitively. A savepoint of the same name replaces the earlier one. The statement
20 * names no relation and has no facts.
21 * Source: https://dev.mysql.com/doc/refman/8.4/en/savepoint.html.
22 * Status: Implemented.
23 *
24 * @visibility public
25 * @example Naming the savepoint
26 *     $statement = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('savepoint sp1');
27 *     [$statement->toString(), $statement->statement->savepoint->value] // => ['SAVEPOINT sp1', 'sp1']
28 */
29final class Savepoint implements Statement
30{
31    use Snapshot;
32
33    /**
34     * @param Name $savepoint The savepoint name
35     */
36    public function __construct(public readonly Name $savepoint)
37    {
38    }
39
40    /**
41     * Has nothing to derive: the request names no relation and no value.
42     */
43    public function deriveStatement(Derivation $derivation): void
44    {
45    }
46
47    /**
48     * Writes the request.
49     */
50    public function render(Output $out): void
51    {
52        $out->keyword('SAVEPOINT')->name($this->savepoint, NameUse::Identifier);
53    }
54}
55