April 13th, 2018 by depesz | Tags: , , , , , | 2 comments »
Did it help? If yes - maybe you can help me?

Some time ago I removed paste.depesz.com because of some problems with underlying formatting library.

Now, it's back. New code, new approach, hopefully usable site.

So, previously I was using pgFormatter library, which was using pretty smart regexps to format SQL. But this library was improved in a non-backward-compatible way, and I couldn't really work on paste.depesz.com (then) to fix the problem.

Now, I got some time, and figured I'll look for some other approach.

I found great library pg_query – it's a ruby gem that wraps some magical C code, that is extracted from PostgreSQL sources. Long story short – it doesn't do its work using regexps. It uses proper parsing, and output from parser is normal parse tree.

It even consists of a method to print back SQL query from parse tree. This is what I used in my approach to make it print the query in somewhat nice way. Well, at least to my eyes it's pretty 🙂

At the moment, my patch with beautification code has not yet been merged, but I hope it eventually will (possibly with some changes.

In any way – I got my own clone of the library, with my beautification-related code, and am using it now at paste.depesz.com to format your queries.

The thing is that previously if pretty printing failed – it could have printed the queries badly indented or something. Now, if it fails – it just doesn't print anything in the “prettyfied" part of paste/show.

For now – it's should be OK – you can always see the original version of the query, even if pretty-printing fails.

I will be checking periodically to see what kind of queries fail on pretty printing, and will do my best to fix these problems.

  1. 2 comments

  2. # gotar
    Apr 14, 2018

    Remove the spam from the history:)

  3. # Lele Gaifax
    Apr 15, 2018

    Yes, pg_query is pretty good indeed!

    Just FYI, I implemented a somewhat equivalent to your “code beautification” on top of it except I used Python instead: see https://github.com/lelit/pg_query.

Leave a comment