sql-style-guide icon indicating copy to clipboard operation
sql-style-guide copied to clipboard

SQL Formatter/Linter

Open pedromachados opened this issue 6 years ago • 4 comments
trafficstars

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 avatar Nov 07 '19 01:11 pedromachados

@pedromachados I wonder if you found a way to include this in VS Code?

Nic3Guy avatar Jan 21 '21 07:01 Nic3Guy

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.

mattm avatar Jan 29 '21 16:01 mattm

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:

  1. We need to get a good AST representation from the SQL that we're able to rewrite the SQL from the Abstract Syntax Tree.
  2. We need to build an API to allow people to add new rules. Example from Ruby here.
  3. We need to build an API to allow people to refactor the code and autocorrect the fixable issues.
  4. 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.

jonatas avatar May 04 '21 14:05 jonatas

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?

danilo-nzyte avatar Jun 30 '23 06:06 danilo-nzyte