5 ms·
PgFormatter: A PostgreSQL SQL syntax beautifier
- tyingq 8y agoPasting "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/ http://sqlformat.darold.net/ Also, a similar tool was recently discussed: https://news.ycombinator.com/item?id=18087514 https://news.ycombinator.com/item?id=18087514
- dokem 8y agoinjection lol
- frou_dh 8y agoThe stark difference between a formatter and a formatter officially blessed by the parent project, is that the former is far more vulnerable to having its output viewed with a jaundiced eye and the formatter being cast away. I remember trying the one from TFA and NOPE-ing out immediately. Similarly, "sqlfmt" that was on HN recently. If there was an official Postgres formatter, I would probably persevere despite whatever oddities it would inevitably have.
- tyingq 8y agoThe sqlfmt one pulls in the cockroachdb parser, and that team has a fairly big incentive to be compatible with PG syntax. Nobody is perfect, but it seems set up to stay current/correct.
- frou_dh 8y agoI meant the style choices. But it did also choke on Identity Columns IIRC (https://blog.2ndquadrant.com/postgresql-10-identity-columns/ https://blog.2ndquadrant.com/postgresql-10-identity-columns/). Not knocking either project - just pondering adoption.
- dvdhnt 8y agoDo you really need to serve ads on your demo site? Maybe you do, but that doesn't seem very OSS.
- nine_k 8y agoA 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.)
- ioltas 8y agoThe 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 https://github.com/darold/pgbadger), one of his other projects.
- 0x777 8y agoSometimes this (https://zeroturnaround.github.io/sql-formatter/ https://zeroturnaround.github.io/sql-formatter/) does a better job for me. Doesn't support Postgres explicitly but works most of the time.
- bhandziuk 8y agoI like Notepad++'s Poor Man's SQL Formatter plug-in. Works well for anything I do.
- accnumnplus1 8y agoDo 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.
- deleted 8y ago[deleted]
- da_chicken 8y agoNo, not at all. SQL is case agnostic unless you're talking about field collations or, in many systems, object names. It's also largely whitespace agnostic. Many RDBMSs will handle this just fine: select*from"MyTable"where"column"=3
- accnumnplus1 8y agoAh, the whitespace agnostic didn't even occur to me!
- nicoburns 8y agoDepends on your IDE. My editor only picks up SQL in strings sometimes.
- tejtm 8y agoI got over all caps SQL keywords a very long time ago. If anything, I think it hurts readability while taking (me) longer to write.
- yoklov 8y agoI prefer them because they make grepping for SQL code easier. Also syntax coloring inside strings is not common.
- chrisjc 8y agoLooks like they might need a formatter to beautify their README too.
- codefined 8y agoI might be missing something, but it seems like a fairly high quality README file. What do you feel is wrong with it?
- eric_the_read 8y agoI'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)
- overcast 8y agoI do similar, keeps both keywords and fields vertically aligned.
- jerrysievert 8y agoI do too, but it breaks down on me for some specific: * ORDER BY * INNER JOIN etc
- FelipeCortez 8y agosql-indent for Emacs supports that: https://github.com/alex-hhh/emacs-sql-indent https://github.com/alex-hhh/emacs-sql-indent
- zeroimpl 8y agoI 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...
- ComputerGuru 8y agoI’ve adopted the same format myself, it reminds me of aligning numbers at the decimal point.
- mjibson 8y agosqlfmt gets pretty close to this: https://sqlfum.pt/?n=30&indent=4&spaces=1&simplify=1&align=2&sql=ICBTRUxFQ1QgZmllbGQsIG90aGVyX2ZpZWxkCiAgICBGUk9NIHRibAogICBXSEVSRSBzb21lX3RoaW5nIElOICgKICAgICAgICAgU0VMRUNUIGlkIAogICAgICAgICAgIEZST00gb3RoZXJfdGFibGUKICAgICAgICAgKQogICAgIEFORCBvdGhlcl9maWVsZCBJUyBOT1QgTlVMTA%3D%3D https://sqlfum.pt/?n=30&indent=4&spaces=1&simplify=1&align=2...
- omaranto 8y agoI'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.
- codefined 8y agoIt'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.
- andreareina 8y agoI think the beautifier part refers to it applying syntax highlighting to the web output.
- mcrad 8y agoAm I the only one irked by the concept of beautiful SQL? SQL is beautiful by itself. Pick a style (indents,caps,whatever) that fits the project at hand. I have found it useful to write the more mundane parts of a project like a regular paragraph with minimal whitespace, and do more whitespace in areas that are more complex or more likely need attention later. Depending too much on a standard style (ironically) lends itself to some pretty ugly code, eg. tangling too many joins and subqueries that would be far better handled by separate statements.
- hnxx 8y agoI 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.
- mjibson 8y agohttps://www.cockroachlabs.com/blog/sql-fmt-online-sql-formatter/ 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.