final class MySqlParser
    implements SqlParser
Public API: explicitly declared with @visibility public.

Parses MySQL statements with the grammar of a chosen server release.

The parse table is built from the sql_yacc.yy of that release and the lexer follows its sql_lex.cc, so the tree names the nonterminals of the official grammar. One statement is parsed at a time, as the server does.

Parsing with the default releasedoctest
$parser = new \SqlParser\MySql\MySqlParser();
    $tree = $parser->parse('SELECT id FROM users WHERE id = ?');
    $tree->name // => 'start_entry'
    count($tree->find('table_reference')) // => 1
Writing a parsed statement backdoctest
$sql = "SELECT id FROM users -- everyone\n";
    $parser = new \SqlParser\MySql\MySqlParser();
    $parser->parse($sql)->toString() === $sql // => true
Choosing a releasedoctest
$parser = new \SqlParser\MySql\MySqlParser('mysql-5.7.44');
    $parser->version() // => 'mysql-5.7.44'
Rejecting an unsupported releasedoctest
new \SqlParser\MySql\MySqlParser('mysql-4.1.0') // throws \RuntimeException: Unsupported

Methods§

public function __construct(
    string|null $version = null,
    SqlMode $mode = new \SqlParser\MySql\SqlMode(),
    VersionRegistry $registry = new \SqlParser\Resource\VersionRegistry(),
)

Loads the parser of one release.

Parameters

$versionstring|nullRelease tag such as `mysql-8.4.7`, or null for the newest shipped
$modeSqlModeThe `sql_mode` flags that change tokenization
$registryVersionRegistryRecord of shipped releases

Throws

RuntimeException When the release is not shipped or its resources are missing
Calls 7
public function version(): string

Answers the release the parser reads for.

Returns

string Release tag such as `mysql-8.4.7`
Test cases 1
Calls 1
public function tokenize(string $sql): list<Token>

Reads SQL text into the terminals of the grammar, the end marker last.

A token carries the whitespace and comments skipped before it and the end marker what follows the last of them, so the tokens hold every byte of the text.

Parameters

$sqlstringThe SQL text

Returns

list<Token> The tokens in text order

Throws

LexicalException When the text holds something no token starts with
Test cases 3
Called from 1
Calls 3
public static function versions(): list<string>

Answers every release tag the package ships a MySQL grammar for, oldest first.

Returns

list<string> Release tags
Test cases 5
Calls 3

Private surface 3§

Implementation details, listed for orientation only.

private MySqlVersion $version
private ParseTable $table
private MySqlLexer $lexer

Test cases 31§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 11
Other tests reaching this symbol 20

Relations§

Instantiated in 2
Type checks 1