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.68k stars 100 forks source link

Another formatting problem #152

Closed nagylzs closed 4 years ago

nagylzs commented 4 years ago
DO $body$
DECLARE
VALUES jsonb[];
    result jsonb;
    rec record;
BEGIN
    VALUES = ARRAY[]::jsonb[];
    SELECT id, login_name INTO rec
    FROM test.users
    WHERE id = 1;
VALUES = array_append(
        VALUES, to_jsonb (rec));
VALUES = array_append(
        VALUES, to_jsonb (rec));
    result = to_jsonb (
        VALUES);
    raise notice '%', result;
END
$body$;

It is formatted as shown above. The " values = array_append" line does not have an ident.

nagylzs commented 4 years ago

Command line was:

/usr/bin/pg_format --wrap-after 5 ${file}

nagylzs commented 4 years ago

pg_format version was 4.1

nagylzs commented 4 years ago

I think the problem is that "VALUES" is also a keyword, but it is used as a variable name inside the code. Probably this is not something that should be addressed in pg_formatter. I think it should be forbidden by PostgreSQL itself.

darold commented 4 years ago

Yes got it this morning too in a different code but I think this the same problem:

CREATE FUNCTION tg_phone_bu ()
    RETURNS TRIGGER
    AS $$
BEGIN
    IF new.slotname != old.slotname THEN
        DELETE FROM PHone
        WHERE slotname = old.slotname;
        INSERT INTO PHone (slotname, COMMENT,
            slotlink)
    VALUES (new.slotname,
        new.comment,
        new.slotlink);
        RETURN NULL;
    END IF;
    RETURN new;
END;
$$
LANGUAGE plpgsql;
darold commented 4 years ago

Fixed in latest development code with commits84e7728 and fe92cb4.