Show HN: Readsql – convert SQL to most human readable format
github.com
github.com
If you want to make SQL easier to understand then take a look at a 15 year old stored procedure that’s been hacked at by a dozen devs is over 1,000 lines long, has sketchy rollback error protection, uses two CTEs, a pivot, no temp tables and does some xml shenanigans in the middle (I’m being hyperbolic obviously)
This feels like trying to loose weight by trimming your toe nails, you’re technically lighter but not so as it matters.
However, the tool might suggest improvements on SQL code in the future
Because SQL (broadly) was designed to be "human" readable in the first place, it's grammatically very inconsistent and with a lot of keywords. Much more than other languages in use today such as C.
I've yet to find a pattern of indentation, brackets etc. that satisfies my OCD.
Coming up with an example of a nicely formatted SQL statement is not difficult, but turning that into consistent 'rules' and immediately you find counterexamples using other parts of SQL.
I tend to classify SQL statements into two kinds, those that when wrapped in a calling function fit in one screen, and those other longer ones that I'm inclined to write in an imperative language.
Edit: For the author of the repository, the list of reserved words gets longer and more complex when you support different implementations of SQL, and regex may be insufficient once you consider such parsing questions as whether the keyword is within quotation marks or part of a user-defined name.
https://www.drupal.org/docs/develop/coding-standards/list-of...
https://github.com/AzisK/readsql/blob/master/readsql/regexes...
In fact it is a result of theoretical computer science that you _cannot_ correctly parse languages like SQL, HTML, Python, etc. with regular expressions: Any attempt to do anything non-trivial will have cases where it misunderstands the code.
So you would want to find a SQL grammar (an outdated example in [1]) and a module[2] that can use this to parse queries into a data structure to which you can apply transformations (e.g., changing case of keyword tokens) and then write back out as a string.
SQLite's documentation has some nice diagrams[3] to get an idea for how it parses a query string. The table of links at the top lets you dive into, e.g., all the optional parts of a SELECT statement.
1: https://ronsavage.github.io/SQL/sql-92.bnf.html
As a heavy SQL user, I don't see much benefit in this as of yet. It is very misleading in saying it is the "most human readable" format, when the example shows the "format" to be identical to the original. Just by upper-casing keywords doesn't make it any more readable to be honest.
Anyways a good attempt, hope you're not offended by critical feedbacks and hope they are useful for some ideas to improve the tool.
A difficult but incredibly useful idea would be to learn the developer’s style and then format code (theirs or others’) to fit that model.
I also normalize by removing optional/redundant keyword noise, I'm looking at you INNER/OUTER.
In any case, it is objectively not more human writeable.
If we wrote:
select(col1, col2, col3, ..., from=table_name, limit=5)
or something like that, it's obvious what are the intrinsics and what are the variable parts without knowing anything about select but for SQL you need to know all the "sentence patterns".This has to do with the outline of letters in uppercase being indistinct (if you trace an outline - especially with serif fonts - you’ll largely just get a block) so you need to spend more time per-letter to distinguish the characters whereas with lowercase, the “fitted box” shape is shared between fewer letters: https://www.sciencedirect.com/science/article/pii/S004269890...
"Under US law, disclaimers must be 'conspicuous'" https://law.stackexchange.com/a/18210