Like Prettier for JavaScript,
this plugin formats SQL expressions differently depending on their length.
A short SQL query will be formatted on a single line:
SELECT a, b, c FROM tbl WHERE x > 10;
A longer query, will get each clause printed on a separate line:
SELECT id, client.name, client.priorityFROM clientWHERE client.id IN (12, 18, 121);
An even longer one gets the contents of each clause indented:
SELECT client.id, client.name AS client_name, organization.name AS org_name, count(orders.id) AS nr_of_ordersFROM clientLEFT JOIN organization ON client.organization_id = organization.idLEFT JOIN orders ON orders.client_id = client.idWHERE client.status = 'active' AND client.id IN (28, 214, 457) AND orders.status IN ('active', 'pending', 'processing')GROUP BY client.idORDER BY client.name
Formatting philosophy
Adapt formatting based on expression length.
Stick to one style and avoid configuration options.
Format embedded languages (like JSON data and JavaScript programs).
When unsure, preserve existing syntax.
Currently this plugin preserves most of the syntax elements
and concentrates mainly on the layout of whitespace.
See STYLE_GUIDE for overview of the SQL formatting style used.
In PostgreSQL this will only effect a limited set of builtin data types, the rest will be formatted as identifiers.
It will effect casing of all the keywords in types like TIMESTAMP WITH TIME ZONE and INTERVAL DAY TO SECOND,
but it won't effect the casing of SETOF and TABLE keywords in SETOF type and TABLE (x int, y int).
For PostgreSQL the recommended setting of this option is lower.
In BigQuery this also effects the names parametric types,
for example array<struct<x int64>> will get formatted as ARRAY<STRUCT<x INT64>>.
To see what Prettier is, or is not doing - open the VSCode Output window and select the Prettier dropdown.
On format, the window should show your inferredParser. It should reconfirm that by showing "parser": "sqlite"
(or whichever perser you have configured inside your prettier config overrides section)
and plugins with the path to this package. If you don't see that part, Prettier not using this package.
FAQ
The SQL dialect I'm using is not supported. Can you add support for it?
Support for new SQL dialects depends on these dialects being supported by sql-parser-cst.
If you really want to, you can open a new issue for that in the parser repo.
But be aware that implementing parser support for new dialects takes a lot of work.
As long as the ongoing implementation of PostgreSQL, MySQL and MariaDB is not finished,
it's unlikely that work on any other dialect will start.
How can I format SQL strings inside JavaScript files?
It also has experimental support for the following dialects:
PostgreSQL
PL/pgSQL
MySQL
MariaDB
The main limitation is that the parser does not support full syntax of
these dialects. To overcome this limitation you can use conditional comments
to disable parsing of the problematic section of SQL:
SELECT * FROM products;/* sql-parser-cst-disable */UNSUPPORTED SQL IN HERE;/* sql-parser-cst-enable */UPDATE products SET sold = true WHERE id = 2;
The specifics of the SQL formatting style are still very much subject to change.
Though the general principles should be mostly in place by now.
LIMIT
100
;
"preserve"
Enforce case of unquoted identifier names.
This effects all identifiers except function names (see sqlFunctionCase).
Beware that in BigQuery unquoted identifiers are case-sensitive, so use of this option with BigQuery is not recommended.
Experimental: This option is still new and not thoroughly tested. You have been warned.
This option also applies to schema-qualified function names,
for example my_schema.foo() will get formatted as my_schema.FOO()
when sqlFunctionCase: "upper" is configured.
Experimental: This option is still new and not thoroughly tested. You have been warned.
By default the parser will reject the following SQL:
SELECT id, name FROM products WHERE price > ? AND category = ?
Configuring sqlParamTypes: ["?"] will make the parser recognize the ? placeholders.
Since 0.7.0
sqlCanonicalSyntax: boolean (default true)
When enabled, performs some opinionated changes of keywords and operators,
like enforcing the use of AS in aliases and replacing <> comparisons with !=.
See STYLE_GUIDE for more details.
Since 0.11.0
sqlFinalSemicolon: boolean (default true)
When enabled, enforces a semicolon at the end of last statement.
When disabled leaves it up to the author whether to add a final semicolon or not.
Since 0.13.0
sqlExperimentalPlpgsql: boolean (default false)
When enabled, the plugin will attempt to format PL/pgSQL function bodies.
This might fail, as the underlying parser support for PL/pgSQL is incomplete.
How to wrap SQL that could fit on fewer lines.
With "preserve", code that's multi-line in the original stays multi-line.
With "collapse", the original line breaks are ignored and lines are only broken when they don't fit,
like Prettier's objectWrap option does for JavaScript objects.