Live data from Hacker News

Sqlfmt: an opinionated online SQL formatter

cockroachlabs.com

51–60 of 94 posts

Re: Sqlfmt: an opinionated online SQL formatter

#52
This looks great! I notice you mentioned Prettier in the post — do you have any plans to integrate sqlfmt with that?

Prettier actually has a beta Postgres plugin, but progress on it seems to have stalled. Hooking it up with sqlfmt would be neat - then prettier could format SQL inside other languages too, like code fences in Markdown or template literals in JS.

Re: Sqlfmt: an opinionated online SQL formatter

#53
post #50

Looks really cool, but I have one suggestion. Have you considered using leading commas? It makes big SQL statements much easier to edit and it improves diffs. I normally use trailing commas for my code, but most sql dialects don't support it so I fallback to leading ones there.

I believe and direct my team to use leading commas, with fields on individual lines, to facilitate easier copy/paste and also to make it easier to comment out sections of the query.

It does lead to longer queries and poor use of screen real estate, but it is very readable.

Re: Sqlfmt: an opinionated online SQL formatter

#54

Tangentially related. I'm of the opinion that SQL shouldn't be uppercased. I don't want to hit shift or caps lock and I find lowercase easier to read. Are there others out there like me? I assume I'm in the minority, but I don't understand why. People stopped uppercasing HTML tags, why can't we do the same with SQL?

I agree, I Write SQL code all day long, can’t explain why, I have just always hated upper case, so never used it for SQL. I also like the expressiveness of Postgres double quoted identifiers, so use these a lot in for auto generated app tables, reserving snake case for meta hand coded tables.

Re: Sqlfmt: an opinionated online SQL formatter

#55

Tangentially related. I'm of the opinion that SQL shouldn't be uppercased. I don't want to hit shift or caps lock and I find lowercase easier to read. Are there others out there like me? I assume I'm in the minority, but I don't understand why. People stopped uppercasing HTML tags, why can't we do the same with SQL?

Dimitri Fontaine writes about this in Master PostgreSQL in Application Development. He agrees. I found this to be a great modern sql resource.

Re: Sqlfmt: an opinionated online SQL formatter

#56
post #50

Looks really cool, but I have one suggestion. Have you considered using leading commas? It makes big SQL statements much easier to edit and it improves diffs. I normally use trailing commas for my code, but most sql dialects don't support it so I fallback to leading ones there.

For really complex queries it can be totally confusing. At least for me it does. I prefer indentation. I like my SQL blocky.

Re: Sqlfmt: an opinionated online SQL formatter

#57
post #37

I would challenge the decision to UPPER CASE keywords, it looks oldschool and is unnecessary since syntax highlighting is ubiquitous. And right-justifying is something that only looks clean for the simplest of queries and wreaks havoc with editors that detect mixed tabs/spaces.

I still prefer upper case and indentation. It's cleaner imo. Lower casing makes everything blend together regardless of modern editors highlighting the text for key commands.

Re: Sqlfmt: an opinionated online SQL formatter

#58

Tangentially related. I'm of the opinion that SQL shouldn't be uppercased. I don't want to hit shift or caps lock and I find lowercase easier to read. Are there others out there like me? I assume I'm in the minority, but I don't understand why. People stopped uppercasing HTML tags, why can't we do the same with SQL?

I never uppercase the SQL keywords. If it's more than a one-liner select statement, I format it so the indentation tells me what's going on.

To throw out a tossed-off example:

    select
        P.full_name, A.full_address, AT.type as address_type_name
    from
        People as P
        inner join
        Addresses as A on P.address_id=A.id
    where
        P.deleted is null

Re: Sqlfmt: an opinionated online SQL formatter

#59
post #43

Earlier quoted context omitted.

Hey, thank you for the formatter! I really like the way it formats statements. Although I have found two issues: 1. It lowercases table and field names, rendering the formatted statement useless for a project with not exclusively lowercased names. Could you please fix this? 2. I'd appreciate accepting common placeholder "?" as a valid part of the SQL statement so that we can format queries with placeholders. Would th…

The formatter adheres to CockroachDB rules which is why it acts like you describe. Case is insensitive in names unless you double quote it. Postgres-style (CockroachDB is one) uses "$1" for placeholders instead of "?". Adding "?" would complicate our grammar enough that it's not worth adding it. sqlfmt is not super useful for non-postgres grammars.

Consider those of us stuck with Java adapters to Postgres, which means Postgres syntax but ? for placeholders.

Re: Sqlfmt: an opinionated online SQL formatter

#60

Tangentially related. I'm of the opinion that SQL shouldn't be uppercased. I don't want to hit shift or caps lock and I find lowercase easier to read. Are there others out there like me? I assume I'm in the minority, but I don't understand why. People stopped uppercasing HTML tags, why can't we do the same with SQL?

it’s because if i’m writing sql, i’m definitely angry
Post reply on HN