Live data from Hacker News

PgFormatter: A PostgreSQL SQL syntax beautifier

github.com

21–30 of 33 posts

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#21

Do we need to use caps for SQL keywords? My understanding is that it served a purpose back in the days of monochrome screens, but modern IDEs colour the keywords so caps are now redundant. Though I don't use caps even without colouring, I find them just a nuisance.

I got over all caps SQL keywords a very long time ago. If anything, I think it hurts readability while taking (me) longer to write.

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#22

I've never heard of a code formatter called a "syntax beautifier" before. The phrase "syntax beautifier" sounds more like a compiler plug-in that actually changes the language syntax to something more beautiful.

It's a relatively common name. I would say it might even be more common than the alternative of "code formatter". I do agree that the syntax of the language isn't changing, but it does seem like the phrase that has stuck.

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#24

I'll have to see if I can adapt this to my somewhat idiosyncratic syntax: SELECT field, other_field FROM table WHERE some_thing IN ( SELECT id FROM other_table ) AND other_field IS NOT NULL (mostly it's right-aligning keywords, but there are a few other weird tweaks)

I tried your SQL in the demo, and it came out like this:

    SELECT
        field,
        other_field
    FROM
        TABLE
    WHERE
        some_thing IN (
            SELECT
                id
            FROM
                other_table)
            AND other_field IS NOT NULL
I'd have expected the last line to have one less indentation level...

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#25

I'll have to see if I can adapt this to my somewhat idiosyncratic syntax: SELECT field, other_field FROM table WHERE some_thing IN ( SELECT id FROM other_table ) AND other_field IS NOT NULL (mostly it's right-aligning keywords, but there are a few other weird tweaks)

I’ve adopted the same format myself, it reminds me of aligning numbers at the decimal point.

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#26
post #2

Pasting "SELECT 1,2," then clicking [FORMAT MY CODE] on their online demo[1] seems to break something. It renders the site header, but nothing else. [1] http://sqlformat.darold.net/ Also, a similar tool was recently discussed: https://news.ycombinator.com/item?id=18087514

injection lol

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#28
post #27

I really hope there will be a sql formatter like prettier for javascript, I have tried Intellij's sql formater and mysql workbench's sql formatter, neither has some warts.

https://www.cockroachlabs.com/blog/sql-fmt-online-sql-format... was on here last week. It's a sql formatter based on the same paper prettier is.

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#29

I'll have to see if I can adapt this to my somewhat idiosyncratic syntax: SELECT field, other_field FROM table WHERE some_thing IN ( SELECT id FROM other_table ) AND other_field IS NOT NULL (mostly it's right-aligning keywords, but there are a few other weird tweaks)

sqlfmt gets pretty close to this: https://sqlfum.pt/?n=30&indent=4&spaces=1&simplify=1&align=2...

Re: PgFormatter: A PostgreSQL SQL syntax beautifier

#30
post #5

A 100% Perl project in 2018 is a rare sight. (Implementing a SQL formatter in pure SQL is not impossible, but would be quite a tour de force. Just a thought.)

The most productive language of Gilles, well-known in the Postgres community by the way, is perl, and he can do completely crazy things with it. See for example pgbadger (https://github.com/darold/pgbadger), one of his other projects.
Post reply on HN