packages/ztd-query-postgres/fuzz/Input/TruncateTargets.php

1<?php
2
3declare(strict_types=1);
4
5namespace Fuzz\Input;
6
7use ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile;
8use ZtdQuery\Sql\SqlTokenStream;
9
10/**
11 * Counts the explicit target list independently of mutation construction.
12 */
13final class TruncateTargets
14{
15    /**
16     * Target count.
17     */
18    public function targetCount(string $sql): ?int
19    {
20        $stream = SqlTokenStream::tokenize($sql, PgSqlLexerProfile::create());
21        if ($stream->firstTopLevelKeyword() !== 'TRUNCATE') {
22            return null;
23        }
24        $targetList = $stream->topLevelClause(['TRUNCATE'], [['RESTART', 'IDENTITY'], ['CONTINUE', 'IDENTITY'], ['CASCADE'], ['RESTRICT']]);
25        if ($targetList === null || $targetList === '') {
26            return 0;
27        }
28        return count(SqlTokenStream::tokenize($targetList, PgSqlLexerProfile::create())->splitTopLevel());
29    }
30}
31