final class PostgreSqlParser
    implements SqlParser
Public API: explicitly declared with @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.

Parsing with the default releasedoctest
$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
Writing a parsed statement backdoctest
$sql = "SELECT id FROM users -- everyone\n";
    $parser = new \SqlParser\PostgreSql\PostgreSqlParser();
    $parser->parse($sql)->toString() === $sql // => true
Rejecting an unsupported releasedoctest
new \SqlParser\PostgreSql\PostgreSqlParser('pg-9.6.0') // throws \RuntimeException: Unsupported
Reading a named placeholderdoctest
$parser = new \SqlParser\PostgreSql\PostgreSqlParser(parameters: \SqlParser\Lexer\ParameterSyntax::Named);
    $parser->tokenize('SELECT id FROM users WHERE id = :id')[7]->name // => 'PARAM'

Methods§

public function __construct(
    string|null $version = null,
    VersionRegistry $registry = new \SqlParser\Resource\VersionRegistry(),
    ParameterSyntax $parameters = ParameterSyntax::Native,
)

Loads the parser of one release.

Parameters

$versionstring|nullRelease tag such as `pg-17.2`, or null for the newest shipped
$registryVersionRegistryRecord of shipped releases
$parametersParameterSyntaxWhich parameter markers are read; the server reads only `$1`

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 `pg-17.2`
Test cases 1
Called from 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 function parse(string $sql): Node

Parses one or more statements.

Parameters

$sqlstringThe SQL text

Returns

Node The tree, rooted at the grammar's start symbol, holding every byte of the text

Throws

LexicalException When the text holds something no token starts with
SyntaxException When the text is not in the grammar of the release
Test cases 543
Calls 3
public static function versions(): list<string>

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

Returns

list<string> Release tags
Test cases 1
Calls 3

Private surface 3§

Implementation details, listed for orientation only.

private PostgreSqlVersion $version
private ParseTable $table
private PostgreSqlLexer $lexer

Test cases 562§

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

Dedicated tests 10
Other tests reaching this symbol 552

Relations§

Instantiated in 3
Method calls 1
Type checks 1