Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The formatter doesn't handle some blocks correctly #828

Open
Boiethios opened this issue Jun 14, 2021 · 1 comment
Open

The formatter doesn't handle some blocks correctly #828

Boiethios opened this issue Jun 14, 2021 · 1 comment

Comments

@Boiethios
Copy link

Describe the bug
Some blocks like BEGIN/END or IF/END IF are not handled.

To Reproduce
This code is formatted like that:

CREATE OR REPLACE PROCEDURE lock_object(
        IN obj_uid UUID,
        IN id_user VARCHAR(255)
    ) AS $$
DECLARE found_id_user VARCHAR(255);

BEGIN found_id_user := (
    SELECT locked_objects.id_user
    FROM locked_objects
    WHERE locked_objects.uid = obj_uid
);

IF id_user <> found_id_user THEN raise exception 'The object % is already locked by someone else',
obj_uid;

END IF;

UPDATE locked_objects
SET instant = CURRENT_TIMESTAMP
WHERE uid = obj_uid;

ELSE
INSERT INTO locked_objects (uid, id_user)
VALUES (obj_uid, id_user);

END IF;

COMMIT;

END;

$$ LANGUAGE plpgsql;

Expected behavior
It should look something like that:

CREATE OR REPLACE PROCEDURE lock_object(
        IN obj_uid UUID,
        IN id_user VARCHAR(255)
    ) AS $$
DECLARE
    found_id_user VARCHAR(255);
BEGIN
    found_id_user := (
        SELECT locked_objects.id_user
        FROM locked_objects
        WHERE locked_objects.uid = obj_uid
    );

    IF found_id_user IS NOT NULL THEN
        IF id_user <> found_id_user THEN
            raise exception 'The object % is already locked by someone else', obj_uid;
        END IF;

        UPDATE locked_objects
        SET instant = CURRENT_TIMESTAMP
        WHERE uid = obj_uid;

    ELSE
        INSERT INTO locked_objects (uid, id_user)
        VALUES (obj_uid, id_user);

    END IF;

    COMMIT;
END;
$$ LANGUAGE plpgsql;
@skellogg
Copy link

skellogg commented Oct 4, 2022

I find the same...

formatted output of test:

SELECT line
FROM tsttable IF(@id <> 0) BEGIN
INSERT INTO tsttable(line1, line2)
SELECT line1,
line2
FROM tsttbl2
END

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants