classMySqlParser
final class MySqlParser
implements SqlParser
@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.
$parser = new \SqlParser\MySql\MySqlParser();
$tree = $parser->parse('SELECT id FROM users WHERE id = ?');
$tree->name // => 'start_entry'
count($tree->find('table_reference')) // => 1$sql = "SELECT id FROM users -- everyone\n";
$parser = new \SqlParser\MySql\MySqlParser();
$parser->parse($sql)->toString() === $sql // => true$parser = new \SqlParser\MySql\MySqlParser('mysql-5.7.44');
$parser->version() // => 'mysql-5.7.44'new \SqlParser\MySql\MySqlParser('mysql-4.1.0') // throws \RuntimeException: UnsupportedMethods§
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
$version | string|null | Release tag such as `mysql-8.4.7`, or null for the newest shipped |
$mode | SqlMode | The `sql_mode` flags that change tokenization |
$registry | VersionRegistry | Record of shipped releases |
Throws
RuntimeException When the release is not shipped or its resources are missingCalls 7
- new SqlMode line 62
- new VersionRegistry line 62
- static-call MySqlVersion::resolve() line 64
- method-call TableFile::load() line 65
- new TableFile line 65
- new MySqlLexer line 66
- static-call KeywordTable::load() line 66
public function version(): stringAnswers the release the parser reads for.
Returns
string Release tag such as `mysql-8.4.7`Test cases 1
Calls 1
- method-call MySqlVersion::name() line 76
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
$sql | string | The SQL text |
Returns
list<Token> The tokens in text orderThrows
LexicalException When the text holds something no token starts withTest cases 3
Called from 1
Calls 3
- method-call TerminalIndex::tokens() line 94
- new TerminalIndex line 94
- method-call MySqlLexer::scan() line 94
public function parse(string $sql): NodeParses one statement.
Parameters
$sql | string | The SQL text |
Returns
Node The tree, rooted at the grammar's start symbol, holding every byte of the textThrows
LexicalException When the text holds something no token starts withSyntaxException When the statement is not in the grammar of the releaseTest cases 10
ParseBench::benchParsecallsDocumentTest::testSignatureDistinguishesHintsAndExecutableCommentscallsRendererTest::testDocumentSelectsDialectSpecificSpellingscallsRendererTest::testRenderHonorsMysqlLexicalModescallsShapeTest::testOfDistinguishesAliasAndStringConcatenationGrammarOwnerscallsMySqlParserTest::testParsecallsMySqlParserTest::testParseFollowsTheChosenReleasecallsMySqlParserTest::testParseRejectsAStatementThatEndsEarlycallsMySqlParserTest::testParseRejectsSyntaxTheReleaseLackscallsMySqlParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecalls
Calls 3
- method-call LrParser::parse() line 109
- new LrParser line 109
- method-call MySqlParser::tokenize() line 109
public static function versions(): list<string>Answers every release tag the package ships a MySQL grammar for, oldest first.
Returns
list<string> Release tagsTest cases 5
Calls 3
- method-call VersionRegistry::names() line 119
- new VersionRegistry line 119
- class-const MySqlVersion::DIALECT() line 119
Private surface 3§
Implementation details, listed for orientation only.
private MySqlVersion $versionprivate ParseTable $tableprivate MySqlLexer $lexerTest cases 31§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 11
MySqlParserTest::testParsecallsMySqlParserTest::testParseFollowsTheChosenReleasecallsMySqlParserTest::testParseRejectsAStatementThatEndsEarlycallsMySqlParserTest::testParseRejectsSyntaxTheReleaseLackscallsMySqlParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecallsMySqlParserTest::testTokenizecallsMySqlParserTest::testTokenizeHonoursTheModecallsMySqlParserTest::testTokenizeRejectsAnUnterminatedStringcallsMySqlParserTest::testVersioncallsMySqlParserTest::testVersionRejectsAnUnknownReleasecallsMySqlParserTest::testVersionscalls
Other tests reaching this symbol 20
ParseBench::benchLoadTablecallsParseBench::benchParsecallsParseBench::setUpcallsFormatTargetcallsDocumentTest::testSignatureDistinguishesHintsAndExecutableCommentscallsGroupingTest::providerParserscallsRendererTest::providerCanonicalFormscallsRendererTest::testDocumentSelectsDialectSpecificSpellingscallsRendererTest::testRenderHonorsMysqlLexicalModescallsShapeTest::testOfDistinguishesAliasAndStringConcatenationGrammarOwnerscallsDialectFactoryTest::testForParserUsesEachBuiltInImplementationcallsFormatterTest::providerExpandedLayoutscallsFormatterTest::providerGeneratedStatementscallsFormatterTest::providerPreservationcallsFormatterTest::providerStylescallsFormatterTest::providerVersionscallsFormatterTest::testFormatEveryGrammarReleasecallsFormatterTest::testFormatKeepsHostnamesAdjacentToAtSignscallsFormatterTest::testFormatPreservesMysqlSqlModecallsSqlParserTest::providerParserscalls