classSqlToken
final class SqlToken
Public API: explicitly declared with
@visibility public.One lexeme, with where it was written and how deeply it was nested.
The offset is what lets a rewriter put a statement back together around a token it wants to change, and the two depths are what let it tell a clause of the statement itself from the same words inside a subquery or a parenthesised list.
$token = new \ZtdQuery\Sql\SqlToken(\ZtdQuery\Sql\SqlTokenKind::Word, 'SELECT', 2, 0, 0);
$token->isKeyword('select') // => true
$token->endOffset() // => 8
$token->isTopLevel() // => trueProperties§
public SqlTokenKind $kindpublic string $textpublic int $offsetpublic int $depthpublic int $bracketDepthMethods§
public function __construct(
public SqlTokenKind $kind,
public string $text,
public int $offset,
public int $depth,
public int $bracketDepth,
)Parameters
$kind | SqlTokenKind | What kind of lexeme this is |
$text | string | The lexeme, exactly as it was written |
$offset | int | Where in the statement it starts |
$depth | int | How many parentheses it is inside |
$bracketDepth | int | How many brackets it is inside |
Test cases 10
SqlTokenTest::testEndOffsetIsJustPastTheLastByteOfTheLexemecoversSqlTokenTest::testIsKeywordDoesNotMindHowTheWordWasCasedcoversSqlTokenTest::testIsKeywordIsFalseForAKeywordSpelledInsideAStringcoversSqlTokenTest::testIsKeywordIsFalseForAWordThatMerelyStartsWithItcoversSqlTokenTest::testIsTopLevelIsFalseForATokenInsideABracketcoversSqlTokenTest::testIsTopLevelIsFalseForATokenInsideAParenthesiscoversSqlTokenTest::testIsTopLevelWhenTheTokenIsInsideNoParenthesisAndNoBracketcoversSqlTokenTest::testSliceCarriesHowDeeplyItWasNestedcoversSqlTokenTest::testSliceCarriesTheTextTheStatementWasWrittenWithcoversSqlTokenTest::testSliceRemembersWhereInTheStatementItWasWrittencovers
public static function slice(
string $sql,
SqlTokenKind $kind,
int $start,
int $end,
int $depth,
int $bracketDepth,
): selfAnswers the token a stretch of the statement was read as.
Parameters
$sql | string | The statement, as written |
$kind | SqlTokenKind | What kind of lexeme the stretch was read as |
$start | int | Where the lexeme starts |
$end | int | Where it ends, just past its last byte |
$depth | int | How many parentheses it is inside |
$bracketDepth | int | How many brackets it is inside |
Returns
self The lexeme, carrying the text it was written asTest cases 3
SqlTokenTest::testSliceCarriesHowDeeplyItWasNestedcovers and callsSqlTokenTest::testSliceCarriesTheTextTheStatementWasWrittenWithcovers and callsSqlTokenTest::testSliceRemembersWhereInTheStatementItWasWrittencovers and calls
Called from 2
public function endOffset(): intAnswers where in the statement the lexeme ends.
Returns
int The offset just past its last byteTest cases 1
SqlTokenTest::testEndOffsetIsJustPastTheLastByteOfTheLexemecovers and calls
Calls 1
- function-call
strlenline 71
public function isTopLevel(): boolReports whether the lexeme belongs to the statement itself.
Returns
bool True when it is inside no parenthesis and no bracketTest cases 3
SqlTokenTest::testIsTopLevelIsFalseForATokenInsideABracketcovers and callsSqlTokenTest::testIsTopLevelIsFalseForATokenInsideAParenthesiscovers and callsSqlTokenTest::testIsTopLevelWhenTheTokenIsInsideNoParenthesisAndNoBracketcovers and calls
public function isKeyword(string $keyword): boolReports whether the lexeme is a given keyword.
SQL keywords are not case-sensitive, and neither is this.
Parameters
$keyword | string | Keyword to test against |
Returns
bool True when the lexeme is a word spelling that keywordTest cases 3
SqlTokenTest::testIsKeywordDoesNotMindHowTheWordWasCasedcovers and callsSqlTokenTest::testIsKeywordIsFalseForAKeywordSpelledInsideAStringcovers and callsSqlTokenTest::testIsKeywordIsFalseForAWordThatMerelyStartsWithItcovers and calls
Calls 2
- class-const SqlTokenKind::Word() line 95
- function-call
strcasecmpline 95
Test cases 36§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 10
SqlTokenTest::testEndOffsetIsJustPastTheLastByteOfTheLexemecovers and callsSqlTokenTest::testIsKeywordDoesNotMindHowTheWordWasCasedcovers and callsSqlTokenTest::testIsKeywordIsFalseForAKeywordSpelledInsideAStringcovers and callsSqlTokenTest::testIsKeywordIsFalseForAWordThatMerelyStartsWithItcovers and callsSqlTokenTest::testIsTopLevelIsFalseForATokenInsideABracketcovers and callsSqlTokenTest::testIsTopLevelIsFalseForATokenInsideAParenthesiscovers and callsSqlTokenTest::testIsTopLevelWhenTheTokenIsInsideNoParenthesisAndNoBracketcovers and callsSqlTokenTest::testSliceCarriesHowDeeplyItWasNestedcovers and callsSqlTokenTest::testSliceCarriesTheTextTheStatementWasWrittenWithcovers and callsSqlTokenTest::testSliceRemembersWhereInTheStatementItWasWrittencovers and calls
Other tests reaching this symbol 26
MutationImpactTestcallsSqlIdentifierComponentTestcallsSqlKeywordSequenceTestcallsSqlTokenScannerTest::testScanCountsHowDeeplyEachLexemeIsNestedcallsSqlTokenScannerTest::testScanLosesNothingOfTheStatementItReadcallsSqlTokenScannerTest::testScanReadsEachLexemeAsWhatItIscallsSqlTokenStreamTest::testAdditionalLineCommentsRemainProfileSpecificcallsSqlTokenStreamTest::testAsteriskWithoutOpeningSlashIsNotACommentcallsSqlTokenStreamTest::testCommentDelimitersDoNotOverlapOrConsumeFollowingTokenscallsSqlTokenStreamTest::testDollarTagMustStartAtCurrentOffsetcallsSqlTokenStreamTest::testIdentifierQuotesDoNotUseStringBackslashEscapescallsSqlTokenStreamTest::testKeepsArithmeticOperatorsOutsideNumberTokenscallsSqlTokenStreamTest::testNestedCommentDelimitersAdvanceAsPairscallsSqlTokenStreamTest::testParametersStopBeforeFollowingWordAndAtEndOfInputcallsSqlTokenStreamTest::testQuestionMarkProfileCanRejectNumericSuffixescallsSqlTokenStreamTest::testQuotedEscapeAtEndRemainsInsideUnterminatedValuecallsSqlTokenStreamTest::testQuotedEscapeControlsWhereTheValueEndscallsSqlTokenStreamTest::testSignificantTokensLeavesOutWhitespaceAndCommentscallsSqlTokenStreamTest::testTokenizesDollarStringsAndParameterscallsSqlTokenStreamTest::testTokenizesNumberedQuestionMarkAndStructuredNamedParameterscallsSqlTokenStreamTest::testTokenizesQuotedValuesAndIdentifierscallsSqlTokenStreamTest::testTokenizesUnterminatedCommentsLosslesslycallsSqlTokenStreamTest::testTokenizesUnterminatedDollarStringcallsSqlTokenStreamTest::testTokenizesWhitespaceAndNestedCommentsLosslesslycallsSqlTokenStreamTest::testTokenizesWordsAndConfiguredNumberFormscallsSqlTokenStreamTest::testTracksParenthesisAndBracketDepthWithoutUnderflowcalls
Relations§
Static calls 2
Type declarations 4
- type ZtdQuery\Sql\SqlTokenStream::significantTokens() packages/ztd-query-core/src/Sql/SqlTokenStream.php:57
- type ZtdQuery\Sql\SqlTokenStream packages/ztd-query-core/src/Sql/SqlTokenStream.php:71
- type ZtdQuery\Sql\SqlTokenStream packages/ztd-query-core/src/Sql/SqlTokenStream.php:90
- type ZtdQuery\Sql\SqlTokenStream packages/ztd-query-core/src/Sql/SqlTokenStream.php:109