-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SQL code formatter #45
Comments
I tried adding
and to
I was able to run it with
|
I think |
I'm stopping work on this. Some comments for visibility. The overall reason is that there isn't sufficient support for auto-formatting SQL + psql + pl/pgsql. sqlfluff is one of the more customizable SQL formatters, but it doesn't for example support custom operators (e.g., <->), psql commands like \gset and \echo, and variable substitution like LIMIT :LIMIT. pgFormatter worked fine with custom operators, but I think it was inadequate. It didn't for example handle CTEs well, and I didn't see a good way to fix that. Also, it didn't support "magic comments". They do support placeholders, which I tried to use for magic comments, but this caused formatting issues. (e.g., next line always started with a space) pglast I liked because it was based on libpg_query, which is a port of Postgres's parser. I wasn't the biggest fan of all their formatting choices, but I thought it'd be easier to hack together support for the psql commands on top of it + magic comments. My attempt is included in the closed PR. In the end, it didn't handle \gset ideally, it didn't handle :'v4444' (but did handle limit :limit). I could spend some more time getting those cases to work, but then there's the additional pain of, sometimes it doesn't format \gset in the way you might like. At that point, if you're constantly using magic comments to force it to format in an aesthetic way, what is the point? I think if there's a nice formatter with psql support in the future, it would be great to use that. |
Hi, I'm taking a fresh look at this issue. Two solutions seem possible:
I suggest that choice (2) be implemented using a library (https://github.com/pganalyze/libpg_query). If this approach is acceptable, I can write a testing tool. |
This sounds interesting, @bobdevine ! Quick question:
Does this guarantee compatibility with PSQL magic commands ( It seems @dqii considered an approach similar to your proposal (2) via pglast (which is also based on libpg_query!) but that approach did not handle PSQL magic commands. |
Here's a zip-file containing 2 C files to read a test file and then format all lines that are SQL. Formatting uses the PostgreSQL parser to validate the SQL command. Then the parsed SQL is "deparsed" to a canonical string. |
This should format
lantern.sql
and regression test SQL files.Sqlfluff is one good option to use for this, but have not looked too much into it.
The text was updated successfully, but these errors were encountered: