packages/sql-semantics-mysql/src/Model/Statement/Reference/Column/AmbiguousColumn.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Model\Statement\Reference\Column;
6
7use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
8use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
9use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11
12/**
13 * A name several visible slots have at the same precedence; none is chosen.
14 *
15 * @visibility public
16 * @example Keeping every candidate of an ambiguous column
17 * $semantics = new \SqlSemantics\Platform\MySql\Semantics();
18 * $declarations = $semantics->context([$semantics->analyze('CREATE TABLE t (a INTEGER)'), $semantics->analyze('CREATE TABLE u (a INTEGER)')]);
19 * $semantics->analyze('SELECT a FROM t, u', $declarations) // throws \SqlSemantics\Platform\MySql\Model\Diagnostic\SemanticException: Column a is ambiguous.
20 */
21final class AmbiguousColumn implements Resolution, Diagnostic
22{
23 use Snapshot;
24
25 /**
26 * @var list<ResolvedColumn> The equally ranked candidates
27 */
28 public readonly array $candidates;
29
30 /**
31 * @param Name $name The column name
32 * @param list<ResolvedColumn> $candidates The equally ranked candidates; at least two
33 */
34 public function __construct(public readonly Name $name, array $candidates)
35 {
36 $this->candidates = Check::listOf($candidates, ResolvedColumn::class, 'An ambiguous column has at least two candidates.', 2);
37 }
38
39 /**
40 * Describes the problem.
41 */
42 public function message(): string
43 {
44 return 'Column ' . $this->name->value . ' is ambiguous.';
45 }
46}
47