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

Lightweight SQL parser for SQLite.

Uses lexical statement classification and focused extraction for the SQL subset needed by ZTD: SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, DROP TABLE, ALTER TABLE ADD COLUMN.

Returns structured representations of parsed statements.

Inspect SQLite statements without executing themdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->classifyStatement("SELECT 1") // => 'SELECT'

Methods§

public function classifyStatement(string $sql): string|null
Public API: explicitly declared with @visibility public.

Classify the type of a SQL statement.

Parameters

$sqlstring

Returns

string|null Statement type: 'SELECT', 'INSERT', 'UPDATE', 'DELETE', 'CREATE_TABLE', 'DROP_TABLE', 'ALTER_TABLE', or null if unsupported.
Classify a statement following a CTEdoctest
$parser = new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser();
    $parser->classifyStatement('WITH ids AS (SELECT 1) SELECT * FROM ids') // => 'SELECT'
    $parser->classifyStatement('VACUUM') // => null
Test cases 67
Called from 7
Calls 2
public function splitStatements(string $sql): list<string>
Public API: explicitly declared with @visibility public.

Split a SQL string into individual statements.

Parameters

$sqlstring

Returns

list<string>
Split statements without splitting quoted semicolonsdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->splitStatements("SELECT ';'; SELECT 2") // => ["SELECT ';'", 'SELECT 2']
Test cases 37
Called from 2
Calls 2
public function extractTargetTable(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract the target table name from a DML statement.

Parameters

$sqlstring

Returns

?string
Find the table modified by an INSERTdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractTargetTable('INSERT INTO users (id) VALUES (1)') // => 'users'
Test cases 64
Called from 10
Calls 2
public function extractSelectTables(string $sql): array<int, string>
Public API: explicitly declared with @visibility public.

Extract table names referenced in a SELECT statement.

Parameters

$sqlstring

Returns

array<int, string>
List tables read by a querydoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractSelectTables('SELECT * FROM users JOIN orders ON users.id = orders.user_id') // => ['users', 'orders']
Test cases 57
Called from 1
Calls 2
public function extractInsertColumns(string $sql): array<int, string>
public function extractInsertValues(string $sql): array<int, array<int, string>>
Public API: explicitly declared with @visibility public.

Extract VALUES from an INSERT statement.

Parameters

$sqlstring

Returns

array<int, array<int, string>>
Keep VALUES as SQL expressionsdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractInsertValues('INSERT INTO users VALUES (1, 2 + 3)') // => [['1', '2 + 3']]
Test cases 23
Called from 1
Calls 2
public function extractUpdateAssignments(string $sql): array<string, string>
Public API: explicitly declared with @visibility public.

Extract SET assignments from an UPDATE statement.

Parameters

$sqlstring

Returns

array<string, string> Column name => value expression.
Read SET expressions by column namedoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractUpdateAssignments('UPDATE users SET score = score + 1, name = NULL') // => ['score' => 'score + 1', 'name' => 'NULL']
Test cases 32
Called from 1
Calls 2
public function extractUpdateAlias(string $sql): ?string
Public API: explicitly declared with @visibility public.

Returns the target alias preceding an UPDATE SET clause.

Parameters

$sqlstring

Returns

?string
Find an UPDATE target aliasdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractUpdateAlias('UPDATE users AS u SET score = 1') // => 'u'
Test cases 5
Called from 1
Calls 2
public function extractUpdateFromClause(string $sql): ?string
Public API: explicitly declared with @visibility public.

Returns the UPDATE FROM source before filtering clauses.

Parameters

$sqlstring

Returns

?string
Read an UPDATE FROM sourcedoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractUpdateFromClause('UPDATE users SET score = s.score FROM scores AS s WHERE users.id = s.id') // => 'scores AS s'
Test cases 2
Called from 1
Calls 2
public function extractWhereClause(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract WHERE clause from a DML statement.

Parameters

$sqlstring

Returns

?string
Extract the filter without its keyworddoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractWhereClause('DELETE FROM users WHERE id = 1') // => 'id = 1'
Test cases 19
Called from 2
Calls 2
public function extractOrderByClause(string $sql): ?string
public function hasOnConflict(string $sql): bool
Public API: explicitly declared with @visibility public.

Check if an INSERT statement has ON CONFLICT clause (upsert).

Parameters

$sqlstring

Returns

bool
Recognize a conflict clausedoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->hasOnConflict('INSERT INTO users VALUES (1) ON CONFLICT DO NOTHING') // => true
Test cases 6
Called from 1
Calls 2
public function isReplace(string $sql): bool
Public API: explicitly declared with @visibility public.

Check if the statement is INSERT OR REPLACE / REPLACE INTO.

Parameters

$sqlstring

Returns

bool
Recognize replacement writesdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->isReplace('INSERT OR REPLACE INTO users VALUES (1)') // => true
Test cases 9
Called from 1
Calls 2
public function isInsertIgnore(string $sql): bool
Public API: explicitly declared with @visibility public.

Check if the statement is INSERT OR IGNORE / INSERT IGNORE.

Parameters

$sqlstring

Returns

bool
Recognize ignored conflictsdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->isInsertIgnore('INSERT OR IGNORE INTO users VALUES (1)') // => true
Test cases 8
Called from 1
Calls 2
public function extractOnConflictUpdates(string $sql): array<string, string>
Public API: explicitly declared with @visibility public.

Extract ON CONFLICT update columns from an upsert statement.

Parameters

$sqlstring

Returns

array<string, string> Column name => value expression.
Read upsert update expressionsdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractOnConflictUpdates('INSERT INTO users VALUES (1, 2) ON CONFLICT(id) DO UPDATE SET score = excluded.score') // => ['score' => 'excluded.score']
Test cases 12
Called from 2
Calls 2
public function extractOnConflictUpdateWhere(string $sql): ?string
Public API: explicitly declared with @visibility public.

Returns the predicate belonging to DO UPDATE SET.

Parameters

$sqlstring

Returns

?string
Distinguish the upsert update predicatedoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractOnConflictUpdateWhere('INSERT INTO users VALUES (1, 2) ON CONFLICT(id) DO UPDATE SET score = excluded.score WHERE score < 5') // => 'score < 5'
Test cases 3
Called from 2
Calls 2
public function extractInsertSelect(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract the SELECT subquery from an INSERT ... SELECT statement.

Parameters

$sqlstring

Returns

?string
Read the SELECT supplying inserted rowsdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->extractInsertSelect('INSERT INTO users SELECT * FROM archived_users') // => 'SELECT * FROM archived_users'
Test cases 15
Called from 1
Calls 2
public function stripComments(string $sql): string
public function maskStringLiterals(string $sql): string
Public API: explicitly declared with @visibility public.

Replaces single-quoted literal spans with spaces at the same offsets.

Parameters

$sqlstring

Returns

string
Preserve offsets while hiding string contentsdoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->maskStringLiterals("SELECT 'abc'") // => 'SELECT      '
Test cases 2
Calls 2
public function unquoteIdentifier(string $identifier): string
Public API: explicitly declared with @visibility public.

Unquote a SQL identifier (double-quoted or backtick-quoted).

Parameters

$identifierstring

Returns

string
Decode a doubled identifier quotedoctest
(new \ZtdQuery\Platform\Sqlite\Sql\SqliteParser())->unquoteIdentifier('"user""name"') // => 'user"name'
Test cases 30
Called from 4
Calls 2

Test cases 945§

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

Dedicated tests 430
Other tests reaching this symbol 515

Relations§

Instantiated in 5
Method calls 44
Type declarations 22