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

on conflict do update (& `language` named column) #349

Open zoriya opened 3 weeks ago

zoriya commented 3 weeks ago

I found two issues. First, #150 which seems to occur again. The second is each time I reference a table named language, the formatter inserts a newline.

Here is my query:

insert into videos(sha, idx, title, language, codec, mime_codec, width, height, is_default, bitrate)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx) do update set
    sha = excluded.sha,
    idx = excluded.idx,
    title = excluded.title,
    language = excluded.language,
    codec = excluded.codec,
    mime_codec = excluded.mime_codec,
    width = excluded.width,
    height = excluded.height,
    is_default = excluded.is_default,
    bitrate = excluded.bitrate;

and it gets formatted like:

insert into videos (sha, idx, title,
    language, codec, mime_codec, width, height, is_default, bitrate)
    values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx)
    do update set
        sha = excluded.sha, idx = excluded.idx, title = excluded.title,
        language =
        excluded.language, codec = excluded.codec, mime_codec = excluded.mime_codec, width = excluded.width, height = excluded.height, is_default = excluded.is_default, bitrate = excluded.bitrate;

notice how there's a newline before every language. if I remove each reference to this table, i get this:

insert into videos (sha, idx, title, codec, mime_codec, width, height, is_default, bitrate)
    values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx)
    do update set
        sha = excluded.sha, idx = excluded.idx, title = excluded.title, codec = excluded.codec, mime_codec = excluded.mime_codec, width = excluded.width, height = excluded.height, is_default = excluded.is_default, bitrate = excluded.bitrate;

and I'd expect every statement in do update set to be on a single line (like in my initial example or what was talked in #150).

darold commented 2 weeks ago

Thanks for the report #150 is solves but language is recognize as a plpgsql keyword which should not happen. I'll have a look

zoriya commented 2 weeks ago

are you sure #150 is fixed? i tested both locally and on the website and both gave this sql:

insert into videos (sha, idx, title, codec, mime_codec, width, height, is_default, bitrate)
    values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx)
    do update set
        sha = excluded.sha, idx = excluded.idx, title = excluded.title, codec = excluded.codec, mime_codec = excluded.mime_codec, width = excluded.width, height = excluded.height, is_default = excluded.is_default, bitrate = excluded.bitrate;

i'd expect it to format like this:

insert into videos(sha, idx, title, codec, mime_codec, width, height, is_default, bitrate)
    values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx)
    do update set
        sha = excluded.sha,
        idx = excluded.idx,
        title = excluded.title,
        codec = excluded.codec,
        mime_codec = excluded.mime_codec,
        width = excluded.width,
        height = excluded.height,
        is_default = excluded.is_default,
        bitrate = excluded.bitrate;
darold commented 2 weeks ago

You are right, I will fix it again in the same commit.

darold commented 2 weeks ago

Fixed by commit 5c7463d.

Note that this patch break the form:

...
    DO UPDATE SET
        (a, b, c) = (excluded.a, excluded.b, excluded.c);

I will try to fix this later.