A self contained SQL parser/pretty printer

For a great part of the day, I work as an backend developer, dealing with writing and reviewing all kinds of SQL to move data in commercial(a.k.a. enterprise) applications. For that purpose, using a tool that formats SQL statements is essential, in order to keep your code as neat as possible and easier to mantain.

I've used some commercial and freemium applications such as SQLInform, and also sites like http://sqlformat.org/ and http://www.dpriver.com/pp/sqlformat.htm from time to time. But always felt that there is no need to use this kind of "heavy" applications for something that could be done entirely in JavaScript, self-contained, and possibly, evolving through Open Source.

So I ended up writing a simple SQL formatter/pretty printer that works in a browser, with no need for server-side processing.

You can find it here: http://codespeak.cc/sequeljs

Working entirely on the browser, SequelJS has the additional benefit that developers aren't forced to disclose details of schema design or business issues to third parties, because there isn't any data transferred through the ether when formatting statements.

Currently, the formatter still lacks important features (like parsing comments or establishing a max line width), but can handle an nice subset of SELECT queries, with joins, subquerys, expressions, function calls, etc. The current style of indentation is just what I'm accustomed to, but is something that will be possible to tune in future releases.

When building a program to process SQL statements, it's important to settle on which set of possible statements are accepted as valid, and also recognize their structure. In compilers jargon, this is called a grammar.

Some SQL grammars can get tough quickly. Parsing real-life SQL statements (like PostgreSQL/MSSQL ones) —even if only to format them— is a difficult task. So I started from scratch, using the H2 SQL grammar as a foundation, which I believe is a really nice basis for a design that can be evolved organically.

SequelJS uses the great Jison library, which is a JavaScript port of the Bison LALR parser. Color formatting is done with the prettify library.

I plan to keep adding new features in the following days. If you want to contribute, feel free to fork the project on GitHub.


Comments powered by Disqus