Search by

k-kinzal / sql-formatter

kinzal

SQL formatting with Compact, Expanded, Tabular, and River layouts for MySQL, PostgreSQL, and SQLite

Package info

github.com/k-kinzal/sql-formatter

Documentation

pkg:composer/k-kinzal/sql-formatter

Statistics

Installs: 0

Dependents: 4

Suggesters: 0

Stars: 0

dev-main 2026-09-28 02:32 UTC

This package is auto-updated.

Last update: 2026-09-28 02:32:35 UTC


README

Packagist Downloads PHP Version License: MIT Docs Ask DeepWiki

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.