TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Sqlfmt: an opinionated online SQL formatter

207 pointsby mjibsonover 6 years ago

21 comments

justin_oaksover 6 years ago
Tangentially related. I&#x27;m of the opinion that SQL shouldn&#x27;t be uppercased. I don&#x27;t want to hit shift or caps lock and I find lowercase easier to read.<p>Are there others out there like me? I assume I&#x27;m in the minority, but I don&#x27;t understand why. People stopped uppercasing HTML tags, why can&#x27;t we do the same with SQL?
评论 #18089020 未加载
评论 #18090016 未加载
评论 #18089523 未加载
评论 #18090372 未加载
评论 #18091677 未加载
评论 #18089222 未加载
评论 #18089075 未加载
评论 #18088741 未加载
评论 #18089412 未加载
评论 #18091726 未加载
评论 #18089269 未加载
评论 #18089169 未加载
评论 #18091396 未加载
评论 #18088915 未加载
mjibsonover 6 years ago
Author here. Based on feedback I am going to make a sqlfmt binary for easy editor integration.<p>You can currently already do this by using the CockroachDB binary (<a href="https:&#x2F;&#x2F;www.cockroachlabs.com&#x2F;docs&#x2F;releases&#x2F;v2.1.0-beta.20180924.html" rel="nofollow">https:&#x2F;&#x2F;www.cockroachlabs.com&#x2F;docs&#x2F;releases&#x2F;v2.1.0-beta.2018...</a>) but understandably people want a more minimal solution.
评论 #18089081 未加载
评论 #18092336 未加载
评论 #18090776 未加载
sbuttgereitover 6 years ago
If you&#x27;re working with something close to PostgreSQL syntax support, why wouldn&#x27;t you start with pgFormatter (<a href="http:&#x2F;&#x2F;sqlformat.darold.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;sqlformat.darold.net&#x2F;</a>)? It&#x27;s been around for awhile and I can&#x27;t see anything new or substantially more advanced in the proposed solution.<p>I have to say, I do most of my development work in the database, and I&#x27;m not thrilled with any of the automatic formatters thus far. The ones I&#x27;ve seen can do a good job in many cases, but if you start getting very advanced at all, the weaknesses of an automatic formatting approach start to become more evident. It may just be that anything other than relatively simple SQL, PL&#x2F;SQL are not well suited for automatic formatting.<p>I&#x27;m still working through a good set of formatting rules, and I always find I end up making exceptions for readability.
评论 #18088021 未加载
fefe23over 6 years ago
Brief question, only tangentially related:<p>Is it me or is &quot;opinionated&quot; software popping up all over the place? It looks to me like the author considers it a selling point, too.<p>Can someone explain to me why I would want an &quot;opinionated&quot; software over any regular old software? Especially over a configurable software?<p>I&#x27;m not a native speaker, but I never encountered &quot;opinionated&quot; being used with positive connotation. It has so far always been in the &quot;well, uh, he&#x27;s very opinionated!&quot; sense of apologizing for that old grumpy person who gets on everybody&#x27;s nerves.
评论 #18088708 未加载
评论 #18088618 未加载
评论 #18088675 未加载
评论 #18088636 未加载
评论 #18088691 未加载
评论 #18088612 未加载
评论 #18088828 未加载
评论 #18091432 未加载
gwbas1cover 6 years ago
Great idea.<p>I work on a project that&#x27;s grown in complexity and team size. Developers have come and gone. Sometimes a bit of code that&#x27;s grown over a few years needs its style to evolve. This kind of formatter clearly realizes this.<p>What I tell developers that complain about this or that style is that it&#x27;s more important to have a consistent code style throughout the lifetime of a project, then to adopt whatever bracing style someone happens to like at the time.<p>I&#x27;d really like all of my code to be automatically formatted with a tool like this, as part of the build process. This would help with situations where code evolves, as it would reformat it when needed.
ianmcgowanover 6 years ago
The problem with opinions is everyone has a different one.<p>I like <a href="https:&#x2F;&#x2F;www.sqlinform.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.sqlinform.com&#x2F;</a> - keep a copy of notepad around just so I can use it. Wish there was a VSCode extension. The free version was good enough for my needs, but it&#x27;s so handy I upgraded to the pro version just to show appreciation. The best part is the ability to really customize the layout and save the settings. It&#x27;s always step #1 when someone sends me a massive blob of badly formatted SQL.
评论 #18089250 未加载
clarkdaveover 6 years ago
This looks great! I notice you mentioned Prettier in the post — do you have any plans to integrate sqlfmt with that?<p>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.
评论 #18090401 未加载
JelteFover 6 years ago
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&#x27;t support it so I fallback to leading ones there.
评论 #18089381 未加载
评论 #18091394 未加载
评论 #18089958 未加载
llimllibover 6 years ago
I&#x27;m sad this isn&#x27;t available as a binary, it&#x27;s not much use to me as a website.
评论 #18087884 未加载
评论 #18087929 未加载
PaulJuliusover 6 years ago
I was just wishing for a better SQL formatter! I&#x27;m wasting too much time pasting queries into a doc just to reformat them.<p>It would be great if the parser supported question marks as placeholders. The pg_stat_statements table in Postgres saves denormalized queries, replacing the literal values with &#x27;?&#x27; placeholders (e.g., `SELECT * FROM table_name WHERE id = ?`).
blairandersonover 6 years ago
I love this. Now to implement as:<p>- javascript lib &#x2F; (free atom plugin with this)<p>- jetbrains plugin<p>- vim plugin
fiatjafover 6 years ago
What I want: an SQL (actually, it could be just Postgres as I only use that) parser that takes two schema declarations and calculates the commands needed to go from one to the other, so we can have automatic migrations.<p>For example, if A is<p><pre><code> CREATE TABLE person ( id serial PRIMARY KEY, name text ) </code></pre> and B is<p><pre><code> CREATE TABLE person ( id serial PRIMARY KEY, name text, age int ) </code></pre> Then this tool would output<p><pre><code> ALTER TABLE person ADD COLUMN age int</code></pre>
DaiPlusPlusover 6 years ago
I’d love it if StackOverflow would let me select code text and automatically reformat it according to some standard spec (I don’t care which set of opinions are used provided it’s readable!) - too many inexperienced (or unqualified, it seems) people copy and paste ad-hoc written SQL that I always have to manually reformat myself when I edit their questions for readability.
amakeover 6 years ago
I would love to package this (most likely from <a href="https:&#x2F;&#x2F;github.com&#x2F;lopezator&#x2F;sqlfmt" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lopezator&#x2F;sqlfmt</a>) for my package manager of choice. What is the license?
评论 #18091624 未加载
weaksauceover 6 years ago
that would be really cool to have as a local package you can plug into your text editor instead of copy and paste. people using this at companies might leak sensitive information or possible attack vectors to the opaque service.
评论 #18087895 未加载
gfodyover 6 years ago
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&#x2F;spaces.
评论 #18089966 未加载
kyberiasover 6 years ago
My opinion: text should never be aligned to right as the keywords here are.
评论 #18091783 未加载
qaqover 6 years ago
standalone Query parser would be nice :)
评论 #18088018 未加载
keithnzover 6 years ago
not sure what this offers over<p><a href="https:&#x2F;&#x2F;sqlformat.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sqlformat.org&#x2F;</a><p>or<p><a href="http:&#x2F;&#x2F;www.dpriver.com&#x2F;pp&#x2F;sqlformat.htm" rel="nofollow">http:&#x2F;&#x2F;www.dpriver.com&#x2F;pp&#x2F;sqlformat.htm</a><p>both of which seem to give nicer output and handle more diverse sql without erroring.<p>also not really sure why it considers itself &quot;opinionated&quot;
WorldMakerover 6 years ago
Why all the SCREAMING? Is something wrong?<p>It&#x27;s 2018, we aren&#x27;t using teletypes with bad shift keys and we all have syntax coloring IDEs, we don&#x27;t need to scream out all our KEYWORDS like we were still writing COBOL or FORTRAN in the 1960s.<p>It&#x27;s become my biggest pet peeve with most SQL formatters. I think sentence cased SQL is far more readable.<p><pre><code> Select * from sometable where SomeColumn &gt; 3 </code></pre> But I realize I&#x27;m a minority with the opinion right now.
评论 #18088963 未加载
评论 #18089052 未加载
评论 #18091399 未加载
datavirtueover 6 years ago
&quot;As my code gets more complicated or deeplier nested&quot;<p>OMG will someone please tell college professors about Clean Code? Perhaps it takes years of writing deeplier nested code before you even know what Uncle Bob is talking about.