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: Unsupported$parser = new \SqlParser\MySql\MySqlParser(parameters: \SqlParser\Lexer\ParameterSyntax::Named);
$parser->tokenize('SELECT id FROM users WHERE id = :id')[7]->name // => 'PARAM_MARKER'Methods§
public function __construct(
string|null $version = null,
SqlMode $mode = new \SqlParser\MySql\SqlMode(),
VersionRegistry $registry = new \SqlParser\Resource\VersionRegistry(),
ParameterSyntax $parameters = ParameterSyntax::Native,
)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 |
$parameters | ParameterSyntax | Which parameter markers are read; the server reads only `?` |
Throws
RuntimeException When the release is not shipped or its resources are missingCalls 8
- new SqlMode line 67
- new VersionRegistry line 67
- class-const ParameterSyntax::Native() line 67
- static-call MySqlVersion::resolve() line 69
- method-call TableFile::load() line 70
- new TableFile line 70
- new MySqlLexer line 71
- static-call KeywordTable::load() line 71
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 81
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 4
Called from 1
Calls 3
- method-call TerminalIndex::tokens() line 99
- new TerminalIndex line 99
- method-call MySqlLexer::scan() line 99
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 12
ParseBench::benchParsecallsDocumentTest::testSignatureDistinguishesHintsAndExecutableCommentscallsRendererTest::testDocumentSelectsDialectSpecificSpellingscallsRendererTest::testRenderHonorsMysqlLexicalModescallsShapeTest::testOfDistinguishesAliasAndStringConcatenationGrammarOwnerscallsOptimizerHintsTest::testFirstFindsTheHintCommentAfterTheStatementKeywordcallsMySqlParserTest::testParsecallsMySqlParserTest::testParseFollowsTheChosenReleasecallsMySqlParserTest::testParseReadsNamedParametersUnderTheNamedSyntaxcallsMySqlParserTest::testParseRejectsAStatementThatEndsEarlycallsMySqlParserTest::testParseRejectsSyntaxTheReleaseLackscallsMySqlParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecalls
Calls 3
- method-call LrParser::parse() line 114
- new LrParser line 114
- method-call MySqlParser::tokenize() line 114
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 124
- new VersionRegistry line 124
- class-const MySqlVersion::DIALECT() line 124
Private surface 3§
Implementation details, listed for orientation only.
private MySqlVersion $versionprivate ParseTable $tableprivate MySqlLexer $lexerTest cases 34§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 12
MySqlParserTest::testParsecallsMySqlParserTest::testParseFollowsTheChosenReleasecallsMySqlParserTest::testParseReadsNamedParametersUnderTheNamedSyntaxcallsMySqlParserTest::testParseRejectsAStatementThatEndsEarlycallsMySqlParserTest::testParseRejectsSyntaxTheReleaseLackscallsMySqlParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecallsMySqlParserTest::testTokenizecallsMySqlParserTest::testTokenizeHonoursTheModecallsMySqlParserTest::testTokenizeRejectsAnUnterminatedStringcallsMySqlParserTest::testVersioncallsMySqlParserTest::testVersionRejectsAnUnknownReleasecallsMySqlParserTest::testVersionscalls
Other tests reaching this symbol 22
ParseBench::benchLoadTablecallsParseBench::benchParsecallsParseBench::setUpcallsFormatTargetcallsDocumentTest::testSignatureDistinguishesHintsAndExecutableCommentscallsGroupingTest::providerParserscallsRendererTest::providerCanonicalFormscallsRendererTest::testDocumentSelectsDialectSpecificSpellingscallsRendererTest::testRenderHonorsMysqlLexicalModescallsShapeTest::testOfDistinguishesAliasAndStringConcatenationGrammarOwnerscallsDialectFactoryTest::testForParserUsesEachBuiltInImplementationcallsFormatterTest::providerExpandedLayoutscallsFormatterTest::providerGeneratedStatementscallsFormatterTest::providerPreservationcallsFormatterTest::providerStylescallsFormatterTest::providerVersionscallsFormatterTest::testFormatEveryGrammarReleasecallsFormatterTest::testFormatKeepsHostnamesAdjacentToAtSignscallsFormatterTest::testFormatPreservesMysqlSqlModecallsOptimizerHintsTest::testFirstFindsTheHintCommentAfterTheStatementKeywordcallsSqlParserTest::providerParserscallsTableFileTest::providerShippedGrammarscalls