sql-style-guide
sql-style-guide copied to clipboard
SQL Formatter/Linter
Hi @mattm thank you for making this guide available! It's great.
Have you found a good formatter/linter that can be configured to follow a style like this one?
I use VS Code but haven't found a good option yet.
Thanks!
@pedromachados I wonder if you found a way to include this in VS Code?
Just noting that I haven't found a way to do this, though honestly haven't looked into it that much.
If anyone does find a way, please drop a comment here and I'll update the style guide to mention it.
It was great that @pedromachados raised the topic and I'm also very interested.
I worked on the RuboCop project that is a linter for the Ruby ecosystem and works with this configuration. The linter allows you to export the output in several formats that are easy to integrate with any text editor.
I see a great opportunity to embrace this challenge as something really necessary for the community. Here are a few steps to make the linter/formatter works:
- We need to get a good AST representation from the SQL that we're able to rewrite the SQL from the Abstract Syntax Tree.
- We need to build an API to allow people to add new rules. Example from Ruby here.
- We need to build an API to allow people to refactor the code and autocorrect the fixable issues.
- We need to build a CLI to allow us to join all rules and check the issues and a flag to autocorrect it.
I see a lot of opportunities on https://github.com/okbob/plpgsql_check and this is a great project.
I also see SQL format https://sqlformat.org/source/ as something that worth check the internals.
And this project is also very interesting as it looks like using pg internals to parse the AST: https://github.com/pganalyze/pg_query
Let me know if you need any help to build it, I'm willed to help.
I'm a huge fan of this style, it's very similar to how I already write it, with a few stricter decisions.
SQLFluff has a lot of configurable options, I wondered whether anybody has (in the last 2 years) explored the settings here.
I wonder whether anybody has tried configuring it to match this?