darold / pgFormatter

A PostgreSQL SQL syntax beautifier that can work as a console program or as a CGI. On-line demo site at http://sqlformat.darold.net/
PostgreSQL License
1.69k stars 101 forks source link

A handful of formatting issues I've discovered recently relating to anonymous code blocks, comments, and multi-row inserts statements. #317

Open paulgelardi opened 1 year ago

paulgelardi commented 1 year ago

I noticed a handful of issues and I wanted to bring them to attention. I'm unfamiliar with Perl so it'd be a bit much to make the fixes myself - I'd be happy to be a tester if needed.

Command line arguments specified:

Issues noticed:

I'm unsure how much of this is related to the use of an anonymous code block, but that does help highlight the indentation issue - it may also occur elsewhere. I have confirmed these issues still occur in the latest version of Beautify.pm as of the time of this submission.

I've prepared some SQL below to demonstrate this

Expected output (also the input):

DO $$
DECLARE
    _variable int := 42;
BEGIN

    -- This is a comment
    CREATE TEMP TABLE IF NOT EXISTS tempy_mc_tempface (
        test text
    );
    TRUNCATE TABLE tempy_mc_tempface;

    INSERT INTO tempy_mc_tempface
    VALUES
        ('tempy'),
        ('mc'),
        ('tempface');

    -- Another Comment
    INSERT INTO tempy_mc_tempface
        VALUES ('another');

END
$$;

Actual output:

DO $$
DECLARE
    _variable int := 42;
BEGIN
    -- This is a comment
    CREATE TEMP TABLE IF NOT EXISTS tempy_mc_tempface (
        test text
    );
    TRUNCATE TABLE tempy_mc_tempface;

INSERT INTO tempy_mc_tempface
    VALUES ('tempy'),
    ('mc'),
    ('tempface');
    -- Another Comment
    INSERT INTO tempy_mc_tempface
        VALUES ('another');

END
$$;