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

Focused PostgreSQL SQL parser.

Handles the ZTD-required SQL subset: SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, DROP TABLE, ALTER TABLE, TRUNCATE.

Uses regex + recursive descent hybrid approach to extract structural information without a full PostgreSQL grammar parser.

Identify a SELECT statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->classifyStatement('SELECT 1') // => 'SELECT'

Methods§

public function classifyStatement(string $sql): 'SELECT'|'INSERT'|'UPDATE'|'DELETE'|'MERGE'|'TRUNCATE'|'CREATE_TABLE'|'DROP_TABLE'|'ALTER_TABLE'|'DO'|'TCL'|null
Public API: explicitly declared with @visibility public.

Classify a SQL statement type.

Parameters

$sqlstring

Returns

'SELECT'|'INSERT'|'UPDATE'|'DELETE'|'MERGE'|'TRUNCATE'|'CREATE_TABLE'|'DROP_TABLE'|'ALTER_TABLE'|'DO'|'TCL'|null
Classify the write following a common table expressiondoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->classifyStatement('WITH ids AS (SELECT 1) DELETE FROM users WHERE id IN (SELECT * FROM ids)') // => 'DELETE'
Test cases 77
Called from 3
Calls 2
public function splitStatements(string $sql): list<string>
Public API: explicitly declared with @visibility public.

Split SQL string into individual statements.

Parameters

$sqlstring

Returns

