packages/sql-semantics-sqlite/src/Statement/Schema/Limit/ProhibitedExpression.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Statement\Schema\Limit;
6
7use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\Diagnostic;
8use SqlSemantics\Platform\Sqlite\Model\Statement\Snapshot;
9
10/**
11 * An expression of a definition that uses a construct SQLite rejects at its position.
12 *
13 * SQLite reports "parameters prohibited in CHECK constraints" and the like
14 * when the definition is prepared.
15 * Source: https://sqlite.org/lang_createtable.html#check_constraints,
16 * https://sqlite.org/partialindex.html, https://sqlite.org/expridx.html,
17 * https://sqlite.org/gencol.html.
18 *
19 * @visibility public
20 * @example Reporting a subquery in a partial index condition
21 *     (new \SqlSemantics\Platform\Sqlite\Semantics())->analyze('CREATE INDEX i ON t (a) WHERE a IN (SELECT 1)') // throws \SqlSemantics\Platform\Sqlite\Model\Diagnostic\SemanticException: Subqueries are prohibited in partial index WHERE clauses.
22 */
23final class ProhibitedExpression implements Diagnostic
24{
25    use Snapshot;
26
27    /**
28     * @param ProhibitedConstruct $construct What the expression uses
29     * @param DefinitionPosition $position Where it is used
30     */
31    public function __construct(public readonly ProhibitedConstruct $construct, public readonly DefinitionPosition $position)
32    {
33    }
34
35    /**
36     * Describes the problem.
37     */
38    public function message(): string
39    {
40        $construct = $this->construct === ProhibitedConstruct::DotOperator ? 'The "." operator is' : ucfirst($this->construct->value) . ' are';
41
42        return $construct . ' prohibited in ' . $this->position->value . '.';
43    }
44}
45