final class SqliteParser
    implements SqlParser
Public API: explicitly declared with @visibility public.

Parses SQLite statements with the grammar of a chosen library release.

The parse table is built from the parse.y of that release and the lexer follows its tokenize.c, so the tree names the nonterminals of the official grammar. Several statements separated by semicolons parse as one tree, and a missing final semicolon is supplied as SQLite supplies it.

Parsing with the default releasedoctest
$parser = new \SqlParser\Sqlite\SqliteParser();
    $tree = $parser->parse('SELECT id FROM users WHERE id = ?');
    $tree->name // => 'input'
    count($tree->find('select')) // => 1
Writing a parsed statement backdoctest
$sql = "SELECT id FROM users -- everyone\n";
    $parser = new \SqlParser\Sqlite\SqliteParser();
    $parser->parse($sql)->toString() === $sql // => true
Rejecting an unsupported releasedoctest
new \SqlParser\Sqlite\SqliteParser('sqlite-2.8.17') // 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 `sqlite-3.47.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 7
public function version(): string

Answers the release the parser reads for.

Returns

string Release tag such as `sqlite-3.47.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 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 22
Calls 3
public static function versions(): list<string>

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

Returns

list<string> Release tags
Test cases 1
Calls 3

Private surface 3§

Implementation details, listed for orientation only.

private SqliteVersion $version
private ParseTable $table
private SqliteLexer $lexer

Test cases 42§

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 32

Relations§

Instantiated in 2
Type checks 1