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

Methods§

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

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

Throws

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

Answers the release the parser reads for.

Returns

string Release tag such as `pg-17.2`
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 2
Called from 1
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 34§

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

Dedicated tests 9
Other tests reaching this symbol 25

Relations§

Instantiated in 2
Type checks 1