classSqliteParser
final class SqliteParser
implements SqlParser
@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.
$parser = new \SqlParser\Sqlite\SqliteParser();
$tree = $parser->parse('SELECT id FROM users WHERE id = ?');
$tree->name // => 'input'
count($tree->find('select')) // => 1$sql = "SELECT id FROM users -- everyone\n";
$parser = new \SqlParser\Sqlite\SqliteParser();
$parser->parse($sql)->toString() === $sql // => truenew \SqlParser\Sqlite\SqliteParser('sqlite-2.8.17') // 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 `sqlite-3.47.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 7
- new VersionRegistry line 59
- static-call SqliteVersion::resolve() line 61
- method-call TableFile::load() line 62
- new TableFile line 62
- function-call
array_keysline 64 - new SqliteLexer line 67
- static-call KeywordTable::load() line 67
public function version(): stringAnswers the release the parser reads for.
Returns
string Release tag such as `sqlite-3.47.2`Test cases 1
Calls 1
- method-call SqliteVersion::name() line 77
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 95
- new TerminalIndex line 95
- method-call SqliteLexer::scan() line 95
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 22
ParseBench::benchParsecallsStatementListTest::testRejectsAnotherParsersRootcallsExpressionBinderTest::testQualifiedRejectsNonNameOperandscallsDocumentTest::testSignatureIgnoresOptionalSyntaxButPreservesOperatorAssociationcallsReductionsTest::testCandidatesProtectsHintBearingGroupingcallsRendererTest::testRenderPreservesSqliteResultsAndOperatorAssociationcallsBlockTest::testRenderFormatsNestedBlockscallsElementsTest::testWriteFormatsCaseBranchescallsHeadersTest::testLengthIncludesInterwordSpacescallsHeadersTest::testWidthExcludesNestedQueryHeaderscallsHeadersTest::testWriteAlignsRiverHeadercallsRendererTest::testRenderExpandedPreservesSpellingcallsAnalyzerTest::testVisitKeepsTokensInSourceOrdercallsDocumentTest::testFromDistinguishesFunctionAndProjectionCommascallsListsTest::testMarkKeepsFunctionArgumentCommasInlinecallsMarkersTest::testApplyPreservesBetweenConjunctioncallsSqliteParserTest::testParsecallsSqliteParserTest::testParseAcceptsAnEmptyInputcallsSqliteParserTest::testParseLetsKeywordsFallBackToIdentifierscallsSqliteParserTest::testParseRejectsAStatementThatEndsEarlycallsSqliteParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecallsTypeRulesTest::testReadPreservesAnyValuesInStrictTablescalls
Calls 3
- method-call LrParser::parse() line 110
- new LrParser line 110
- method-call SqliteParser::tokenize() line 110
public static function versions(): list<string>Answers every release tag the package ships a SQLite grammar for, oldest first.
Returns
list<string> Release tagsTest cases 1
Calls 3
- method-call VersionRegistry::names() line 120
- new VersionRegistry line 120
- class-const SqliteVersion::DIALECT() line 120
Private surface 3§
Implementation details, listed for orientation only.
private SqliteVersion $versionprivate ParseTable $tableprivate SqliteLexer $lexerTest cases 42§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 10
SqliteParserTest::testParsecallsSqliteParserTest::testParseAcceptsAnEmptyInputcallsSqliteParserTest::testParseLetsKeywordsFallBackToIdentifierscallsSqliteParserTest::testParseRejectsAStatementThatEndsEarlycallsSqliteParserTest::testParseWritesTheTextBackWithItsCommentsAndWhitespacecallsSqliteParserTest::testTokenizecallsSqliteParserTest::testTokenizeRejectsAnUnterminatedStringcallsSqliteParserTest::testVersioncallsSqliteParserTest::testVersionRejectsAnUnknownReleasecallsSqliteParserTest::testVersionscalls
Other tests reaching this symbol 32
FormatterBench::setUpcallsFormatterBench::setUpCompactcallsParseBench::benchParsecallsParseBench::setUpcallsFormatTargetcallsStatementListTest::testRejectsAnotherParsersRootcallsExpressionBinderTest::testQualifiedRejectsNonNameOperandscallsDocumentTest::testSignatureIgnoresOptionalSyntaxButPreservesOperatorAssociationcallsGroupingTest::providerParserscallsReductionsTest::testCandidatesProtectsHintBearingGroupingcallsRendererTest::providerCanonicalFormscallsRendererTest::testRenderPreservesSqliteResultsAndOperatorAssociationcallsRendererTest::testWriteRetainsDirectiveOwnershipAndRequiredSeparatorscallsBlockTest::testRenderFormatsNestedBlockscallsElementsTest::testWriteFormatsCaseBranchescallsHeadersTest::testLengthIncludesInterwordSpacescallsHeadersTest::testWidthExcludesNestedQueryHeaderscallsHeadersTest::testWriteAlignsRiverHeadercallsRendererTest::testRenderExpandedPreservesSpellingcallsAnalyzerTest::testVisitKeepsTokensInSourceOrdercallsDocumentTest::testFromDistinguishesFunctionAndProjectionCommascallsListsTest::testMarkKeepsFunctionArgumentCommasInlinecallsMarkersTest::testApplyPreservesBetweenConjunctioncallsDialectFactoryTest::testForParserUsesEachBuiltInImplementationcallsFormatterTest::providerExpandedLayoutscallsFormatterTest::providerGeneratedStatementscallsFormatterTest::providerPreservationcallsFormatterTest::providerStylescallsFormatterTest::testFormatNestedCteWithConfiguredIndentcallsFormatterTest::testFormatRejectsInvalidInputcallsSqlParserTest::providerParserscallsTypeRulesTest::testReadPreservesAnyValuesInStrictTablescalls