Is it possible to ROLLBACK TO SAVEPOINT
with CASE?
My query is
BEGIN;
SAVEPOINT my_savepoint;
INSERT INTO DPoint (uuid) VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214')
ON CONFLICT (uuid) DO NOTHING;
WITH
ins1 AS (INSERT INTO Point (latitude, longitude, srid)
VALUES (37.251667, 14.917222, 4326) RETURNING id),
ins2 as (INSERT INTO SPoint (idPt, uuiddpt)
VALUES ((SELECT id FROM ins1), '5547f4b7-00b3-4aac-8ceb-c9ca163a0214') RETURNING id),
ins3 as (INSERT INTO Distance (idSpt, uuiddpt)
VALUES ((SELECT id FROM ins2), '5547f4b7-00b3-4aac-8ceb-c9ca163a0214'))
INSERT INTO DPointTS (uuid, type, name, idPoint)
VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214', NULL, NULL, (SELECT id FROM ins1));
SELECT CASE WHEN
(SELECT uuid FROM DPoint
WHERE uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214' )
is not NULL THEN ROLLBACK TO SAVEPOINT my_savepoint END;
COMMIT;
My idea is:
When trying to insert once again DPoint.uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214', it is no need to insert Point, SPoint, Distance, DPointTS. So I would like to ROLLBACK these insertions to my_savepoint in transaction. Maybe any idea in what way I have to rewrite my code?
EDIT:
SELECT uuid IS NULL AS is_not_uuid FROM DPoint WHERE uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214';
\gset
\if :is_not_uuid
\echo 'insert row to DPoint'
INSERT INTO DPoint (uuid) VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214');
...
my INSERT query
\endif
I update my strategy without SAVEPOINTs - if SELECT query returns TRUE
I evaluate all insertions. What way I am execute the query, only in command line? When trying in console.sql in DataGRIP it throws an error - it honestly execute all the rows and fails in INSERT INTO DPoint (uuid)...
in case the point is already exists. I would like to execute the statements in one way