k-kinzal / sql-formatter
SQL formatting with Compact, Expanded, Tabular, and River layouts for MySQL, PostgreSQL, and SQLite
Requires
- php: ^8.1
- k-kinzal/sql-parser: dev-main
Requires (Dev)
- ext-pdo: *
- ext-pdo_sqlite: *
- brianium/paratest: ^7.3
- deptrac/deptrac: ^3.0
- fakerphp/faker: ^1.23
- friendsofphp/php-cs-fixer: ^3.95.25
- k-kinzal/php-ai-toolkit: dev-main
- k-kinzal/sql-faker: dev-main
- k-kinzal/testcontainers-php: ^0.5.1
- nikic/php-fuzzer: ^0.0.11
- phpbench/phpbench: ^1.4
- phpcompatibility/php-compatibility: ^10.0.0-alpha2
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^4.0.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-28 02:32:35 UTC
README
SQL Formatter formats MySQL, PostgreSQL, and SQLite statements with the Compact, Expanded, Tabular, or River layout. It reads each statement with the lossless parser of sql-parser, which is built from the official grammar of the selected database version, Expanded, Tabular, and River change only layout whitespace and keep keywords, identifiers, literals, and comments as written; Compact writes a canonical single form of the statement, without comments or optional whitespace. Every output is parsed again to confirm that its syntax is unchanged. No schema or database connection is needed.
Requirements
- PHP 8.1+ with the zlib extension
Support Syntax
The following grammar versions are supported. Pass the version tag to the parser given to the formatter; omitting it uses the default for that database.
MySQL
| Version | Version tag | Default |
|---|---|---|
| 5.6.51 | mysql-5.6.51 |
|
| 5.7.44 | mysql-5.7.44 |
|
| 8.0.44 | mysql-8.0.44 |
|
| 8.1.0 | mysql-8.1.0 |
|
| 8.2.0 | mysql-8.2.0 |
|
| 8.3.0 | mysql-8.3.0 |
|
| 8.4.7 | mysql-8.4.7 |
Yes |
| 9.0.1 | mysql-9.0.1 |
|
| 9.1.0 | mysql-9.1.0 |
PostgreSQL
| Version | Version tag | Default |
|---|---|---|
| 16.6 | pg-16.6 |
|
| 17.2 | pg-17.2 |
Yes |
SQLite
| Version | Version tag | Default |
|---|---|---|
| 3.47.2 | sqlite-3.47.2 |
Yes |
Installation
composer require k-kinzal/sql-formatter
Usage
use SqlFormatter\Core\FormatOptions; use SqlFormatter\Core\Style; use SqlFormatter\Facade\Formatter; use SqlParser\MySql\MySqlParser; $formatter = new Formatter( new MySqlParser('mysql-8.4.7'), new FormatOptions(style: Style::Expanded, indentWidth: 4), ); echo $formatter->format('SELECT id,name FROM users WHERE active=1 AND age>=18 ORDER BY name;');
SELECT id, name FROM users WHERE active = 1 AND age >= 18 ORDER BY name;
License
MIT License. See LICENSE for details.