packages/sql-semantics-mysql/src/Mode.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql;
6
7use SqlParser\MySql\SqlMode;
8
9/**
10 * The parts of a MySQL session's `sql_mode` that change how SQL text is read.
11 *
12 * Five modes change tokenization: `ANSI_QUOTES`, `PIPES_AS_CONCAT`,
13 * `HIGH_NOT_PRECEDENCE`, `NO_BACKSLASH_ESCAPES` and `IGNORE_SPACE`. A
14 * combination mode such as `ANSI` turns on the ones it includes. Every other
15 * mode name, such as `STRICT_TRANS_TABLES` or `REAL_AS_FLOAT`, is read and not
16 * recorded: it changes no token, and a fact that depends on it names the
17 * session state it is missing.
18 * Source: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html.
19 *
20 * @visibility public
21 * @example Reading the mode a session reports
22 *     \SqlSemantics\Platform\MySql\Mode::fromString('STRICT_TRANS_TABLES,ansi_quotes,NO_BACKSLASH_ESCAPES')->toString() // => 'ANSI_QUOTES,NO_BACKSLASH_ESCAPES'
23 * @example Reading double quotes as identifiers
24 *     $semantics = new \SqlSemantics\Platform\MySql\Semantics(null, \SqlSemantics\Platform\MySql\Mode::fromString('ANSI_QUOTES'));
25 *     $semantics->analyze('SELECT "order" FROM t')->toString() // => 'SELECT `order` FROM t'
26 */
27final class Mode implements \SqlSemantics\Contract\Mode
28{
29    /**
30     * @param bool $ansiQuotes Whether double quotes delimit identifiers instead of strings
31     * @param bool $pipesAsConcat Whether `||` is string concatenation instead of `OR`
32     * @param bool $highNotPrecedence Whether `NOT` binds as tightly as `!`
33     * @param bool $noBackslashEscapes Whether a backslash in a string is an ordinary character
34     * @param bool $ignoreSpace Whether a function name may be followed by spaces before its parenthesis
35     */
36    public function __construct(
37        public readonly bool $ansiQuotes = false,
38        public readonly bool $pipesAsConcat = false,
39        public readonly bool $highNotPrecedence = false,
40        public readonly bool $noBackslashEscapes = false,
41        public readonly bool $ignoreSpace = false,
42    ) {
43    }
44
45    /**
46     * Reads a `sql_mode` value as `SELECT @@SESSION.sql_mode` answers it.
47     */
48    public static function fromString(string $modes): self
49    {
50        $flags = SqlMode::fromString($modes);
51
52        return new self($flags->ansiQuotes, $flags->pipesAsConcat, $flags->highNotPrecedence, $flags->noBackslashEscapes, $flags->ignoreSpace);
53    }
54
55    /**
56     * Answers the recorded modes in the canonical spelling the language profile keeps.
57     */
58    public function toString(): string
59    {
60        $names = [];
61        foreach (['ANSI_QUOTES' => $this->ansiQuotes, 'PIPES_AS_CONCAT' => $this->pipesAsConcat, 'HIGH_NOT_PRECEDENCE' => $this->highNotPrecedence, 'NO_BACKSLASH_ESCAPES' => $this->noBackslashEscapes, 'IGNORE_SPACE' => $this->ignoreSpace] as $name => $enabled) {
62            if ($enabled) {
63                $names[] = $name;
64            }
65        }
66
67        return implode(',', $names);
68    }
69}
70