classPostgreSqlParser
final class PostgreSqlParser
implements SqlParser
@visibility public.Parses PostgreSQL statements with the grammar of a chosen server release.
The parse table is built from the gram.y of that release and the lexer follows its scan.l, so the tree names the nonterminals of the official grammar. Several statements separated by semicolons parse as one tree.
$parser = new \SqlParser\PostgreSql\PostgreSqlParser();
$tree = $parser->parse('SELECT id FROM users WHERE id = $1');
$tree->name // => 'parse_toplevel'
count($tree->find('relation_expr')) // => 1$sql = "SELECT id FROM users -- everyone\n";
$parser = new \SqlParser\PostgreSql\PostgreSqlParser();
$parser->parse($sql)->toString() === $sql // => truenew \SqlParser\PostgreSql\PostgreSqlParser('pg-9.6.0') // throws \RuntimeException: UnsupportedMethods§
public function __construct(
string|null $version = null,
VersionRegistry $registry = new \SqlParser\Resource\VersionRegistry(),
)Loads the parser of one release.
Parameters
$version | string|null | Release tag such as `pg-17.2`, or null for the newest shipped |
$registry | VersionRegistry | Record of shipped releases |
Throws
RuntimeException When the release is not shipped or its resources are missingCalls 6
- new VersionRegistry line 58
- static-call PostgreSqlVersion::resolve() line 60
- method-call TableFile::load() line 61
- new TableFile line 61
- new PostgreSqlLexer line 62
- static-call KeywordTable::load() line 62
public function version(): stringAnswers the release the parser reads for.
Returns
string Release tag such as `pg-17.2`Test cases 1
Calls 1
- method-call PostgreSqlVersion::name() line 72
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 2
Called from 1
Calls 3
- method-call TerminalIndex::tokens() line 90
- new TerminalIndex line 90
- method-call PostgreSqlLexer::scan() line 90
public function parse(string $sql): NodeParses one or more statements.
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 text is not in the grammar of the releaseTest cases 16
ParseBench::benchParsecallsStatementListTest::testReadPreservesStatementBoundariescallsTokenGroupsTest::testConstraintHeaderSeparatesQuotedNamescallsTokenGroupsTest::testNamesPreservesQuotedCommascallsTokenGroupsTest::testParenthesesRetainsNestedGroupscallsTreeTest::testAssertChildrenRejectsUnknownClausescallsTreeTest::testChildDoesNotSearchNestedScopescallsTreeTest::testOuterStopsAtTheRequestedGrammarBoundarycallsBoundRelationTest::testKeepsScopeAndRelationAtTheSameStagecallsFromBinderTest::testKindRecognizesRightAndRejectsNaturalcallsReductionsTest::testAliasesKeepsCastAndCteAscallsRulesTest::testApplyDoesNotRemoveCastAsInsideAProjectioncallsPostgreSqlParserTest::testParsecallsPostgreSqlParserTest::testParseAcceptsAnEmptyInputcallsPostgreSqlParserTest::testParseRejectsAChainedComparisoncallsPostgreSqlParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecalls
Calls 3
- method-call LrParser::parse() line 105
- new LrParser line 105
- method-call PostgreSqlParser::tokenize() line 105
public static function versions(): list<string>Answers every release tag the package ships a PostgreSQL grammar for, oldest first.
Returns
list<string> Release tagsTest cases 1
Calls 3
- method-call VersionRegistry::names() line 115
- new VersionRegistry line 115
- class-const PostgreSqlVersion::DIALECT() line 115
Private surface 3§
Implementation details, listed for orientation only.
private PostgreSqlVersion $versionprivate ParseTable $tableprivate PostgreSqlLexer $lexerTest cases 34§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 9
PostgreSqlParserTest::testParsecallsPostgreSqlParserTest::testParseAcceptsAnEmptyInputcallsPostgreSqlParserTest::testParseRejectsAChainedComparisoncallsPostgreSqlParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecallsPostgreSqlParserTest::testTokenizecallsPostgreSqlParserTest::testTokenizeRejectsAnUnterminatedStringcallsPostgreSqlParserTest::testVersioncallsPostgreSqlParserTest::testVersionRejectsAnUnknownReleasecallsPostgreSqlParserTest::testVersionscalls
Other tests reaching this symbol 25
ParseBench::benchParsecallsParseBench::setUpcallsFormatTargetcallsStatementListTest::testReadPreservesStatementBoundariescallsTokenGroupsTest::testConstraintHeaderSeparatesQuotedNamescallsTokenGroupsTest::testNamesPreservesQuotedCommascallsTokenGroupsTest::testParenthesesRetainsNestedGroupscallsTreeTest::testAssertChildrenRejectsUnknownClausescallsTreeTest::testChildDoesNotSearchNestedScopescallsTreeTest::testOuterStopsAtTheRequestedGrammarBoundarycallsBoundRelationTest::testKeepsScopeAndRelationAtTheSameStagecallsFromBinderTest::testKindRecognizesRightAndRejectsNaturalcallsGroupingTest::providerParserscallsReductionsTest::testAliasesKeepsCastAndCteAscallsRendererTest::providerCanonicalFormscallsRulesTest::testApplyDoesNotRemoveCastAsInsideAProjectioncallsSpacingTest::testBetweenDoesNotMergeQuotedTokensOrOperatorTokenscallsSpacingTest::testBetweenProtectsNumberExponentAndCommentBoundariescallsSpacingTest::testSignatureDistinguishesTokenMergingcallsDialectFactoryTest::testForParserUsesEachBuiltInImplementationcallsFormatterTest::providerExpandedLayoutscallsFormatterTest::providerGeneratedStatementscallsFormatterTest::providerPreservationcallsFormatterTest::providerStylescallsSqlParserTest::providerParserscalls