- Notifications
You must be signed in to change notification settings - Fork10
Prettier SQL plugin that uses sql-parser-cst
License
nene/prettier-plugin-sql-cst
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
APrettier plugin for SQL that usessql-parser-cst and theactualPrettier formatting algorithm.
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, cFROM tblWHERE x>10;
A longer query, will get each clause printed on a separate line:
SELECT id,client.name,client.priorityFROM clientWHEREclient.idIN (12,18,121);
An even longer one gets the contents of each clause indented:
SELECTclient.id,client.nameAS client_name,organization.nameAS org_name,count(orders.id)AS nr_of_ordersFROM clientLEFT JOIN organizationONclient.organization_id=organization.idLEFT JOIN ordersONorders.client_id=client.idWHEREclient.status='active'ANDclient.idIN (28,214,457)ANDorders.statusIN ('active','pending','processing')GROUP BYclient.idORDER BYclient.nameLIMIT100;
- 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 elementsand concentrates mainly on the layout of whitespace.
SeeSTYLE_GUIDE for overview of the SQL formatting style used.
Install it as any other Prettier plugin:
npm install --save-dev prettier prettier-plugin-sql-cst# orpnpm add --save-dev prettier prettier-plugin-sql-cst# oryarn add --dev prettier prettier-plugin-sql-cst
Then use it on SQL files through Prettier command line tool or Prettier extensionfor your editor of choice.
By default the plugin will determine SQL dialect based on file extension:
.sql
or.sqlite
- SQLite.bigquery
- BigQuery
You can override this behavior with a prettier configuration in.prettierrc.json
file:
{"plugins": ["prettier-plugin-sql-cst"],"overrides": [ {"files": ["*.sql"],"options": {"parser":"bigquery" } } ]}
Or you could also store it inside yourpackage.json
:
{"prettier": {"plugins": ["prettier-plugin-sql-cst"],"overrides": [ {"files": ["*.sql"],"options": {"parser":"bigquery" } } ] }}
The plugin provides the following parsers:
sqlite
bigquery
postgresql
(experimental! expect crashes)mysql
(experimental! expect crashes)mariadb
(experimental! expect crashes)
The standard Prettier optionsprintWidth,tabWidth,useTabs apply.There are also some SQL-specific options:
API Option | Default | Description |
---|---|---|
sqlKeywordCase | upper | Convert SQL keywords toupper orlower case, orpreserve existing. Note that for nowpreserve is somewhat incompatible withsqlCanonicalSyntax: true (e.g. the addedAS keywords will always be in uppercase). |
sqlLiteralCase | upper | Convert SQL literals TRUE, FALSE and NULL toupper orlower case, orpreserve existing. |
sqlParamTypes | [] | Array of bound parameter types:? ,?nr ,$nr ,:name ,@name ,$name . |
sqlCanonicalSyntax | true | When enabled, performs some opinionated changes of keywords and operators, like enforcing the use ofAS in aliases and replacing<> comparisons with!= . SeeSTYLE_GUIDE for more details. (Since 0.11.0) |
sqlFinalSemicolon | 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) |
sqlAcceptUnsupportedGrammar | false | Normally when the plugin encounters SQL syntax it doesn't support it will throw an error and won't format anything at all. With this option enabled, it will skip over SQL statements it doesn't recognize, leaving them as-is. |
To use this plugin inside VSCode,install thePrettier VSCode extension.
FollowPrettier VSCode docsto configure it as the default formatter.
You might also need to configureprettier.documentSelectorsto enable Prettier for*.sql
files.
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 yourinferredParser
. It should reconfirm that by showing"parser": "sqlite"
(or whichever perser you have configured inside your prettier configoverrides
section)andplugins
with the path to this package. If you don't see that part, Prettier not using this package.
Support for new SQL dialects depends on these dialects being supported bysql-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.
Useprettier-plugin-embed together withprettier-plugin-sql-cst
.
Currently this plugin supports two SQL dialects:
- SQLite - full support.
- BigQuery - full support.
It also has experimental support for the following dialects:
- PostgreSQL
- MySQL
- MariaDB
The main limitation is thatthe parser does not support full syntax ofthese dialects. One should expect the parser to crash for syntax that's more specific tothese dialects. But as long as the parsing succeeds, the formatting should also succeed.Mainly one can expect the formatting of SELECT, INSERT, UPDATE, DELETE statements to work.
To overcome this limitation you can enable thesqlAcceptUnsupportedGrammar
option,which will make the plugin skip the SQL statements it doesn't recognize.
The specifics of theSQL formatting style are still very much subject to change.Though the general principles should be mostly in place by now.
About
Prettier SQL plugin that uses sql-parser-cst
Resources
License
Uh oh!
There was an error while loading.Please reload this page.