packages/sql-semantics-mysql/src/Statement/Relation/Join/JoinOperator.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Relation\Join;
6
7/**
8 * The operator of a join, by the keywords that write it.
9 *
10 * JOIN, INNER JOIN and CROSS JOIN are equivalent inner joins in MySQL and
11 * are kept as written; STRAIGHT_JOIN is an inner join that reads its left
12 * operand first. The optional OUTER after LEFT and RIGHT and the optional
13 * INNER after NATURAL do not change the join, and are kept as written
14 * because MySQL names an unaliased select list expression, which can hold
15 * a join in a subquery, after its text.
16 * Source: https://dev.mysql.com/doc/refman/8.4/en/join.html.
17 *
18 * @visibility public
19 * @example Reading the keywords of a join operator
20 *     \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::NaturalLeft->keywords() // => ['NATURAL', 'LEFT', 'JOIN']
21 */
22enum JoinOperator
23{
24    case Join;
25    case Inner;
26    case Cross;
27    case StraightJoin;
28    case Left;
29    case Right;
30    case Natural;
31    case NaturalLeft;
32    case NaturalRight;
33    case LeftOuter;
34    case RightOuter;
35    case NaturalInner;
36    case NaturalLeftOuter;
37    case NaturalRightOuter;
38
39    /**
40     * Answers the keywords the operator is written with.
41     *
42     * @return list<string>
43     * @example Reading the keywords of STRAIGHT_JOIN
44     *     \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::StraightJoin->keywords() // => ['STRAIGHT_JOIN']
45     */
46    public function keywords(): array
47    {
48        return match ($this) {
49            self::Join => ['JOIN'],
50            self::Inner => ['INNER', 'JOIN'],
51            self::Cross => ['CROSS', 'JOIN'],
52            self::StraightJoin => ['STRAIGHT_JOIN'],
53            self::Left => ['LEFT', 'JOIN'],
54            self::Right => ['RIGHT', 'JOIN'],
55            self::Natural => ['NATURAL', 'JOIN'],
56            self::NaturalLeft => ['NATURAL', 'LEFT', 'JOIN'],
57            self::NaturalRight => ['NATURAL', 'RIGHT', 'JOIN'],
58            self::LeftOuter => ['LEFT', 'OUTER', 'JOIN'],
59            self::RightOuter => ['RIGHT', 'OUTER', 'JOIN'],
60            self::NaturalInner => ['NATURAL', 'INNER', 'JOIN'],
61            self::NaturalLeftOuter => ['NATURAL', 'LEFT', 'OUTER', 'JOIN'],
62            self::NaturalRightOuter => ['NATURAL', 'RIGHT', 'OUTER', 'JOIN'],
63        };
64    }
65
66    /**
67     * Tells whether the join merges the columns its operands have in common.
68     *
69     * @example Telling a natural join
70     *     \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::Natural->natural() // => true
71     */
72    public function natural(): bool
73    {
74        return in_array($this, [self::Natural, self::NaturalLeft, self::NaturalRight, self::NaturalInner, self::NaturalLeftOuter, self::NaturalRightOuter], true);
75    }
76
77    /**
78     * Tells whether the join keeps every row of its left operand, extending the right one with NULLs.
79     *
80     * @example Telling a left join
81     *     \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::Left->keepsLeft() // => true
82     */
83    public function keepsLeft(): bool
84    {
85        return in_array($this, [self::Left, self::NaturalLeft, self::LeftOuter, self::NaturalLeftOuter], true);
86    }
87
88    /**
89     * Tells whether the join keeps every row of its right operand, extending the left one with NULLs.
90     *
91     * @example Telling a right join
92     *     \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::NaturalRight->keepsRight() // => true
93     */
94    public function keepsRight(): bool
95    {
96        return in_array($this, [self::Right, self::NaturalRight, self::RightOuter, self::NaturalRightOuter], true);
97    }
98
99    /**
100     * Tells whether the grammar requires an ON or USING condition after the right operand.
101     *
102     * @example Telling that a left join needs a condition
103     *     \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::Left->conditioned() // => true
104     */
105    public function conditioned(): bool
106    {
107        return in_array($this, [self::Left, self::Right, self::LeftOuter, self::RightOuter], true);
108    }
109
110    /**
111     * Answers the operator written with the optional OUTER or INNER word; an operator without such a word answers itself.
112     *
113     * @example Reading LEFT OUTER JOIN
114     *     \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::Left->worded() // => \SqlSemantics\Platform\MySql\Statement\Relation\Join\JoinOperator::LeftOuter
115     */
116    public function worded(): self
117    {
118        return match ($this) {
119            self::Left => self::LeftOuter,
120            self::Right => self::RightOuter,
121            self::Natural => self::NaturalInner,
122            self::NaturalLeft => self::NaturalLeftOuter,
123            self::NaturalRight => self::NaturalRightOuter,
124            self::Join, self::Inner, self::Cross, self::StraightJoin, self::LeftOuter, self::RightOuter, self::NaturalInner, self::NaturalLeftOuter, self::NaturalRightOuter => $this,
125        };
126    }
127}
128