msstc4symfony / sql-labeller
Statement type and table of an SQL string, for metric labels and span names
v1.0.0
2026-10-22 12:05 UTC
Requires
- php: >=8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.76
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: >=11.0
- rector/rector: ^2.1
- roave/security-advisories: dev-latest
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The statement type and the table of an SQL string, cheap enough to run on every query: the labels metrics-bundle puts on its Doctrine metrics and the names profiling-bundle gives its SQL spans. A plain PHP library: no Symfony, no Doctrine.
Compatibility
| Library | PHP |
|---|---|
| 1.0 | 8.2+ |
Installation
composer require msstc4symfony/sql-labeller
Usage
use Msstc4Symfony\SqlLabeller\Domain\Helper\SqlLabeller; $labels = (new SqlLabeller())->label('SELECT o.id FROM shop.orders o JOIN customers c ON c.id = o.customer_id'); $labels->type; // StatementType::SELECT $labels->table; // 'orders'
typeisselect,insert,updateordeleteby the statement's first keyword; anything else isother— a statement starting withWITH, a parenthesisedUNION, a leading comment — even when its table is found.tableis the first table the statement reads or writes, unquoted ("…",`…`,[…]) and without its schema or catalog;nullwhen none is found. Comments and string literals are skipped, and only the statement's ownFROMcounts: a subquery's or a function argument's (EXTRACT(EPOCH FROM …)) does not. A derived table, a parenthesised statement (aUNIONbuilt by DBAL's QueryBuilder) and a common table expression are followed to the table inside.- Only the first 16 KiB are parsed: a
FROMbeyond them is not found. Results are cached per SQL (256 statements, least recently used out), so keep one labeller per connection or process.
Known limits:
- a table function in
FROMis labelled with the function's name; - a table named
onlyorlateralis taken for the keyword: the next word (an alias or a keyword) is labelled; - a string literal ending in a backslash (standard_conforming_strings, MySQL's NO_BACKSLASH_ESCAPES), dollar-quoted
strings and MySQL
#comments are not recognised, so aFROMinside them may be taken; - MySQL's multi-table
DELETE t FROM t JOIN …has no table.
Public API
Domain\Enum\StatementType, Domain\ValueObject\SqlLabels, Domain\Helper\SqlLabelling (the interface, for test
doubles) and Domain\Helper\SqlLabeller under Msstc4Symfony\SqlLabeller\.
Local development
make install-ci # composer-ci.json profile with every CI tool COMPOSER=composer-ci.json make check # lint, PHPStan, CS, composer validate/audit, Rector, deptrac make test # unit + integration (DBAL 4 QueryBuilder SQL of five platforms)
License
MIT — see LICENSE.