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

Indentation levels #334

Closed alexeysamoilov closed 8 months ago

alexeysamoilov commented 8 months ago

Thank you for this tool. I'm using the version 5.5. I don't understand the logic of allocating indentation levels.

For еxample:

CREATE OR REPLACE FUNCTION loader_os(OUT o_rc INTEGER, OUT o_err CHARACTER VARYING, IN i_acctoken CHARACTER VARYING, IN i_os  TEXT)
  RETURNS record
  AS $$
  -- Description
DECLARE
  v_os BIGINT;
  v_id BIGINT;
BEGIN
  SELECT * INTO o_rc, o_err
  FROM loader_add(i_acctoken);

  IF o_rc != 0 THEN
    o_err = '(): ' || o_err;
  END IF;

  SELECT ost_id INTO STRICT v_os
  FROM os_form
  WHERE UPPER(ost_form) = UPPER(i_os);

  SELECT os_id INTO v_id
  FROM os_get(v_os);

  UPDATE
    mbile
  SET os_id = 1
  WHERE id = v_id;

  UPDATE
    mbsim
  SET reg_id = 2
  WHERE id = v_id;
END
$$ LANGUAGE plpgsql;

Output after formatting:

CREATE OR REPLACE FUNCTION loader_os(OUT o_rc INTEGER, OUT o_err CHARACTER VARYING, IN i_acctoken CHARACTER VARYING, IN i_os TEXT)
  RETURNS record
  AS $$
  -- Description
DECLARE
  v_os BIGINT;
  v_id BIGINT;
BEGIN
  SELECT * INTO o_rc,
    o_err
  FROM loader_add(i_acctoken);
    IF o_rc != 0 THEN
      o_err = '(): ' || o_err;
    END IF;
    SELECT ost_id INTO STRICT v_os
    FROM os_form
    WHERE UPPER(ost_form) = UPPER(i_os);

      SELECT os_id INTO v_id
      FROM os_get(v_os);

        UPDATE
          mbile
        SET os_id = 1
        WHERE id = v_id;

          UPDATE
            mbsim
          SET reg_id = 2
          WHERE id = v_id;
END
$$
LANGUAGE plpgsql;

Why it depends on the comment at the top befor DECLARE (-- Description) It can be fixed?

darold commented 8 months ago

Commit 6b5e4f0 might fix this issue.