packages/sql-semantics-mysql/src/Statement/Server/Problem/StorageProblem.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Server\Problem;
6
7use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
8use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
9
10/**
11 * A tablespace or log file group option that breaks a rule the server checks while it parses the statement.
12 *
13 * The server rejects the statement with the error of the rule.
14 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-tablespace.html.
15 *
16 * @visibility public
17 * @example Describing a repeated option
18 *     (new \SqlSemantics\Platform\MySql\Statement\Server\Problem\StorageProblem(\SqlSemantics\Platform\MySql\Statement\Server\Problem\StorageRule::RepeatedOption, 'COMMENT'))->message() // => 'The COMMENT option is written more than once (ER_FILEGROUP_OPTION_ONLY_ONCE).'
19 */
20final class StorageProblem implements Diagnostic
21{
22    use Snapshot;
23
24    /**
25     * @param StorageRule $rule The broken rule
26     * @param string $option The keyword of the option, such as COMMENT or INITIAL_SIZE
27     */
28    public function __construct(public readonly StorageRule $rule, public readonly string $option)
29    {
30    }
31
32    /**
33     * Describes the problem.
34     */
35    public function message(): string
36    {
37        $text = match ($this->rule) {
38            StorageRule::RepeatedOption => 'The ' . $this->option . ' option is written more than once',
39            StorageRule::WrongSize => 'The size of ' . $this->option . ' is a number with an optional K, M or G multiplier',
40            StorageRule::SizeOverflow => 'The size of ' . $this->option . ' overflows: the number before the multiplier must be below 2^31',
41        };
42
43        return $text . ' (' . $this->rule->value . ').';
44    }
45}
46