packages/sql-semantics-mysql/src/Statement/Expression/Predicate/Regexp.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Expression\Predicate;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
13use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
14use SqlSemantics\Platform\MySql\Rules\Expression\Operands;
15use SqlSemantics\Platform\MySql\Rules\Expression\Precedence;
16
17/**
18 * A regular expression match: `x [NOT] REGEXP pattern` (`Item_func_regex`, `Item_func_regexp_like` in 8.0 and later).
19 *
20 * RLIKE is the same keyword to the lexer and is written REGEXP. Both
21 * operands are bit_expr (MYSQL-PRECEDENCE-001).
22 *
23 * Rule: MYSQL-REGEXP-001. Facts: 1, 0 or NULL, an integer; it can be NULL
24 * when an operand can. Both operands take a single value. Terminates: the
25 * operands are strict parts.
26 * Source: https://dev.mysql.com/doc/refman/8.4/en/regexp.html#operator_regexp.
27 * Status: Implemented.
28 *
29 * @visibility public
30 * @example Writing RLIKE as REGEXP
31 *     (new \SqlSemantics\Platform\MySql\Semantics())->analyze("SELECT a FROM t WHERE a NOT RLIKE '^x'")->toString() // => "SELECT a FROM t WHERE a NOT REGEXP '^x'"
32 */
33final class Regexp implements Scalar
34{
35    use Snapshot;
36
37    /**
38     * @param Scalar $operand The matched expression
39     * @param Scalar $pattern The regular expression
40     * @param bool $negated Whether NOT is written before REGEXP
41     */
42    public function __construct(public readonly Scalar $operand, public readonly Scalar $pattern, public readonly bool $negated = false)
43    {
44        $precedence = new Precedence();
45        Check::input($precedence->fits($operand, Precedence::PREDICATE, Precedence::BIT_EXPR), 'The operand of REGEXP needs a grouping to keep its place.');
46        Check::input($precedence->admits($pattern, Precedence::BIT_EXPR), 'The pattern of REGEXP needs a grouping to keep its place.');
47    }
48
49    /**
50     * Derives both operands and combines their NULL facts.
51     */
52    public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
53    {
54        $operands = new Operands();
55        $operand = $operands->single($derivation->scalar($this->operand, $environment), $derivation);
56        $pattern = $operands->single($derivation->scalar($this->pattern, $environment), $derivation);
57
58        return $operands->truth($operand->nullability->propagate($pattern->nullability));
59    }
60
61    /**
62     * Writes the operand, the optional NOT, REGEXP and the pattern.
63     */
64    public function render(Output $out): void
65    {
66        $out->node($this->operand);
67        if ($this->negated) {
68            $out->keyword('NOT');
69        }
70        $out->keyword('REGEXP')->node($this->pattern);
71    }
72}
73