packages/sql-semantics-mysql/src/Statement/Expression/LogicalOperator.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Expression;
6
7use SqlSemantics\Platform\MySql\Rules\Expression\Precedence;
8
9/**
10 * The binary logical operators of MySQL.
11 *
12 * `&&` is a synonym of AND and, unless PIPES_AS_CONCAT is set, `||` is a
13 * synonym of OR; both are written in their keyword form. Each case holds
14 * the keyword.
15 * Source: https://dev.mysql.com/doc/refman/8.4/en/logical-operators.html.
16 *
17 * @visibility public
18 * @example Reading the operator of a disjunction
19 *     $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT a FROM t WHERE a || b');
20 *     $query->statement->where->operator // => \SqlSemantics\Platform\MySql\Statement\Expression\LogicalOperator::Or
21 */
22enum LogicalOperator: string
23{
24    case Or = 'OR';
25    case Xor = 'XOR';
26    case And = 'AND';
27
28    /**
29     * Answers the binding level of the operator on the scale of MYSQL-PRECEDENCE-001.
30     */
31    public function level(): int
32    {
33        return match ($this) {
34            self::Or => Precedence::DISJUNCTION,
35            self::Xor => Precedence::EXCLUSION,
36            self::And => Precedence::CONJUNCTION,
37        };
38    }
39}
40