sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.32k stars 398 forks source link

[FORMATTING] function name should not be changed in pgsql #710

Closed thilllon closed 8 months ago

thilllon commented 8 months ago

Hi, thanks for the great work.

$function$ should not be changed after formatting the code below:

Input data

Which SQL and options did you provide as input?

-- original code

CREATE OR REPLACE FUNCTION public.handle_new_user()
 RETURNS trigger
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$
begin
  insert into public.profiles (id, full_name, avatar_url)
  values (new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url');
  return new;
end;
$function$
;

Expected Output

CREATE
    OR REPLACE FUNCTION public.handle_new_user() RETURNS trigger
    LANGUAGE plpgsql
    SECURITY DEFINER AS
$function$
begin
    insert into public.profiles (id, full_name, avatar_url)
    values (new.id,
            new.raw_user_meta_data ->> 'full_name',
            new.raw_user_meta_data ->> 'avatar_url');

    return new;
end;
$function$;

Actual Output


CREATE
OR REPLACE FUNCTION public.handle_new_user() RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER AS $ function $ begin
-- Error occurs here `$ function $` 
insert into
  public.profiles (id, full_name, avatar_url)
values
  (
    new.id,
    new.raw_user_meta_data ->> 'full_name',
    new.raw_user_meta_data ->> 'avatar_url'
  );

return new;

end;

$ function $;

Usage

nene commented 8 months ago

Thanks for reporting, but I'm unable to reproduce this issue.

Make sure you have selected PostgreSQL as the SQL dialect you want to format: https://github.com/sql-formatter-org/sql-formatter#parse-error-unexpected--at-line-

thilllon commented 8 months ago

My bad. It worked after setting this, "SQL-Formatter-VSCode.dialect": "postgresql" Thanks!