list<string>
Split SQL string into individual statementsdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->splitStatements("SELECT ';'; SELECT 2") // => ["SELECT ';'", 'SELECT 2']
Test cases 32
Called from 1
Calls 2
public function extractInsertTable(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract table name from INSERT statement.

Parameters

$sqlstring

Returns

?string
Extract table name from INSERT statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractInsertTable('INSERT INTO public.users (id) VALUES (1)') // => 'users'
Test cases 13
Called from 2
Calls 2
public function extractInsertColumns(string $sql): list<string>
Public API: explicitly declared with @visibility public.

Extract column list from INSERT statement.

Parameters

$sqlstring

Returns

list<string>
Extract column list from INSERT statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractInsertColumns('INSERT INTO users (id, name) VALUES (1, NULL)') // => ['id', 'name']
Test cases 9
Called from 1
Calls 2
public function extractInsertValues(string $sql): list<list<string>>
Public API: explicitly declared with @visibility public.

Extract VALUES rows from INSERT statement.

Parameters

$sqlstring

Returns

list<list<string>>
Extract VALUES rows from INSERT statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractInsertValues('INSERT INTO users (id) VALUES (1), (2)') // => [['1'], ['2']]
Test cases 27
Called from 1
Calls 2
public function hasOnConflict(string $sql): bool
Public API: explicitly declared with @visibility public.

Check if INSERT has ON CONFLICT clause.

Parameters

$sqlstring

Returns

bool
Check if INSERT has ON CONFLICT clausedoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->hasOnConflict('INSERT INTO users VALUES (1) ON CONFLICT DO NOTHING') // => true
Test cases 5
Called from 1
Calls 2
public function extractOnConflictTarget(string $sql): ?PgSqlConflictTarget
Public API: explicitly declared with @visibility public.

Parses ON CONFLICT columns, predicates, or a named constraint.

Parameters

$sqlstring
Parses ON CONFLICT columns, predicates, or a named constraintdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractOnConflictTarget('INSERT INTO users VALUES (1) ON CONFLICT (id) DO NOTHING')?->columns // => ['id']
Test cases 4
Called from 2
Calls 2
public function extractOnConflictUpdateColumns(string $sql): array{columns: list<string>, values: array<string, string>}
Public API: explicitly declared with @visibility public.

Extract ON CONFLICT ... DO UPDATE SET columns and values.

Parameters

$sqlstring

Returns

array{columns: list<string>, values: array<string, string>}
Extract ON CONFLICT ... DO UPDATE SET columns and valuesdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractOnConflictUpdateColumns('INSERT INTO users VALUES (1) ON CONFLICT (id) DO UPDATE SET id = 2') // => ['columns' => ['id'], 'values' => ['id' => '2']]
Test cases 35
Called from 2
Calls 2
public function extractOnConflictUpdateWhere(string $sql): ?string
Public API: explicitly declared with @visibility public.

Returns the predicate limiting an ON CONFLICT update, when present.

Parameters

$sqlstring

Returns

?string
Returns the predicate limiting an ON CONFLICT update, when presentdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractOnConflictUpdateWhere('INSERT INTO users VALUES (1) ON CONFLICT (id) DO UPDATE SET id = 2 WHERE users.id = 1') // => 'users.id = 1'
Test cases 3
Called from 2
Calls 2
public function hasInsertSelect(string $sql): bool
Public API: explicitly declared with @visibility public.

Check if INSERT has a SELECT subquery (INSERT ... SELECT).

Parameters

$sqlstring

Returns

bool
Check if INSERT has a SELECT subquery (INSERT ... SELECT)doctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->hasInsertSelect('INSERT INTO users (id) SELECT id FROM archive') // => true
Test cases 21
Called from 1
Calls 2
public function extractUpdateAlias(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract table alias from UPDATE statement.

Parameters

$sqlstring

Returns

?string
Extract table alias from UPDATE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractUpdateAlias('UPDATE users AS u SET id = 2') // => 'u'
Test cases 6
Called from 1
Calls 2
public function extractUpdateSets(string $sql): array<string, string>
Public API: explicitly declared with @visibility public.

Extract SET assignments from UPDATE statement.

Parameters

$sqlstring

Returns

array<string, string> column => value expression
Extract SET assignments from UPDATE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractUpdateSets('UPDATE users SET id = 2, name = NULL WHERE id = 1') // => ['id' => '2', 'name' => 'NULL']
Test cases 40
Called from 1
Calls 2
public function extractWhereClause(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract WHERE clause from UPDATE or DELETE statement.

Parameters

$sqlstring

Returns

?string
Extract WHERE clause from UPDATE or DELETE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractWhereClause('DELETE FROM users WHERE id = 1 RETURNING id') // => 'id = 1'
Test cases 37
Called from 2
Calls 2
public function extractUpdateFromClause(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract FROM clause from UPDATE statement (PostgreSQL extension).

Parameters

$sqlstring

Returns

?string
Extract FROM clause from UPDATE statement (PostgreSQL extension)doctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractUpdateFromClause('UPDATE users SET id = a.id FROM archive AS a WHERE users.id = a.id') // => 'archive AS a'
Test cases 16
Called from 1
Calls 2
public function extractDeleteUsingClause(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract USING clause from DELETE statement.

Parameters

$sqlstring

Returns

?string
Extract USING clause from DELETE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractDeleteUsingClause('DELETE FROM users USING archive WHERE users.id = archive.id') // => 'archive'
Test cases 15
Called from 1
Calls 2
public function extractTruncateTable(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract table name from TRUNCATE statement.

Parameters

$sqlstring

Returns

?string
Extract table name from TRUNCATE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractTruncateTable('TRUNCATE TABLE users, archive') // => 'users'
Test cases 9
Calls 2
public function extractTruncateTables(string $sql): list<string>
Public API: explicitly declared with @visibility public.

Parameters

$sqlstring

Returns

list<string>
Extract every table in a TRUNCATE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractTruncateTables('TRUNCATE TABLE users, archive') // => ['users', 'archive']
Test cases 3
Called from 1
Calls 2
public function extractCreateTableName(string $sql): ?string
public function hasIfNotExists(string $sql): bool
Public API: explicitly declared with @visibility public.

Check if CREATE TABLE has IF NOT EXISTS.

Parameters

$sqlstring

Returns

bool
Check if CREATE TABLE has IF NOT EXISTSdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->hasIfNotExists('CREATE TABLE IF NOT EXISTS users (id INTEGER)') // => true
Test cases 3
Called from 1
Calls 2
public function extractCreateTableSelectSql(string $sql): ?string
public function extractCreateTableLikeSource(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract the LIKE source table name.

Parameters

$sqlstring

Returns

?string
Extract the LIKE source table namedoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractCreateTableLikeSource('CREATE TABLE archive (LIKE users INCLUDING ALL)') // => 'users'
Test cases 5
Called from 1
Calls 2
public function extractDropTableName(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract table name from DROP TABLE statement.

Parameters

$sqlstring

Returns

?string
Extract table name from DROP TABLE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractDropTableName('DROP TABLE IF EXISTS users') // => 'users'
Test cases 7
Called from 1
Calls 2
public function hasDropTableIfExists(string $sql): bool
Public API: explicitly declared with @visibility public.

Check if DROP TABLE has IF EXISTS.

Parameters

$sqlstring

Returns

bool
Check if DROP TABLE has IF EXISTSdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->hasDropTableIfExists('DROP TABLE IF EXISTS users') // => true
Test cases 3
Called from 1
Calls 2
public function extractAlterTableName(string $sql): ?string
Public API: explicitly declared with @visibility public.

Extract table name from ALTER TABLE statement.

Parameters

$sqlstring

Returns

?string
Extract table name from ALTER TABLE statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractAlterTableName('ALTER TABLE users ADD COLUMN name TEXT') // => 'users'
Test cases 7
Called from 1
Calls 2
public function unquoteIdentifier(string $identifier): string
Public API: explicitly declared with @visibility public.

Unquote a PostgreSQL identifier (remove double quotes).

Parameters

$identifierstring

Returns

string
Unquote a PostgreSQL identifier (remove double quotes)doctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->unquoteIdentifier('"order""items"') // => 'order"items'
Test cases 7
Calls 2
public function stripSchemaPrefix(string $name): string
Public API: explicitly declared with @visibility public.

Strip schema prefix from a potentially schema-qualified name. "public"."users" -> "users", public.users -> users

Parameters

$namestring

Returns

string
Strip schema prefix from a potentially schema-qualified namedoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->stripSchemaPrefix('public.users') // => 'users'
Test cases 10
Calls 2
public function extractSelectTableNames(string $sql): list<string>
Public API: explicitly declared with @visibility public.

Extract table names referenced in a SELECT statement.

Parameters

$sqlstring

Returns

list<string>
Extract table names referenced in a SELECT statementdoctest
(new \ZtdQuery\Platform\Postgres\Sql\PgSqlParser())->extractSelectTableNames('SELECT u.id FROM users u JOIN orders o ON u.id = o.user_id') // => ['users', 'orders']
Test cases 64
Called from 1
Calls 2

Test cases 934§

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

Dedicated tests 520
Other tests reaching this symbol 414

Relations§

Instantiated in 1
Method calls 41
Type declarations 24