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

wrong format for FUNCTION with SET variable FROM current #332

Closed bobnil closed 7 months ago

bobnil commented 7 months ago

When creating functions, you can specify variables with SET name FROM CURRENT to get the value of the parameter that is current when CREATE FUNCTION is executed.

pg_format is unaware of this syntax and treats FROM as if it were FROM from a query.

CREATE FUNCTION name ()
  RETURNS text
  LANGUAGE plpgsql
  SET search_path
FROM
  current
  AS $$
BEGIN
  RETURN 'text';
END;
$$;

Expected output:

CREATE FUNCTION name ()
  RETURNS text
  LANGUAGE plpgsql
  SET search_path FROM current
  AS $$
BEGIN
  RETURN 'text';
END;
$$;

I don't know enough perl to fix it, but by trial and error I found that line 2097 in Beautify.pm has something to do with inserting the line feed:

2097         elsif ( $token =~ /^(?:FROM|WHERE|SET|RETURNING|HAVING|VALUES)$/i )
darold commented 7 months ago

Commit 6ef53a3 fixes this issue.